From 5be7a32e6ab0c360ac93a1e0785ef02e06800374 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Mon, 19 Oct 2026 18:02:57 +0200 Subject: [PATCH 01/60] refactor(cloud): extract public IPs service in CAPG style --- cloud/cleanup.go | 21 ++ cloud/cleanup_test.go | 20 ++ cloud/client.go | 20 +- cloud/errors.go | 33 +- cloud/fake/client.go | 349 +++++++++++++++---- cloud/sdk_client.go | 284 +++------------ cloud/sdk_client_test.go | 19 +- cloud/services/publicip/reconcile.go | 248 ++++++++++++++ cloud/services/publicip/reconcile_test.go | 360 ++++++++++++++++++++ cloud/services/publicip/service.go | 64 ++++ cloud/services/shared/errors.go | 44 +++ cloud/{ => services/shared}/errors_test.go | 2 +- cloud/services/shared/types.go | 28 ++ cloud/services/shared/utils.go | 96 ++++++ cloud/types.go | 28 +- controller/stackitcluster_bastion.go | 44 ++- controller/stackitcluster_infrastructure.go | 14 +- scope/cluster_scope.go | 57 +++- test/e2e/e2e_test.go | 5 +- util/conditions.go | 3 +- 20 files changed, 1364 insertions(+), 375 deletions(-) create mode 100644 cloud/services/publicip/reconcile.go create mode 100644 cloud/services/publicip/reconcile_test.go create mode 100644 cloud/services/publicip/service.go create mode 100644 cloud/services/shared/errors.go rename cloud/{ => services/shared}/errors_test.go (98%) create mode 100644 cloud/services/shared/types.go create mode 100644 cloud/services/shared/utils.go diff --git a/cloud/cleanup.go b/cloud/cleanup.go index 9f155d1..3d635e0 100644 --- a/cloud/cleanup.go +++ b/cloud/cleanup.go @@ -15,6 +15,8 @@ import ( "fmt" "maps" "strings" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" ) // CleanupByTags deletes provider-managed cloud resources matching tags without @@ -29,6 +31,25 @@ func CleanupByTags(ctx context.Context, client Client, tags map[string]string) e bastionTags := make(map[string]string, len(tags)+1) maps.Copy(bastionTags, tags) bastionTags["cluster-api-provider-stackit/resource-role"] = "bastion" + bastionServers, err := client.ListServersByTags(ctx, bastionTags) + if err != nil { + errs = append(errs, err.Error()) + } + publicIPs, err := publicip.ListByTags(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), tags) + if err != nil { + errs = append(errs, err.Error()) + } else { + for _, pubIP := range publicIPs { + for _, server := range bastionServers { + if err := publicip.DetachFromServer(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), server.ID, pubIP.ID); err != nil { + errs = append(errs, fmt.Sprintf("detach public IP %q from server %q: %v", pubIP.ID, server.ID, err)) + } + } + if err := publicip.DeleteByID(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), pubIP.ID); err != nil { + errs = append(errs, fmt.Sprintf("delete public IP %q: %v", pubIP.ID, err)) + } + } + } if err := client.DeleteBastion(ctx, BastionInput{Tags: bastionTags}, Bastion{}); err != nil && !IsNotFound(err) { errs = append(errs, fmt.Sprintf("delete bastion resources: %v", err)) } diff --git a/cloud/cleanup_test.go b/cloud/cleanup_test.go index 6ef2778..40f2304 100644 --- a/cloud/cleanup_test.go +++ b/cloud/cleanup_test.go @@ -36,6 +36,12 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { if _, err := client.CreateServer(ctx, cloud.CreateServerInput{Name: "other", Tags: otherTags}); err != nil { t.Fatalf("CreateServer() error = %v", err) } + if _, err := client.CreatePublicIP(ctx, tags); err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + if _, err := client.CreatePublicIP(ctx, otherTags); err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } if _, err := client.EnsureAPIServerLoadBalancer(ctx, cloud.LoadBalancerInput{ Name: "matched", Tags: tags, @@ -86,4 +92,18 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { if len(matchedLoadBalancers) != 0 { t.Fatalf("matched load balancer count = %d, want 0", len(matchedLoadBalancers)) } + matchedPublicIPs, err := client.ListPublicIPsByTags(ctx, tags) + if err != nil { + t.Fatalf("ListPublicIPsByTags() error = %v", err) + } + if len(matchedPublicIPs) != 0 { + t.Fatalf("matched public IP count = %d, want 0", len(matchedPublicIPs)) + } + otherPublicIPs, err := client.ListPublicIPsByTags(ctx, otherTags) + if err != nil { + t.Fatalf("ListPublicIPsByTags(other) error = %v", err) + } + if len(otherPublicIPs) != 1 { + t.Fatalf("other public IP count = %d, want 1", len(otherPublicIPs)) + } } diff --git a/cloud/client.go b/cloud/client.go index a3ab870..7e5bd73 100644 --- a/cloud/client.go +++ b/cloud/client.go @@ -10,7 +10,17 @@ You may obtain a copy of the License at package cloud -import "context" +import ( + "context" + + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" +) + +// Reconciler is a generic interface used by components offering a type of service. +type Reconciler interface { + Reconcile(ctx context.Context) error + Delete(ctx context.Context) error +} // Client is the abstraction over STACKIT cloud APIs used by the controllers. // @@ -23,6 +33,12 @@ import "context" // - be idempotent (CreateServer/EnsureAPIServerLoadBalancer must not produce // duplicates when a resource with matching tags already exists) type Client interface { + ProjectID() string + + Region() string + + IaaSClient() *iaas.APIClient + GetServer(ctx context.Context, id string) (*Server, error) FindServerByTags(ctx context.Context, tags map[string]string) (*Server, error) @@ -43,8 +59,6 @@ type Client interface { DeleteNodeSSHAccess(ctx context.Context, tags map[string]string) error - ListPublicIPsByTags(ctx context.Context, tags map[string]string) ([]*PublicIP, error) - ListSecurityGroupsByTags(ctx context.Context, tags map[string]string) ([]*SecurityGroup, error) EnsureAPIServerLoadBalancer(ctx context.Context, input LoadBalancerInput) (*LoadBalancer, error) diff --git a/cloud/errors.go b/cloud/errors.go index 1aeaa68..e038288 100644 --- a/cloud/errors.go +++ b/cloud/errors.go @@ -10,35 +10,32 @@ You may obtain a copy of the License at package cloud -import "errors" +import "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" -// Sentinel errors returned by Client implementations. Controllers use -// errors.Is to classify them into requeue / condition-update behavior. +// Sentinel errors returned by Client implementations, re-exported from shared. +// Controllers use errors.Is to classify them into requeue / condition-update behavior. var ( - ErrNotFound = errors.New("not found") - ErrUnauthorized = errors.New("unauthorized") - ErrInvalidInput = errors.New("invalid input") - ErrConflict = errors.New("conflict") - ErrTransient = errors.New("transient") + ErrNotFound = shared.ErrNotFound + ErrUnauthorized = shared.ErrUnauthorized + ErrInvalidInput = shared.ErrInvalidInput + ErrConflict = shared.ErrConflict + ErrTransient = shared.ErrTransient ) // IsNotFound reports whether err wraps ErrNotFound. -func IsNotFound(err error) bool { return errors.Is(err, ErrNotFound) } +func IsNotFound(err error) bool { return shared.IsNotFound(err) } // IsUnauthorized reports whether err wraps ErrUnauthorized. -func IsUnauthorized(err error) bool { return errors.Is(err, ErrUnauthorized) } +func IsUnauthorized(err error) bool { return shared.IsUnauthorized(err) } // IsInvalidInput reports whether err wraps ErrInvalidInput. -func IsInvalidInput(err error) bool { return errors.Is(err, ErrInvalidInput) } +func IsInvalidInput(err error) bool { return shared.IsInvalidInput(err) } // IsConflict reports whether err wraps ErrConflict. -func IsConflict(err error) bool { return errors.Is(err, ErrConflict) } +func IsConflict(err error) bool { return shared.IsConflict(err) } // IsTransient reports whether err wraps ErrTransient. -func IsTransient(err error) bool { return errors.Is(err, ErrTransient) } +func IsTransient(err error) bool { return shared.IsTransient(err) } -// IsRetryable reports whether err should trigger a requeue (conflict or -// transient). -func IsRetryable(err error) bool { - return IsConflict(err) || IsTransient(err) -} +// IsRetryable reports whether err should trigger a requeue (conflict or transient). +func IsRetryable(err error) bool { return shared.IsRetryable(err) } diff --git a/cloud/fake/client.go b/cloud/fake/client.go index 06465cc..531ae31 100644 --- a/cloud/fake/client.go +++ b/cloud/fake/client.go @@ -14,12 +14,18 @@ package fake import ( "context" + "encoding/json" "fmt" "maps" + "net/http" + "net/http/httptest" "slices" + "strings" "sync" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/stackit-sdk-go/core/config" + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" ) const ( @@ -36,24 +42,31 @@ type Client struct { publicIPs map[string]*publicIPEntry securityGroups map[string]*securityGroupEntry networks map[string]*cloud.Network + iaasClient *iaas.APIClient nextID int // failure injection: if non-nil, the next call returns this error and the // field is cleared. - FailNextCreateServer error - FailNextDeleteServer error - FailNextGetServer error - FailNextFindServer error - FailNextEnsureLB error - FailNextDeleteLB error - FailNextSetTargets error - FailNextGetNetwork error - FailNextEnsureBastion error - FailNextDeleteBastion error - FailNextEnsureNodeSSH error - FailNextDeleteNodeSSH error - FailNextListAZ error + FailNextCreateServer error + FailNextDeleteServer error + FailNextGetServer error + FailNextFindServer error + FailNextEnsureLB error + FailNextDeleteLB error + FailNextSetTargets error + FailNextGetNetwork error + FailNextEnsureBastion error + FailNextDeleteBastion error + FailNextEnsureNodeSSH error + FailNextDeleteNodeSSH error + FailNextListAZ error + FailNextGetPublicIP error + FailNextListPublicIPs error + FailNextCreatePublicIP error + FailNextDeletePublicIP error + FailNextAddPublicIP error + FailNextRemovePublicIP error // AvailabilityZones is returned by ListAvailabilityZones if non-empty. // Defaults to eu01-1, eu01-2, eu01-3. @@ -113,9 +126,30 @@ func New(networks ...cloud.Network) *Client { n := networks[i] c.networks[n.ID] = &n } + iaasClient, err := iaas.NewAPIClient( + config.WithoutAuthentication(), + config.WithHTTPClient(&http.Client{Transport: roundTripperFunc(c.roundTripIaaS)}), + config.WithEndpoint("http://stackit.fake.local"), + ) + if err != nil { + panic(fmt.Sprintf("create fake iaas client: %v", err)) + } + c.iaasClient = iaasClient return c } +func (c *Client) ProjectID() string { + return "00000000-0000-0000-0000-000000000000" +} + +func (c *Client) Region() string { + return "eu01" +} + +func (c *Client) IaaSClient() *iaas.APIClient { + return c.iaasClient +} + func consume(p *error) error { if *p == nil { return nil @@ -127,7 +161,7 @@ func consume(p *error) error { func (c *Client) genID() string { c.nextID++ - return fmt.Sprintf("fake-%d", c.nextID) + return fmt.Sprintf("00000000-0000-4000-8000-%012d", c.nextID) } func (c *Client) GetServer(_ context.Context, id string) (*cloud.Server, error) { @@ -260,14 +294,11 @@ func (c *Client) EnsureBastion(_ context.Context, input cloud.BastionInput) (*cl } for _, serverEntry := range c.servers { if len(input.Tags) > 0 && mapContains(serverEntry.tags, input.Tags) { - publicIP := c.publicIPByTags(input.Tags) securityGroup := c.securityGroupByTags(input.Tags) return &cloud.Bastion{ ServerID: serverEntry.server.ID, ServerState: serverEntry.server.State, ServerPowerStatus: serverEntry.server.PowerStatus, - PublicIPID: idOfPublicIP(publicIP), - PublicIP: ipOfPublicIP(publicIP), SecurityGroupID: idOfSecurityGroup(securityGroup), }, nil } @@ -300,20 +331,10 @@ func (c *Client) EnsureBastion(_ context.Context, input cloud.BastionInput) (*cl userData: append([]byte(nil), input.CloudInit...), } - publicIPID := c.genID() - publicIP := &cloud.PublicIP{ - ID: publicIPID, - IP: "203.0.113.22", - NetworkInterfaceID: "fake-nic-" + serverID, - } - c.publicIPs[publicIPID] = &publicIPEntry{publicIP: publicIP, tags: copyTags(input.Tags)} - c.EnsureBastionCalls++ return &cloud.Bastion{ ServerID: serverID, ServerState: server.State, - PublicIPID: publicIPID, - PublicIP: publicIP.IP, SecurityGroupID: securityGroupID, }, nil } @@ -328,9 +349,6 @@ func (c *Client) DeleteBastion(_ context.Context, input cloud.BastionInput, stat if status.ServerID != "" { delete(c.servers, status.ServerID) } - if status.PublicIPID != "" { - delete(c.publicIPs, status.PublicIPID) - } if status.SecurityGroupID != "" { delete(c.securityGroups, status.SecurityGroupID) } @@ -339,11 +357,6 @@ func (c *Client) DeleteBastion(_ context.Context, input cloud.BastionInput, stat delete(c.servers, id) } } - for id, entry := range c.publicIPs { - if mapContains(entry.tags, input.Tags) { - delete(c.publicIPs, id) - } - } for id, entry := range c.securityGroups { if mapContains(entry.tags, input.Tags) { delete(c.securityGroups, id) @@ -407,10 +420,31 @@ func (c *Client) DeleteNodeSSHAccess(_ context.Context, tags map[string]string) return nil } +func (c *Client) GetPublicIP(_ context.Context, id string) (*cloud.PublicIP, error) { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextGetPublicIP); err != nil { + return nil, err + } + if id == "" { + return nil, fmt.Errorf("public IP ID is empty: %w", cloud.ErrInvalidInput) + } + entry, ok := c.publicIPs[id] + if !ok { + return nil, fmt.Errorf("public IP %q: %w", id, cloud.ErrNotFound) + } + out := *entry.publicIP + return &out, nil +} + func (c *Client) ListPublicIPsByTags(_ context.Context, tags map[string]string) ([]*cloud.PublicIP, error) { c.mu.Lock() defer c.mu.Unlock() + if err := consume(&c.FailNextListPublicIPs); err != nil { + return nil, err + } if len(tags) == 0 { return nil, fmt.Errorf("empty tags: %w", cloud.ErrInvalidInput) } @@ -424,6 +458,82 @@ func (c *Client) ListPublicIPsByTags(_ context.Context, tags map[string]string) return publicIPs, nil } +func (c *Client) CreatePublicIP(_ context.Context, tags map[string]string) (*cloud.PublicIP, error) { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextCreatePublicIP); err != nil { + return nil, err + } + id := c.genID() + publicIP := &cloud.PublicIP{ + ID: id, + IP: "203.0.113.22", + } + c.publicIPs[id] = &publicIPEntry{ + publicIP: publicIP, + tags: copyTags(tags), + } + out := *publicIP + return &out, nil +} + +func (c *Client) DeletePublicIP(_ context.Context, id string) error { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextDeletePublicIP); err != nil { + return err + } + if id == "" { + return fmt.Errorf("public IP ID is empty: %w", cloud.ErrInvalidInput) + } + if _, ok := c.publicIPs[id]; !ok { + return fmt.Errorf("public IP %q: %w", id, cloud.ErrNotFound) + } + delete(c.publicIPs, id) + return nil +} + +func (c *Client) AddPublicIPToServer(_ context.Context, serverID, publicIPID string) error { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextAddPublicIP); err != nil { + return err + } + if serverID == "" || publicIPID == "" { + return fmt.Errorf("server ID and public IP ID are required: %w", cloud.ErrInvalidInput) + } + if _, ok := c.servers[serverID]; !ok { + return fmt.Errorf("server %q: %w", serverID, cloud.ErrNotFound) + } + entry, ok := c.publicIPs[publicIPID] + if !ok { + return fmt.Errorf("public IP %q: %w", publicIPID, cloud.ErrNotFound) + } + entry.publicIP.NetworkInterfaceID = "fake-nic-" + serverID + return nil +} + +func (c *Client) RemovePublicIPFromServer(_ context.Context, serverID, publicIPID string) error { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextRemovePublicIP); err != nil { + return err + } + if serverID == "" || publicIPID == "" { + return fmt.Errorf("server ID and public IP ID are required: %w", cloud.ErrInvalidInput) + } + entry, ok := c.publicIPs[publicIPID] + if !ok { + return fmt.Errorf("public IP %q: %w", publicIPID, cloud.ErrNotFound) + } + entry.publicIP.NetworkInterfaceID = "" + return nil +} + func (c *Client) ListSecurityGroupsByTags(_ context.Context, tags map[string]string) ([]*cloud.SecurityGroup, error) { c.mu.Lock() defer c.mu.Unlock() @@ -684,16 +794,6 @@ func containsString(values []string, needle string) bool { return slices.Contains(values, needle) } -func (c *Client) publicIPByTags(tags map[string]string) *cloud.PublicIP { - for _, entry := range c.publicIPs { - if mapContains(entry.tags, tags) { - out := *entry.publicIP - return &out - } - } - return nil -} - func (c *Client) securityGroupByTags(tags map[string]string) *cloud.SecurityGroup { for _, entry := range c.securityGroups { if mapContains(entry.tags, tags) { @@ -704,20 +804,6 @@ func (c *Client) securityGroupByTags(tags map[string]string) *cloud.SecurityGrou return nil } -func idOfPublicIP(publicIP *cloud.PublicIP) string { - if publicIP == nil { - return "" - } - return publicIP.ID -} - -func ipOfPublicIP(publicIP *cloud.PublicIP) string { - if publicIP == nil { - return "" - } - return publicIP.IP -} - func idOfSecurityGroup(securityGroup *cloud.SecurityGroup) string { if securityGroup == nil { return "" @@ -745,3 +831,148 @@ func cloneServer(s *cloud.Server) *cloud.Server { } return &out } + +type roundTripperFunc func(*http.Request) (*http.Response, error) + +func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { + return f(r) +} + +func (c *Client) roundTripIaaS(r *http.Request) (*http.Response, error) { + rec := httptest.NewRecorder() + rec.Header().Set("Content-Type", "application/json") + path := r.URL.Path + + switch { + case strings.Contains(path, "/servers/") && strings.Contains(path, "/public-ips/"): + parts := strings.Split(path, "/servers/") + sub := strings.Split(parts[1], "/public-ips/") + serverID, publicIPID := sub[0], sub[1] + var err error + switch r.Method { + case http.MethodPut: + err = c.AddPublicIPToServer(r.Context(), serverID, publicIPID) + case http.MethodDelete: + err = c.RemovePublicIPFromServer(r.Context(), serverID, publicIPID) + default: + err = cloud.ErrInvalidInput + } + if err != nil { + writeFakeError(rec, err) + } else { + rec.WriteHeader(http.StatusNoContent) + } + + case r.Method == http.MethodPost && strings.HasSuffix(path, "/public-ips"): + var body struct { + Labels map[string]string `json:"labels"` + } + if r.Body != nil { + _ = json.NewDecoder(r.Body).Decode(&body) + } + created, err := c.CreatePublicIP(r.Context(), body.Labels) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(fakePublicIPMap(created, body.Labels)) + } + + case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips"): + tags := parseLabelSelector(r.URL.Query().Get("label_selector")) + items, err := c.listPublicIPItems(tags) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(map[string]any{"items": items}) + } + + case r.Method == http.MethodGet && strings.Contains(path, "/public-ips/"): + id := path[strings.LastIndex(path, "/public-ips/")+len("/public-ips/"):] + got, err := c.GetPublicIP(r.Context(), id) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(fakePublicIPMap(got, nil)) + } + + case r.Method == http.MethodDelete && strings.Contains(path, "/public-ips/"): + id := path[strings.LastIndex(path, "/public-ips/")+len("/public-ips/"):] + if err := c.DeletePublicIP(r.Context(), id); err != nil { + writeFakeError(rec, err) + } else { + rec.WriteHeader(http.StatusNoContent) + } + + default: + rec.WriteHeader(http.StatusNotFound) + _ = json.NewEncoder(rec).Encode(map[string]any{"message": "not found"}) + } + + return rec.Result(), nil +} + +func (c *Client) listPublicIPItems(tags map[string]string) ([]map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextListPublicIPs); err != nil { + return nil, err + } + if len(tags) == 0 { + return nil, fmt.Errorf("empty tags: %w", cloud.ErrInvalidInput) + } + items := make([]map[string]any, 0, len(c.publicIPs)) + for _, entry := range c.publicIPs { + if mapContains(entry.tags, tags) { + items = append(items, fakePublicIPMap(entry.publicIP, entry.tags)) + } + } + return items, nil +} + +func fakePublicIPMap(p *cloud.PublicIP, tags map[string]string) map[string]any { + m := map[string]any{ + "id": p.ID, + "ip": p.IP, + } + if p.NetworkInterfaceID != "" { + m["networkInterface"] = p.NetworkInterfaceID + } + if len(tags) > 0 { + labels := make(map[string]any, len(tags)) + for k, v := range tags { + labels[k] = v + } + m["labels"] = labels + } + return m +} + +func parseLabelSelector(selector string) map[string]string { + if selector == "" { + return nil + } + tags := make(map[string]string) + for part := range strings.SplitSeq(selector, ",") { + if k, v, ok := strings.Cut(part, "="); ok && k != "" { + tags[k] = v + } + } + return tags +} + +func writeFakeError(rec *httptest.ResponseRecorder, err error) { + status := http.StatusInternalServerError + switch { + case cloud.IsNotFound(err): + status = http.StatusNotFound + case cloud.IsConflict(err): + status = http.StatusConflict + case cloud.IsInvalidInput(err): + status = http.StatusBadRequest + case cloud.IsUnauthorized(err): + status = http.StatusUnauthorized + } + rec.WriteHeader(status) + _ = json.NewEncoder(rec).Encode(map[string]any{"message": err.Error()}) +} diff --git a/cloud/sdk_client.go b/cloud/sdk_client.go index 433e22f..0fa1751 100644 --- a/cloud/sdk_client.go +++ b/cloud/sdk_client.go @@ -14,15 +14,13 @@ import ( "cmp" "context" "encoding/base64" - "errors" "fmt" - "net/http" "os" "slices" "strings" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/stackit-sdk-go/core/config" - "github.com/stackitcloud/stackit-sdk-go/core/oapierror" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" ) @@ -83,6 +81,18 @@ func NewClient(_ context.Context, creds Credentials) (Client, error) { }, nil } +func (c *SDKClient) ProjectID() string { + return c.projectID +} + +func (c *SDKClient) Region() string { + return c.region +} + +func (c *SDKClient) IaaSClient() *iaas.APIClient { + return c.iaasClient +} + func stackitOptions(creds Credentials, endpoint string) []config.ConfigurationOption { opts := []config.ConfigurationOption{ config.WithUserAgent("cluster-api-provider-stackit"), @@ -104,7 +114,7 @@ func stackitOptions(creds Credentials, endpoint string) []config.ConfigurationOp func (c *SDKClient) GetServer(ctx context.Context, id string) (*Server, error) { server, err := c.iaasClient.DefaultAPI.GetServer(ctx, c.projectID, c.region, id).Execute() if err != nil { - return nil, classifySDKError("get server", err) + return nil, shared.ClassifySDKError("get server", err) } return c.serverFromSDK(ctx, server), nil } @@ -128,15 +138,15 @@ func (c *SDKClient) ListServersByTags(ctx context.Context, tags map[string]strin return nil, fmt.Errorf("%w: empty tag selector", ErrInvalidInput) } resp, err := c.iaasClient.DefaultAPI.ListServers(ctx, c.projectID, c.region). - LabelSelector(labelSelector(tags)). + LabelSelector(shared.LabelSelector(tags)). Details(true). Execute() if err != nil { - return nil, classifySDKError("list servers", err) + return nil, shared.ClassifySDKError("list servers", err) } matched := []*Server{} for _, server := range resp.GetItems() { - if labelsContainTags(server.GetLabels(), tags) { + if shared.LabelsContainTags(server.GetLabels(), tags) { server := server matched = append(matched, c.serverFromSDK(ctx, &server)) } @@ -159,7 +169,7 @@ func (c *SDKClient) CreateServer(ctx context.Context, input CreateServerInput) ( payload.SetImageId(input.ImageID) } if len(input.Tags) > 0 { - payload.SetLabels(tagsToSDKLabels(input.Tags)) + payload.SetLabels(shared.TagsToSDKLabels(input.Tags)) } networking := iaas.NewCreateServerNetworking() if input.NetworkID != "" { @@ -198,7 +208,7 @@ func (c *SDKClient) CreateServer(ctx context.Context, input CreateServerInput) ( CreateServerPayload(*payload). Execute() if err != nil { - return nil, classifySDKError("create server", err) + return nil, shared.ClassifySDKError("create server", err) } return c.serverFromSDK(ctx, server), nil } @@ -206,7 +216,7 @@ func (c *SDKClient) CreateServer(ctx context.Context, input CreateServerInput) ( func (c *SDKClient) ListAvailabilityZones(ctx context.Context) ([]string, error) { resp, err := c.iaasClient.DefaultAPI.ListAvailabilityZones(ctx, c.region).Execute() if err != nil { - return nil, classifySDKError("list availability zones", err) + return nil, shared.ClassifySDKError("list availability zones", err) } if resp == nil || resp.Items == nil { return nil, nil @@ -216,7 +226,7 @@ func (c *SDKClient) ListAvailabilityZones(ctx context.Context) ([]string, error) func (c *SDKClient) DeleteServer(ctx context.Context, id string) error { if err := c.iaasClient.DefaultAPI.DeleteServer(ctx, c.projectID, c.region, id).Execute(); err != nil { - return classifySDKError("delete server", err) + return shared.ClassifySDKError("delete server", err) } return nil } @@ -224,7 +234,7 @@ func (c *SDKClient) DeleteServer(ctx context.Context, id string) error { func (c *SDKClient) GetNetwork(ctx context.Context, id string) (*Network, error) { network, err := c.iaasClient.DefaultAPI.GetNetwork(ctx, c.projectID, c.region, id).Execute() if err != nil { - return nil, classifySDKError("get network", err) + return nil, shared.ClassifySDKError("get network", err) } ipv4 := network.GetIpv4() return &Network{ @@ -283,31 +293,10 @@ func (c *SDKClient) EnsureBastion(ctx context.Context, input BastionInput) (*Bas return nil, err } - publicIP, err := c.ensurePublicIP(ctx, input.Tags) - if err != nil { - return nil, err - } - if publicIP.NetworkInterfaceID == "" { - if err := c.iaasClient.DefaultAPI. - AddPublicIpToServer(ctx, c.projectID, c.region, server.ID, publicIP.ID). - Execute(); err != nil { - err := classifySDKError("add public IP to server", err) - if !IsConflict(err) { - return nil, err - } - } - publicIP, err = c.getPublicIP(ctx, publicIP.ID) - if err != nil { - return nil, err - } - } - return &Bastion{ ServerID: server.ID, ServerState: server.State, ServerPowerStatus: server.PowerStatus, - PublicIPID: publicIP.ID, - PublicIP: publicIP.IP, SecurityGroupID: securityGroup.ID, }, nil } @@ -323,16 +312,6 @@ func (c *SDKClient) DeleteBastion(ctx context.Context, input BastionInput, statu } } - publicIPID := status.PublicIPID - if publicIPID == "" { - publicIP, err := c.findPublicIPByTags(ctx, input.Tags) - if err == nil { - publicIPID = publicIP.ID - } else if !IsNotFound(err) { - return err - } - } - securityGroupID := status.SecurityGroupID if securityGroupID == "" { securityGroup, err := c.findSecurityGroupByTags(ctx, input.Tags) @@ -343,21 +322,11 @@ func (c *SDKClient) DeleteBastion(ctx context.Context, input BastionInput, statu } } - if serverID != "" && publicIPID != "" { - if err := c.iaasClient.DefaultAPI. - RemovePublicIpFromServer(ctx, c.projectID, c.region, serverID, publicIPID). - Execute(); err != nil { - err := classifySDKError("remove public IP from server", err) - if !IsNotFound(err) && !IsConflict(err) { - return err - } - } - } if serverID != "" && securityGroupID != "" { if err := c.iaasClient.DefaultAPI. RemoveSecurityGroupFromServer(ctx, c.projectID, c.region, serverID, securityGroupID). Execute(); err != nil { - err := classifySDKError("remove security group from server", err) + err := shared.ClassifySDKError("remove security group from server", err) if !IsNotFound(err) && !IsConflict(err) { return err } @@ -368,14 +337,6 @@ func (c *SDKClient) DeleteBastion(ctx context.Context, input BastionInput, statu return err } } - if publicIPID != "" { - if err := c.iaasClient.DefaultAPI.DeletePublicIP(ctx, c.projectID, c.region, publicIPID).Execute(); err != nil { - err := classifySDKError("delete public IP", err) - if !IsNotFound(err) { - return err - } - } - } if securityGroupID != "" { if err := c.deleteSecurityGroupRules(ctx, securityGroupID); err != nil { return err @@ -383,7 +344,7 @@ func (c *SDKClient) DeleteBastion(ctx context.Context, input BastionInput, statu if err := c.iaasClient.DefaultAPI. DeleteSecurityGroup(ctx, c.projectID, c.region, securityGroupID). Execute(); err != nil { - err := classifySDKError("delete security group", err) + err := shared.ClassifySDKError("delete security group", err) if !IsNotFound(err) { return err } @@ -432,7 +393,7 @@ func (c *SDKClient) DeleteNodeSSHAccess(ctx context.Context, tags map[string]str if err := c.iaasClient.DefaultAPI. RemoveSecurityGroupFromServer(ctx, c.projectID, c.region, server.ID, securityGroup.ID). Execute(); err != nil { - err := classifySDKError("remove security group from server", err) + err := shared.ClassifySDKError("remove security group from server", err) if !IsNotFound(err) && !IsConflict(err) { return err } @@ -444,7 +405,7 @@ func (c *SDKClient) DeleteNodeSSHAccess(ctx context.Context, tags map[string]str if err := c.iaasClient.DefaultAPI. DeleteSecurityGroup(ctx, c.projectID, c.region, securityGroup.ID). Execute(); err != nil { - err := classifySDKError("delete security group", err) + err := shared.ClassifySDKError("delete security group", err) if !IsNotFound(err) { return err } @@ -452,39 +413,19 @@ func (c *SDKClient) DeleteNodeSSHAccess(ctx context.Context, tags map[string]str return nil } -func (c *SDKClient) ListPublicIPsByTags(ctx context.Context, tags map[string]string) ([]*PublicIP, error) { - if len(tags) == 0 { - return nil, fmt.Errorf("%w: empty tag selector", ErrInvalidInput) - } - resp, err := c.iaasClient.DefaultAPI.ListPublicIPs(ctx, c.projectID, c.region). - LabelSelector(labelSelector(tags)). - Execute() - if err != nil { - return nil, classifySDKError("list public IPs", err) - } - matched := []*PublicIP{} - for _, publicIP := range resp.GetItems() { - if labelsContainTags(publicIP.GetLabels(), tags) { - publicIP := publicIP - matched = append(matched, publicIPFromSDK(&publicIP)) - } - } - return matched, nil -} - func (c *SDKClient) ListSecurityGroupsByTags(ctx context.Context, tags map[string]string) ([]*SecurityGroup, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", ErrInvalidInput) } resp, err := c.iaasClient.DefaultAPI.ListSecurityGroups(ctx, c.projectID, c.region). - LabelSelector(labelSelector(tags)). + LabelSelector(shared.LabelSelector(tags)). Execute() if err != nil { - return nil, classifySDKError("list security groups", err) + return nil, shared.ClassifySDKError("list security groups", err) } matched := []*SecurityGroup{} for _, securityGroup := range resp.GetItems() { - if labelsContainTags(securityGroup.GetLabels(), tags) { + if shared.LabelsContainTags(securityGroup.GetLabels(), tags) { securityGroup := securityGroup matched = append(matched, securityGroupFromSDK(&securityGroup)) } @@ -502,7 +443,7 @@ func (c *SDKClient) EnsureAPIServerLoadBalancer(ctx context.Context, input LoadB payload.SetName(input.Name) payload.SetRegion(input.Region) if len(input.Tags) > 0 { - payload.SetLabels(loadBalancerLabels(input.Tags)) + payload.SetLabels(shared.LoadBalancerLabels(input.Tags)) } options := lb.NewLoadBalancerOptions() options.SetEphemeralAddress(true) @@ -544,14 +485,14 @@ func (c *SDKClient) EnsureAPIServerLoadBalancer(ctx context.Context, input LoadB CreateLoadBalancerPayload(*payload). Execute() if err != nil { - return nil, classifySDKError("create load balancer", err) + return nil, shared.ClassifySDKError("create load balancer", err) } return loadBalancerFromSDK(lbResp), nil } func (c *SDKClient) DeleteAPIServerLoadBalancer(ctx context.Context, id string) error { if _, err := c.lbClient.DefaultAPI.DeleteLoadBalancer(ctx, c.projectID, c.region, id).Execute(); err != nil { - return classifySDKError("delete load balancer", err) + return shared.ClassifySDKError("delete load balancer", err) } return nil } @@ -565,16 +506,16 @@ func (c *SDKClient) ListAPIServerLoadBalancersByTags( } resp, err := c.lbClient.DefaultAPI.ListLoadBalancers(ctx, c.projectID, c.region).Execute() if err != nil { - err := classifySDKError("list load balancers", err) + err := shared.ClassifySDKError("list load balancers", err) if isLoadBalancerServiceNotEnabled(err) { return nil, nil } return nil, err } matched := []*LoadBalancer{} - labels := loadBalancerLabels(tags) + labels := shared.LoadBalancerLabels(tags) for _, candidate := range resp.GetLoadBalancers() { - if mapContains(candidate.GetLabels(), labels) { + if shared.MapContains(candidate.GetLabels(), labels) { candidate := candidate matched = append(matched, loadBalancerFromSDK(&candidate)) } @@ -608,7 +549,7 @@ func (c *SDKClient) SetAPIServerLoadBalancerTargets( loadBalancer, err := c.lbClient.DefaultAPI.GetLoadBalancer(ctx, c.projectID, c.region, loadBalancerID).Execute() if err != nil { - return classifySDKError("get load balancer", err) + return shared.ClassifySDKError("get load balancer", err) } targetPool := apiServerTargetPool(loadBalancer) if targetPool == nil { @@ -670,12 +611,12 @@ func (c *SDKClient) ensureBastionSecurityGroup(ctx context.Context, input Bastio } payload := iaas.NewCreateSecurityGroupPayload(input.Name + "-ssh") payload.SetDescription("Cluster API Provider STACKIT bastion SSH access") - payload.SetLabels(tagsToSDKLabels(input.Tags)) + payload.SetLabels(shared.TagsToSDKLabels(input.Tags)) securityGroup, err := c.iaasClient.DefaultAPI.CreateSecurityGroup(ctx, c.projectID, c.region). CreateSecurityGroupPayload(*payload). Execute() if err != nil { - return nil, classifySDKError("create security group", err) + return nil, shared.ClassifySDKError("create security group", err) } return securityGroupFromSDK(securityGroup), nil } @@ -688,12 +629,12 @@ func (c *SDKClient) ensureNodeSSHSecurityGroup(ctx context.Context, input NodeSS } payload := iaas.NewCreateSecurityGroupPayload(input.Name) payload.SetDescription("Cluster API Provider STACKIT node SSH access from bastion") - payload.SetLabels(tagsToSDKLabels(input.Tags)) + payload.SetLabels(shared.TagsToSDKLabels(input.Tags)) securityGroup, err := c.iaasClient.DefaultAPI.CreateSecurityGroup(ctx, c.projectID, c.region). CreateSecurityGroupPayload(*payload). Execute() if err != nil { - return nil, classifySDKError("create security group", err) + return nil, shared.ClassifySDKError("create security group", err) } return securityGroupFromSDK(securityGroup), nil } @@ -704,7 +645,7 @@ func (c *SDKClient) ensureBastionSecurityGroupRules(ctx context.Context, securit } resp, err := c.iaasClient.DefaultAPI.ListSecurityGroupRules(ctx, c.projectID, c.region, securityGroupID).Execute() if err != nil { - return classifySDKError("list security group rules", err) + return shared.ClassifySDKError("list security group rules", err) } existingRules := resp.GetItems() desired := make(map[string]struct{}, len(cidrs)) @@ -731,7 +672,7 @@ func (c *SDKClient) ensureBastionSecurityGroupRules(ctx context.Context, securit if _, err := c.iaasClient.DefaultAPI.CreateSecurityGroupRule(ctx, c.projectID, c.region, securityGroupID). CreateSecurityGroupRulePayload(*rule). Execute(); err != nil { - return classifySDKError("create security group rule", err) + return shared.ClassifySDKError("create security group rule", err) } } @@ -752,7 +693,7 @@ func (c *SDKClient) ensureBastionSecurityGroupRules(ctx context.Context, securit if err := c.iaasClient.DefaultAPI. DeleteSecurityGroupRule(ctx, c.projectID, c.region, securityGroupID, ruleID). Execute(); err != nil { - err := classifySDKError("delete security group rule", err) + err := shared.ClassifySDKError("delete security group rule", err) if !IsNotFound(err) { return err } @@ -770,7 +711,7 @@ func (c *SDKClient) ensureNodeSSHSecurityGroupRule( } resp, err := c.iaasClient.DefaultAPI.ListSecurityGroupRules(ctx, c.projectID, c.region, nodeSecurityGroupID).Execute() if err != nil { - return classifySDKError("list security group rules", err) + return shared.ClassifySDKError("list security group rules", err) } if hasRemoteSecurityGroupSSHRule(resp.GetItems(), bastionSecurityGroupID) { return nil @@ -783,50 +724,11 @@ func (c *SDKClient) ensureNodeSSHSecurityGroupRule( if _, err := c.iaasClient.DefaultAPI.CreateSecurityGroupRule(ctx, c.projectID, c.region, nodeSecurityGroupID). CreateSecurityGroupRulePayload(*rule). Execute(); err != nil { - return classifySDKError("create security group rule", err) + return shared.ClassifySDKError("create security group rule", err) } return nil } -func (c *SDKClient) ensurePublicIP(ctx context.Context, tags map[string]string) (*PublicIP, error) { - if existing, err := c.findPublicIPByTags(ctx, tags); err == nil { - return existing, nil - } else if !IsNotFound(err) { - return nil, err - } - payload := iaas.NewCreatePublicIPPayload() - payload.SetLabels(tagsToSDKLabels(tags)) - publicIP, err := c.iaasClient.DefaultAPI.CreatePublicIP(ctx, c.projectID, c.region). - CreatePublicIPPayload(*payload). - Execute() - if err != nil { - return nil, classifySDKError("create public IP", err) - } - return publicIPFromSDK(publicIP), nil -} - -func (c *SDKClient) getPublicIP(ctx context.Context, id string) (*PublicIP, error) { - publicIP, err := c.iaasClient.DefaultAPI.GetPublicIP(ctx, c.projectID, c.region, id).Execute() - if err != nil { - return nil, classifySDKError("get public IP", err) - } - return publicIPFromSDK(publicIP), nil -} - -func (c *SDKClient) findPublicIPByTags(ctx context.Context, tags map[string]string) (*PublicIP, error) { - matched, err := c.ListPublicIPsByTags(ctx, tags) - if err != nil { - return nil, err - } - if len(matched) == 0 { - return nil, fmt.Errorf("%w: no public IP matching tags", ErrNotFound) - } - if len(matched) > 1 { - return nil, fmt.Errorf("%w: multiple public IPs matching tags", ErrConflict) - } - return matched[0], nil -} - func (c *SDKClient) findSecurityGroupByTags(ctx context.Context, tags map[string]string) (*SecurityGroup, error) { matched, err := c.ListSecurityGroupsByTags(ctx, tags) if err != nil { @@ -851,7 +753,7 @@ func (c *SDKClient) addSecurityGroupToServer(ctx context.Context, serverID, secu if err := c.iaasClient.DefaultAPI. AddSecurityGroupToServer(ctx, c.projectID, c.region, serverID, securityGroupID). Execute(); err != nil { - err := classifySDKError("add security group to server", err) + err := shared.ClassifySDKError("add security group to server", err) if !IsConflict(err) && !IsInvalidInput(err) && !IsNotFound(err) { return err } @@ -862,7 +764,7 @@ func (c *SDKClient) addSecurityGroupToServer(ctx context.Context, serverID, secu func (c *SDKClient) deleteSecurityGroupRules(ctx context.Context, securityGroupID string) error { resp, err := c.iaasClient.DefaultAPI.ListSecurityGroupRules(ctx, c.projectID, c.region, securityGroupID).Execute() if err != nil { - err := classifySDKError("list security group rules", err) + err := shared.ClassifySDKError("list security group rules", err) if IsNotFound(err) { return nil } @@ -876,7 +778,7 @@ func (c *SDKClient) deleteSecurityGroupRules(ctx context.Context, securityGroupI if err := c.iaasClient.DefaultAPI. DeleteSecurityGroupRule(ctx, c.projectID, c.region, securityGroupID, ruleID). Execute(); err != nil { - err := classifySDKError("delete security group rule", err) + err := shared.ClassifySDKError("delete security group rule", err) if !IsNotFound(err) { return err } @@ -943,7 +845,7 @@ func (c *SDKClient) updateAPIServerTargetPool( UpdateTargetPool(ctx, c.projectID, c.region, loadBalancerID, apiserverTargetPoolName). UpdateTargetPoolPayload(*payload). Execute(); err != nil { - return classifySDKError("update load balancer target pool", err) + return shared.ClassifySDKError("update load balancer target pool", err) } return nil } @@ -992,22 +894,11 @@ func loadBalancerFromSDK(in *lb.LoadBalancer) *LoadBalancer { return &LoadBalancer{ ID: in.GetName(), Name: in.GetName(), - IP: firstNonEmpty(in.GetExternalAddress(), in.GetPrivateAddress()), + IP: shared.FirstNonEmpty(in.GetExternalAddress(), in.GetPrivateAddress()), Port: listenerPort(in.GetListeners()), } } -func publicIPFromSDK(in *iaas.PublicIp) *PublicIP { - if in == nil { - return nil - } - return &PublicIP{ - ID: in.GetId(), - IP: in.GetIp(), - NetworkInterfaceID: in.GetNetworkInterface(), - } -} - func securityGroupFromSDK(in *iaas.SecurityGroup) *SecurityGroup { if in == nil { return nil @@ -1027,32 +918,6 @@ func listenerPort(listeners []lb.Listener) int32 { return 0 } -func labelSelector(tags map[string]string) string { - parts := make([]string, 0, len(tags)) - for k, v := range tags { - parts = append(parts, k+"="+v) - } - return strings.Join(parts, ",") -} - -func labelsContainTags(labels map[string]any, tags map[string]string) bool { - for k, want := range tags { - got, ok := labels[k] - if !ok || fmt.Sprint(got) != want { - return false - } - } - return true -} - -func tagsToSDKLabels(tags map[string]string) map[string]any { - out := make(map[string]any, len(tags)) - for k, v := range tags { - out[k] = v - } - return out -} - func clusterServerSelectorFromTags(tags map[string]string) map[string]string { selector := map[string]string{} for _, key := range []string{ @@ -1067,55 +932,6 @@ func clusterServerSelectorFromTags(tags map[string]string) map[string]string { return selector } -func mapContains(haystack, needle map[string]string) bool { - for k, v := range needle { - if haystack[k] != v { - return false - } - } - return true -} - -func loadBalancerLabels(tags map[string]string) map[string]string { - out := make(map[string]string, len(tags)) - for k, v := range tags { - out[strings.ReplaceAll(k, "/", ".")] = v - } - return out -} - -func firstNonEmpty(values ...string) string { - for _, value := range values { - if value != "" { - return value - } - } - return "" -} - -func classifySDKError(op string, err error) error { - var oapiErr *oapierror.GenericOpenAPIError - if errors.As(err, &oapiErr) { - switch oapiErr.StatusCode { - case http.StatusNotFound: - return fmt.Errorf("%w: %s: %v", ErrNotFound, op, err) - case http.StatusUnauthorized, http.StatusForbidden: - return fmt.Errorf("%w: %s: %v", ErrUnauthorized, op, err) - case http.StatusConflict: - return fmt.Errorf("%w: %s: %v", ErrConflict, op, err) - case http.StatusBadRequest, http.StatusUnprocessableEntity: - return fmt.Errorf("%w: %s: %v", ErrInvalidInput, op, err) - case http.StatusTooManyRequests: - return fmt.Errorf("%w: %s: %v", ErrTransient, op, err) - default: - if oapiErr.StatusCode >= 500 { - return fmt.Errorf("%w: %s: %v", ErrTransient, op, err) - } - } - } - return fmt.Errorf("%w: %s: %v", ErrTransient, op, err) -} - func isLoadBalancerServiceNotEnabled(err error) bool { return IsUnauthorized(err) && strings.Contains(err.Error(), "Service not enabled") } diff --git a/cloud/sdk_client_test.go b/cloud/sdk_client_test.go index 34a3e9b..ad0c9e7 100644 --- a/cloud/sdk_client_test.go +++ b/cloud/sdk_client_test.go @@ -469,9 +469,8 @@ func lookup(m map[string]any, key string) any { // short-circuits on an existing server, making this the only re-attach path. func TestSDKClientEnsureBastionToleratesDuplicateSecurityGroupAttach(t *testing.T) { var ( - createPayload map[string]any - attachCallCount int - publicIPAssigned bool + createPayload map[string]any + attachCallCount int ) server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path @@ -502,13 +501,6 @@ func TestSDKClientEnsureBastionToleratesDuplicateSecurityGroupAttach(t *testing. "code": 400, "msg": "request invalid: Invalid input for security_groups. Reason: Duplicate items in the list.", }) - case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips"): - writeJSON(t, w, map[string]any{"items": []any{}}) - case r.Method == http.MethodPost && strings.HasSuffix(path, "/public-ips"): - writeJSON(t, w, map[string]any{"id": "66666666-6666-4666-8666-666666666666", "ip": "203.0.113.10"}) - case strings.Contains(path, "/public-ips/"): - publicIPAssigned = true - writeJSON(t, w, map[string]any{"id": "66666666-6666-4666-8666-666666666666", "ip": "203.0.113.10"}) case r.Method == http.MethodGet && strings.Contains(path, "/servers/"): writeJSON(t, w, map[string]any{ "id": testSDKServerID, "name": "bastion", "status": "ACTIVE", "machineType": "c2i.1", @@ -541,11 +533,8 @@ func TestSDKClientEnsureBastionToleratesDuplicateSecurityGroupAttach(t *testing. if len(groups) != 1 || groups[0] != testSDKSecurityGroup { t.Fatalf("create payload securityGroups = %v, want [%s]", createPayload["securityGroups"], testSDKSecurityGroup) } - if !publicIPAssigned { - t.Fatal("public IP was never assigned to the bastion server") - } - if bastion.PublicIP != "203.0.113.10" { - t.Fatalf("bastion.PublicIP = %q, want 203.0.113.10", bastion.PublicIP) + if bastion.ServerID != testSDKServerID { + t.Fatalf("bastion.ServerID = %q, want %s", bastion.ServerID, testSDKServerID) } } diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go new file mode 100644 index 0000000..d3f947c --- /dev/null +++ b/cloud/services/publicip/reconcile.go @@ -0,0 +1,248 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package publicip + +import ( + "context" + "fmt" + + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + logf "sigs.k8s.io/controller-runtime/pkg/log" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +// Reconcile reconciles cluster public IP resources. +func (s *Service) Reconcile(ctx context.Context) error { + log := logf.FromContext(ctx) + if !s.scope.BastionSpec().Enabled { + return s.Delete(ctx) + } + + log.V(1).Info("Reconciling public IP resources") + publicIP, err := s.createOrGetPublicIP(ctx) + if err != nil { + return err + } + + serverID := s.scope.BastionStatus().ServerID + if serverID != "" && publicIP.NetworkInterfaceID == "" { + log.V(1).Info("Attaching public IP to server", "publicIPID", publicIP.ID, "serverID", serverID) + if err := s.iaasClient.DefaultAPI. + AddPublicIpToServer(ctx, s.scope.ProjectID(), s.scope.Region(), serverID, publicIP.ID). + Execute(); err != nil { + err := shared.ClassifySDKError("add public IP to server", err) + if !shared.IsConflict(err) { + log.Error(err, "Failed to attach public IP to server", "publicIPID", publicIP.ID, "serverID", serverID) + return err + } + } + publicIP, err = s.getPublicIP(ctx, publicIP.ID) + if err != nil { + log.Error(err, "Failed to get public IP after attachment", "publicIPID", publicIP.ID) + return err + } + } + + s.scope.SetBastionPublicIP(publicIP.ID, publicIP.IP) + return nil +} + +// Delete deletes cluster public IP resources. +func (s *Service) Delete(ctx context.Context) error { + log := logf.FromContext(ctx) + log.V(1).Info("Deleting public IP resources") + + publicIP, err := s.findPublicIP(ctx) + if err != nil { + return err + } + if publicIP == nil { + s.scope.SetBastionPublicIP("", "") + return nil + } + + serverID, err := s.resolveServerID(ctx) + if err != nil { + return err + } + if serverID != "" { + log.V(1).Info("Detaching public IP from server", "publicIPID", publicIP.ID, "serverID", serverID) + if err := DetachFromServer(ctx, s.iaasClient, s.scope.ProjectID(), s.scope.Region(), serverID, publicIP.ID); err != nil { + log.Error(err, "Failed to detach public IP from server", "publicIPID", publicIP.ID, "serverID", serverID) + return err + } + } + + log.V(1).Info("Deleting public IP", "publicIPID", publicIP.ID) + if err := DeleteByID(ctx, s.iaasClient, s.scope.ProjectID(), s.scope.Region(), publicIP.ID); err != nil { + log.Error(err, "Failed to delete public IP", "publicIPID", publicIP.ID) + return err + } + + s.scope.SetBastionPublicIP("", "") + return nil +} + +func (s *Service) createOrGetPublicIP(ctx context.Context) (*PublicIP, error) { + log := logf.FromContext(ctx) + + publicIP, err := s.findPublicIP(ctx) + if err != nil { + return nil, err + } + if publicIP != nil { + return publicIP, nil + } + + log.V(1).Info("Creating public IP") + payload := iaas.NewCreatePublicIPPayload() + if tags := s.scope.BastionTags(); len(tags) > 0 { + payload.SetLabels(shared.TagsToSDKLabels(tags)) + } + created, err := s.iaasClient.DefaultAPI. + CreatePublicIP(ctx, s.scope.ProjectID(), s.scope.Region()). + CreatePublicIPPayload(*payload). + Execute() + if err != nil { + err := shared.ClassifySDKError("create public IP", err) + log.Error(err, "Failed to create public IP") + return nil, err + } + return publicIPFromSDK(created), nil +} + +func (s *Service) findPublicIP(ctx context.Context) (*PublicIP, error) { + log := logf.FromContext(ctx) + if id := s.scope.BastionStatus().PublicIPID; id != "" { + publicIP, err := s.getPublicIP(ctx, id) + if err == nil { + return publicIP, nil + } + if !shared.IsNotFound(err) { + log.Error(err, "Failed to get public IP", "publicIPID", id) + return nil, err + } + } + + matched, err := ListByTags(ctx, s.iaasClient, s.scope.ProjectID(), s.scope.Region(), s.scope.BastionTags()) + if err != nil { + log.Error(err, "Failed to list public IPs by tags") + return nil, err + } + if len(matched) == 0 { + return nil, nil + } + if len(matched) > 1 { + return nil, fmt.Errorf("%w: multiple public IPs matching tags", shared.ErrConflict) + } + return matched[0], nil +} + +func (s *Service) getPublicIP(ctx context.Context, id string) (*PublicIP, error) { + if id == "" { + return nil, fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) + } + publicIP, err := s.iaasClient.DefaultAPI.GetPublicIP(ctx, s.scope.ProjectID(), s.scope.Region(), id).Execute() + if err != nil { + return nil, shared.ClassifySDKError("get public IP", err) + } + return publicIPFromSDK(publicIP), nil +} + +func (s *Service) resolveServerID(ctx context.Context) (string, error) { + if id := s.scope.BastionStatus().ServerID; id != "" { + return id, nil + } + server, err := s.client.FindServerByTags(ctx, s.scope.BastionTags()) + if err != nil { + if shared.IsNotFound(err) { + return "", nil + } + return "", err + } + return server.ID, nil +} + +// ListByTags lists STACKIT public IPs matching the given tag selector. +func ListByTags( + ctx context.Context, + iaasClient *iaas.APIClient, + projectID, region string, + tags map[string]string, +) ([]*PublicIP, error) { + if len(tags) == 0 { + return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) + } + resp, err := iaasClient.DefaultAPI.ListPublicIPs(ctx, projectID, region). + LabelSelector(shared.LabelSelector(tags)). + Execute() + if err != nil { + return nil, shared.ClassifySDKError("list public IPs", err) + } + matched := []*PublicIP{} + for _, publicIP := range resp.GetItems() { + if shared.LabelsContainTags(publicIP.GetLabels(), tags) { + publicIP := publicIP + matched = append(matched, publicIPFromSDK(&publicIP)) + } + } + return matched, nil +} + +// DetachFromServer detaches a public IP from a server, ignoring NotFound and Conflict errors. +func DetachFromServer( + ctx context.Context, + iaasClient *iaas.APIClient, + projectID, region, serverID, publicIPID string, +) error { + if serverID == "" || publicIPID == "" { + return fmt.Errorf("%w: server ID and public IP ID are required", shared.ErrInvalidInput) + } + if err := iaasClient.DefaultAPI. + RemovePublicIpFromServer(ctx, projectID, region, serverID, publicIPID). + Execute(); err != nil { + err := shared.ClassifySDKError("remove public IP from server", err) + if !shared.IsNotFound(err) && !shared.IsConflict(err) { + return err + } + } + return nil +} + +// DeleteByID deletes a public IP by ID, ignoring NotFound errors. +func DeleteByID( + ctx context.Context, + iaasClient *iaas.APIClient, + projectID, region, id string, +) error { + if id == "" { + return fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) + } + if err := iaasClient.DefaultAPI.DeletePublicIP(ctx, projectID, region, id).Execute(); err != nil { + err := shared.ClassifySDKError("delete public IP", err) + if !shared.IsNotFound(err) { + return err + } + } + return nil +} + +func publicIPFromSDK(in *iaas.PublicIp) *PublicIP { + if in == nil { + return nil + } + return &PublicIP{ + ID: in.GetId(), + IP: in.GetIp(), + NetworkInterfaceID: in.GetNetworkInterface(), + } +} diff --git a/cloud/services/publicip/reconcile_test.go b/cloud/services/publicip/reconcile_test.go new file mode 100644 index 0000000..b162e5d --- /dev/null +++ b/cloud/services/publicip/reconcile_test.go @@ -0,0 +1,360 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package publicip_test + +import ( + "context" + "errors" + "testing" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes/scheme" + clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + + infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" + "github.com/stackitcloud/cluster-api-provider-stackit/scope" +) + +func init() { + _ = clusterv1.AddToScheme(scheme.Scheme) + _ = infrav1.AddToScheme(scheme.Scheme) +} + +func newTestClusterScope(t *testing.T, bastionEnabled bool, cloudClient cloud.Client) *scope.ClusterScope { + t.Helper() + + cluster := &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cluster", + Namespace: "default", + }, + } + stackitCluster := &infrav1.StackitCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cluster", + Namespace: "default", + }, + Spec: infrav1.StackitClusterSpec{ + ProjectID: "00000000-0000-0000-0000-000000000000", + Region: "eu01", + Bastion: infrav1.StackitBastionSpec{ + Enabled: bastionEnabled, + }, + }, + } + + k8sClient := fake.NewClientBuilder(). + WithScheme(scheme.Scheme). + WithObjects(cluster, stackitCluster). + Build() + + clusterScope, err := scope.NewClusterScope(k8sClient, cluster, stackitCluster) + if err != nil { + t.Fatalf("NewClusterScope() error = %v", err) + } + clusterScope.SetCloudClient(cloudClient) + return clusterScope +} + +func TestServiceReconcile(t *testing.T) { + t.Run("deletes existing public IP when bastion is disabled", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, false, fakeCloud) + + created, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) + if err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + clusterScope.SetBastionPublicIP(created.ID, created.IP) + + svc := publicip.New(clusterScope) + if err := svc.Reconcile(ctx); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + + if fakeCloud.PublicIPCount() != 0 { + t.Fatalf("PublicIPCount() = %d, want 0", fakeCloud.PublicIPCount()) + } + if clusterScope.BastionStatus().PublicIPID != "" { + t.Fatalf("BastionStatus().PublicIPID = %q, want empty", clusterScope.BastionStatus().PublicIPID) + } + }) + + t.Run("creates public IP and attaches it to the bastion server", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + Name: "test-cluster-bastion", + Tags: clusterScope.BastionTags(), + }) + if err != nil { + t.Fatalf("CreateServer() error = %v", err) + } + clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID + + svc := publicip.New(clusterScope) + if err := svc.Reconcile(ctx); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + + if fakeCloud.PublicIPCount() != 1 { + t.Fatalf("PublicIPCount() = %d, want 1", fakeCloud.PublicIPCount()) + } + if clusterScope.BastionStatus().PublicIPID == "" { + t.Fatal("BastionStatus().PublicIPID is empty, want set") + } + if clusterScope.StackitCluster.Status.Bastion.PublicIP != "203.0.113.22" { + t.Fatalf("Bastion.PublicIP = %q, want 203.0.113.22", clusterScope.StackitCluster.Status.Bastion.PublicIP) + } + + gotIP, err := fakeCloud.GetPublicIP(ctx, clusterScope.BastionStatus().PublicIPID) + if err != nil { + t.Fatalf("GetPublicIP() error = %v", err) + } + if gotIP.NetworkInterfaceID == "" { + t.Fatal("PublicIP NetworkInterfaceID is empty, want attached to server") + } + }) + + t.Run("reuses existing public IP found by status ID without listing by tags", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + Name: "test-cluster-bastion", + Tags: clusterScope.BastionTags(), + }) + if err != nil { + t.Fatalf("CreateServer() error = %v", err) + } + clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID + + existing, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) + if err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + clusterScope.SetBastionPublicIP(existing.ID, existing.IP) + fakeCloud.FailNextListPublicIPs = shared.ErrTransient + + svc := publicip.New(clusterScope) + if err := svc.Reconcile(ctx); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + + if fakeCloud.PublicIPCount() != 1 { + t.Fatalf("PublicIPCount() = %d, want 1", fakeCloud.PublicIPCount()) + } + if clusterScope.BastionStatus().PublicIPID != existing.ID { + t.Fatalf("BastionStatus().PublicIPID = %q, want %q", clusterScope.BastionStatus().PublicIPID, existing.ID) + } + }) + + t.Run("reuses existing public IP found by tags without creating duplicate", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + Name: "test-cluster-bastion", + Tags: clusterScope.BastionTags(), + }) + if err != nil { + t.Fatalf("CreateServer() error = %v", err) + } + clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID + + existing, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) + if err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + + svc := publicip.New(clusterScope) + if err := svc.Reconcile(ctx); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + + if fakeCloud.PublicIPCount() != 1 { + t.Fatalf("PublicIPCount() = %d, want 1", fakeCloud.PublicIPCount()) + } + if clusterScope.BastionStatus().PublicIPID != existing.ID { + t.Fatalf("BastionStatus().PublicIPID = %q, want %q", clusterScope.BastionStatus().PublicIPID, existing.ID) + } + }) + + t.Run("tolerates conflict when attaching public IP to server", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + Name: "test-cluster-bastion", + Tags: clusterScope.BastionTags(), + }) + if err != nil { + t.Fatalf("CreateServer() error = %v", err) + } + clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID + fakeCloud.FailNextAddPublicIP = shared.ErrConflict + + svc := publicip.New(clusterScope) + if err := svc.Reconcile(ctx); err != nil { + t.Fatalf("Reconcile() error = %v, want nil on conflict", err) + } + if clusterScope.BastionStatus().PublicIPID == "" { + t.Fatal("BastionStatus().PublicIPID is empty, want set") + } + }) + + t.Run("returns conflict error when multiple public IPs match tags", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + if _, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()); err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + if _, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()); err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + + svc := publicip.New(clusterScope) + err := svc.Reconcile(ctx) + if !errors.Is(err, shared.ErrConflict) { + t.Fatalf("Reconcile() error = %v, want ErrConflict", err) + } + }) + + t.Run("returns error when creating public IP fails", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + fakeCloud.FailNextCreatePublicIP = shared.ErrTransient + + svc := publicip.New(clusterScope) + err := svc.Reconcile(ctx) + if !errors.Is(err, shared.ErrTransient) { + t.Fatalf("Reconcile() error = %v, want ErrTransient", err) + } + }) +} + +func TestServiceDelete(t *testing.T) { + t.Run("detaches and deletes public IP using recorded status ID", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + Name: "test-cluster-bastion", + Tags: clusterScope.BastionTags(), + }) + if err != nil { + t.Fatalf("CreateServer() error = %v", err) + } + clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID + + created, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) + if err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + if err := fakeCloud.AddPublicIPToServer(ctx, server.ID, created.ID); err != nil { + t.Fatalf("AddPublicIPToServer() error = %v", err) + } + clusterScope.SetBastionPublicIP(created.ID, created.IP) + + svc := publicip.New(clusterScope) + if err := svc.Delete(ctx); err != nil { + t.Fatalf("Delete() error = %v", err) + } + + if fakeCloud.PublicIPCount() != 0 { + t.Fatalf("PublicIPCount() = %d, want 0", fakeCloud.PublicIPCount()) + } + if clusterScope.BastionStatus().PublicIPID != "" { + t.Fatalf("BastionStatus().PublicIPID = %q, want empty", clusterScope.BastionStatus().PublicIPID) + } + if clusterScope.StackitCluster.Status.Bastion.PublicIP != "" { + t.Fatalf("Bastion.PublicIP = %q, want empty", clusterScope.StackitCluster.Status.Bastion.PublicIP) + } + }) + + t.Run("falls back to tag lookup for both public IP and server when status IDs are empty", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + Name: "test-cluster-bastion", + Tags: clusterScope.BastionTags(), + }) + if err != nil { + t.Fatalf("CreateServer() error = %v", err) + } + created, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) + if err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + if err := fakeCloud.AddPublicIPToServer(ctx, server.ID, created.ID); err != nil { + t.Fatalf("AddPublicIPToServer() error = %v", err) + } + + svc := publicip.New(clusterScope) + if err := svc.Delete(ctx); err != nil { + t.Fatalf("Delete() error = %v", err) + } + + if fakeCloud.PublicIPCount() != 0 { + t.Fatalf("PublicIPCount() = %d, want 0", fakeCloud.PublicIPCount()) + } + }) + + t.Run("succeeds when no public IP exists", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + svc := publicip.New(clusterScope) + if err := svc.Delete(ctx); err != nil { + t.Fatalf("Delete() error = %v, want nil", err) + } + }) + + t.Run("returns error when DeletePublicIP fails with non-NotFound error", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + created, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) + if err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + clusterScope.SetBastionPublicIP(created.ID, created.IP) + fakeCloud.FailNextDeletePublicIP = shared.ErrTransient + + svc := publicip.New(clusterScope) + err = svc.Delete(ctx) + if !errors.Is(err, shared.ErrTransient) { + t.Fatalf("Delete() error = %v, want ErrTransient", err) + } + if clusterScope.BastionStatus().PublicIPID != created.ID { + t.Fatalf("BastionStatus().PublicIPID = %q, want preserved %q on error", clusterScope.BastionStatus().PublicIPID, created.ID) + } + }) +} diff --git a/cloud/services/publicip/service.go b/cloud/services/publicip/service.go new file mode 100644 index 0000000..62cc0c0 --- /dev/null +++ b/cloud/services/publicip/service.go @@ -0,0 +1,64 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package publicip + +import ( + "context" + + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + + infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +// PublicIP describes a STACKIT public IP resource. +type PublicIP struct { + ID string + IP string + NetworkInterfaceID string +} + +// Client defines the cloud API operations required by the public IP service. +type Client interface { + FindServerByTags(ctx context.Context, tags map[string]string) (*shared.Server, error) +} + +// Scope is an interface that provides cluster scope data needed for public IP reconciliation. +type Scope[C Client] interface { + scopeData + Cloud() C +} + +type scopeData interface { + ProjectID() string + Region() string + IaaSClient() *iaas.APIClient + BastionSpec() infrav1.StackitBastionSpec + BastionStatus() infrav1.StackitBastionStatus + BastionTags() map[string]string + SetBastionPublicIP(id, ip string) +} + +// Service implements public IPs reconciler. +type Service struct { + scope scopeData + client Client + iaasClient *iaas.APIClient +} + +// New returns Service from given scope. +func New[C Client](scope Scope[C]) *Service { + return &Service{ + scope: scope, + client: scope.Cloud(), + iaasClient: scope.IaaSClient(), + } +} diff --git a/cloud/services/shared/errors.go b/cloud/services/shared/errors.go new file mode 100644 index 0000000..73ecf9b --- /dev/null +++ b/cloud/services/shared/errors.go @@ -0,0 +1,44 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package shared + +import "errors" + +// Sentinel errors returned by Client implementations. Controllers use +// errors.Is to classify them into requeue / condition-update behavior. +var ( + ErrNotFound = errors.New("not found") + ErrUnauthorized = errors.New("unauthorized") + ErrInvalidInput = errors.New("invalid input") + ErrConflict = errors.New("conflict") + ErrTransient = errors.New("transient") +) + +// IsNotFound reports whether err wraps ErrNotFound. +func IsNotFound(err error) bool { return errors.Is(err, ErrNotFound) } + +// IsUnauthorized reports whether err wraps ErrUnauthorized. +func IsUnauthorized(err error) bool { return errors.Is(err, ErrUnauthorized) } + +// IsInvalidInput reports whether err wraps ErrInvalidInput. +func IsInvalidInput(err error) bool { return errors.Is(err, ErrInvalidInput) } + +// IsConflict reports whether err wraps ErrConflict. +func IsConflict(err error) bool { return errors.Is(err, ErrConflict) } + +// IsTransient reports whether err wraps ErrTransient. +func IsTransient(err error) bool { return errors.Is(err, ErrTransient) } + +// IsRetryable reports whether err should trigger a requeue (conflict or +// transient). +func IsRetryable(err error) bool { + return IsConflict(err) || IsTransient(err) +} diff --git a/cloud/errors_test.go b/cloud/services/shared/errors_test.go similarity index 98% rename from cloud/errors_test.go rename to cloud/services/shared/errors_test.go index e1b82f3..314eb25 100644 --- a/cloud/errors_test.go +++ b/cloud/services/shared/errors_test.go @@ -8,7 +8,7 @@ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 */ -package cloud +package shared import ( "fmt" diff --git a/cloud/services/shared/types.go b/cloud/services/shared/types.go new file mode 100644 index 0000000..a413a30 --- /dev/null +++ b/cloud/services/shared/types.go @@ -0,0 +1,28 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package shared + +// Server describes a STACKIT compute instance in provider-neutral terms. +type Server struct { + ID string + Name string + State string + PowerStatus string + ProviderID string + AvailabilityZone string + Addresses []Address +} + +// Address is an IP or DNS endpoint of a Server. +type Address struct { + Type string + Address string +} diff --git a/cloud/services/shared/utils.go b/cloud/services/shared/utils.go new file mode 100644 index 0000000..430d98b --- /dev/null +++ b/cloud/services/shared/utils.go @@ -0,0 +1,96 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package shared + +import ( + "errors" + "fmt" + "net/http" + "strings" + + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" +) + +func MapContains(haystack, needle map[string]string) bool { + for k, v := range needle { + if haystack[k] != v { + return false + } + } + return true +} + +// TODO: Move to LB Service +func LoadBalancerLabels(tags map[string]string) map[string]string { + out := make(map[string]string, len(tags)) + for k, v := range tags { + out[strings.ReplaceAll(k, "/", ".")] = v + } + return out +} + +func FirstNonEmpty(values ...string) string { + for _, value := range values { + if value != "" { + return value + } + } + return "" +} + +func LabelSelector(tags map[string]string) string { + parts := make([]string, 0, len(tags)) + for k, v := range tags { + parts = append(parts, k+"="+v) + } + return strings.Join(parts, ",") +} + +func LabelsContainTags(labels map[string]any, tags map[string]string) bool { + for k, want := range tags { + got, ok := labels[k] + if !ok || fmt.Sprint(got) != want { + return false + } + } + return true +} + +func TagsToSDKLabels(tags map[string]string) map[string]any { + out := make(map[string]any, len(tags)) + for k, v := range tags { + out[k] = v + } + return out +} + +func ClassifySDKError(op string, err error) error { + var oapiErr *oapierror.GenericOpenAPIError + if errors.As(err, &oapiErr) { + switch oapiErr.StatusCode { + case http.StatusNotFound: + return fmt.Errorf("%w: %s: %v", ErrNotFound, op, err) + case http.StatusUnauthorized, http.StatusForbidden: + return fmt.Errorf("%w: %s: %v", ErrUnauthorized, op, err) + case http.StatusConflict: + return fmt.Errorf("%w: %s: %v", ErrConflict, op, err) + case http.StatusBadRequest, http.StatusUnprocessableEntity: + return fmt.Errorf("%w: %s: %v", ErrInvalidInput, op, err) + case http.StatusTooManyRequests: + return fmt.Errorf("%w: %s: %v", ErrTransient, op, err) + default: + if oapiErr.StatusCode >= 500 { + return fmt.Errorf("%w: %s: %v", ErrTransient, op, err) + } + } + } + return fmt.Errorf("%w: %s: %v", ErrTransient, op, err) +} diff --git a/cloud/types.go b/cloud/types.go index 289353f..92a63dc 100644 --- a/cloud/types.go +++ b/cloud/types.go @@ -13,22 +13,16 @@ You may obtain a copy of the License at // through the Client interface in this package. package cloud +import ( + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + // Server describes a STACKIT compute instance in provider-neutral terms. -type Server struct { - ID string - Name string - State string - PowerStatus string - ProviderID string - AvailabilityZone string - Addresses []Address -} +type Server = shared.Server // Address is an IP or DNS endpoint of a Server. -type Address struct { - Type string - Address string -} +type Address = shared.Address // Network is an existing STACKIT virtual network referenced by the provider. type Network struct { @@ -47,11 +41,7 @@ type LoadBalancer struct { } // PublicIP describes a STACKIT public IP resource. -type PublicIP struct { - ID string - IP string - NetworkInterfaceID string -} +type PublicIP = publicip.PublicIP // SecurityGroup describes a STACKIT security group. type SecurityGroup struct { @@ -64,8 +54,6 @@ type Bastion struct { ServerID string ServerState string ServerPowerStatus string - PublicIPID string - PublicIP string SecurityGroupID string } diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index e8c3d8b..e2c1a1a 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -29,6 +29,7 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" bastionservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/bastion" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/scope" ) @@ -46,8 +47,6 @@ func (r *StackitClusterReconciler) reconcileBastion( input := bastionservice.Input(stackitCluster, nil) status := cloud.Bastion{ ServerID: stackitCluster.Status.Bastion.ServerID, - PublicIPID: stackitCluster.Status.Bastion.PublicIPID, - PublicIP: stackitCluster.Status.Bastion.PublicIP, SecurityGroupID: stackitCluster.Status.Bastion.SecurityGroupID, } @@ -57,10 +56,7 @@ func (r *StackitClusterReconciler) reconcileBastion( // the status keeps the tag-based cleanup to once per cluster. condition := meta.FindStatusCondition(stackitCluster.Status.Conditions, infrav1.ClusterBastionReadyCondition) if condition == nil || condition.Reason != bastionDisabledReason { - if err := cloudClient.DeleteNodeSSHAccess(ctx, bastionservice.NodeSSHAccessTags(stackitCluster)); err != nil { - return ctrl.Result{}, false, err - } - if err := cloudClient.DeleteBastion(ctx, input, status); err != nil { + if err := r.deleteBastionResources(ctx, cloudClient, clusterScope, input, status, false); err != nil { return ctrl.Result{}, false, err } clusterScope.ClearBastionStatus() @@ -102,10 +98,7 @@ func (r *StackitClusterReconciler) reconcileBastion( input.CloudInit = cloudInit if bastionNeedsRecreate(stackitCluster, cloudInit) { - if err := cloudClient.DeleteNodeSSHAccess(ctx, bastionservice.NodeSSHAccessTags(stackitCluster)); err != nil && !cloud.IsNotFound(err) { - return ctrl.Result{}, false, err - } - if err := cloudClient.DeleteBastion(ctx, input, status); err != nil && !cloud.IsNotFound(err) { + if err := r.deleteBastionResources(ctx, cloudClient, clusterScope, input, status, true); err != nil { return ctrl.Result{}, false, err } clusterScope.ClearBastionStatus() @@ -133,6 +126,9 @@ func (r *StackitClusterReconciler) reconcileBastion( stackitCluster, "BastionCreated", "Created bastion %s", bastion.ServerID, ) } + if err := publicip.New(clusterScope).Reconcile(ctx); err != nil { + return ctrl.Result{}, false, err + } if ready, reason, message, warn := serverStateCondition(bastion.ServerState, bastion.ServerPowerStatus); !ready { previousReason := "" if previous := meta.FindStatusCondition(stackitCluster.Status.Conditions, infrav1.ClusterBastionReadyCondition); previous != nil { @@ -145,7 +141,7 @@ func (r *StackitClusterReconciler) reconcileBastion( } return ctrl.Result{RequeueAfter: 15 * time.Second}, false, nil } - if bastion.PublicIP == "" { + if stackitCluster.Status.Bastion.PublicIP == "" { clusterScope.SetNotReady( "Provisioning", "waiting for bastion public IP address", @@ -188,6 +184,32 @@ func hasBastionStatus(status infrav1.StackitBastionStatus) bool { return status.ServerID != "" || status.PublicIPID != "" || status.PublicIP != "" || status.SecurityGroupID != "" } +func (r *StackitClusterReconciler) deleteBastionResources( + ctx context.Context, + cloudClient cloud.Client, + clusterScope *scope.ClusterScope, + input cloud.BastionInput, + status cloud.Bastion, + ignoreNotFound bool, +) error { + if err := cloudClient.DeleteNodeSSHAccess(ctx, bastionservice.NodeSSHAccessTags(clusterScope.StackitCluster)); err != nil { + if !ignoreNotFound || !cloud.IsNotFound(err) { + return err + } + } + if err := publicip.New(clusterScope).Delete(ctx); err != nil { + if !ignoreNotFound || !cloud.IsNotFound(err) { + return err + } + } + if err := cloudClient.DeleteBastion(ctx, input, status); err != nil { + if !ignoreNotFound || !cloud.IsNotFound(err) { + return err + } + } + return nil +} + func bastionNeedsRecreate(stackitCluster *infrav1.StackitCluster, cloudInit []byte) bool { if !hasBastionStatus(stackitCluster.Status.Bastion) { return false diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index f4c4f8e..f2c55e8 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -33,6 +33,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud" bastionservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/bastion" loadbalancerservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -66,6 +67,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS infrav1.ClusterReadyCondition, ) } + clusterScope.SetCloudClient(cloudClient) clusterScope.SetConditions( metav1.ConditionTrue, "Available", @@ -412,6 +414,7 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS ) return ctrl.Result{}, err } + clusterScope.SetCloudClient(cloudClient) loadBalancerID, err := loadbalancerservice.ResolveID(ctx, cloudClient, stackitCluster) if err != nil { log.Error(err, "Failed to resolve API server load balancer for deletion", "cluster", stackitCluster.Name) @@ -438,10 +441,15 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS "Failed to delete bastion node SSH access: %v", err) return ctrl.Result{}, err } + hadBastion := hasBastionStatus(stackitCluster.Status.Bastion) + if err := publicip.New(clusterScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { + log.Error(err, "Failed to delete bastion public IP", "cluster", stackitCluster.Name) + record.Warnf(stackitCluster, "FailedDeleteBastion", + "Failed to delete bastion public IP: %v", err) + return ctrl.Result{}, err + } if err := cloudClient.DeleteBastion(ctx, bastionservice.Input(stackitCluster, nil), cloud.Bastion{ ServerID: stackitCluster.Status.Bastion.ServerID, - PublicIPID: stackitCluster.Status.Bastion.PublicIPID, - PublicIP: stackitCluster.Status.Bastion.PublicIP, SecurityGroupID: stackitCluster.Status.Bastion.SecurityGroupID, }); err != nil && !cloud.IsNotFound(err) { log.Error(err, "Failed to delete bastion", "cluster", stackitCluster.Name) @@ -449,7 +457,7 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS "Failed to delete bastion: %v", err) return ctrl.Result{}, err } - if hasBastionStatus(stackitCluster.Status.Bastion) { + if hadBastion { clusterScope.ClearBastionStatus() record.Event(stackitCluster, "BastionDeleted", "Deleted bastion") } diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index d1a4705..12fc45a 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -25,6 +25,7 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "github.com/stackitcloud/cluster-api-provider-stackit/util" + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" ) // ClusterScope holds the per-reconcile state for a StackitCluster. @@ -32,6 +33,7 @@ type ClusterScope struct { Client client.Client Cluster *clusterv1.Cluster StackitCluster *infrav1.StackitCluster + CloudClient cloud.Client patchHelper *patch.Helper } @@ -87,13 +89,9 @@ func (s *ClusterScope) SetAPIServerEndpoint(endpoint clusterv1.APIEndpoint) { } func (s *ClusterScope) SetBastionStatus(bastion *cloud.Bastion, cloudInitHash string) { - s.StackitCluster.Status.Bastion = infrav1.StackitBastionStatus{ - ServerID: bastion.ServerID, - PublicIPID: bastion.PublicIPID, - PublicIP: bastion.PublicIP, - SecurityGroupID: bastion.SecurityGroupID, - CloudInitHash: cloudInitHash, - } + s.StackitCluster.Status.Bastion.ServerID = bastion.ServerID + s.StackitCluster.Status.Bastion.SecurityGroupID = bastion.SecurityGroupID + s.StackitCluster.Status.Bastion.CloudInitHash = cloudInitHash } func (s *ClusterScope) ClearBastionStatus() { @@ -103,3 +101,48 @@ func (s *ClusterScope) ClearBastionStatus() { func (s *ClusterScope) SetFailureDomains(failureDomains []clusterv1.FailureDomain) { s.StackitCluster.Status.FailureDomains = failureDomains } + +func (s *ClusterScope) Cloud() cloud.Client { + return s.CloudClient +} + +func (s *ClusterScope) SetCloudClient(cloudClient cloud.Client) { + s.CloudClient = cloudClient +} + +func (s *ClusterScope) ProjectID() string { + if s.StackitCluster.Spec.ProjectID != "" { + return s.StackitCluster.Spec.ProjectID + } + return s.CloudClient.ProjectID() +} + +func (s *ClusterScope) Region() string { + if s.StackitCluster.Spec.Region != "" { + return s.StackitCluster.Spec.Region + } + return s.CloudClient.Region() +} + +func (s *ClusterScope) IaaSClient() *iaas.APIClient { + return s.CloudClient.IaaSClient() +} + +func (s *ClusterScope) BastionSpec() infrav1.StackitBastionSpec { + return s.StackitCluster.Spec.Bastion +} + +func (s *ClusterScope) BastionStatus() infrav1.StackitBastionStatus { + return s.StackitCluster.Status.Bastion +} + +func (s *ClusterScope) BastionTags() map[string]string { + tags := util.ClusterTags(s.StackitCluster.Name, s.StackitCluster.Namespace, s.StackitCluster.Spec.AdditionalLabels) + tags[util.LabelResourceRole] = util.ResourceRoleBastion + return tags +} + +func (s *ClusterScope) SetBastionPublicIP(id, ip string) { + s.StackitCluster.Status.Bastion.PublicIPID = id + s.StackitCluster.Status.Bastion.PublicIP = ip +} diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index 224d56a..d753b39 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -34,6 +34,7 @@ import ( . "github.com/onsi/gomega" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/test/utils" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -490,7 +491,7 @@ var _ = Describe("Manager", Ordered, func() { Eventually(func(g Gomega) { output := kubectlOutput(g, "get", "stackitcluster", clusterName, "-n", cfg.Namespace, "-o", "jsonpath={.status.bastion.publicIP}") g.Expect(output).NotTo(BeEmpty()) - publicIPs, err := cloudClient.ListPublicIPsByTags(ctx, stackitE2EBastionTags(testID)) + publicIPs, err := publicip.ListByTags(ctx, cloudClient.IaaSClient(), cloudClient.ProjectID(), cloudClient.Region(), stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) g.Expect(publicIPs).To(HaveLen(1)) securityGroups, err := cloudClient.ListSecurityGroupsByTags(ctx, stackitE2EBastionTags(testID)) @@ -556,7 +557,7 @@ var _ = Describe("Manager", Ordered, func() { loadBalancers, err := cloudClient.ListAPIServerLoadBalancersByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(loadBalancers).To(BeEmpty()) - publicIPs, err := cloudClient.ListPublicIPsByTags(ctx, stackitE2EBastionTags(testID)) + publicIPs, err := publicip.ListByTags(ctx, cloudClient.IaaSClient(), cloudClient.ProjectID(), cloudClient.Region(), stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) g.Expect(publicIPs).To(BeEmpty()) securityGroups, err := cloudClient.ListSecurityGroupsByTags(ctx, stackitE2EBastionTags(testID)) diff --git a/util/conditions.go b/util/conditions.go index f60b2bf..d22050e 100644 --- a/util/conditions.go +++ b/util/conditions.go @@ -14,11 +14,10 @@ import ( "errors" "time" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" - - "github.com/stackitcloud/cluster-api-provider-stackit/cloud" ) // SetCondition adds or updates a condition on conditions. From 235e1292fbdc51b975b29c8b71161bafcccd9ede Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Wed, 28 Oct 2026 13:54:43 +0100 Subject: [PATCH 02/60] fix: add legacy event api permssion to controller role --- config/rbac/role.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index b29226e..ba165ea 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -29,6 +29,13 @@ rules: verbs: - create - patch +- apiGroups: + - "" + resources: + - events + verbs: + - create + - patch - apiGroups: - infrastructure.cluster.x-k8s.io resources: From 36f9f8b6e44bb0d8074c68cc4ba30dc689330eb3 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Wed, 28 Oct 2026 14:15:57 +0100 Subject: [PATCH 03/60] feat: create bastion host in a single availability zone and not metro --- cloud/fake/client.go | 15 ++++- cloud/sdk_client.go | 23 +++---- cloud/sdk_client_test.go | 20 +++--- cloud/services/bastion/bastion.go | 20 +++--- cloud/services/bastion/bastion_test.go | 68 ++++++++++++++++++++ cloud/types.go | 23 +++---- config/rbac/role.yaml | 20 ++---- controller/stackitcluster_controller.go | 1 + controller/stackitcluster_controller_test.go | 19 ++++++ controller/stackitmachine_controller.go | 1 + docs/src/topics/failure-domains.md | 5 ++ scope/cluster_scope.go | 23 +++++++ scope/machine_scope.go | 16 +---- 13 files changed, 183 insertions(+), 71 deletions(-) create mode 100644 cloud/services/bastion/bastion_test.go diff --git a/cloud/fake/client.go b/cloud/fake/client.go index 531ae31..0f5c589 100644 --- a/cloud/fake/client.go +++ b/cloud/fake/client.go @@ -75,6 +75,9 @@ type Client struct { // LastCreateServerInput records the most recent input passed to CreateServer. LastCreateServerInput cloud.CreateServerInput + // LastEnsureBastionInput records the most recent input passed to EnsureBastion. + LastEnsureBastionInput cloud.BastionInput + // Before* hooks, if non-nil, run before the call they belong to. They let a // test observe API server state at the exact moment a cloud call would // happen. Unlike FailNext*, they are not consumed. @@ -316,10 +319,16 @@ func (c *Client) EnsureBastion(_ context.Context, input cloud.BastionInput) (*cl } serverID := c.genID() + c.LastEnsureBastionInput = input + zone := input.AvailabilityZone + if zone == "" { + zone = "eu01-1" + } server := &cloud.Server{ - ID: serverID, - Name: input.Name, - State: "ACTIVE", + ID: serverID, + Name: input.Name, + State: "ACTIVE", + AvailabilityZone: zone, Addresses: []cloud.Address{ {Type: "InternalIP", Address: "10.0.0.20"}, }, diff --git a/cloud/sdk_client.go b/cloud/sdk_client.go index 0fa1751..531b7ec 100644 --- a/cloud/sdk_client.go +++ b/cloud/sdk_client.go @@ -268,17 +268,18 @@ func (c *SDKClient) EnsureBastion(ctx context.Context, input BastionInput) (*Bas } server, err := c.CreateServer(ctx, CreateServerInput{ - Name: input.Name, - ProjectID: input.ProjectID, - Region: input.Region, - ImageID: input.ImageID, - MachineType: input.MachineType, - SSHKeyName: input.SSHKeyName, - NetworkID: input.NetworkID, - SecurityGroups: []string{securityGroup.ID}, - UserData: input.CloudInit, - Tags: input.Tags, - RootVolume: input.RootVolume, + Name: input.Name, + ProjectID: input.ProjectID, + Region: input.Region, + ImageID: input.ImageID, + MachineType: input.MachineType, + AvailabilityZone: input.AvailabilityZone, + SSHKeyName: input.SSHKeyName, + NetworkID: input.NetworkID, + SecurityGroups: []string{securityGroup.ID}, + UserData: input.CloudInit, + Tags: input.Tags, + RootVolume: input.RootVolume, }) if err != nil { return nil, err diff --git a/cloud/sdk_client_test.go b/cloud/sdk_client_test.go index ad0c9e7..3e235b7 100644 --- a/cloud/sdk_client_test.go +++ b/cloud/sdk_client_test.go @@ -512,15 +512,16 @@ func TestSDKClientEnsureBastionToleratesDuplicateSecurityGroupAttach(t *testing. client := newTestSDKClient(t, server.URL) bastion, err := client.EnsureBastion(context.Background(), BastionInput{ - Name: "bastion", - ProjectID: testSDKProjectID, - Region: testSDKRegion, - NetworkID: testSDKNetworkID, - ImageID: testSDKImageID, - MachineType: "c2i.1", - SSHKeyName: "default", - AllowedCIDRs: []string{"203.0.113.0/24"}, - Tags: map[string]string{"cluster": "test"}, + Name: "bastion", + ProjectID: testSDKProjectID, + Region: testSDKRegion, + NetworkID: testSDKNetworkID, + ImageID: testSDKImageID, + MachineType: "c2i.1", + AvailabilityZone: "eu01-1", + SSHKeyName: "default", + AllowedCIDRs: []string{"203.0.113.0/24"}, + Tags: map[string]string{"cluster": "test"}, }) if err != nil { t.Fatalf("EnsureBastion() error = %v", err) @@ -529,6 +530,7 @@ func TestSDKClientEnsureBastionToleratesDuplicateSecurityGroupAttach(t *testing. if attachCallCount != 1 { t.Fatalf("attach attempted %d time(s), want exactly 1 (the idempotent re-attach)", attachCallCount) } + assertStringField(t, createPayload, "availabilityZone", "eu01-1") groups, _ := createPayload["securityGroups"].([]any) if len(groups) != 1 || groups[0] != testSDKSecurityGroup { t.Fatalf("create payload securityGroups = %v, want [%s]", createPayload["securityGroups"], testSDKSecurityGroup) diff --git a/cloud/services/bastion/bastion.go b/cloud/services/bastion/bastion.go index 6b7f5d9..80eab63 100644 --- a/cloud/services/bastion/bastion.go +++ b/cloud/services/bastion/bastion.go @@ -13,6 +13,7 @@ package bastion import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -26,15 +27,16 @@ func Input(stackitCluster *infrav1.StackitCluster, cloudInit []byte) cloud.Basti tags[util.LabelResourceRole] = util.ResourceRoleBastion return cloud.BastionInput{ - Name: stackitCluster.Name + "-bastion", - ProjectID: stackitCluster.Spec.ProjectID, - Region: stackitCluster.Spec.Region, - NetworkID: stackitCluster.Spec.Network.ID, - ImageID: stackitCluster.Spec.Bastion.ImageID, - MachineType: stackitCluster.Spec.Bastion.MachineType, - SSHKeyName: stackitCluster.Spec.Bastion.SSHKeyName, - AllowedCIDRs: stackitCluster.Spec.Bastion.AllowedCIDRs, - Tags: tags, + Name: stackitCluster.Name + "-bastion", + ProjectID: stackitCluster.Spec.ProjectID, + Region: stackitCluster.Spec.Region, + NetworkID: stackitCluster.Spec.Network.ID, + ImageID: stackitCluster.Spec.Bastion.ImageID, + MachineType: stackitCluster.Spec.Bastion.MachineType, + AvailabilityZone: scope.DefaultAvailabilityZone(stackitCluster), + SSHKeyName: stackitCluster.Spec.Bastion.SSHKeyName, + AllowedCIDRs: stackitCluster.Spec.Bastion.AllowedCIDRs, + Tags: tags, RootVolume: cloud.RootVolumeInput{ SizeGiB: stackitCluster.Spec.Bastion.RootVolume.SizeGiB, PerformanceClass: stackitCluster.Spec.Bastion.RootVolume.PerformanceClass, diff --git a/cloud/services/bastion/bastion_test.go b/cloud/services/bastion/bastion_test.go new file mode 100644 index 0000000..88722a1 --- /dev/null +++ b/cloud/services/bastion/bastion_test.go @@ -0,0 +1,68 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package bastion + +import ( + "testing" + + clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" + + infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" +) + +func TestInputAvailabilityZone(t *testing.T) { + tests := []struct { + name string + region string + failureDomains []clusterv1.FailureDomain + wantAZ string + }{ + { + name: "selects first failure domain sorted alphabetically", + region: "eu01", + failureDomains: []clusterv1.FailureDomain{ + {Name: "eu01-3"}, + {Name: "eu01-2"}, + {Name: "eu01-1"}, + }, + wantAZ: "eu01-1", + }, + { + name: "falls back to region-1 when failureDomains is empty", + region: "eu02", + failureDomains: nil, + wantAZ: "eu02-1", + }, + { + name: "returns empty when region and failureDomains are empty", + region: "", + failureDomains: nil, + wantAZ: "", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + cluster := &infrav1.StackitCluster{ + Spec: infrav1.StackitClusterSpec{ + Region: test.region, + }, + Status: infrav1.StackitClusterStatus{ + FailureDomains: test.failureDomains, + }, + } + input := Input(cluster, nil) + if input.AvailabilityZone != test.wantAZ { + t.Fatalf("Input().AvailabilityZone = %q, want %q", input.AvailabilityZone, test.wantAZ) + } + }) + } +} diff --git a/cloud/types.go b/cloud/types.go index 92a63dc..6cca29a 100644 --- a/cloud/types.go +++ b/cloud/types.go @@ -75,17 +75,18 @@ type CreateServerInput struct { // BastionInput holds all parameters required to ensure a bastion host. type BastionInput struct { - Name string - ProjectID string - Region string - NetworkID string - ImageID string - MachineType string - SSHKeyName string - AllowedCIDRs []string - Tags map[string]string - RootVolume RootVolumeInput - CloudInit []byte + Name string + ProjectID string + Region string + NetworkID string + ImageID string + MachineType string + AvailabilityZone string + SSHKeyName string + AllowedCIDRs []string + Tags map[string]string + RootVolume RootVolumeInput + CloudInit []byte } // NodeSSHAccessInput holds parameters required to allow SSH from the bastion diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index ba165ea..75a1891 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -14,15 +14,7 @@ rules: - list - watch - apiGroups: - - cluster.x-k8s.io - resources: - - clusters - - machines - verbs: - - get - - list - - watch -- apiGroups: + - "" - events.k8s.io resources: - events @@ -30,12 +22,14 @@ rules: - create - patch - apiGroups: - - "" + - cluster.x-k8s.io resources: - - events + - clusters + - machines verbs: - - create - - patch + - get + - list + - watch - apiGroups: - infrastructure.cluster.x-k8s.io resources: diff --git a/controller/stackitcluster_controller.go b/controller/stackitcluster_controller.go index a9e4a3c..8b35147 100644 --- a/controller/stackitcluster_controller.go +++ b/controller/stackitcluster_controller.go @@ -62,6 +62,7 @@ type StackitClusterReconciler struct { // +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch // +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch // +kubebuilder:rbac:groups=events.k8s.io,resources=events,verbs=create;patch +// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch func (r *StackitClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, err error) { log := logf.FromContext(ctx) diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index 65077b2..ccd6cf1 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -986,6 +986,25 @@ var _ = Describe("StackitCluster Controller", func() { })) }) + It("creates the bastion host in a single availability zone and not in metro (-m)", func() { + fakeCloud.AvailabilityZones = []string{"eu01-m", "eu01-3", "eu01-2"} + + got := &infrav1.StackitCluster{} + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + got.Spec.Bastion = validBastionSpec() + Expect(k8sClient.Update(ctx, got)).To(Succeed()) + + result, err := reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + Expect(result).To(Equal(reconcile.Result{})) + + Expect(fakeCloud.LastEnsureBastionInput.AvailabilityZone).To(Equal("eu01-2")) + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + server, err := fakeCloud.GetServer(ctx, got.Status.Bastion.ServerID) + Expect(err).NotTo(HaveOccurred()) + Expect(server.AvailabilityZone).To(Equal("eu01-2")) + }) + It("reports an error condition when listing availability zones fails", func() { fakeCloud.FailNextListAZ = fmt.Errorf("cloud failure: %w", cloud.ErrTransient) diff --git a/controller/stackitmachine_controller.go b/controller/stackitmachine_controller.go index 30688dd..cf71067 100644 --- a/controller/stackitmachine_controller.go +++ b/controller/stackitmachine_controller.go @@ -53,6 +53,7 @@ type StackitMachineReconciler struct { // +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines,verbs=get;list;watch // +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch // +kubebuilder:rbac:groups=events.k8s.io,resources=events,verbs=create;patch +// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch func (r *StackitMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, err error) { log := logf.FromContext(ctx) diff --git a/docs/src/topics/failure-domains.md b/docs/src/topics/failure-domains.md index 113e1fb..79e2ad3 100644 --- a/docs/src/topics/failure-domains.md +++ b/docs/src/topics/failure-domains.md @@ -51,6 +51,11 @@ using the following precedence: The actual zone where the VM was placed is surfaced on `StackitMachine.status.availabilityZone`. +When `StackitCluster.spec.bastion.enabled` is `true`, the provider-managed bastion +host is also placed into the cluster's default availability zone (the first +sorted published failure domain, e.g., `eu01-1`) so it is never created in a +metro zone (`-m`). + ## Worker Node Spreading Unlike the control plane, worker nodes do not automatically spread across failure domains diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index 12fc45a..0bb4c8b 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -15,6 +15,7 @@ package scope import ( "context" + "slices" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -102,6 +103,28 @@ func (s *ClusterScope) SetFailureDomains(failureDomains []clusterv1.FailureDomai s.StackitCluster.Status.FailureDomains = failureDomains } +// DefaultAvailabilityZone returns the default single availability zone for a +// StackitCluster (the alphabetically first published failure domain, falling +// back to -1). This prevents the STACKIT IaaS API from defaulting to +// the expensive metro zone (-m). +func DefaultAvailabilityZone(stackitCluster *infrav1.StackitCluster) string { + if stackitCluster == nil { + return "" + } + if len(stackitCluster.Status.FailureDomains) > 0 { + zones := make([]string, 0, len(stackitCluster.Status.FailureDomains)) + for _, fd := range stackitCluster.Status.FailureDomains { + zones = append(zones, fd.Name) + } + slices.Sort(zones) + return zones[0] + } + if stackitCluster.Spec.Region != "" { + return stackitCluster.Spec.Region + "-1" + } + return "" +} + func (s *ClusterScope) Cloud() cloud.Client { return s.CloudClient } diff --git a/scope/machine_scope.go b/scope/machine_scope.go index 58bafde..b736ce1 100644 --- a/scope/machine_scope.go +++ b/scope/machine_scope.go @@ -12,7 +12,6 @@ package scope import ( "context" - "slices" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -128,18 +127,5 @@ func (s *MachineScope) AvailabilityZone() string { return s.StackitMachine.Spec.AvailabilityZone } - if s.StackitCluster != nil && len(s.StackitCluster.Status.FailureDomains) > 0 { - zones := make([]string, 0, len(s.StackitCluster.Status.FailureDomains)) - for _, fd := range s.StackitCluster.Status.FailureDomains { - zones = append(zones, fd.Name) - } - slices.Sort(zones) - return zones[0] - } - - if s.StackitCluster != nil && s.StackitCluster.Spec.Region != "" { - return s.StackitCluster.Spec.Region + "-1" - } - - return "" + return DefaultAvailabilityZone(s.StackitCluster) } From 1f76439b48f30aaac969faa1447e75e6e0217beb Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Thu, 29 Oct 2026 11:33:01 +0100 Subject: [PATCH 04/60] docs: removed comments --- cloud/services/publicip/reconcile.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index d3f947c..449ee75 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -20,7 +20,6 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) -// Reconcile reconciles cluster public IP resources. func (s *Service) Reconcile(ctx context.Context) error { log := logf.FromContext(ctx) if !s.scope.BastionSpec().Enabled { @@ -56,7 +55,6 @@ func (s *Service) Reconcile(ctx context.Context) error { return nil } -// Delete deletes cluster public IP resources. func (s *Service) Delete(ctx context.Context) error { log := logf.FromContext(ctx) log.V(1).Info("Deleting public IP resources") From 84012bf11493a9ae497eb4d186eaf5917bdbd562 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 30 Oct 2026 10:02:50 +0100 Subject: [PATCH 05/60] feat: refactor security groups --- cloud/cleanup.go | 13 + cloud/cleanup_test.go | 20 + cloud/client.go | 6 - cloud/fake/client.go | 553 ++++++++++--- cloud/sdk_client.go | 383 +-------- cloud/sdk_client_test.go | 166 +--- cloud/services/bastion/bastion.go | 7 +- cloud/services/securitygroup/reconcile.go | 323 ++++++++ .../services/securitygroup/reconcile_test.go | 743 ++++++++++++++++++ cloud/services/securitygroup/rules.go | 198 +++++ cloud/services/securitygroup/service.go | 102 +++ cloud/types.go | 16 +- controller/stackitcluster_bastion.go | 24 +- controller/stackitcluster_controller_test.go | 66 +- controller/stackitcluster_infrastructure.go | 18 +- controller/stackitmachine_controller_test.go | 17 +- controller/stackitmachine_infrastructure.go | 14 +- scope/cluster_scope.go | 79 +- scope/machine_scope.go | 35 + test/e2e/e2e_test.go | 10 +- test/utils/utils.go | 9 + 21 files changed, 2106 insertions(+), 696 deletions(-) create mode 100644 cloud/services/securitygroup/reconcile.go create mode 100644 cloud/services/securitygroup/reconcile_test.go create mode 100644 cloud/services/securitygroup/rules.go create mode 100644 cloud/services/securitygroup/service.go diff --git a/cloud/cleanup.go b/cloud/cleanup.go index 3d635e0..481f3de 100644 --- a/cloud/cleanup.go +++ b/cloud/cleanup.go @@ -17,6 +17,7 @@ import ( "strings" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" ) // CleanupByTags deletes provider-managed cloud resources matching tags without @@ -76,6 +77,18 @@ func CleanupByTags(ctx context.Context, client Client, tags map[string]string) e } } + sgClient := securitygroup.NewClient(client) + securityGroups, err := sgClient.ListByTags(ctx, tags) + if err != nil { + errs = append(errs, err.Error()) + } else { + for _, sg := range securityGroups { + if err := sgClient.DeleteByID(ctx, sg.ID); err != nil { + errs = append(errs, fmt.Sprintf("delete security group %q: %v", sg.ID, err)) + } + } + } + if len(errs) > 0 { return fmt.Errorf("%w: %s", ErrTransient, strings.Join(errs, "; ")) } diff --git a/cloud/cleanup_test.go b/cloud/cleanup_test.go index 40f2304..2fe2dc0 100644 --- a/cloud/cleanup_test.go +++ b/cloud/cleanup_test.go @@ -42,6 +42,12 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { if _, err := client.CreatePublicIP(ctx, otherTags); err != nil { t.Fatalf("CreatePublicIP() error = %v", err) } + if _, err := client.CreateSecurityGroup(ctx, "matched-sg", tags); err != nil { + t.Fatalf("CreateSecurityGroup() error = %v", err) + } + if _, err := client.CreateSecurityGroup(ctx, "other-sg", otherTags); err != nil { + t.Fatalf("CreateSecurityGroup() error = %v", err) + } if _, err := client.EnsureAPIServerLoadBalancer(ctx, cloud.LoadBalancerInput{ Name: "matched", Tags: tags, @@ -106,4 +112,18 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { if len(otherPublicIPs) != 1 { t.Fatalf("other public IP count = %d, want 1", len(otherPublicIPs)) } + matchedSecurityGroups, err := client.ListSecurityGroupsByTags(ctx, tags) + if err != nil { + t.Fatalf("ListSecurityGroupsByTags() error = %v", err) + } + if len(matchedSecurityGroups) != 0 { + t.Fatalf("matched security group count = %d, want 0", len(matchedSecurityGroups)) + } + otherSecurityGroups, err := client.ListSecurityGroupsByTags(ctx, otherTags) + if err != nil { + t.Fatalf("ListSecurityGroupsByTags(other) error = %v", err) + } + if len(otherSecurityGroups) != 1 { + t.Fatalf("other security group count = %d, want 1", len(otherSecurityGroups)) + } } diff --git a/cloud/client.go b/cloud/client.go index 7e5bd73..40b657a 100644 --- a/cloud/client.go +++ b/cloud/client.go @@ -55,12 +55,6 @@ type Client interface { DeleteBastion(ctx context.Context, input BastionInput, status Bastion) error - EnsureNodeSSHAccess(ctx context.Context, input NodeSSHAccessInput) (*SecurityGroup, error) - - DeleteNodeSSHAccess(ctx context.Context, tags map[string]string) error - - ListSecurityGroupsByTags(ctx context.Context, tags map[string]string) ([]*SecurityGroup, error) - EnsureAPIServerLoadBalancer(ctx context.Context, input LoadBalancerInput) (*LoadBalancer, error) ListAPIServerLoadBalancersByTags(ctx context.Context, tags map[string]string) ([]*LoadBalancer, error) diff --git a/cloud/fake/client.go b/cloud/fake/client.go index 0f5c589..5671191 100644 --- a/cloud/fake/client.go +++ b/cloud/fake/client.go @@ -24,6 +24,7 @@ import ( "sync" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/stackit-sdk-go/core/config" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" ) @@ -48,25 +49,24 @@ type Client struct { // failure injection: if non-nil, the next call returns this error and the // field is cleared. - FailNextCreateServer error - FailNextDeleteServer error - FailNextGetServer error - FailNextFindServer error - FailNextEnsureLB error - FailNextDeleteLB error - FailNextSetTargets error - FailNextGetNetwork error - FailNextEnsureBastion error - FailNextDeleteBastion error - FailNextEnsureNodeSSH error - FailNextDeleteNodeSSH error - FailNextListAZ error - FailNextGetPublicIP error - FailNextListPublicIPs error - FailNextCreatePublicIP error - FailNextDeletePublicIP error - FailNextAddPublicIP error - FailNextRemovePublicIP error + FailNextCreateServer error + FailNextDeleteServer error + FailNextGetServer error + FailNextFindServer error + FailNextEnsureLB error + FailNextDeleteLB error + FailNextSetTargets error + FailNextGetNetwork error + FailNextEnsureBastion error + FailNextDeleteBastion error + FailNextAddSecurityGroup error + FailNextListAZ error + FailNextGetPublicIP error + FailNextListPublicIPs error + FailNextCreatePublicIP error + FailNextDeletePublicIP error + FailNextAddPublicIP error + FailNextRemovePublicIP error // AvailabilityZones is returned by ListAvailabilityZones if non-empty. // Defaults to eu01-1, eu01-2, eu01-3. @@ -86,9 +86,9 @@ type Client struct { // CreateServerCalls counts successful CreateServer calls (for idempotency // assertions). - CreateServerCalls int - EnsureBastionCalls int - EnsureNodeSSHCalls int + CreateServerCalls int + EnsureBastionCalls int + AddSecurityGroupCalls int } type serverEntry struct { @@ -109,11 +109,18 @@ type publicIPEntry struct { tags map[string]string } +type securityGroupRuleEntry struct { + id string + securitygroup.Rule +} + type securityGroupEntry struct { securityGroup *cloud.SecurityGroup + description string tags map[string]string allowedCIDRs []string remoteSecurityGroups []string + rules map[string]*securityGroupRuleEntry } // New returns a Client preconfigured with the given networks. @@ -295,29 +302,27 @@ func (c *Client) EnsureBastion(_ context.Context, input cloud.BastionInput) (*cl if err := consume(&c.FailNextEnsureBastion); err != nil { return nil, err } + securityGroupID := input.SecurityGroupID + if securityGroupID == "" { + securityGroupID = idOfSecurityGroup(c.securityGroupByTags(input.Tags)) + } for _, serverEntry := range c.servers { if len(input.Tags) > 0 && mapContains(serverEntry.tags, input.Tags) { - securityGroup := c.securityGroupByTags(input.Tags) + if securityGroupID != "" { + if serverEntry.securityGroups == nil { + serverEntry.securityGroups = map[string]struct{}{} + } + serverEntry.securityGroups[securityGroupID] = struct{}{} + } return &cloud.Bastion{ ServerID: serverEntry.server.ID, ServerState: serverEntry.server.State, ServerPowerStatus: serverEntry.server.PowerStatus, - SecurityGroupID: idOfSecurityGroup(securityGroup), + SecurityGroupID: securityGroupID, }, nil } } - securityGroupID := c.genID() - securityGroup := &cloud.SecurityGroup{ - ID: securityGroupID, - Name: input.Name + "-ssh", - } - c.securityGroups[securityGroupID] = &securityGroupEntry{ - securityGroup: securityGroup, - tags: copyTags(input.Tags), - allowedCIDRs: append([]string(nil), input.AllowedCIDRs...), - } - serverID := c.genID() c.LastEnsureBastionInput = input zone := input.AvailabilityZone @@ -333,10 +338,14 @@ func (c *Client) EnsureBastion(_ context.Context, input cloud.BastionInput) (*cl {Type: "InternalIP", Address: "10.0.0.20"}, }, } + var sgSet map[string]struct{} + if securityGroupID != "" { + sgSet = securityGroupSet([]string{securityGroupID}) + } c.servers[serverID] = &serverEntry{ server: server, tags: copyTags(input.Tags), - securityGroups: securityGroupSet([]string{securityGroupID}), + securityGroups: sgSet, userData: append([]byte(nil), input.CloudInit...), } @@ -358,74 +367,11 @@ func (c *Client) DeleteBastion(_ context.Context, input cloud.BastionInput, stat if status.ServerID != "" { delete(c.servers, status.ServerID) } - if status.SecurityGroupID != "" { - delete(c.securityGroups, status.SecurityGroupID) - } for id, entry := range c.servers { - if mapContains(entry.tags, input.Tags) { + if len(input.Tags) > 0 && mapContains(entry.tags, input.Tags) { delete(c.servers, id) } } - for id, entry := range c.securityGroups { - if mapContains(entry.tags, input.Tags) { - delete(c.securityGroups, id) - } - } - return nil -} - -func (c *Client) EnsureNodeSSHAccess(_ context.Context, input cloud.NodeSSHAccessInput) (*cloud.SecurityGroup, error) { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextEnsureNodeSSH); err != nil { - return nil, err - } - serverEntry, ok := c.servers[input.ServerID] - if !ok { - return nil, fmt.Errorf("server %q: %w", input.ServerID, cloud.ErrNotFound) - } - securityGroup := c.securityGroupByTags(input.Tags) - if securityGroup == nil { - securityGroup = &cloud.SecurityGroup{ - ID: c.genID(), - Name: input.Name, - } - c.securityGroups[securityGroup.ID] = &securityGroupEntry{ - securityGroup: securityGroup, - tags: copyTags(input.Tags), - remoteSecurityGroups: []string{input.BastionSecurityGroupID}, - } - } else { - entry := c.securityGroups[securityGroup.ID] - if !containsString(entry.remoteSecurityGroups, input.BastionSecurityGroupID) { - entry.remoteSecurityGroups = append(entry.remoteSecurityGroups, input.BastionSecurityGroupID) - } - } - if serverEntry.securityGroups == nil { - serverEntry.securityGroups = map[string]struct{}{} - } - serverEntry.securityGroups[securityGroup.ID] = struct{}{} - c.EnsureNodeSSHCalls++ - out := *securityGroup - return &out, nil -} - -func (c *Client) DeleteNodeSSHAccess(_ context.Context, tags map[string]string) error { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextDeleteNodeSSH); err != nil { - return err - } - for id, entry := range c.securityGroups { - if mapContains(entry.tags, tags) { - delete(c.securityGroups, id) - for _, server := range c.servers { - delete(server.securityGroups, id) - } - } - } return nil } @@ -560,6 +506,18 @@ func (c *Client) ListSecurityGroupsByTags(_ context.Context, tags map[string]str return securityGroups, nil } +func (c *Client) CreateSecurityGroup(_ context.Context, name string, tags map[string]string) (*cloud.SecurityGroup, error) { + c.mu.Lock() + defer c.mu.Unlock() + + entry, err := c.createSecurityGroupEntry(name, "", tags) + if err != nil { + return nil, err + } + out := *entry.securityGroup + return &out, nil +} + func (c *Client) EnsureAPIServerLoadBalancer( _ context.Context, input cloud.LoadBalancerInput, @@ -736,6 +694,21 @@ func (c *Client) SecurityGroupRemoteSources(securityGroupID string) []string { return append([]string(nil), entry.remoteSecurityGroups...) } +// SecurityGroupRules returns the rules currently stored on a fake security group. +func (c *Client) SecurityGroupRules(securityGroupID string) []securitygroup.Rule { + c.mu.Lock() + defer c.mu.Unlock() + entry, ok := c.securityGroups[securityGroupID] + if !ok { + return nil + } + rules := make([]securitygroup.Rule, 0, len(entry.rules)) + for _, r := range entry.rules { + rules = append(rules, r.Rule) + } + return rules +} + // LoadBalancerTargetCount returns the number of targets in one load balancer // target pool (test helper). func (c *Client) LoadBalancerTargetCount(id string) int { @@ -850,6 +823,17 @@ func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { func (c *Client) roundTripIaaS(r *http.Request) (*http.Response, error) { rec := httptest.NewRecorder() rec.Header().Set("Content-Type", "application/json") + + if c.handlePublicIPRequest(rec, r) || c.handleSecurityGroupRequest(rec, r) { + return rec.Result(), nil + } + + rec.WriteHeader(http.StatusNotFound) + _ = json.NewEncoder(rec).Encode(map[string]any{"message": "not found"}) + return rec.Result(), nil +} + +func (c *Client) handlePublicIPRequest(rec *httptest.ResponseRecorder, r *http.Request) bool { path := r.URL.Path switch { @@ -866,11 +850,8 @@ func (c *Client) roundTripIaaS(r *http.Request) (*http.Response, error) { default: err = cloud.ErrInvalidInput } - if err != nil { - writeFakeError(rec, err) - } else { - rec.WriteHeader(http.StatusNoContent) - } + writeFakeStatus(rec, err) + return true case r.Method == http.MethodPost && strings.HasSuffix(path, "/public-ips"): var body struct { @@ -885,6 +866,7 @@ func (c *Client) roundTripIaaS(r *http.Request) (*http.Response, error) { } else { _ = json.NewEncoder(rec).Encode(fakePublicIPMap(created, body.Labels)) } + return true case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips"): tags := parseLabelSelector(r.URL.Query().Get("label_selector")) @@ -894,6 +876,7 @@ func (c *Client) roundTripIaaS(r *http.Request) (*http.Response, error) { } else { _ = json.NewEncoder(rec).Encode(map[string]any{"items": items}) } + return true case r.Method == http.MethodGet && strings.Contains(path, "/public-ips/"): id := path[strings.LastIndex(path, "/public-ips/")+len("/public-ips/"):] @@ -903,21 +886,151 @@ func (c *Client) roundTripIaaS(r *http.Request) (*http.Response, error) { } else { _ = json.NewEncoder(rec).Encode(fakePublicIPMap(got, nil)) } + return true case r.Method == http.MethodDelete && strings.Contains(path, "/public-ips/"): id := path[strings.LastIndex(path, "/public-ips/")+len("/public-ips/"):] - if err := c.DeletePublicIP(r.Context(), id); err != nil { + writeFakeStatus(rec, c.DeletePublicIP(r.Context(), id)) + return true + + default: + return false + } +} + +func (c *Client) handleSecurityGroupRequest(rec *httptest.ResponseRecorder, r *http.Request) bool { + path := r.URL.Path + + switch { + case strings.Contains(path, "/servers/") && strings.Contains(path, "/security-groups/"): + parts := strings.Split(path, "/servers/") + sub := strings.Split(parts[1], "/security-groups/") + serverID, securityGroupID := sub[0], sub[1] + var err error + switch r.Method { + case http.MethodPut: + err = c.addSecurityGroupToServer(serverID, securityGroupID) + case http.MethodDelete: + err = c.removeSecurityGroupFromServer(serverID, securityGroupID) + default: + err = cloud.ErrInvalidInput + } + writeFakeStatus(rec, err) + return true + + case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers"): + tags := parseLabelSelector(r.URL.Query().Get("label_selector")) + items, err := c.listServerItems(tags) + if err != nil { writeFakeError(rec, err) } else { - rec.WriteHeader(http.StatusNoContent) + _ = json.NewEncoder(rec).Encode(map[string]any{"items": items}) } + return true + + case r.Method == http.MethodPost && strings.HasSuffix(path, "/security-groups"): + var body struct { + Name string `json:"name"` + Description string `json:"description"` + Labels map[string]string `json:"labels"` + } + if r.Body != nil { + _ = json.NewDecoder(r.Body).Decode(&body) + } + created, err := c.createSecurityGroup(body.Name, body.Description, body.Labels) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(created) + } + return true + + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): + tags := parseLabelSelector(r.URL.Query().Get("label_selector")) + items, err := c.listSecurityGroupItems(tags) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(map[string]any{"items": items}) + } + return true + + case strings.Contains(path, "/security-groups/") && strings.HasSuffix(path, "/rules") && r.Method == http.MethodGet: + sub := path[strings.Index(path, "/security-groups/")+len("/security-groups/"):] + sgID := strings.TrimSuffix(sub, "/rules") + items, err := c.listSecurityGroupRuleItems(sgID) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(map[string]any{"items": items}) + } + return true + + case strings.Contains(path, "/security-groups/") && strings.HasSuffix(path, "/rules") && r.Method == http.MethodPost: + c.handleCreateSecurityGroupRule(rec, r) + return true + + case strings.Contains(path, "/security-groups/") && strings.Contains(path, "/rules/") && r.Method == http.MethodDelete: + sub := path[strings.Index(path, "/security-groups/")+len("/security-groups/"):] + parts := strings.SplitN(sub, "/rules/", 2) + writeFakeStatus(rec, c.deleteSecurityGroupRule(parts[0], parts[1])) + return true + + case r.Method == http.MethodDelete && strings.Contains(path, "/security-groups/"): + id := path[strings.LastIndex(path, "/security-groups/")+len("/security-groups/"):] + writeFakeStatus(rec, c.deleteSecurityGroup(id)) + return true default: - rec.WriteHeader(http.StatusNotFound) - _ = json.NewEncoder(rec).Encode(map[string]any{"message": "not found"}) + return false } +} - return rec.Result(), nil +func (c *Client) handleCreateSecurityGroupRule(rec *httptest.ResponseRecorder, r *http.Request) { + path := r.URL.Path + sub := path[strings.Index(path, "/security-groups/")+len("/security-groups/"):] + sgID := strings.TrimSuffix(sub, "/rules") + var body struct { + Direction string `json:"direction"` + Protocol string `json:"protocol"` + PortRange *struct { + Min int64 `json:"min"` + Max int64 `json:"max"` + } `json:"portRange"` + IPRange string `json:"ipRange"` + RemoteSecurityGroupID string `json:"remoteSecurityGroupId"` + } + if r.Body != nil { + _ = json.NewDecoder(r.Body).Decode(&body) + } + var portMin, portMax int64 + if body.PortRange != nil { + portMin = body.PortRange.Min + portMax = body.PortRange.Max + } + created, err := c.createSecurityGroupRule(sgID, securityGroupRuleEntry{ + Rule: securitygroup.Rule{ + Direction: securitygroup.Direction(body.Direction), + Protocol: securitygroup.Protocol(body.Protocol), + PortMin: portMin, + PortMax: portMax, + IPRange: body.IPRange, + RemoteSecurityGroupID: body.RemoteSecurityGroupID, + }, + }) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(created) + } +} + +func writeFakeStatus(rec *httptest.ResponseRecorder, err error) { + if err != nil { + writeFakeError(rec, err) + } else { + rec.WriteHeader(http.StatusNoContent) + } } func (c *Client) listPublicIPItems(tags map[string]string) ([]map[string]any, error) { @@ -957,6 +1070,226 @@ func fakePublicIPMap(p *cloud.PublicIP, tags map[string]string) map[string]any { return m } +func (c *Client) addSecurityGroupToServer(serverID, securityGroupID string) error { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextAddSecurityGroup); err != nil { + return err + } + serverEntry, ok := c.servers[serverID] + if !ok { + return fmt.Errorf("server %q: %w", serverID, cloud.ErrNotFound) + } + if _, ok := c.securityGroups[securityGroupID]; !ok { + return fmt.Errorf("security group %q: %w", securityGroupID, cloud.ErrNotFound) + } + if serverEntry.securityGroups == nil { + serverEntry.securityGroups = map[string]struct{}{} + } + serverEntry.securityGroups[securityGroupID] = struct{}{} + c.AddSecurityGroupCalls++ + return nil +} + +func (c *Client) listServerItems(tags map[string]string) ([]map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + items := make([]map[string]any, 0, len(c.servers)) + for _, entry := range c.servers { + if len(tags) > 0 && !mapContains(entry.tags, tags) { + continue + } + m := map[string]any{ + "id": entry.server.ID, + "name": entry.server.Name, + "machineType": "c2i.1", + "status": entry.server.State, + } + if len(entry.tags) > 0 { + labels := make(map[string]any, len(entry.tags)) + for k, v := range entry.tags { + labels[k] = v + } + m["labels"] = labels + } + items = append(items, m) + } + return items, nil +} + +func (c *Client) removeSecurityGroupFromServer(serverID, securityGroupID string) error { + c.mu.Lock() + defer c.mu.Unlock() + + serverEntry, ok := c.servers[serverID] + if !ok { + return fmt.Errorf("server %q: %w", serverID, cloud.ErrNotFound) + } + delete(serverEntry.securityGroups, securityGroupID) + return nil +} + +func (c *Client) createSecurityGroup(name, description string, tags map[string]string) (map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + entry, err := c.createSecurityGroupEntry(name, description, tags) + if err != nil { + return nil, err + } + return fakeSecurityGroupMap(entry), nil +} + +func (c *Client) createSecurityGroupEntry(name, description string, tags map[string]string) (*securityGroupEntry, error) { + if name == "" { + return nil, fmt.Errorf("security group name is empty: %w", cloud.ErrInvalidInput) + } + id := c.genID() + sg := &cloud.SecurityGroup{ + ID: id, + Name: name, + } + entry := &securityGroupEntry{ + securityGroup: sg, + description: description, + tags: copyTags(tags), + rules: make(map[string]*securityGroupRuleEntry), + } + c.securityGroups[id] = entry + return entry, nil +} + +func (c *Client) listSecurityGroupItems(tags map[string]string) ([]map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + if len(tags) == 0 { + return nil, fmt.Errorf("empty tags: %w", cloud.ErrInvalidInput) + } + items := make([]map[string]any, 0, len(c.securityGroups)) + for _, entry := range c.securityGroups { + if mapContains(entry.tags, tags) { + items = append(items, fakeSecurityGroupMap(entry)) + } + } + return items, nil +} + +func (c *Client) listSecurityGroupRuleItems(sgID string) ([]map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + entry, ok := c.securityGroups[sgID] + if !ok { + return nil, fmt.Errorf("security group %q: %w", sgID, cloud.ErrNotFound) + } + items := make([]map[string]any, 0, len(entry.rules)) + for _, rule := range entry.rules { + items = append(items, fakeSecurityGroupRuleMap(rule)) + } + return items, nil +} + +func (c *Client) createSecurityGroupRule(sgID string, rule securityGroupRuleEntry) (map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + entry, ok := c.securityGroups[sgID] + if !ok { + return nil, fmt.Errorf("security group %q: %w", sgID, cloud.ErrNotFound) + } + if entry.rules == nil { + entry.rules = make(map[string]*securityGroupRuleEntry) + } + rule.id = c.genID() + stored := rule + entry.rules[rule.id] = &stored + if rule.IPRange != "" && !containsString(entry.allowedCIDRs, rule.IPRange) { + entry.allowedCIDRs = append(entry.allowedCIDRs, rule.IPRange) + } + if rule.RemoteSecurityGroupID != "" && !containsString(entry.remoteSecurityGroups, rule.RemoteSecurityGroupID) { + entry.remoteSecurityGroups = append(entry.remoteSecurityGroups, rule.RemoteSecurityGroupID) + } + return fakeSecurityGroupRuleMap(&stored), nil +} + +func (c *Client) deleteSecurityGroupRule(sgID, ruleID string) error { + c.mu.Lock() + defer c.mu.Unlock() + + entry, ok := c.securityGroups[sgID] + if !ok { + return fmt.Errorf("security group %q: %w", sgID, cloud.ErrNotFound) + } + if entry.rules == nil { + return fmt.Errorf("rule %q: %w", ruleID, cloud.ErrNotFound) + } + if _, ok := entry.rules[ruleID]; !ok { + return fmt.Errorf("rule %q: %w", ruleID, cloud.ErrNotFound) + } + delete(entry.rules, ruleID) + return nil +} + +func (c *Client) deleteSecurityGroup(id string) error { + c.mu.Lock() + defer c.mu.Unlock() + + if id == "" { + return fmt.Errorf("security group ID is empty: %w", cloud.ErrInvalidInput) + } + if _, ok := c.securityGroups[id]; !ok { + return fmt.Errorf("security group %q: %w", id, cloud.ErrNotFound) + } + for serverID, server := range c.servers { + if _, attached := server.securityGroups[id]; attached { + return fmt.Errorf("security group %q is still attached to server %q: %w", id, serverID, cloud.ErrConflict) + } + } + delete(c.securityGroups, id) + return nil +} + +func fakeSecurityGroupMap(entry *securityGroupEntry) map[string]any { + m := map[string]any{ + "id": entry.securityGroup.ID, + "name": entry.securityGroup.Name, + } + if entry.description != "" { + m["description"] = entry.description + } + if len(entry.tags) > 0 { + labels := make(map[string]any, len(entry.tags)) + for k, v := range entry.tags { + labels[k] = v + } + m["labels"] = labels + } + return m +} + +func fakeSecurityGroupRuleMap(rule *securityGroupRuleEntry) map[string]any { + m := map[string]any{ + "id": rule.id, + "direction": string(rule.Direction), + } + if rule.Protocol != "" { + m["protocol"] = map[string]any{"name": string(rule.Protocol)} + } + if rule.PortMin > 0 || rule.PortMax > 0 { + m["portRange"] = map[string]any{"min": rule.PortMin, "max": rule.PortMax} + } + if rule.IPRange != "" { + m["ipRange"] = rule.IPRange + } + if rule.RemoteSecurityGroupID != "" { + m["remoteSecurityGroupId"] = rule.RemoteSecurityGroupID + } + return m +} + func parseLabelSelector(selector string) map[string]string { if selector == "" { return nil diff --git a/cloud/sdk_client.go b/cloud/sdk_client.go index 531b7ec..4777106 100644 --- a/cloud/sdk_client.go +++ b/cloud/sdk_client.go @@ -19,6 +19,7 @@ import ( "slices" "strings" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/stackit-sdk-go/core/config" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" @@ -35,8 +36,6 @@ const ( apiserverListenerName = "apiserver" bootstrapTargetName = "capi-bootstrap-placeholder" bootstrapTargetIP = "10.0.0.1" - sshPort = 22 - tcpProtocol = "tcp" ) // SDKClient is the STACKIT SDK-backed implementation of Client. @@ -259,12 +258,14 @@ func (c *SDKClient) EnsureBastion(ctx context.Context, input BastionInput) (*Bas return nil, fmt.Errorf("%w: bastion tags are required", ErrInvalidInput) } - securityGroup, err := c.ensureBastionSecurityGroup(ctx, input) - if err != nil { - return nil, err - } - if err := c.ensureBastionSecurityGroupRules(ctx, securityGroup.ID, input.AllowedCIDRs); err != nil { - return nil, err + sgClient := securitygroup.NewClient(c) + securityGroupID := input.SecurityGroupID + if securityGroupID == "" { + securityGroup, err := sgClient.FindByTags(ctx, input.Tags) + if err != nil { + return nil, err + } + securityGroupID = securityGroup.ID } server, err := c.CreateServer(ctx, CreateServerInput{ @@ -276,7 +277,7 @@ func (c *SDKClient) EnsureBastion(ctx context.Context, input BastionInput) (*Bas AvailabilityZone: input.AvailabilityZone, SSHKeyName: input.SSHKeyName, NetworkID: input.NetworkID, - SecurityGroups: []string{securityGroup.ID}, + SecurityGroups: []string{securityGroupID}, UserData: input.CloudInit, Tags: input.Tags, RootVolume: input.RootVolume, @@ -290,7 +291,7 @@ func (c *SDKClient) EnsureBastion(ctx context.Context, input BastionInput) (*Bas // by an interrupted DeleteBastion). Kept deliberately, and idempotent: the // duplicate/no-port-yet responses are tolerated so they can no longer abort // the reconcile before the public IP below is assigned. - if err := c.addSecurityGroupToServer(ctx, server.ID, securityGroup.ID); err != nil { + if err := sgClient.AttachToServer(ctx, server.ID, securityGroupID); err != nil { return nil, err } @@ -298,7 +299,7 @@ func (c *SDKClient) EnsureBastion(ctx context.Context, input BastionInput) (*Bas ServerID: server.ID, ServerState: server.State, ServerPowerStatus: server.PowerStatus, - SecurityGroupID: securityGroup.ID, + SecurityGroupID: securityGroupID, }, nil } @@ -312,10 +313,17 @@ func (c *SDKClient) DeleteBastion(ctx context.Context, input BastionInput, statu return err } } + if serverID == "" { + return nil + } + sgClient := securitygroup.NewClient(c) securityGroupID := status.SecurityGroupID if securityGroupID == "" { - securityGroup, err := c.findSecurityGroupByTags(ctx, input.Tags) + securityGroupID = input.SecurityGroupID + } + if securityGroupID == "" && len(input.Tags) > 0 { + securityGroup, err := sgClient.FindByTags(ctx, input.Tags) if err == nil { securityGroupID = securityGroup.ID } else if !IsNotFound(err) { @@ -323,117 +331,17 @@ func (c *SDKClient) DeleteBastion(ctx context.Context, input BastionInput, statu } } - if serverID != "" && securityGroupID != "" { - if err := c.iaasClient.DefaultAPI. - RemoveSecurityGroupFromServer(ctx, c.projectID, c.region, serverID, securityGroupID). - Execute(); err != nil { - err := shared.ClassifySDKError("remove security group from server", err) - if !IsNotFound(err) && !IsConflict(err) { - return err - } - } - } - if serverID != "" { - if err := c.DeleteServer(ctx, serverID); err != nil && !IsNotFound(err) { - return err - } - } if securityGroupID != "" { - if err := c.deleteSecurityGroupRules(ctx, securityGroupID); err != nil { + if err := sgClient.DetachFromServer(ctx, serverID, securityGroupID); err != nil { return err } - if err := c.iaasClient.DefaultAPI. - DeleteSecurityGroup(ctx, c.projectID, c.region, securityGroupID). - Execute(); err != nil { - err := shared.ClassifySDKError("delete security group", err) - if !IsNotFound(err) { - return err - } - } - } - return nil -} - -func (c *SDKClient) EnsureNodeSSHAccess(ctx context.Context, input NodeSSHAccessInput) (*SecurityGroup, error) { - if input.Name == "" || input.ServerID == "" || input.BastionSecurityGroupID == "" { - return nil, fmt.Errorf( - "%w: node SSH access name, server ID, and bastion security group ID are required", - ErrInvalidInput, - ) - } - if len(input.Tags) == 0 { - return nil, fmt.Errorf("%w: node SSH access tags are required", ErrInvalidInput) - } - - securityGroup, err := c.ensureNodeSSHSecurityGroup(ctx, input) - if err != nil { - return nil, err - } - if err := c.ensureNodeSSHSecurityGroupRule(ctx, securityGroup.ID, input.BastionSecurityGroupID); err != nil { - return nil, err - } - if err := c.addSecurityGroupToServer(ctx, input.ServerID, securityGroup.ID); err != nil { - return nil, err - } - return securityGroup, nil -} - -func (c *SDKClient) DeleteNodeSSHAccess(ctx context.Context, tags map[string]string) error { - securityGroup, err := c.findSecurityGroupByTags(ctx, tags) - if IsNotFound(err) { - return nil - } - if err != nil { - return err - } - servers, err := c.ListServersByTags(ctx, clusterServerSelectorFromTags(tags)) - if err != nil { - return err - } - for _, server := range servers { - if err := c.iaasClient.DefaultAPI. - RemoveSecurityGroupFromServer(ctx, c.projectID, c.region, server.ID, securityGroup.ID). - Execute(); err != nil { - err := shared.ClassifySDKError("remove security group from server", err) - if !IsNotFound(err) && !IsConflict(err) { - return err - } - } } - if err := c.deleteSecurityGroupRules(ctx, securityGroup.ID); err != nil { + if err := c.DeleteServer(ctx, serverID); err != nil && !IsNotFound(err) { return err } - if err := c.iaasClient.DefaultAPI. - DeleteSecurityGroup(ctx, c.projectID, c.region, securityGroup.ID). - Execute(); err != nil { - err := shared.ClassifySDKError("delete security group", err) - if !IsNotFound(err) { - return err - } - } return nil } -func (c *SDKClient) ListSecurityGroupsByTags(ctx context.Context, tags map[string]string) ([]*SecurityGroup, error) { - if len(tags) == 0 { - return nil, fmt.Errorf("%w: empty tag selector", ErrInvalidInput) - } - resp, err := c.iaasClient.DefaultAPI.ListSecurityGroups(ctx, c.projectID, c.region). - LabelSelector(shared.LabelSelector(tags)). - Execute() - if err != nil { - return nil, shared.ClassifySDKError("list security groups", err) - } - matched := []*SecurityGroup{} - for _, securityGroup := range resp.GetItems() { - if shared.LabelsContainTags(securityGroup.GetLabels(), tags) { - securityGroup := securityGroup - matched = append(matched, securityGroupFromSDK(&securityGroup)) - } - } - return matched, nil -} - func (c *SDKClient) EnsureAPIServerLoadBalancer(ctx context.Context, input LoadBalancerInput) (*LoadBalancer, error) { if existing, err := c.findLoadBalancerByTags(ctx, input.Tags); err == nil { return existing, nil @@ -604,229 +512,6 @@ func (c *SDKClient) findLoadBalancerByTags(ctx context.Context, tags map[string] return matched[0], nil } -func (c *SDKClient) ensureBastionSecurityGroup(ctx context.Context, input BastionInput) (*SecurityGroup, error) { - if existing, err := c.findSecurityGroupByTags(ctx, input.Tags); err == nil { - return existing, nil - } else if !IsNotFound(err) { - return nil, err - } - payload := iaas.NewCreateSecurityGroupPayload(input.Name + "-ssh") - payload.SetDescription("Cluster API Provider STACKIT bastion SSH access") - payload.SetLabels(shared.TagsToSDKLabels(input.Tags)) - securityGroup, err := c.iaasClient.DefaultAPI.CreateSecurityGroup(ctx, c.projectID, c.region). - CreateSecurityGroupPayload(*payload). - Execute() - if err != nil { - return nil, shared.ClassifySDKError("create security group", err) - } - return securityGroupFromSDK(securityGroup), nil -} - -func (c *SDKClient) ensureNodeSSHSecurityGroup(ctx context.Context, input NodeSSHAccessInput) (*SecurityGroup, error) { - if existing, err := c.findSecurityGroupByTags(ctx, input.Tags); err == nil { - return existing, nil - } else if !IsNotFound(err) { - return nil, err - } - payload := iaas.NewCreateSecurityGroupPayload(input.Name) - payload.SetDescription("Cluster API Provider STACKIT node SSH access from bastion") - payload.SetLabels(shared.TagsToSDKLabels(input.Tags)) - securityGroup, err := c.iaasClient.DefaultAPI.CreateSecurityGroup(ctx, c.projectID, c.region). - CreateSecurityGroupPayload(*payload). - Execute() - if err != nil { - return nil, shared.ClassifySDKError("create security group", err) - } - return securityGroupFromSDK(securityGroup), nil -} - -func (c *SDKClient) ensureBastionSecurityGroupRules(ctx context.Context, securityGroupID string, cidrs []string) error { - if securityGroupID == "" { - return fmt.Errorf("%w: security group ID is required", ErrInvalidInput) - } - resp, err := c.iaasClient.DefaultAPI.ListSecurityGroupRules(ctx, c.projectID, c.region, securityGroupID).Execute() - if err != nil { - return shared.ClassifySDKError("list security group rules", err) - } - existingRules := resp.GetItems() - desired := make(map[string]struct{}, len(cidrs)) - for _, cidr := range cidrs { - if cidr == "" { - return fmt.Errorf("%w: empty bastion allowed CIDR", ErrInvalidInput) - } - if _, seen := desired[cidr]; seen { - // A CIDR listed twice would otherwise be created twice: - // existingRules is a snapshot from before this loop, so the second - // pass does not see the rule the first pass just created, and the - // duplicate create fails the whole bastion reconcile. - continue - } - desired[cidr] = struct{}{} - if hasSSHRule(existingRules, cidr) { - continue - } - protocol := iaas.StringAsCreateProtocol(new(tcpProtocol)) - rule := iaas.NewCreateSecurityGroupRulePayload("ingress") - rule.SetIpRange(cidr) - rule.SetPortRange(*iaas.NewPortRange(sshPort, sshPort)) - rule.SetProtocol(protocol) - if _, err := c.iaasClient.DefaultAPI.CreateSecurityGroupRule(ctx, c.projectID, c.region, securityGroupID). - CreateSecurityGroupRulePayload(*rule). - Execute(); err != nil { - return shared.ClassifySDKError("create security group rule", err) - } - } - - // Revoke SSH rules for CIDRs that are no longer desired. Without this the - // rule set only ever grows, so narrowing allowedCIDRs would not actually - // take access away from the previously allowed range. - for _, rule := range existingRules { - if !isSSHRule(rule) { - continue - } - if _, keep := desired[rule.GetIpRange()]; keep { - continue - } - ruleID := rule.GetId() - if ruleID == "" { - continue - } - if err := c.iaasClient.DefaultAPI. - DeleteSecurityGroupRule(ctx, c.projectID, c.region, securityGroupID, ruleID). - Execute(); err != nil { - err := shared.ClassifySDKError("delete security group rule", err) - if !IsNotFound(err) { - return err - } - } - } - return nil -} - -func (c *SDKClient) ensureNodeSSHSecurityGroupRule( - ctx context.Context, - nodeSecurityGroupID, bastionSecurityGroupID string, -) error { - if nodeSecurityGroupID == "" || bastionSecurityGroupID == "" { - return fmt.Errorf("%w: node and bastion security group IDs are required", ErrInvalidInput) - } - resp, err := c.iaasClient.DefaultAPI.ListSecurityGroupRules(ctx, c.projectID, c.region, nodeSecurityGroupID).Execute() - if err != nil { - return shared.ClassifySDKError("list security group rules", err) - } - if hasRemoteSecurityGroupSSHRule(resp.GetItems(), bastionSecurityGroupID) { - return nil - } - protocol := iaas.StringAsCreateProtocol(new(tcpProtocol)) - rule := iaas.NewCreateSecurityGroupRulePayload("ingress") - rule.SetRemoteSecurityGroupId(bastionSecurityGroupID) - rule.SetPortRange(*iaas.NewPortRange(sshPort, sshPort)) - rule.SetProtocol(protocol) - if _, err := c.iaasClient.DefaultAPI.CreateSecurityGroupRule(ctx, c.projectID, c.region, nodeSecurityGroupID). - CreateSecurityGroupRulePayload(*rule). - Execute(); err != nil { - return shared.ClassifySDKError("create security group rule", err) - } - return nil -} - -func (c *SDKClient) findSecurityGroupByTags(ctx context.Context, tags map[string]string) (*SecurityGroup, error) { - matched, err := c.ListSecurityGroupsByTags(ctx, tags) - if err != nil { - return nil, err - } - if len(matched) == 0 { - return nil, fmt.Errorf("%w: no security group matching tags", ErrNotFound) - } - if len(matched) > 1 { - return nil, fmt.Errorf("%w: multiple security groups matching tags", ErrConflict) - } - return matched[0], nil -} - -// addSecurityGroupToServer attaches the group idempotently. Three outcomes are -// expected and must not fail the caller: -// - conflict: already attached -// - invalid input: the API rejects the duplicate ("Duplicate items in the list") -// - not found: the server has no network port yet, so there is nothing to -// attach to — the next reconcile retries once it does -func (c *SDKClient) addSecurityGroupToServer(ctx context.Context, serverID, securityGroupID string) error { - if err := c.iaasClient.DefaultAPI. - AddSecurityGroupToServer(ctx, c.projectID, c.region, serverID, securityGroupID). - Execute(); err != nil { - err := shared.ClassifySDKError("add security group to server", err) - if !IsConflict(err) && !IsInvalidInput(err) && !IsNotFound(err) { - return err - } - } - return nil -} - -func (c *SDKClient) deleteSecurityGroupRules(ctx context.Context, securityGroupID string) error { - resp, err := c.iaasClient.DefaultAPI.ListSecurityGroupRules(ctx, c.projectID, c.region, securityGroupID).Execute() - if err != nil { - err := shared.ClassifySDKError("list security group rules", err) - if IsNotFound(err) { - return nil - } - return err - } - for _, rule := range resp.GetItems() { - ruleID := rule.GetId() - if ruleID == "" { - continue - } - if err := c.iaasClient.DefaultAPI. - DeleteSecurityGroupRule(ctx, c.projectID, c.region, securityGroupID, ruleID). - Execute(); err != nil { - err := shared.ClassifySDKError("delete security group rule", err) - if !IsNotFound(err) { - return err - } - } - } - return nil -} - -func hasSSHRule(rules []iaas.SecurityGroupRule, cidr string) bool { - for _, rule := range rules { - if rule.GetIpRange() == cidr && isSSHRule(rule) { - return true - } - } - return false -} - -// isSSHRule reports whether the rule is an ingress TCP/22 rule, regardless of -// which CIDR it allows. -func isSSHRule(rule iaas.SecurityGroupRule) bool { - if rule.GetDirection() != "ingress" { - return false - } - portRange := rule.GetPortRange() - if portRange.GetMin() != sshPort || portRange.GetMax() != sshPort { - return false - } - protocol, ok := rule.GetProtocolOk() - return ok && protocol.GetName() == tcpProtocol -} - -func hasRemoteSecurityGroupSSHRule(rules []iaas.SecurityGroupRule, remoteSecurityGroupID string) bool { - for _, rule := range rules { - if rule.GetDirection() != "ingress" || rule.GetRemoteSecurityGroupId() != remoteSecurityGroupID { - continue - } - portRange := rule.GetPortRange() - if portRange.GetMin() != sshPort || portRange.GetMax() != sshPort { - continue - } - if protocol, ok := rule.GetProtocolOk(); ok && protocol.GetName() == tcpProtocol { - return true - } - } - return false -} - func (c *SDKClient) updateAPIServerTargetPool( ctx context.Context, loadBalancerID string, @@ -900,16 +585,6 @@ func loadBalancerFromSDK(in *lb.LoadBalancer) *LoadBalancer { } } -func securityGroupFromSDK(in *iaas.SecurityGroup) *SecurityGroup { - if in == nil { - return nil - } - return &SecurityGroup{ - ID: in.GetId(), - Name: in.GetName(), - } -} - func listenerPort(listeners []lb.Listener) int32 { for _, listener := range listeners { if port := listener.GetPort(); port > 0 { @@ -919,20 +594,6 @@ func listenerPort(listeners []lb.Listener) int32 { return 0 } -func clusterServerSelectorFromTags(tags map[string]string) map[string]string { - selector := map[string]string{} - for _, key := range []string{ - "cluster.x-k8s.io/cluster-name", - "cluster.x-k8s.io/cluster-namespace", - "cluster.x-k8s.io/managed-by", - } { - if value := tags[key]; value != "" { - selector[key] = value - } - } - return selector -} - func isLoadBalancerServiceNotEnabled(err error) bool { return IsUnauthorized(err) && strings.Contains(err.Error(), "Service not enabled") } diff --git a/cloud/sdk_client_test.go b/cloud/sdk_client_test.go index 3e235b7..16eef07 100644 --- a/cloud/sdk_client_test.go +++ b/cloud/sdk_client_test.go @@ -482,10 +482,6 @@ func TestSDKClientEnsureBastionToleratesDuplicateSecurityGroupAttach(t *testing. "labels": map[string]any{"cluster": "test"}, }, }}) - case r.Method == http.MethodGet && strings.HasSuffix(path, "/rules"): - writeJSON(t, w, map[string]any{"items": []any{}}) - case r.Method == http.MethodPost && strings.HasSuffix(path, "/rules"): - writeJSON(t, w, map[string]any{"id": "77777777-7777-4777-8777-777777777777", "direction": "ingress"}) case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers"): writeJSON(t, w, map[string]any{"items": []any{}}) case r.Method == http.MethodPost && strings.HasSuffix(path, "/servers"): @@ -540,162 +536,42 @@ func TestSDKClientEnsureBastionToleratesDuplicateSecurityGroupAttach(t *testing. } } -// TestSDKClientEnsureBastionRevokesRemovedCIDR guards against security-group -// rules only ever being added. Narrowing allowedCIDRs must actually take -// access away from the previously allowed range. -func TestSDKClientEnsureBastionRevokesRemovedCIDR(t *testing.T) { - const staleRuleID = "55555555-5555-4555-8555-555555555555" +func TestSDKClientDeleteBastionDetachesSecurityGroupWithoutDeletingIt(t *testing.T) { var ( - createdRuleCIDRs []string - deletedRuleIDs []string + detachedServerID string + detachedSGID string + deletedServerID string ) server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path switch { - case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): - writeJSON(t, w, map[string]any{"items": []any{ - map[string]any{ - "id": testSDKSecurityGroup, "name": "bastion-ssh", - "labels": map[string]any{"cluster": "test"}, - }, - }}) - case r.Method == http.MethodGet && strings.HasSuffix(path, "/rules"): - // One pre-existing SSH rule for the CIDR that is being revoked. - writeJSON(t, w, map[string]any{"items": []any{ - map[string]any{ - "id": staleRuleID, - "direction": "ingress", - "ipRange": "198.51.100.0/24", - "portRange": map[string]any{"min": 22, "max": 22}, - "protocol": map[string]any{"name": "tcp"}, - }, - }}) - case r.Method == http.MethodPost && strings.HasSuffix(path, "/rules"): - payload := readJSON(t, r) - if cidr, ok := payload["ipRange"].(string); ok { - createdRuleCIDRs = append(createdRuleCIDRs, cidr) - } - writeJSON(t, w, map[string]any{"id": "77777777-7777-4777-8777-777777777777", "direction": "ingress"}) - case strings.Contains(path, "/security-groups/") && strings.Contains(path, "/servers/"): + case r.Method == http.MethodDelete && strings.Contains(path, "/servers/") && strings.Contains(path, "/security-groups/"): + parts := strings.Split(path, "/servers/") + sub := strings.Split(parts[1], "/security-groups/") + detachedServerID, detachedSGID = sub[0], sub[1] w.WriteHeader(http.StatusNoContent) - case r.Method == http.MethodDelete && strings.Contains(path, "/rules/"): - parts := strings.Split(strings.TrimSuffix(path, "/"), "/") - deletedRuleIDs = append(deletedRuleIDs, parts[len(parts)-1]) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/servers/"+testSDKServerID): + deletedServerID = testSDKServerID w.WriteHeader(http.StatusNoContent) - case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers"): - writeJSON(t, w, map[string]any{"items": []any{ - map[string]any{ - "id": testSDKServerID, "name": "bastion", "status": "ACTIVE", "machineType": "c2i.1", - "labels": map[string]any{"cluster": "test"}, - }, - }}) - case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips"): - writeJSON(t, w, map[string]any{"items": []any{ - map[string]any{ - "id": "66666666-6666-4666-8666-666666666666", "ip": "203.0.113.10", "networkInterface": "nic-1", - "labels": map[string]any{"cluster": "test"}, - }, - }}) - case r.Method == http.MethodGet && strings.Contains(path, "/servers/"): - writeJSON(t, w, map[string]any{ - "id": testSDKServerID, "name": "bastion", "status": "ACTIVE", "machineType": "c2i.1", - }) default: t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) } })) client := newTestSDKClient(t, server.URL) - if _, err := client.EnsureBastion(context.Background(), BastionInput{ - Name: "bastion", - ProjectID: testSDKProjectID, - Region: testSDKRegion, - NetworkID: testSDKNetworkID, - ImageID: testSDKImageID, - MachineType: "c2i.1", - SSHKeyName: "default", - AllowedCIDRs: []string{"203.0.113.0/24"}, - Tags: map[string]string{"cluster": "test"}, - }); err != nil { - t.Fatalf("EnsureBastion() error = %v", err) - } - - if len(createdRuleCIDRs) != 1 || createdRuleCIDRs[0] != "203.0.113.0/24" { - t.Fatalf("created rules for %v, want [203.0.113.0/24]", createdRuleCIDRs) + err := client.DeleteBastion( + context.Background(), + BastionInput{Tags: map[string]string{"cluster": "test"}}, + Bastion{ServerID: testSDKServerID, SecurityGroupID: testSDKSecurityGroup}, + ) + if err != nil { + t.Fatalf("DeleteBastion() error = %v", err) } - if len(deletedRuleIDs) != 1 || deletedRuleIDs[0] != staleRuleID { - t.Fatalf("deleted rules %v, want [%s] — the revoked CIDR keeps its SSH access", deletedRuleIDs, staleRuleID) + if detachedServerID != testSDKServerID || detachedSGID != testSDKSecurityGroup { + t.Fatalf("detached (%q, %q), want (%q, %q)", detachedServerID, detachedSGID, testSDKServerID, testSDKSecurityGroup) } -} - -// TestSDKClientEnsureBastionDeduplicatesRepeatedCIDRs guards against a CIDR -// listed twice producing two identical rules: existingRules is a snapshot taken -// before the loop, so the second pass would not see the rule the first pass -// created and the duplicate create fails the whole reconcile. -func TestSDKClientEnsureBastionDeduplicatesRepeatedCIDRs(t *testing.T) { - var createdRuleCIDRs []string - server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - path := r.URL.Path - switch { - case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): - writeJSON(t, w, map[string]any{"items": []any{ - map[string]any{ - "id": testSDKSecurityGroup, "name": "bastion-ssh", - "labels": map[string]any{"cluster": "test"}, - }, - }}) - case r.Method == http.MethodGet && strings.HasSuffix(path, "/rules"): - writeJSON(t, w, map[string]any{"items": []any{}}) - case r.Method == http.MethodPost && strings.HasSuffix(path, "/rules"): - payload := readJSON(t, r) - if cidr, ok := payload["ipRange"].(string); ok { - createdRuleCIDRs = append(createdRuleCIDRs, cidr) - } - writeJSON(t, w, map[string]any{"id": "77777777-7777-4777-8777-777777777777", "direction": "ingress"}) - case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers"): - writeJSON(t, w, map[string]any{"items": []any{ - map[string]any{ - "id": testSDKServerID, "name": "bastion", "status": "ACTIVE", "machineType": "c2i.1", - "labels": map[string]any{"cluster": "test"}, - }, - }}) - case strings.Contains(path, "/security-groups/") && strings.Contains(path, "/servers/"): - w.WriteHeader(http.StatusNoContent) - case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips"): - writeJSON(t, w, map[string]any{"items": []any{ - map[string]any{ - "id": "66666666-6666-4666-8666-666666666666", "ip": "203.0.113.10", "networkInterface": "nic-1", - "labels": map[string]any{"cluster": "test"}, - }, - }}) - case r.Method == http.MethodGet && strings.Contains(path, "/servers/"): - writeJSON(t, w, map[string]any{ - "id": testSDKServerID, "name": "bastion", "status": "ACTIVE", "machineType": "c2i.1", - }) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - } - })) - - client := newTestSDKClient(t, server.URL) - if _, err := client.EnsureBastion(context.Background(), BastionInput{ - Name: "bastion", - ProjectID: testSDKProjectID, - Region: testSDKRegion, - NetworkID: testSDKNetworkID, - ImageID: testSDKImageID, - MachineType: "c2i.1", - SSHKeyName: "default", - // The same CIDR twice, plus a distinct one. - AllowedCIDRs: []string{"203.0.113.0/24", "203.0.113.0/24", "198.51.100.0/24"}, - Tags: map[string]string{"cluster": "test"}, - }); err != nil { - t.Fatalf("EnsureBastion() error = %v", err) - } - - if len(createdRuleCIDRs) != 2 { - t.Fatalf("created %d rules (%v), want 2 — the repeated CIDR was created twice", - len(createdRuleCIDRs), createdRuleCIDRs) + if deletedServerID != testSDKServerID { + t.Fatalf("deletedServerID = %q, want %q", deletedServerID, testSDKServerID) } } diff --git a/cloud/services/bastion/bastion.go b/cloud/services/bastion/bastion.go index 80eab63..d89ae2c 100644 --- a/cloud/services/bastion/bastion.go +++ b/cloud/services/bastion/bastion.go @@ -36,6 +36,7 @@ func Input(stackitCluster *infrav1.StackitCluster, cloudInit []byte) cloud.Basti AvailabilityZone: scope.DefaultAvailabilityZone(stackitCluster), SSHKeyName: stackitCluster.Spec.Bastion.SSHKeyName, AllowedCIDRs: stackitCluster.Spec.Bastion.AllowedCIDRs, + SecurityGroupID: stackitCluster.Status.Bastion.SecurityGroupID, Tags: tags, RootVolume: cloud.RootVolumeInput{ SizeGiB: stackitCluster.Spec.Bastion.RootVolume.SizeGiB, @@ -45,9 +46,3 @@ func Input(stackitCluster *infrav1.StackitCluster, cloudInit []byte) cloud.Basti CloudInit: cloudInit, } } - -func NodeSSHAccessTags(stackitCluster *infrav1.StackitCluster) map[string]string { - tags := util.ClusterTags(stackitCluster.Name, stackitCluster.Namespace, stackitCluster.Spec.AdditionalLabels) - tags[util.LabelResourceRole] = util.ResourceRoleNodeSSH - return tags -} diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go new file mode 100644 index 0000000..75dbdc5 --- /dev/null +++ b/cloud/services/securitygroup/reconcile.go @@ -0,0 +1,323 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package securitygroup + +import ( + "context" + "fmt" + "slices" + + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + logf "sigs.k8s.io/controller-runtime/pkg/log" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +func (s *Service) Reconcile(ctx context.Context) error { + log := logf.FromContext(ctx) + for _, spec := range s.scope.SecurityGroupSpecs() { + if err := s.reconcileSecurityGroup(ctx, spec); err != nil { + return err + } + } + log.V(1).Info("Reconciled security group resources") + return nil +} + +func (s *Service) Delete(ctx context.Context) error { + log := logf.FromContext(ctx) + for _, spec := range slices.Backward(s.scope.SecurityGroupSpecs()) { + if err := s.deleteSecurityGroup(ctx, spec); err != nil { + return err + } + } + log.V(1).Info("Deleted security group resources") + return nil +} + +// FindByTags finds a single STACKIT security group matching the given tag selector. +func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*SecurityGroup, error) { + matched, err := c.ListByTags(ctx, tags) + if err != nil { + return nil, err + } + if len(matched) == 0 { + return nil, fmt.Errorf("%w: no security group matching tags", shared.ErrNotFound) + } + if len(matched) > 1 { + return nil, fmt.Errorf("%w: multiple security groups matching tags", shared.ErrConflict) + } + return matched[0], nil +} + +// ListByTags lists STACKIT security groups matching the given tag selector. +func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*SecurityGroup, error) { + if len(tags) == 0 { + return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) + } + resp, err := c.iaasClient.DefaultAPI.ListSecurityGroups(ctx, c.projectID, c.region). + LabelSelector(shared.LabelSelector(tags)). + Execute() + if err != nil { + return nil, shared.ClassifySDKError("list security groups", err) + } + matched := []*SecurityGroup{} + for _, securityGroup := range resp.GetItems() { + if shared.LabelsContainTags(securityGroup.GetLabels(), tags) { + securityGroup := securityGroup + matched = append(matched, securityGroupFromSDK(&securityGroup)) + } + } + return matched, nil +} + +// DeleteByID deletes a security group and its rules by ID, ignoring NotFound errors. +func (c *Client) DeleteByID(ctx context.Context, id string) error { + if id == "" { + return fmt.Errorf("%w: security group ID is empty", shared.ErrInvalidInput) + } + rules, ok, err := c.listRulesIfGroupExists(ctx, id) + if err != nil || !ok { + return err + } + return c.deleteSecurityGroupWithRules(ctx, id, rules) +} + +// AttachToServer attaches a security group to a server idempotently. +// Three error responses are tolerated: +// - conflict: already attached +// - invalid input: duplicate item in security_groups list +// - not found: server has no network port yet +func (c *Client) AttachToServer(ctx context.Context, serverID, securityGroupID string) error { + if err := c.iaasClient.DefaultAPI. + AddSecurityGroupToServer(ctx, c.projectID, c.region, serverID, securityGroupID). + Execute(); err != nil { + err := shared.ClassifySDKError("add security group to server", err) + if !shared.IsConflict(err) && !shared.IsInvalidInput(err) && !shared.IsNotFound(err) { + return err + } + } + logf.FromContext(ctx).V(1).Info("Attached security group to server", "securityGroupID", securityGroupID, "serverID", serverID) + return nil +} + +// DetachFromServer detaches a security group from a server, ignoring NotFound and Conflict errors. +func (c *Client) DetachFromServer(ctx context.Context, serverID, securityGroupID string) error { + if err := c.iaasClient.DefaultAPI. + RemoveSecurityGroupFromServer(ctx, c.projectID, c.region, serverID, securityGroupID). + Execute(); err != nil { + err := shared.ClassifySDKError("remove security group from server", err) + if !shared.IsNotFound(err) && !shared.IsConflict(err) { + return err + } + } + logf.FromContext(ctx).V(1).Info("Detached security group from server", "securityGroupID", securityGroupID, "serverID", serverID) + return nil +} + +func (c *Client) reconcileSecurityGroup(ctx context.Context, spec SecurityGroupSpec) error { + if err := spec.validate(); err != nil { + return err + } + securityGroup, err := c.ensureSecurityGroup(ctx, spec) + if err != nil { + return err + } + if spec.SetID != nil { + spec.SetID(securityGroup.ID) + } + if err := c.ensureSecurityGroupRules(ctx, securityGroup.ID, spec.Rules); err != nil { + return err + } + for _, serverID := range spec.ServerIDs { + if serverID == "" { + continue + } + if err := c.AttachToServer(ctx, serverID, securityGroup.ID); err != nil { + return err + } + } + return nil +} + +func (c *Client) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpec) error { + if spec.ID == "" && len(spec.Tags) == 0 { + return fmt.Errorf("%w: security group ID or tags are required", shared.ErrInvalidInput) + } + + securityGroupID, rules, err := c.resolveSecurityGroupForDelete(ctx, spec) + if err != nil { + return err + } + if securityGroupID != "" { + if err := c.detachSecurityGroupFromServers(ctx, securityGroupID, spec); err != nil { + return err + } + if err := c.deleteSecurityGroupWithRules(ctx, securityGroupID, rules); err != nil { + return err + } + logf.FromContext(ctx).V(1).Info("Deleted security group", "securityGroupID", securityGroupID) + } + if spec.SetID != nil { + spec.SetID("") + } + return nil +} + +func (c *Client) resolveSecurityGroupForDelete( + ctx context.Context, + spec SecurityGroupSpec, +) (string, []iaas.SecurityGroupRule, error) { + if spec.ID != "" { + rules, ok, err := c.listRulesIfGroupExists(ctx, spec.ID) + if err != nil || ok { + return spec.ID, rules, err + } + } + if len(spec.Tags) == 0 { + return "", nil, nil + } + securityGroup, err := c.FindByTags(ctx, spec.Tags) + if shared.IsNotFound(err) { + return "", nil, nil + } + if err != nil { + return "", nil, err + } + rules, _, err := c.listRulesIfGroupExists(ctx, securityGroup.ID) + return securityGroup.ID, rules, err +} + +func (c *Client) listRulesIfGroupExists(ctx context.Context, id string) ([]iaas.SecurityGroupRule, bool, error) { + rules, err := c.listSecurityGroupRules(ctx, id) + if shared.IsNotFound(err) { + return nil, false, nil + } + if err != nil { + return nil, false, err + } + return rules, true, nil +} + +func (c *Client) ensureSecurityGroup(ctx context.Context, spec SecurityGroupSpec) (*SecurityGroup, error) { + if existing, err := c.FindByTags(ctx, spec.Tags); err == nil { + return existing, nil + } else if !shared.IsNotFound(err) { + return nil, err + } + securityGroup, err := c.iaasClient.DefaultAPI.CreateSecurityGroup(ctx, c.projectID, c.region). + CreateSecurityGroupPayload(spec.toCreatePayload()). + Execute() + if err != nil { + return nil, shared.ClassifySDKError("create security group", err) + } + out := securityGroupFromSDK(securityGroup) + log := logf.FromContext(ctx) + log.V(1).Info("Created security group", "name", spec.Name, "securityGroupID", out.ID) + return out, nil +} + +func (c *Client) deleteSecurityGroupWithRules( + ctx context.Context, + id string, + rules []iaas.SecurityGroupRule, +) error { + for _, rule := range rules { + if err := c.deleteSecurityGroupRule(ctx, id, rule.GetId()); err != nil { + return err + } + } + if err := c.iaasClient.DefaultAPI. + DeleteSecurityGroup(ctx, c.projectID, c.region, id). + Execute(); err != nil { + err := shared.ClassifySDKError("delete security group", err) + if !shared.IsNotFound(err) { + return err + } + } + return nil +} + +func (c *Client) detachSecurityGroupFromServers(ctx context.Context, securityGroupID string, spec SecurityGroupSpec) error { + if err := c.detachServers(ctx, securityGroupID, spec.ServerIDs); err != nil { + return err + } + if len(spec.ServerSelector) == 0 { + return nil + } + serverIDs, err := c.listServerIDsByTags(ctx, spec.ServerSelector) + if err != nil { + return err + } + return c.detachServers(ctx, securityGroupID, serverIDs) +} + +func (c *Client) detachServers(ctx context.Context, securityGroupID string, serverIDs []string) error { + for _, serverID := range serverIDs { + if serverID == "" { + continue + } + if err := c.DetachFromServer(ctx, serverID, securityGroupID); err != nil { + return err + } + } + return nil +} + +func (c *Client) listServerIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { + resp, err := c.iaasClient.DefaultAPI.ListServers(ctx, c.projectID, c.region). + LabelSelector(shared.LabelSelector(tags)). + Execute() + if err != nil { + return nil, shared.ClassifySDKError("list servers", err) + } + var serverIDs []string + for _, server := range resp.GetItems() { + if shared.LabelsContainTags(server.GetLabels(), tags) && server.GetId() != "" { + serverIDs = append(serverIDs, server.GetId()) + } + } + return serverIDs, nil +} + +func (s SecurityGroupSpec) validate() error { + if s.Name == "" { + return fmt.Errorf("%w: security group name is required", shared.ErrInvalidInput) + } + if len(s.Tags) == 0 { + return fmt.Errorf("%w: security group tags are required", shared.ErrInvalidInput) + } + for _, rule := range s.Rules { + if err := rule.validate(); err != nil { + return err + } + } + return nil +} + +func (s SecurityGroupSpec) toCreatePayload() iaas.CreateSecurityGroupPayload { + payload := iaas.NewCreateSecurityGroupPayload(s.Name) + if s.Description != "" { + payload.SetDescription(s.Description) + } + payload.SetLabels(shared.TagsToSDKLabels(s.Tags)) + return *payload +} + +func securityGroupFromSDK(in *iaas.SecurityGroup) *SecurityGroup { + if in == nil { + return nil + } + return &SecurityGroup{ + ID: in.GetId(), + Name: in.GetName(), + } +} diff --git a/cloud/services/securitygroup/reconcile_test.go b/cloud/services/securitygroup/reconcile_test.go new file mode 100644 index 0000000..3c44ad0 --- /dev/null +++ b/cloud/services/securitygroup/reconcile_test.go @@ -0,0 +1,743 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package securitygroup_test + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "slices" + "strings" + "testing" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +const ( + testProjectID = "00000000-0000-0000-0000-000000000000" + testRegion = "eu01" + testSecurityGroupID = "11111111-1111-4111-8111-111111111111" + testRemoteSGID = "22222222-2222-4222-8222-222222222222" + testServerID = "33333333-3333-4333-8333-333333333333" + testRuleID1 = "66666666-6666-4666-8666-666666666666" + testRuleID2 = "77777777-7777-4777-8777-777777777777" +) + +type testScope struct { + projectID string + region string + iaasClient *iaas.APIClient + specs []securitygroup.SecurityGroupSpec +} + +func (s *testScope) ProjectID() string { + return s.projectID +} + +func (s *testScope) Region() string { + return s.region +} + +func (s *testScope) IaaSClient() *iaas.APIClient { + return s.iaasClient +} + +func (s *testScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { + return s.specs +} + +func newTestIaaSClient(t *testing.T, handler http.Handler) *iaas.APIClient { + t.Helper() + + server := httptest.NewServer(handler) + t.Cleanup(server.Close) + + iaasClient, err := iaas.NewAPIClient( + config.WithoutAuthentication(), + config.WithEndpoint(server.URL), + ) + if err != nil { + t.Fatalf("iaas.NewAPIClient() error = %v", err) + } + return iaasClient +} + +func newTestService(t *testing.T, specs []securitygroup.SecurityGroupSpec, handler http.Handler) *securitygroup.Service { + t.Helper() + + return securitygroup.New(&testScope{ + projectID: testProjectID, + region: testRegion, + iaasClient: newTestIaaSClient(t, handler), + specs: specs, + }) +} + +func writeJSON(t *testing.T, w http.ResponseWriter, value any) { + t.Helper() + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(value); err != nil { + t.Fatalf("encode response body: %v", err) + } +} + +func readJSON(t *testing.T, r *http.Request) map[string]any { + t.Helper() + var out map[string]any + if err := json.NewDecoder(r.Body).Decode(&out); err != nil { + t.Fatalf("decode request body: %v", err) + } + return out +} + +func TestServiceReconcile(t *testing.T) { + t.Run("creates security group and generic rules when not found", func(t *testing.T) { + var ( + createdSGPayload map[string]any + createdRulePayloads []map[string]any + ) + specs := []securitygroup.SecurityGroupSpec{{ + Name: "cluster-sg", + Description: "Generic security group", + Tags: map[string]string{"cluster": "test"}, + Rules: []securitygroup.Rule{ + { + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 6443, + PortMax: 6443, + IPRange: "203.0.113.0/24", + }, + { + Direction: securitygroup.DirectionEgress, + Protocol: securitygroup.ProtocolUDP, + PortMin: 8472, + PortMax: 8472, + RemoteSecurityGroupID: testRemoteSGID, + }, + }, + }} + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodPost && strings.HasSuffix(path, "/security-groups"): + createdSGPayload = readJSON(t, r) + writeJSON(t, w, map[string]any{ + "id": testSecurityGroupID, + "name": "cluster-sg", + }) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodPost && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + createdRulePayloads = append(createdRulePayloads, readJSON(t, r)) + writeJSON(t, w, map[string]any{"id": testRuleID1, "direction": "ingress"}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + + if createdSGPayload["name"] != "cluster-sg" { + t.Fatalf("created SG name = %v, want cluster-sg", createdSGPayload["name"]) + } + if createdSGPayload["description"] != "Generic security group" { + t.Fatalf("created SG description = %v, want Generic security group", createdSGPayload["description"]) + } + labels, _ := createdSGPayload["labels"].(map[string]any) + if labels["cluster"] != "test" { + t.Fatalf("created SG labels = %v, want cluster=test", labels) + } + if len(createdRulePayloads) != 2 { + t.Fatalf("created %d rules, want 2", len(createdRulePayloads)) + } + if createdRulePayloads[0]["direction"] != "ingress" || createdRulePayloads[0]["ipRange"] != "203.0.113.0/24" { + t.Fatalf("first rule payload = %#v", createdRulePayloads[0]) + } + if createdRulePayloads[1]["direction"] != "egress" || createdRulePayloads[1]["remoteSecurityGroupId"] != testRemoteSGID { + t.Fatalf("second rule payload = %#v", createdRulePayloads[1]) + } + }) + + t.Run("reuses existing security group found by tags without creating duplicate", func(t *testing.T) { + specs := []securitygroup.SecurityGroupSpec{{ + Name: "cluster-sg", + Tags: map[string]string{"cluster": "test"}, + }} + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{ + "id": testSecurityGroupID, + "name": "cluster-sg", + "labels": map[string]any{"cluster": "test"}, + }, + }}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + writeJSON(t, w, map[string]any{"items": []any{}}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + }) + + t.Run("returns ErrConflict when multiple security groups match tags", func(t *testing.T) { + specs := []securitygroup.SecurityGroupSpec{{ + Name: "cluster-sg", + Tags: map[string]string{"cluster": "test"}, + }} + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testSecurityGroupID, "name": "sg-1", "labels": map[string]any{"cluster": "test"}}, + map[string]any{"id": testRemoteSGID, "name": "sg-2", "labels": map[string]any{"cluster": "test"}}, + }}) + })) + + err := svc.Reconcile(context.Background()) + if !errors.Is(err, shared.ErrConflict) { + t.Fatalf("Reconcile() error = %v, want ErrConflict", err) + } + }) +} + +func TestServiceReconcileBastionAndNodeSSH(t *testing.T) { + var ( + bastionSGID string + bastionCreated bool + nodeRulesCreated []map[string]any + attachedServerIDs []string + ) + specs := []securitygroup.SecurityGroupSpec{ + { + Name: "cluster-bastion-ssh", + Tags: map[string]string{"role": "bastion"}, + SetID: func(id string) { + bastionSGID = id + }, + Rules: []securitygroup.Rule{ + { + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 22, + PortMax: 22, + IPRange: "203.0.113.0/24", + }, + { + Direction: securitygroup.DirectionEgress, + }, + }, + }, + { + Name: "cluster-node-ssh", + Tags: map[string]string{"role": "node-ssh"}, + ServerIDs: []string{testServerID}, + Rules: []securitygroup.Rule{ + { + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 22, + PortMax: 22, + RemoteSecurityGroupTags: map[string]string{"role": "bastion"}, + }, + { + Direction: securitygroup.DirectionEgress, + }, + }, + }, + } + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): + selector := r.URL.Query().Get("label_selector") + if bastionCreated && strings.Contains(selector, "role=bastion") { + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{ + "id": testRemoteSGID, + "name": "cluster-bastion-ssh", + "labels": map[string]any{"role": "bastion"}, + }, + }}) + return + } + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodPost && strings.HasSuffix(path, "/security-groups"): + payload := readJSON(t, r) + if payload["name"] == "cluster-bastion-ssh" { + bastionCreated = true + writeJSON(t, w, map[string]any{"id": testRemoteSGID, "name": "cluster-bastion-ssh"}) + return + } + writeJSON(t, w, map[string]any{"id": testSecurityGroupID, "name": "cluster-node-ssh"}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testRemoteSGID+"/rules"): + // Simulate STACKIT default egress rule already present. + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testRuleID1, "direction": "egress", "ethertype": "IPv4"}, + }}) + case r.Method == http.MethodPost && strings.HasSuffix(path, "/security-groups/"+testRemoteSGID+"/rules"): + writeJSON(t, w, map[string]any{"id": testRuleID2, "direction": "ingress"}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodPost && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + nodeRulesCreated = append(nodeRulesCreated, readJSON(t, r)) + writeJSON(t, w, map[string]any{"id": testRuleID1, "direction": "ingress"}) + case r.Method == http.MethodPut && strings.HasSuffix(path, "/servers/"+testServerID+"/security-groups/"+testSecurityGroupID): + attachedServerIDs = append(attachedServerIDs, testServerID) + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if bastionSGID != testRemoteSGID { + t.Fatalf("bastionSGID = %q, want %q", bastionSGID, testRemoteSGID) + } + if len(nodeRulesCreated) != 2 { + t.Fatalf("nodeRulesCreated = %#v, want 2 rules (ingress from bastion + egress)", nodeRulesCreated) + } + if nodeRulesCreated[0]["remoteSecurityGroupId"] != testRemoteSGID { + t.Fatalf("node ingress rule remoteSecurityGroupId = %v, want %q", nodeRulesCreated[0]["remoteSecurityGroupId"], testRemoteSGID) + } + if nodeRulesCreated[1]["direction"] != "egress" { + t.Fatalf("node egress rule direction = %v, want egress", nodeRulesCreated[1]["direction"]) + } + if !slices.Equal(attachedServerIDs, []string{testServerID}) { + t.Fatalf("attachedServerIDs = %v, want [%s]", attachedServerIDs, testServerID) + } +} + +func TestServiceReconcileRules(t *testing.T) { + var ( + createdRules []map[string]any + deletedRuleIDs []string + ) + specs := []securitygroup.SecurityGroupSpec{{ + Name: "cluster-sg", + Tags: map[string]string{"cluster": "test"}, + Rules: []securitygroup.Rule{ + { + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 443, + PortMax: 443, + IPRange: "203.0.113.0/24", + }, + { + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 443, + PortMax: 443, + IPRange: "203.0.113.0/24", + }, + { + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 10250, + PortMax: 10250, + RemoteSecurityGroupID: testRemoteSGID, + }, + }, + }} + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{ + "id": testSecurityGroupID, + "name": "cluster-sg", + "labels": map[string]any{"cluster": "test"}, + }, + }}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{ + "id": testRuleID1, + "direction": "ingress", + "remoteSecurityGroupId": testRemoteSGID, + "portRange": map[string]any{"min": 10250, "max": 10250}, + "protocol": map[string]any{"name": "tcp"}, + }, + map[string]any{ + "id": testRuleID2, + "direction": "ingress", + "ipRange": "198.51.100.0/24", + "portRange": map[string]any{"min": 22, "max": 22}, + "protocol": map[string]any{"name": "tcp"}, + }, + }}) + case r.Method == http.MethodPost && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + createdRules = append(createdRules, readJSON(t, r)) + writeJSON(t, w, map[string]any{"id": "88888888-8888-4888-8888-888888888888", "direction": "ingress"}) + case r.Method == http.MethodDelete && strings.Contains(path, "/rules/"): + parts := strings.Split(strings.TrimSuffix(path, "/"), "/") + deletedRuleIDs = append(deletedRuleIDs, parts[len(parts)-1]) + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + + if len(createdRules) != 1 || createdRules[0]["ipRange"] != "203.0.113.0/24" { + t.Fatalf("createdRules = %#v, want single rule for 203.0.113.0/24", createdRules) + } + if !slices.Equal(deletedRuleIDs, []string{testRuleID2}) { + t.Fatalf("deletedRuleIDs = %v, want [%s]", deletedRuleIDs, testRuleID2) + } +} + +func TestServiceReconcileValidation(t *testing.T) { + tests := []struct { + name string + spec securitygroup.SecurityGroupSpec + }{ + { + name: "missing name", + spec: securitygroup.SecurityGroupSpec{ + Tags: map[string]string{"cluster": "test"}, + }, + }, + { + name: "missing tags", + spec: securitygroup.SecurityGroupSpec{ + Name: "cluster-sg", + }, + }, + { + name: "invalid rule direction", + spec: securitygroup.SecurityGroupSpec{ + Name: "cluster-sg", + Tags: map[string]string{"cluster": "test"}, + Rules: []securitygroup.Rule{{Direction: "invalid", Protocol: securitygroup.ProtocolTCP, PortMin: 80, PortMax: 80, IPRange: "0.0.0.0/0"}}, + }, + }, + { + name: "both ipRange and remoteSecurityGroupID set", + spec: securitygroup.SecurityGroupSpec{ + Name: "cluster-sg", + Tags: map[string]string{"cluster": "test"}, + Rules: []securitygroup.Rule{{ + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 80, + PortMax: 80, + IPRange: "0.0.0.0/0", + RemoteSecurityGroupID: testRemoteSGID, + }}, + }, + }, + { + name: "both ipRange and remoteSecurityGroupTags set", + spec: securitygroup.SecurityGroupSpec{ + Name: "cluster-sg", + Tags: map[string]string{"cluster": "test"}, + Rules: []securitygroup.Rule{{ + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 80, + PortMax: 80, + IPRange: "0.0.0.0/0", + RemoteSecurityGroupTags: map[string]string{"role": "bastion"}, + }}, + }, + }, + { + name: "invalid port range", + spec: securitygroup.SecurityGroupSpec{ + Name: "cluster-sg", + Tags: map[string]string{"cluster": "test"}, + Rules: []securitygroup.Rule{{Direction: securitygroup.DirectionIngress, Protocol: securitygroup.ProtocolTCP, PortMin: 8080, PortMax: 80, IPRange: "0.0.0.0/0"}}, + }, + }, + { + name: "port range without protocol", + spec: securitygroup.SecurityGroupSpec{ + Name: "cluster-sg", + Tags: map[string]string{"cluster": "test"}, + Rules: []securitygroup.Rule{{Direction: securitygroup.DirectionIngress, PortMin: 80, PortMax: 80, IPRange: "0.0.0.0/0"}}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + svc := newTestService(t, []securitygroup.SecurityGroupSpec{tt.spec}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + t.Fatalf("unexpected request before validation failure: %s %s", r.Method, r.URL.String()) + })) + + err := svc.Reconcile(context.Background()) + if !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("Reconcile() error = %v, want ErrInvalidInput", err) + } + }) + } +} + +func TestServiceDelete(t *testing.T) { + t.Run("deletes rules and security group using ID when provided", func(t *testing.T) { + var ( + deletedRules []string + deletedSGID string + ) + specs := []securitygroup.SecurityGroupSpec{{ + ID: testSecurityGroupID, + }} + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testRuleID1, "direction": "ingress"}, + map[string]any{"id": testRuleID2, "direction": "egress"}, + }}) + case r.Method == http.MethodDelete && strings.Contains(path, "/rules/"): + parts := strings.Split(strings.TrimSuffix(path, "/"), "/") + deletedRules = append(deletedRules, parts[len(parts)-1]) + w.WriteHeader(http.StatusNoContent) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID): + deletedSGID = testSecurityGroupID + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v", err) + } + if !slices.Equal(deletedRules, []string{testRuleID1, testRuleID2}) { + t.Fatalf("deletedRules = %v, want [%s %s]", deletedRules, testRuleID1, testRuleID2) + } + if deletedSGID != testSecurityGroupID { + t.Fatalf("deletedSGID = %q, want %q", deletedSGID, testSecurityGroupID) + } + }) + + t.Run("deletes rules and security group found by tags", func(t *testing.T) { + var ( + deletedRules []string + deletedSGID string + ) + specs := []securitygroup.SecurityGroupSpec{{ + Tags: map[string]string{"cluster": "test"}, + }} + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testSecurityGroupID, "name": "cluster-sg", "labels": map[string]any{"cluster": "test"}}, + }}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testRuleID1, "direction": "ingress"}, + map[string]any{"id": testRuleID2, "direction": "egress"}, + }}) + case r.Method == http.MethodDelete && strings.Contains(path, "/rules/"): + parts := strings.Split(strings.TrimSuffix(path, "/"), "/") + deletedRules = append(deletedRules, parts[len(parts)-1]) + w.WriteHeader(http.StatusNoContent) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID): + deletedSGID = testSecurityGroupID + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v", err) + } + if !slices.Equal(deletedRules, []string{testRuleID1, testRuleID2}) { + t.Fatalf("deletedRules = %v, want [%s %s]", deletedRules, testRuleID1, testRuleID2) + } + if deletedSGID != testSecurityGroupID { + t.Fatalf("deletedSGID = %q, want %q", deletedSGID, testSecurityGroupID) + } + }) + + t.Run("succeeds when security group does not exist", func(t *testing.T) { + specs := []securitygroup.SecurityGroupSpec{{ + Tags: map[string]string{"cluster": "test"}, + }} + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + writeJSON(t, w, map[string]any{"items": []any{}}) + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v, want nil", err) + } + }) + + t.Run("returns ErrInvalidInput when tags are empty", func(t *testing.T) { + svc := newTestService(t, []securitygroup.SecurityGroupSpec{{}}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + err := svc.Delete(context.Background()) + if !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("Delete() error = %v, want ErrInvalidInput", err) + } + }) +} + +func TestServiceDeleteStaleIDFallbackAndDetach(t *testing.T) { + var ( + detachedPairs []string + deletedSGIDs []string + ) + specs := []securitygroup.SecurityGroupSpec{ + { + ID: "99999999-9999-4999-8999-999999999999", + Tags: map[string]string{"role": "bastion"}, + }, + { + Tags: map[string]string{"role": "node-ssh"}, + ServerSelector: map[string]string{"cluster": "test"}, + }, + } + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/99999999-9999-4999-8999-999999999999/rules"): + w.WriteHeader(http.StatusNotFound) + writeJSON(t, w, map[string]any{"message": "not found"}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): + selector := r.URL.Query().Get("label_selector") + if strings.Contains(selector, "role=node-ssh") { + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testSecurityGroupID, "name": "node-ssh", "labels": map[string]any{"role": "node-ssh"}}, + }}) + return + } + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testRemoteSGID, "name": "bastion-ssh", "labels": map[string]any{"role": "bastion"}}, + }}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{ + "id": testServerID, + "name": "worker-1", + "machineType": "c2i.1", + "labels": map[string]any{"cluster": "test"}, + }, + }}) + case r.Method == http.MethodDelete && strings.Contains(path, "/servers/") && strings.Contains(path, "/security-groups/"): + parts := strings.Split(path, "/servers/") + sub := strings.Split(parts[1], "/security-groups/") + detachedPairs = append(detachedPairs, sub[0]+":"+sub[1]) + w.WriteHeader(http.StatusNoContent) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/rules"): + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodDelete && strings.Contains(path, "/security-groups/"): + id := path[strings.LastIndex(path, "/security-groups/")+len("/security-groups/"):] + deletedSGIDs = append(deletedSGIDs, id) + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v", err) + } + if !slices.Equal(detachedPairs, []string{testServerID + ":" + testSecurityGroupID}) { + t.Fatalf("detachedPairs = %v, want [%s:%s]", detachedPairs, testServerID, testSecurityGroupID) + } + if !slices.Equal(deletedSGIDs, []string{testSecurityGroupID, testRemoteSGID}) { + t.Fatalf("deletedSGIDs = %v, want [%s %s] (reverse order: node-ssh then bastion-ssh)", deletedSGIDs, testSecurityGroupID, testRemoteSGID) + } +} + +func TestFindByTagsAndDeleteByID(t *testing.T) { + t.Run("FindByTags returns matching security group", func(t *testing.T) { + svc := newTestService(t, nil, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testSecurityGroupID, "name": "bastion-ssh", "labels": map[string]any{"cluster": "test"}}, + }}) + })) + + got, err := svc.FindByTags(context.Background(), map[string]string{"cluster": "test"}) + if err != nil { + t.Fatalf("FindByTags() error = %v", err) + } + if got.ID != testSecurityGroupID { + t.Fatalf("FindByTags().ID = %q, want %q", got.ID, testSecurityGroupID) + } + }) + + t.Run("DeleteByID deletes rules and security group and ignores NotFound", func(t *testing.T) { + var ( + deletedRules []string + deletedSGID string + ) + svc := newTestService(t, nil, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testRuleID1, "direction": "ingress"}, + }}) + case r.Method == http.MethodDelete && strings.Contains(path, "/rules/"): + parts := strings.Split(strings.TrimSuffix(path, "/"), "/") + deletedRules = append(deletedRules, parts[len(parts)-1]) + w.WriteHeader(http.StatusNoContent) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID): + deletedSGID = testSecurityGroupID + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.DeleteByID(context.Background(), testSecurityGroupID); err != nil { + t.Fatalf("DeleteByID() error = %v", err) + } + if !slices.Equal(deletedRules, []string{testRuleID1}) { + t.Fatalf("deletedRules = %v, want [%s]", deletedRules, testRuleID1) + } + if deletedSGID != testSecurityGroupID { + t.Fatalf("deletedSGID = %q, want %q", deletedSGID, testSecurityGroupID) + } + }) +} diff --git a/cloud/services/securitygroup/rules.go b/cloud/services/securitygroup/rules.go new file mode 100644 index 0000000..901f585 --- /dev/null +++ b/cloud/services/securitygroup/rules.go @@ -0,0 +1,198 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package securitygroup + +import ( + "context" + "fmt" + "slices" + "strings" + + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +func (c *Client) ensureSecurityGroupRules(ctx context.Context, securityGroupID string, rules []Rule) error { + if securityGroupID == "" { + return fmt.Errorf("%w: security group ID is required", shared.ErrInvalidInput) + } + existingRules, err := c.listSecurityGroupRules(ctx, securityGroupID) + if err != nil { + return err + } + desired := make([]Rule, 0, len(rules)) + for _, rule := range rules { + rule, err := c.resolveRule(ctx, rule) + if err != nil { + return err + } + if slices.ContainsFunc(desired, rule.equal) { + continue + } + desired = append(desired, rule) + if hasRule(existingRules, rule) { + continue + } + if err := c.createSecurityGroupRule(ctx, securityGroupID, rule); err != nil { + return err + } + } + + for _, existing := range existingRules { + if slices.ContainsFunc(desired, ruleFromSDK(existing).equal) { + continue + } + if err := c.deleteSecurityGroupRule(ctx, securityGroupID, existing.GetId()); err != nil { + return err + } + } + return nil +} + +func (c *Client) resolveRule(ctx context.Context, rule Rule) (Rule, error) { + rule = rule.normalize() + if rule.RemoteSecurityGroupID == "" && len(rule.RemoteSecurityGroupTags) > 0 { + remoteSG, err := c.FindByTags(ctx, rule.RemoteSecurityGroupTags) + if err != nil { + return Rule{}, err + } + rule.RemoteSecurityGroupID = remoteSG.ID + } + rule.RemoteSecurityGroupTags = nil + return rule, nil +} + +func (c *Client) createSecurityGroupRule(ctx context.Context, securityGroupID string, rule Rule) error { + if _, err := c.iaasClient.DefaultAPI.CreateSecurityGroupRule(ctx, c.projectID, c.region, securityGroupID). + CreateSecurityGroupRulePayload(rule.toCreatePayload()). + Execute(); err != nil { + return shared.ClassifySDKError("create security group rule", err) + } + return nil +} + +func (c *Client) listSecurityGroupRules(ctx context.Context, securityGroupID string) ([]iaas.SecurityGroupRule, error) { + resp, err := c.iaasClient.DefaultAPI.ListSecurityGroupRules(ctx, c.projectID, c.region, securityGroupID).Execute() + if err != nil { + return nil, shared.ClassifySDKError("list security group rules", err) + } + return resp.GetItems(), nil +} + +func (c *Client) deleteSecurityGroupRule(ctx context.Context, securityGroupID, ruleID string) error { + if ruleID == "" { + return nil + } + if err := c.iaasClient.DefaultAPI. + DeleteSecurityGroupRule(ctx, c.projectID, c.region, securityGroupID, ruleID). + Execute(); err != nil { + err := shared.ClassifySDKError("delete security group rule", err) + if !shared.IsNotFound(err) { + return err + } + } + return nil +} + +func (r Rule) validate() error { + if r.Direction != DirectionIngress && r.Direction != DirectionEgress { + return fmt.Errorf( + "%w: security group rule direction must be %q or %q", + shared.ErrInvalidInput, + DirectionIngress, + DirectionEgress, + ) + } + if r.IPRange != "" && (r.RemoteSecurityGroupID != "" || len(r.RemoteSecurityGroupTags) > 0) { + return fmt.Errorf( + "%w: security group rule cannot specify both ipRange and remoteSecurityGroupID", + shared.ErrInvalidInput, + ) + } + if r.hasInvalidPortRange() { + return fmt.Errorf("%w: invalid security group rule port range", shared.ErrInvalidInput) + } + if r.hasPortRange() && r.Protocol == "" { + return fmt.Errorf("%w: security group rule port range requires a protocol", shared.ErrInvalidInput) + } + return nil +} + +func (r Rule) hasPortRange() bool { + return r.PortMin > 0 || r.PortMax > 0 +} + +func (r Rule) hasInvalidPortRange() bool { + return r.PortMin < 0 || r.PortMax < 0 || r.PortMin > r.PortMax +} + +func (p Protocol) normalize() Protocol { + return Protocol(strings.ToLower(string(p))) +} + +func (r Rule) normalize() Rule { + r.Protocol = r.Protocol.normalize() + return r +} + +func (r Rule) equal(other Rule) bool { + return r.Direction == other.Direction && + r.Protocol == other.Protocol && + r.PortMin == other.PortMin && + r.PortMax == other.PortMax && + r.IPRange == other.IPRange && + r.RemoteSecurityGroupID == other.RemoteSecurityGroupID +} + +func (r Rule) toCreatePayload() iaas.CreateSecurityGroupRulePayload { + payload := iaas.NewCreateSecurityGroupRulePayload(string(r.Direction)) + if r.Protocol != "" { + protocol := iaas.StringAsCreateProtocol(new(string(r.Protocol))) + payload.SetProtocol(protocol) + } + if r.hasPortRange() { + payload.SetPortRange(*iaas.NewPortRange(r.PortMin, r.PortMax)) + } + if r.IPRange != "" { + payload.SetIpRange(r.IPRange) + } + if r.RemoteSecurityGroupID != "" { + payload.SetRemoteSecurityGroupId(r.RemoteSecurityGroupID) + } + return *payload +} + +func hasRule(rules []iaas.SecurityGroupRule, desired Rule) bool { + return slices.ContainsFunc(rules, func(rule iaas.SecurityGroupRule) bool { + return ruleFromSDK(rule).equal(desired) + }) +} + +func ruleFromSDK(in iaas.SecurityGroupRule) Rule { + var ruleProtocol Protocol + if protocol, ok := in.GetProtocolOk(); ok { + ruleProtocol = Protocol(protocol.GetName()).normalize() + } + var portMin, portMax int64 + if portRange, ok := in.GetPortRangeOk(); ok { + portMin = portRange.GetMin() + portMax = portRange.GetMax() + } + return Rule{ + Direction: Direction(in.GetDirection()), + Protocol: ruleProtocol, + PortMin: portMin, + PortMax: portMax, + IPRange: in.GetIpRange(), + RemoteSecurityGroupID: in.GetRemoteSecurityGroupId(), + } +} diff --git a/cloud/services/securitygroup/service.go b/cloud/services/securitygroup/service.go new file mode 100644 index 0000000..584227a --- /dev/null +++ b/cloud/services/securitygroup/service.go @@ -0,0 +1,102 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package securitygroup + +import iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + +// Direction specifies the traffic direction of a security group rule. +type Direction string + +const ( + DirectionIngress Direction = "ingress" + DirectionEgress Direction = "egress" +) + +// Protocol specifies the network protocol of a security group rule. +type Protocol string + +const ( + ProtocolTCP Protocol = "tcp" + ProtocolUDP Protocol = "udp" + ProtocolICMP Protocol = "icmp" +) + +// SecurityGroup describes a STACKIT security group. +type SecurityGroup struct { + ID string + Name string +} + +// Rule describes a desired STACKIT security group rule. +type Rule struct { + Direction Direction + Protocol Protocol + PortMin int64 + PortMax int64 + IPRange string + RemoteSecurityGroupID string + RemoteSecurityGroupTags map[string]string +} + +// SecurityGroupSpec describes the desired state of a STACKIT security group and its rules. +type SecurityGroupSpec struct { + ID string + Name string + Description string + Tags map[string]string + Rules []Rule + ServerIDs []string + ServerSelector map[string]string + SetID func(string) +} + +// ClientScope is an interface that provides the IaaS client and target scope for security group operations. +type ClientScope interface { + ProjectID() string + Region() string + IaaSClient() *iaas.APIClient +} + +// Scope is an interface that provides scope data needed for security group reconciliation. +type Scope interface { + ClientScope + SecurityGroupSpecs() []SecurityGroupSpec +} + +// Client provides direct security group operations against the STACKIT IaaS API. +type Client struct { + iaasClient *iaas.APIClient + projectID string + region string +} + +// NewClient returns a Client for the given scope. +func NewClient(scope ClientScope) *Client { + return &Client{ + iaasClient: scope.IaaSClient(), + projectID: scope.ProjectID(), + region: scope.Region(), + } +} + +// Service implements security groups reconciler. +type Service struct { + scope Scope + *Client +} + +// New returns Service from given scope. +func New(scope Scope) *Service { + return &Service{ + scope: scope, + Client: NewClient(scope), + } +} diff --git a/cloud/types.go b/cloud/types.go index 6cca29a..98b9fda 100644 --- a/cloud/types.go +++ b/cloud/types.go @@ -15,6 +15,7 @@ package cloud import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) @@ -44,10 +45,7 @@ type LoadBalancer struct { type PublicIP = publicip.PublicIP // SecurityGroup describes a STACKIT security group. -type SecurityGroup struct { - ID string - Name string -} +type SecurityGroup = securitygroup.SecurityGroup // Bastion describes the provider-managed SSH bastion resources. type Bastion struct { @@ -84,20 +82,12 @@ type BastionInput struct { AvailabilityZone string SSHKeyName string AllowedCIDRs []string + SecurityGroupID string Tags map[string]string RootVolume RootVolumeInput CloudInit []byte } -// NodeSSHAccessInput holds parameters required to allow SSH from the bastion -// security group to cluster nodes. -type NodeSSHAccessInput struct { - Name string - ServerID string - BastionSecurityGroupID string - Tags map[string]string -} - // RootVolumeInput describes the root disk of a VM. type RootVolumeInput struct { SizeGiB int diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index e2c1a1a..8c7d888 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -30,6 +30,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud" bastionservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/bastion" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/scope" ) @@ -115,13 +116,21 @@ func (r *StackitClusterReconciler) reconcileBastion( return ctrl.Result{RequeueAfter: retryableErrorRequeueAfter}, false, nil } - hadBastionStatus := hasBastionStatus(stackitCluster.Status.Bastion) + hadBastionServer := stackitCluster.Status.Bastion.ServerID != "" + // Temporary ordering until bastion server reconciliation is extracted into a + // service: ensure the security group first so its ID can be attached to the + // bastion server by EnsureBastion. + if err := securitygroup.New(clusterScope).Reconcile(ctx); err != nil { + return ctrl.Result{}, false, err + } + input.SecurityGroupID = stackitCluster.Status.Bastion.SecurityGroupID + bastion, err := cloudClient.EnsureBastion(ctx, input) if err != nil { return ctrl.Result{}, false, err } clusterScope.SetBastionStatus(bastion, bastionCloudInitHash(cloudInit)) - if !hadBastionStatus { + if !hadBastionServer { record.Eventf( stackitCluster, "BastionCreated", "Created bastion %s", bastion.ServerID, ) @@ -192,17 +201,20 @@ func (r *StackitClusterReconciler) deleteBastionResources( status cloud.Bastion, ignoreNotFound bool, ) error { - if err := cloudClient.DeleteNodeSSHAccess(ctx, bastionservice.NodeSSHAccessTags(clusterScope.StackitCluster)); err != nil { + if err := publicip.New(clusterScope).Delete(ctx); err != nil { if !ignoreNotFound || !cloud.IsNotFound(err) { return err } } - if err := publicip.New(clusterScope).Delete(ctx); err != nil { + if err := cloudClient.DeleteBastion(ctx, input, status); err != nil { if !ignoreNotFound || !cloud.IsNotFound(err) { return err } } - if err := cloudClient.DeleteBastion(ctx, input, status); err != nil { + // Temporary ordering until bastion server reconciliation is extracted into a + // service: the security group must be deleted after the server is detached + // and deleted by DeleteBastion. + if err := securitygroup.New(clusterScope).Delete(ctx); err != nil { if !ignoreNotFound || !cloud.IsNotFound(err) { return err } @@ -211,7 +223,7 @@ func (r *StackitClusterReconciler) deleteBastionResources( } func bastionNeedsRecreate(stackitCluster *infrav1.StackitCluster, cloudInit []byte) bool { - if !hasBastionStatus(stackitCluster.Status.Bastion) { + if stackitCluster.Status.Bastion.ServerID == "" { return false } return stackitCluster.Status.Bastion.CloudInitHash != bastionCloudInitHash(cloudInit) diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index ccd6cf1..3898f6d 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -28,7 +28,9 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" - bastionservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/bastion" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/scope" + "github.com/stackitcloud/cluster-api-provider-stackit/util" ) var _ = Describe("StackitCluster Controller", func() { @@ -135,7 +137,34 @@ var _ = Describe("StackitCluster Controller", func() { Expect(got.Status.Bastion.SecurityGroupID).NotTo(BeEmpty()) Expect(fakeCloud.ServerCount()).To(Equal(1)) Expect(fakeCloud.PublicIPCount()).To(Equal(1)) - Expect(fakeCloud.SecurityGroupCount()).To(Equal(1)) + Expect(fakeCloud.SecurityGroupCount()).To(Equal(2)) + Expect(fakeCloud.SecurityGroupRules(got.Status.Bastion.SecurityGroupID)).To(ConsistOf( + securitygroup.Rule{ + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 22, + PortMax: 22, + IPRange: "203.0.113.10/32", + }, + securitygroup.Rule{ + Direction: securitygroup.DirectionEgress, + }, + )) + nodeSGs, err := fakeCloud.ListSecurityGroupsByTags(ctx, scope.NodeSSHTags(got)) + Expect(err).NotTo(HaveOccurred()) + Expect(nodeSGs).To(HaveLen(1)) + Expect(fakeCloud.SecurityGroupRules(nodeSGs[0].ID)).To(ConsistOf( + securitygroup.Rule{ + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 22, + PortMax: 22, + RemoteSecurityGroupID: got.Status.Bastion.SecurityGroupID, + }, + securitygroup.Rule{ + Direction: securitygroup.DirectionEgress, + }, + )) expectCondition(got.Status.Conditions, infrav1.ClusterBastionReadyCondition, metav1.ConditionTrue, "Available") }) @@ -169,14 +198,18 @@ var _ = Describe("StackitCluster Controller", func() { Expect(err).NotTo(HaveOccurred()) Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) - _, err = fakeCloud.EnsureNodeSSHAccess(ctx, cloud.NodeSSHAccessInput{ - Name: got.Name + "-node-ssh", - ServerID: got.Status.Bastion.ServerID, - BastionSecurityGroupID: got.Status.Bastion.SecurityGroupID, - Tags: bastionservice.NodeSSHAccessTags(got), + Expect(fakeCloud.SecurityGroupCount()).To(Equal(2)) + nodeSGs, err := fakeCloud.ListSecurityGroupsByTags(ctx, scope.NodeSSHTags(got)) + Expect(err).NotTo(HaveOccurred()) + Expect(nodeSGs).To(HaveLen(1)) + workerServer, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + Name: got.Name + "-worker-0", + Tags: util.MachineTags(got.Name, got.Namespace, "worker-0", "uid-1", nil), + SecurityGroups: []string{nodeSGs[0].ID}, }) Expect(err).NotTo(HaveOccurred()) - Expect(fakeCloud.SecurityGroupCount()).To(Equal(2)) + Expect(fakeCloud.ServerHasSecurityGroup(workerServer.ID, nodeSGs[0].ID)).To(BeTrue()) + got.Spec.Bastion.Enabled = false Expect(k8sClient.Update(ctx, got)).To(Succeed()) @@ -186,7 +219,8 @@ var _ = Describe("StackitCluster Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(got.Status.Bastion).To(Equal(infrav1.StackitBastionStatus{})) - Expect(fakeCloud.ServerCount()).To(Equal(0)) + Expect(fakeCloud.ServerHasSecurityGroup(workerServer.ID, nodeSGs[0].ID)).To(BeFalse()) + Expect(fakeCloud.ServerCount()).To(Equal(1)) Expect(fakeCloud.PublicIPCount()).To(Equal(0)) Expect(fakeCloud.SecurityGroupCount()).To(Equal(0)) expectCondition(got.Status.Conditions, infrav1.ClusterBastionReadyCondition, metav1.ConditionTrue, "Skipped") @@ -202,13 +236,6 @@ var _ = Describe("StackitCluster Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(got.Status.Bastion.PublicIP).NotTo(BeEmpty()) - _, err = fakeCloud.EnsureNodeSSHAccess(ctx, cloud.NodeSSHAccessInput{ - Name: got.Name + "-node-ssh", - ServerID: got.Status.Bastion.ServerID, - BastionSecurityGroupID: got.Status.Bastion.SecurityGroupID, - Tags: bastionservice.NodeSSHAccessTags(got), - }) - Expect(err).NotTo(HaveOccurred()) Expect(fakeCloud.SecurityGroupCount()).To(Equal(2)) Expect(k8sClient.Delete(ctx, got)).To(Succeed()) @@ -450,13 +477,6 @@ var _ = Describe("StackitCluster Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) oldServerID := got.Status.Bastion.ServerID Expect(oldServerID).NotTo(BeEmpty()) - _, err = fakeCloud.EnsureNodeSSHAccess(ctx, cloud.NodeSSHAccessInput{ - Name: got.Name + "-node-ssh", - ServerID: got.Status.Bastion.ServerID, - BastionSecurityGroupID: got.Status.Bastion.SecurityGroupID, - Tags: bastionservice.NodeSSHAccessTags(got), - }) - Expect(err).NotTo(HaveOccurred()) Expect(fakeCloud.SecurityGroupCount()).To(Equal(2)) configMap := &corev1.ConfigMap{} diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index f2c55e8..318c18d 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -34,6 +34,7 @@ import ( bastionservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/bastion" loadbalancerservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -385,8 +386,9 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS } // Cleanup runs unconditionally: neither spec nor status is a trustworthy - // record of what exists in the cloud. ResolveID, DeleteBastion and - // DeleteNodeSSHAccess fall back to tag lookups and tolerate NotFound. + // record of what exists in the cloud. ResolveID, DeleteBastion, and the + // public IP and security group services fall back to tag lookups and + // tolerate NotFound. cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, stackitCluster) if err != nil { // A missing credentials Secret cannot be recovered from and commonly @@ -435,12 +437,6 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS "Deleted API server load balancer %s", loadBalancerID, ) } - if err := cloudClient.DeleteNodeSSHAccess(ctx, bastionservice.NodeSSHAccessTags(stackitCluster)); err != nil && !cloud.IsNotFound(err) { - log.Error(err, "Failed to delete bastion node SSH access", "cluster", stackitCluster.Name) - record.Warnf(stackitCluster, "FailedDeleteNodeSSHAccess", - "Failed to delete bastion node SSH access: %v", err) - return ctrl.Result{}, err - } hadBastion := hasBastionStatus(stackitCluster.Status.Bastion) if err := publicip.New(clusterScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { log.Error(err, "Failed to delete bastion public IP", "cluster", stackitCluster.Name) @@ -457,6 +453,12 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS "Failed to delete bastion: %v", err) return ctrl.Result{}, err } + if err := securitygroup.New(clusterScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { + log.Error(err, "Failed to delete bastion security groups", "cluster", stackitCluster.Name) + record.Warnf(stackitCluster, "FailedDeleteBastion", + "Failed to delete bastion security groups: %v", err) + return ctrl.Result{}, err + } if hadBastion { clusterScope.ClearBastionStatus() record.Event(stackitCluster, "BastionDeleted", "Deleted bastion") diff --git a/controller/stackitmachine_controller_test.go b/controller/stackitmachine_controller_test.go index 0efd701..85b1b39 100644 --- a/controller/stackitmachine_controller_test.go +++ b/controller/stackitmachine_controller_test.go @@ -28,6 +28,7 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -237,7 +238,7 @@ var _ = Describe("StackitMachine Controller", func() { got := &infrav1.StackitMachine{} Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(got.Status.Ready).To(BeTrue()) - Expect(fakeCloud.EnsureNodeSSHCalls).To(Equal(1)) + Expect(fakeCloud.AddSecurityGroupCalls).To(Equal(1)) securityGroups, err := fakeCloud.ListSecurityGroupsByTags(ctx, map[string]string{ util.LabelClusterName: clusterName, util.LabelResourceRole: util.ResourceRoleNodeSSH, @@ -245,6 +246,18 @@ var _ = Describe("StackitMachine Controller", func() { Expect(err).NotTo(HaveOccurred()) Expect(securityGroups).To(HaveLen(1)) Expect(fakeCloud.SecurityGroupRemoteSources(securityGroups[0].ID)).To(ConsistOf("bastion-sg")) + Expect(fakeCloud.SecurityGroupRules(securityGroups[0].ID)).To(ConsistOf( + securitygroup.Rule{ + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 22, + PortMax: 22, + RemoteSecurityGroupID: "bastion-sg", + }, + securitygroup.Rule{ + Direction: securitygroup.DirectionEgress, + }, + )) Expect(fakeCloud.ServerHasSecurityGroup(got.Status.InstanceID, securityGroups[0].ID)).To(BeTrue()) }) @@ -257,7 +270,7 @@ var _ = Describe("StackitMachine Controller", func() { stackitCluster.Status.Bastion.SecurityGroupID = "bastion-sg" Expect(k8sClient.Status().Update(ctx, stackitCluster)).To(Succeed()) - fakeCloud.FailNextEnsureNodeSSH = fmt.Errorf("node ssh access timeout: %w", cloud.ErrTransient) + fakeCloud.FailNextAddSecurityGroup = fmt.Errorf("node ssh access timeout: %w", cloud.ErrTransient) result, err := reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) diff --git a/controller/stackitmachine_infrastructure.go b/controller/stackitmachine_infrastructure.go index 920f016..ba1ae6a 100644 --- a/controller/stackitmachine_infrastructure.go +++ b/controller/stackitmachine_infrastructure.go @@ -29,7 +29,7 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" - bastionservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/bastion" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -91,6 +91,7 @@ func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineS infrav1.MachineReadyCondition, ) } + machineScope.SetCloudClient(cloudClient) machineScope.SetConditions(metav1.ConditionTrue, "Available", "", infrav1.MachineCredentialsReadyCondition) server, created, err := r.ensureServer(ctx, cloudClient, machineScope, bootstrapData) @@ -160,7 +161,7 @@ func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineS return ctrl.Result{RequeueAfter: 15 * time.Second}, nil } - if err := r.reconcileBastionNodeSSHAccess(ctx, cloudClient, machineScope, server); err != nil { + if err := r.reconcileBastionNodeSSHAccess(ctx, machineScope, server); err != nil { log.Error(err, "Failed to configure bastion SSH access for node", "machine", stackitMachine.Name) record.Warnf( stackitMachine, "FailedNodeSSHAccess", @@ -364,7 +365,6 @@ func (r *StackitMachineReconciler) ensureServer( func (r *StackitMachineReconciler) reconcileBastionNodeSSHAccess( ctx context.Context, - cloudClient cloud.Client, machineScope *scope.MachineScope, server *cloud.Server, ) error { @@ -377,13 +377,7 @@ func (r *StackitMachineReconciler) reconcileBastionNodeSSHAccess( if server == nil || server.ID == "" { return fmt.Errorf("%w: server ID is empty", cloud.ErrTransient) } - _, err := cloudClient.EnsureNodeSSHAccess(ctx, cloud.NodeSSHAccessInput{ - Name: machineScope.StackitCluster.Name + "-node-ssh", - ServerID: server.ID, - BastionSecurityGroupID: machineScope.StackitCluster.Status.Bastion.SecurityGroupID, - Tags: bastionservice.NodeSSHAccessTags(machineScope.StackitCluster), - }) - return err + return securitygroup.New(machineScope).Reconcile(ctx) } func (r *StackitMachineReconciler) getStackitCluster(ctx context.Context, cluster *clusterv1.Cluster) (*infrav1.StackitCluster, error) { diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index 0bb4c8b..af7c1bc 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -25,10 +25,13 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/util" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" ) +const bastionSSHPort = 22 + // ClusterScope holds the per-reconcile state for a StackitCluster. type ClusterScope struct { Client client.Client @@ -91,10 +94,16 @@ func (s *ClusterScope) SetAPIServerEndpoint(endpoint clusterv1.APIEndpoint) { func (s *ClusterScope) SetBastionStatus(bastion *cloud.Bastion, cloudInitHash string) { s.StackitCluster.Status.Bastion.ServerID = bastion.ServerID - s.StackitCluster.Status.Bastion.SecurityGroupID = bastion.SecurityGroupID + if bastion.SecurityGroupID != "" { + s.StackitCluster.Status.Bastion.SecurityGroupID = bastion.SecurityGroupID + } s.StackitCluster.Status.Bastion.CloudInitHash = cloudInitHash } +func (s *ClusterScope) SetBastionSecurityGroupID(id string) { + s.StackitCluster.Status.Bastion.SecurityGroupID = id +} + func (s *ClusterScope) ClearBastionStatus() { s.StackitCluster.Status.Bastion = infrav1.StackitBastionStatus{} } @@ -160,12 +169,78 @@ func (s *ClusterScope) BastionStatus() infrav1.StackitBastionStatus { } func (s *ClusterScope) BastionTags() map[string]string { - tags := util.ClusterTags(s.StackitCluster.Name, s.StackitCluster.Namespace, s.StackitCluster.Spec.AdditionalLabels) + return bastionTags(s.StackitCluster) +} + +func bastionTags(stackitCluster *infrav1.StackitCluster) map[string]string { + tags := util.ClusterTags(stackitCluster.Name, stackitCluster.Namespace, stackitCluster.Spec.AdditionalLabels) tags[util.LabelResourceRole] = util.ResourceRoleBastion return tags } +func NodeSSHTags(stackitCluster *infrav1.StackitCluster) map[string]string { + tags := util.ClusterTags(stackitCluster.Name, stackitCluster.Namespace, stackitCluster.Spec.AdditionalLabels) + tags[util.LabelResourceRole] = util.ResourceRoleNodeSSH + return tags +} + func (s *ClusterScope) SetBastionPublicIP(id, ip string) { s.StackitCluster.Status.Bastion.PublicIPID = id s.StackitCluster.Status.Bastion.PublicIP = ip } + +func (s *ClusterScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { + rules := make([]securitygroup.Rule, 0, len(s.StackitCluster.Spec.Bastion.AllowedCIDRs)+1) + for _, cidr := range s.StackitCluster.Spec.Bastion.AllowedCIDRs { + if cidr == "" { + continue + } + rules = append(rules, securitygroup.Rule{ + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: bastionSSHPort, + PortMax: bastionSSHPort, + IPRange: cidr, + }) + } + rules = append(rules, securitygroup.Rule{ + Direction: securitygroup.DirectionEgress, + }) + return []securitygroup.SecurityGroupSpec{ + { + ID: s.StackitCluster.Status.Bastion.SecurityGroupID, + Name: s.StackitCluster.Name + "-bastion-ssh", + Description: "Cluster API Provider STACKIT bastion SSH access", + Tags: s.BastionTags(), + Rules: rules, + SetID: s.SetBastionSecurityGroupID, + }, + nodeSSHSecurityGroupSpec(s.StackitCluster, ""), + } +} + +func nodeSSHSecurityGroupSpec(stackitCluster *infrav1.StackitCluster, remoteSecurityGroupID string, serverIDs ...string) securitygroup.SecurityGroupSpec { + ingressRule := securitygroup.Rule{ + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: bastionSSHPort, + PortMax: bastionSSHPort, + RemoteSecurityGroupID: remoteSecurityGroupID, + } + if remoteSecurityGroupID == "" { + ingressRule.RemoteSecurityGroupTags = bastionTags(stackitCluster) + } + return securitygroup.SecurityGroupSpec{ + Name: stackitCluster.Name + "-node-ssh", + Description: "Cluster API Provider STACKIT node SSH access from bastion", + Tags: NodeSSHTags(stackitCluster), + ServerIDs: serverIDs, + ServerSelector: util.ClusterTags(stackitCluster.Name, stackitCluster.Namespace, nil), + Rules: []securitygroup.Rule{ + ingressRule, + { + Direction: securitygroup.DirectionEgress, + }, + }, + } +} diff --git a/scope/machine_scope.go b/scope/machine_scope.go index b736ce1..aea2b32 100644 --- a/scope/machine_scope.go +++ b/scope/machine_scope.go @@ -21,7 +21,9 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/util" + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" ) // MachineScope holds the per-reconcile state for a StackitMachine. @@ -31,6 +33,7 @@ type MachineScope struct { Machine *clusterv1.Machine StackitCluster *infrav1.StackitCluster StackitMachine *infrav1.StackitMachine + CloudClient cloud.Client patchHelper *patch.Helper } @@ -129,3 +132,35 @@ func (s *MachineScope) AvailabilityZone() string { return DefaultAvailabilityZone(s.StackitCluster) } + +func (s *MachineScope) SetCloudClient(cloudClient cloud.Client) { + s.CloudClient = cloudClient +} + +func (s *MachineScope) ProjectID() string { + if s.StackitCluster.Spec.ProjectID != "" { + return s.StackitCluster.Spec.ProjectID + } + return s.CloudClient.ProjectID() +} + +func (s *MachineScope) Region() string { + if s.StackitCluster.Spec.Region != "" { + return s.StackitCluster.Spec.Region + } + return s.CloudClient.Region() +} + +func (s *MachineScope) IaaSClient() *iaas.APIClient { + return s.CloudClient.IaaSClient() +} + +func (s *MachineScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { + return []securitygroup.SecurityGroupSpec{ + nodeSSHSecurityGroupSpec( + s.StackitCluster, + s.StackitCluster.Status.Bastion.SecurityGroupID, + s.StackitMachine.Status.InstanceID, + ), + } +} diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index d753b39..ebef540 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -35,6 +35,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/test/utils" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -494,7 +495,7 @@ var _ = Describe("Manager", Ordered, func() { publicIPs, err := publicip.ListByTags(ctx, cloudClient.IaaSClient(), cloudClient.ProjectID(), cloudClient.Region(), stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) g.Expect(publicIPs).To(HaveLen(1)) - securityGroups, err := cloudClient.ListSecurityGroupsByTags(ctx, stackitE2EBastionTags(testID)) + securityGroups, err := securitygroup.NewClient(cloudClient).ListByTags(ctx, stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) g.Expect(securityGroups).To(HaveLen(1)) }, 10*time.Minute, 15*time.Second).Should(Succeed()) @@ -516,7 +517,7 @@ var _ = Describe("Manager", Ordered, func() { if cfg.BastionEnabled { By("verifying the node SSH security group exists") Eventually(func(g Gomega) { - securityGroups, err := cloudClient.ListSecurityGroupsByTags(ctx, stackitE2ENodeSSHTags(testID)) + securityGroups, err := securitygroup.NewClient(cloudClient).ListByTags(ctx, stackitE2ENodeSSHTags(testID)) g.Expect(err).NotTo(HaveOccurred()) g.Expect(securityGroups).To(HaveLen(1)) }, 10*time.Minute, 15*time.Second).Should(Succeed()) @@ -560,10 +561,11 @@ var _ = Describe("Manager", Ordered, func() { publicIPs, err := publicip.ListByTags(ctx, cloudClient.IaaSClient(), cloudClient.ProjectID(), cloudClient.Region(), stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) g.Expect(publicIPs).To(BeEmpty()) - securityGroups, err := cloudClient.ListSecurityGroupsByTags(ctx, stackitE2EBastionTags(testID)) + sgClient := securitygroup.NewClient(cloudClient) + securityGroups, err := sgClient.ListByTags(ctx, stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) g.Expect(securityGroups).To(BeEmpty()) - securityGroups, err = cloudClient.ListSecurityGroupsByTags(ctx, stackitE2ENodeSSHTags(testID)) + securityGroups, err = sgClient.ListByTags(ctx, stackitE2ENodeSSHTags(testID)) g.Expect(err).NotTo(HaveOccurred()) g.Expect(securityGroups).To(BeEmpty()) }, 20*time.Minute, 15*time.Second).Should(Succeed()) diff --git a/test/utils/utils.go b/test/utils/utils.go index a408630..9600e4b 100644 --- a/test/utils/utils.go +++ b/test/utils/utils.go @@ -35,10 +35,12 @@ const ( defaultKindCluster = "kind" ) +// TODO: unreachable func warnError(err error) { _, _ = fmt.Fprintf(GinkgoWriter, "warning: %v\n", err) } +// TODO: unreachable // Run executes the provided command within this context func Run(cmd *exec.Cmd) (string, error) { dir, _ := GetProjectDir() @@ -59,6 +61,7 @@ func Run(cmd *exec.Cmd) (string, error) { return string(output), nil } +// TODO: unreachable // UninstallCertManager uninstalls the cert manager func UninstallCertManager() { url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion) @@ -81,6 +84,7 @@ func UninstallCertManager() { } } +// TODO: unreachable // InstallCertManager installs the cert manager bundle. func InstallCertManager() error { url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion) @@ -100,6 +104,7 @@ func InstallCertManager() error { return err } +// TODO: unreachable // IsCertManagerCRDsInstalled checks if any Cert Manager CRDs are installed // by verifying the existence of key CRDs related to Cert Manager. func IsCertManagerCRDsInstalled() bool { @@ -133,6 +138,7 @@ func IsCertManagerCRDsInstalled() bool { return false } +// TODO: unreachable // LoadImageToKindClusterWithName loads a local docker image to the kind cluster func LoadImageToKindClusterWithName(name string) error { cluster := defaultKindCluster @@ -149,6 +155,7 @@ func LoadImageToKindClusterWithName(name string) error { return err } +// TODO: unreachable // GetNonEmptyLines converts given command output string into individual objects // according to line breakers, and ignores the empty elements in it. func GetNonEmptyLines(output string) []string { @@ -163,6 +170,7 @@ func GetNonEmptyLines(output string) []string { return res } +// TODO: unreachable // GetProjectDir will return the directory where the project is func GetProjectDir() (string, error) { wd, err := os.Getwd() @@ -173,6 +181,7 @@ func GetProjectDir() (string, error) { return wd, nil } +// TODO: unreachable // UncommentCode searches for target in the file and remove the comment prefix // of the target content. The target content may span multiple lines. func UncommentCode(filename, target, prefix string) error { From 2d595d23cf78f7d8a84b37e38eebdd3f283f5484 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Tue, 10 Nov 2026 16:10:40 +0100 Subject: [PATCH 06/60] refactor: public ip service (#35) --- cloud/cleanup.go | 21 ++ cloud/cleanup_test.go | 20 + cloud/client.go | 20 +- cloud/errors.go | 33 +- cloud/fake/client.go | 364 +++++++++++++++---- cloud/sdk_client.go | 307 ++++------------ cloud/sdk_client_test.go | 39 +- cloud/services/bastion/bastion.go | 20 +- cloud/services/bastion/bastion_test.go | 68 ++++ cloud/services/publicip/reconcile.go | 246 +++++++++++++ cloud/services/publicip/reconcile_test.go | 360 ++++++++++++++++++ cloud/services/publicip/service.go | 64 ++++ cloud/services/shared/errors.go | 44 +++ cloud/{ => services/shared}/errors_test.go | 2 +- cloud/services/shared/types.go | 28 ++ cloud/services/shared/utils.go | 96 +++++ cloud/types.go | 51 +-- config/rbac/role.yaml | 15 +- controller/stackitcluster_bastion.go | 44 ++- controller/stackitcluster_controller.go | 1 + controller/stackitcluster_controller_test.go | 19 + controller/stackitcluster_infrastructure.go | 14 +- controller/stackitmachine_controller.go | 1 + docs/src/topics/failure-domains.md | 5 + scope/cluster_scope.go | 80 +++- scope/machine_scope.go | 16 +- test/e2e/e2e_test.go | 5 +- util/conditions.go | 3 +- 28 files changed, 1546 insertions(+), 440 deletions(-) create mode 100644 cloud/services/bastion/bastion_test.go create mode 100644 cloud/services/publicip/reconcile.go create mode 100644 cloud/services/publicip/reconcile_test.go create mode 100644 cloud/services/publicip/service.go create mode 100644 cloud/services/shared/errors.go rename cloud/{ => services/shared}/errors_test.go (98%) create mode 100644 cloud/services/shared/types.go create mode 100644 cloud/services/shared/utils.go diff --git a/cloud/cleanup.go b/cloud/cleanup.go index 9f155d1..3d635e0 100644 --- a/cloud/cleanup.go +++ b/cloud/cleanup.go @@ -15,6 +15,8 @@ import ( "fmt" "maps" "strings" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" ) // CleanupByTags deletes provider-managed cloud resources matching tags without @@ -29,6 +31,25 @@ func CleanupByTags(ctx context.Context, client Client, tags map[string]string) e bastionTags := make(map[string]string, len(tags)+1) maps.Copy(bastionTags, tags) bastionTags["cluster-api-provider-stackit/resource-role"] = "bastion" + bastionServers, err := client.ListServersByTags(ctx, bastionTags) + if err != nil { + errs = append(errs, err.Error()) + } + publicIPs, err := publicip.ListByTags(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), tags) + if err != nil { + errs = append(errs, err.Error()) + } else { + for _, pubIP := range publicIPs { + for _, server := range bastionServers { + if err := publicip.DetachFromServer(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), server.ID, pubIP.ID); err != nil { + errs = append(errs, fmt.Sprintf("detach public IP %q from server %q: %v", pubIP.ID, server.ID, err)) + } + } + if err := publicip.DeleteByID(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), pubIP.ID); err != nil { + errs = append(errs, fmt.Sprintf("delete public IP %q: %v", pubIP.ID, err)) + } + } + } if err := client.DeleteBastion(ctx, BastionInput{Tags: bastionTags}, Bastion{}); err != nil && !IsNotFound(err) { errs = append(errs, fmt.Sprintf("delete bastion resources: %v", err)) } diff --git a/cloud/cleanup_test.go b/cloud/cleanup_test.go index 6ef2778..40f2304 100644 --- a/cloud/cleanup_test.go +++ b/cloud/cleanup_test.go @@ -36,6 +36,12 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { if _, err := client.CreateServer(ctx, cloud.CreateServerInput{Name: "other", Tags: otherTags}); err != nil { t.Fatalf("CreateServer() error = %v", err) } + if _, err := client.CreatePublicIP(ctx, tags); err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + if _, err := client.CreatePublicIP(ctx, otherTags); err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } if _, err := client.EnsureAPIServerLoadBalancer(ctx, cloud.LoadBalancerInput{ Name: "matched", Tags: tags, @@ -86,4 +92,18 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { if len(matchedLoadBalancers) != 0 { t.Fatalf("matched load balancer count = %d, want 0", len(matchedLoadBalancers)) } + matchedPublicIPs, err := client.ListPublicIPsByTags(ctx, tags) + if err != nil { + t.Fatalf("ListPublicIPsByTags() error = %v", err) + } + if len(matchedPublicIPs) != 0 { + t.Fatalf("matched public IP count = %d, want 0", len(matchedPublicIPs)) + } + otherPublicIPs, err := client.ListPublicIPsByTags(ctx, otherTags) + if err != nil { + t.Fatalf("ListPublicIPsByTags(other) error = %v", err) + } + if len(otherPublicIPs) != 1 { + t.Fatalf("other public IP count = %d, want 1", len(otherPublicIPs)) + } } diff --git a/cloud/client.go b/cloud/client.go index a3ab870..7e5bd73 100644 --- a/cloud/client.go +++ b/cloud/client.go @@ -10,7 +10,17 @@ You may obtain a copy of the License at package cloud -import "context" +import ( + "context" + + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" +) + +// Reconciler is a generic interface used by components offering a type of service. +type Reconciler interface { + Reconcile(ctx context.Context) error + Delete(ctx context.Context) error +} // Client is the abstraction over STACKIT cloud APIs used by the controllers. // @@ -23,6 +33,12 @@ import "context" // - be idempotent (CreateServer/EnsureAPIServerLoadBalancer must not produce // duplicates when a resource with matching tags already exists) type Client interface { + ProjectID() string + + Region() string + + IaaSClient() *iaas.APIClient + GetServer(ctx context.Context, id string) (*Server, error) FindServerByTags(ctx context.Context, tags map[string]string) (*Server, error) @@ -43,8 +59,6 @@ type Client interface { DeleteNodeSSHAccess(ctx context.Context, tags map[string]string) error - ListPublicIPsByTags(ctx context.Context, tags map[string]string) ([]*PublicIP, error) - ListSecurityGroupsByTags(ctx context.Context, tags map[string]string) ([]*SecurityGroup, error) EnsureAPIServerLoadBalancer(ctx context.Context, input LoadBalancerInput) (*LoadBalancer, error) diff --git a/cloud/errors.go b/cloud/errors.go index 1aeaa68..e038288 100644 --- a/cloud/errors.go +++ b/cloud/errors.go @@ -10,35 +10,32 @@ You may obtain a copy of the License at package cloud -import "errors" +import "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" -// Sentinel errors returned by Client implementations. Controllers use -// errors.Is to classify them into requeue / condition-update behavior. +// Sentinel errors returned by Client implementations, re-exported from shared. +// Controllers use errors.Is to classify them into requeue / condition-update behavior. var ( - ErrNotFound = errors.New("not found") - ErrUnauthorized = errors.New("unauthorized") - ErrInvalidInput = errors.New("invalid input") - ErrConflict = errors.New("conflict") - ErrTransient = errors.New("transient") + ErrNotFound = shared.ErrNotFound + ErrUnauthorized = shared.ErrUnauthorized + ErrInvalidInput = shared.ErrInvalidInput + ErrConflict = shared.ErrConflict + ErrTransient = shared.ErrTransient ) // IsNotFound reports whether err wraps ErrNotFound. -func IsNotFound(err error) bool { return errors.Is(err, ErrNotFound) } +func IsNotFound(err error) bool { return shared.IsNotFound(err) } // IsUnauthorized reports whether err wraps ErrUnauthorized. -func IsUnauthorized(err error) bool { return errors.Is(err, ErrUnauthorized) } +func IsUnauthorized(err error) bool { return shared.IsUnauthorized(err) } // IsInvalidInput reports whether err wraps ErrInvalidInput. -func IsInvalidInput(err error) bool { return errors.Is(err, ErrInvalidInput) } +func IsInvalidInput(err error) bool { return shared.IsInvalidInput(err) } // IsConflict reports whether err wraps ErrConflict. -func IsConflict(err error) bool { return errors.Is(err, ErrConflict) } +func IsConflict(err error) bool { return shared.IsConflict(err) } // IsTransient reports whether err wraps ErrTransient. -func IsTransient(err error) bool { return errors.Is(err, ErrTransient) } +func IsTransient(err error) bool { return shared.IsTransient(err) } -// IsRetryable reports whether err should trigger a requeue (conflict or -// transient). -func IsRetryable(err error) bool { - return IsConflict(err) || IsTransient(err) -} +// IsRetryable reports whether err should trigger a requeue (conflict or transient). +func IsRetryable(err error) bool { return shared.IsRetryable(err) } diff --git a/cloud/fake/client.go b/cloud/fake/client.go index 06465cc..0f5c589 100644 --- a/cloud/fake/client.go +++ b/cloud/fake/client.go @@ -14,12 +14,18 @@ package fake import ( "context" + "encoding/json" "fmt" "maps" + "net/http" + "net/http/httptest" "slices" + "strings" "sync" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/stackit-sdk-go/core/config" + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" ) const ( @@ -36,24 +42,31 @@ type Client struct { publicIPs map[string]*publicIPEntry securityGroups map[string]*securityGroupEntry networks map[string]*cloud.Network + iaasClient *iaas.APIClient nextID int // failure injection: if non-nil, the next call returns this error and the // field is cleared. - FailNextCreateServer error - FailNextDeleteServer error - FailNextGetServer error - FailNextFindServer error - FailNextEnsureLB error - FailNextDeleteLB error - FailNextSetTargets error - FailNextGetNetwork error - FailNextEnsureBastion error - FailNextDeleteBastion error - FailNextEnsureNodeSSH error - FailNextDeleteNodeSSH error - FailNextListAZ error + FailNextCreateServer error + FailNextDeleteServer error + FailNextGetServer error + FailNextFindServer error + FailNextEnsureLB error + FailNextDeleteLB error + FailNextSetTargets error + FailNextGetNetwork error + FailNextEnsureBastion error + FailNextDeleteBastion error + FailNextEnsureNodeSSH error + FailNextDeleteNodeSSH error + FailNextListAZ error + FailNextGetPublicIP error + FailNextListPublicIPs error + FailNextCreatePublicIP error + FailNextDeletePublicIP error + FailNextAddPublicIP error + FailNextRemovePublicIP error // AvailabilityZones is returned by ListAvailabilityZones if non-empty. // Defaults to eu01-1, eu01-2, eu01-3. @@ -62,6 +75,9 @@ type Client struct { // LastCreateServerInput records the most recent input passed to CreateServer. LastCreateServerInput cloud.CreateServerInput + // LastEnsureBastionInput records the most recent input passed to EnsureBastion. + LastEnsureBastionInput cloud.BastionInput + // Before* hooks, if non-nil, run before the call they belong to. They let a // test observe API server state at the exact moment a cloud call would // happen. Unlike FailNext*, they are not consumed. @@ -113,9 +129,30 @@ func New(networks ...cloud.Network) *Client { n := networks[i] c.networks[n.ID] = &n } + iaasClient, err := iaas.NewAPIClient( + config.WithoutAuthentication(), + config.WithHTTPClient(&http.Client{Transport: roundTripperFunc(c.roundTripIaaS)}), + config.WithEndpoint("http://stackit.fake.local"), + ) + if err != nil { + panic(fmt.Sprintf("create fake iaas client: %v", err)) + } + c.iaasClient = iaasClient return c } +func (c *Client) ProjectID() string { + return "00000000-0000-0000-0000-000000000000" +} + +func (c *Client) Region() string { + return "eu01" +} + +func (c *Client) IaaSClient() *iaas.APIClient { + return c.iaasClient +} + func consume(p *error) error { if *p == nil { return nil @@ -127,7 +164,7 @@ func consume(p *error) error { func (c *Client) genID() string { c.nextID++ - return fmt.Sprintf("fake-%d", c.nextID) + return fmt.Sprintf("00000000-0000-4000-8000-%012d", c.nextID) } func (c *Client) GetServer(_ context.Context, id string) (*cloud.Server, error) { @@ -260,14 +297,11 @@ func (c *Client) EnsureBastion(_ context.Context, input cloud.BastionInput) (*cl } for _, serverEntry := range c.servers { if len(input.Tags) > 0 && mapContains(serverEntry.tags, input.Tags) { - publicIP := c.publicIPByTags(input.Tags) securityGroup := c.securityGroupByTags(input.Tags) return &cloud.Bastion{ ServerID: serverEntry.server.ID, ServerState: serverEntry.server.State, ServerPowerStatus: serverEntry.server.PowerStatus, - PublicIPID: idOfPublicIP(publicIP), - PublicIP: ipOfPublicIP(publicIP), SecurityGroupID: idOfSecurityGroup(securityGroup), }, nil } @@ -285,10 +319,16 @@ func (c *Client) EnsureBastion(_ context.Context, input cloud.BastionInput) (*cl } serverID := c.genID() + c.LastEnsureBastionInput = input + zone := input.AvailabilityZone + if zone == "" { + zone = "eu01-1" + } server := &cloud.Server{ - ID: serverID, - Name: input.Name, - State: "ACTIVE", + ID: serverID, + Name: input.Name, + State: "ACTIVE", + AvailabilityZone: zone, Addresses: []cloud.Address{ {Type: "InternalIP", Address: "10.0.0.20"}, }, @@ -300,20 +340,10 @@ func (c *Client) EnsureBastion(_ context.Context, input cloud.BastionInput) (*cl userData: append([]byte(nil), input.CloudInit...), } - publicIPID := c.genID() - publicIP := &cloud.PublicIP{ - ID: publicIPID, - IP: "203.0.113.22", - NetworkInterfaceID: "fake-nic-" + serverID, - } - c.publicIPs[publicIPID] = &publicIPEntry{publicIP: publicIP, tags: copyTags(input.Tags)} - c.EnsureBastionCalls++ return &cloud.Bastion{ ServerID: serverID, ServerState: server.State, - PublicIPID: publicIPID, - PublicIP: publicIP.IP, SecurityGroupID: securityGroupID, }, nil } @@ -328,9 +358,6 @@ func (c *Client) DeleteBastion(_ context.Context, input cloud.BastionInput, stat if status.ServerID != "" { delete(c.servers, status.ServerID) } - if status.PublicIPID != "" { - delete(c.publicIPs, status.PublicIPID) - } if status.SecurityGroupID != "" { delete(c.securityGroups, status.SecurityGroupID) } @@ -339,11 +366,6 @@ func (c *Client) DeleteBastion(_ context.Context, input cloud.BastionInput, stat delete(c.servers, id) } } - for id, entry := range c.publicIPs { - if mapContains(entry.tags, input.Tags) { - delete(c.publicIPs, id) - } - } for id, entry := range c.securityGroups { if mapContains(entry.tags, input.Tags) { delete(c.securityGroups, id) @@ -407,10 +429,31 @@ func (c *Client) DeleteNodeSSHAccess(_ context.Context, tags map[string]string) return nil } +func (c *Client) GetPublicIP(_ context.Context, id string) (*cloud.PublicIP, error) { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextGetPublicIP); err != nil { + return nil, err + } + if id == "" { + return nil, fmt.Errorf("public IP ID is empty: %w", cloud.ErrInvalidInput) + } + entry, ok := c.publicIPs[id] + if !ok { + return nil, fmt.Errorf("public IP %q: %w", id, cloud.ErrNotFound) + } + out := *entry.publicIP + return &out, nil +} + func (c *Client) ListPublicIPsByTags(_ context.Context, tags map[string]string) ([]*cloud.PublicIP, error) { c.mu.Lock() defer c.mu.Unlock() + if err := consume(&c.FailNextListPublicIPs); err != nil { + return nil, err + } if len(tags) == 0 { return nil, fmt.Errorf("empty tags: %w", cloud.ErrInvalidInput) } @@ -424,6 +467,82 @@ func (c *Client) ListPublicIPsByTags(_ context.Context, tags map[string]string) return publicIPs, nil } +func (c *Client) CreatePublicIP(_ context.Context, tags map[string]string) (*cloud.PublicIP, error) { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextCreatePublicIP); err != nil { + return nil, err + } + id := c.genID() + publicIP := &cloud.PublicIP{ + ID: id, + IP: "203.0.113.22", + } + c.publicIPs[id] = &publicIPEntry{ + publicIP: publicIP, + tags: copyTags(tags), + } + out := *publicIP + return &out, nil +} + +func (c *Client) DeletePublicIP(_ context.Context, id string) error { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextDeletePublicIP); err != nil { + return err + } + if id == "" { + return fmt.Errorf("public IP ID is empty: %w", cloud.ErrInvalidInput) + } + if _, ok := c.publicIPs[id]; !ok { + return fmt.Errorf("public IP %q: %w", id, cloud.ErrNotFound) + } + delete(c.publicIPs, id) + return nil +} + +func (c *Client) AddPublicIPToServer(_ context.Context, serverID, publicIPID string) error { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextAddPublicIP); err != nil { + return err + } + if serverID == "" || publicIPID == "" { + return fmt.Errorf("server ID and public IP ID are required: %w", cloud.ErrInvalidInput) + } + if _, ok := c.servers[serverID]; !ok { + return fmt.Errorf("server %q: %w", serverID, cloud.ErrNotFound) + } + entry, ok := c.publicIPs[publicIPID] + if !ok { + return fmt.Errorf("public IP %q: %w", publicIPID, cloud.ErrNotFound) + } + entry.publicIP.NetworkInterfaceID = "fake-nic-" + serverID + return nil +} + +func (c *Client) RemovePublicIPFromServer(_ context.Context, serverID, publicIPID string) error { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextRemovePublicIP); err != nil { + return err + } + if serverID == "" || publicIPID == "" { + return fmt.Errorf("server ID and public IP ID are required: %w", cloud.ErrInvalidInput) + } + entry, ok := c.publicIPs[publicIPID] + if !ok { + return fmt.Errorf("public IP %q: %w", publicIPID, cloud.ErrNotFound) + } + entry.publicIP.NetworkInterfaceID = "" + return nil +} + func (c *Client) ListSecurityGroupsByTags(_ context.Context, tags map[string]string) ([]*cloud.SecurityGroup, error) { c.mu.Lock() defer c.mu.Unlock() @@ -684,16 +803,6 @@ func containsString(values []string, needle string) bool { return slices.Contains(values, needle) } -func (c *Client) publicIPByTags(tags map[string]string) *cloud.PublicIP { - for _, entry := range c.publicIPs { - if mapContains(entry.tags, tags) { - out := *entry.publicIP - return &out - } - } - return nil -} - func (c *Client) securityGroupByTags(tags map[string]string) *cloud.SecurityGroup { for _, entry := range c.securityGroups { if mapContains(entry.tags, tags) { @@ -704,20 +813,6 @@ func (c *Client) securityGroupByTags(tags map[string]string) *cloud.SecurityGrou return nil } -func idOfPublicIP(publicIP *cloud.PublicIP) string { - if publicIP == nil { - return "" - } - return publicIP.ID -} - -func ipOfPublicIP(publicIP *cloud.PublicIP) string { - if publicIP == nil { - return "" - } - return publicIP.IP -} - func idOfSecurityGroup(securityGroup *cloud.SecurityGroup) string { if securityGroup == nil { return "" @@ -745,3 +840,148 @@ func cloneServer(s *cloud.Server) *cloud.Server { } return &out } + +type roundTripperFunc func(*http.Request) (*http.Response, error) + +func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { + return f(r) +} + +func (c *Client) roundTripIaaS(r *http.Request) (*http.Response, error) { + rec := httptest.NewRecorder() + rec.Header().Set("Content-Type", "application/json") + path := r.URL.Path + + switch { + case strings.Contains(path, "/servers/") && strings.Contains(path, "/public-ips/"): + parts := strings.Split(path, "/servers/") + sub := strings.Split(parts[1], "/public-ips/") + serverID, publicIPID := sub[0], sub[1] + var err error + switch r.Method { + case http.MethodPut: + err = c.AddPublicIPToServer(r.Context(), serverID, publicIPID) + case http.MethodDelete: + err = c.RemovePublicIPFromServer(r.Context(), serverID, publicIPID) + default: + err = cloud.ErrInvalidInput + } + if err != nil { + writeFakeError(rec, err) + } else { + rec.WriteHeader(http.StatusNoContent) + } + + case r.Method == http.MethodPost && strings.HasSuffix(path, "/public-ips"): + var body struct { + Labels map[string]string `json:"labels"` + } + if r.Body != nil { + _ = json.NewDecoder(r.Body).Decode(&body) + } + created, err := c.CreatePublicIP(r.Context(), body.Labels) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(fakePublicIPMap(created, body.Labels)) + } + + case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips"): + tags := parseLabelSelector(r.URL.Query().Get("label_selector")) + items, err := c.listPublicIPItems(tags) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(map[string]any{"items": items}) + } + + case r.Method == http.MethodGet && strings.Contains(path, "/public-ips/"): + id := path[strings.LastIndex(path, "/public-ips/")+len("/public-ips/"):] + got, err := c.GetPublicIP(r.Context(), id) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(fakePublicIPMap(got, nil)) + } + + case r.Method == http.MethodDelete && strings.Contains(path, "/public-ips/"): + id := path[strings.LastIndex(path, "/public-ips/")+len("/public-ips/"):] + if err := c.DeletePublicIP(r.Context(), id); err != nil { + writeFakeError(rec, err) + } else { + rec.WriteHeader(http.StatusNoContent) + } + + default: + rec.WriteHeader(http.StatusNotFound) + _ = json.NewEncoder(rec).Encode(map[string]any{"message": "not found"}) + } + + return rec.Result(), nil +} + +func (c *Client) listPublicIPItems(tags map[string]string) ([]map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextListPublicIPs); err != nil { + return nil, err + } + if len(tags) == 0 { + return nil, fmt.Errorf("empty tags: %w", cloud.ErrInvalidInput) + } + items := make([]map[string]any, 0, len(c.publicIPs)) + for _, entry := range c.publicIPs { + if mapContains(entry.tags, tags) { + items = append(items, fakePublicIPMap(entry.publicIP, entry.tags)) + } + } + return items, nil +} + +func fakePublicIPMap(p *cloud.PublicIP, tags map[string]string) map[string]any { + m := map[string]any{ + "id": p.ID, + "ip": p.IP, + } + if p.NetworkInterfaceID != "" { + m["networkInterface"] = p.NetworkInterfaceID + } + if len(tags) > 0 { + labels := make(map[string]any, len(tags)) + for k, v := range tags { + labels[k] = v + } + m["labels"] = labels + } + return m +} + +func parseLabelSelector(selector string) map[string]string { + if selector == "" { + return nil + } + tags := make(map[string]string) + for part := range strings.SplitSeq(selector, ",") { + if k, v, ok := strings.Cut(part, "="); ok && k != "" { + tags[k] = v + } + } + return tags +} + +func writeFakeError(rec *httptest.ResponseRecorder, err error) { + status := http.StatusInternalServerError + switch { + case cloud.IsNotFound(err): + status = http.StatusNotFound + case cloud.IsConflict(err): + status = http.StatusConflict + case cloud.IsInvalidInput(err): + status = http.StatusBadRequest + case cloud.IsUnauthorized(err): + status = http.StatusUnauthorized + } + rec.WriteHeader(status) + _ = json.NewEncoder(rec).Encode(map[string]any{"message": err.Error()}) +} diff --git a/cloud/sdk_client.go b/cloud/sdk_client.go index 433e22f..531b7ec 100644 --- a/cloud/sdk_client.go +++ b/cloud/sdk_client.go @@ -14,15 +14,13 @@ import ( "cmp" "context" "encoding/base64" - "errors" "fmt" - "net/http" "os" "slices" "strings" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/stackit-sdk-go/core/config" - "github.com/stackitcloud/stackit-sdk-go/core/oapierror" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" ) @@ -83,6 +81,18 @@ func NewClient(_ context.Context, creds Credentials) (Client, error) { }, nil } +func (c *SDKClient) ProjectID() string { + return c.projectID +} + +func (c *SDKClient) Region() string { + return c.region +} + +func (c *SDKClient) IaaSClient() *iaas.APIClient { + return c.iaasClient +} + func stackitOptions(creds Credentials, endpoint string) []config.ConfigurationOption { opts := []config.ConfigurationOption{ config.WithUserAgent("cluster-api-provider-stackit"), @@ -104,7 +114,7 @@ func stackitOptions(creds Credentials, endpoint string) []config.ConfigurationOp func (c *SDKClient) GetServer(ctx context.Context, id string) (*Server, error) { server, err := c.iaasClient.DefaultAPI.GetServer(ctx, c.projectID, c.region, id).Execute() if err != nil { - return nil, classifySDKError("get server", err) + return nil, shared.ClassifySDKError("get server", err) } return c.serverFromSDK(ctx, server), nil } @@ -128,15 +138,15 @@ func (c *SDKClient) ListServersByTags(ctx context.Context, tags map[string]strin return nil, fmt.Errorf("%w: empty tag selector", ErrInvalidInput) } resp, err := c.iaasClient.DefaultAPI.ListServers(ctx, c.projectID, c.region). - LabelSelector(labelSelector(tags)). + LabelSelector(shared.LabelSelector(tags)). Details(true). Execute() if err != nil { - return nil, classifySDKError("list servers", err) + return nil, shared.ClassifySDKError("list servers", err) } matched := []*Server{} for _, server := range resp.GetItems() { - if labelsContainTags(server.GetLabels(), tags) { + if shared.LabelsContainTags(server.GetLabels(), tags) { server := server matched = append(matched, c.serverFromSDK(ctx, &server)) } @@ -159,7 +169,7 @@ func (c *SDKClient) CreateServer(ctx context.Context, input CreateServerInput) ( payload.SetImageId(input.ImageID) } if len(input.Tags) > 0 { - payload.SetLabels(tagsToSDKLabels(input.Tags)) + payload.SetLabels(shared.TagsToSDKLabels(input.Tags)) } networking := iaas.NewCreateServerNetworking() if input.NetworkID != "" { @@ -198,7 +208,7 @@ func (c *SDKClient) CreateServer(ctx context.Context, input CreateServerInput) ( CreateServerPayload(*payload). Execute() if err != nil { - return nil, classifySDKError("create server", err) + return nil, shared.ClassifySDKError("create server", err) } return c.serverFromSDK(ctx, server), nil } @@ -206,7 +216,7 @@ func (c *SDKClient) CreateServer(ctx context.Context, input CreateServerInput) ( func (c *SDKClient) ListAvailabilityZones(ctx context.Context) ([]string, error) { resp, err := c.iaasClient.DefaultAPI.ListAvailabilityZones(ctx, c.region).Execute() if err != nil { - return nil, classifySDKError("list availability zones", err) + return nil, shared.ClassifySDKError("list availability zones", err) } if resp == nil || resp.Items == nil { return nil, nil @@ -216,7 +226,7 @@ func (c *SDKClient) ListAvailabilityZones(ctx context.Context) ([]string, error) func (c *SDKClient) DeleteServer(ctx context.Context, id string) error { if err := c.iaasClient.DefaultAPI.DeleteServer(ctx, c.projectID, c.region, id).Execute(); err != nil { - return classifySDKError("delete server", err) + return shared.ClassifySDKError("delete server", err) } return nil } @@ -224,7 +234,7 @@ func (c *SDKClient) DeleteServer(ctx context.Context, id string) error { func (c *SDKClient) GetNetwork(ctx context.Context, id string) (*Network, error) { network, err := c.iaasClient.DefaultAPI.GetNetwork(ctx, c.projectID, c.region, id).Execute() if err != nil { - return nil, classifySDKError("get network", err) + return nil, shared.ClassifySDKError("get network", err) } ipv4 := network.GetIpv4() return &Network{ @@ -258,17 +268,18 @@ func (c *SDKClient) EnsureBastion(ctx context.Context, input BastionInput) (*Bas } server, err := c.CreateServer(ctx, CreateServerInput{ - Name: input.Name, - ProjectID: input.ProjectID, - Region: input.Region, - ImageID: input.ImageID, - MachineType: input.MachineType, - SSHKeyName: input.SSHKeyName, - NetworkID: input.NetworkID, - SecurityGroups: []string{securityGroup.ID}, - UserData: input.CloudInit, - Tags: input.Tags, - RootVolume: input.RootVolume, + Name: input.Name, + ProjectID: input.ProjectID, + Region: input.Region, + ImageID: input.ImageID, + MachineType: input.MachineType, + AvailabilityZone: input.AvailabilityZone, + SSHKeyName: input.SSHKeyName, + NetworkID: input.NetworkID, + SecurityGroups: []string{securityGroup.ID}, + UserData: input.CloudInit, + Tags: input.Tags, + RootVolume: input.RootVolume, }) if err != nil { return nil, err @@ -283,31 +294,10 @@ func (c *SDKClient) EnsureBastion(ctx context.Context, input BastionInput) (*Bas return nil, err } - publicIP, err := c.ensurePublicIP(ctx, input.Tags) - if err != nil { - return nil, err - } - if publicIP.NetworkInterfaceID == "" { - if err := c.iaasClient.DefaultAPI. - AddPublicIpToServer(ctx, c.projectID, c.region, server.ID, publicIP.ID). - Execute(); err != nil { - err := classifySDKError("add public IP to server", err) - if !IsConflict(err) { - return nil, err - } - } - publicIP, err = c.getPublicIP(ctx, publicIP.ID) - if err != nil { - return nil, err - } - } - return &Bastion{ ServerID: server.ID, ServerState: server.State, ServerPowerStatus: server.PowerStatus, - PublicIPID: publicIP.ID, - PublicIP: publicIP.IP, SecurityGroupID: securityGroup.ID, }, nil } @@ -323,16 +313,6 @@ func (c *SDKClient) DeleteBastion(ctx context.Context, input BastionInput, statu } } - publicIPID := status.PublicIPID - if publicIPID == "" { - publicIP, err := c.findPublicIPByTags(ctx, input.Tags) - if err == nil { - publicIPID = publicIP.ID - } else if !IsNotFound(err) { - return err - } - } - securityGroupID := status.SecurityGroupID if securityGroupID == "" { securityGroup, err := c.findSecurityGroupByTags(ctx, input.Tags) @@ -343,21 +323,11 @@ func (c *SDKClient) DeleteBastion(ctx context.Context, input BastionInput, statu } } - if serverID != "" && publicIPID != "" { - if err := c.iaasClient.DefaultAPI. - RemovePublicIpFromServer(ctx, c.projectID, c.region, serverID, publicIPID). - Execute(); err != nil { - err := classifySDKError("remove public IP from server", err) - if !IsNotFound(err) && !IsConflict(err) { - return err - } - } - } if serverID != "" && securityGroupID != "" { if err := c.iaasClient.DefaultAPI. RemoveSecurityGroupFromServer(ctx, c.projectID, c.region, serverID, securityGroupID). Execute(); err != nil { - err := classifySDKError("remove security group from server", err) + err := shared.ClassifySDKError("remove security group from server", err) if !IsNotFound(err) && !IsConflict(err) { return err } @@ -368,14 +338,6 @@ func (c *SDKClient) DeleteBastion(ctx context.Context, input BastionInput, statu return err } } - if publicIPID != "" { - if err := c.iaasClient.DefaultAPI.DeletePublicIP(ctx, c.projectID, c.region, publicIPID).Execute(); err != nil { - err := classifySDKError("delete public IP", err) - if !IsNotFound(err) { - return err - } - } - } if securityGroupID != "" { if err := c.deleteSecurityGroupRules(ctx, securityGroupID); err != nil { return err @@ -383,7 +345,7 @@ func (c *SDKClient) DeleteBastion(ctx context.Context, input BastionInput, statu if err := c.iaasClient.DefaultAPI. DeleteSecurityGroup(ctx, c.projectID, c.region, securityGroupID). Execute(); err != nil { - err := classifySDKError("delete security group", err) + err := shared.ClassifySDKError("delete security group", err) if !IsNotFound(err) { return err } @@ -432,7 +394,7 @@ func (c *SDKClient) DeleteNodeSSHAccess(ctx context.Context, tags map[string]str if err := c.iaasClient.DefaultAPI. RemoveSecurityGroupFromServer(ctx, c.projectID, c.region, server.ID, securityGroup.ID). Execute(); err != nil { - err := classifySDKError("remove security group from server", err) + err := shared.ClassifySDKError("remove security group from server", err) if !IsNotFound(err) && !IsConflict(err) { return err } @@ -444,7 +406,7 @@ func (c *SDKClient) DeleteNodeSSHAccess(ctx context.Context, tags map[string]str if err := c.iaasClient.DefaultAPI. DeleteSecurityGroup(ctx, c.projectID, c.region, securityGroup.ID). Execute(); err != nil { - err := classifySDKError("delete security group", err) + err := shared.ClassifySDKError("delete security group", err) if !IsNotFound(err) { return err } @@ -452,39 +414,19 @@ func (c *SDKClient) DeleteNodeSSHAccess(ctx context.Context, tags map[string]str return nil } -func (c *SDKClient) ListPublicIPsByTags(ctx context.Context, tags map[string]string) ([]*PublicIP, error) { - if len(tags) == 0 { - return nil, fmt.Errorf("%w: empty tag selector", ErrInvalidInput) - } - resp, err := c.iaasClient.DefaultAPI.ListPublicIPs(ctx, c.projectID, c.region). - LabelSelector(labelSelector(tags)). - Execute() - if err != nil { - return nil, classifySDKError("list public IPs", err) - } - matched := []*PublicIP{} - for _, publicIP := range resp.GetItems() { - if labelsContainTags(publicIP.GetLabels(), tags) { - publicIP := publicIP - matched = append(matched, publicIPFromSDK(&publicIP)) - } - } - return matched, nil -} - func (c *SDKClient) ListSecurityGroupsByTags(ctx context.Context, tags map[string]string) ([]*SecurityGroup, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", ErrInvalidInput) } resp, err := c.iaasClient.DefaultAPI.ListSecurityGroups(ctx, c.projectID, c.region). - LabelSelector(labelSelector(tags)). + LabelSelector(shared.LabelSelector(tags)). Execute() if err != nil { - return nil, classifySDKError("list security groups", err) + return nil, shared.ClassifySDKError("list security groups", err) } matched := []*SecurityGroup{} for _, securityGroup := range resp.GetItems() { - if labelsContainTags(securityGroup.GetLabels(), tags) { + if shared.LabelsContainTags(securityGroup.GetLabels(), tags) { securityGroup := securityGroup matched = append(matched, securityGroupFromSDK(&securityGroup)) } @@ -502,7 +444,7 @@ func (c *SDKClient) EnsureAPIServerLoadBalancer(ctx context.Context, input LoadB payload.SetName(input.Name) payload.SetRegion(input.Region) if len(input.Tags) > 0 { - payload.SetLabels(loadBalancerLabels(input.Tags)) + payload.SetLabels(shared.LoadBalancerLabels(input.Tags)) } options := lb.NewLoadBalancerOptions() options.SetEphemeralAddress(true) @@ -544,14 +486,14 @@ func (c *SDKClient) EnsureAPIServerLoadBalancer(ctx context.Context, input LoadB CreateLoadBalancerPayload(*payload). Execute() if err != nil { - return nil, classifySDKError("create load balancer", err) + return nil, shared.ClassifySDKError("create load balancer", err) } return loadBalancerFromSDK(lbResp), nil } func (c *SDKClient) DeleteAPIServerLoadBalancer(ctx context.Context, id string) error { if _, err := c.lbClient.DefaultAPI.DeleteLoadBalancer(ctx, c.projectID, c.region, id).Execute(); err != nil { - return classifySDKError("delete load balancer", err) + return shared.ClassifySDKError("delete load balancer", err) } return nil } @@ -565,16 +507,16 @@ func (c *SDKClient) ListAPIServerLoadBalancersByTags( } resp, err := c.lbClient.DefaultAPI.ListLoadBalancers(ctx, c.projectID, c.region).Execute() if err != nil { - err := classifySDKError("list load balancers", err) + err := shared.ClassifySDKError("list load balancers", err) if isLoadBalancerServiceNotEnabled(err) { return nil, nil } return nil, err } matched := []*LoadBalancer{} - labels := loadBalancerLabels(tags) + labels := shared.LoadBalancerLabels(tags) for _, candidate := range resp.GetLoadBalancers() { - if mapContains(candidate.GetLabels(), labels) { + if shared.MapContains(candidate.GetLabels(), labels) { candidate := candidate matched = append(matched, loadBalancerFromSDK(&candidate)) } @@ -608,7 +550,7 @@ func (c *SDKClient) SetAPIServerLoadBalancerTargets( loadBalancer, err := c.lbClient.DefaultAPI.GetLoadBalancer(ctx, c.projectID, c.region, loadBalancerID).Execute() if err != nil { - return classifySDKError("get load balancer", err) + return shared.ClassifySDKError("get load balancer", err) } targetPool := apiServerTargetPool(loadBalancer) if targetPool == nil { @@ -670,12 +612,12 @@ func (c *SDKClient) ensureBastionSecurityGroup(ctx context.Context, input Bastio } payload := iaas.NewCreateSecurityGroupPayload(input.Name + "-ssh") payload.SetDescription("Cluster API Provider STACKIT bastion SSH access") - payload.SetLabels(tagsToSDKLabels(input.Tags)) + payload.SetLabels(shared.TagsToSDKLabels(input.Tags)) securityGroup, err := c.iaasClient.DefaultAPI.CreateSecurityGroup(ctx, c.projectID, c.region). CreateSecurityGroupPayload(*payload). Execute() if err != nil { - return nil, classifySDKError("create security group", err) + return nil, shared.ClassifySDKError("create security group", err) } return securityGroupFromSDK(securityGroup), nil } @@ -688,12 +630,12 @@ func (c *SDKClient) ensureNodeSSHSecurityGroup(ctx context.Context, input NodeSS } payload := iaas.NewCreateSecurityGroupPayload(input.Name) payload.SetDescription("Cluster API Provider STACKIT node SSH access from bastion") - payload.SetLabels(tagsToSDKLabels(input.Tags)) + payload.SetLabels(shared.TagsToSDKLabels(input.Tags)) securityGroup, err := c.iaasClient.DefaultAPI.CreateSecurityGroup(ctx, c.projectID, c.region). CreateSecurityGroupPayload(*payload). Execute() if err != nil { - return nil, classifySDKError("create security group", err) + return nil, shared.ClassifySDKError("create security group", err) } return securityGroupFromSDK(securityGroup), nil } @@ -704,7 +646,7 @@ func (c *SDKClient) ensureBastionSecurityGroupRules(ctx context.Context, securit } resp, err := c.iaasClient.DefaultAPI.ListSecurityGroupRules(ctx, c.projectID, c.region, securityGroupID).Execute() if err != nil { - return classifySDKError("list security group rules", err) + return shared.ClassifySDKError("list security group rules", err) } existingRules := resp.GetItems() desired := make(map[string]struct{}, len(cidrs)) @@ -731,7 +673,7 @@ func (c *SDKClient) ensureBastionSecurityGroupRules(ctx context.Context, securit if _, err := c.iaasClient.DefaultAPI.CreateSecurityGroupRule(ctx, c.projectID, c.region, securityGroupID). CreateSecurityGroupRulePayload(*rule). Execute(); err != nil { - return classifySDKError("create security group rule", err) + return shared.ClassifySDKError("create security group rule", err) } } @@ -752,7 +694,7 @@ func (c *SDKClient) ensureBastionSecurityGroupRules(ctx context.Context, securit if err := c.iaasClient.DefaultAPI. DeleteSecurityGroupRule(ctx, c.projectID, c.region, securityGroupID, ruleID). Execute(); err != nil { - err := classifySDKError("delete security group rule", err) + err := shared.ClassifySDKError("delete security group rule", err) if !IsNotFound(err) { return err } @@ -770,7 +712,7 @@ func (c *SDKClient) ensureNodeSSHSecurityGroupRule( } resp, err := c.iaasClient.DefaultAPI.ListSecurityGroupRules(ctx, c.projectID, c.region, nodeSecurityGroupID).Execute() if err != nil { - return classifySDKError("list security group rules", err) + return shared.ClassifySDKError("list security group rules", err) } if hasRemoteSecurityGroupSSHRule(resp.GetItems(), bastionSecurityGroupID) { return nil @@ -783,50 +725,11 @@ func (c *SDKClient) ensureNodeSSHSecurityGroupRule( if _, err := c.iaasClient.DefaultAPI.CreateSecurityGroupRule(ctx, c.projectID, c.region, nodeSecurityGroupID). CreateSecurityGroupRulePayload(*rule). Execute(); err != nil { - return classifySDKError("create security group rule", err) + return shared.ClassifySDKError("create security group rule", err) } return nil } -func (c *SDKClient) ensurePublicIP(ctx context.Context, tags map[string]string) (*PublicIP, error) { - if existing, err := c.findPublicIPByTags(ctx, tags); err == nil { - return existing, nil - } else if !IsNotFound(err) { - return nil, err - } - payload := iaas.NewCreatePublicIPPayload() - payload.SetLabels(tagsToSDKLabels(tags)) - publicIP, err := c.iaasClient.DefaultAPI.CreatePublicIP(ctx, c.projectID, c.region). - CreatePublicIPPayload(*payload). - Execute() - if err != nil { - return nil, classifySDKError("create public IP", err) - } - return publicIPFromSDK(publicIP), nil -} - -func (c *SDKClient) getPublicIP(ctx context.Context, id string) (*PublicIP, error) { - publicIP, err := c.iaasClient.DefaultAPI.GetPublicIP(ctx, c.projectID, c.region, id).Execute() - if err != nil { - return nil, classifySDKError("get public IP", err) - } - return publicIPFromSDK(publicIP), nil -} - -func (c *SDKClient) findPublicIPByTags(ctx context.Context, tags map[string]string) (*PublicIP, error) { - matched, err := c.ListPublicIPsByTags(ctx, tags) - if err != nil { - return nil, err - } - if len(matched) == 0 { - return nil, fmt.Errorf("%w: no public IP matching tags", ErrNotFound) - } - if len(matched) > 1 { - return nil, fmt.Errorf("%w: multiple public IPs matching tags", ErrConflict) - } - return matched[0], nil -} - func (c *SDKClient) findSecurityGroupByTags(ctx context.Context, tags map[string]string) (*SecurityGroup, error) { matched, err := c.ListSecurityGroupsByTags(ctx, tags) if err != nil { @@ -851,7 +754,7 @@ func (c *SDKClient) addSecurityGroupToServer(ctx context.Context, serverID, secu if err := c.iaasClient.DefaultAPI. AddSecurityGroupToServer(ctx, c.projectID, c.region, serverID, securityGroupID). Execute(); err != nil { - err := classifySDKError("add security group to server", err) + err := shared.ClassifySDKError("add security group to server", err) if !IsConflict(err) && !IsInvalidInput(err) && !IsNotFound(err) { return err } @@ -862,7 +765,7 @@ func (c *SDKClient) addSecurityGroupToServer(ctx context.Context, serverID, secu func (c *SDKClient) deleteSecurityGroupRules(ctx context.Context, securityGroupID string) error { resp, err := c.iaasClient.DefaultAPI.ListSecurityGroupRules(ctx, c.projectID, c.region, securityGroupID).Execute() if err != nil { - err := classifySDKError("list security group rules", err) + err := shared.ClassifySDKError("list security group rules", err) if IsNotFound(err) { return nil } @@ -876,7 +779,7 @@ func (c *SDKClient) deleteSecurityGroupRules(ctx context.Context, securityGroupI if err := c.iaasClient.DefaultAPI. DeleteSecurityGroupRule(ctx, c.projectID, c.region, securityGroupID, ruleID). Execute(); err != nil { - err := classifySDKError("delete security group rule", err) + err := shared.ClassifySDKError("delete security group rule", err) if !IsNotFound(err) { return err } @@ -943,7 +846,7 @@ func (c *SDKClient) updateAPIServerTargetPool( UpdateTargetPool(ctx, c.projectID, c.region, loadBalancerID, apiserverTargetPoolName). UpdateTargetPoolPayload(*payload). Execute(); err != nil { - return classifySDKError("update load balancer target pool", err) + return shared.ClassifySDKError("update load balancer target pool", err) } return nil } @@ -992,22 +895,11 @@ func loadBalancerFromSDK(in *lb.LoadBalancer) *LoadBalancer { return &LoadBalancer{ ID: in.GetName(), Name: in.GetName(), - IP: firstNonEmpty(in.GetExternalAddress(), in.GetPrivateAddress()), + IP: shared.FirstNonEmpty(in.GetExternalAddress(), in.GetPrivateAddress()), Port: listenerPort(in.GetListeners()), } } -func publicIPFromSDK(in *iaas.PublicIp) *PublicIP { - if in == nil { - return nil - } - return &PublicIP{ - ID: in.GetId(), - IP: in.GetIp(), - NetworkInterfaceID: in.GetNetworkInterface(), - } -} - func securityGroupFromSDK(in *iaas.SecurityGroup) *SecurityGroup { if in == nil { return nil @@ -1027,32 +919,6 @@ func listenerPort(listeners []lb.Listener) int32 { return 0 } -func labelSelector(tags map[string]string) string { - parts := make([]string, 0, len(tags)) - for k, v := range tags { - parts = append(parts, k+"="+v) - } - return strings.Join(parts, ",") -} - -func labelsContainTags(labels map[string]any, tags map[string]string) bool { - for k, want := range tags { - got, ok := labels[k] - if !ok || fmt.Sprint(got) != want { - return false - } - } - return true -} - -func tagsToSDKLabels(tags map[string]string) map[string]any { - out := make(map[string]any, len(tags)) - for k, v := range tags { - out[k] = v - } - return out -} - func clusterServerSelectorFromTags(tags map[string]string) map[string]string { selector := map[string]string{} for _, key := range []string{ @@ -1067,55 +933,6 @@ func clusterServerSelectorFromTags(tags map[string]string) map[string]string { return selector } -func mapContains(haystack, needle map[string]string) bool { - for k, v := range needle { - if haystack[k] != v { - return false - } - } - return true -} - -func loadBalancerLabels(tags map[string]string) map[string]string { - out := make(map[string]string, len(tags)) - for k, v := range tags { - out[strings.ReplaceAll(k, "/", ".")] = v - } - return out -} - -func firstNonEmpty(values ...string) string { - for _, value := range values { - if value != "" { - return value - } - } - return "" -} - -func classifySDKError(op string, err error) error { - var oapiErr *oapierror.GenericOpenAPIError - if errors.As(err, &oapiErr) { - switch oapiErr.StatusCode { - case http.StatusNotFound: - return fmt.Errorf("%w: %s: %v", ErrNotFound, op, err) - case http.StatusUnauthorized, http.StatusForbidden: - return fmt.Errorf("%w: %s: %v", ErrUnauthorized, op, err) - case http.StatusConflict: - return fmt.Errorf("%w: %s: %v", ErrConflict, op, err) - case http.StatusBadRequest, http.StatusUnprocessableEntity: - return fmt.Errorf("%w: %s: %v", ErrInvalidInput, op, err) - case http.StatusTooManyRequests: - return fmt.Errorf("%w: %s: %v", ErrTransient, op, err) - default: - if oapiErr.StatusCode >= 500 { - return fmt.Errorf("%w: %s: %v", ErrTransient, op, err) - } - } - } - return fmt.Errorf("%w: %s: %v", ErrTransient, op, err) -} - func isLoadBalancerServiceNotEnabled(err error) bool { return IsUnauthorized(err) && strings.Contains(err.Error(), "Service not enabled") } diff --git a/cloud/sdk_client_test.go b/cloud/sdk_client_test.go index 34a3e9b..3e235b7 100644 --- a/cloud/sdk_client_test.go +++ b/cloud/sdk_client_test.go @@ -469,9 +469,8 @@ func lookup(m map[string]any, key string) any { // short-circuits on an existing server, making this the only re-attach path. func TestSDKClientEnsureBastionToleratesDuplicateSecurityGroupAttach(t *testing.T) { var ( - createPayload map[string]any - attachCallCount int - publicIPAssigned bool + createPayload map[string]any + attachCallCount int ) server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path @@ -502,13 +501,6 @@ func TestSDKClientEnsureBastionToleratesDuplicateSecurityGroupAttach(t *testing. "code": 400, "msg": "request invalid: Invalid input for security_groups. Reason: Duplicate items in the list.", }) - case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips"): - writeJSON(t, w, map[string]any{"items": []any{}}) - case r.Method == http.MethodPost && strings.HasSuffix(path, "/public-ips"): - writeJSON(t, w, map[string]any{"id": "66666666-6666-4666-8666-666666666666", "ip": "203.0.113.10"}) - case strings.Contains(path, "/public-ips/"): - publicIPAssigned = true - writeJSON(t, w, map[string]any{"id": "66666666-6666-4666-8666-666666666666", "ip": "203.0.113.10"}) case r.Method == http.MethodGet && strings.Contains(path, "/servers/"): writeJSON(t, w, map[string]any{ "id": testSDKServerID, "name": "bastion", "status": "ACTIVE", "machineType": "c2i.1", @@ -520,15 +512,16 @@ func TestSDKClientEnsureBastionToleratesDuplicateSecurityGroupAttach(t *testing. client := newTestSDKClient(t, server.URL) bastion, err := client.EnsureBastion(context.Background(), BastionInput{ - Name: "bastion", - ProjectID: testSDKProjectID, - Region: testSDKRegion, - NetworkID: testSDKNetworkID, - ImageID: testSDKImageID, - MachineType: "c2i.1", - SSHKeyName: "default", - AllowedCIDRs: []string{"203.0.113.0/24"}, - Tags: map[string]string{"cluster": "test"}, + Name: "bastion", + ProjectID: testSDKProjectID, + Region: testSDKRegion, + NetworkID: testSDKNetworkID, + ImageID: testSDKImageID, + MachineType: "c2i.1", + AvailabilityZone: "eu01-1", + SSHKeyName: "default", + AllowedCIDRs: []string{"203.0.113.0/24"}, + Tags: map[string]string{"cluster": "test"}, }) if err != nil { t.Fatalf("EnsureBastion() error = %v", err) @@ -537,15 +530,13 @@ func TestSDKClientEnsureBastionToleratesDuplicateSecurityGroupAttach(t *testing. if attachCallCount != 1 { t.Fatalf("attach attempted %d time(s), want exactly 1 (the idempotent re-attach)", attachCallCount) } + assertStringField(t, createPayload, "availabilityZone", "eu01-1") groups, _ := createPayload["securityGroups"].([]any) if len(groups) != 1 || groups[0] != testSDKSecurityGroup { t.Fatalf("create payload securityGroups = %v, want [%s]", createPayload["securityGroups"], testSDKSecurityGroup) } - if !publicIPAssigned { - t.Fatal("public IP was never assigned to the bastion server") - } - if bastion.PublicIP != "203.0.113.10" { - t.Fatalf("bastion.PublicIP = %q, want 203.0.113.10", bastion.PublicIP) + if bastion.ServerID != testSDKServerID { + t.Fatalf("bastion.ServerID = %q, want %s", bastion.ServerID, testSDKServerID) } } diff --git a/cloud/services/bastion/bastion.go b/cloud/services/bastion/bastion.go index 6b7f5d9..80eab63 100644 --- a/cloud/services/bastion/bastion.go +++ b/cloud/services/bastion/bastion.go @@ -13,6 +13,7 @@ package bastion import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -26,15 +27,16 @@ func Input(stackitCluster *infrav1.StackitCluster, cloudInit []byte) cloud.Basti tags[util.LabelResourceRole] = util.ResourceRoleBastion return cloud.BastionInput{ - Name: stackitCluster.Name + "-bastion", - ProjectID: stackitCluster.Spec.ProjectID, - Region: stackitCluster.Spec.Region, - NetworkID: stackitCluster.Spec.Network.ID, - ImageID: stackitCluster.Spec.Bastion.ImageID, - MachineType: stackitCluster.Spec.Bastion.MachineType, - SSHKeyName: stackitCluster.Spec.Bastion.SSHKeyName, - AllowedCIDRs: stackitCluster.Spec.Bastion.AllowedCIDRs, - Tags: tags, + Name: stackitCluster.Name + "-bastion", + ProjectID: stackitCluster.Spec.ProjectID, + Region: stackitCluster.Spec.Region, + NetworkID: stackitCluster.Spec.Network.ID, + ImageID: stackitCluster.Spec.Bastion.ImageID, + MachineType: stackitCluster.Spec.Bastion.MachineType, + AvailabilityZone: scope.DefaultAvailabilityZone(stackitCluster), + SSHKeyName: stackitCluster.Spec.Bastion.SSHKeyName, + AllowedCIDRs: stackitCluster.Spec.Bastion.AllowedCIDRs, + Tags: tags, RootVolume: cloud.RootVolumeInput{ SizeGiB: stackitCluster.Spec.Bastion.RootVolume.SizeGiB, PerformanceClass: stackitCluster.Spec.Bastion.RootVolume.PerformanceClass, diff --git a/cloud/services/bastion/bastion_test.go b/cloud/services/bastion/bastion_test.go new file mode 100644 index 0000000..88722a1 --- /dev/null +++ b/cloud/services/bastion/bastion_test.go @@ -0,0 +1,68 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package bastion + +import ( + "testing" + + clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" + + infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" +) + +func TestInputAvailabilityZone(t *testing.T) { + tests := []struct { + name string + region string + failureDomains []clusterv1.FailureDomain + wantAZ string + }{ + { + name: "selects first failure domain sorted alphabetically", + region: "eu01", + failureDomains: []clusterv1.FailureDomain{ + {Name: "eu01-3"}, + {Name: "eu01-2"}, + {Name: "eu01-1"}, + }, + wantAZ: "eu01-1", + }, + { + name: "falls back to region-1 when failureDomains is empty", + region: "eu02", + failureDomains: nil, + wantAZ: "eu02-1", + }, + { + name: "returns empty when region and failureDomains are empty", + region: "", + failureDomains: nil, + wantAZ: "", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + cluster := &infrav1.StackitCluster{ + Spec: infrav1.StackitClusterSpec{ + Region: test.region, + }, + Status: infrav1.StackitClusterStatus{ + FailureDomains: test.failureDomains, + }, + } + input := Input(cluster, nil) + if input.AvailabilityZone != test.wantAZ { + t.Fatalf("Input().AvailabilityZone = %q, want %q", input.AvailabilityZone, test.wantAZ) + } + }) + } +} diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go new file mode 100644 index 0000000..449ee75 --- /dev/null +++ b/cloud/services/publicip/reconcile.go @@ -0,0 +1,246 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package publicip + +import ( + "context" + "fmt" + + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + logf "sigs.k8s.io/controller-runtime/pkg/log" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +func (s *Service) Reconcile(ctx context.Context) error { + log := logf.FromContext(ctx) + if !s.scope.BastionSpec().Enabled { + return s.Delete(ctx) + } + + log.V(1).Info("Reconciling public IP resources") + publicIP, err := s.createOrGetPublicIP(ctx) + if err != nil { + return err + } + + serverID := s.scope.BastionStatus().ServerID + if serverID != "" && publicIP.NetworkInterfaceID == "" { + log.V(1).Info("Attaching public IP to server", "publicIPID", publicIP.ID, "serverID", serverID) + if err := s.iaasClient.DefaultAPI. + AddPublicIpToServer(ctx, s.scope.ProjectID(), s.scope.Region(), serverID, publicIP.ID). + Execute(); err != nil { + err := shared.ClassifySDKError("add public IP to server", err) + if !shared.IsConflict(err) { + log.Error(err, "Failed to attach public IP to server", "publicIPID", publicIP.ID, "serverID", serverID) + return err + } + } + publicIP, err = s.getPublicIP(ctx, publicIP.ID) + if err != nil { + log.Error(err, "Failed to get public IP after attachment", "publicIPID", publicIP.ID) + return err + } + } + + s.scope.SetBastionPublicIP(publicIP.ID, publicIP.IP) + return nil +} + +func (s *Service) Delete(ctx context.Context) error { + log := logf.FromContext(ctx) + log.V(1).Info("Deleting public IP resources") + + publicIP, err := s.findPublicIP(ctx) + if err != nil { + return err + } + if publicIP == nil { + s.scope.SetBastionPublicIP("", "") + return nil + } + + serverID, err := s.resolveServerID(ctx) + if err != nil { + return err + } + if serverID != "" { + log.V(1).Info("Detaching public IP from server", "publicIPID", publicIP.ID, "serverID", serverID) + if err := DetachFromServer(ctx, s.iaasClient, s.scope.ProjectID(), s.scope.Region(), serverID, publicIP.ID); err != nil { + log.Error(err, "Failed to detach public IP from server", "publicIPID", publicIP.ID, "serverID", serverID) + return err + } + } + + log.V(1).Info("Deleting public IP", "publicIPID", publicIP.ID) + if err := DeleteByID(ctx, s.iaasClient, s.scope.ProjectID(), s.scope.Region(), publicIP.ID); err != nil { + log.Error(err, "Failed to delete public IP", "publicIPID", publicIP.ID) + return err + } + + s.scope.SetBastionPublicIP("", "") + return nil +} + +func (s *Service) createOrGetPublicIP(ctx context.Context) (*PublicIP, error) { + log := logf.FromContext(ctx) + + publicIP, err := s.findPublicIP(ctx) + if err != nil { + return nil, err + } + if publicIP != nil { + return publicIP, nil + } + + log.V(1).Info("Creating public IP") + payload := iaas.NewCreatePublicIPPayload() + if tags := s.scope.BastionTags(); len(tags) > 0 { + payload.SetLabels(shared.TagsToSDKLabels(tags)) + } + created, err := s.iaasClient.DefaultAPI. + CreatePublicIP(ctx, s.scope.ProjectID(), s.scope.Region()). + CreatePublicIPPayload(*payload). + Execute() + if err != nil { + err := shared.ClassifySDKError("create public IP", err) + log.Error(err, "Failed to create public IP") + return nil, err + } + return publicIPFromSDK(created), nil +} + +func (s *Service) findPublicIP(ctx context.Context) (*PublicIP, error) { + log := logf.FromContext(ctx) + if id := s.scope.BastionStatus().PublicIPID; id != "" { + publicIP, err := s.getPublicIP(ctx, id) + if err == nil { + return publicIP, nil + } + if !shared.IsNotFound(err) { + log.Error(err, "Failed to get public IP", "publicIPID", id) + return nil, err + } + } + + matched, err := ListByTags(ctx, s.iaasClient, s.scope.ProjectID(), s.scope.Region(), s.scope.BastionTags()) + if err != nil { + log.Error(err, "Failed to list public IPs by tags") + return nil, err + } + if len(matched) == 0 { + return nil, nil + } + if len(matched) > 1 { + return nil, fmt.Errorf("%w: multiple public IPs matching tags", shared.ErrConflict) + } + return matched[0], nil +} + +func (s *Service) getPublicIP(ctx context.Context, id string) (*PublicIP, error) { + if id == "" { + return nil, fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) + } + publicIP, err := s.iaasClient.DefaultAPI.GetPublicIP(ctx, s.scope.ProjectID(), s.scope.Region(), id).Execute() + if err != nil { + return nil, shared.ClassifySDKError("get public IP", err) + } + return publicIPFromSDK(publicIP), nil +} + +func (s *Service) resolveServerID(ctx context.Context) (string, error) { + if id := s.scope.BastionStatus().ServerID; id != "" { + return id, nil + } + server, err := s.client.FindServerByTags(ctx, s.scope.BastionTags()) + if err != nil { + if shared.IsNotFound(err) { + return "", nil + } + return "", err + } + return server.ID, nil +} + +// ListByTags lists STACKIT public IPs matching the given tag selector. +func ListByTags( + ctx context.Context, + iaasClient *iaas.APIClient, + projectID, region string, + tags map[string]string, +) ([]*PublicIP, error) { + if len(tags) == 0 { + return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) + } + resp, err := iaasClient.DefaultAPI.ListPublicIPs(ctx, projectID, region). + LabelSelector(shared.LabelSelector(tags)). + Execute() + if err != nil { + return nil, shared.ClassifySDKError("list public IPs", err) + } + matched := []*PublicIP{} + for _, publicIP := range resp.GetItems() { + if shared.LabelsContainTags(publicIP.GetLabels(), tags) { + publicIP := publicIP + matched = append(matched, publicIPFromSDK(&publicIP)) + } + } + return matched, nil +} + +// DetachFromServer detaches a public IP from a server, ignoring NotFound and Conflict errors. +func DetachFromServer( + ctx context.Context, + iaasClient *iaas.APIClient, + projectID, region, serverID, publicIPID string, +) error { + if serverID == "" || publicIPID == "" { + return fmt.Errorf("%w: server ID and public IP ID are required", shared.ErrInvalidInput) + } + if err := iaasClient.DefaultAPI. + RemovePublicIpFromServer(ctx, projectID, region, serverID, publicIPID). + Execute(); err != nil { + err := shared.ClassifySDKError("remove public IP from server", err) + if !shared.IsNotFound(err) && !shared.IsConflict(err) { + return err + } + } + return nil +} + +// DeleteByID deletes a public IP by ID, ignoring NotFound errors. +func DeleteByID( + ctx context.Context, + iaasClient *iaas.APIClient, + projectID, region, id string, +) error { + if id == "" { + return fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) + } + if err := iaasClient.DefaultAPI.DeletePublicIP(ctx, projectID, region, id).Execute(); err != nil { + err := shared.ClassifySDKError("delete public IP", err) + if !shared.IsNotFound(err) { + return err + } + } + return nil +} + +func publicIPFromSDK(in *iaas.PublicIp) *PublicIP { + if in == nil { + return nil + } + return &PublicIP{ + ID: in.GetId(), + IP: in.GetIp(), + NetworkInterfaceID: in.GetNetworkInterface(), + } +} diff --git a/cloud/services/publicip/reconcile_test.go b/cloud/services/publicip/reconcile_test.go new file mode 100644 index 0000000..b162e5d --- /dev/null +++ b/cloud/services/publicip/reconcile_test.go @@ -0,0 +1,360 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package publicip_test + +import ( + "context" + "errors" + "testing" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes/scheme" + clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + + infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" + "github.com/stackitcloud/cluster-api-provider-stackit/scope" +) + +func init() { + _ = clusterv1.AddToScheme(scheme.Scheme) + _ = infrav1.AddToScheme(scheme.Scheme) +} + +func newTestClusterScope(t *testing.T, bastionEnabled bool, cloudClient cloud.Client) *scope.ClusterScope { + t.Helper() + + cluster := &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cluster", + Namespace: "default", + }, + } + stackitCluster := &infrav1.StackitCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cluster", + Namespace: "default", + }, + Spec: infrav1.StackitClusterSpec{ + ProjectID: "00000000-0000-0000-0000-000000000000", + Region: "eu01", + Bastion: infrav1.StackitBastionSpec{ + Enabled: bastionEnabled, + }, + }, + } + + k8sClient := fake.NewClientBuilder(). + WithScheme(scheme.Scheme). + WithObjects(cluster, stackitCluster). + Build() + + clusterScope, err := scope.NewClusterScope(k8sClient, cluster, stackitCluster) + if err != nil { + t.Fatalf("NewClusterScope() error = %v", err) + } + clusterScope.SetCloudClient(cloudClient) + return clusterScope +} + +func TestServiceReconcile(t *testing.T) { + t.Run("deletes existing public IP when bastion is disabled", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, false, fakeCloud) + + created, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) + if err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + clusterScope.SetBastionPublicIP(created.ID, created.IP) + + svc := publicip.New(clusterScope) + if err := svc.Reconcile(ctx); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + + if fakeCloud.PublicIPCount() != 0 { + t.Fatalf("PublicIPCount() = %d, want 0", fakeCloud.PublicIPCount()) + } + if clusterScope.BastionStatus().PublicIPID != "" { + t.Fatalf("BastionStatus().PublicIPID = %q, want empty", clusterScope.BastionStatus().PublicIPID) + } + }) + + t.Run("creates public IP and attaches it to the bastion server", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + Name: "test-cluster-bastion", + Tags: clusterScope.BastionTags(), + }) + if err != nil { + t.Fatalf("CreateServer() error = %v", err) + } + clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID + + svc := publicip.New(clusterScope) + if err := svc.Reconcile(ctx); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + + if fakeCloud.PublicIPCount() != 1 { + t.Fatalf("PublicIPCount() = %d, want 1", fakeCloud.PublicIPCount()) + } + if clusterScope.BastionStatus().PublicIPID == "" { + t.Fatal("BastionStatus().PublicIPID is empty, want set") + } + if clusterScope.StackitCluster.Status.Bastion.PublicIP != "203.0.113.22" { + t.Fatalf("Bastion.PublicIP = %q, want 203.0.113.22", clusterScope.StackitCluster.Status.Bastion.PublicIP) + } + + gotIP, err := fakeCloud.GetPublicIP(ctx, clusterScope.BastionStatus().PublicIPID) + if err != nil { + t.Fatalf("GetPublicIP() error = %v", err) + } + if gotIP.NetworkInterfaceID == "" { + t.Fatal("PublicIP NetworkInterfaceID is empty, want attached to server") + } + }) + + t.Run("reuses existing public IP found by status ID without listing by tags", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + Name: "test-cluster-bastion", + Tags: clusterScope.BastionTags(), + }) + if err != nil { + t.Fatalf("CreateServer() error = %v", err) + } + clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID + + existing, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) + if err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + clusterScope.SetBastionPublicIP(existing.ID, existing.IP) + fakeCloud.FailNextListPublicIPs = shared.ErrTransient + + svc := publicip.New(clusterScope) + if err := svc.Reconcile(ctx); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + + if fakeCloud.PublicIPCount() != 1 { + t.Fatalf("PublicIPCount() = %d, want 1", fakeCloud.PublicIPCount()) + } + if clusterScope.BastionStatus().PublicIPID != existing.ID { + t.Fatalf("BastionStatus().PublicIPID = %q, want %q", clusterScope.BastionStatus().PublicIPID, existing.ID) + } + }) + + t.Run("reuses existing public IP found by tags without creating duplicate", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + Name: "test-cluster-bastion", + Tags: clusterScope.BastionTags(), + }) + if err != nil { + t.Fatalf("CreateServer() error = %v", err) + } + clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID + + existing, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) + if err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + + svc := publicip.New(clusterScope) + if err := svc.Reconcile(ctx); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + + if fakeCloud.PublicIPCount() != 1 { + t.Fatalf("PublicIPCount() = %d, want 1", fakeCloud.PublicIPCount()) + } + if clusterScope.BastionStatus().PublicIPID != existing.ID { + t.Fatalf("BastionStatus().PublicIPID = %q, want %q", clusterScope.BastionStatus().PublicIPID, existing.ID) + } + }) + + t.Run("tolerates conflict when attaching public IP to server", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + Name: "test-cluster-bastion", + Tags: clusterScope.BastionTags(), + }) + if err != nil { + t.Fatalf("CreateServer() error = %v", err) + } + clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID + fakeCloud.FailNextAddPublicIP = shared.ErrConflict + + svc := publicip.New(clusterScope) + if err := svc.Reconcile(ctx); err != nil { + t.Fatalf("Reconcile() error = %v, want nil on conflict", err) + } + if clusterScope.BastionStatus().PublicIPID == "" { + t.Fatal("BastionStatus().PublicIPID is empty, want set") + } + }) + + t.Run("returns conflict error when multiple public IPs match tags", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + if _, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()); err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + if _, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()); err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + + svc := publicip.New(clusterScope) + err := svc.Reconcile(ctx) + if !errors.Is(err, shared.ErrConflict) { + t.Fatalf("Reconcile() error = %v, want ErrConflict", err) + } + }) + + t.Run("returns error when creating public IP fails", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + fakeCloud.FailNextCreatePublicIP = shared.ErrTransient + + svc := publicip.New(clusterScope) + err := svc.Reconcile(ctx) + if !errors.Is(err, shared.ErrTransient) { + t.Fatalf("Reconcile() error = %v, want ErrTransient", err) + } + }) +} + +func TestServiceDelete(t *testing.T) { + t.Run("detaches and deletes public IP using recorded status ID", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + Name: "test-cluster-bastion", + Tags: clusterScope.BastionTags(), + }) + if err != nil { + t.Fatalf("CreateServer() error = %v", err) + } + clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID + + created, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) + if err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + if err := fakeCloud.AddPublicIPToServer(ctx, server.ID, created.ID); err != nil { + t.Fatalf("AddPublicIPToServer() error = %v", err) + } + clusterScope.SetBastionPublicIP(created.ID, created.IP) + + svc := publicip.New(clusterScope) + if err := svc.Delete(ctx); err != nil { + t.Fatalf("Delete() error = %v", err) + } + + if fakeCloud.PublicIPCount() != 0 { + t.Fatalf("PublicIPCount() = %d, want 0", fakeCloud.PublicIPCount()) + } + if clusterScope.BastionStatus().PublicIPID != "" { + t.Fatalf("BastionStatus().PublicIPID = %q, want empty", clusterScope.BastionStatus().PublicIPID) + } + if clusterScope.StackitCluster.Status.Bastion.PublicIP != "" { + t.Fatalf("Bastion.PublicIP = %q, want empty", clusterScope.StackitCluster.Status.Bastion.PublicIP) + } + }) + + t.Run("falls back to tag lookup for both public IP and server when status IDs are empty", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + Name: "test-cluster-bastion", + Tags: clusterScope.BastionTags(), + }) + if err != nil { + t.Fatalf("CreateServer() error = %v", err) + } + created, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) + if err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + if err := fakeCloud.AddPublicIPToServer(ctx, server.ID, created.ID); err != nil { + t.Fatalf("AddPublicIPToServer() error = %v", err) + } + + svc := publicip.New(clusterScope) + if err := svc.Delete(ctx); err != nil { + t.Fatalf("Delete() error = %v", err) + } + + if fakeCloud.PublicIPCount() != 0 { + t.Fatalf("PublicIPCount() = %d, want 0", fakeCloud.PublicIPCount()) + } + }) + + t.Run("succeeds when no public IP exists", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + svc := publicip.New(clusterScope) + if err := svc.Delete(ctx); err != nil { + t.Fatalf("Delete() error = %v, want nil", err) + } + }) + + t.Run("returns error when DeletePublicIP fails with non-NotFound error", func(t *testing.T) { + ctx := context.Background() + fakeCloud := cloudfake.New() + clusterScope := newTestClusterScope(t, true, fakeCloud) + + created, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) + if err != nil { + t.Fatalf("CreatePublicIP() error = %v", err) + } + clusterScope.SetBastionPublicIP(created.ID, created.IP) + fakeCloud.FailNextDeletePublicIP = shared.ErrTransient + + svc := publicip.New(clusterScope) + err = svc.Delete(ctx) + if !errors.Is(err, shared.ErrTransient) { + t.Fatalf("Delete() error = %v, want ErrTransient", err) + } + if clusterScope.BastionStatus().PublicIPID != created.ID { + t.Fatalf("BastionStatus().PublicIPID = %q, want preserved %q on error", clusterScope.BastionStatus().PublicIPID, created.ID) + } + }) +} diff --git a/cloud/services/publicip/service.go b/cloud/services/publicip/service.go new file mode 100644 index 0000000..62cc0c0 --- /dev/null +++ b/cloud/services/publicip/service.go @@ -0,0 +1,64 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package publicip + +import ( + "context" + + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + + infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +// PublicIP describes a STACKIT public IP resource. +type PublicIP struct { + ID string + IP string + NetworkInterfaceID string +} + +// Client defines the cloud API operations required by the public IP service. +type Client interface { + FindServerByTags(ctx context.Context, tags map[string]string) (*shared.Server, error) +} + +// Scope is an interface that provides cluster scope data needed for public IP reconciliation. +type Scope[C Client] interface { + scopeData + Cloud() C +} + +type scopeData interface { + ProjectID() string + Region() string + IaaSClient() *iaas.APIClient + BastionSpec() infrav1.StackitBastionSpec + BastionStatus() infrav1.StackitBastionStatus + BastionTags() map[string]string + SetBastionPublicIP(id, ip string) +} + +// Service implements public IPs reconciler. +type Service struct { + scope scopeData + client Client + iaasClient *iaas.APIClient +} + +// New returns Service from given scope. +func New[C Client](scope Scope[C]) *Service { + return &Service{ + scope: scope, + client: scope.Cloud(), + iaasClient: scope.IaaSClient(), + } +} diff --git a/cloud/services/shared/errors.go b/cloud/services/shared/errors.go new file mode 100644 index 0000000..73ecf9b --- /dev/null +++ b/cloud/services/shared/errors.go @@ -0,0 +1,44 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package shared + +import "errors" + +// Sentinel errors returned by Client implementations. Controllers use +// errors.Is to classify them into requeue / condition-update behavior. +var ( + ErrNotFound = errors.New("not found") + ErrUnauthorized = errors.New("unauthorized") + ErrInvalidInput = errors.New("invalid input") + ErrConflict = errors.New("conflict") + ErrTransient = errors.New("transient") +) + +// IsNotFound reports whether err wraps ErrNotFound. +func IsNotFound(err error) bool { return errors.Is(err, ErrNotFound) } + +// IsUnauthorized reports whether err wraps ErrUnauthorized. +func IsUnauthorized(err error) bool { return errors.Is(err, ErrUnauthorized) } + +// IsInvalidInput reports whether err wraps ErrInvalidInput. +func IsInvalidInput(err error) bool { return errors.Is(err, ErrInvalidInput) } + +// IsConflict reports whether err wraps ErrConflict. +func IsConflict(err error) bool { return errors.Is(err, ErrConflict) } + +// IsTransient reports whether err wraps ErrTransient. +func IsTransient(err error) bool { return errors.Is(err, ErrTransient) } + +// IsRetryable reports whether err should trigger a requeue (conflict or +// transient). +func IsRetryable(err error) bool { + return IsConflict(err) || IsTransient(err) +} diff --git a/cloud/errors_test.go b/cloud/services/shared/errors_test.go similarity index 98% rename from cloud/errors_test.go rename to cloud/services/shared/errors_test.go index e1b82f3..314eb25 100644 --- a/cloud/errors_test.go +++ b/cloud/services/shared/errors_test.go @@ -8,7 +8,7 @@ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 */ -package cloud +package shared import ( "fmt" diff --git a/cloud/services/shared/types.go b/cloud/services/shared/types.go new file mode 100644 index 0000000..a413a30 --- /dev/null +++ b/cloud/services/shared/types.go @@ -0,0 +1,28 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package shared + +// Server describes a STACKIT compute instance in provider-neutral terms. +type Server struct { + ID string + Name string + State string + PowerStatus string + ProviderID string + AvailabilityZone string + Addresses []Address +} + +// Address is an IP or DNS endpoint of a Server. +type Address struct { + Type string + Address string +} diff --git a/cloud/services/shared/utils.go b/cloud/services/shared/utils.go new file mode 100644 index 0000000..430d98b --- /dev/null +++ b/cloud/services/shared/utils.go @@ -0,0 +1,96 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package shared + +import ( + "errors" + "fmt" + "net/http" + "strings" + + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" +) + +func MapContains(haystack, needle map[string]string) bool { + for k, v := range needle { + if haystack[k] != v { + return false + } + } + return true +} + +// TODO: Move to LB Service +func LoadBalancerLabels(tags map[string]string) map[string]string { + out := make(map[string]string, len(tags)) + for k, v := range tags { + out[strings.ReplaceAll(k, "/", ".")] = v + } + return out +} + +func FirstNonEmpty(values ...string) string { + for _, value := range values { + if value != "" { + return value + } + } + return "" +} + +func LabelSelector(tags map[string]string) string { + parts := make([]string, 0, len(tags)) + for k, v := range tags { + parts = append(parts, k+"="+v) + } + return strings.Join(parts, ",") +} + +func LabelsContainTags(labels map[string]any, tags map[string]string) bool { + for k, want := range tags { + got, ok := labels[k] + if !ok || fmt.Sprint(got) != want { + return false + } + } + return true +} + +func TagsToSDKLabels(tags map[string]string) map[string]any { + out := make(map[string]any, len(tags)) + for k, v := range tags { + out[k] = v + } + return out +} + +func ClassifySDKError(op string, err error) error { + var oapiErr *oapierror.GenericOpenAPIError + if errors.As(err, &oapiErr) { + switch oapiErr.StatusCode { + case http.StatusNotFound: + return fmt.Errorf("%w: %s: %v", ErrNotFound, op, err) + case http.StatusUnauthorized, http.StatusForbidden: + return fmt.Errorf("%w: %s: %v", ErrUnauthorized, op, err) + case http.StatusConflict: + return fmt.Errorf("%w: %s: %v", ErrConflict, op, err) + case http.StatusBadRequest, http.StatusUnprocessableEntity: + return fmt.Errorf("%w: %s: %v", ErrInvalidInput, op, err) + case http.StatusTooManyRequests: + return fmt.Errorf("%w: %s: %v", ErrTransient, op, err) + default: + if oapiErr.StatusCode >= 500 { + return fmt.Errorf("%w: %s: %v", ErrTransient, op, err) + } + } + } + return fmt.Errorf("%w: %s: %v", ErrTransient, op, err) +} diff --git a/cloud/types.go b/cloud/types.go index 289353f..6cca29a 100644 --- a/cloud/types.go +++ b/cloud/types.go @@ -13,22 +13,16 @@ You may obtain a copy of the License at // through the Client interface in this package. package cloud +import ( + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + // Server describes a STACKIT compute instance in provider-neutral terms. -type Server struct { - ID string - Name string - State string - PowerStatus string - ProviderID string - AvailabilityZone string - Addresses []Address -} +type Server = shared.Server // Address is an IP or DNS endpoint of a Server. -type Address struct { - Type string - Address string -} +type Address = shared.Address // Network is an existing STACKIT virtual network referenced by the provider. type Network struct { @@ -47,11 +41,7 @@ type LoadBalancer struct { } // PublicIP describes a STACKIT public IP resource. -type PublicIP struct { - ID string - IP string - NetworkInterfaceID string -} +type PublicIP = publicip.PublicIP // SecurityGroup describes a STACKIT security group. type SecurityGroup struct { @@ -64,8 +54,6 @@ type Bastion struct { ServerID string ServerState string ServerPowerStatus string - PublicIPID string - PublicIP string SecurityGroupID string } @@ -87,17 +75,18 @@ type CreateServerInput struct { // BastionInput holds all parameters required to ensure a bastion host. type BastionInput struct { - Name string - ProjectID string - Region string - NetworkID string - ImageID string - MachineType string - SSHKeyName string - AllowedCIDRs []string - Tags map[string]string - RootVolume RootVolumeInput - CloudInit []byte + Name string + ProjectID string + Region string + NetworkID string + ImageID string + MachineType string + AvailabilityZone string + SSHKeyName string + AllowedCIDRs []string + Tags map[string]string + RootVolume RootVolumeInput + CloudInit []byte } // NodeSSHAccessInput holds parameters required to allow SSH from the bastion diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index b29226e..75a1891 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -13,6 +13,14 @@ rules: - get - list - watch +- apiGroups: + - "" + - events.k8s.io + resources: + - events + verbs: + - create + - patch - apiGroups: - cluster.x-k8s.io resources: @@ -22,13 +30,6 @@ rules: - get - list - watch -- apiGroups: - - events.k8s.io - resources: - - events - verbs: - - create - - patch - apiGroups: - infrastructure.cluster.x-k8s.io resources: diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index e8c3d8b..e2c1a1a 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -29,6 +29,7 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" bastionservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/bastion" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/scope" ) @@ -46,8 +47,6 @@ func (r *StackitClusterReconciler) reconcileBastion( input := bastionservice.Input(stackitCluster, nil) status := cloud.Bastion{ ServerID: stackitCluster.Status.Bastion.ServerID, - PublicIPID: stackitCluster.Status.Bastion.PublicIPID, - PublicIP: stackitCluster.Status.Bastion.PublicIP, SecurityGroupID: stackitCluster.Status.Bastion.SecurityGroupID, } @@ -57,10 +56,7 @@ func (r *StackitClusterReconciler) reconcileBastion( // the status keeps the tag-based cleanup to once per cluster. condition := meta.FindStatusCondition(stackitCluster.Status.Conditions, infrav1.ClusterBastionReadyCondition) if condition == nil || condition.Reason != bastionDisabledReason { - if err := cloudClient.DeleteNodeSSHAccess(ctx, bastionservice.NodeSSHAccessTags(stackitCluster)); err != nil { - return ctrl.Result{}, false, err - } - if err := cloudClient.DeleteBastion(ctx, input, status); err != nil { + if err := r.deleteBastionResources(ctx, cloudClient, clusterScope, input, status, false); err != nil { return ctrl.Result{}, false, err } clusterScope.ClearBastionStatus() @@ -102,10 +98,7 @@ func (r *StackitClusterReconciler) reconcileBastion( input.CloudInit = cloudInit if bastionNeedsRecreate(stackitCluster, cloudInit) { - if err := cloudClient.DeleteNodeSSHAccess(ctx, bastionservice.NodeSSHAccessTags(stackitCluster)); err != nil && !cloud.IsNotFound(err) { - return ctrl.Result{}, false, err - } - if err := cloudClient.DeleteBastion(ctx, input, status); err != nil && !cloud.IsNotFound(err) { + if err := r.deleteBastionResources(ctx, cloudClient, clusterScope, input, status, true); err != nil { return ctrl.Result{}, false, err } clusterScope.ClearBastionStatus() @@ -133,6 +126,9 @@ func (r *StackitClusterReconciler) reconcileBastion( stackitCluster, "BastionCreated", "Created bastion %s", bastion.ServerID, ) } + if err := publicip.New(clusterScope).Reconcile(ctx); err != nil { + return ctrl.Result{}, false, err + } if ready, reason, message, warn := serverStateCondition(bastion.ServerState, bastion.ServerPowerStatus); !ready { previousReason := "" if previous := meta.FindStatusCondition(stackitCluster.Status.Conditions, infrav1.ClusterBastionReadyCondition); previous != nil { @@ -145,7 +141,7 @@ func (r *StackitClusterReconciler) reconcileBastion( } return ctrl.Result{RequeueAfter: 15 * time.Second}, false, nil } - if bastion.PublicIP == "" { + if stackitCluster.Status.Bastion.PublicIP == "" { clusterScope.SetNotReady( "Provisioning", "waiting for bastion public IP address", @@ -188,6 +184,32 @@ func hasBastionStatus(status infrav1.StackitBastionStatus) bool { return status.ServerID != "" || status.PublicIPID != "" || status.PublicIP != "" || status.SecurityGroupID != "" } +func (r *StackitClusterReconciler) deleteBastionResources( + ctx context.Context, + cloudClient cloud.Client, + clusterScope *scope.ClusterScope, + input cloud.BastionInput, + status cloud.Bastion, + ignoreNotFound bool, +) error { + if err := cloudClient.DeleteNodeSSHAccess(ctx, bastionservice.NodeSSHAccessTags(clusterScope.StackitCluster)); err != nil { + if !ignoreNotFound || !cloud.IsNotFound(err) { + return err + } + } + if err := publicip.New(clusterScope).Delete(ctx); err != nil { + if !ignoreNotFound || !cloud.IsNotFound(err) { + return err + } + } + if err := cloudClient.DeleteBastion(ctx, input, status); err != nil { + if !ignoreNotFound || !cloud.IsNotFound(err) { + return err + } + } + return nil +} + func bastionNeedsRecreate(stackitCluster *infrav1.StackitCluster, cloudInit []byte) bool { if !hasBastionStatus(stackitCluster.Status.Bastion) { return false diff --git a/controller/stackitcluster_controller.go b/controller/stackitcluster_controller.go index a9e4a3c..8b35147 100644 --- a/controller/stackitcluster_controller.go +++ b/controller/stackitcluster_controller.go @@ -62,6 +62,7 @@ type StackitClusterReconciler struct { // +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch // +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch // +kubebuilder:rbac:groups=events.k8s.io,resources=events,verbs=create;patch +// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch func (r *StackitClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, err error) { log := logf.FromContext(ctx) diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index 65077b2..ccd6cf1 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -986,6 +986,25 @@ var _ = Describe("StackitCluster Controller", func() { })) }) + It("creates the bastion host in a single availability zone and not in metro (-m)", func() { + fakeCloud.AvailabilityZones = []string{"eu01-m", "eu01-3", "eu01-2"} + + got := &infrav1.StackitCluster{} + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + got.Spec.Bastion = validBastionSpec() + Expect(k8sClient.Update(ctx, got)).To(Succeed()) + + result, err := reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + Expect(result).To(Equal(reconcile.Result{})) + + Expect(fakeCloud.LastEnsureBastionInput.AvailabilityZone).To(Equal("eu01-2")) + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + server, err := fakeCloud.GetServer(ctx, got.Status.Bastion.ServerID) + Expect(err).NotTo(HaveOccurred()) + Expect(server.AvailabilityZone).To(Equal("eu01-2")) + }) + It("reports an error condition when listing availability zones fails", func() { fakeCloud.FailNextListAZ = fmt.Errorf("cloud failure: %w", cloud.ErrTransient) diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index f4c4f8e..f2c55e8 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -33,6 +33,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud" bastionservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/bastion" loadbalancerservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -66,6 +67,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS infrav1.ClusterReadyCondition, ) } + clusterScope.SetCloudClient(cloudClient) clusterScope.SetConditions( metav1.ConditionTrue, "Available", @@ -412,6 +414,7 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS ) return ctrl.Result{}, err } + clusterScope.SetCloudClient(cloudClient) loadBalancerID, err := loadbalancerservice.ResolveID(ctx, cloudClient, stackitCluster) if err != nil { log.Error(err, "Failed to resolve API server load balancer for deletion", "cluster", stackitCluster.Name) @@ -438,10 +441,15 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS "Failed to delete bastion node SSH access: %v", err) return ctrl.Result{}, err } + hadBastion := hasBastionStatus(stackitCluster.Status.Bastion) + if err := publicip.New(clusterScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { + log.Error(err, "Failed to delete bastion public IP", "cluster", stackitCluster.Name) + record.Warnf(stackitCluster, "FailedDeleteBastion", + "Failed to delete bastion public IP: %v", err) + return ctrl.Result{}, err + } if err := cloudClient.DeleteBastion(ctx, bastionservice.Input(stackitCluster, nil), cloud.Bastion{ ServerID: stackitCluster.Status.Bastion.ServerID, - PublicIPID: stackitCluster.Status.Bastion.PublicIPID, - PublicIP: stackitCluster.Status.Bastion.PublicIP, SecurityGroupID: stackitCluster.Status.Bastion.SecurityGroupID, }); err != nil && !cloud.IsNotFound(err) { log.Error(err, "Failed to delete bastion", "cluster", stackitCluster.Name) @@ -449,7 +457,7 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS "Failed to delete bastion: %v", err) return ctrl.Result{}, err } - if hasBastionStatus(stackitCluster.Status.Bastion) { + if hadBastion { clusterScope.ClearBastionStatus() record.Event(stackitCluster, "BastionDeleted", "Deleted bastion") } diff --git a/controller/stackitmachine_controller.go b/controller/stackitmachine_controller.go index 30688dd..cf71067 100644 --- a/controller/stackitmachine_controller.go +++ b/controller/stackitmachine_controller.go @@ -53,6 +53,7 @@ type StackitMachineReconciler struct { // +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines,verbs=get;list;watch // +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch // +kubebuilder:rbac:groups=events.k8s.io,resources=events,verbs=create;patch +// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch func (r *StackitMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, err error) { log := logf.FromContext(ctx) diff --git a/docs/src/topics/failure-domains.md b/docs/src/topics/failure-domains.md index 113e1fb..79e2ad3 100644 --- a/docs/src/topics/failure-domains.md +++ b/docs/src/topics/failure-domains.md @@ -51,6 +51,11 @@ using the following precedence: The actual zone where the VM was placed is surfaced on `StackitMachine.status.availabilityZone`. +When `StackitCluster.spec.bastion.enabled` is `true`, the provider-managed bastion +host is also placed into the cluster's default availability zone (the first +sorted published failure domain, e.g., `eu01-1`) so it is never created in a +metro zone (`-m`). + ## Worker Node Spreading Unlike the control plane, worker nodes do not automatically spread across failure domains diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index d1a4705..0bb4c8b 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -15,6 +15,7 @@ package scope import ( "context" + "slices" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -25,6 +26,7 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "github.com/stackitcloud/cluster-api-provider-stackit/util" + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" ) // ClusterScope holds the per-reconcile state for a StackitCluster. @@ -32,6 +34,7 @@ type ClusterScope struct { Client client.Client Cluster *clusterv1.Cluster StackitCluster *infrav1.StackitCluster + CloudClient cloud.Client patchHelper *patch.Helper } @@ -87,13 +90,9 @@ func (s *ClusterScope) SetAPIServerEndpoint(endpoint clusterv1.APIEndpoint) { } func (s *ClusterScope) SetBastionStatus(bastion *cloud.Bastion, cloudInitHash string) { - s.StackitCluster.Status.Bastion = infrav1.StackitBastionStatus{ - ServerID: bastion.ServerID, - PublicIPID: bastion.PublicIPID, - PublicIP: bastion.PublicIP, - SecurityGroupID: bastion.SecurityGroupID, - CloudInitHash: cloudInitHash, - } + s.StackitCluster.Status.Bastion.ServerID = bastion.ServerID + s.StackitCluster.Status.Bastion.SecurityGroupID = bastion.SecurityGroupID + s.StackitCluster.Status.Bastion.CloudInitHash = cloudInitHash } func (s *ClusterScope) ClearBastionStatus() { @@ -103,3 +102,70 @@ func (s *ClusterScope) ClearBastionStatus() { func (s *ClusterScope) SetFailureDomains(failureDomains []clusterv1.FailureDomain) { s.StackitCluster.Status.FailureDomains = failureDomains } + +// DefaultAvailabilityZone returns the default single availability zone for a +// StackitCluster (the alphabetically first published failure domain, falling +// back to -1). This prevents the STACKIT IaaS API from defaulting to +// the expensive metro zone (-m). +func DefaultAvailabilityZone(stackitCluster *infrav1.StackitCluster) string { + if stackitCluster == nil { + return "" + } + if len(stackitCluster.Status.FailureDomains) > 0 { + zones := make([]string, 0, len(stackitCluster.Status.FailureDomains)) + for _, fd := range stackitCluster.Status.FailureDomains { + zones = append(zones, fd.Name) + } + slices.Sort(zones) + return zones[0] + } + if stackitCluster.Spec.Region != "" { + return stackitCluster.Spec.Region + "-1" + } + return "" +} + +func (s *ClusterScope) Cloud() cloud.Client { + return s.CloudClient +} + +func (s *ClusterScope) SetCloudClient(cloudClient cloud.Client) { + s.CloudClient = cloudClient +} + +func (s *ClusterScope) ProjectID() string { + if s.StackitCluster.Spec.ProjectID != "" { + return s.StackitCluster.Spec.ProjectID + } + return s.CloudClient.ProjectID() +} + +func (s *ClusterScope) Region() string { + if s.StackitCluster.Spec.Region != "" { + return s.StackitCluster.Spec.Region + } + return s.CloudClient.Region() +} + +func (s *ClusterScope) IaaSClient() *iaas.APIClient { + return s.CloudClient.IaaSClient() +} + +func (s *ClusterScope) BastionSpec() infrav1.StackitBastionSpec { + return s.StackitCluster.Spec.Bastion +} + +func (s *ClusterScope) BastionStatus() infrav1.StackitBastionStatus { + return s.StackitCluster.Status.Bastion +} + +func (s *ClusterScope) BastionTags() map[string]string { + tags := util.ClusterTags(s.StackitCluster.Name, s.StackitCluster.Namespace, s.StackitCluster.Spec.AdditionalLabels) + tags[util.LabelResourceRole] = util.ResourceRoleBastion + return tags +} + +func (s *ClusterScope) SetBastionPublicIP(id, ip string) { + s.StackitCluster.Status.Bastion.PublicIPID = id + s.StackitCluster.Status.Bastion.PublicIP = ip +} diff --git a/scope/machine_scope.go b/scope/machine_scope.go index 58bafde..b736ce1 100644 --- a/scope/machine_scope.go +++ b/scope/machine_scope.go @@ -12,7 +12,6 @@ package scope import ( "context" - "slices" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -128,18 +127,5 @@ func (s *MachineScope) AvailabilityZone() string { return s.StackitMachine.Spec.AvailabilityZone } - if s.StackitCluster != nil && len(s.StackitCluster.Status.FailureDomains) > 0 { - zones := make([]string, 0, len(s.StackitCluster.Status.FailureDomains)) - for _, fd := range s.StackitCluster.Status.FailureDomains { - zones = append(zones, fd.Name) - } - slices.Sort(zones) - return zones[0] - } - - if s.StackitCluster != nil && s.StackitCluster.Spec.Region != "" { - return s.StackitCluster.Spec.Region + "-1" - } - - return "" + return DefaultAvailabilityZone(s.StackitCluster) } diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index 224d56a..d753b39 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -34,6 +34,7 @@ import ( . "github.com/onsi/gomega" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/test/utils" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -490,7 +491,7 @@ var _ = Describe("Manager", Ordered, func() { Eventually(func(g Gomega) { output := kubectlOutput(g, "get", "stackitcluster", clusterName, "-n", cfg.Namespace, "-o", "jsonpath={.status.bastion.publicIP}") g.Expect(output).NotTo(BeEmpty()) - publicIPs, err := cloudClient.ListPublicIPsByTags(ctx, stackitE2EBastionTags(testID)) + publicIPs, err := publicip.ListByTags(ctx, cloudClient.IaaSClient(), cloudClient.ProjectID(), cloudClient.Region(), stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) g.Expect(publicIPs).To(HaveLen(1)) securityGroups, err := cloudClient.ListSecurityGroupsByTags(ctx, stackitE2EBastionTags(testID)) @@ -556,7 +557,7 @@ var _ = Describe("Manager", Ordered, func() { loadBalancers, err := cloudClient.ListAPIServerLoadBalancersByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(loadBalancers).To(BeEmpty()) - publicIPs, err := cloudClient.ListPublicIPsByTags(ctx, stackitE2EBastionTags(testID)) + publicIPs, err := publicip.ListByTags(ctx, cloudClient.IaaSClient(), cloudClient.ProjectID(), cloudClient.Region(), stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) g.Expect(publicIPs).To(BeEmpty()) securityGroups, err := cloudClient.ListSecurityGroupsByTags(ctx, stackitE2EBastionTags(testID)) diff --git a/util/conditions.go b/util/conditions.go index f60b2bf..d22050e 100644 --- a/util/conditions.go +++ b/util/conditions.go @@ -14,11 +14,10 @@ import ( "errors" "time" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" - - "github.com/stackitcloud/cluster-api-provider-stackit/cloud" ) // SetCondition adds or updates a condition on conditions. From 254dd8b165ac48921d3e03d056ff3b45cacc14e4 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Wed, 11 Nov 2026 11:37:29 +0100 Subject: [PATCH 07/60] refactor: fix merge --- cloud/fake/client.go | 76 --------------------- controller/stackitcluster_infrastructure.go | 8 +-- 2 files changed, 1 insertion(+), 83 deletions(-) diff --git a/cloud/fake/client.go b/cloud/fake/client.go index 1283943..5671191 100644 --- a/cloud/fake/client.go +++ b/cloud/fake/client.go @@ -489,82 +489,6 @@ func (c *Client) RemovePublicIPFromServer(_ context.Context, serverID, publicIPI return nil } -func (c *Client) CreatePublicIP(_ context.Context, tags map[string]string) (*cloud.PublicIP, error) { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextCreatePublicIP); err != nil { - return nil, err - } - id := c.genID() - publicIP := &cloud.PublicIP{ - ID: id, - IP: "203.0.113.22", - } - c.publicIPs[id] = &publicIPEntry{ - publicIP: publicIP, - tags: copyTags(tags), - } - out := *publicIP - return &out, nil -} - -func (c *Client) DeletePublicIP(_ context.Context, id string) error { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextDeletePublicIP); err != nil { - return err - } - if id == "" { - return fmt.Errorf("public IP ID is empty: %w", cloud.ErrInvalidInput) - } - if _, ok := c.publicIPs[id]; !ok { - return fmt.Errorf("public IP %q: %w", id, cloud.ErrNotFound) - } - delete(c.publicIPs, id) - return nil -} - -func (c *Client) AddPublicIPToServer(_ context.Context, serverID, publicIPID string) error { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextAddPublicIP); err != nil { - return err - } - if serverID == "" || publicIPID == "" { - return fmt.Errorf("server ID and public IP ID are required: %w", cloud.ErrInvalidInput) - } - if _, ok := c.servers[serverID]; !ok { - return fmt.Errorf("server %q: %w", serverID, cloud.ErrNotFound) - } - entry, ok := c.publicIPs[publicIPID] - if !ok { - return fmt.Errorf("public IP %q: %w", publicIPID, cloud.ErrNotFound) - } - entry.publicIP.NetworkInterfaceID = "fake-nic-" + serverID - return nil -} - -func (c *Client) RemovePublicIPFromServer(_ context.Context, serverID, publicIPID string) error { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextRemovePublicIP); err != nil { - return err - } - if serverID == "" || publicIPID == "" { - return fmt.Errorf("server ID and public IP ID are required: %w", cloud.ErrInvalidInput) - } - entry, ok := c.publicIPs[publicIPID] - if !ok { - return fmt.Errorf("public IP %q: %w", publicIPID, cloud.ErrNotFound) - } - entry.publicIP.NetworkInterfaceID = "" - return nil -} - func (c *Client) ListSecurityGroupsByTags(_ context.Context, tags map[string]string) ([]*cloud.SecurityGroup, error) { c.mu.Lock() defer c.mu.Unlock() diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index 2cf0a24..8bbaf7a 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -444,13 +444,7 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS "Failed to delete bastion public IP: %v", err) return ctrl.Result{}, err } - hadBastion := hasBastionStatus(stackitCluster.Status.Bastion) - if err := publicip.New(clusterScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { - log.Error(err, "Failed to delete bastion public IP", "cluster", stackitCluster.Name) - record.Warnf(stackitCluster, "FailedDeleteBastion", - "Failed to delete bastion public IP: %v", err) - return ctrl.Result{}, err - } + if err := cloudClient.DeleteBastion(ctx, bastionservice.Input(stackitCluster, nil), cloud.Bastion{ ServerID: stackitCluster.Status.Bastion.ServerID, SecurityGroupID: stackitCluster.Status.Bastion.SecurityGroupID, From 2fddb3a5ae468f3198a694290f964e7e96404f97 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Tue, 10 Nov 2026 20:44:06 +0100 Subject: [PATCH 08/60] feat: refactor loadbalancer service --- cloud/cleanup.go | 10 +- cloud/cleanup_test.go | 39 +- cloud/client.go | 17 +- cloud/fake/client.go | 366 ++++++++--- cloud/sdk_client.go | 243 +------ cloud/sdk_client_integration_test.go | 81 ++- cloud/sdk_client_test.go | 207 ------ cloud/services/loadbalancer/loadbalancer.go | 144 ----- cloud/services/loadbalancer/reconcile.go | 273 ++++++++ cloud/services/loadbalancer/reconcile_test.go | 597 ++++++++++++++++++ cloud/services/loadbalancer/service.go | 107 ++++ cloud/services/loadbalancer/targets.go | 213 +++++++ .../{loadbalancer_test.go => targets_test.go} | 12 +- cloud/services/shared/utils.go | 9 - cloud/types.go | 25 +- controller/constants.go | 2 - controller/stackitcluster_infrastructure.go | 116 ++-- scope/cluster_scope.go | 45 ++ test/e2e/e2e_test.go | 19 +- 19 files changed, 1663 insertions(+), 862 deletions(-) delete mode 100644 cloud/services/loadbalancer/loadbalancer.go create mode 100644 cloud/services/loadbalancer/reconcile.go create mode 100644 cloud/services/loadbalancer/reconcile_test.go create mode 100644 cloud/services/loadbalancer/service.go create mode 100644 cloud/services/loadbalancer/targets.go rename cloud/services/loadbalancer/{loadbalancer_test.go => targets_test.go} (92%) diff --git a/cloud/cleanup.go b/cloud/cleanup.go index 481f3de..805b937 100644 --- a/cloud/cleanup.go +++ b/cloud/cleanup.go @@ -16,6 +16,7 @@ import ( "maps" "strings" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" ) @@ -55,13 +56,14 @@ func CleanupByTags(ctx context.Context, client Client, tags map[string]string) e errs = append(errs, fmt.Sprintf("delete bastion resources: %v", err)) } - loadBalancers, err := client.ListAPIServerLoadBalancersByTags(ctx, tags) + lbClient := loadbalancer.NewClient(client) + loadBalancers, err := lbClient.ListByTags(ctx, tags) if err != nil { errs = append(errs, err.Error()) } else { - for _, loadBalancer := range loadBalancers { - if err := client.DeleteAPIServerLoadBalancer(ctx, loadBalancer.ID); err != nil && !IsNotFound(err) { - errs = append(errs, fmt.Sprintf("delete load balancer %q: %v", loadBalancer.ID, err)) + for _, lb := range loadBalancers { + if err := lbClient.DeleteByID(ctx, lb.ID); err != nil { + errs = append(errs, fmt.Sprintf("delete load balancer %q: %v", lb.ID, err)) } } } diff --git a/cloud/cleanup_test.go b/cloud/cleanup_test.go index 2fe2dc0..ff1ce52 100644 --- a/cloud/cleanup_test.go +++ b/cloud/cleanup_test.go @@ -48,29 +48,11 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { if _, err := client.CreateSecurityGroup(ctx, "other-sg", otherTags); err != nil { t.Fatalf("CreateSecurityGroup() error = %v", err) } - if _, err := client.EnsureAPIServerLoadBalancer(ctx, cloud.LoadBalancerInput{ - Name: "matched", - Tags: tags, - NetworkID: "network-id", - Port: 6443, - Targets: []cloud.LoadBalancerTargetInput{{ - Name: "target", - IP: "10.0.0.10", - }}, - }); err != nil { - t.Fatalf("EnsureAPIServerLoadBalancer() error = %v", err) - } - if _, err := client.EnsureAPIServerLoadBalancer(ctx, cloud.LoadBalancerInput{ - Name: "other", - Tags: otherTags, - NetworkID: "network-id", - Port: 6443, - Targets: []cloud.LoadBalancerTargetInput{{ - Name: "target", - IP: "10.0.0.11", - }}, - }); err != nil { - t.Fatalf("EnsureAPIServerLoadBalancer() error = %v", err) + if _, err := client.CreateLoadBalancer(ctx, "matched", tags); err != nil { + t.Fatalf("CreateLoadBalancer() error = %v", err) + } + if _, err := client.CreateLoadBalancer(ctx, "other", otherTags); err != nil { + t.Fatalf("CreateLoadBalancer() error = %v", err) } if err := cloud.CleanupByTags(ctx, client, tags); err != nil { @@ -91,13 +73,20 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { if len(otherServers) != 1 { t.Fatalf("other server count = %d, want 1", len(otherServers)) } - matchedLoadBalancers, err := client.ListAPIServerLoadBalancersByTags(ctx, tags) + matchedLoadBalancers, err := client.ListLoadBalancersByTags(ctx, tags) if err != nil { - t.Fatalf("ListAPIServerLoadBalancersByTags() error = %v", err) + t.Fatalf("ListLoadBalancersByTags() error = %v", err) } if len(matchedLoadBalancers) != 0 { t.Fatalf("matched load balancer count = %d, want 0", len(matchedLoadBalancers)) } + otherLoadBalancers, err := client.ListLoadBalancersByTags(ctx, otherTags) + if err != nil { + t.Fatalf("ListLoadBalancersByTags(other) error = %v", err) + } + if len(otherLoadBalancers) != 1 { + t.Fatalf("other load balancer count = %d, want 1", len(otherLoadBalancers)) + } matchedPublicIPs, err := client.ListPublicIPsByTags(ctx, tags) if err != nil { t.Fatalf("ListPublicIPsByTags() error = %v", err) diff --git a/cloud/client.go b/cloud/client.go index 40b657a..f36919d 100644 --- a/cloud/client.go +++ b/cloud/client.go @@ -14,6 +14,7 @@ import ( "context" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" ) // Reconciler is a generic interface used by components offering a type of service. @@ -30,8 +31,8 @@ type Reconciler interface { // - return ErrInvalidInput for permanent input validation errors // - return ErrConflict for retryable conflict errors // - return ErrTransient for retryable transient errors -// - be idempotent (CreateServer/EnsureAPIServerLoadBalancer must not produce -// duplicates when a resource with matching tags already exists) +// - be idempotent (CreateServer must not produce duplicates when a resource +// with matching tags already exists) type Client interface { ProjectID() string @@ -39,6 +40,8 @@ type Client interface { IaaSClient() *iaas.APIClient + LoadBalancerClient() *lb.APIClient + GetServer(ctx context.Context, id string) (*Server, error) FindServerByTags(ctx context.Context, tags map[string]string) (*Server, error) @@ -55,16 +58,6 @@ type Client interface { DeleteBastion(ctx context.Context, input BastionInput, status Bastion) error - EnsureAPIServerLoadBalancer(ctx context.Context, input LoadBalancerInput) (*LoadBalancer, error) - - ListAPIServerLoadBalancersByTags(ctx context.Context, tags map[string]string) ([]*LoadBalancer, error) - - // SetAPIServerLoadBalancerTargets replaces the contents of the API-server - // target pool. STACKIT rejects an empty pool. - SetAPIServerLoadBalancerTargets(ctx context.Context, loadBalancerID string, port int32, targets []LoadBalancerTargetInput) error - - DeleteAPIServerLoadBalancer(ctx context.Context, id string) error - ListAvailabilityZones(ctx context.Context) ([]string, error) } diff --git a/cloud/fake/client.go b/cloud/fake/client.go index 5671191..bd85967 100644 --- a/cloud/fake/client.go +++ b/cloud/fake/client.go @@ -24,9 +24,11 @@ import ( "sync" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/stackit-sdk-go/core/config" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" ) const ( @@ -44,6 +46,7 @@ type Client struct { securityGroups map[string]*securityGroupEntry networks map[string]*cloud.Network iaasClient *iaas.APIClient + lbClient *lb.APIClient nextID int @@ -145,6 +148,16 @@ func New(networks ...cloud.Network) *Client { panic(fmt.Sprintf("create fake iaas client: %v", err)) } c.iaasClient = iaasClient + + lbClient, err := lb.NewAPIClient( + config.WithoutAuthentication(), + config.WithHTTPClient(&http.Client{Transport: roundTripperFunc(c.roundTripLB)}), + config.WithEndpoint("http://stackit.fake.local"), + ) + if err != nil { + panic(fmt.Sprintf("create fake loadbalancer client: %v", err)) + } + c.lbClient = lbClient return c } @@ -160,6 +173,10 @@ func (c *Client) IaaSClient() *iaas.APIClient { return c.iaasClient } +func (c *Client) LoadBalancerClient() *lb.APIClient { + return c.lbClient +} + func consume(p *error) error { if *p == nil { return nil @@ -518,55 +535,29 @@ func (c *Client) CreateSecurityGroup(_ context.Context, name string, tags map[st return &out, nil } -func (c *Client) EnsureAPIServerLoadBalancer( - _ context.Context, - input cloud.LoadBalancerInput, -) (*cloud.LoadBalancer, error) { +func (c *Client) CreateLoadBalancer(_ context.Context, name string, tags map[string]string) (*cloud.LoadBalancer, error) { c.mu.Lock() defer c.mu.Unlock() - if err := consume(&c.FailNextEnsureLB); err != nil { + entry, err := c.createLoadBalancerEntry(name, 6443, loadbalancer.Labels(tags), nil) + if err != nil { return nil, err } - for _, entry := range c.loadBalancers { - if len(input.Tags) > 0 && mapContains(entry.tags, input.Tags) { - out := *entry.lb - return &out, nil - } - } - id := c.genID() - lb := &cloud.LoadBalancer{ - ID: id, - Name: input.Name, - IP: "203.0.113.10", - Port: input.Port, - } - targets := map[string]string{} - if len(input.Targets) == 0 { - targets[bootstrapTargetName] = bootstrapTargetIP - } else { - for _, target := range input.Targets { - targets[target.Name] = target.IP - } - } - c.loadBalancers[id] = &lbEntry{lb: lb, tags: copyTags(input.Tags), targets: targets} - out := *lb + out := *entry.lb return &out, nil } -func (c *Client) ListAPIServerLoadBalancersByTags( - _ context.Context, - tags map[string]string, -) ([]*cloud.LoadBalancer, error) { +func (c *Client) ListLoadBalancersByTags(_ context.Context, tags map[string]string) ([]*cloud.LoadBalancer, error) { c.mu.Lock() defer c.mu.Unlock() if len(tags) == 0 { return nil, fmt.Errorf("empty tags: %w", cloud.ErrInvalidInput) } + wantLabels := loadbalancer.Labels(tags) loadBalancers := []*cloud.LoadBalancer{} for _, entry := range c.loadBalancers { - if mapContains(entry.tags, tags) { + if mapContains(entry.tags, wantLabels) || mapContains(entry.tags, tags) { out := *entry.lb loadBalancers = append(loadBalancers, &out) } @@ -574,51 +565,6 @@ func (c *Client) ListAPIServerLoadBalancersByTags( return loadBalancers, nil } -func (c *Client) SetAPIServerLoadBalancerTargets( - _ context.Context, - loadBalancerID string, - port int32, - targets []cloud.LoadBalancerTargetInput, -) error { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextSetTargets); err != nil { - return err - } - if loadBalancerID == "" || port <= 0 { - return fmt.Errorf("load balancer ID and target port are required: %w", cloud.ErrInvalidInput) - } - if len(targets) == 0 { - return fmt.Errorf("at least one target is required: %w", cloud.ErrInvalidInput) - } - entry, ok := c.loadBalancers[loadBalancerID] - if !ok { - return fmt.Errorf("load balancer %q: %w", loadBalancerID, cloud.ErrNotFound) - } - replaced := make(map[string]string, len(targets)) - for _, target := range targets { - // The real API rejects these; accepting them would hide a regression. - if target.Name == "" || target.IP == "" { - return fmt.Errorf("target name and target IP are required: %w", cloud.ErrInvalidInput) - } - replaced[target.Name] = target.IP - } - entry.targets = replaced - return nil -} - -func (c *Client) DeleteAPIServerLoadBalancer(_ context.Context, id string) error { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextDeleteLB); err != nil { - return err - } - delete(c.loadBalancers, id) - return nil -} - // ServerCount returns the number of currently tracked servers (test helper). func (c *Client) ServerCount() int { c.mu.Lock() @@ -833,6 +779,270 @@ func (c *Client) roundTripIaaS(r *http.Request) (*http.Response, error) { return rec.Result(), nil } +func (c *Client) roundTripLB(r *http.Request) (*http.Response, error) { + rec := httptest.NewRecorder() + rec.Header().Set("Content-Type", "application/json") + + if c.handleLoadBalancerRequest(rec, r) { + return rec.Result(), nil + } + + rec.WriteHeader(http.StatusNotFound) + _ = json.NewEncoder(rec).Encode(map[string]any{"message": "not found"}) + return rec.Result(), nil +} + +type fakeTargetJSON struct { + DisplayName string `json:"displayName"` + IP string `json:"ip"` +} + +func toLoadBalancerTargets(in []fakeTargetJSON) []loadbalancer.Target { + out := make([]loadbalancer.Target, 0, len(in)) + for _, t := range in { + out = append(out, loadbalancer.Target{Name: t.DisplayName, IP: t.IP}) + } + return out +} + +func (c *Client) handleLoadBalancerRequest(rec *httptest.ResponseRecorder, r *http.Request) bool { + path := r.URL.Path + + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers"): + items, err := c.listLoadBalancerItems() + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(map[string]any{"loadBalancers": items}) + } + return true + + case r.Method == http.MethodPost && strings.HasSuffix(path, "/load-balancers"): + var body struct { + Name string `json:"name"` + Labels map[string]string `json:"labels"` + TargetPools []struct { + Name string `json:"name"` + TargetPort int32 `json:"targetPort"` + Targets []fakeTargetJSON `json:"targets"` + } `json:"targetPools"` + Listeners []struct { + Port int32 `json:"port"` + } `json:"listeners"` + } + if r.Body != nil { + _ = json.NewDecoder(r.Body).Decode(&body) + } + var port int32 = 6443 + if len(body.Listeners) > 0 && body.Listeners[0].Port > 0 { + port = body.Listeners[0].Port + } + var targets []loadbalancer.Target + if len(body.TargetPools) > 0 { + targets = toLoadBalancerTargets(body.TargetPools[0].Targets) + } + created, err := c.createLoadBalancer(body.Name, port, body.Labels, targets) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(created) + } + return true + + case r.Method == http.MethodPut && strings.Contains(path, "/load-balancers/") && strings.Contains(path, "/target-pools/"): + sub := path[strings.Index(path, "/load-balancers/")+len("/load-balancers/"):] + parts := strings.SplitN(sub, "/target-pools/", 2) + lbID := parts[0] + var body struct { + Name string `json:"name"` + TargetPort int32 `json:"targetPort"` + Targets []fakeTargetJSON `json:"targets"` + } + if r.Body != nil { + _ = json.NewDecoder(r.Body).Decode(&body) + } + pool, err := c.updateLoadBalancerTargetPool(lbID, body.TargetPort, toLoadBalancerTargets(body.Targets)) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(pool) + } + return true + + case r.Method == http.MethodGet && strings.Contains(path, "/load-balancers/"): + id := path[strings.LastIndex(path, "/load-balancers/")+len("/load-balancers/"):] + got, err := c.getLoadBalancerItem(id) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(got) + } + return true + + case r.Method == http.MethodDelete && strings.Contains(path, "/load-balancers/"): + id := path[strings.LastIndex(path, "/load-balancers/")+len("/load-balancers/"):] + if err := c.deleteLoadBalancer(id); err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(map[string]any{}) + } + return true + + default: + return false + } +} + +func (c *Client) listLoadBalancerItems() ([]map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + items := make([]map[string]any, 0, len(c.loadBalancers)) + for _, entry := range c.loadBalancers { + items = append(items, fakeLoadBalancerMap(entry)) + } + return items, nil +} + +func (c *Client) getLoadBalancerItem(id string) (map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextSetTargets); err != nil { + return nil, err + } + entry, ok := c.loadBalancers[id] + if !ok { + return nil, fmt.Errorf("load balancer %q: %w", id, cloud.ErrNotFound) + } + return fakeLoadBalancerMap(entry), nil +} + +func (c *Client) createLoadBalancer(name string, port int32, labels map[string]string, targets []loadbalancer.Target) (map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextEnsureLB); err != nil { + return nil, err + } + entry, err := c.createLoadBalancerEntry(name, port, labels, targets) + if err != nil { + return nil, err + } + return fakeLoadBalancerMap(entry), nil +} + +func (c *Client) createLoadBalancerEntry(name string, port int32, labels map[string]string, targets []loadbalancer.Target) (*lbEntry, error) { + if name == "" { + return nil, fmt.Errorf("load balancer name is empty: %w", cloud.ErrInvalidInput) + } + if port <= 0 { + port = 6443 + } + lbObj := &cloud.LoadBalancer{ + ID: name, + Name: name, + IP: "203.0.113.10", + Port: port, + } + targetMap := map[string]string{} + if len(targets) == 0 { + targetMap[bootstrapTargetName] = bootstrapTargetIP + } else { + for _, target := range targets { + targetMap[target.Name] = target.IP + } + } + entry := &lbEntry{ + lb: lbObj, + tags: copyTags(labels), + targets: targetMap, + } + c.loadBalancers[name] = entry + return entry, nil +} + +func (c *Client) updateLoadBalancerTargetPool(id string, port int32, targets []loadbalancer.Target) (map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextSetTargets); err != nil { + return nil, err + } + if id == "" || port <= 0 { + return nil, fmt.Errorf("load balancer ID and target port are required: %w", cloud.ErrInvalidInput) + } + if len(targets) == 0 { + return nil, fmt.Errorf("at least one target is required: %w", cloud.ErrInvalidInput) + } + entry, ok := c.loadBalancers[id] + if !ok { + return nil, fmt.Errorf("load balancer %q: %w", id, cloud.ErrNotFound) + } + replaced := make(map[string]string, len(targets)) + for _, target := range targets { + if target.Name == "" || target.IP == "" { + return nil, fmt.Errorf("target name and target IP are required: %w", cloud.ErrInvalidInput) + } + replaced[target.Name] = target.IP + } + entry.targets = replaced + entry.lb.Port = port + return fakeTargetPoolMap(entry), nil +} + +func (c *Client) deleteLoadBalancer(id string) error { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextDeleteLB); err != nil { + return err + } + delete(c.loadBalancers, id) + return nil +} + +func fakeLoadBalancerMap(entry *lbEntry) map[string]any { + m := map[string]any{ + "name": entry.lb.Name, + "externalAddress": entry.lb.IP, + "listeners": []any{ + map[string]any{ + "displayName": "apiserver", + "port": entry.lb.Port, + "targetPool": "apiserver", + }, + }, + "targetPools": []any{ + fakeTargetPoolMap(entry), + }, + } + if len(entry.tags) > 0 { + labels := make(map[string]any, len(entry.tags)) + for k, v := range entry.tags { + labels[k] = v + } + m["labels"] = labels + } + return m +} + +func fakeTargetPoolMap(entry *lbEntry) map[string]any { + targets := make([]any, 0, len(entry.targets)) + for name, ip := range entry.targets { + targets = append(targets, map[string]any{ + "displayName": name, + "ip": ip, + }) + } + return map[string]any{ + "name": "apiserver", + "targetPort": entry.lb.Port, + "targets": targets, + } +} + func (c *Client) handlePublicIPRequest(rec *httptest.ResponseRecorder, r *http.Request) bool { path := r.URL.Path diff --git a/cloud/sdk_client.go b/cloud/sdk_client.go index 4777106..c1e3376 100644 --- a/cloud/sdk_client.go +++ b/cloud/sdk_client.go @@ -11,13 +11,10 @@ You may obtain a copy of the License at package cloud import ( - "cmp" "context" "encoding/base64" "fmt" "os" - "slices" - "strings" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" @@ -31,11 +28,6 @@ const ( stackitTokenEndpointEnv = "STACKIT_TOKEN_BASEURL" stackitIAASEndpointEnv = "STACKIT_IAAS_ENDPOINT" stackitLBEndpointEnv = "STACKIT_LOADBALANCER_ENDPOINT" - - apiserverTargetPoolName = "apiserver" - apiserverListenerName = "apiserver" - bootstrapTargetName = "capi-bootstrap-placeholder" - bootstrapTargetIP = "10.0.0.1" ) // SDKClient is the STACKIT SDK-backed implementation of Client. @@ -92,6 +84,10 @@ func (c *SDKClient) IaaSClient() *iaas.APIClient { return c.iaasClient } +func (c *SDKClient) LoadBalancerClient() *lb.APIClient { + return c.lbClient +} + func stackitOptions(creds Credentials, endpoint string) []config.ConfigurationOption { opts := []config.ConfigurationOption{ config.WithUserAgent("cluster-api-provider-stackit"), @@ -342,212 +338,6 @@ func (c *SDKClient) DeleteBastion(ctx context.Context, input BastionInput, statu return nil } -func (c *SDKClient) EnsureAPIServerLoadBalancer(ctx context.Context, input LoadBalancerInput) (*LoadBalancer, error) { - if existing, err := c.findLoadBalancerByTags(ctx, input.Tags); err == nil { - return existing, nil - } else if !IsNotFound(err) && !isLoadBalancerServiceNotEnabled(err) { - return nil, err - } - payload := lb.NewCreateLoadBalancerPayload() - payload.SetName(input.Name) - payload.SetRegion(input.Region) - if len(input.Tags) > 0 { - payload.SetLabels(shared.LoadBalancerLabels(input.Tags)) - } - options := lb.NewLoadBalancerOptions() - options.SetEphemeralAddress(true) - payload.SetOptions(*options) - - network := lb.NewNetwork() - network.SetNetworkId(input.NetworkID) - network.SetRole("ROLE_LISTENERS_AND_TARGETS") - payload.SetNetworks([]lb.Network{*network}) - - targetPool := lb.NewTargetPool() - targetPool.SetName(apiserverTargetPoolName) - targetPool.SetTargetPort(input.Port) - targets := make([]lb.Target, 0, len(input.Targets)) - if len(input.Targets) == 0 { - target := lb.NewTarget() - target.SetDisplayName(bootstrapTargetName) - target.SetIp(bootstrapTargetIP) - targets = append(targets, *target) - } else { - for _, targetInput := range input.Targets { - target := lb.NewTarget() - target.SetDisplayName(targetInput.Name) - target.SetIp(targetInput.IP) - targets = append(targets, *target) - } - } - targetPool.SetTargets(targets) - payload.SetTargetPools([]lb.TargetPool{*targetPool}) - - listener := lb.NewListener() - listener.SetDisplayName(apiserverListenerName) - listener.SetPort(input.Port) - listener.SetProtocol("PROTOCOL_TCP") - listener.SetTargetPool(apiserverTargetPoolName) - payload.SetListeners([]lb.Listener{*listener}) - - lbResp, err := c.lbClient.DefaultAPI.CreateLoadBalancer(ctx, c.projectID, c.region). - CreateLoadBalancerPayload(*payload). - Execute() - if err != nil { - return nil, shared.ClassifySDKError("create load balancer", err) - } - return loadBalancerFromSDK(lbResp), nil -} - -func (c *SDKClient) DeleteAPIServerLoadBalancer(ctx context.Context, id string) error { - if _, err := c.lbClient.DefaultAPI.DeleteLoadBalancer(ctx, c.projectID, c.region, id).Execute(); err != nil { - return shared.ClassifySDKError("delete load balancer", err) - } - return nil -} - -func (c *SDKClient) ListAPIServerLoadBalancersByTags( - ctx context.Context, - tags map[string]string, -) ([]*LoadBalancer, error) { - if len(tags) == 0 { - return nil, fmt.Errorf("%w: empty tag selector", ErrInvalidInput) - } - resp, err := c.lbClient.DefaultAPI.ListLoadBalancers(ctx, c.projectID, c.region).Execute() - if err != nil { - err := shared.ClassifySDKError("list load balancers", err) - if isLoadBalancerServiceNotEnabled(err) { - return nil, nil - } - return nil, err - } - matched := []*LoadBalancer{} - labels := shared.LoadBalancerLabels(tags) - for _, candidate := range resp.GetLoadBalancers() { - if shared.MapContains(candidate.GetLabels(), labels) { - candidate := candidate - matched = append(matched, loadBalancerFromSDK(&candidate)) - } - } - return matched, nil -} - -func (c *SDKClient) SetAPIServerLoadBalancerTargets( - ctx context.Context, - loadBalancerID string, - port int32, - targets []LoadBalancerTargetInput, -) error { - if loadBalancerID == "" || port <= 0 { - return fmt.Errorf("%w: load balancer ID and target port are required", ErrInvalidInput) - } - // STACKIT NLB target pools must contain at least one target. - if len(targets) == 0 { - return fmt.Errorf("%w: at least one target is required", ErrInvalidInput) - } - desired := make([]lb.Target, 0, len(targets)) - for _, targetInput := range targets { - if targetInput.Name == "" || targetInput.IP == "" { - return fmt.Errorf("%w: target name and target IP are required", ErrInvalidInput) - } - target := lb.NewTarget() - target.SetDisplayName(targetInput.Name) - target.SetIp(targetInput.IP) - desired = append(desired, *target) - } - - loadBalancer, err := c.lbClient.DefaultAPI.GetLoadBalancer(ctx, c.projectID, c.region, loadBalancerID).Execute() - if err != nil { - return shared.ClassifySDKError("get load balancer", err) - } - targetPool := apiServerTargetPool(loadBalancer) - if targetPool == nil { - return fmt.Errorf( - "%w: load balancer %q has no %q target pool", - ErrNotFound, - loadBalancerID, - apiserverTargetPoolName, - ) - } - - // Every cluster reconcile ends up here; skip the update call when the pool - // already matches. - if targetPool.GetTargetPort() == port && sameTargets(targetPool.GetTargets(), desired) { - return nil - } - return c.updateAPIServerTargetPool(ctx, loadBalancerID, targetPool, desired, port) -} - -func sameTargets(current, desired []lb.Target) bool { - if len(current) != len(desired) { - return false - } - return slices.Equal(sortedTargetKeys(current), sortedTargetKeys(desired)) -} - -type targetKey struct{ name, ip string } - -func sortedTargetKeys(targets []lb.Target) []targetKey { - keys := make([]targetKey, 0, len(targets)) - for _, target := range targets { - keys = append(keys, targetKey{name: target.GetDisplayName(), ip: target.GetIp()}) - } - slices.SortFunc(keys, func(a, b targetKey) int { - return cmp.Or(strings.Compare(a.name, b.name), strings.Compare(a.ip, b.ip)) - }) - return keys -} - -func (c *SDKClient) findLoadBalancerByTags(ctx context.Context, tags map[string]string) (*LoadBalancer, error) { - matched, err := c.ListAPIServerLoadBalancersByTags(ctx, tags) - if err != nil { - return nil, err - } - if len(matched) == 0 { - return nil, fmt.Errorf("%w: no load balancer matching tags", ErrNotFound) - } - if len(matched) > 1 { - return nil, fmt.Errorf("%w: multiple load balancers matching tags", ErrConflict) - } - return matched[0], nil -} - -func (c *SDKClient) updateAPIServerTargetPool( - ctx context.Context, - loadBalancerID string, - current *lb.TargetPool, - targets []lb.Target, - port int32, -) error { - payload := lb.NewUpdateTargetPoolPayload() - payload.SetName(apiserverTargetPoolName) - if port > 0 { - payload.SetTargetPort(port) - } else if current.GetTargetPort() > 0 { - payload.SetTargetPort(current.GetTargetPort()) - } - payload.SetTargets(targets) - if _, err := c.lbClient.DefaultAPI. - UpdateTargetPool(ctx, c.projectID, c.region, loadBalancerID, apiserverTargetPoolName). - UpdateTargetPoolPayload(*payload). - Execute(); err != nil { - return shared.ClassifySDKError("update load balancer target pool", err) - } - return nil -} - -func apiServerTargetPool(loadBalancer *lb.LoadBalancer) *lb.TargetPool { - if loadBalancer == nil { - return nil - } - for _, targetPool := range loadBalancer.GetTargetPools() { - if targetPool.GetName() == apiserverTargetPoolName { - return &targetPool - } - } - return nil -} - func (c *SDKClient) serverFromSDK(ctx context.Context, server *iaas.Server) *Server { if server == nil { return nil @@ -572,28 +362,3 @@ func (c *SDKClient) serverFromSDK(ctx context.Context, server *iaas.Server) *Ser } return out } - -func loadBalancerFromSDK(in *lb.LoadBalancer) *LoadBalancer { - if in == nil { - return nil - } - return &LoadBalancer{ - ID: in.GetName(), - Name: in.GetName(), - IP: shared.FirstNonEmpty(in.GetExternalAddress(), in.GetPrivateAddress()), - Port: listenerPort(in.GetListeners()), - } -} - -func listenerPort(listeners []lb.Listener) int32 { - for _, listener := range listeners { - if port := listener.GetPort(); port > 0 { - return port - } - } - return 0 -} - -func isLoadBalancerServiceNotEnabled(err error) bool { - return IsUnauthorized(err) && strings.Contains(err.Error(), "Service not enabled") -} diff --git a/cloud/sdk_client_integration_test.go b/cloud/sdk_client_integration_test.go index 0fd1594..2d03335 100644 --- a/cloud/sdk_client_integration_test.go +++ b/cloud/sdk_client_integration_test.go @@ -20,6 +20,8 @@ import ( "os" "testing" "time" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" ) const ( @@ -66,13 +68,13 @@ func TestSDKClientLoadBalancerCreateDeleteIntegration(t *testing.T) { client := newIntegrationClient(t) networkID := requiredIntegrationEnv(t, envIntegrationNetworkID) targetIP := requiredIntegrationEnv(t, envIntegrationTargetIP) - loadBalancer := createIntegrationLoadBalancer(t, client, networkID, LoadBalancerTargetInput{ + lbID := createIntegrationLoadBalancer(t, client, networkID, LoadBalancerTargetInput{ Name: "capistackit-initial-target", IP: targetIP, }) - if err := client.DeleteAPIServerLoadBalancer(context.Background(), loadBalancer.ID); err != nil { - t.Fatalf("DeleteAPIServerLoadBalancer() error = %v", err) + if err := loadbalancer.NewClient(client).DeleteByID(context.Background(), lbID); err != nil { + t.Fatalf("DeleteByID() error = %v", err) } } @@ -81,13 +83,14 @@ func TestSDKClientLoadBalancerTargetIntegration(t *testing.T) { networkID := requiredIntegrationEnv(t, envIntegrationNetworkID) targetIP := requiredIntegrationEnv(t, envIntegrationTargetIP) initialIP := integrationInitialTargetIP(targetIP) - loadBalancer := createIntegrationLoadBalancer(t, client, networkID, LoadBalancerTargetInput{ + lbID := createIntegrationLoadBalancer(t, client, networkID, LoadBalancerTargetInput{ Name: "capistackit-initial-target", IP: initialIP, }) + lbClient := loadbalancer.NewClient(client) t.Cleanup(func() { - if err := client.DeleteAPIServerLoadBalancer(context.Background(), loadBalancer.ID); err != nil && !IsNotFound(err) { - t.Logf("DeleteAPIServerLoadBalancer() cleanup error = %v", err) + if err := lbClient.DeleteByID(context.Background(), lbID); err != nil && !IsNotFound(err) { + t.Logf("DeleteByID() cleanup error = %v", err) } }) @@ -95,11 +98,11 @@ func TestSDKClientLoadBalancerTargetIntegration(t *testing.T) { {Name: "capistackit-initial-target", IP: initialIP}, {Name: "capistackit-integration-target", IP: targetIP}, } - if err := client.SetAPIServerLoadBalancerTargets(context.Background(), loadBalancer.ID, 6443, targets); err != nil { - t.Fatalf("SetAPIServerLoadBalancerTargets() error = %v", err) + if err := lbClient.SetTargets(context.Background(), lbID, 6443, targets); err != nil { + t.Fatalf("SetTargets() error = %v", err) } - if err := client.SetAPIServerLoadBalancerTargets(context.Background(), loadBalancer.ID, 6443, targets[:1]); err != nil { - t.Fatalf("SetAPIServerLoadBalancerTargets() shrink error = %v", err) + if err := lbClient.SetTargets(context.Background(), lbID, 6443, targets[:1]); err != nil { + t.Fatalf("SetTargets() shrink error = %v", err) } } @@ -175,42 +178,50 @@ func serviceAccountProjectID(t *testing.T, serviceAccountJSON []byte) string { return key.ProjectID } -func createIntegrationLoadBalancer(t *testing.T, client Client, networkID string, target LoadBalancerTargetInput) *LoadBalancer { +type integrationLBScope struct { + Client + spec loadbalancer.LoadBalancerSpec +} + +func (s *integrationLBScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec { + return s.spec +} + +func createIntegrationLoadBalancer(t *testing.T, client Client, networkID string, target LoadBalancerTargetInput) string { t.Helper() name := fmt.Sprintf("capistackit-it-%d", time.Now().UnixNano()) - loadBalancer, err := client.EnsureAPIServerLoadBalancer(context.Background(), LoadBalancerInput{ - Name: name, - Region: integrationRegion(), - NetworkID: networkID, - Port: 6443, - Targets: []LoadBalancerTargetInput{target}, - Tags: map[string]string{ - "capistackit-test": "true", - "capistackit-name": name, + var lbID string + scope := &integrationLBScope{ + Client: client, + spec: loadbalancer.LoadBalancerSpec{ + Name: name, + NetworkID: networkID, + Port: 6443, + SeedTargets: []LoadBalancerTargetInput{target}, + Tags: map[string]string{ + "capistackit-test": "true", + "capistackit-name": name, + }, + SetID: func(id string) { + lbID = id + }, }, - }) - if err != nil { - t.Fatalf("EnsureAPIServerLoadBalancer() error = %v", err) } - if loadBalancer == nil || loadBalancer.ID == "" { - t.Fatalf("EnsureAPIServerLoadBalancer() = %#v, want load balancer with ID", loadBalancer) + if err := loadbalancer.New(scope).Reconcile(context.Background()); err != nil { + t.Fatalf("loadbalancer.Reconcile() error = %v", err) + } + if lbID == "" { + t.Fatal("loadbalancer.Reconcile() did not record a load balancer ID") } t.Cleanup(func() { - if err := client.DeleteAPIServerLoadBalancer(context.Background(), loadBalancer.ID); err != nil && + if err := loadbalancer.NewClient(client).DeleteByID(context.Background(), lbID); err != nil && !IsNotFound(err) && !errors.Is(err, ErrInvalidInput) { - t.Logf("DeleteAPIServerLoadBalancer() cleanup error = %v", err) + t.Logf("DeleteByID() cleanup error = %v", err) } }) - return loadBalancer -} - -func integrationRegion() string { - if region := os.Getenv(envIntegrationRegion); region != "" { - return region - } - return "eu01" + return lbID } func integrationInitialTargetIP(targetIP string) string { diff --git a/cloud/sdk_client_test.go b/cloud/sdk_client_test.go index 16eef07..0671dad 100644 --- a/cloud/sdk_client_test.go +++ b/cloud/sdk_client_test.go @@ -16,7 +16,6 @@ import ( "net/http" "net/http/httptest" "strings" - "sync" "testing" ) @@ -28,7 +27,6 @@ const ( testSDKImageID = "22222222-2222-4222-8222-222222222222" testSDKSecurityGroup = "44444444-4444-4444-8444-444444444444" testBoolTrue = "true" - testSDKPublicIP = "203.0.113.10" ) func TestSDKClientCreateServerUsesExpectedPayload(t *testing.T) { @@ -111,193 +109,6 @@ func assertFieldAbsent(t *testing.T, payload map[string]any, key string) { } } -func TestSDKClientEnsureAPIServerLoadBalancerCreatesExpectedPayload(t *testing.T) { - var createPayload map[string]any - server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - switch { - case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/load-balancers"): - writeJSON(t, w, map[string]any{"loadBalancers": []any{}}) - case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/load-balancers"): - createPayload = readJSON(t, r) - writeJSON(t, w, sdkLoadBalancerJSON("apiserver-test", "203.0.113.10", []any{ - map[string]any{"name": apiserverTargetPoolName, "targetPort": 6443, "targets": []any{ - map[string]any{"displayName": "cp-0", "ip": "10.0.0.10"}, - }}, - })) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - } - })) - - client := newTestSDKClient(t, server.URL) - loadBalancer, err := client.EnsureAPIServerLoadBalancer(context.Background(), LoadBalancerInput{ - Name: "apiserver-test", - Region: testSDKRegion, - NetworkID: testSDKNetworkID, - Port: 6443, - Targets: []LoadBalancerTargetInput{{ - Name: "cp-0", - IP: "10.0.0.10", - }}, - Tags: map[string]string{ - "cluster.x-k8s.io/cluster-name": "test", - "cluster-api-provider-stackit/test-id": "run-1", - }, - }) - if err != nil { - t.Fatalf("EnsureAPIServerLoadBalancer() error = %v", err) - } - if loadBalancer.ID != "apiserver-test" || loadBalancer.IP != testSDKPublicIP || loadBalancer.Port != 6443 { - t.Fatalf("EnsureAPIServerLoadBalancer() = %#v", loadBalancer) - } - - assertStringField(t, createPayload, "name", "apiserver-test") - assertStringField(t, createPayload, "region", testSDKRegion) - assertNestedStringField(t, createPayload, []string{"labels", "cluster.x-k8s.io.cluster-name"}, "test") - assertNestedStringField(t, createPayload, []string{"labels", "cluster-api-provider-stackit.test-id"}, "run-1") - assertNestedBoolField(t, createPayload, []string{"options", "ephemeralAddress"}, true) - assertNestedStringField(t, createPayload, []string{"networks", "0", "networkId"}, testSDKNetworkID) - assertNestedStringField(t, createPayload, []string{"networks", "0", "role"}, "ROLE_LISTENERS_AND_TARGETS") - assertNestedStringField(t, createPayload, []string{"targetPools", "0", "name"}, apiserverTargetPoolName) - assertNestedNumberField(t, createPayload, []string{"targetPools", "0", "targetPort"}, 6443) - assertNestedStringField(t, createPayload, []string{"targetPools", "0", "targets", "0", "displayName"}, "cp-0") - assertNestedStringField(t, createPayload, []string{"targetPools", "0", "targets", "0", "ip"}, "10.0.0.10") - assertNestedStringField(t, createPayload, []string{"listeners", "0", "targetPool"}, apiserverTargetPoolName) - assertNestedNumberField(t, createPayload, []string{"listeners", "0", "port"}, 6443) -} - -func TestSDKClientEnsureAPIServerLoadBalancerUsesBootstrapTargetWhenInitialTargetsAreEmpty(t *testing.T) { - var createPayload map[string]any - server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - switch { - case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/load-balancers"): - writeJSON(t, w, map[string]any{"loadBalancers": []any{}}) - case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/load-balancers"): - createPayload = readJSON(t, r) - writeJSON(t, w, sdkLoadBalancerJSON("apiserver-test", "203.0.113.10", []any{ - map[string]any{"name": apiserverTargetPoolName, "targetPort": 6443, "targets": []any{ - map[string]any{"displayName": bootstrapTargetName, "ip": bootstrapTargetIP}, - }}, - })) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - } - })) - - client := newTestSDKClient(t, server.URL) - loadBalancer, err := client.EnsureAPIServerLoadBalancer(context.Background(), LoadBalancerInput{ - Name: "apiserver-test", - Region: testSDKRegion, - NetworkID: testSDKNetworkID, - Port: 6443, - Tags: map[string]string{ - "cluster.x-k8s.io/cluster-name": "test", - }, - }) - if err != nil { - t.Fatalf("EnsureAPIServerLoadBalancer() error = %v", err) - } - if loadBalancer.ID != "apiserver-test" || loadBalancer.IP != "203.0.113.10" || loadBalancer.Port != 6443 { - t.Fatalf("EnsureAPIServerLoadBalancer() = %#v", loadBalancer) - } - - assertNestedStringField( - t, - createPayload, - []string{"targetPools", "0", "targets", "0", "displayName"}, - bootstrapTargetName, - ) - assertNestedStringField(t, createPayload, []string{"targetPools", "0", "targets", "0", "ip"}, bootstrapTargetIP) - assertNestedStringField(t, createPayload, []string{"listeners", "0", "targetPool"}, apiserverTargetPoolName) -} - -func TestSDKClientSetsAPIServerTargetPool(t *testing.T) { - var mu sync.Mutex - targets := []any{ - map[string]any{"displayName": "cp-0", "ip": "10.0.0.10"}, - } - var updatePayloads []map[string]any - - server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - mu.Lock() - defer mu.Unlock() - - switch { - case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/load-balancers/apiserver-test"): - writeJSON(t, w, sdkLoadBalancerJSON("apiserver-test", "203.0.113.10", []any{ - map[string]any{"name": apiserverTargetPoolName, "targetPort": 6443, "targets": targets}, - })) - case r.Method == http.MethodPut && - strings.HasSuffix(r.URL.Path, "/load-balancers/apiserver-test/target-pools/"+apiserverTargetPoolName): - payload := readJSON(t, r) - updatePayloads = append(updatePayloads, payload) - next, ok := lookup(payload, "targets").([]any) - if !ok { - t.Fatalf("update target payload missing targets: %#v", payload) - } - targets = next - writeJSON(t, w, map[string]any{ - "name": apiserverTargetPoolName, - "targetPort": 6443, - "targets": targets, - }) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - } - })) - - client := newTestSDKClient(t, server.URL) - desired := []LoadBalancerTargetInput{ - {Name: "cp-0", IP: "10.0.0.10"}, - {Name: "cp-1", IP: "10.0.0.11"}, - } - if err := client.SetAPIServerLoadBalancerTargets(context.Background(), "apiserver-test", 6443, desired); err != nil { - t.Fatalf("SetAPIServerLoadBalancerTargets() error = %v", err) - } - if len(updatePayloads) != 1 { - t.Fatalf("got %d update payloads, want 1", len(updatePayloads)) - } - assertNestedStringField(t, updatePayloads[0], []string{"targets", "0", "displayName"}, "cp-0") - assertNestedStringField(t, updatePayloads[0], []string{"targets", "1", "displayName"}, "cp-1") - assertNestedStringField(t, updatePayloads[0], []string{"targets", "1", "ip"}, "10.0.0.11") - - // An unchanged pool must not turn into a write. - if err := client.SetAPIServerLoadBalancerTargets(context.Background(), "apiserver-test", 6443, desired); err != nil { - t.Fatalf("SetAPIServerLoadBalancerTargets() repeat error = %v", err) - } - if len(updatePayloads) != 1 { - t.Fatalf("got %d update payloads after an unchanged set, want 1", len(updatePayloads)) - } - - if err := client.SetAPIServerLoadBalancerTargets( - context.Background(), - "apiserver-test", - 6443, - desired[:1], - ); err != nil { - t.Fatalf("SetAPIServerLoadBalancerTargets() shrink error = %v", err) - } - if len(updatePayloads) != 2 { - t.Fatalf("got %d update payloads, want 2", len(updatePayloads)) - } - if got := nestedValue(t, updatePayloads[1], []string{"targets"}).([]any); len(got) != 1 { - t.Fatalf("shrunk target payload targets = %#v, want one remaining target", got) - } - assertNestedStringField(t, updatePayloads[1], []string{"targets", "0", "displayName"}, "cp-0") -} - -func TestSDKClientRejectsEmptyAPIServerTargetPool(t *testing.T) { - server := newSDKTestServer(t, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - })) - - client := newTestSDKClient(t, server.URL) - err := client.SetAPIServerLoadBalancerTargets(context.Background(), "apiserver-test", 6443, nil) - if !IsInvalidInput(err) { - t.Fatalf("SetAPIServerLoadBalancerTargets() error = %v, want ErrInvalidInput", err) - } -} - func TestSDKClientClassifiesHTTPStatusCodes(t *testing.T) { tests := []struct { name string @@ -359,17 +170,6 @@ func newTestSDKClient(t *testing.T, endpoint string) *SDKClient { return sdkClient } -func sdkLoadBalancerJSON(name, externalAddress string, targetPools []any) map[string]any { - return map[string]any{ - "name": name, - "externalAddress": externalAddress, - "listeners": []any{ - map[string]any{"displayName": apiserverListenerName, "port": 6443, "targetPool": apiserverTargetPoolName}, - }, - "targetPools": targetPools, - } -} - func readJSON(t *testing.T, r *http.Request) map[string]any { t.Helper() @@ -410,13 +210,6 @@ func assertNestedStringField(t *testing.T, m map[string]any, path []string, want } } -func assertNestedBoolField(t *testing.T, m map[string]any, path []string, want bool) { - t.Helper() - if got := nestedValue(t, m, path); got != want { - t.Fatalf("%s = %#v, want %t in %#v", strings.Join(path, "."), got, want, m) - } -} - func assertNestedNumberField(t *testing.T, m map[string]any, path []string, want float64) { t.Helper() if got := nestedValue(t, m, path); got != want { diff --git a/cloud/services/loadbalancer/loadbalancer.go b/cloud/services/loadbalancer/loadbalancer.go deleted file mode 100644 index b03a355..0000000 --- a/cloud/services/loadbalancer/loadbalancer.go +++ /dev/null @@ -1,144 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -package loadbalancer - -import ( - "context" - "crypto/sha256" - "encoding/hex" - "fmt" - "slices" - "strings" - - clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" - - infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud" - "github.com/stackitcloud/cluster-api-provider-stackit/util" -) - -const ( - defaultAPIServerPort int32 = 6443 - - // maxTargetNameLength is the RFC 1123 label length, which STACKIT enforces on - // a target display name. - maxTargetNameLength = 63 - - targetNameDigestLength = 7 -) - -func APIServerTags(stackitCluster *infrav1.StackitCluster) map[string]string { - return util.ClusterTags(stackitCluster.Name, stackitCluster.Namespace, stackitCluster.Spec.AdditionalLabels) -} - -func APIServerInput( - stackitCluster *infrav1.StackitCluster, - targets []cloud.LoadBalancerTargetInput, -) cloud.LoadBalancerInput { - return cloud.LoadBalancerInput{ - Name: stackitCluster.Name + "-apiserver", - ProjectID: stackitCluster.Spec.ProjectID, - Region: stackitCluster.Spec.Region, - NetworkID: stackitCluster.Spec.Network.ID, - Port: defaultAPIServerPort, - Tags: APIServerTags(stackitCluster), - Targets: targets, - } -} - -// BootstrapTarget seeds a new load balancer; STACKIT rejects an empty pool. -func BootstrapTarget(ip string) cloud.LoadBalancerTargetInput { - return cloud.LoadBalancerTargetInput{ - Name: "capi-bootstrap-placeholder", - IP: ip, - } -} - -// APIServerTargets builds the desired API server target pool, sorted by machine -// name so the pool can be compared without spurious updates. The result is empty -// while no control plane machine has an internal IP yet. -func APIServerTargets(machines []*clusterv1.Machine) []cloud.LoadBalancerTargetInput { - sorted := make([]*clusterv1.Machine, 0, len(machines)) - for _, machine := range machines { - if machine != nil { - sorted = append(sorted, machine) - } - } - slices.SortFunc(sorted, func(a, b *clusterv1.Machine) int { - return strings.Compare(a.Name, b.Name) - }) - - targets := make([]cloud.LoadBalancerTargetInput, 0, len(sorted)) - // A target IP must be unique within the pool, and a replacement machine can - // transiently report the IP of the one it replaces. - seen := make(map[string]struct{}, len(sorted)) - for _, machine := range sorted { - ip := firstInternalIP(machine.Status.Addresses) - if ip == "" { - continue - } - if _, duplicate := seen[ip]; duplicate { - continue - } - seen[ip] = struct{}{} - targets = append(targets, cloud.LoadBalancerTargetInput{Name: targetName(machine.Name), IP: ip}) - } - return targets -} - -// targetName maps a machine name, an RFC 1123 subdomain, onto a STACKIT target -// display name, an RFC 1123 label. A name that already is a label is returned -// unchanged; any other carries a digest so that two machines cannot collapse -// onto one target. -func targetName(machineName string) string { - if len(machineName) <= maxTargetNameLength && !strings.Contains(machineName, ".") { - return machineName - } - - sum := sha256.Sum256([]byte(machineName)) - suffix := "-" + hex.EncodeToString(sum[:])[:targetNameDigestLength] - name := strings.ReplaceAll(machineName, ".", "-") - if len(name) > maxTargetNameLength-len(suffix) { - name = strings.TrimRight(name[:maxTargetNameLength-len(suffix)], "-") - } - return name + suffix -} - -func ResolveID( - ctx context.Context, - cloudClient cloud.Client, - stackitCluster *infrav1.StackitCluster, -) (string, error) { - if stackitCluster.Status.APIServerLoadBalancerID != "" { - return stackitCluster.Status.APIServerLoadBalancerID, nil - } - - loadBalancers, err := cloudClient.ListAPIServerLoadBalancersByTags(ctx, APIServerTags(stackitCluster)) - if err != nil { - return "", err - } - if len(loadBalancers) == 0 { - return "", nil - } - if len(loadBalancers) > 1 { - return "", fmt.Errorf("multiple API server load balancers match cluster tags: %w", cloud.ErrConflict) - } - return loadBalancers[0].ID, nil -} - -func firstInternalIP(addresses []clusterv1.MachineAddress) string { - for _, address := range addresses { - if address.Type == clusterv1.MachineInternalIP && address.Address != "" { - return address.Address - } - } - return "" -} diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go new file mode 100644 index 0000000..4ecc124 --- /dev/null +++ b/cloud/services/loadbalancer/reconcile.go @@ -0,0 +1,273 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package loadbalancer + +import ( + "context" + "fmt" + "strings" + + lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" + logf "sigs.k8s.io/controller-runtime/pkg/log" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +func (s *Service) Reconcile(ctx context.Context) error { + log := logf.FromContext(ctx) + spec := s.scope.LoadBalancerSpec() + if err := spec.validate(); err != nil { + return err + } + + loadBalancer, err := s.ensureLoadBalancer(ctx, spec) + if err != nil { + return err + } + if spec.SetID != nil { + spec.SetID(loadBalancer.ID) + } + if spec.SetAddress != nil { + spec.SetAddress(loadBalancer.IP, spec.Port) + } + if loadBalancer.IP == "" { + return nil + } + + // An empty set never overwrites a populated pool: cluster deletion filters + // out every machine while the API server is still needed for drain. + if len(spec.Targets) > 0 { + if err := s.SetTargets(ctx, loadBalancer.ID, spec.Port, spec.Targets); err != nil { + return fmt.Errorf("%w: %w", ErrTargetPoolUpdate, err) + } + log.V(1).Info("Reconciled API server load balancer target pool", "targets", len(spec.Targets)) + } + log.V(1).Info("Reconciled load balancer resources", "loadBalancerID", loadBalancer.ID) + return nil +} + +func (s *Service) Delete(ctx context.Context) error { + log := logf.FromContext(ctx) + spec := s.scope.LoadBalancerSpec() + if spec.ID == "" && len(spec.Tags) == 0 { + return fmt.Errorf("%w: load balancer ID or tags are required", shared.ErrInvalidInput) + } + + loadBalancerID, err := s.resolveLoadBalancerForDelete(ctx, spec) + if err != nil { + return err + } + if loadBalancerID != "" { + if err := s.DeleteByID(ctx, loadBalancerID); err != nil { + return err + } + log.V(1).Info("Deleted load balancer", "loadBalancerID", loadBalancerID) + } + if spec.SetID != nil { + spec.SetID("") + } + return nil +} + +// FindByTags finds a single STACKIT load balancer matching the given tag selector. +func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*LoadBalancer, error) { + matched, err := c.ListByTags(ctx, tags) + if err != nil { + return nil, err + } + if len(matched) == 0 { + return nil, fmt.Errorf("%w: no load balancer matching tags", shared.ErrNotFound) + } + if len(matched) > 1 { + return nil, fmt.Errorf("%w: multiple load balancers matching tags", shared.ErrConflict) + } + return matched[0], nil +} + +// ListByTags lists STACKIT load balancers matching the given tag selector. +func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*LoadBalancer, error) { + if len(tags) == 0 { + return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) + } + resp, err := c.lbClient.DefaultAPI.ListLoadBalancers(ctx, c.projectID, c.region).Execute() + if err != nil { + err := shared.ClassifySDKError("list load balancers", err) + if isLoadBalancerServiceNotEnabled(err) { + return nil, nil + } + return nil, err + } + matched := []*LoadBalancer{} + labels := Labels(tags) + for _, candidate := range resp.GetLoadBalancers() { + if shared.MapContains(candidate.GetLabels(), labels) { + candidate := candidate + matched = append(matched, loadBalancerFromSDK(&candidate)) + } + } + return matched, nil +} + +// DeleteByID deletes a load balancer by ID, ignoring NotFound errors. +func (c *Client) DeleteByID(ctx context.Context, id string) error { + if id == "" { + return fmt.Errorf("%w: load balancer ID is empty", shared.ErrInvalidInput) + } + if _, err := c.lbClient.DefaultAPI.DeleteLoadBalancer(ctx, c.projectID, c.region, id).Execute(); err != nil { + err := shared.ClassifySDKError("delete load balancer", err) + if !shared.IsNotFound(err) { + return err + } + } + return nil +} + +func (c *Client) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) (*LoadBalancer, error) { + if existing, err := c.FindByTags(ctx, spec.Tags); err == nil { + return existing, nil + } else if !shared.IsNotFound(err) && !isLoadBalancerServiceNotEnabled(err) { + return nil, err + } + + payload := spec.toCreatePayload(c.region) + lbResp, err := c.lbClient.DefaultAPI.CreateLoadBalancer(ctx, c.projectID, c.region). + CreateLoadBalancerPayload(payload). + Execute() + if err != nil { + return nil, shared.ClassifySDKError("create load balancer", err) + } + out := loadBalancerFromSDK(lbResp) + logf.FromContext(ctx).V(1).Info("Created load balancer", "name", spec.Name, "loadBalancerID", out.ID) + return out, nil +} + +func (c *Client) resolveLoadBalancerForDelete(ctx context.Context, spec LoadBalancerSpec) (string, error) { + if spec.ID != "" { + exists, err := c.loadBalancerExists(ctx, spec.ID) + if err != nil || exists { + return spec.ID, err + } + } + if len(spec.Tags) == 0 { + return "", nil + } + loadBalancer, err := c.FindByTags(ctx, spec.Tags) + if shared.IsNotFound(err) { + return "", nil + } + if err != nil { + return "", err + } + return loadBalancer.ID, nil +} + +func (c *Client) loadBalancerExists(ctx context.Context, id string) (bool, error) { + _, err := c.lbClient.DefaultAPI.GetLoadBalancer(ctx, c.projectID, c.region, id).Execute() + if err != nil { + err := shared.ClassifySDKError("get load balancer", err) + if shared.IsNotFound(err) { + return false, nil + } + return false, err + } + return true, nil +} + +func (s LoadBalancerSpec) validate() error { + if s.Name == "" { + return fmt.Errorf("%w: load balancer name is required", shared.ErrInvalidInput) + } + if s.NetworkID == "" { + return fmt.Errorf("%w: load balancer network ID is required", shared.ErrInvalidInput) + } + if s.Port <= 0 { + return fmt.Errorf("%w: load balancer port must be positive", shared.ErrInvalidInput) + } + if len(s.Tags) == 0 { + return fmt.Errorf("%w: load balancer tags are required", shared.ErrInvalidInput) + } + return nil +} + +func (s LoadBalancerSpec) toCreatePayload(region string) lb.CreateLoadBalancerPayload { + payload := lb.NewCreateLoadBalancerPayload() + payload.SetName(s.Name) + payload.SetRegion(region) + if len(s.Tags) > 0 { + payload.SetLabels(Labels(s.Tags)) + } + options := lb.NewLoadBalancerOptions() + options.SetEphemeralAddress(true) + payload.SetOptions(*options) + + network := lb.NewNetwork() + network.SetNetworkId(s.NetworkID) + network.SetRole("ROLE_LISTENERS_AND_TARGETS") + payload.SetNetworks([]lb.Network{*network}) + + targetPool := lb.NewTargetPool() + targetPool.SetName(apiserverTargetPoolName) + targetPool.SetTargetPort(s.Port) + seed := s.SeedTargets + if len(seed) == 0 { + seed = s.Targets + } + targets := make([]lb.Target, 0, max(1, len(seed))) + if len(seed) == 0 { + target := lb.NewTarget() + target.SetDisplayName(bootstrapTargetName) + target.SetIp(bootstrapTargetIP) + targets = append(targets, *target) + } else { + for _, targetInput := range seed { + target := lb.NewTarget() + target.SetDisplayName(targetInput.Name) + target.SetIp(targetInput.IP) + targets = append(targets, *target) + } + } + targetPool.SetTargets(targets) + payload.SetTargetPools([]lb.TargetPool{*targetPool}) + + listener := lb.NewListener() + listener.SetDisplayName(apiserverListenerName) + listener.SetPort(s.Port) + listener.SetProtocol("PROTOCOL_TCP") + listener.SetTargetPool(apiserverTargetPoolName) + payload.SetListeners([]lb.Listener{*listener}) + + return *payload +} + +func loadBalancerFromSDK(in *lb.LoadBalancer) *LoadBalancer { + if in == nil { + return nil + } + return &LoadBalancer{ + ID: in.GetName(), + Name: in.GetName(), + IP: shared.FirstNonEmpty(in.GetExternalAddress(), in.GetPrivateAddress()), + Port: listenerPort(in.GetListeners()), + } +} + +func listenerPort(listeners []lb.Listener) int32 { + for _, listener := range listeners { + if port := listener.GetPort(); port > 0 { + return port + } + } + return 0 +} + +func isLoadBalancerServiceNotEnabled(err error) bool { + return shared.IsUnauthorized(err) && strings.Contains(err.Error(), "Service not enabled") +} diff --git a/cloud/services/loadbalancer/reconcile_test.go b/cloud/services/loadbalancer/reconcile_test.go new file mode 100644 index 0000000..81e7c1a --- /dev/null +++ b/cloud/services/loadbalancer/reconcile_test.go @@ -0,0 +1,597 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package loadbalancer_test + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +const ( + testProjectID = "00000000-0000-0000-0000-000000000000" + testRegion = "eu01" + testNetworkID = "11111111-1111-4111-8111-111111111111" + testLBName = "test-cluster-apiserver" + testPublicIP = "203.0.113.10" +) + +type testScope struct { + projectID string + region string + lbClient *lb.APIClient + spec loadbalancer.LoadBalancerSpec +} + +func (s *testScope) ProjectID() string { + return s.projectID +} + +func (s *testScope) Region() string { + return s.region +} + +func (s *testScope) LoadBalancerClient() *lb.APIClient { + return s.lbClient +} + +func (s *testScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec { + return s.spec +} + +func newTestLBClient(t *testing.T, handler http.Handler) *lb.APIClient { + t.Helper() + + server := httptest.NewServer(handler) + t.Cleanup(server.Close) + + lbClient, err := lb.NewAPIClient( + config.WithoutAuthentication(), + config.WithEndpoint(server.URL), + ) + if err != nil { + t.Fatalf("lb.NewAPIClient() error = %v", err) + } + return lbClient +} + +func newTestService(t *testing.T, spec loadbalancer.LoadBalancerSpec, handler http.Handler) *loadbalancer.Service { + t.Helper() + + return loadbalancer.New(&testScope{ + projectID: testProjectID, + region: testRegion, + lbClient: newTestLBClient(t, handler), + spec: spec, + }) +} + +func writeJSON(t *testing.T, w http.ResponseWriter, value any) { + t.Helper() + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(value); err != nil { + t.Fatalf("encode response body: %v", err) + } +} + +func readJSON(t *testing.T, r *http.Request) map[string]any { + t.Helper() + var out map[string]any + if err := json.NewDecoder(r.Body).Decode(&out); err != nil { + t.Fatalf("decode request body: %v", err) + } + return out +} + +func sdkLoadBalancerJSON(name string, labels map[string]any, targetPools []any) map[string]any { + m := map[string]any{ + "name": name, + "externalAddress": testPublicIP, + "listeners": []any{ + map[string]any{"displayName": "apiserver", "port": 6443, "targetPool": "apiserver"}, + }, + "targetPools": targetPools, + } + if len(labels) > 0 { + m["labels"] = labels + } + return m +} + +func TestServiceReconcile(t *testing.T) { + t.Run("creates load balancer with expected payload and updates target pool", func(t *testing.T) { + var ( + createPayload map[string]any + recordedID string + recordedIP string + recordedPort int32 + ) + spec := loadbalancer.LoadBalancerSpec{ + Name: testLBName, + NetworkID: testNetworkID, + Port: loadbalancer.DefaultAPIServerPort, + Tags: map[string]string{ + "cluster.x-k8s.io/cluster-name": "test", + "cluster-api-provider-stackit/test-id": "run-1", + }, + Targets: []loadbalancer.Target{ + {Name: "cp-0", IP: "10.0.0.10"}, + }, + SeedTargets: []loadbalancer.Target{ + {Name: "cp-0", IP: "10.0.0.10"}, + }, + SetID: func(id string) { + recordedID = id + }, + SetAddress: func(ip string, port int32) { + recordedIP = ip + recordedPort = port + }, + } + + targetPool := []any{ + map[string]any{ + "name": "apiserver", + "targetPort": 6443, + "targets": []any{ + map[string]any{"displayName": "cp-0", "ip": "10.0.0.10"}, + }, + }, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers"): + writeJSON(t, w, map[string]any{"loadBalancers": []any{}}) + case r.Method == http.MethodPost && strings.HasSuffix(path, "/load-balancers"): + createPayload = readJSON(t, r) + writeJSON(t, w, sdkLoadBalancerJSON(testLBName, nil, targetPool)) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers/"+testLBName): + writeJSON(t, w, sdkLoadBalancerJSON(testLBName, nil, targetPool)) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + + if recordedID != testLBName { + t.Fatalf("recordedID = %q, want %q", recordedID, testLBName) + } + if recordedIP != testPublicIP || recordedPort != 6443 { + t.Fatalf("recorded address = %s:%d, want %s:6443", recordedIP, recordedPort, testPublicIP) + } + if createPayload["name"] != testLBName { + t.Fatalf("payload name = %v, want %q", createPayload["name"], testLBName) + } + if createPayload["region"] != testRegion { + t.Fatalf("payload region = %v, want %q", createPayload["region"], testRegion) + } + labels, _ := createPayload["labels"].(map[string]any) + if labels["cluster.x-k8s.io.cluster-name"] != "test" || labels["cluster-api-provider-stackit.test-id"] != "run-1" { + t.Fatalf("payload labels = %#v", labels) + } + }) + + t.Run("uses bootstrap placeholder target when initial targets are empty", func(t *testing.T) { + var createPayload map[string]any + spec := loadbalancer.LoadBalancerSpec{ + Name: testLBName, + NetworkID: testNetworkID, + Port: loadbalancer.DefaultAPIServerPort, + Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers"): + writeJSON(t, w, map[string]any{"loadBalancers": []any{}}) + case r.Method == http.MethodPost && strings.HasSuffix(path, "/load-balancers"): + createPayload = readJSON(t, r) + writeJSON(t, w, sdkLoadBalancerJSON(testLBName, nil, []any{ + map[string]any{ + "name": "apiserver", + "targetPort": 6443, + "targets": []any{ + map[string]any{"displayName": "capi-bootstrap-placeholder", "ip": "10.0.0.1"}, + }, + }, + })) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + + pools, _ := createPayload["targetPools"].([]any) + if len(pools) != 1 { + t.Fatalf("targetPools = %#v, want 1 pool", pools) + } + pool, _ := pools[0].(map[string]any) + targets, _ := pool["targets"].([]any) + if len(targets) != 1 { + t.Fatalf("targets = %#v, want 1 target", targets) + } + target, _ := targets[0].(map[string]any) + if target["displayName"] != "capi-bootstrap-placeholder" || target["ip"] != "10.0.0.1" { + t.Fatalf("target = %#v, want placeholder", target) + } + }) +} + +func TestServiceReconcileReuseAndErrors(t *testing.T) { + t.Run("reuses existing load balancer found by tags without creating duplicate", func(t *testing.T) { + var recordedID string + spec := loadbalancer.LoadBalancerSpec{ + Name: testLBName, + NetworkID: testNetworkID, + Port: loadbalancer.DefaultAPIServerPort, + Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, + SetID: func(id string) { + recordedID = id + }, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/load-balancers") { + writeJSON(t, w, map[string]any{"loadBalancers": []any{ + sdkLoadBalancerJSON(testLBName, map[string]any{ + "cluster.x-k8s.io.cluster-name": "test", + }, nil), + }}) + return + } + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if recordedID != testLBName { + t.Fatalf("recordedID = %q, want %q", recordedID, testLBName) + } + }) + + t.Run("returns ErrConflict when multiple load balancers match tags", func(t *testing.T) { + spec := loadbalancer.LoadBalancerSpec{ + Name: testLBName, + NetworkID: testNetworkID, + Port: loadbalancer.DefaultAPIServerPort, + Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + writeJSON(t, w, map[string]any{"loadBalancers": []any{ + sdkLoadBalancerJSON("lb-1", map[string]any{"cluster.x-k8s.io.cluster-name": "test"}, nil), + sdkLoadBalancerJSON("lb-2", map[string]any{"cluster.x-k8s.io.cluster-name": "test"}, nil), + }}) + })) + + err := svc.Reconcile(context.Background()) + if !errors.Is(err, shared.ErrConflict) { + t.Fatalf("Reconcile() error = %v, want ErrConflict", err) + } + }) + + t.Run("wraps target pool update failures in ErrTargetPoolUpdate after setting ID and address", func(t *testing.T) { + var ( + recordedID string + recordedIP string + ) + spec := loadbalancer.LoadBalancerSpec{ + Name: testLBName, + NetworkID: testNetworkID, + Port: loadbalancer.DefaultAPIServerPort, + Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, + Targets: []loadbalancer.Target{{Name: "cp-0", IP: "10.0.0.10"}}, + SetID: func(id string) { + recordedID = id + }, + SetAddress: func(ip string, _ int32) { + recordedIP = ip + }, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers"): + writeJSON(t, w, map[string]any{"loadBalancers": []any{ + sdkLoadBalancerJSON(testLBName, map[string]any{ + "cluster.x-k8s.io.cluster-name": "test", + }, nil), + }}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers/"+testLBName): + w.WriteHeader(http.StatusInternalServerError) + writeJSON(t, w, map[string]any{"message": "backend error"}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + err := svc.Reconcile(context.Background()) + if !loadbalancer.IsTargetPoolUpdateError(err) { + t.Fatalf("Reconcile() error = %v, want IsTargetPoolUpdateError", err) + } + if !shared.IsTransient(err) { + t.Fatalf("Reconcile() error = %v, want wrapped ErrTransient", err) + } + if recordedID != testLBName || recordedIP != testPublicIP { + t.Fatalf("recordedID=%q recordedIP=%q, want both set before target error", recordedID, recordedIP) + } + }) +} + +func TestSetTargets(t *testing.T) { + var mu sync.Mutex + targets := []any{ + map[string]any{"displayName": "cp-0", "ip": "10.0.0.10"}, + } + var updatePayloads []map[string]any + + svc := newTestService(t, loadbalancer.LoadBalancerSpec{}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + defer mu.Unlock() + + switch { + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/load-balancers/"+testLBName): + writeJSON(t, w, sdkLoadBalancerJSON(testLBName, nil, []any{ + map[string]any{"name": "apiserver", "targetPort": 6443, "targets": targets}, + })) + case r.Method == http.MethodPut && strings.HasSuffix(r.URL.Path, "/load-balancers/"+testLBName+"/target-pools/apiserver"): + payload := readJSON(t, r) + updatePayloads = append(updatePayloads, payload) + next, ok := payload["targets"].([]any) + if !ok { + t.Fatalf("update target payload missing targets: %#v", payload) + } + targets = next + writeJSON(t, w, map[string]any{ + "name": "apiserver", + "targetPort": 6443, + "targets": targets, + }) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + desired := []loadbalancer.Target{ + {Name: "cp-0", IP: "10.0.0.10"}, + {Name: "cp-1", IP: "10.0.0.11"}, + } + if err := svc.SetTargets(context.Background(), testLBName, 6443, desired); err != nil { + t.Fatalf("SetTargets() error = %v", err) + } + if len(updatePayloads) != 1 { + t.Fatalf("got %d update payloads, want 1", len(updatePayloads)) + } + + // Unchanged pool must not trigger another write. + if err := svc.SetTargets(context.Background(), testLBName, 6443, desired); err != nil { + t.Fatalf("SetTargets() repeat error = %v", err) + } + if len(updatePayloads) != 1 { + t.Fatalf("got %d update payloads after unchanged set, want 1", len(updatePayloads)) + } + + // Shrinking pool updates targets. + if err := svc.SetTargets(context.Background(), testLBName, 6443, desired[:1]); err != nil { + t.Fatalf("SetTargets() shrink error = %v", err) + } + if len(updatePayloads) != 2 { + t.Fatalf("got %d update payloads, want 2", len(updatePayloads)) + } + + // Empty pool is rejected. + if err := svc.SetTargets(context.Background(), testLBName, 6443, nil); !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("SetTargets(nil) error = %v, want ErrInvalidInput", err) + } +} + +func TestServiceReconcileValidation(t *testing.T) { + tests := []struct { + name string + spec loadbalancer.LoadBalancerSpec + }{ + { + name: "missing name", + spec: loadbalancer.LoadBalancerSpec{ + NetworkID: testNetworkID, + Port: 6443, + Tags: map[string]string{"cluster": "test"}, + }, + }, + { + name: "missing network ID", + spec: loadbalancer.LoadBalancerSpec{ + Name: testLBName, + Port: 6443, + Tags: map[string]string{"cluster": "test"}, + }, + }, + { + name: "invalid port", + spec: loadbalancer.LoadBalancerSpec{ + Name: testLBName, + NetworkID: testNetworkID, + Tags: map[string]string{"cluster": "test"}, + }, + }, + { + name: "missing tags", + spec: loadbalancer.LoadBalancerSpec{ + Name: testLBName, + NetworkID: testNetworkID, + Port: 6443, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + svc := newTestService(t, tt.spec, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + err := svc.Reconcile(context.Background()) + if !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("Reconcile() error = %v, want ErrInvalidInput", err) + } + }) + } +} + +func TestServiceDelete(t *testing.T) { + t.Run("deletes load balancer using ID when provided and clears ID", func(t *testing.T) { + var ( + deletedID string + clearedID = "not-cleared" + ) + spec := loadbalancer.LoadBalancerSpec{ + ID: testLBName, + SetID: func(id string) { + clearedID = id + }, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers/"+testLBName): + writeJSON(t, w, sdkLoadBalancerJSON(testLBName, nil, nil)) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/load-balancers/"+testLBName): + deletedID = testLBName + writeJSON(t, w, map[string]any{}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v", err) + } + if deletedID != testLBName { + t.Fatalf("deletedID = %q, want %q", deletedID, testLBName) + } + if clearedID != "" { + t.Fatalf("clearedID = %q, want empty", clearedID) + } + }) + + t.Run("falls back to tag lookup when ID is empty", func(t *testing.T) { + var deletedID string + spec := loadbalancer.LoadBalancerSpec{ + Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers"): + writeJSON(t, w, map[string]any{"loadBalancers": []any{ + sdkLoadBalancerJSON(testLBName, map[string]any{ + "cluster.x-k8s.io.cluster-name": "test", + }, nil), + }}) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/load-balancers/"+testLBName): + deletedID = testLBName + writeJSON(t, w, map[string]any{}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v", err) + } + if deletedID != testLBName { + t.Fatalf("deletedID = %q, want %q", deletedID, testLBName) + } + }) + + t.Run("falls back to tag lookup when recorded ID no longer exists", func(t *testing.T) { + var deletedID string + spec := loadbalancer.LoadBalancerSpec{ + ID: "stale-lb", + Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers/stale-lb"): + w.WriteHeader(http.StatusNotFound) + writeJSON(t, w, map[string]any{"message": "not found"}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers"): + writeJSON(t, w, map[string]any{"loadBalancers": []any{ + sdkLoadBalancerJSON(testLBName, map[string]any{ + "cluster.x-k8s.io.cluster-name": "test", + }, nil), + }}) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/load-balancers/"+testLBName): + deletedID = testLBName + writeJSON(t, w, map[string]any{}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v", err) + } + if deletedID != testLBName { + t.Fatalf("deletedID = %q, want %q", deletedID, testLBName) + } + }) + + t.Run("succeeds when load balancer does not exist", func(t *testing.T) { + spec := loadbalancer.LoadBalancerSpec{ + Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + writeJSON(t, w, map[string]any{"loadBalancers": []any{}}) + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v, want nil", err) + } + }) + + t.Run("returns ErrInvalidInput when both ID and tags are empty", func(t *testing.T) { + svc := newTestService(t, loadbalancer.LoadBalancerSpec{}, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + if err := svc.Delete(context.Background()); !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("Delete() error = %v, want ErrInvalidInput", err) + } + }) +} diff --git a/cloud/services/loadbalancer/service.go b/cloud/services/loadbalancer/service.go new file mode 100644 index 0000000..a5d0e8e --- /dev/null +++ b/cloud/services/loadbalancer/service.go @@ -0,0 +1,107 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package loadbalancer + +import ( + "errors" + + lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" +) + +const ( + // DefaultAPIServerPort is the default TCP port for the Kubernetes API server. + DefaultAPIServerPort int32 = 6443 + + apiserverTargetPoolName = "apiserver" + apiserverListenerName = "apiserver" + bootstrapTargetName = "capi-bootstrap-placeholder" + bootstrapTargetIP = "10.0.0.1" +) + +// ErrTargetPoolUpdate indicates that the load balancer was created/resolved +// successfully, but updating its target pool failed. +var ErrTargetPoolUpdate = errors.New("load balancer target pool update failed") + +// IsTargetPoolUpdateError reports whether err wraps ErrTargetPoolUpdate. +func IsTargetPoolUpdateError(err error) bool { + return errors.Is(err, ErrTargetPoolUpdate) +} + +// LoadBalancer describes an API-server load balancer. +type LoadBalancer struct { + ID string + Name string + IP string + DNSName string + Port int32 +} + +// Target describes a VM target in the API-server load balancer target pool. +type Target struct { + Name string + IP string +} + +// LoadBalancerSpec describes the desired state of a STACKIT API server load balancer. +type LoadBalancerSpec struct { + ID string + Name string + NetworkID string + Port int32 + Tags map[string]string + Targets []Target + SeedTargets []Target + SetID func(id string) + SetAddress func(ip string, port int32) +} + +// ClientScope is an interface that provides the LoadBalancer client and target scope for load balancer operations. +type ClientScope interface { + ProjectID() string + Region() string + LoadBalancerClient() *lb.APIClient +} + +// Scope is an interface that provides scope data needed for load balancer reconciliation. +type Scope interface { + ClientScope + LoadBalancerSpec() LoadBalancerSpec +} + +// Client provides direct load balancer operations against the STACKIT Load Balancer API. +type Client struct { + lbClient *lb.APIClient + projectID string + region string +} + +// NewClient returns a Client for the given scope. +func NewClient(scope ClientScope) *Client { + return &Client{ + lbClient: scope.LoadBalancerClient(), + projectID: scope.ProjectID(), + region: scope.Region(), + } +} + +// Service implements load balancer reconciler. +type Service struct { + scope Scope + *Client +} + +// New returns Service from given scope. +func New(scope Scope) *Service { + return &Service{ + scope: scope, + Client: NewClient(scope), + } +} diff --git a/cloud/services/loadbalancer/targets.go b/cloud/services/loadbalancer/targets.go new file mode 100644 index 0000000..c831c41 --- /dev/null +++ b/cloud/services/loadbalancer/targets.go @@ -0,0 +1,213 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package loadbalancer + +import ( + "cmp" + "context" + "crypto/sha256" + "encoding/hex" + "fmt" + "slices" + "strings" + + lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" + clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +const ( + // maxTargetNameLength is the RFC 1123 label length, which STACKIT enforces on + // a target display name. + maxTargetNameLength = 63 + + targetNameDigestLength = 7 +) + +// BootstrapTarget seeds a new load balancer; STACKIT rejects an empty pool. +func BootstrapTarget(ip string) Target { + return Target{ + Name: bootstrapTargetName, + IP: ip, + } +} + +// APIServerTargets builds the desired API server target pool, sorted by machine +// name so the pool can be compared without spurious updates. The result is empty +// while no control plane machine has an internal IP yet. +func APIServerTargets(machines []*clusterv1.Machine) []Target { + sorted := make([]*clusterv1.Machine, 0, len(machines)) + for _, machine := range machines { + if machine != nil { + sorted = append(sorted, machine) + } + } + slices.SortFunc(sorted, func(a, b *clusterv1.Machine) int { + return strings.Compare(a.Name, b.Name) + }) + + targets := make([]Target, 0, len(sorted)) + // A target IP must be unique within the pool, and a replacement machine can + // transiently report the IP of the one it replaces. + seen := make(map[string]struct{}, len(sorted)) + for _, machine := range sorted { + ip := firstInternalIP(machine.Status.Addresses) + if ip == "" { + continue + } + if _, duplicate := seen[ip]; duplicate { + continue + } + seen[ip] = struct{}{} + targets = append(targets, Target{Name: targetName(machine.Name), IP: ip}) + } + return targets +} + +// Labels converts tag keys containing "/" into "." to satisfy STACKIT Load Balancer label rules. +func Labels(tags map[string]string) map[string]string { + out := make(map[string]string, len(tags)) + for k, v := range tags { + out[strings.ReplaceAll(k, "/", ".")] = v + } + return out +} + +// targetName maps a machine name, an RFC 1123 subdomain, onto a STACKIT target +// display name, an RFC 1123 label. A name that already is a label is returned +// unchanged; any other carries a digest so that two machines cannot collapse +// onto one target. +func targetName(machineName string) string { + if len(machineName) <= maxTargetNameLength && !strings.Contains(machineName, ".") { + return machineName + } + + sum := sha256.Sum256([]byte(machineName)) + suffix := "-" + hex.EncodeToString(sum[:])[:targetNameDigestLength] + name := strings.ReplaceAll(machineName, ".", "-") + if len(name) > maxTargetNameLength-len(suffix) { + name = strings.TrimRight(name[:maxTargetNameLength-len(suffix)], "-") + } + return name + suffix +} + +func firstInternalIP(addresses []clusterv1.MachineAddress) string { + for _, address := range addresses { + if address.Type == clusterv1.MachineInternalIP && address.Address != "" { + return address.Address + } + } + return "" +} + +// SetTargets replaces the contents of the API-server target pool. STACKIT rejects an empty pool. +func (c *Client) SetTargets( + ctx context.Context, + loadBalancerID string, + port int32, + targets []Target, +) error { + if loadBalancerID == "" || port <= 0 { + return fmt.Errorf("%w: load balancer ID and target port are required", shared.ErrInvalidInput) + } + // STACKIT NLB target pools must contain at least one target. + if len(targets) == 0 { + return fmt.Errorf("%w: at least one target is required", shared.ErrInvalidInput) + } + desired := make([]lb.Target, 0, len(targets)) + for _, targetInput := range targets { + if targetInput.Name == "" || targetInput.IP == "" { + return fmt.Errorf("%w: target name and target IP are required", shared.ErrInvalidInput) + } + target := lb.NewTarget() + target.SetDisplayName(targetInput.Name) + target.SetIp(targetInput.IP) + desired = append(desired, *target) + } + + loadBalancer, err := c.lbClient.DefaultAPI.GetLoadBalancer(ctx, c.projectID, c.region, loadBalancerID).Execute() + if err != nil { + return shared.ClassifySDKError("get load balancer", err) + } + targetPool := apiServerTargetPool(loadBalancer) + if targetPool == nil { + return fmt.Errorf( + "%w: load balancer %q has no %q target pool", + shared.ErrNotFound, + loadBalancerID, + apiserverTargetPoolName, + ) + } + + // Every cluster reconcile ends up here; skip the update call when the pool + // already matches. + if targetPool.GetTargetPort() == port && sameTargets(targetPool.GetTargets(), desired) { + return nil + } + return c.updateAPIServerTargetPool(ctx, loadBalancerID, targetPool, desired, port) +} + +func (c *Client) updateAPIServerTargetPool( + ctx context.Context, + loadBalancerID string, + current *lb.TargetPool, + targets []lb.Target, + port int32, +) error { + payload := lb.NewUpdateTargetPoolPayload() + payload.SetName(apiserverTargetPoolName) + if port > 0 { + payload.SetTargetPort(port) + } else if current.GetTargetPort() > 0 { + payload.SetTargetPort(current.GetTargetPort()) + } + payload.SetTargets(targets) + if _, err := c.lbClient.DefaultAPI. + UpdateTargetPool(ctx, c.projectID, c.region, loadBalancerID, apiserverTargetPoolName). + UpdateTargetPoolPayload(*payload). + Execute(); err != nil { + return shared.ClassifySDKError("update load balancer target pool", err) + } + return nil +} + +func apiServerTargetPool(loadBalancer *lb.LoadBalancer) *lb.TargetPool { + if loadBalancer == nil { + return nil + } + for _, targetPool := range loadBalancer.GetTargetPools() { + if targetPool.GetName() == apiserverTargetPoolName { + return &targetPool + } + } + return nil +} + +func sameTargets(current, desired []lb.Target) bool { + if len(current) != len(desired) { + return false + } + return slices.Equal(sortedTargetKeys(current), sortedTargetKeys(desired)) +} + +type targetKey struct{ name, ip string } + +func sortedTargetKeys(targets []lb.Target) []targetKey { + keys := make([]targetKey, 0, len(targets)) + for _, target := range targets { + keys = append(keys, targetKey{name: target.GetDisplayName(), ip: target.GetIp()}) + } + slices.SortFunc(keys, func(a, b targetKey) int { + return cmp.Or(strings.Compare(a.name, b.name), strings.Compare(a.ip, b.ip)) + }) + return keys +} diff --git a/cloud/services/loadbalancer/loadbalancer_test.go b/cloud/services/loadbalancer/targets_test.go similarity index 92% rename from cloud/services/loadbalancer/loadbalancer_test.go rename to cloud/services/loadbalancer/targets_test.go index 8e02fe5..839ae13 100644 --- a/cloud/services/loadbalancer/loadbalancer_test.go +++ b/cloud/services/loadbalancer/targets_test.go @@ -19,8 +19,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" - - "github.com/stackitcloud/cluster-api-provider-stackit/cloud" ) func machineWithAddresses(name string, addresses ...clusterv1.MachineAddress) *clusterv1.Machine { @@ -38,7 +36,7 @@ func TestAPIServerTargets(t *testing.T) { tests := []struct { name string machines []*clusterv1.Machine - want []cloud.LoadBalancerTargetInput + want []Target }{ { name: "sorts targets by machine name", @@ -47,7 +45,7 @@ func TestAPIServerTargets(t *testing.T) { machineWithAddresses("cp-0", internalIP("10.0.0.10")), machineWithAddresses("cp-1", internalIP("10.0.0.11")), }, - want: []cloud.LoadBalancerTargetInput{ + want: []Target{ {Name: "cp-0", IP: "10.0.0.10"}, {Name: "cp-1", IP: "10.0.0.11"}, {Name: "cp-2", IP: "10.0.0.12"}, @@ -63,7 +61,7 @@ func TestAPIServerTargets(t *testing.T) { Address: "203.0.113.10", }), }, - want: []cloud.LoadBalancerTargetInput{{Name: "cp-0", IP: "10.0.0.10"}}, + want: []Target{{Name: "cp-0", IP: "10.0.0.10"}}, }, { name: "drops a duplicate IP address", @@ -71,7 +69,7 @@ func TestAPIServerTargets(t *testing.T) { machineWithAddresses("cp-1", internalIP("10.0.0.10")), machineWithAddresses("cp-0", internalIP("10.0.0.10")), }, - want: []cloud.LoadBalancerTargetInput{{Name: "cp-0", IP: "10.0.0.10"}}, + want: []Target{{Name: "cp-0", IP: "10.0.0.10"}}, }, { // The placeholder belongs to the creation path, not here. @@ -82,7 +80,7 @@ func TestAPIServerTargets(t *testing.T) { { name: "tolerates nil entries", machines: []*clusterv1.Machine{nil, machineWithAddresses("cp-0", internalIP("10.0.0.11"))}, - want: []cloud.LoadBalancerTargetInput{{Name: "cp-0", IP: "10.0.0.11"}}, + want: []Target{{Name: "cp-0", IP: "10.0.0.11"}}, }, } diff --git a/cloud/services/shared/utils.go b/cloud/services/shared/utils.go index 430d98b..cf555eb 100644 --- a/cloud/services/shared/utils.go +++ b/cloud/services/shared/utils.go @@ -28,15 +28,6 @@ func MapContains(haystack, needle map[string]string) bool { return true } -// TODO: Move to LB Service -func LoadBalancerLabels(tags map[string]string) map[string]string { - out := make(map[string]string, len(tags)) - for k, v := range tags { - out[strings.ReplaceAll(k, "/", ".")] = v - } - return out -} - func FirstNonEmpty(values ...string) string { for _, value := range values { if value != "" { diff --git a/cloud/types.go b/cloud/types.go index 98b9fda..d9720a2 100644 --- a/cloud/types.go +++ b/cloud/types.go @@ -14,6 +14,7 @@ You may obtain a copy of the License at package cloud import ( + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" @@ -33,13 +34,7 @@ type Network struct { } // LoadBalancer describes an API-server load balancer. -type LoadBalancer struct { - ID string - Name string - IP string - DNSName string - Port int32 -} +type LoadBalancer = loadbalancer.LoadBalancer // PublicIP describes a STACKIT public IP resource. type PublicIP = publicip.PublicIP @@ -95,20 +90,6 @@ type RootVolumeInput struct { DeleteOnTermination bool } -// LoadBalancerInput holds all parameters required to ensure an API-server LB. -type LoadBalancerInput struct { - Name string - ProjectID string - Region string - NetworkID string - Tags map[string]string - Port int32 - Targets []LoadBalancerTargetInput -} - // LoadBalancerTargetInput describes a VM target in the API-server load // balancer target pool. -type LoadBalancerTargetInput struct { - Name string - IP string -} +type LoadBalancerTargetInput = loadbalancer.Target diff --git a/controller/constants.go b/controller/constants.go index cc4e652..3a2b1ce 100644 --- a/controller/constants.go +++ b/controller/constants.go @@ -13,8 +13,6 @@ package controller import "time" const ( - defaultAPIServerPort int32 = 6443 - cloudInitRefKindSecret = "Secret" retryableErrorRequeueAfter = 5 * time.Second diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index 8bbaf7a..e1d4c58 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -32,7 +32,7 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" bastionservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/bastion" - loadbalancerservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/scope" @@ -143,18 +143,48 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS ) return ctrl.Result{}, fmt.Errorf("list control plane machines: %w", err) } - targets := loadbalancerservice.APIServerTargets(machines.UnsortedList()) + targets := loadbalancer.APIServerTargets(machines.UnsortedList()) // Seeding with the real targets lets a load balancer deleted out of band // come back with the control plane behind it. seed := targets if len(seed) == 0 { - seed = []cloud.LoadBalancerTargetInput{loadbalancerservice.BootstrapTarget(bootstrapTargetIP(network))} + seed = []loadbalancer.Target{loadbalancer.BootstrapTarget(bootstrapTargetIP(network))} + } + clusterScope.SetAPIServerLoadBalancerTargets(targets, seed) + + hadLoadBalancerID := stackitCluster.Status.APIServerLoadBalancerID != "" + previousEndpointHost := stackitCluster.Status.APIServerEndpoint.Host + wasLBReady := meta.IsStatusConditionTrue(stackitCluster.Status.Conditions, infrav1.ClusterLoadBalancerReadyCondition) + + err = loadbalancer.New(clusterScope).Reconcile(ctx) + if !hadLoadBalancerID && stackitCluster.Status.APIServerLoadBalancerID != "" { + record.Eventf( + stackitCluster, "LoadBalancerCreated", + "Created API server load balancer %s", stackitCluster.Status.APIServerLoadBalancerID, + ) + } + if loadbalancer.IsTargetPoolUpdateError(err) { + if !cloud.IsRetryable(err) { + record.Warnf( + stackitCluster, "LoadBalancerTargetError", + "Cannot update API server load balancer target pool: %v", err, + ) + } + // Scoped to the load balancer condition: failing the cluster would + // stop the machine reconciler from replacing the broken machine. + util.SetConditions( + &stackitCluster.Status.Conditions, + stackitCluster.Generation, + metav1.ConditionFalse, + "LoadBalancerTargetError", + err.Error(), + infrav1.ClusterLoadBalancerReadyCondition, + ) + // Requeue even on a permanent error: this return skips SetReady, so + // no machine exists yet whose events could retry it. + return ctrl.Result{RequeueAfter: retryableErrorRequeueAfter}, nil } - loadBalancer, err := cloudClient.EnsureAPIServerLoadBalancer( - ctx, - loadbalancerservice.APIServerInput(stackitCluster, seed), - ) if err != nil { log.Error(err, "Failed to create API server load balancer", "cluster", stackitCluster.Name, @@ -176,17 +206,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS infrav1.ClusterReadyCondition, ) } - hadLoadBalancerID := stackitCluster.Status.APIServerLoadBalancerID != "" - if loadBalancer != nil { - stackitCluster.Status.APIServerLoadBalancerID = loadBalancer.ID - if !hadLoadBalancerID && loadBalancer.ID != "" { - record.Eventf( - stackitCluster, "LoadBalancerCreated", - "Created API server load balancer %s", loadBalancer.ID, - ) - } - } - if loadBalancer == nil || loadBalancer.IP == "" { + if stackitCluster.Status.APIServerEndpoint.Host == "" { clusterScope.SetNotReady( "Provisioning", "waiting for API server load balancer IP address", @@ -196,42 +216,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS return ctrl.Result{RequeueAfter: 10 * time.Second}, nil } - isLBReady := meta.IsStatusConditionTrue(stackitCluster.Status.Conditions, infrav1.ClusterLoadBalancerReadyCondition) && - stackitCluster.Status.APIServerEndpoint.Host == loadBalancer.IP - - endpoint := clusterv1.APIEndpoint{ - Host: loadBalancer.IP, - Port: defaultAPIServerPort, - } - clusterScope.SetAPIServerEndpoint(endpoint) - - // An empty set never overwrites a populated pool: cluster deletion filters - // out every machine while the API server is still needed for drain. - if len(targets) > 0 { - if err := cloudClient.SetAPIServerLoadBalancerTargets(ctx, loadBalancer.ID, defaultAPIServerPort, targets); err != nil { - if !cloud.IsRetryable(err) { - record.Warnf( - stackitCluster, "LoadBalancerTargetError", - "Cannot update API server load balancer target pool: %v", err, - ) - } - // Scoped to the load balancer condition: failing the cluster would - // stop the machine reconciler from replacing the broken machine. - util.SetConditions( - &stackitCluster.Status.Conditions, - stackitCluster.Generation, - metav1.ConditionFalse, - "LoadBalancerTargetError", - err.Error(), - infrav1.ClusterLoadBalancerReadyCondition, - ) - // Requeue even on a permanent error: this return skips SetReady, so - // no machine exists yet whose events could retry it. - return ctrl.Result{RequeueAfter: retryableErrorRequeueAfter}, nil - } - log.V(1).Info("Reconciled API server load balancer target pool", "targets", len(targets)) - } - + isLBReady := wasLBReady && previousEndpointHost == stackitCluster.Status.APIServerEndpoint.Host if !isLBReady { clusterScope.SetConditions( metav1.ConditionTrue, @@ -241,7 +226,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS ) record.Eventf( stackitCluster, "LoadBalancerReady", - "API server load balancer is ready at %s", loadBalancer.IP, + "API server load balancer is ready at %s", stackitCluster.Status.APIServerEndpoint.Host, ) } } else if stackitCluster.Spec.ControlPlaneEndpoint.Host != "" { @@ -386,8 +371,8 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS } // Cleanup runs unconditionally: neither spec nor status is a trustworthy - // record of what exists in the cloud. ResolveID, DeleteBastion, and the - // public IP and security group services fall back to tag lookups and + // record of what exists in the cloud. DeleteBastion and the load balancer, + // public IP, and security group services fall back to tag lookups and // tolerate NotFound. cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, stackitCluster) if err != nil { @@ -417,21 +402,14 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS return ctrl.Result{}, err } clusterScope.SetCloudClient(cloudClient) - loadBalancerID, err := loadbalancerservice.ResolveID(ctx, cloudClient, stackitCluster) - if err != nil { - log.Error(err, "Failed to resolve API server load balancer for deletion", "cluster", stackitCluster.Name) - record.Warnf(stackitCluster, "FailedResolveLoadBalancer", - "Failed to resolve API server load balancer: %v", err) + loadBalancerID := stackitCluster.Status.APIServerLoadBalancerID + if err := loadbalancer.New(clusterScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { + log.Error(err, "Failed to delete API server load balancer", "cluster", stackitCluster.Name, "loadBalancerID", loadBalancerID) + record.Warnf(stackitCluster, "FailedDeleteLoadBalancer", + "Failed to delete API server load balancer %s: %v", loadBalancerID, err) return ctrl.Result{}, err } if loadBalancerID != "" { - if err := cloudClient.DeleteAPIServerLoadBalancer(ctx, loadBalancerID); err != nil && !cloud.IsNotFound(err) { - log.Error(err, "Failed to delete API server load balancer", "cluster", stackitCluster.Name, "loadBalancerID", loadBalancerID) - record.Warnf(stackitCluster, "FailedDeleteLoadBalancer", - "Failed to delete API server load balancer %s: %v", loadBalancerID, err) - return ctrl.Result{}, err - } - stackitCluster.Status.APIServerLoadBalancerID = "" record.Eventf( stackitCluster, "LoadBalancerDeleted", "Deleted API server load balancer %s", loadBalancerID, diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index af7c1bc..3a64b0d 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -25,9 +25,11 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/util" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" ) const bastionSSHPort = 22 @@ -39,6 +41,9 @@ type ClusterScope struct { StackitCluster *infrav1.StackitCluster CloudClient cloud.Client + loadBalancerTargets []loadbalancer.Target + loadBalancerSeedTargets []loadbalancer.Target + patchHelper *patch.Helper } @@ -160,6 +165,46 @@ func (s *ClusterScope) IaaSClient() *iaas.APIClient { return s.CloudClient.IaaSClient() } +func (s *ClusterScope) LoadBalancerClient() *lb.APIClient { + return s.CloudClient.LoadBalancerClient() +} + +func (s *ClusterScope) APIServerLoadBalancerTags() map[string]string { + return util.ClusterTags(s.StackitCluster.Name, s.StackitCluster.Namespace, s.StackitCluster.Spec.AdditionalLabels) +} + +func (s *ClusterScope) SetAPIServerLoadBalancerID(id string) { + s.StackitCluster.Status.APIServerLoadBalancerID = id +} + +func (s *ClusterScope) SetAPIServerLoadBalancerTargets(targets, seedTargets []loadbalancer.Target) { + s.loadBalancerTargets = targets + s.loadBalancerSeedTargets = seedTargets +} + +func (s *ClusterScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec { + return loadbalancer.LoadBalancerSpec{ + ID: s.StackitCluster.Status.APIServerLoadBalancerID, + Name: s.StackitCluster.Name + "-apiserver", + NetworkID: s.StackitCluster.Spec.Network.ID, + Port: loadbalancer.DefaultAPIServerPort, + Tags: s.APIServerLoadBalancerTags(), + Targets: s.loadBalancerTargets, + SeedTargets: s.loadBalancerSeedTargets, + SetID: s.SetAPIServerLoadBalancerID, + SetAddress: func(ip string, port int32) { + if ip == "" { + s.StackitCluster.Status.APIServerEndpoint = clusterv1.APIEndpoint{} + return + } + s.SetAPIServerEndpoint(clusterv1.APIEndpoint{ + Host: ip, + Port: port, + }) + }, + } +} + func (s *ClusterScope) BastionSpec() infrav1.StackitBastionSpec { return s.StackitCluster.Spec.Bastion } diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index ebef540..f5b6af9 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -34,6 +34,7 @@ import ( . "github.com/onsi/gomega" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/test/utils" @@ -445,7 +446,7 @@ var _ = Describe("Manager", Ordered, func() { servers, err := cloudClient.ListServersByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) - loadBalancers, err := cloudClient.ListAPIServerLoadBalancersByTags(ctx, leakTags) + loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(loadBalancers).To(BeEmpty()) }, 5*time.Minute, 15*time.Second).Should(Succeed()) @@ -555,7 +556,7 @@ var _ = Describe("Manager", Ordered, func() { servers, err := cloudClient.ListServersByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) - loadBalancers, err := cloudClient.ListAPIServerLoadBalancersByTags(ctx, leakTags) + loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(loadBalancers).To(BeEmpty()) publicIPs, err := publicip.ListByTags(ctx, cloudClient.IaaSClient(), cloudClient.ProjectID(), cloudClient.Region(), stackitE2EBastionTags(testID)) @@ -633,7 +634,7 @@ var _ = Describe("Manager", Ordered, func() { servers, err := cloudClient.ListServersByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) - loadBalancers, err := cloudClient.ListAPIServerLoadBalancersByTags(ctx, leakTags) + loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(loadBalancers).To(BeEmpty()) }, 20*time.Minute, 15*time.Second).Should(Succeed()) @@ -728,7 +729,7 @@ var _ = Describe("Manager", Ordered, func() { servers, err := cloudClient.ListServersByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) - loadBalancers, err := cloudClient.ListAPIServerLoadBalancersByTags(ctx, leakTags) + loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(loadBalancers).To(BeEmpty()) }, 20*time.Minute, 15*time.Second).Should(Succeed()) @@ -857,7 +858,7 @@ var _ = Describe("Manager", Ordered, func() { servers, err := cloudClient.ListServersByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) - loadBalancers, err := cloudClient.ListAPIServerLoadBalancersByTags(ctx, leakTags) + loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(loadBalancers).To(BeEmpty()) }, 20*time.Minute, 15*time.Second).Should(Succeed()) @@ -1018,7 +1019,7 @@ var _ = Describe("Manager", Ordered, func() { servers, err := cloudClient.ListServersByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) - loadBalancers, err := cloudClient.ListAPIServerLoadBalancersByTags(ctx, leakTags) + loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(loadBalancers).To(BeEmpty()) }, 20*time.Minute, 15*time.Second).Should(Succeed()) @@ -1137,7 +1138,7 @@ var _ = Describe("Manager", Ordered, func() { servers, err := cloudClient.ListServersByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) - loadBalancers, err := cloudClient.ListAPIServerLoadBalancersByTags(ctx, leakTags) + loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(loadBalancers).To(BeEmpty()) }, 20*time.Minute, 15*time.Second).Should(Succeed()) @@ -1276,7 +1277,7 @@ var _ = Describe("Manager", Ordered, func() { servers, err := cloudClient.ListServersByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) - loadBalancers, err := cloudClient.ListAPIServerLoadBalancersByTags(ctx, leakTags) + loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(loadBalancers).To(BeEmpty()) }, 20*time.Minute, 15*time.Second).Should(Succeed()) @@ -1401,7 +1402,7 @@ var _ = Describe("Manager", Ordered, func() { servers, err := cloudClient.ListServersByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) - loadBalancers, err := cloudClient.ListAPIServerLoadBalancersByTags(ctx, leakTags) + loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(loadBalancers).To(BeEmpty()) }, 20*time.Minute, 15*time.Second).Should(Succeed()) From f6ae98b193101938b9a2c9a3c4d4e5deb418a02d Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Thu, 12 Nov 2026 14:25:44 +0100 Subject: [PATCH 09/60] feat: refactor server service --- cloud/cleanup.go | 42 +- cloud/cleanup_test.go | 5 +- cloud/client.go | 16 - cloud/fake/client.go | 357 ++++++---- cloud/sdk_client.go | 235 ------- cloud/sdk_client_integration_test.go | 7 +- cloud/sdk_client_test.go | 286 +------- cloud/services/bastion/bastion.go | 48 -- cloud/services/publicip/reconcile.go | 5 +- cloud/services/publicip/reconcile_test.go | 13 +- cloud/services/publicip/service.go | 21 +- cloud/services/server/reconcile.go | 278 ++++++++ cloud/services/server/reconcile_test.go | 630 ++++++++++++++++++ cloud/services/server/service.go | 111 +++ cloud/services/shared/types.go | 28 - cloud/types.go | 57 +- controller/stackitcluster_bastion.go | 48 +- controller/stackitcluster_controller_test.go | 7 +- controller/stackitcluster_infrastructure.go | 26 +- controller/stackitmachine_infrastructure.go | 141 +--- scope/cluster_scope.go | 72 +- .../cluster_scope_test.go | 10 +- scope/machine_scope.go | 66 +- test/e2e/e2e_test.go | 47 +- 24 files changed, 1488 insertions(+), 1068 deletions(-) delete mode 100644 cloud/services/bastion/bastion.go create mode 100644 cloud/services/server/reconcile.go create mode 100644 cloud/services/server/reconcile_test.go create mode 100644 cloud/services/server/service.go delete mode 100644 cloud/services/shared/types.go rename cloud/services/bastion/bastion_test.go => scope/cluster_scope_test.go (83%) diff --git a/cloud/cleanup.go b/cloud/cleanup.go index 805b937..08c9c72 100644 --- a/cloud/cleanup.go +++ b/cloud/cleanup.go @@ -19,6 +19,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" ) // CleanupByTags deletes provider-managed cloud resources matching tags without @@ -30,10 +31,13 @@ func CleanupByTags(ctx context.Context, client Client, tags map[string]string) e } var errs []string + srvClient := server.NewClient(client) + sgClient := securitygroup.NewClient(client) + bastionTags := make(map[string]string, len(tags)+1) maps.Copy(bastionTags, tags) bastionTags["cluster-api-provider-stackit/resource-role"] = "bastion" - bastionServers, err := client.ListServersByTags(ctx, bastionTags) + bastionServers, err := srvClient.ListByTags(ctx, bastionTags) if err != nil { errs = append(errs, err.Error()) } @@ -42,9 +46,9 @@ func CleanupByTags(ctx context.Context, client Client, tags map[string]string) e errs = append(errs, err.Error()) } else { for _, pubIP := range publicIPs { - for _, server := range bastionServers { - if err := publicip.DetachFromServer(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), server.ID, pubIP.ID); err != nil { - errs = append(errs, fmt.Sprintf("detach public IP %q from server %q: %v", pubIP.ID, server.ID, err)) + for _, srv := range bastionServers { + if err := publicip.DetachFromServer(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), srv.ID, pubIP.ID); err != nil { + errs = append(errs, fmt.Sprintf("detach public IP %q from server %q: %v", pubIP.ID, srv.ID, err)) } } if err := publicip.DeleteByID(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), pubIP.ID); err != nil { @@ -52,9 +56,6 @@ func CleanupByTags(ctx context.Context, client Client, tags map[string]string) e } } } - if err := client.DeleteBastion(ctx, BastionInput{Tags: bastionTags}, Bastion{}); err != nil && !IsNotFound(err) { - errs = append(errs, fmt.Sprintf("delete bastion resources: %v", err)) - } lbClient := loadbalancer.NewClient(client) loadBalancers, err := lbClient.ListByTags(ctx, tags) @@ -68,29 +69,36 @@ func CleanupByTags(ctx context.Context, client Client, tags map[string]string) e } } - servers, err := client.ListServersByTags(ctx, tags) + servers, err := srvClient.ListByTags(ctx, tags) if err != nil { errs = append(errs, err.Error()) - } else { - for _, server := range servers { - if err := client.DeleteServer(ctx, server.ID); err != nil && !IsNotFound(err) { - errs = append(errs, fmt.Sprintf("delete server %q: %v", server.ID, err)) - } - } } - sgClient := securitygroup.NewClient(client) securityGroups, err := sgClient.ListByTags(ctx, tags) if err != nil { errs = append(errs, err.Error()) } else { for _, sg := range securityGroups { - if err := sgClient.DeleteByID(ctx, sg.ID); err != nil { - errs = append(errs, fmt.Sprintf("delete security group %q: %v", sg.ID, err)) + for _, srv := range servers { + if err := sgClient.DetachFromServer(ctx, srv.ID, sg.ID); err != nil { + errs = append(errs, fmt.Sprintf("detach security group %q from server %q: %v", sg.ID, srv.ID, err)) + } } } } + for _, srv := range servers { + if err := srvClient.DeleteByID(ctx, srv.ID); err != nil { + errs = append(errs, fmt.Sprintf("delete server %q: %v", srv.ID, err)) + } + } + + for _, sg := range securityGroups { + if err := sgClient.DeleteByID(ctx, sg.ID); err != nil { + errs = append(errs, fmt.Sprintf("delete security group %q: %v", sg.ID, err)) + } + } + if len(errs) > 0 { return fmt.Errorf("%w: %s", ErrTransient, strings.Join(errs, "; ")) } diff --git a/cloud/cleanup_test.go b/cloud/cleanup_test.go index ff1ce52..ef6566e 100644 --- a/cloud/cleanup_test.go +++ b/cloud/cleanup_test.go @@ -16,6 +16,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud" cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" ) func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { @@ -30,10 +31,10 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { "cluster-api-provider-stackit/test-id": "test-2", } - if _, err := client.CreateServer(ctx, cloud.CreateServerInput{Name: "matched", Tags: tags}); err != nil { + if _, err := client.CreateServer(ctx, server.ServerSpec{Name: "matched", Tags: tags}); err != nil { t.Fatalf("CreateServer() error = %v", err) } - if _, err := client.CreateServer(ctx, cloud.CreateServerInput{Name: "other", Tags: otherTags}); err != nil { + if _, err := client.CreateServer(ctx, server.ServerSpec{Name: "other", Tags: otherTags}); err != nil { t.Fatalf("CreateServer() error = %v", err) } if _, err := client.CreatePublicIP(ctx, tags); err != nil { diff --git a/cloud/client.go b/cloud/client.go index f36919d..a24b68f 100644 --- a/cloud/client.go +++ b/cloud/client.go @@ -31,8 +31,6 @@ type Reconciler interface { // - return ErrInvalidInput for permanent input validation errors // - return ErrConflict for retryable conflict errors // - return ErrTransient for retryable transient errors -// - be idempotent (CreateServer must not produce duplicates when a resource -// with matching tags already exists) type Client interface { ProjectID() string @@ -42,22 +40,8 @@ type Client interface { LoadBalancerClient() *lb.APIClient - GetServer(ctx context.Context, id string) (*Server, error) - - FindServerByTags(ctx context.Context, tags map[string]string) (*Server, error) - - ListServersByTags(ctx context.Context, tags map[string]string) ([]*Server, error) - - CreateServer(ctx context.Context, input CreateServerInput) (*Server, error) - - DeleteServer(ctx context.Context, id string) error - GetNetwork(ctx context.Context, id string) (*Network, error) - EnsureBastion(ctx context.Context, input BastionInput) (*Bastion, error) - - DeleteBastion(ctx context.Context, input BastionInput, status Bastion) error - ListAvailabilityZones(ctx context.Context) ([]string, error) } diff --git a/cloud/fake/client.go b/cloud/fake/client.go index bd85967..f0e8af9 100644 --- a/cloud/fake/client.go +++ b/cloud/fake/client.go @@ -14,6 +14,7 @@ package fake import ( "context" + "encoding/base64" "encoding/json" "fmt" "maps" @@ -26,6 +27,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/stackit-sdk-go/core/config" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" @@ -60,8 +62,6 @@ type Client struct { FailNextDeleteLB error FailNextSetTargets error FailNextGetNetwork error - FailNextEnsureBastion error - FailNextDeleteBastion error FailNextAddSecurityGroup error FailNextListAZ error FailNextGetPublicIP error @@ -75,11 +75,8 @@ type Client struct { // Defaults to eu01-1, eu01-2, eu01-3. AvailabilityZones []string - // LastCreateServerInput records the most recent input passed to CreateServer. - LastCreateServerInput cloud.CreateServerInput - - // LastEnsureBastionInput records the most recent input passed to EnsureBastion. - LastEnsureBastionInput cloud.BastionInput + // LastCreateServerInput records the most recent spec passed to CreateServer. + LastCreateServerInput server.ServerSpec // Before* hooks, if non-nil, run before the call they belong to. They let a // test observe API server state at the exact moment a cloud call would @@ -90,7 +87,6 @@ type Client struct { // CreateServerCalls counts successful CreateServer calls (for idempotency // assertions). CreateServerCalls int - EnsureBastionCalls int AddSecurityGroupCalls int } @@ -239,15 +235,16 @@ func (c *Client) ListServersByTags(_ context.Context, tags map[string]string) ([ return servers, nil } -// CreateServer creates a server. To satisfy spec section 20 (idempotency), -// it returns the existing server if one with matching tags already exists. -func (c *Client) CreateServer(_ context.Context, input cloud.CreateServerInput) (*cloud.Server, error) { +// CreateServer creates a server in the fake store. +func (c *Client) CreateServer(_ context.Context, input server.ServerSpec) (*cloud.Server, error) { c.mu.Lock() - defer c.mu.Unlock() - if c.BeforeCreateServer != nil { + c.mu.Unlock() c.BeforeCreateServer() + c.mu.Lock() } + defer c.mu.Unlock() + if err := consume(&c.FailNextCreateServer); err != nil { return nil, err } @@ -257,13 +254,18 @@ func (c *Client) CreateServer(_ context.Context, input cloud.CreateServerInput) } } + entry := c.createServerEntry(input) + return cloneServer(entry.server), nil +} + +func (c *Client) createServerEntry(input server.ServerSpec) *serverEntry { id := c.genID() c.LastCreateServerInput = input zone := input.AvailabilityZone if zone == "" { zone = "eu01-1" } - server := &cloud.Server{ + srv := &cloud.Server{ ID: id, Name: input.Name, State: "ACTIVE", @@ -272,14 +274,15 @@ func (c *Client) CreateServer(_ context.Context, input cloud.CreateServerInput) {Type: "InternalIP", Address: "10.0.0.10"}, }, } - c.servers[id] = &serverEntry{ - server: server, + entry := &serverEntry{ + server: srv, tags: copyTags(input.Tags), securityGroups: securityGroupSet(input.SecurityGroups), userData: append([]byte(nil), input.UserData...), } + c.servers[id] = entry c.CreateServerCalls++ - return cloneServer(server), nil + return entry } // DeleteServer removes a server. It returns nil if the server is already gone. @@ -312,86 +315,6 @@ func (c *Client) GetNetwork(_ context.Context, id string) (*cloud.Network, error return &out, nil } -func (c *Client) EnsureBastion(_ context.Context, input cloud.BastionInput) (*cloud.Bastion, error) { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextEnsureBastion); err != nil { - return nil, err - } - securityGroupID := input.SecurityGroupID - if securityGroupID == "" { - securityGroupID = idOfSecurityGroup(c.securityGroupByTags(input.Tags)) - } - for _, serverEntry := range c.servers { - if len(input.Tags) > 0 && mapContains(serverEntry.tags, input.Tags) { - if securityGroupID != "" { - if serverEntry.securityGroups == nil { - serverEntry.securityGroups = map[string]struct{}{} - } - serverEntry.securityGroups[securityGroupID] = struct{}{} - } - return &cloud.Bastion{ - ServerID: serverEntry.server.ID, - ServerState: serverEntry.server.State, - ServerPowerStatus: serverEntry.server.PowerStatus, - SecurityGroupID: securityGroupID, - }, nil - } - } - - serverID := c.genID() - c.LastEnsureBastionInput = input - zone := input.AvailabilityZone - if zone == "" { - zone = "eu01-1" - } - server := &cloud.Server{ - ID: serverID, - Name: input.Name, - State: "ACTIVE", - AvailabilityZone: zone, - Addresses: []cloud.Address{ - {Type: "InternalIP", Address: "10.0.0.20"}, - }, - } - var sgSet map[string]struct{} - if securityGroupID != "" { - sgSet = securityGroupSet([]string{securityGroupID}) - } - c.servers[serverID] = &serverEntry{ - server: server, - tags: copyTags(input.Tags), - securityGroups: sgSet, - userData: append([]byte(nil), input.CloudInit...), - } - - c.EnsureBastionCalls++ - return &cloud.Bastion{ - ServerID: serverID, - ServerState: server.State, - SecurityGroupID: securityGroupID, - }, nil -} - -func (c *Client) DeleteBastion(_ context.Context, input cloud.BastionInput, status cloud.Bastion) error { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextDeleteBastion); err != nil { - return err - } - if status.ServerID != "" { - delete(c.servers, status.ServerID) - } - for id, entry := range c.servers { - if len(input.Tags) > 0 && mapContains(entry.tags, input.Tags) { - delete(c.servers, id) - } - } - return nil -} - func (c *Client) GetPublicIP(_ context.Context, id string) (*cloud.PublicIP, error) { c.mu.Lock() defer c.mu.Unlock() @@ -722,23 +645,6 @@ func containsString(values []string, needle string) bool { return slices.Contains(values, needle) } -func (c *Client) securityGroupByTags(tags map[string]string) *cloud.SecurityGroup { - for _, entry := range c.securityGroups { - if mapContains(entry.tags, tags) { - out := *entry.securityGroup - return &out - } - } - return nil -} - -func idOfSecurityGroup(securityGroup *cloud.SecurityGroup) string { - if securityGroup == nil { - return "" - } - return securityGroup.ID -} - func (c *Client) ListAvailabilityZones(_ context.Context) ([]string, error) { c.mu.Lock() defer c.mu.Unlock() @@ -770,7 +676,7 @@ func (c *Client) roundTripIaaS(r *http.Request) (*http.Response, error) { rec := httptest.NewRecorder() rec.Header().Set("Content-Type", "application/json") - if c.handlePublicIPRequest(rec, r) || c.handleSecurityGroupRequest(rec, r) { + if c.handlePublicIPRequest(rec, r) || c.handleSecurityGroupRequest(rec, r) || c.handleServerRequest(rec, r) { return rec.Result(), nil } @@ -1128,16 +1034,6 @@ func (c *Client) handleSecurityGroupRequest(rec *httptest.ResponseRecorder, r *h writeFakeStatus(rec, err) return true - case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers"): - tags := parseLabelSelector(r.URL.Query().Get("label_selector")) - items, err := c.listServerItems(tags) - if err != nil { - writeFakeError(rec, err) - } else { - _ = json.NewEncoder(rec).Encode(map[string]any{"items": items}) - } - return true - case r.Method == http.MethodPost && strings.HasSuffix(path, "/security-groups"): var body struct { Name string `json:"name"` @@ -1302,33 +1198,212 @@ func (c *Client) addSecurityGroupToServer(serverID, securityGroupID string) erro return nil } +func (c *Client) handleServerRequest(rec *httptest.ResponseRecorder, r *http.Request) bool { + path := r.URL.Path + + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers"): + tags := parseLabelSelector(r.URL.Query().Get("label_selector")) + items, err := c.listServerItems(tags) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(map[string]any{"items": items}) + } + return true + + case r.Method == http.MethodPost && strings.HasSuffix(path, "/servers"): + c.handleCreateServer(rec, r) + return true + + case r.Method == http.MethodGet && strings.Contains(path, "/servers/") && strings.HasSuffix(path, "/nics"): + sub := path[strings.Index(path, "/servers/")+len("/servers/"):] + serverID := strings.TrimSuffix(sub, "/nics") + items, err := c.listServerNICItems(serverID) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(map[string]any{"items": items}) + } + return true + + case r.Method == http.MethodGet && strings.Contains(path, "/servers/"): + serverID := path[strings.LastIndex(path, "/servers/")+len("/servers/"):] + item, err := c.getServerItem(serverID) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(item) + } + return true + + case r.Method == http.MethodDelete && strings.Contains(path, "/servers/"): + serverID := path[strings.LastIndex(path, "/servers/")+len("/servers/"):] + writeFakeStatus(rec, c.deleteServerHTTP(serverID)) + return true + + default: + return false + } +} + +func (c *Client) deleteServerHTTP(id string) error { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextDeleteServer); err != nil { + return err + } + if _, ok := c.servers[id]; !ok { + return fmt.Errorf("server %q: %w", id, cloud.ErrNotFound) + } + delete(c.servers, id) + return nil +} + +func (c *Client) handleCreateServer(rec *httptest.ResponseRecorder, r *http.Request) { + var body struct { + Name string `json:"name"` + MachineType string `json:"machineType"` + ImageID string `json:"imageId"` + AvailabilityZone string `json:"availabilityZone"` + KeypairName string `json:"keypairName"` + SecurityGroups []string `json:"securityGroups"` + UserData string `json:"userData"` + Labels map[string]string `json:"labels"` + Networking *struct { + NetworkID string `json:"networkId"` + } `json:"networking"` + BootVolume *struct { + Size int `json:"size"` + PerformanceClass string `json:"performanceClass"` + DeleteOnTermination bool `json:"deleteOnTermination"` + Source *struct { + ID string `json:"id"` + } `json:"source"` + } `json:"bootVolume"` + } + if r.Body != nil { + _ = json.NewDecoder(r.Body).Decode(&body) + } + var userData []byte + if body.UserData != "" { + userData, _ = base64.StdEncoding.DecodeString(body.UserData) + } + var networkID string + if body.Networking != nil { + networkID = body.Networking.NetworkID + } + imageID := body.ImageID + var rootVolume server.RootVolume + if body.BootVolume != nil { + rootVolume = server.RootVolume{ + SizeGiB: body.BootVolume.Size, + PerformanceClass: body.BootVolume.PerformanceClass, + DeleteOnTermination: body.BootVolume.DeleteOnTermination, + } + if imageID == "" && body.BootVolume.Source != nil { + imageID = body.BootVolume.Source.ID + } + } + spec := server.ServerSpec{ + Name: body.Name, + MachineType: body.MachineType, + ImageID: imageID, + AvailabilityZone: body.AvailabilityZone, + SSHKeyName: body.KeypairName, + NetworkID: networkID, + SecurityGroups: body.SecurityGroups, + UserData: userData, + Tags: body.Labels, + RootVolume: rootVolume, + } + + c.mu.Lock() + if c.BeforeCreateServer != nil { + c.mu.Unlock() + c.BeforeCreateServer() + c.mu.Lock() + } + if err := consume(&c.FailNextCreateServer); err != nil { + c.mu.Unlock() + writeFakeError(rec, err) + return + } + entry := c.createServerEntry(spec) + item := fakeServerMap(entry) + c.mu.Unlock() + + _ = json.NewEncoder(rec).Encode(item) +} + +func (c *Client) getServerItem(id string) (map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + if err := consume(&c.FailNextGetServer); err != nil { + return nil, err + } + entry, ok := c.servers[id] + if !ok { + return nil, fmt.Errorf("server %q: %w", id, cloud.ErrNotFound) + } + return fakeServerMap(entry), nil +} + +func (c *Client) listServerNICItems(serverID string) ([]map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + entry, ok := c.servers[serverID] + if !ok { + return nil, fmt.Errorf("server %q: %w", serverID, cloud.ErrNotFound) + } + items := make([]map[string]any, 0, len(entry.server.Addresses)) + for _, addr := range entry.server.Addresses { + items = append(items, map[string]any{"ipv4": addr.Address}) + } + return items, nil +} + func (c *Client) listServerItems(tags map[string]string) ([]map[string]any, error) { c.mu.Lock() defer c.mu.Unlock() + if err := consume(&c.FailNextFindServer); err != nil { + return nil, err + } items := make([]map[string]any, 0, len(c.servers)) for _, entry := range c.servers { if len(tags) > 0 && !mapContains(entry.tags, tags) { continue } - m := map[string]any{ - "id": entry.server.ID, - "name": entry.server.Name, - "machineType": "c2i.1", - "status": entry.server.State, - } - if len(entry.tags) > 0 { - labels := make(map[string]any, len(entry.tags)) - for k, v := range entry.tags { - labels[k] = v - } - m["labels"] = labels - } - items = append(items, m) + items = append(items, fakeServerMap(entry)) } return items, nil } +func fakeServerMap(entry *serverEntry) map[string]any { + m := map[string]any{ + "id": entry.server.ID, + "name": entry.server.Name, + "machineType": "c2i.1", + "status": entry.server.State, + "availabilityZone": entry.server.AvailabilityZone, + } + if entry.server.PowerStatus != "" { + m["powerStatus"] = entry.server.PowerStatus + } + if len(entry.tags) > 0 { + labels := make(map[string]any, len(entry.tags)) + for k, v := range entry.tags { + labels[k] = v + } + m["labels"] = labels + } + return m +} + func (c *Client) removeSecurityGroupFromServer(serverID, securityGroupID string) error { c.mu.Lock() defer c.mu.Unlock() @@ -1453,8 +1528,8 @@ func (c *Client) deleteSecurityGroup(id string) error { if _, ok := c.securityGroups[id]; !ok { return fmt.Errorf("security group %q: %w", id, cloud.ErrNotFound) } - for serverID, server := range c.servers { - if _, attached := server.securityGroups[id]; attached { + for serverID, srv := range c.servers { + if _, attached := srv.securityGroups[id]; attached { return fmt.Errorf("security group %q is still attached to server %q: %w", id, serverID, cloud.ErrConflict) } } diff --git a/cloud/sdk_client.go b/cloud/sdk_client.go index c1e3376..a41bea3 100644 --- a/cloud/sdk_client.go +++ b/cloud/sdk_client.go @@ -12,11 +12,9 @@ package cloud import ( "context" - "encoding/base64" "fmt" "os" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/stackit-sdk-go/core/config" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" @@ -106,108 +104,6 @@ func stackitOptions(creds Credentials, endpoint string) []config.ConfigurationOp return opts } -func (c *SDKClient) GetServer(ctx context.Context, id string) (*Server, error) { - server, err := c.iaasClient.DefaultAPI.GetServer(ctx, c.projectID, c.region, id).Execute() - if err != nil { - return nil, shared.ClassifySDKError("get server", err) - } - return c.serverFromSDK(ctx, server), nil -} - -func (c *SDKClient) FindServerByTags(ctx context.Context, tags map[string]string) (*Server, error) { - servers, err := c.ListServersByTags(ctx, tags) - if err != nil { - return nil, err - } - if len(servers) == 0 { - return nil, fmt.Errorf("%w: no server matching tags", ErrNotFound) - } - if len(servers) > 1 { - return nil, fmt.Errorf("%w: multiple servers matching tags", ErrConflict) - } - return servers[0], nil -} - -func (c *SDKClient) ListServersByTags(ctx context.Context, tags map[string]string) ([]*Server, error) { - if len(tags) == 0 { - return nil, fmt.Errorf("%w: empty tag selector", ErrInvalidInput) - } - resp, err := c.iaasClient.DefaultAPI.ListServers(ctx, c.projectID, c.region). - LabelSelector(shared.LabelSelector(tags)). - Details(true). - Execute() - if err != nil { - return nil, shared.ClassifySDKError("list servers", err) - } - matched := []*Server{} - for _, server := range resp.GetItems() { - if shared.LabelsContainTags(server.GetLabels(), tags) { - server := server - matched = append(matched, c.serverFromSDK(ctx, &server)) - } - } - return matched, nil -} - -func (c *SDKClient) CreateServer(ctx context.Context, input CreateServerInput) (*Server, error) { - if existing, err := c.FindServerByTags(ctx, input.Tags); err == nil { - return existing, nil - } else if !IsNotFound(err) { - return nil, err - } - - payload := &iaas.CreateServerPayload{} - payload.SetName(input.Name) - payload.SetMachineType(input.MachineType) - useBootVolume := input.RootVolume.SizeGiB > 0 || input.RootVolume.PerformanceClass != "" - if input.ImageID != "" && !useBootVolume { - payload.SetImageId(input.ImageID) - } - if len(input.Tags) > 0 { - payload.SetLabels(shared.TagsToSDKLabels(input.Tags)) - } - networking := iaas.NewCreateServerNetworking() - if input.NetworkID != "" { - networking.SetNetworkId(input.NetworkID) - } - payload.SetNetworking(iaas.CreateServerNetworkingAsCreateServerPayloadAllOfNetworking(networking)) - if len(input.SecurityGroups) > 0 { - payload.SetSecurityGroups(input.SecurityGroups) - } - if len(input.UserData) > 0 { - payload.SetUserData(base64.StdEncoding.EncodeToString(input.UserData)) - payload.SetConfigDrive(true) - } - if useBootVolume { - bootVolume := iaas.NewBootVolume() - if input.ImageID != "" { - bootVolume.SetSource(*iaas.NewBootVolumeSource(input.ImageID, "image")) - } - if input.RootVolume.SizeGiB > 0 { - bootVolume.SetSize(int64(input.RootVolume.SizeGiB)) - } - if input.RootVolume.PerformanceClass != "" { - bootVolume.SetPerformanceClass(input.RootVolume.PerformanceClass) - } - bootVolume.SetDeleteOnTermination(input.RootVolume.DeleteOnTermination) - payload.SetBootVolume(*bootVolume) - } - if input.SSHKeyName != "" { - payload.SetKeypairName(input.SSHKeyName) - } - if input.AvailabilityZone != "" { - payload.SetAvailabilityZone(input.AvailabilityZone) - } - - server, err := c.iaasClient.DefaultAPI.CreateServer(ctx, c.projectID, c.region). - CreateServerPayload(*payload). - Execute() - if err != nil { - return nil, shared.ClassifySDKError("create server", err) - } - return c.serverFromSDK(ctx, server), nil -} - func (c *SDKClient) ListAvailabilityZones(ctx context.Context) ([]string, error) { resp, err := c.iaasClient.DefaultAPI.ListAvailabilityZones(ctx, c.region).Execute() if err != nil { @@ -219,13 +115,6 @@ func (c *SDKClient) ListAvailabilityZones(ctx context.Context) ([]string, error) return resp.GetItems(), nil } -func (c *SDKClient) DeleteServer(ctx context.Context, id string) error { - if err := c.iaasClient.DefaultAPI.DeleteServer(ctx, c.projectID, c.region, id).Execute(); err != nil { - return shared.ClassifySDKError("delete server", err) - } - return nil -} - func (c *SDKClient) GetNetwork(ctx context.Context, id string) (*Network, error) { network, err := c.iaasClient.DefaultAPI.GetNetwork(ctx, c.projectID, c.region, id).Execute() if err != nil { @@ -238,127 +127,3 @@ func (c *SDKClient) GetNetwork(ctx context.Context, id string) (*Network, error) IPv4Prefixes: ipv4.GetPrefixes(), }, nil } - -func (c *SDKClient) EnsureBastion(ctx context.Context, input BastionInput) (*Bastion, error) { - if input.Name == "" || input.NetworkID == "" || input.ImageID == "" || - input.MachineType == "" || input.SSHKeyName == "" { - return nil, fmt.Errorf( - "%w: bastion name, network ID, image ID, machine type, and SSH key name are required", - ErrInvalidInput, - ) - } - if len(input.AllowedCIDRs) == 0 { - return nil, fmt.Errorf("%w: bastion allowed CIDRs are required", ErrInvalidInput) - } - if len(input.Tags) == 0 { - return nil, fmt.Errorf("%w: bastion tags are required", ErrInvalidInput) - } - - sgClient := securitygroup.NewClient(c) - securityGroupID := input.SecurityGroupID - if securityGroupID == "" { - securityGroup, err := sgClient.FindByTags(ctx, input.Tags) - if err != nil { - return nil, err - } - securityGroupID = securityGroup.ID - } - - server, err := c.CreateServer(ctx, CreateServerInput{ - Name: input.Name, - ProjectID: input.ProjectID, - Region: input.Region, - ImageID: input.ImageID, - MachineType: input.MachineType, - AvailabilityZone: input.AvailabilityZone, - SSHKeyName: input.SSHKeyName, - NetworkID: input.NetworkID, - SecurityGroups: []string{securityGroupID}, - UserData: input.CloudInit, - Tags: input.Tags, - RootVolume: input.RootVolume, - }) - if err != nil { - return nil, err - } - // The group is already in the CreateServer payload, but CreateServer - // short-circuits on an existing server found by tags — this is then the only - // path that re-attaches a group that was detached out of band (for example - // by an interrupted DeleteBastion). Kept deliberately, and idempotent: the - // duplicate/no-port-yet responses are tolerated so they can no longer abort - // the reconcile before the public IP below is assigned. - if err := sgClient.AttachToServer(ctx, server.ID, securityGroupID); err != nil { - return nil, err - } - - return &Bastion{ - ServerID: server.ID, - ServerState: server.State, - ServerPowerStatus: server.PowerStatus, - SecurityGroupID: securityGroupID, - }, nil -} - -func (c *SDKClient) DeleteBastion(ctx context.Context, input BastionInput, status Bastion) error { - serverID := status.ServerID - if serverID == "" { - server, err := c.FindServerByTags(ctx, input.Tags) - if err == nil { - serverID = server.ID - } else if !IsNotFound(err) { - return err - } - } - if serverID == "" { - return nil - } - - sgClient := securitygroup.NewClient(c) - securityGroupID := status.SecurityGroupID - if securityGroupID == "" { - securityGroupID = input.SecurityGroupID - } - if securityGroupID == "" && len(input.Tags) > 0 { - securityGroup, err := sgClient.FindByTags(ctx, input.Tags) - if err == nil { - securityGroupID = securityGroup.ID - } else if !IsNotFound(err) { - return err - } - } - - if securityGroupID != "" { - if err := sgClient.DetachFromServer(ctx, serverID, securityGroupID); err != nil { - return err - } - } - if err := c.DeleteServer(ctx, serverID); err != nil && !IsNotFound(err) { - return err - } - return nil -} - -func (c *SDKClient) serverFromSDK(ctx context.Context, server *iaas.Server) *Server { - if server == nil { - return nil - } - out := &Server{ - ID: server.GetId(), - Name: server.GetName(), - State: server.GetStatus(), - PowerStatus: server.GetPowerStatus(), - AvailabilityZone: server.GetAvailabilityZone(), - } - nics, err := c.iaasClient.DefaultAPI.ListServerNICs(ctx, c.projectID, c.region, out.ID).Execute() - if err == nil { - for _, nic := range nics.GetItems() { - if ipv4 := nic.GetIpv4(); ipv4 != "" { - out.Addresses = append(out.Addresses, Address{Type: "InternalIP", Address: ipv4}) - } - if ipv6 := nic.GetIpv6(); ipv6 != "" { - out.Addresses = append(out.Addresses, Address{Type: "InternalIP", Address: ipv6}) - } - } - } - return out -} diff --git a/cloud/sdk_client_integration_test.go b/cloud/sdk_client_integration_test.go index 2d03335..996f7f4 100644 --- a/cloud/sdk_client_integration_test.go +++ b/cloud/sdk_client_integration_test.go @@ -22,6 +22,7 @@ import ( "time" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" ) const ( @@ -36,16 +37,16 @@ const ( func TestSDKClientCredentialsIntegration(t *testing.T) { client := newIntegrationClient(t) - _, err := client.FindServerByTags(context.Background(), map[string]string{ + _, err := server.NewClient(client).FindByTags(context.Background(), map[string]string{ "capistackit-test": "credential-check", }) if IsNotFound(err) { return } if err != nil { - t.Fatalf("FindServerByTags() error = %v", err) + t.Fatalf("FindByTags() error = %v", err) } - t.Fatal("FindServerByTags() unexpectedly found a server matching integration-test tags") + t.Fatal("FindByTags() unexpectedly found a server matching integration-test tags") } func TestSDKClientListNetworksIntegration(t *testing.T) { diff --git a/cloud/sdk_client_test.go b/cloud/sdk_client_test.go index 0671dad..d0c21a9 100644 --- a/cloud/sdk_client_test.go +++ b/cloud/sdk_client_test.go @@ -20,95 +20,12 @@ import ( ) const ( - testSDKProjectID = "00000000-0000-0000-0000-000000000000" - testSDKRegion = "eu01" - testSDKServerID = "33333333-3333-4333-8333-333333333333" - testSDKNetworkID = "11111111-1111-4111-8111-111111111111" - testSDKImageID = "22222222-2222-4222-8222-222222222222" - testSDKSecurityGroup = "44444444-4444-4444-8444-444444444444" - testBoolTrue = "true" + testSDKProjectID = "00000000-0000-0000-0000-000000000000" + testSDKRegion = "eu01" + testSDKNetworkID = "11111111-1111-4111-8111-111111111111" + testBoolTrue = "true" ) -func TestSDKClientCreateServerUsesExpectedPayload(t *testing.T) { - var createPayload map[string]any - server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - switch { - case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers"): - if got := r.URL.Query().Get("label_selector"); got != "cluster=test" { - t.Fatalf("label_selector = %q, want cluster=test", got) - } - if got := r.URL.Query().Get("details"); got != testBoolTrue { - t.Fatalf("details = %q, want true", got) - } - writeJSON(t, w, map[string]any{"items": []any{}}) - case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/servers"): - createPayload = readJSON(t, r) - writeJSON(t, w, map[string]any{ - "id": testSDKServerID, - "machineType": "c2i.2", - "name": "node-0", - "status": "ACTIVE", - }) - case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testSDKServerID+"/nics"): - writeJSON(t, w, map[string]any{ - "items": []any{ - map[string]any{"ipv4": "10.0.0.10", "ipv6": "fd00::10"}, - }, - }) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - } - })) - - client := newTestSDKClient(t, server.URL) - created, err := client.CreateServer(context.Background(), CreateServerInput{ - Name: "node-0", - MachineType: "c2i.2", - ImageID: testSDKImageID, - AvailabilityZone: "eu01-1", - SSHKeyName: "default", - NetworkID: testSDKNetworkID, - SecurityGroups: []string{testSDKSecurityGroup}, - UserData: []byte("#cloud-config\n"), - RootVolume: RootVolumeInput{ - SizeGiB: 50, - PerformanceClass: "storage_premium_perf6", - DeleteOnTermination: true, - }, - Tags: map[string]string{"cluster": "test"}, - }) - if err != nil { - t.Fatalf("CreateServer() error = %v", err) - } - if created.ID != testSDKServerID || created.Name != "node-0" || created.State != "ACTIVE" { - t.Fatalf("CreateServer() = %#v", created) - } - if len(created.Addresses) != 2 { - t.Fatalf("CreateServer() addresses = %#v, want two internal addresses", created.Addresses) - } - - assertStringField(t, createPayload, "name", "node-0") - assertStringField(t, createPayload, "machineType", "c2i.2") - assertFieldAbsent(t, createPayload, "imageId") - assertStringField(t, createPayload, "availabilityZone", "eu01-1") - assertStringField(t, createPayload, "keypairName", "default") - assertStringField(t, createPayload, "userData", "I2Nsb3VkLWNvbmZpZwo=") - assertBoolField(t, createPayload, "configDrive", true) - assertNestedStringField(t, createPayload, []string{"labels", "cluster"}, "test") - assertNestedStringField(t, createPayload, []string{"networking", "networkId"}, testSDKNetworkID) - assertNestedStringField(t, createPayload, []string{"bootVolume", "performanceClass"}, "storage_premium_perf6") - assertNestedStringField(t, createPayload, []string{"bootVolume", "source", "id"}, testSDKImageID) - assertNestedStringField(t, createPayload, []string{"bootVolume", "source", "type"}, "image") - assertNestedNumberField(t, createPayload, []string{"bootVolume", "size"}, 50) -} - -func assertFieldAbsent(t *testing.T, payload map[string]any, key string) { - t.Helper() - if _, ok := payload[key]; ok { - t.Fatalf("%s present in payload, want absent: %#v", key, payload[key]) - } -} - func TestSDKClientClassifiesHTTPStatusCodes(t *testing.T) { tests := []struct { name string @@ -130,12 +47,12 @@ func TestSDKClientClassifiesHTTPStatusCodes(t *testing.T) { })) client := newTestSDKClient(t, server.URL) - _, err := client.GetServer(context.Background(), testSDKServerID) + _, err := client.GetNetwork(context.Background(), testSDKNetworkID) if err == nil { - t.Fatal("GetServer() error = nil") + t.Fatal("GetNetwork() error = nil") } if !tt.check(err) { - t.Fatalf("GetServer() error = %v, did not match expected classifier", err) + t.Fatalf("GetNetwork() error = %v, did not match expected classifier", err) } }) } @@ -170,16 +87,6 @@ func newTestSDKClient(t *testing.T, endpoint string) *SDKClient { return sdkClient } -func readJSON(t *testing.T, r *http.Request) map[string]any { - t.Helper() - - var out map[string]any - if err := json.NewDecoder(r.Body).Decode(&out); err != nil { - t.Fatalf("decode request body: %v", err) - } - return out -} - func writeJSON(t *testing.T, w http.ResponseWriter, value any) { t.Helper() @@ -189,185 +96,6 @@ func writeJSON(t *testing.T, w http.ResponseWriter, value any) { } } -func assertStringField(t *testing.T, m map[string]any, key, want string) { - t.Helper() - if got := lookup(m, key); got != want { - t.Fatalf("%s = %#v, want %q in %#v", key, got, want, m) - } -} - -func assertBoolField(t *testing.T, m map[string]any, key string, want bool) { - t.Helper() - if got := lookup(m, key); got != want { - t.Fatalf("%s = %#v, want %t in %#v", key, got, want, m) - } -} - -func assertNestedStringField(t *testing.T, m map[string]any, path []string, want string) { - t.Helper() - if got := nestedValue(t, m, path); got != want { - t.Fatalf("%s = %#v, want %q in %#v", strings.Join(path, "."), got, want, m) - } -} - -func assertNestedNumberField(t *testing.T, m map[string]any, path []string, want float64) { - t.Helper() - if got := nestedValue(t, m, path); got != want { - t.Fatalf("%s = %#v, want %v in %#v", strings.Join(path, "."), got, want, m) - } -} - -func nestedValue(t *testing.T, m map[string]any, path []string) any { - t.Helper() - - var current any = m - for _, part := range path { - switch typed := current.(type) { - case map[string]any: - current = lookup(typed, part) - case []any: - if len(part) != 1 || part[0] < '0' || part[0] > '9' { - t.Fatalf("path element %q cannot index %#v", part, typed) - } - index := int(part[0] - '0') - if index >= len(typed) { - t.Fatalf("path element %q out of range for %#v", part, typed) - } - current = typed[index] - default: - t.Fatalf("path element %q cannot descend into %#v", part, typed) - } - } - return current -} - -func lookup(m map[string]any, key string) any { - for _, candidate := range []string{ - key, - strings.ToUpper(key[:1]) + key[1:], - } { - if value, ok := m[candidate]; ok { - return value - } - } - return nil -} - -// TestSDKClientEnsureBastionToleratesDuplicateSecurityGroupAttach guards the -// idempotency of the security-group attach. The group is already in the create -// payload, so the attach usually hits a duplicate ("400 Duplicate items in the -// list") or a server without a port yet ("404 ... as device id on any ports"). -// Neither may abort EnsureBastion — that used to leave the public IP unassigned -// for a full reconcile cycle. The attach itself is kept because CreateServer -// short-circuits on an existing server, making this the only re-attach path. -func TestSDKClientEnsureBastionToleratesDuplicateSecurityGroupAttach(t *testing.T) { - var ( - createPayload map[string]any - attachCallCount int - ) - server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - path := r.URL.Path - switch { - case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): - writeJSON(t, w, map[string]any{"items": []any{ - map[string]any{ - "id": testSDKSecurityGroup, "name": "bastion-ssh", - "labels": map[string]any{"cluster": "test"}, - }, - }}) - case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers"): - writeJSON(t, w, map[string]any{"items": []any{}}) - case r.Method == http.MethodPost && strings.HasSuffix(path, "/servers"): - createPayload = readJSON(t, r) - writeJSON(t, w, map[string]any{ - "id": testSDKServerID, "name": "bastion", "status": "ACTIVE", "machineType": "c2i.1", - }) - case strings.Contains(path, "/security-groups/") && strings.Contains(path, "/servers/"): - // Answer the way the real API does for an already-attached group. - attachCallCount++ - w.WriteHeader(http.StatusBadRequest) - writeJSON(t, w, map[string]any{ - "code": 400, - "msg": "request invalid: Invalid input for security_groups. Reason: Duplicate items in the list.", - }) - case r.Method == http.MethodGet && strings.Contains(path, "/servers/"): - writeJSON(t, w, map[string]any{ - "id": testSDKServerID, "name": "bastion", "status": "ACTIVE", "machineType": "c2i.1", - }) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - } - })) - - client := newTestSDKClient(t, server.URL) - bastion, err := client.EnsureBastion(context.Background(), BastionInput{ - Name: "bastion", - ProjectID: testSDKProjectID, - Region: testSDKRegion, - NetworkID: testSDKNetworkID, - ImageID: testSDKImageID, - MachineType: "c2i.1", - AvailabilityZone: "eu01-1", - SSHKeyName: "default", - AllowedCIDRs: []string{"203.0.113.0/24"}, - Tags: map[string]string{"cluster": "test"}, - }) - if err != nil { - t.Fatalf("EnsureBastion() error = %v", err) - } - - if attachCallCount != 1 { - t.Fatalf("attach attempted %d time(s), want exactly 1 (the idempotent re-attach)", attachCallCount) - } - assertStringField(t, createPayload, "availabilityZone", "eu01-1") - groups, _ := createPayload["securityGroups"].([]any) - if len(groups) != 1 || groups[0] != testSDKSecurityGroup { - t.Fatalf("create payload securityGroups = %v, want [%s]", createPayload["securityGroups"], testSDKSecurityGroup) - } - if bastion.ServerID != testSDKServerID { - t.Fatalf("bastion.ServerID = %q, want %s", bastion.ServerID, testSDKServerID) - } -} - -func TestSDKClientDeleteBastionDetachesSecurityGroupWithoutDeletingIt(t *testing.T) { - var ( - detachedServerID string - detachedSGID string - deletedServerID string - ) - server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - path := r.URL.Path - switch { - case r.Method == http.MethodDelete && strings.Contains(path, "/servers/") && strings.Contains(path, "/security-groups/"): - parts := strings.Split(path, "/servers/") - sub := strings.Split(parts[1], "/security-groups/") - detachedServerID, detachedSGID = sub[0], sub[1] - w.WriteHeader(http.StatusNoContent) - case r.Method == http.MethodDelete && strings.HasSuffix(path, "/servers/"+testSDKServerID): - deletedServerID = testSDKServerID - w.WriteHeader(http.StatusNoContent) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - } - })) - - client := newTestSDKClient(t, server.URL) - err := client.DeleteBastion( - context.Background(), - BastionInput{Tags: map[string]string{"cluster": "test"}}, - Bastion{ServerID: testSDKServerID, SecurityGroupID: testSDKSecurityGroup}, - ) - if err != nil { - t.Fatalf("DeleteBastion() error = %v", err) - } - if detachedServerID != testSDKServerID || detachedSGID != testSDKSecurityGroup { - t.Fatalf("detached (%q, %q), want (%q, %q)", detachedServerID, detachedSGID, testSDKServerID, testSDKSecurityGroup) - } - if deletedServerID != testSDKServerID { - t.Fatalf("deletedServerID = %q, want %q", deletedServerID, testSDKServerID) - } -} - func TestSDKClientListAvailabilityZones(t *testing.T) { server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/availability-zones") { diff --git a/cloud/services/bastion/bastion.go b/cloud/services/bastion/bastion.go deleted file mode 100644 index d89ae2c..0000000 --- a/cloud/services/bastion/bastion.go +++ /dev/null @@ -1,48 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -package bastion - -import ( - infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud" - "github.com/stackitcloud/cluster-api-provider-stackit/scope" - "github.com/stackitcloud/cluster-api-provider-stackit/util" -) - -func Input(stackitCluster *infrav1.StackitCluster, cloudInit []byte) cloud.BastionInput { - deleteOnTermination := true - if stackitCluster.Spec.Bastion.RootVolume.DeleteOnTermination != nil { - deleteOnTermination = *stackitCluster.Spec.Bastion.RootVolume.DeleteOnTermination - } - - tags := util.ClusterTags(stackitCluster.Name, stackitCluster.Namespace, stackitCluster.Spec.AdditionalLabels) - tags[util.LabelResourceRole] = util.ResourceRoleBastion - - return cloud.BastionInput{ - Name: stackitCluster.Name + "-bastion", - ProjectID: stackitCluster.Spec.ProjectID, - Region: stackitCluster.Spec.Region, - NetworkID: stackitCluster.Spec.Network.ID, - ImageID: stackitCluster.Spec.Bastion.ImageID, - MachineType: stackitCluster.Spec.Bastion.MachineType, - AvailabilityZone: scope.DefaultAvailabilityZone(stackitCluster), - SSHKeyName: stackitCluster.Spec.Bastion.SSHKeyName, - AllowedCIDRs: stackitCluster.Spec.Bastion.AllowedCIDRs, - SecurityGroupID: stackitCluster.Status.Bastion.SecurityGroupID, - Tags: tags, - RootVolume: cloud.RootVolumeInput{ - SizeGiB: stackitCluster.Spec.Bastion.RootVolume.SizeGiB, - PerformanceClass: stackitCluster.Spec.Bastion.RootVolume.PerformanceClass, - DeleteOnTermination: deleteOnTermination, - }, - CloudInit: cloudInit, - } -} diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index 449ee75..0d67b38 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -17,6 +17,7 @@ import ( iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" logf "sigs.k8s.io/controller-runtime/pkg/log" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) @@ -160,14 +161,14 @@ func (s *Service) resolveServerID(ctx context.Context) (string, error) { if id := s.scope.BastionStatus().ServerID; id != "" { return id, nil } - server, err := s.client.FindServerByTags(ctx, s.scope.BastionTags()) + srv, err := server.NewClient(s.scope).FindByTags(ctx, s.scope.BastionTags()) if err != nil { if shared.IsNotFound(err) { return "", nil } return "", err } - return server.ID, nil + return srv.ID, nil } // ListByTags lists STACKIT public IPs matching the given tag selector. diff --git a/cloud/services/publicip/reconcile_test.go b/cloud/services/publicip/reconcile_test.go index b162e5d..af2ad7e 100644 --- a/cloud/services/publicip/reconcile_test.go +++ b/cloud/services/publicip/reconcile_test.go @@ -24,6 +24,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud" cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/scope" ) @@ -99,7 +100,7 @@ func TestServiceReconcile(t *testing.T) { fakeCloud := cloudfake.New() clusterScope := newTestClusterScope(t, true, fakeCloud) - server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + server, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ Name: "test-cluster-bastion", Tags: clusterScope.BastionTags(), }) @@ -137,7 +138,7 @@ func TestServiceReconcile(t *testing.T) { fakeCloud := cloudfake.New() clusterScope := newTestClusterScope(t, true, fakeCloud) - server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + server, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ Name: "test-cluster-bastion", Tags: clusterScope.BastionTags(), }) @@ -171,7 +172,7 @@ func TestServiceReconcile(t *testing.T) { fakeCloud := cloudfake.New() clusterScope := newTestClusterScope(t, true, fakeCloud) - server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + server, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ Name: "test-cluster-bastion", Tags: clusterScope.BastionTags(), }) @@ -203,7 +204,7 @@ func TestServiceReconcile(t *testing.T) { fakeCloud := cloudfake.New() clusterScope := newTestClusterScope(t, true, fakeCloud) - server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + server, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ Name: "test-cluster-bastion", Tags: clusterScope.BastionTags(), }) @@ -261,7 +262,7 @@ func TestServiceDelete(t *testing.T) { fakeCloud := cloudfake.New() clusterScope := newTestClusterScope(t, true, fakeCloud) - server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + server, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ Name: "test-cluster-bastion", Tags: clusterScope.BastionTags(), }) @@ -300,7 +301,7 @@ func TestServiceDelete(t *testing.T) { fakeCloud := cloudfake.New() clusterScope := newTestClusterScope(t, true, fakeCloud) - server, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + server, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ Name: "test-cluster-bastion", Tags: clusterScope.BastionTags(), }) diff --git a/cloud/services/publicip/service.go b/cloud/services/publicip/service.go index 62cc0c0..6fbb8d3 100644 --- a/cloud/services/publicip/service.go +++ b/cloud/services/publicip/service.go @@ -11,12 +11,9 @@ You may obtain a copy of the License at package publicip import ( - "context" - iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) // PublicIP describes a STACKIT public IP resource. @@ -26,18 +23,8 @@ type PublicIP struct { NetworkInterfaceID string } -// Client defines the cloud API operations required by the public IP service. -type Client interface { - FindServerByTags(ctx context.Context, tags map[string]string) (*shared.Server, error) -} - // Scope is an interface that provides cluster scope data needed for public IP reconciliation. -type Scope[C Client] interface { - scopeData - Cloud() C -} - -type scopeData interface { +type Scope interface { ProjectID() string Region() string IaaSClient() *iaas.APIClient @@ -49,16 +36,14 @@ type scopeData interface { // Service implements public IPs reconciler. type Service struct { - scope scopeData - client Client + scope Scope iaasClient *iaas.APIClient } // New returns Service from given scope. -func New[C Client](scope Scope[C]) *Service { +func New(scope Scope) *Service { return &Service{ scope: scope, - client: scope.Cloud(), iaasClient: scope.IaaSClient(), } } diff --git a/cloud/services/server/reconcile.go b/cloud/services/server/reconcile.go new file mode 100644 index 0000000..83655a5 --- /dev/null +++ b/cloud/services/server/reconcile.go @@ -0,0 +1,278 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package server + +import ( + "context" + "encoding/base64" + "fmt" + + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + logf "sigs.k8s.io/controller-runtime/pkg/log" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +func (s *Service) Reconcile(ctx context.Context) error { + log := logf.FromContext(ctx) + spec := s.scope.ServerSpec() + if err := spec.validate(); err != nil { + return err + } + + srv, created, err := s.ensureServer(ctx, spec) + if err != nil { + return err + } + if spec.SetServer != nil { + spec.SetServer(srv, created) + } + log.V(1).Info("Reconciled server resources", "serverID", srv.ID, "created", created) + return nil +} + +func (s *Service) Delete(ctx context.Context) error { + log := logf.FromContext(ctx) + spec := s.scope.ServerSpec() + if spec.ID == "" && len(spec.Tags) == 0 { + return fmt.Errorf("%w: server ID or tags are required", shared.ErrInvalidInput) + } + + deletedID, err := s.deleteBySpec(ctx, spec) + if err != nil { + return err + } + if deletedID != "" { + log.V(1).Info("Deleted server", "serverID", deletedID) + } + if spec.ClearServer != nil { + spec.ClearServer() + } + return nil +} + +// Get retrieves a STACKIT server by ID. +func (c *Client) Get(ctx context.Context, id string) (*Server, error) { + if id == "" { + return nil, fmt.Errorf("%w: server ID is empty", shared.ErrInvalidInput) + } + srv, err := c.iaasClient.DefaultAPI.GetServer(ctx, c.projectID, c.region, id).Execute() + if err != nil { + return nil, shared.ClassifySDKError("get server", err) + } + return c.serverFromSDK(ctx, srv), nil +} + +// FindByTags finds a single STACKIT server matching the given tag selector. +func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*Server, error) { + matched, err := c.ListByTags(ctx, tags) + if err != nil { + return nil, err + } + if len(matched) == 0 { + return nil, fmt.Errorf("%w: no server matching tags", shared.ErrNotFound) + } + if len(matched) > 1 { + return nil, fmt.Errorf("%w: multiple servers matching tags", shared.ErrConflict) + } + return matched[0], nil +} + +// ListByTags lists STACKIT servers matching the given tag selector. +func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*Server, error) { + if len(tags) == 0 { + return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) + } + resp, err := c.iaasClient.DefaultAPI.ListServers(ctx, c.projectID, c.region). + LabelSelector(shared.LabelSelector(tags)). + Details(true). + Execute() + if err != nil { + return nil, shared.ClassifySDKError("list servers", err) + } + matched := []*Server{} + for _, srv := range resp.GetItems() { + if shared.LabelsContainTags(srv.GetLabels(), tags) { + srv := srv + matched = append(matched, c.serverFromSDK(ctx, &srv)) + } + } + return matched, nil +} + +// DeleteByID deletes a server by ID, ignoring NotFound errors. +func (c *Client) DeleteByID(ctx context.Context, id string) error { + if id == "" { + return fmt.Errorf("%w: server ID is empty", shared.ErrInvalidInput) + } + if err := c.iaasClient.DefaultAPI.DeleteServer(ctx, c.projectID, c.region, id).Execute(); err != nil { + err := shared.ClassifySDKError("delete server", err) + if !shared.IsNotFound(err) { + return err + } + } + return nil +} + +func (c *Client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bool, error) { + if spec.ID != "" { + srv, err := c.Get(ctx, spec.ID) + if err == nil { + return srv, false, nil + } + if !shared.IsNotFound(err) { + return nil, false, err + } + } + if srv, err := c.FindByTags(ctx, spec.Tags); err == nil { + return srv, false, nil + } else if !shared.IsNotFound(err) { + return nil, false, err + } + + // Recreating an already-provisioned server would replay bootstrap data + // pinned to the previous identity, so surface the loss and let Cluster API + // replace the Machine. + if spec.DisallowRecreate { + return nil, false, fmt.Errorf( + "%w: server %s; the Machine must be replaced", + ErrServerGone, spec.ID, + ) + } + + payload := spec.toCreatePayload() + created, err := c.iaasClient.DefaultAPI.CreateServer(ctx, c.projectID, c.region). + CreateServerPayload(payload). + Execute() + if err != nil { + return nil, false, shared.ClassifySDKError("create server", err) + } + out := c.serverFromSDK(ctx, created) + logf.FromContext(ctx).V(1).Info("Created server", "name", spec.Name, "serverID", out.ID) + return out, true, nil +} + +func (c *Client) deleteBySpec(ctx context.Context, spec ServerSpec) (string, error) { + if spec.ID != "" { + err := c.iaasClient.DefaultAPI.DeleteServer(ctx, c.projectID, c.region, spec.ID).Execute() + if err == nil { + return spec.ID, nil + } + err = shared.ClassifySDKError("delete server", err) + if !shared.IsNotFound(err) { + return "", err + } + } + if len(spec.Tags) == 0 { + return "", nil + } + srv, err := c.FindByTags(ctx, spec.Tags) + if err != nil { + if shared.IsNotFound(err) { + return "", nil + } + return "", err + } + if err := c.DeleteByID(ctx, srv.ID); err != nil { + return "", err + } + return srv.ID, nil +} + +func (s ServerSpec) validate() error { + if s.Name == "" { + return fmt.Errorf("%w: server name is required", shared.ErrInvalidInput) + } + if s.ImageID == "" { + return fmt.Errorf("%w: server image ID is required", shared.ErrInvalidInput) + } + if s.MachineType == "" { + return fmt.Errorf("%w: server machine type is required", shared.ErrInvalidInput) + } + if s.NetworkID == "" { + return fmt.Errorf("%w: server network ID is required", shared.ErrInvalidInput) + } + if len(s.Tags) == 0 { + return fmt.Errorf("%w: server tags are required", shared.ErrInvalidInput) + } + return nil +} + +func (s ServerSpec) toCreatePayload() iaas.CreateServerPayload { + payload := &iaas.CreateServerPayload{} + payload.SetName(s.Name) + payload.SetMachineType(s.MachineType) + useBootVolume := s.RootVolume.SizeGiB > 0 || s.RootVolume.PerformanceClass != "" + if s.ImageID != "" && !useBootVolume { + payload.SetImageId(s.ImageID) + } + if len(s.Tags) > 0 { + payload.SetLabels(shared.TagsToSDKLabels(s.Tags)) + } + networking := iaas.NewCreateServerNetworking() + if s.NetworkID != "" { + networking.SetNetworkId(s.NetworkID) + } + payload.SetNetworking(iaas.CreateServerNetworkingAsCreateServerPayloadAllOfNetworking(networking)) + if len(s.SecurityGroups) > 0 { + payload.SetSecurityGroups(s.SecurityGroups) + } + if len(s.UserData) > 0 { + payload.SetUserData(base64.StdEncoding.EncodeToString(s.UserData)) + payload.SetConfigDrive(true) + } + if useBootVolume { + bootVolume := iaas.NewBootVolume() + if s.ImageID != "" { + bootVolume.SetSource(*iaas.NewBootVolumeSource(s.ImageID, "image")) + } + if s.RootVolume.SizeGiB > 0 { + bootVolume.SetSize(int64(s.RootVolume.SizeGiB)) + } + if s.RootVolume.PerformanceClass != "" { + bootVolume.SetPerformanceClass(s.RootVolume.PerformanceClass) + } + bootVolume.SetDeleteOnTermination(s.RootVolume.DeleteOnTermination) + payload.SetBootVolume(*bootVolume) + } + if s.SSHKeyName != "" { + payload.SetKeypairName(s.SSHKeyName) + } + if s.AvailabilityZone != "" { + payload.SetAvailabilityZone(s.AvailabilityZone) + } + return *payload +} + +func (c *Client) serverFromSDK(ctx context.Context, srv *iaas.Server) *Server { + if srv == nil { + return nil + } + out := &Server{ + ID: srv.GetId(), + Name: srv.GetName(), + State: srv.GetStatus(), + PowerStatus: srv.GetPowerStatus(), + AvailabilityZone: srv.GetAvailabilityZone(), + } + nics, err := c.iaasClient.DefaultAPI.ListServerNICs(ctx, c.projectID, c.region, out.ID).Execute() + if err == nil { + for _, nic := range nics.GetItems() { + if ipv4 := nic.GetIpv4(); ipv4 != "" { + out.Addresses = append(out.Addresses, Address{Type: "InternalIP", Address: ipv4}) + } + if ipv6 := nic.GetIpv6(); ipv6 != "" { + out.Addresses = append(out.Addresses, Address{Type: "InternalIP", Address: ipv6}) + } + } + } + return out +} diff --git a/cloud/services/server/reconcile_test.go b/cloud/services/server/reconcile_test.go new file mode 100644 index 0000000..ae01d09 --- /dev/null +++ b/cloud/services/server/reconcile_test.go @@ -0,0 +1,630 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package server_test + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +const ( + testProjectID = "00000000-0000-0000-0000-000000000000" + testRegion = "eu01" + testServerID = "33333333-3333-4333-8333-333333333333" + testOtherServerID = "44444444-4444-4444-8444-444444444444" + testNetworkID = "11111111-1111-4111-8111-111111111111" + testImageID = "22222222-2222-4222-8222-222222222222" + testSecurityGroupID = "55555555-5555-4555-8555-555555555555" + testBoolTrue = "true" +) + +type testScope struct { + projectID string + region string + iaasClient *iaas.APIClient + spec server.ServerSpec +} + +func (s *testScope) ProjectID() string { + return s.projectID +} + +func (s *testScope) Region() string { + return s.region +} + +func (s *testScope) IaaSClient() *iaas.APIClient { + return s.iaasClient +} + +func (s *testScope) ServerSpec() server.ServerSpec { + return s.spec +} + +func newTestIaaSClient(t *testing.T, handler http.Handler) *iaas.APIClient { + t.Helper() + + srv := httptest.NewServer(handler) + t.Cleanup(srv.Close) + + iaasClient, err := iaas.NewAPIClient( + config.WithoutAuthentication(), + config.WithEndpoint(srv.URL), + ) + if err != nil { + t.Fatalf("iaas.NewAPIClient() error = %v", err) + } + return iaasClient +} + +func newTestService(t *testing.T, spec server.ServerSpec, handler http.Handler) *server.Service { + t.Helper() + + return server.New(&testScope{ + projectID: testProjectID, + region: testRegion, + iaasClient: newTestIaaSClient(t, handler), + spec: spec, + }) +} + +func writeJSON(t *testing.T, w http.ResponseWriter, value any) { + t.Helper() + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(value); err != nil { + t.Fatalf("encode response body: %v", err) + } +} + +func readJSON(t *testing.T, r *http.Request) map[string]any { + t.Helper() + var out map[string]any + if err := json.NewDecoder(r.Body).Decode(&out); err != nil { + t.Fatalf("decode request body: %v", err) + } + return out +} + +func assertStringField(t *testing.T, m map[string]any, key, want string) { + t.Helper() + if got := lookup(m, key); got != want { + t.Fatalf("%s = %#v, want %q in %#v", key, got, want, m) + } +} + +func assertBoolField(t *testing.T, m map[string]any, key string, want bool) { + t.Helper() + if got := lookup(m, key); got != want { + t.Fatalf("%s = %#v, want %t in %#v", key, got, want, m) + } +} + +func assertFieldAbsent(t *testing.T, payload map[string]any, key string) { + t.Helper() + if _, ok := payload[key]; ok { + t.Fatalf("%s present in payload, want absent: %#v", key, payload[key]) + } +} + +func assertNestedStringField(t *testing.T, m map[string]any, path []string, want string) { + t.Helper() + if got := nestedValue(t, m, path); got != want { + t.Fatalf("%s = %#v, want %q in %#v", strings.Join(path, "."), got, want, m) + } +} + +func assertNestedNumberField(t *testing.T, m map[string]any, path []string, want float64) { + t.Helper() + if got := nestedValue(t, m, path); got != want { + t.Fatalf("%s = %#v, want %v in %#v", strings.Join(path, "."), got, want, m) + } +} + +func nestedValue(t *testing.T, m map[string]any, path []string) any { + t.Helper() + + var current any = m + for _, part := range path { + switch typed := current.(type) { + case map[string]any: + current = lookup(typed, part) + default: + t.Fatalf("path element %q cannot descend into %#v", part, typed) + } + } + return current +} + +func lookup(m map[string]any, key string) any { + for _, candidate := range []string{ + key, + strings.ToUpper(key[:1]) + key[1:], + } { + if value, ok := m[candidate]; ok { + return value + } + } + return nil +} + +func TestServiceReconcile(t *testing.T) { + t.Run("creates server with bootVolume and expected payload when not found", func(t *testing.T) { + var ( + createPayload map[string]any + recordedServer *server.Server + wasCreated bool + ) + spec := server.ServerSpec{ + Name: "node-0", + MachineType: "c2i.2", + ImageID: testImageID, + AvailabilityZone: "eu01-1", + SSHKeyName: "default", + NetworkID: testNetworkID, + SecurityGroups: []string{testSecurityGroupID}, + UserData: []byte("#cloud-config\n"), + RootVolume: server.RootVolume{ + SizeGiB: 50, + PerformanceClass: "storage_premium_perf6", + DeleteOnTermination: true, + }, + Tags: map[string]string{"cluster": "test"}, + SetServer: func(srv *server.Server, created bool) { + recordedServer = srv + wasCreated = created + }, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers"): + if got := r.URL.Query().Get("label_selector"); got != "cluster=test" { + t.Fatalf("label_selector = %q, want cluster=test", got) + } + if got := r.URL.Query().Get("details"); got != testBoolTrue { + t.Fatalf("details = %q, want true", got) + } + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/servers"): + createPayload = readJSON(t, r) + writeJSON(t, w, map[string]any{ + "id": testServerID, + "machineType": "c2i.2", + "name": "node-0", + "status": "ACTIVE", + "availabilityZone": "eu01-1", + }) + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID+"/nics"): + writeJSON(t, w, map[string]any{ + "items": []any{ + map[string]any{"ipv4": "10.0.0.10", "ipv6": "fd00::10"}, + }, + }) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if !wasCreated { + t.Fatal("SetServer created = false, want true") + } + if recordedServer == nil || recordedServer.ID != testServerID || recordedServer.Name != "node-0" || recordedServer.State != "ACTIVE" { + t.Fatalf("recordedServer = %#v", recordedServer) + } + if len(recordedServer.Addresses) != 2 { + t.Fatalf("recordedServer.Addresses = %#v, want 2 addresses", recordedServer.Addresses) + } + + assertStringField(t, createPayload, "name", "node-0") + assertStringField(t, createPayload, "machineType", "c2i.2") + assertFieldAbsent(t, createPayload, "imageId") + assertStringField(t, createPayload, "availabilityZone", "eu01-1") + assertStringField(t, createPayload, "keypairName", "default") + assertStringField(t, createPayload, "userData", "I2Nsb3VkLWNvbmZpZwo=") + assertBoolField(t, createPayload, "configDrive", true) + assertNestedStringField(t, createPayload, []string{"labels", "cluster"}, "test") + assertNestedStringField(t, createPayload, []string{"networking", "networkId"}, testNetworkID) + assertNestedStringField(t, createPayload, []string{"bootVolume", "performanceClass"}, "storage_premium_perf6") + assertNestedStringField(t, createPayload, []string{"bootVolume", "source", "id"}, testImageID) + assertNestedStringField(t, createPayload, []string{"bootVolume", "source", "type"}, "image") + assertNestedNumberField(t, createPayload, []string{"bootVolume", "size"}, 50) + }) + + t.Run("creates server with imageId when rootVolume size and performanceClass are unset", func(t *testing.T) { + var createPayload map[string]any + spec := server.ServerSpec{ + Name: "node-0", + MachineType: "c2i.2", + ImageID: testImageID, + NetworkID: testNetworkID, + Tags: map[string]string{"cluster": "test"}, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers"): + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/servers"): + createPayload = readJSON(t, r) + writeJSON(t, w, map[string]any{ + "id": testServerID, + "machineType": "c2i.2", + "name": "node-0", + "status": "ACTIVE", + }) + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID+"/nics"): + writeJSON(t, w, map[string]any{"items": []any{}}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + assertStringField(t, createPayload, "imageId", testImageID) + assertFieldAbsent(t, createPayload, "bootVolume") + }) +} + +func TestServiceReconcileReuseAndErrors(t *testing.T) { + t.Run("reuses existing server by ID without listing by tags or creating duplicate", func(t *testing.T) { + var ( + recordedServer *server.Server + wasCreated = true + ) + spec := server.ServerSpec{ + ID: testServerID, + Name: "node-0", + MachineType: "c2i.2", + ImageID: testImageID, + NetworkID: testNetworkID, + Tags: map[string]string{"cluster": "test"}, + SetServer: func(srv *server.Server, created bool) { + recordedServer = srv + wasCreated = created + }, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID): + writeJSON(t, w, map[string]any{ + "id": testServerID, + "machineType": "c2i.2", + "name": "node-0", + "status": "ACTIVE", + }) + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID+"/nics"): + writeJSON(t, w, map[string]any{"items": []any{}}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if wasCreated { + t.Fatal("SetServer created = true, want false") + } + if recordedServer == nil || recordedServer.ID != testServerID { + t.Fatalf("recordedServer = %#v, want ID %s", recordedServer, testServerID) + } + }) + + t.Run("reuses existing server found by tags without creating duplicate", func(t *testing.T) { + var ( + recordedServer *server.Server + wasCreated = true + ) + spec := server.ServerSpec{ + Name: "node-0", + MachineType: "c2i.2", + ImageID: testImageID, + NetworkID: testNetworkID, + Tags: map[string]string{"cluster": "test"}, + SetServer: func(srv *server.Server, created bool) { + recordedServer = srv + wasCreated = created + }, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{ + "id": testServerID, + "machineType": "c2i.2", + "name": "node-0", + "status": "ACTIVE", + "labels": map[string]any{"cluster": "test"}, + }, + }}) + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID+"/nics"): + writeJSON(t, w, map[string]any{"items": []any{}}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if wasCreated { + t.Fatal("SetServer created = true, want false") + } + if recordedServer == nil || recordedServer.ID != testServerID { + t.Fatalf("recordedServer = %#v, want ID %s", recordedServer, testServerID) + } + }) + + t.Run("returns ErrServerGone when DisallowRecreate is true and server is missing", func(t *testing.T) { + spec := server.ServerSpec{ + ID: testServerID, + Name: "node-0", + MachineType: "c2i.2", + ImageID: testImageID, + NetworkID: testNetworkID, + Tags: map[string]string{"cluster": "test"}, + DisallowRecreate: true, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID): + w.WriteHeader(http.StatusNotFound) + writeJSON(t, w, map[string]any{"message": "not found"}) + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers"): + writeJSON(t, w, map[string]any{"items": []any{}}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + err := svc.Reconcile(context.Background()) + if !server.IsServerGone(err) { + t.Fatalf("Reconcile() error = %v, want ErrServerGone", err) + } + }) + + t.Run("returns ErrConflict when multiple servers match tags", func(t *testing.T) { + spec := server.ServerSpec{ + Name: "node-0", + MachineType: "c2i.2", + ImageID: testImageID, + NetworkID: testNetworkID, + Tags: map[string]string{"cluster": "test"}, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testServerID, "name": "node-0", "machineType": "c2i.2", "labels": map[string]any{"cluster": "test"}}, + map[string]any{"id": testOtherServerID, "name": "node-1", "machineType": "c2i.2", "labels": map[string]any{"cluster": "test"}}, + }}) + case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/nics"): + writeJSON(t, w, map[string]any{"items": []any{}}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + err := svc.Reconcile(context.Background()) + if !errors.Is(err, shared.ErrConflict) { + t.Fatalf("Reconcile() error = %v, want ErrConflict", err) + } + }) +} + +func TestServiceReconcileValidation(t *testing.T) { + tests := []struct { + name string + spec server.ServerSpec + }{ + { + name: "missing name", + spec: server.ServerSpec{ + ImageID: testImageID, + MachineType: "c2i.2", + NetworkID: testNetworkID, + Tags: map[string]string{"cluster": "test"}, + }, + }, + { + name: "missing image ID", + spec: server.ServerSpec{ + Name: "node-0", + MachineType: "c2i.2", + NetworkID: testNetworkID, + Tags: map[string]string{"cluster": "test"}, + }, + }, + { + name: "missing machine type", + spec: server.ServerSpec{ + Name: "node-0", + ImageID: testImageID, + NetworkID: testNetworkID, + Tags: map[string]string{"cluster": "test"}, + }, + }, + { + name: "missing network ID", + spec: server.ServerSpec{ + Name: "node-0", + ImageID: testImageID, + MachineType: "c2i.2", + Tags: map[string]string{"cluster": "test"}, + }, + }, + { + name: "missing tags", + spec: server.ServerSpec{ + Name: "node-0", + ImageID: testImageID, + MachineType: "c2i.2", + NetworkID: testNetworkID, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + svc := newTestService(t, tt.spec, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + err := svc.Reconcile(context.Background()) + if !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("Reconcile() error = %v, want ErrInvalidInput", err) + } + }) + } +} + +func TestServiceDelete(t *testing.T) { + t.Run("deletes server by ID when provided and calls ClearServer", func(t *testing.T) { + var ( + deletedID string + cleared bool + ) + spec := server.ServerSpec{ + ID: testServerID, + ClearServer: func() { + cleared = true + }, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodDelete && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID) { + deletedID = testServerID + w.WriteHeader(http.StatusNoContent) + return + } + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v", err) + } + if deletedID != testServerID { + t.Fatalf("deletedID = %q, want %q", deletedID, testServerID) + } + if !cleared { + t.Fatal("ClearServer was not called") + } + }) + + t.Run("falls back to tag lookup when ID is empty", func(t *testing.T) { + var deletedID string + spec := server.ServerSpec{ + Tags: map[string]string{"cluster": "test"}, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testServerID, "name": "node-0", "machineType": "c2i.2", "labels": map[string]any{"cluster": "test"}}, + }}) + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID+"/nics"): + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodDelete && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID): + deletedID = testServerID + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v", err) + } + if deletedID != testServerID { + t.Fatalf("deletedID = %q, want %q", deletedID, testServerID) + } + }) + + t.Run("falls back to tag lookup when recorded ID no longer exists", func(t *testing.T) { + var deletedID string + spec := server.ServerSpec{ + ID: testOtherServerID, + Tags: map[string]string{"cluster": "test"}, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodDelete && strings.HasSuffix(r.URL.Path, "/servers/"+testOtherServerID): + w.WriteHeader(http.StatusNotFound) + writeJSON(t, w, map[string]any{"message": "not found"}) + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testServerID, "name": "node-0", "machineType": "c2i.2", "labels": map[string]any{"cluster": "test"}}, + }}) + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID+"/nics"): + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodDelete && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID): + deletedID = testServerID + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v", err) + } + if deletedID != testServerID { + t.Fatalf("deletedID = %q, want %q", deletedID, testServerID) + } + }) + + t.Run("succeeds when server is not found", func(t *testing.T) { + spec := server.ServerSpec{ + Tags: map[string]string{"cluster": "test"}, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + writeJSON(t, w, map[string]any{"items": []any{}}) + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v, want nil", err) + } + }) + + t.Run("returns ErrInvalidInput when both ID and tags are empty", func(t *testing.T) { + svc := newTestService(t, server.ServerSpec{}, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + if err := svc.Delete(context.Background()); !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("Delete() error = %v, want ErrInvalidInput", err) + } + }) +} diff --git a/cloud/services/server/service.go b/cloud/services/server/service.go new file mode 100644 index 0000000..c9821b1 --- /dev/null +++ b/cloud/services/server/service.go @@ -0,0 +1,111 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package server + +import ( + "errors" + + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" +) + +// ErrServerGone indicates that an already-provisioned server is missing in +// STACKIT and must not be recreated with stale bootstrap data. +var ErrServerGone = errors.New("server gone") + +// IsServerGone reports whether err wraps ErrServerGone. +func IsServerGone(err error) bool { + return errors.Is(err, ErrServerGone) +} + +// Server describes a STACKIT compute instance in provider-neutral terms. +type Server struct { + ID string + Name string + State string + PowerStatus string + ProviderID string + AvailabilityZone string + Addresses []Address +} + +// Address is an IP or DNS endpoint of a Server. +type Address struct { + Type string + Address string +} + +// RootVolume describes the root disk of a VM. +type RootVolume struct { + SizeGiB int + PerformanceClass string + DeleteOnTermination bool +} + +// ServerSpec describes the desired state of a STACKIT server. +type ServerSpec struct { + ID string + Name string + ImageID string + MachineType string + AvailabilityZone string + SSHKeyName string + NetworkID string + SecurityGroups []string + UserData []byte + Tags map[string]string + RootVolume RootVolume + DisallowRecreate bool + SetServer func(server *Server, created bool) + ClearServer func() +} + +// ClientScope is an interface that provides the IaaS client and target scope for server operations. +type ClientScope interface { + ProjectID() string + Region() string + IaaSClient() *iaas.APIClient +} + +// Scope is an interface that provides scope data needed for server reconciliation. +type Scope interface { + ClientScope + ServerSpec() ServerSpec +} + +// Client provides direct server operations against the STACKIT IaaS API. +type Client struct { + iaasClient *iaas.APIClient + projectID string + region string +} + +// NewClient returns a Client for the given scope. +func NewClient(scope ClientScope) *Client { + return &Client{ + iaasClient: scope.IaaSClient(), + projectID: scope.ProjectID(), + region: scope.Region(), + } +} + +// Service implements server reconciler. +type Service struct { + scope Scope + *Client +} + +// New returns Service from given scope. +func New(scope Scope) *Service { + return &Service{ + scope: scope, + Client: NewClient(scope), + } +} diff --git a/cloud/services/shared/types.go b/cloud/services/shared/types.go deleted file mode 100644 index a413a30..0000000 --- a/cloud/services/shared/types.go +++ /dev/null @@ -1,28 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -package shared - -// Server describes a STACKIT compute instance in provider-neutral terms. -type Server struct { - ID string - Name string - State string - PowerStatus string - ProviderID string - AvailabilityZone string - Addresses []Address -} - -// Address is an IP or DNS endpoint of a Server. -type Address struct { - Type string - Address string -} diff --git a/cloud/types.go b/cloud/types.go index d9720a2..f618eed 100644 --- a/cloud/types.go +++ b/cloud/types.go @@ -17,14 +17,17 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" ) // Server describes a STACKIT compute instance in provider-neutral terms. -type Server = shared.Server +type Server = server.Server // Address is an IP or DNS endpoint of a Server. -type Address = shared.Address +type Address = server.Address + +// RootVolume describes the root disk of a VM. +type RootVolume = server.RootVolume // Network is an existing STACKIT virtual network referenced by the provider. type Network struct { @@ -42,54 +45,6 @@ type PublicIP = publicip.PublicIP // SecurityGroup describes a STACKIT security group. type SecurityGroup = securitygroup.SecurityGroup -// Bastion describes the provider-managed SSH bastion resources. -type Bastion struct { - ServerID string - ServerState string - ServerPowerStatus string - SecurityGroupID string -} - -// CreateServerInput holds all parameters required to create a new VM. -type CreateServerInput struct { - Name string - ProjectID string - Region string - ImageID string - MachineType string - AvailabilityZone string - SSHKeyName string - NetworkID string - SecurityGroups []string - UserData []byte - Tags map[string]string - RootVolume RootVolumeInput -} - -// BastionInput holds all parameters required to ensure a bastion host. -type BastionInput struct { - Name string - ProjectID string - Region string - NetworkID string - ImageID string - MachineType string - AvailabilityZone string - SSHKeyName string - AllowedCIDRs []string - SecurityGroupID string - Tags map[string]string - RootVolume RootVolumeInput - CloudInit []byte -} - -// RootVolumeInput describes the root disk of a VM. -type RootVolumeInput struct { - SizeGiB int - PerformanceClass string - DeleteOnTermination bool -} - // LoadBalancerTargetInput describes a VM target in the API-server load // balancer target pool. type LoadBalancerTargetInput = loadbalancer.Target diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index 8c7d888..23e50b7 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -28,9 +28,9 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" - bastionservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/bastion" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/scope" ) @@ -40,16 +40,10 @@ const bastionDisabledReason = "Skipped" func (r *StackitClusterReconciler) reconcileBastion( ctx context.Context, - cloudClient cloud.Client, clusterScope *scope.ClusterScope, ) (ctrl.Result, bool, error) { log := logf.FromContext(ctx) stackitCluster := clusterScope.StackitCluster - input := bastionservice.Input(stackitCluster, nil) - status := cloud.Bastion{ - ServerID: stackitCluster.Status.Bastion.ServerID, - SecurityGroupID: stackitCluster.Status.Bastion.SecurityGroupID, - } if !stackitCluster.Spec.Bastion.Enabled { // The reason is set only after a cleanup has succeeded, so anything else @@ -57,7 +51,7 @@ func (r *StackitClusterReconciler) reconcileBastion( // the status keeps the tag-based cleanup to once per cluster. condition := meta.FindStatusCondition(stackitCluster.Status.Conditions, infrav1.ClusterBastionReadyCondition) if condition == nil || condition.Reason != bastionDisabledReason { - if err := r.deleteBastionResources(ctx, cloudClient, clusterScope, input, status, false); err != nil { + if err := r.deleteBastionResources(ctx, clusterScope, false); err != nil { return ctrl.Result{}, false, err } clusterScope.ClearBastionStatus() @@ -96,10 +90,10 @@ func (r *StackitClusterReconciler) reconcileBastion( ) return ctrl.Result{}, false, nil } - input.CloudInit = cloudInit + clusterScope.SetBastionCloudInit(cloudInit, bastionCloudInitHash(cloudInit)) if bastionNeedsRecreate(stackitCluster, cloudInit) { - if err := r.deleteBastionResources(ctx, cloudClient, clusterScope, input, status, true); err != nil { + if err := r.deleteBastionResources(ctx, clusterScope, true); err != nil { return ctrl.Result{}, false, err } clusterScope.ClearBastionStatus() @@ -117,28 +111,22 @@ func (r *StackitClusterReconciler) reconcileBastion( } hadBastionServer := stackitCluster.Status.Bastion.ServerID != "" - // Temporary ordering until bastion server reconciliation is extracted into a - // service: ensure the security group first so its ID can be attached to the - // bastion server by EnsureBastion. - if err := securitygroup.New(clusterScope).Reconcile(ctx); err != nil { - return ctrl.Result{}, false, err - } - input.SecurityGroupID = stackitCluster.Status.Bastion.SecurityGroupID - - bastion, err := cloudClient.EnsureBastion(ctx, input) - if err != nil { + if err := server.New(clusterScope).Reconcile(ctx); err != nil { return ctrl.Result{}, false, err } - clusterScope.SetBastionStatus(bastion, bastionCloudInitHash(cloudInit)) - if !hadBastionServer { + if !hadBastionServer && stackitCluster.Status.Bastion.ServerID != "" { record.Eventf( - stackitCluster, "BastionCreated", "Created bastion %s", bastion.ServerID, + stackitCluster, "BastionCreated", "Created bastion %s", stackitCluster.Status.Bastion.ServerID, ) } + if err := securitygroup.New(clusterScope).Reconcile(ctx); err != nil { + return ctrl.Result{}, false, err + } if err := publicip.New(clusterScope).Reconcile(ctx); err != nil { return ctrl.Result{}, false, err } - if ready, reason, message, warn := serverStateCondition(bastion.ServerState, bastion.ServerPowerStatus); !ready { + state, powerStatus := clusterScope.BastionServerState() + if ready, reason, message, warn := serverStateCondition(state, powerStatus); !ready { previousReason := "" if previous := meta.FindStatusCondition(stackitCluster.Status.Conditions, infrav1.ClusterBastionReadyCondition); previous != nil { previousReason = previous.Reason @@ -146,7 +134,7 @@ func (r *StackitClusterReconciler) reconcileBastion( clusterScope.SetNotReady(reason, "bastion "+message, infrav1.ClusterBastionReadyCondition, infrav1.ClusterReadyCondition) // Only warn on entering the state, not on every requeue. if warn && previousReason != reason { - record.Warnf(stackitCluster, reason, "Bastion server %s: %s", bastion.ServerID, message) + record.Warnf(stackitCluster, reason, "Bastion server %s: %s", stackitCluster.Status.Bastion.ServerID, message) } return ctrl.Result{RequeueAfter: 15 * time.Second}, false, nil } @@ -195,10 +183,7 @@ func hasBastionStatus(status infrav1.StackitBastionStatus) bool { func (r *StackitClusterReconciler) deleteBastionResources( ctx context.Context, - cloudClient cloud.Client, clusterScope *scope.ClusterScope, - input cloud.BastionInput, - status cloud.Bastion, ignoreNotFound bool, ) error { if err := publicip.New(clusterScope).Delete(ctx); err != nil { @@ -206,15 +191,12 @@ func (r *StackitClusterReconciler) deleteBastionResources( return err } } - if err := cloudClient.DeleteBastion(ctx, input, status); err != nil { + if err := securitygroup.New(clusterScope).Delete(ctx); err != nil { if !ignoreNotFound || !cloud.IsNotFound(err) { return err } } - // Temporary ordering until bastion server reconciliation is extracted into a - // service: the security group must be deleted after the server is detached - // and deleted by DeleteBastion. - if err := securitygroup.New(clusterScope).Delete(ctx); err != nil { + if err := server.New(clusterScope).Delete(ctx); err != nil { if !ignoreNotFound || !cloud.IsNotFound(err) { return err } diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index 3898f6d..6050f81 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -29,6 +29,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud" cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -202,7 +203,7 @@ var _ = Describe("StackitCluster Controller", func() { nodeSGs, err := fakeCloud.ListSecurityGroupsByTags(ctx, scope.NodeSSHTags(got)) Expect(err).NotTo(HaveOccurred()) Expect(nodeSGs).To(HaveLen(1)) - workerServer, err := fakeCloud.CreateServer(ctx, cloud.CreateServerInput{ + workerServer, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ Name: got.Name + "-worker-0", Tags: util.MachineTags(got.Name, got.Namespace, "worker-0", "uid-1", nil), SecurityGroups: []string{nodeSGs[0].ID}, @@ -537,7 +538,7 @@ var _ = Describe("StackitCluster Controller", func() { got.Spec.Bastion = validBastionSpec() Expect(k8sClient.Update(ctx, got)).To(Succeed()) - fakeCloud.FailNextEnsureBastion = fmt.Errorf("bastion creation rejected: %w", cloud.ErrInvalidInput) + fakeCloud.FailNextCreateServer = fmt.Errorf("bastion creation rejected: %w", cloud.ErrInvalidInput) result, err := reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) @@ -1018,7 +1019,7 @@ var _ = Describe("StackitCluster Controller", func() { Expect(err).NotTo(HaveOccurred()) Expect(result).To(Equal(reconcile.Result{})) - Expect(fakeCloud.LastEnsureBastionInput.AvailabilityZone).To(Equal("eu01-2")) + Expect(fakeCloud.LastCreateServerInput.AvailabilityZone).To(Equal("eu01-2")) Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) server, err := fakeCloud.GetServer(ctx, got.Status.Bastion.ServerID) Expect(err).NotTo(HaveOccurred()) diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index e1d4c58..b94d69f 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -31,10 +31,10 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" - bastionservice "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/bastion" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -249,7 +249,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS return ctrl.Result{}, nil } - if result, ready, err := r.reconcileBastion(ctx, cloudClient, clusterScope); err != nil { + if result, ready, err := r.reconcileBastion(ctx, clusterScope); err != nil { log.Error(err, "Failed to reconcile bastion", "cluster", stackitCluster.Name, ) @@ -371,9 +371,9 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS } // Cleanup runs unconditionally: neither spec nor status is a trustworthy - // record of what exists in the cloud. DeleteBastion and the load balancer, - // public IP, and security group services fall back to tag lookups and - // tolerate NotFound. + // record of what exists in the cloud. The load balancer, public IP, + // security group, and server services fall back to tag lookups and tolerate + // NotFound. cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, stackitCluster) if err != nil { // A missing credentials Secret cannot be recovered from and commonly @@ -422,22 +422,18 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS "Failed to delete bastion public IP: %v", err) return ctrl.Result{}, err } - - if err := cloudClient.DeleteBastion(ctx, bastionservice.Input(stackitCluster, nil), cloud.Bastion{ - ServerID: stackitCluster.Status.Bastion.ServerID, - SecurityGroupID: stackitCluster.Status.Bastion.SecurityGroupID, - }); err != nil && !cloud.IsNotFound(err) { - log.Error(err, "Failed to delete bastion", "cluster", stackitCluster.Name) - record.Warnf(stackitCluster, "FailedDeleteBastion", - "Failed to delete bastion: %v", err) - return ctrl.Result{}, err - } if err := securitygroup.New(clusterScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { log.Error(err, "Failed to delete bastion security groups", "cluster", stackitCluster.Name) record.Warnf(stackitCluster, "FailedDeleteBastion", "Failed to delete bastion security groups: %v", err) return ctrl.Result{}, err } + if err := server.New(clusterScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { + log.Error(err, "Failed to delete bastion", "cluster", stackitCluster.Name) + record.Warnf(stackitCluster, "FailedDeleteBastion", + "Failed to delete bastion: %v", err) + return ctrl.Result{}, err + } if hadBastion { clusterScope.ClearBastionStatus() record.Event(stackitCluster, "BastionDeleted", "Deleted bastion") diff --git a/controller/stackitmachine_infrastructure.go b/controller/stackitmachine_infrastructure.go index ba1ae6a..229a9d9 100644 --- a/controller/stackitmachine_infrastructure.go +++ b/controller/stackitmachine_infrastructure.go @@ -30,12 +30,11 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) -var errInstanceGone = errors.New("instance gone") - func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineScope *scope.MachineScope) (ctrl.Result, error) { log := logf.FromContext(ctx) stackitMachine := machineScope.StackitMachine @@ -94,8 +93,9 @@ func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineS machineScope.SetCloudClient(cloudClient) machineScope.SetConditions(metav1.ConditionTrue, "Available", "", infrav1.MachineCredentialsReadyCondition) - server, created, err := r.ensureServer(ctx, cloudClient, machineScope, bootstrapData) - if errors.Is(err, errInstanceGone) { + machineScope.SetBootstrapData(bootstrapData) + err = server.New(machineScope).Reconcile(ctx) + if server.IsServerGone(err) { machineScope.SetNotReady( "InstanceNotFound", err.Error(), @@ -138,17 +138,13 @@ func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineS infrav1.MachineReadyCondition, ) } - if created { + if machineScope.InstanceCreated() { record.Eventf( - stackitMachine, "InstanceCreated", "Created instance %s", server.ID, + stackitMachine, "InstanceCreated", "Created instance %s", stackitMachine.Status.InstanceID, ) } - stackitMachine.Status.InstanceState = server.State - stackitMachine.Status.Addresses = machineAddressesFromCloud(server.Addresses) - providerID := machineScope.SetInstance(server) - - if ready, reason, message, warn := serverStateCondition(server.State, server.PowerStatus); !ready { + if ready, reason, message, warn := serverStateCondition(stackitMachine.Status.InstanceState, machineScope.InstancePowerStatus()); !ready { previousReason := "" if previous := meta.FindStatusCondition(stackitMachine.Status.Conditions, infrav1.MachineInstanceReadyCondition); previous != nil { previousReason = previous.Reason @@ -156,16 +152,16 @@ func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineS machineScope.SetNotReady(reason, message, infrav1.MachineInstanceReadyCondition, infrav1.MachineReadyCondition) // Only warn on entering the state, not on every requeue. if warn && previousReason != reason { - record.Warnf(stackitMachine, reason, "Server %s: %s", server.ID, message) + record.Warnf(stackitMachine, reason, "Server %s: %s", stackitMachine.Status.InstanceID, message) } return ctrl.Result{RequeueAfter: 15 * time.Second}, nil } - if err := r.reconcileBastionNodeSSHAccess(ctx, machineScope, server); err != nil { + if err := r.reconcileBastionNodeSSHAccess(ctx, machineScope); err != nil { log.Error(err, "Failed to configure bastion SSH access for node", "machine", stackitMachine.Name) record.Warnf( stackitMachine, "FailedNodeSSHAccess", - "Failed to configure bastion SSH access for server %s: %v", server.ID, err, + "Failed to configure bastion SSH access for server %s: %v", stackitMachine.Status.InstanceID, err, ) machineScope.SetNotReady("BastionSSHAccessError", err.Error(), infrav1.MachineReadyCondition) return util.CloudFailureResult( @@ -180,7 +176,7 @@ func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineS } machineScope.SetReady() - log.V(1).Info("StackitMachine ready", "providerID", providerID) + log.V(1).Info("StackitMachine ready", "providerID", stackitMachine.Status.ProviderID) return ctrl.Result{}, nil } @@ -242,21 +238,15 @@ func (r *StackitMachineReconciler) reconcileDelete(ctx context.Context, machineS return resultErr } - instanceID, err := r.resolveServerForDeletion(ctx, cloudClient, machineScope) - if err != nil { - log.Error(err, "Failed to resolve server for deletion", "machine", stackitMachine.Name) - record.Warnf(stackitMachine, "FailedResolveInstance", - "Failed to resolve server for deletion: %v", err) + machineScope.SetCloudClient(cloudClient) + instanceID := stackitMachine.Status.InstanceID + if err := server.New(machineScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { + log.Error(err, "Failed to delete server", "machine", stackitMachine.Name, "instanceID", instanceID) + record.Warnf(stackitMachine, "FailedDeleteInstance", + "Failed to delete server %s: %v", instanceID, err) return err } if instanceID != "" { - if err := cloudClient.DeleteServer(ctx, instanceID); err != nil && !cloud.IsNotFound(err) { - log.Error(err, "Failed to delete server", "machine", stackitMachine.Name, "instanceID", instanceID) - record.Warnf(stackitMachine, "FailedDeleteInstance", - "Failed to delete server %s: %v", instanceID, err) - return err - } - machineScope.ClearInstance() record.Eventf( stackitMachine, "InstanceDeleted", "Deleted instance %s", instanceID, ) @@ -265,28 +255,6 @@ func (r *StackitMachineReconciler) reconcileDelete(ctx context.Context, machineS return nil } -// resolveServerForDeletion reports the ID of the server backing this machine, or -// an empty string once the cloud confirms none exists. It falls back to the tags, -// which carry the Machine UID, because a lost status patch can leave a running -// server that status.instanceID no longer names. -func (r *StackitMachineReconciler) resolveServerForDeletion( - ctx context.Context, - cloudClient cloud.Client, - machineScope *scope.MachineScope, -) (string, error) { - if instanceID := machineScope.StackitMachine.Status.InstanceID; instanceID != "" { - return instanceID, nil - } - server, err := cloudClient.FindServerByTags(ctx, machineScope.Tags()) - if err != nil { - if cloud.IsNotFound(err) { - return "", nil - } - return "", err - } - return server.ID, nil -} - func (r *StackitMachineReconciler) fetchBootstrapData(ctx context.Context, machine *clusterv1.Machine) ([]byte, metav1.ConditionStatus, string, string) { if machine.Spec.Bootstrap.DataSecretName == nil || *machine.Spec.Bootstrap.DataSecretName == "" { return nil, metav1.ConditionFalse, "BootstrapDataSecretMissing", "Machine.spec.bootstrap.dataSecretName is empty" @@ -306,67 +274,9 @@ func (r *StackitMachineReconciler) fetchBootstrapData(ctx context.Context, machi return data, metav1.ConditionTrue, "Available", "" } -func (r *StackitMachineReconciler) ensureServer( - ctx context.Context, - cloudClient cloud.Client, - machineScope *scope.MachineScope, - userData []byte, -) (*cloud.Server, bool, error) { - stackitMachine := machineScope.StackitMachine - tags := machineScope.Tags() - if stackitMachine.Status.InstanceID != "" { - server, err := cloudClient.GetServer(ctx, stackitMachine.Status.InstanceID) - if err == nil { - return server, false, nil - } - if !cloud.IsNotFound(err) { - return nil, false, err - } - } - if server, err := cloudClient.FindServerByTags(ctx, tags); err == nil { - return server, false, nil - } else if !cloud.IsNotFound(err) { - return nil, false, err - } - - // Recreating the server would replay bootstrap data pinned to the previous - // identity, so surface the loss and let Cluster API replace the Machine. - if stackitMachine.Status.Initialization.Provisioned { - return nil, false, fmt.Errorf( - "%w: server %s; the Machine must be replaced", - errInstanceGone, stackitMachine.Status.InstanceID, - ) - } - - deleteOnTermination := true - if stackitMachine.Spec.RootVolume.DeleteOnTermination != nil { - deleteOnTermination = *stackitMachine.Spec.RootVolume.DeleteOnTermination - } - server, err := cloudClient.CreateServer(ctx, cloud.CreateServerInput{ - Name: stackitMachine.Name, - ProjectID: machineScope.StackitCluster.Spec.ProjectID, - Region: machineScope.StackitCluster.Spec.Region, - ImageID: stackitMachine.Spec.ImageID, - MachineType: stackitMachine.Spec.MachineType, - AvailabilityZone: machineScope.AvailabilityZone(), - SSHKeyName: stackitMachine.Spec.SSHKeyName, - NetworkID: stackitMachine.Spec.Network.ID, - SecurityGroups: stackitMachine.Spec.SecurityGroups, - UserData: userData, - Tags: tags, - RootVolume: cloud.RootVolumeInput{ - SizeGiB: stackitMachine.Spec.RootVolume.SizeGiB, - PerformanceClass: stackitMachine.Spec.RootVolume.PerformanceClass, - DeleteOnTermination: deleteOnTermination, - }, - }) - return server, true, err -} - func (r *StackitMachineReconciler) reconcileBastionNodeSSHAccess( ctx context.Context, machineScope *scope.MachineScope, - server *cloud.Server, ) error { if !machineScope.StackitCluster.Spec.Bastion.Enabled { return nil @@ -374,7 +284,7 @@ func (r *StackitMachineReconciler) reconcileBastionNodeSSHAccess( if machineScope.StackitCluster.Status.Bastion.SecurityGroupID == "" { return fmt.Errorf("%w: bastion security group ID is empty", cloud.ErrTransient) } - if server == nil || server.ID == "" { + if machineScope.StackitMachine.Status.InstanceID == "" { return fmt.Errorf("%w: server ID is empty", cloud.ErrTransient) } return securitygroup.New(machineScope).Reconcile(ctx) @@ -394,18 +304,3 @@ func (r *StackitMachineReconciler) getStackitCluster(ctx context.Context, cluste } return stackitCluster, nil } - -func machineAddressesFromCloud(in []cloud.Address) []clusterv1.MachineAddress { - if len(in) == 0 { - return nil - } - - out := make([]clusterv1.MachineAddress, len(in)) - for i, address := range in { - out[i] = clusterv1.MachineAddress{ - Type: clusterv1.MachineAddressType(address.Type), - Address: address.Address, - } - } - return out -} diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index 3a64b0d..371cb37 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -27,6 +27,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/util" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" @@ -44,6 +45,11 @@ type ClusterScope struct { loadBalancerTargets []loadbalancer.Target loadBalancerSeedTargets []loadbalancer.Target + bastionCloudInit []byte + bastionCloudInitHash string + bastionServerState string + bastionServerPowerStatus string + patchHelper *patch.Helper } @@ -97,12 +103,50 @@ func (s *ClusterScope) SetAPIServerEndpoint(endpoint clusterv1.APIEndpoint) { s.StackitCluster.Status.APIServerEndpoint = endpoint } -func (s *ClusterScope) SetBastionStatus(bastion *cloud.Bastion, cloudInitHash string) { - s.StackitCluster.Status.Bastion.ServerID = bastion.ServerID - if bastion.SecurityGroupID != "" { - s.StackitCluster.Status.Bastion.SecurityGroupID = bastion.SecurityGroupID +func (s *ClusterScope) SetBastionCloudInit(cloudInit []byte, cloudInitHash string) { + s.bastionCloudInit = cloudInit + s.bastionCloudInitHash = cloudInitHash +} + +func (s *ClusterScope) BastionServerState() (state, powerStatus string) { + return s.bastionServerState, s.bastionServerPowerStatus +} + +func (s *ClusterScope) ServerSpec() server.ServerSpec { + return server.ServerSpec{ + ID: s.StackitCluster.Status.Bastion.ServerID, + Name: s.StackitCluster.Name + "-bastion", + ImageID: s.StackitCluster.Spec.Bastion.ImageID, + MachineType: s.StackitCluster.Spec.Bastion.MachineType, + AvailabilityZone: DefaultAvailabilityZone(s.StackitCluster), + SSHKeyName: s.StackitCluster.Spec.Bastion.SSHKeyName, + NetworkID: s.StackitCluster.Spec.Network.ID, + UserData: s.bastionCloudInit, + Tags: s.BastionTags(), + RootVolume: rootVolumeFromSpec(s.StackitCluster.Spec.Bastion.RootVolume), + SetServer: func(srv *server.Server, _ bool) { + s.bastionServerState = srv.State + s.bastionServerPowerStatus = srv.PowerStatus + s.StackitCluster.Status.Bastion.ServerID = srv.ID + s.StackitCluster.Status.Bastion.CloudInitHash = s.bastionCloudInitHash + }, + ClearServer: func() { + s.StackitCluster.Status.Bastion.ServerID = "" + s.StackitCluster.Status.Bastion.CloudInitHash = "" + }, + } +} + +func rootVolumeFromSpec(in infrav1.StackitRootVolumeSpec) server.RootVolume { + deleteOnTermination := true + if in.DeleteOnTermination != nil { + deleteOnTermination = *in.DeleteOnTermination + } + return server.RootVolume{ + SizeGiB: in.SizeGiB, + PerformanceClass: in.PerformanceClass, + DeleteOnTermination: deleteOnTermination, } - s.StackitCluster.Status.Bastion.CloudInitHash = cloudInitHash } func (s *ClusterScope) SetBastionSecurityGroupID(id string) { @@ -139,10 +183,6 @@ func DefaultAvailabilityZone(stackitCluster *infrav1.StackitCluster) string { return "" } -func (s *ClusterScope) Cloud() cloud.Client { - return s.CloudClient -} - func (s *ClusterScope) SetCloudClient(cloudClient cloud.Client) { s.CloudClient = cloudClient } @@ -253,12 +293,14 @@ func (s *ClusterScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { }) return []securitygroup.SecurityGroupSpec{ { - ID: s.StackitCluster.Status.Bastion.SecurityGroupID, - Name: s.StackitCluster.Name + "-bastion-ssh", - Description: "Cluster API Provider STACKIT bastion SSH access", - Tags: s.BastionTags(), - Rules: rules, - SetID: s.SetBastionSecurityGroupID, + ID: s.StackitCluster.Status.Bastion.SecurityGroupID, + Name: s.StackitCluster.Name + "-bastion-ssh", + Description: "Cluster API Provider STACKIT bastion SSH access", + Tags: s.BastionTags(), + Rules: rules, + ServerIDs: []string{s.StackitCluster.Status.Bastion.ServerID}, + ServerSelector: s.BastionTags(), + SetID: s.SetBastionSecurityGroupID, }, nodeSSHSecurityGroupSpec(s.StackitCluster, ""), } diff --git a/cloud/services/bastion/bastion_test.go b/scope/cluster_scope_test.go similarity index 83% rename from cloud/services/bastion/bastion_test.go rename to scope/cluster_scope_test.go index 88722a1..d4fcfd3 100644 --- a/cloud/services/bastion/bastion_test.go +++ b/scope/cluster_scope_test.go @@ -8,7 +8,7 @@ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 */ -package bastion +package scope import ( "testing" @@ -18,7 +18,7 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" ) -func TestInputAvailabilityZone(t *testing.T) { +func TestDefaultAvailabilityZone(t *testing.T) { tests := []struct { name string region string @@ -59,9 +59,9 @@ func TestInputAvailabilityZone(t *testing.T) { FailureDomains: test.failureDomains, }, } - input := Input(cluster, nil) - if input.AvailabilityZone != test.wantAZ { - t.Fatalf("Input().AvailabilityZone = %q, want %q", input.AvailabilityZone, test.wantAZ) + clusterScope := &ClusterScope{StackitCluster: cluster} + if got := clusterScope.ServerSpec().AvailabilityZone; got != test.wantAZ { + t.Fatalf("ServerSpec().AvailabilityZone = %q, want %q", got, test.wantAZ) } }) } diff --git a/scope/machine_scope.go b/scope/machine_scope.go index aea2b32..95fce63 100644 --- a/scope/machine_scope.go +++ b/scope/machine_scope.go @@ -22,6 +22,7 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/util" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" ) @@ -35,6 +36,10 @@ type MachineScope struct { StackitMachine *infrav1.StackitMachine CloudClient cloud.Client + bootstrapData []byte + instancePowerStatus string + instanceCreated bool + patchHelper *patch.Helper } @@ -96,11 +101,11 @@ func (s *MachineScope) Tags() map[string]string { ) } -func (s *MachineScope) SetInstance(server *cloud.Server) string { - providerID := cloud.NewProviderID(s.StackitCluster.Spec.ProjectID, s.StackitCluster.Spec.Region, server.ID) - s.StackitMachine.Status.InstanceID = server.ID - s.StackitMachine.Status.InstanceState = server.State - s.StackitMachine.Status.AvailabilityZone = server.AvailabilityZone +func (s *MachineScope) SetInstance(srv *cloud.Server) string { + providerID := cloud.NewProviderID(s.StackitCluster.Spec.ProjectID, s.StackitCluster.Spec.Region, srv.ID) + s.StackitMachine.Status.InstanceID = srv.ID + s.StackitMachine.Status.InstanceState = srv.State + s.StackitMachine.Status.AvailabilityZone = srv.AvailabilityZone s.StackitMachine.Spec.ProviderID = &providerID s.StackitMachine.Status.ProviderID = providerID s.StackitMachine.Status.Initialization.Provisioned = true @@ -155,6 +160,42 @@ func (s *MachineScope) IaaSClient() *iaas.APIClient { return s.CloudClient.IaaSClient() } +func (s *MachineScope) SetBootstrapData(data []byte) { + s.bootstrapData = data +} + +func (s *MachineScope) InstancePowerStatus() string { + return s.instancePowerStatus +} + +func (s *MachineScope) InstanceCreated() bool { + return s.instanceCreated +} + +func (s *MachineScope) ServerSpec() server.ServerSpec { + return server.ServerSpec{ + ID: s.StackitMachine.Status.InstanceID, + Name: s.StackitMachine.Name, + ImageID: s.StackitMachine.Spec.ImageID, + MachineType: s.StackitMachine.Spec.MachineType, + AvailabilityZone: s.AvailabilityZone(), + SSHKeyName: s.StackitMachine.Spec.SSHKeyName, + NetworkID: s.StackitMachine.Spec.Network.ID, + SecurityGroups: s.StackitMachine.Spec.SecurityGroups, + UserData: s.bootstrapData, + Tags: s.Tags(), + RootVolume: rootVolumeFromSpec(s.StackitMachine.Spec.RootVolume), + DisallowRecreate: s.StackitMachine.Status.Initialization.Provisioned, + SetServer: func(srv *server.Server, created bool) { + s.instanceCreated = created + s.instancePowerStatus = srv.PowerStatus + s.StackitMachine.Status.Addresses = machineAddressesFromServer(srv.Addresses) + s.SetInstance(srv) + }, + ClearServer: s.ClearInstance, + } +} + func (s *MachineScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { return []securitygroup.SecurityGroupSpec{ nodeSSHSecurityGroupSpec( @@ -164,3 +205,18 @@ func (s *MachineScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { ), } } + +func machineAddressesFromServer(in []server.Address) []clusterv1.MachineAddress { + if len(in) == 0 { + return nil + } + + out := make([]clusterv1.MachineAddress, len(in)) + for i, address := range in { + out[i] = clusterv1.MachineAddress{ + Type: clusterv1.MachineAddressType(address.Type), + Address: address.Address, + } + } + return out +} diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index f5b6af9..a545529 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -37,6 +37,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/test/utils" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -425,7 +426,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying the VM exists in STACKIT") Eventually(func(g Gomega) { - server, err := cloudClient.GetServer(ctx, instanceID) + server, err := server.NewClient(cloudClient).Get(ctx, instanceID) g.Expect(err).NotTo(HaveOccurred()) g.Expect(server.ID).To(Equal(instanceID)) }, 5*time.Minute, 15*time.Second).Should(Succeed()) @@ -437,13 +438,13 @@ var _ = Describe("Manager", Ordered, func() { By("verifying the VM was deleted from STACKIT") Eventually(func(g Gomega) { - _, err := cloudClient.GetServer(ctx, instanceID) + _, err := server.NewClient(cloudClient).Get(ctx, instanceID) g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := cloudClient.ListServersByTags(ctx, leakTags) + servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) @@ -528,7 +529,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range instanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - server, err := cloudClient.GetServer(ctx, instanceID) + server, err := server.NewClient(cloudClient).Get(ctx, instanceID) g.Expect(err).NotTo(HaveOccurred()) g.Expect(server.ID).To(Equal(instanceID)) }, 5*time.Minute, 15*time.Second).Should(Succeed()) @@ -553,7 +554,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := cloudClient.ListServersByTags(ctx, leakTags) + servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) @@ -631,7 +632,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := cloudClient.ListServersByTags(ctx, leakTags) + servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) @@ -726,7 +727,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := cloudClient.ListServersByTags(ctx, leakTags) + servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) @@ -807,7 +808,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range scaledUpInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - server, err := cloudClient.GetServer(ctx, instanceID) + server, err := server.NewClient(cloudClient).Get(ctx, instanceID) g.Expect(err).NotTo(HaveOccurred()) g.Expect(server.ID).To(Equal(instanceID)) }, 5*time.Minute, 15*time.Second).Should(Succeed()) @@ -834,7 +835,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range deletedInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - _, err := cloudClient.GetServer(ctx, instanceID) + _, err := server.NewClient(cloudClient).Get(ctx, instanceID) g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -848,14 +849,14 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range observedInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - _, err := cloudClient.GetServer(ctx, instanceID) + _, err := server.NewClient(cloudClient).Get(ctx, instanceID) g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := cloudClient.ListServersByTags(ctx, leakTags) + servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) @@ -964,7 +965,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range scaledUpInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - server, err := cloudClient.GetServer(ctx, instanceID) + server, err := server.NewClient(cloudClient).Get(ctx, instanceID) g.Expect(err).NotTo(HaveOccurred()) g.Expect(server.ID).To(Equal(instanceID)) }, 5*time.Minute, 15*time.Second).Should(Succeed()) @@ -1004,7 +1005,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range deletedInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - _, err := cloudClient.GetServer(ctx, instanceID) + _, err := server.NewClient(cloudClient).Get(ctx, instanceID) g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -1016,7 +1017,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := cloudClient.ListServersByTags(ctx, leakTags) + servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) @@ -1112,7 +1113,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying the old worker VM was deleted from STACKIT") Eventually(func(g Gomega) { - _, err := cloudClient.GetServer(ctx, initialInstanceIDs[0]) + _, err := server.NewClient(cloudClient).Get(ctx, initialInstanceIDs[0]) g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected old server %s to be deleted, got %v", initialInstanceIDs[0], err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) @@ -1128,14 +1129,14 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range observedInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - _, err := cloudClient.GetServer(ctx, instanceID) + _, err := server.NewClient(cloudClient).Get(ctx, instanceID) g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := cloudClient.ListServersByTags(ctx, leakTags) + servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) @@ -1262,7 +1263,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range deletedInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - _, err := cloudClient.GetServer(ctx, instanceID) + _, err := server.NewClient(cloudClient).Get(ctx, instanceID) g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected old server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -1274,7 +1275,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := cloudClient.ListServersByTags(ctx, leakTags) + servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) @@ -1387,7 +1388,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range deletedInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - _, err := cloudClient.GetServer(ctx, instanceID) + _, err := server.NewClient(cloudClient).Get(ctx, instanceID) g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected old server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -1399,7 +1400,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := cloudClient.ListServersByTags(ctx, leakTags) + servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(servers).To(BeEmpty()) loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) @@ -3116,13 +3117,13 @@ func appendUnique(values []string, additions ...string) []string { func cleanupCloudServersByID(ctx context.Context, cloudClient cloud.Client, instanceIDs []string) { for _, instanceID := range instanceIDs { - if err := cloudClient.DeleteServer(ctx, instanceID); err != nil && !cloud.IsNotFound(err) { + if err := server.NewClient(cloudClient).DeleteByID(ctx, instanceID); err != nil && !cloud.IsNotFound(err) { _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API server cleanup warning for %s: %v\n", instanceID, err) continue } deadline := time.Now().Add(10 * time.Minute) for time.Now().Before(deadline) { - _, err := cloudClient.GetServer(ctx, instanceID) + _, err := server.NewClient(cloudClient).Get(ctx, instanceID) if cloud.IsNotFound(err) { break } From 2651d710de67483b6b717db994fb55718b695cec Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Thu, 12 Nov 2026 15:53:06 +0100 Subject: [PATCH 10/60] feat: refactor publicip, network, and availabilityzone services --- cloud/cleanup.go | 7 +- cloud/client.go | 4 - cloud/fake/client.go | 84 ++- cloud/sdk_client.go | 25 - cloud/sdk_client_integration_test.go | 10 +- cloud/sdk_client_test.go | 131 ++-- cloud/services/availabilityzone/reconcile.go | 53 ++ .../availabilityzone/reconcile_test.go | 132 ++++ cloud/services/availabilityzone/service.go | 58 ++ cloud/services/network/reconcile.go | 66 ++ cloud/services/network/reconcile_test.go | 167 +++++ cloud/services/network/service.go | 69 +++ cloud/services/publicip/reconcile.go | 248 ++++---- cloud/services/publicip/reconcile_test.go | 574 ++++++++++-------- cloud/services/publicip/service.go | 53 +- cloud/types.go | 14 +- controller/controller_test_helpers_test.go | 3 +- controller/stackitcluster_infrastructure.go | 89 +-- scope/cluster_scope.go | 106 +++- test/e2e/e2e_test.go | 4 +- 20 files changed, 1256 insertions(+), 641 deletions(-) create mode 100644 cloud/services/availabilityzone/reconcile.go create mode 100644 cloud/services/availabilityzone/reconcile_test.go create mode 100644 cloud/services/availabilityzone/service.go create mode 100644 cloud/services/network/reconcile.go create mode 100644 cloud/services/network/reconcile_test.go create mode 100644 cloud/services/network/service.go diff --git a/cloud/cleanup.go b/cloud/cleanup.go index 08c9c72..9e580fb 100644 --- a/cloud/cleanup.go +++ b/cloud/cleanup.go @@ -33,6 +33,7 @@ func CleanupByTags(ctx context.Context, client Client, tags map[string]string) e var errs []string srvClient := server.NewClient(client) sgClient := securitygroup.NewClient(client) + pubIPClient := publicip.NewClient(client) bastionTags := make(map[string]string, len(tags)+1) maps.Copy(bastionTags, tags) @@ -41,17 +42,17 @@ func CleanupByTags(ctx context.Context, client Client, tags map[string]string) e if err != nil { errs = append(errs, err.Error()) } - publicIPs, err := publicip.ListByTags(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), tags) + publicIPs, err := pubIPClient.ListByTags(ctx, tags) if err != nil { errs = append(errs, err.Error()) } else { for _, pubIP := range publicIPs { for _, srv := range bastionServers { - if err := publicip.DetachFromServer(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), srv.ID, pubIP.ID); err != nil { + if err := pubIPClient.DetachFromServer(ctx, srv.ID, pubIP.ID); err != nil { errs = append(errs, fmt.Sprintf("detach public IP %q from server %q: %v", pubIP.ID, srv.ID, err)) } } - if err := publicip.DeleteByID(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), pubIP.ID); err != nil { + if err := pubIPClient.DeleteByID(ctx, pubIP.ID); err != nil { errs = append(errs, fmt.Sprintf("delete public IP %q: %v", pubIP.ID, err)) } } diff --git a/cloud/client.go b/cloud/client.go index a24b68f..51500b9 100644 --- a/cloud/client.go +++ b/cloud/client.go @@ -39,10 +39,6 @@ type Client interface { IaaSClient() *iaas.APIClient LoadBalancerClient() *lb.APIClient - - GetNetwork(ctx context.Context, id string) (*Network, error) - - ListAvailabilityZones(ctx context.Context) ([]string, error) } // Factory builds a Client from credentials (raw bytes from the configured diff --git a/cloud/fake/client.go b/cloud/fake/client.go index f0e8af9..0071c57 100644 --- a/cloud/fake/client.go +++ b/cloud/fake/client.go @@ -201,24 +201,6 @@ func (c *Client) GetServer(_ context.Context, id string) (*cloud.Server, error) return cloneServer(entry.server), nil } -func (c *Client) FindServerByTags(_ context.Context, tags map[string]string) (*cloud.Server, error) { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextFindServer); err != nil { - return nil, err - } - if len(tags) == 0 { - return nil, fmt.Errorf("empty tags: %w", cloud.ErrNotFound) - } - for _, entry := range c.servers { - if mapContains(entry.tags, tags) { - return cloneServer(entry.server), nil - } - } - return nil, fmt.Errorf("no server matching tags: %w", cloud.ErrNotFound) -} - func (c *Client) ListServersByTags(_ context.Context, tags map[string]string) ([]*cloud.Server, error) { c.mu.Lock() defer c.mu.Unlock() @@ -299,11 +281,13 @@ func (c *Client) DeleteServer(_ context.Context, id string) error { func (c *Client) GetNetwork(_ context.Context, id string) (*cloud.Network, error) { c.mu.Lock() - defer c.mu.Unlock() - if c.BeforeGetNetwork != nil { + c.mu.Unlock() c.BeforeGetNetwork() + c.mu.Lock() } + defer c.mu.Unlock() + if err := consume(&c.FailNextGetNetwork); err != nil { return nil, err } @@ -315,7 +299,7 @@ func (c *Client) GetNetwork(_ context.Context, id string) (*cloud.Network, error return &out, nil } -func (c *Client) GetPublicIP(_ context.Context, id string) (*cloud.PublicIP, error) { +func (c *Client) getPublicIP(_ context.Context, id string) (*cloud.PublicIP, error) { c.mu.Lock() defer c.mu.Unlock() @@ -373,7 +357,7 @@ func (c *Client) CreatePublicIP(_ context.Context, tags map[string]string) (*clo return &out, nil } -func (c *Client) DeletePublicIP(_ context.Context, id string) error { +func (c *Client) deletePublicIP(_ context.Context, id string) error { c.mu.Lock() defer c.mu.Unlock() @@ -390,7 +374,7 @@ func (c *Client) DeletePublicIP(_ context.Context, id string) error { return nil } -func (c *Client) AddPublicIPToServer(_ context.Context, serverID, publicIPID string) error { +func (c *Client) addPublicIPToServer(_ context.Context, serverID, publicIPID string) error { c.mu.Lock() defer c.mu.Unlock() @@ -411,7 +395,7 @@ func (c *Client) AddPublicIPToServer(_ context.Context, serverID, publicIPID str return nil } -func (c *Client) RemovePublicIPFromServer(_ context.Context, serverID, publicIPID string) error { +func (c *Client) removePublicIPFromServer(_ context.Context, serverID, publicIPID string) error { c.mu.Lock() defer c.mu.Unlock() @@ -676,7 +660,11 @@ func (c *Client) roundTripIaaS(r *http.Request) (*http.Response, error) { rec := httptest.NewRecorder() rec.Header().Set("Content-Type", "application/json") - if c.handlePublicIPRequest(rec, r) || c.handleSecurityGroupRequest(rec, r) || c.handleServerRequest(rec, r) { + if c.handlePublicIPRequest(rec, r) || + c.handleSecurityGroupRequest(rec, r) || + c.handleServerRequest(rec, r) || + c.handleNetworkRequest(rec, r) || + c.handleAvailabilityZoneRequest(rec, r) { return rec.Result(), nil } @@ -685,6 +673,44 @@ func (c *Client) roundTripIaaS(r *http.Request) (*http.Response, error) { return rec.Result(), nil } +func (c *Client) handleNetworkRequest(rec *httptest.ResponseRecorder, r *http.Request) bool { + if r.Method != http.MethodGet || !strings.Contains(r.URL.Path, "/networks/") { + return false + } + id := r.URL.Path[strings.LastIndex(r.URL.Path, "/networks/")+len("/networks/"):] + n, err := c.GetNetwork(r.Context(), id) + if err != nil { + writeFakeError(rec, err) + return true + } + m := map[string]any{ + "id": n.ID, + "name": n.Name, + "status": "CREATED", + "routed": true, + } + if len(n.IPv4Prefixes) > 0 { + m["ipv4"] = map[string]any{ + "prefixes": slices.Clone(n.IPv4Prefixes), + } + } + _ = json.NewEncoder(rec).Encode(m) + return true +} + +func (c *Client) handleAvailabilityZoneRequest(rec *httptest.ResponseRecorder, r *http.Request) bool { + if r.Method != http.MethodGet || !strings.HasSuffix(r.URL.Path, "/availability-zones") { + return false + } + zones, err := c.ListAvailabilityZones(r.Context()) + if err != nil { + writeFakeError(rec, err) + return true + } + _ = json.NewEncoder(rec).Encode(map[string]any{"items": zones}) + return true +} + func (c *Client) roundTripLB(r *http.Request) (*http.Response, error) { rec := httptest.NewRecorder() rec.Header().Set("Content-Type", "application/json") @@ -960,9 +986,9 @@ func (c *Client) handlePublicIPRequest(rec *httptest.ResponseRecorder, r *http.R var err error switch r.Method { case http.MethodPut: - err = c.AddPublicIPToServer(r.Context(), serverID, publicIPID) + err = c.addPublicIPToServer(r.Context(), serverID, publicIPID) case http.MethodDelete: - err = c.RemovePublicIPFromServer(r.Context(), serverID, publicIPID) + err = c.removePublicIPFromServer(r.Context(), serverID, publicIPID) default: err = cloud.ErrInvalidInput } @@ -996,7 +1022,7 @@ func (c *Client) handlePublicIPRequest(rec *httptest.ResponseRecorder, r *http.R case r.Method == http.MethodGet && strings.Contains(path, "/public-ips/"): id := path[strings.LastIndex(path, "/public-ips/")+len("/public-ips/"):] - got, err := c.GetPublicIP(r.Context(), id) + got, err := c.getPublicIP(r.Context(), id) if err != nil { writeFakeError(rec, err) } else { @@ -1006,7 +1032,7 @@ func (c *Client) handlePublicIPRequest(rec *httptest.ResponseRecorder, r *http.R case r.Method == http.MethodDelete && strings.Contains(path, "/public-ips/"): id := path[strings.LastIndex(path, "/public-ips/")+len("/public-ips/"):] - writeFakeStatus(rec, c.DeletePublicIP(r.Context(), id)) + writeFakeStatus(rec, c.deletePublicIP(r.Context(), id)) return true default: diff --git a/cloud/sdk_client.go b/cloud/sdk_client.go index a41bea3..8edebfd 100644 --- a/cloud/sdk_client.go +++ b/cloud/sdk_client.go @@ -15,7 +15,6 @@ import ( "fmt" "os" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/stackit-sdk-go/core/config" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" @@ -103,27 +102,3 @@ func stackitOptions(creds Credentials, endpoint string) []config.ConfigurationOp } return opts } - -func (c *SDKClient) ListAvailabilityZones(ctx context.Context) ([]string, error) { - resp, err := c.iaasClient.DefaultAPI.ListAvailabilityZones(ctx, c.region).Execute() - if err != nil { - return nil, shared.ClassifySDKError("list availability zones", err) - } - if resp == nil || resp.Items == nil { - return nil, nil - } - return resp.GetItems(), nil -} - -func (c *SDKClient) GetNetwork(ctx context.Context, id string) (*Network, error) { - network, err := c.iaasClient.DefaultAPI.GetNetwork(ctx, c.projectID, c.region, id).Execute() - if err != nil { - return nil, shared.ClassifySDKError("get network", err) - } - ipv4 := network.GetIpv4() - return &Network{ - ID: network.GetId(), - Name: network.GetName(), - IPv4Prefixes: ipv4.GetPrefixes(), - }, nil -} diff --git a/cloud/sdk_client_integration_test.go b/cloud/sdk_client_integration_test.go index 996f7f4..d33bf2b 100644 --- a/cloud/sdk_client_integration_test.go +++ b/cloud/sdk_client_integration_test.go @@ -69,7 +69,7 @@ func TestSDKClientLoadBalancerCreateDeleteIntegration(t *testing.T) { client := newIntegrationClient(t) networkID := requiredIntegrationEnv(t, envIntegrationNetworkID) targetIP := requiredIntegrationEnv(t, envIntegrationTargetIP) - lbID := createIntegrationLoadBalancer(t, client, networkID, LoadBalancerTargetInput{ + lbID := createIntegrationLoadBalancer(t, client, networkID, loadbalancer.Target{ Name: "capistackit-initial-target", IP: targetIP, }) @@ -84,7 +84,7 @@ func TestSDKClientLoadBalancerTargetIntegration(t *testing.T) { networkID := requiredIntegrationEnv(t, envIntegrationNetworkID) targetIP := requiredIntegrationEnv(t, envIntegrationTargetIP) initialIP := integrationInitialTargetIP(targetIP) - lbID := createIntegrationLoadBalancer(t, client, networkID, LoadBalancerTargetInput{ + lbID := createIntegrationLoadBalancer(t, client, networkID, loadbalancer.Target{ Name: "capistackit-initial-target", IP: initialIP, }) @@ -95,7 +95,7 @@ func TestSDKClientLoadBalancerTargetIntegration(t *testing.T) { } }) - targets := []LoadBalancerTargetInput{ + targets := []loadbalancer.Target{ {Name: "capistackit-initial-target", IP: initialIP}, {Name: "capistackit-integration-target", IP: targetIP}, } @@ -188,7 +188,7 @@ func (s *integrationLBScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec { return s.spec } -func createIntegrationLoadBalancer(t *testing.T, client Client, networkID string, target LoadBalancerTargetInput) string { +func createIntegrationLoadBalancer(t *testing.T, client Client, networkID string, target loadbalancer.Target) string { t.Helper() name := fmt.Sprintf("capistackit-it-%d", time.Now().UnixNano()) @@ -199,7 +199,7 @@ func createIntegrationLoadBalancer(t *testing.T, client Client, networkID string Name: name, NetworkID: networkID, Port: 6443, - SeedTargets: []LoadBalancerTargetInput{target}, + SeedTargets: []loadbalancer.Target{target}, Tags: map[string]string{ "capistackit-test": "true", "capistackit-name": name, diff --git a/cloud/sdk_client_test.go b/cloud/sdk_client_test.go index d0c21a9..f392c1d 100644 --- a/cloud/sdk_client_test.go +++ b/cloud/sdk_client_test.go @@ -12,107 +12,62 @@ package cloud import ( "context" - "encoding/json" - "net/http" - "net/http/httptest" - "strings" + "errors" "testing" ) const ( testSDKProjectID = "00000000-0000-0000-0000-000000000000" testSDKRegion = "eu01" - testSDKNetworkID = "11111111-1111-4111-8111-111111111111" testBoolTrue = "true" ) -func TestSDKClientClassifiesHTTPStatusCodes(t *testing.T) { - tests := []struct { - name string - status int - check func(error) bool - }{ - {name: "not found", status: http.StatusNotFound, check: IsNotFound}, - {name: "unauthorized", status: http.StatusUnauthorized, check: IsUnauthorized}, - {name: "conflict", status: http.StatusConflict, check: IsConflict}, - {name: "invalid input", status: http.StatusBadRequest, check: IsInvalidInput}, - {name: "rate limited", status: http.StatusTooManyRequests, check: IsTransient}, - {name: "server error", status: http.StatusInternalServerError, check: IsTransient}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - http.Error(w, http.StatusText(tt.status), tt.status) - })) - client := newTestSDKClient(t, server.URL) - - _, err := client.GetNetwork(context.Background(), testSDKNetworkID) - if err == nil { - t.Fatal("GetNetwork() error = nil") - } - if !tt.check(err) { - t.Fatalf("GetNetwork() error = %v, did not match expected classifier", err) - } +func TestNewClientValidation(t *testing.T) { + t.Run("rejects empty project ID", func(t *testing.T) { + _, err := NewClient(context.Background(), Credentials{ + Region: testSDKRegion, + ServiceAccountJSON: []byte(`{}`), }) - } -} - -func newSDKTestServer(t *testing.T, handler http.Handler) *httptest.Server { - t.Helper() - - server := httptest.NewServer(handler) - t.Cleanup(server.Close) - return server -} - -func newTestSDKClient(t *testing.T, endpoint string) *SDKClient { - t.Helper() - - t.Setenv(stackitNoAuthEnv, testBoolTrue) - t.Setenv(stackitIAASEndpointEnv, endpoint) - t.Setenv(stackitLBEndpointEnv, endpoint) - - client, err := NewClient(context.Background(), Credentials{ - ProjectID: testSDKProjectID, - Region: testSDKRegion, + if !errors.Is(err, ErrInvalidInput) { + t.Fatalf("NewClient() error = %v, want ErrInvalidInput", err) + } }) - if err != nil { - t.Fatalf("NewClient() error = %v", err) - } - sdkClient, ok := client.(*SDKClient) - if !ok { - t.Fatalf("NewClient() = %T, want *SDKClient", client) - } - return sdkClient -} - -func writeJSON(t *testing.T, w http.ResponseWriter, value any) { - t.Helper() - w.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(value); err != nil { - t.Fatalf("encode response body: %v", err) - } -} + t.Run("rejects empty region", func(t *testing.T) { + _, err := NewClient(context.Background(), Credentials{ + ProjectID: testSDKProjectID, + ServiceAccountJSON: []byte(`{}`), + }) + if !errors.Is(err, ErrInvalidInput) { + t.Fatalf("NewClient() error = %v, want ErrInvalidInput", err) + } + }) -func TestSDKClientListAvailabilityZones(t *testing.T) { - server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/availability-zones") { - writeJSON(t, w, map[string]any{ - "items": []any{"eu01-1", "eu01-2", "eu01-3"}, - }) - return + t.Run("rejects empty service account JSON when STACKIT_NO_AUTH is not true", func(t *testing.T) { + t.Setenv(stackitNoAuthEnv, "false") + _, err := NewClient(context.Background(), Credentials{ + ProjectID: testSDKProjectID, + Region: testSDKRegion, + }) + if !errors.Is(err, ErrInvalidInput) { + t.Fatalf("NewClient() error = %v, want ErrInvalidInput", err) } - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - })) + }) - client := newTestSDKClient(t, server.URL) - zones, err := client.ListAvailabilityZones(context.Background()) - if err != nil { - t.Fatalf("ListAvailabilityZones() error = %v", err) - } - if len(zones) != 3 || zones[0] != "eu01-1" || zones[1] != "eu01-2" || zones[2] != "eu01-3" { - t.Fatalf("ListAvailabilityZones() = %v, want [eu01-1 eu01-2 eu01-3]", zones) - } + t.Run("creates SDKClient when STACKIT_NO_AUTH is true", func(t *testing.T) { + t.Setenv(stackitNoAuthEnv, testBoolTrue) + client, err := NewClient(context.Background(), Credentials{ + ProjectID: testSDKProjectID, + Region: testSDKRegion, + }) + if err != nil { + t.Fatalf("NewClient() error = %v", err) + } + if client.ProjectID() != testSDKProjectID || client.Region() != testSDKRegion { + t.Fatalf("client ProjectID=%q Region=%q", client.ProjectID(), client.Region()) + } + if client.IaaSClient() == nil || client.LoadBalancerClient() == nil { + t.Fatal("expected non-nil IaaSClient and LoadBalancerClient") + } + }) } diff --git a/cloud/services/availabilityzone/reconcile.go b/cloud/services/availabilityzone/reconcile.go new file mode 100644 index 0000000..d3e36eb --- /dev/null +++ b/cloud/services/availabilityzone/reconcile.go @@ -0,0 +1,53 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package availabilityzone + +import ( + "context" + "fmt" + + logf "sigs.k8s.io/controller-runtime/pkg/log" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +func (s *Service) Reconcile(ctx context.Context) error { + log := logf.FromContext(ctx) + zones, err := s.List(ctx) + if err != nil { + return err + } + spec := s.scope.AvailabilityZoneSpec() + if spec.SetZones != nil { + spec.SetZones(zones) + } + log.V(1).Info("Reconciled availability zones", "count", len(zones)) + return nil +} + +func (s *Service) Delete(_ context.Context) error { + return nil +} + +// List retrieves the availability zones for the configured region. +func (c *Client) List(ctx context.Context) ([]string, error) { + if c.region == "" { + return nil, fmt.Errorf("%w: region is empty", shared.ErrInvalidInput) + } + resp, err := c.iaasClient.DefaultAPI.ListAvailabilityZones(ctx, c.region).Execute() + if err != nil { + return nil, shared.ClassifySDKError("list availability zones", err) + } + if resp == nil || resp.Items == nil { + return nil, nil + } + return resp.GetItems(), nil +} diff --git a/cloud/services/availabilityzone/reconcile_test.go b/cloud/services/availabilityzone/reconcile_test.go new file mode 100644 index 0000000..c906717 --- /dev/null +++ b/cloud/services/availabilityzone/reconcile_test.go @@ -0,0 +1,132 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package availabilityzone_test + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "slices" + "strings" + "testing" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/availabilityzone" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +const testRegion = "eu01" + +type testScope struct { + region string + iaasClient *iaas.APIClient + spec availabilityzone.AvailabilityZoneSpec +} + +func (s *testScope) Region() string { + return s.region +} + +func (s *testScope) IaaSClient() *iaas.APIClient { + return s.iaasClient +} + +func (s *testScope) AvailabilityZoneSpec() availabilityzone.AvailabilityZoneSpec { + return s.spec +} + +func newTestIaaSClient(t *testing.T, handler http.Handler) *iaas.APIClient { + t.Helper() + + srv := httptest.NewServer(handler) + t.Cleanup(srv.Close) + + iaasClient, err := iaas.NewAPIClient( + config.WithoutAuthentication(), + config.WithEndpoint(srv.URL), + ) + if err != nil { + t.Fatalf("iaas.NewAPIClient() error = %v", err) + } + return iaasClient +} + +func newTestService(t *testing.T, spec availabilityzone.AvailabilityZoneSpec, handler http.Handler) *availabilityzone.Service { + t.Helper() + + return availabilityzone.New(&testScope{ + region: testRegion, + iaasClient: newTestIaaSClient(t, handler), + spec: spec, + }) +} + +func writeJSON(t *testing.T, w http.ResponseWriter, value any) { + t.Helper() + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(value); err != nil { + t.Fatalf("encode response body: %v", err) + } +} + +func TestServiceReconcile(t *testing.T) { + t.Run("lists availability zones and passes them to SetZones", func(t *testing.T) { + var recorded []string + spec := availabilityzone.AvailabilityZoneSpec{ + SetZones: func(zones []string) { + recorded = zones + }, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/availability-zones") { + writeJSON(t, w, map[string]any{ + "items": []string{"eu01-1", "eu01-2", "eu01-3"}, + }) + return + } + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if !slices.Equal(recorded, []string{"eu01-1", "eu01-2", "eu01-3"}) { + t.Fatalf("recorded = %v, want [eu01-1 eu01-2 eu01-3]", recorded) + } + }) + + t.Run("returns classified error when API fails", func(t *testing.T) { + svc := newTestService(t, availabilityzone.AvailabilityZoneSpec{}, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + writeJSON(t, w, map[string]any{"message": "backend error"}) + })) + + err := svc.Reconcile(context.Background()) + if !errors.Is(err, shared.ErrTransient) { + t.Fatalf("Reconcile() error = %v, want ErrTransient", err) + } + }) + + t.Run("Delete is a no-op", func(t *testing.T) { + svc := newTestService(t, availabilityzone.AvailabilityZoneSpec{}, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v, want nil", err) + } + }) +} diff --git a/cloud/services/availabilityzone/service.go b/cloud/services/availabilityzone/service.go new file mode 100644 index 0000000..8e79943 --- /dev/null +++ b/cloud/services/availabilityzone/service.go @@ -0,0 +1,58 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package availabilityzone + +import iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + +// AvailabilityZoneSpec describes the callback used to record discovered availability zones. +type AvailabilityZoneSpec struct { + SetZones func(zones []string) +} + +// ClientScope is an interface that provides the IaaS client and target region for availability zone operations. +type ClientScope interface { + Region() string + IaaSClient() *iaas.APIClient +} + +// Scope is an interface that provides scope data needed for availability zone reconciliation. +type Scope interface { + ClientScope + AvailabilityZoneSpec() AvailabilityZoneSpec +} + +// Client provides direct availability zone operations against the STACKIT IaaS API. +type Client struct { + iaasClient *iaas.APIClient + region string +} + +// NewClient returns a Client for the given scope. +func NewClient(scope ClientScope) *Client { + return &Client{ + iaasClient: scope.IaaSClient(), + region: scope.Region(), + } +} + +// Service implements availability zones reconciler. +type Service struct { + scope Scope + *Client +} + +// New returns Service from given scope. +func New(scope Scope) *Service { + return &Service{ + scope: scope, + Client: NewClient(scope), + } +} diff --git a/cloud/services/network/reconcile.go b/cloud/services/network/reconcile.go new file mode 100644 index 0000000..7f4b993 --- /dev/null +++ b/cloud/services/network/reconcile.go @@ -0,0 +1,66 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package network + +import ( + "context" + "fmt" + + logf "sigs.k8s.io/controller-runtime/pkg/log" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +func (s *Service) Reconcile(ctx context.Context) error { + log := logf.FromContext(ctx) + spec := s.scope.NetworkSpec() + if err := spec.validate(); err != nil { + return err + } + + net, err := s.Get(ctx, spec.ID) + if err != nil { + return err + } + if spec.SetNetwork != nil { + spec.SetNetwork(net) + } + log.V(1).Info("Reconciled network resources", "networkID", net.ID) + return nil +} + +func (s *Service) Delete(_ context.Context) error { + return nil +} + +// Get retrieves a STACKIT network by ID. +func (c *Client) Get(ctx context.Context, id string) (*Network, error) { + if id == "" { + return nil, fmt.Errorf("%w: network ID is empty", shared.ErrInvalidInput) + } + net, err := c.iaasClient.DefaultAPI.GetNetwork(ctx, c.projectID, c.region, id).Execute() + if err != nil { + return nil, shared.ClassifySDKError("get network", err) + } + ipv4 := net.GetIpv4() + return &Network{ + ID: net.GetId(), + Name: net.GetName(), + IPv4Prefixes: ipv4.GetPrefixes(), + }, nil +} + +func (s NetworkSpec) validate() error { + if s.ID == "" { + return fmt.Errorf("%w: network ID is required", shared.ErrInvalidInput) + } + return nil +} diff --git a/cloud/services/network/reconcile_test.go b/cloud/services/network/reconcile_test.go new file mode 100644 index 0000000..f7f2aa8 --- /dev/null +++ b/cloud/services/network/reconcile_test.go @@ -0,0 +1,167 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package network_test + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "slices" + "strings" + "testing" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" +) + +const ( + testProjectID = "00000000-0000-0000-0000-000000000000" + testRegion = "eu01" + testNetworkID = "11111111-1111-4111-8111-111111111111" +) + +type testScope struct { + projectID string + region string + iaasClient *iaas.APIClient + spec network.NetworkSpec +} + +func (s *testScope) ProjectID() string { + return s.projectID +} + +func (s *testScope) Region() string { + return s.region +} + +func (s *testScope) IaaSClient() *iaas.APIClient { + return s.iaasClient +} + +func (s *testScope) NetworkSpec() network.NetworkSpec { + return s.spec +} + +func newTestIaaSClient(t *testing.T, handler http.Handler) *iaas.APIClient { + t.Helper() + + srv := httptest.NewServer(handler) + t.Cleanup(srv.Close) + + iaasClient, err := iaas.NewAPIClient( + config.WithoutAuthentication(), + config.WithEndpoint(srv.URL), + ) + if err != nil { + t.Fatalf("iaas.NewAPIClient() error = %v", err) + } + return iaasClient +} + +func newTestService(t *testing.T, spec network.NetworkSpec, handler http.Handler) *network.Service { + t.Helper() + + return network.New(&testScope{ + projectID: testProjectID, + region: testRegion, + iaasClient: newTestIaaSClient(t, handler), + spec: spec, + }) +} + +func writeJSON(t *testing.T, w http.ResponseWriter, value any) { + t.Helper() + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(value); err != nil { + t.Fatalf("encode response body: %v", err) + } +} + +func TestServiceReconcile(t *testing.T) { + t.Run("fetches network and records IPv4 prefixes via SetNetwork", func(t *testing.T) { + var recorded *network.Network + spec := network.NetworkSpec{ + ID: testNetworkID, + SetNetwork: func(n *network.Network) { + recorded = n + }, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/networks/"+testNetworkID) { + writeJSON(t, w, map[string]any{ + "id": testNetworkID, + "name": "cluster-net", + "status": "CREATED", + "routed": true, + "ipv4": map[string]any{ + "prefixes": []string{"10.20.0.0/24"}, + }, + }) + return + } + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if recorded == nil || recorded.ID != testNetworkID || recorded.Name != "cluster-net" { + t.Fatalf("recorded = %#v, want ID=%s Name=cluster-net", recorded, testNetworkID) + } + if !slices.Equal(recorded.IPv4Prefixes, []string{"10.20.0.0/24"}) { + t.Fatalf("recorded.IPv4Prefixes = %v, want [10.20.0.0/24]", recorded.IPv4Prefixes) + } + }) + + t.Run("returns ErrNotFound when network does not exist", func(t *testing.T) { + spec := network.NetworkSpec{ + ID: testNetworkID, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + writeJSON(t, w, map[string]any{"message": "not found"}) + })) + + err := svc.Reconcile(context.Background()) + if !errors.Is(err, shared.ErrNotFound) { + t.Fatalf("Reconcile() error = %v, want ErrNotFound", err) + } + }) + + t.Run("returns ErrInvalidInput when network ID is empty", func(t *testing.T) { + svc := newTestService(t, network.NetworkSpec{}, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + err := svc.Reconcile(context.Background()) + if !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("Reconcile() error = %v, want ErrInvalidInput", err) + } + }) + + t.Run("Delete is a no-op", func(t *testing.T) { + svc := newTestService(t, network.NetworkSpec{}, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v, want nil", err) + } + }) +} diff --git a/cloud/services/network/service.go b/cloud/services/network/service.go new file mode 100644 index 0000000..a7f3554 --- /dev/null +++ b/cloud/services/network/service.go @@ -0,0 +1,69 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package network + +import iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + +// Network describes an existing STACKIT virtual network referenced by the provider. +type Network struct { + ID string + Name string + IPv4Prefixes []string +} + +// NetworkSpec describes the desired STACKIT network to resolve. +type NetworkSpec struct { + ID string + SetNetwork func(network *Network) +} + +// ClientScope is an interface that provides the IaaS client and target scope for network operations. +type ClientScope interface { + ProjectID() string + Region() string + IaaSClient() *iaas.APIClient +} + +// Scope is an interface that provides scope data needed for network reconciliation. +type Scope interface { + ClientScope + NetworkSpec() NetworkSpec +} + +// Client provides direct network operations against the STACKIT IaaS API. +type Client struct { + iaasClient *iaas.APIClient + projectID string + region string +} + +// NewClient returns a Client for the given scope. +func NewClient(scope ClientScope) *Client { + return &Client{ + iaasClient: scope.IaaSClient(), + projectID: scope.ProjectID(), + region: scope.Region(), + } +} + +// Service implements network reconciler. +type Service struct { + scope Scope + *Client +} + +// New returns Service from given scope. +func New(scope Scope) *Service { + return &Service{ + scope: scope, + Client: NewClient(scope), + } +} diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index 0d67b38..540deba 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -23,122 +23,92 @@ import ( func (s *Service) Reconcile(ctx context.Context) error { log := logf.FromContext(ctx) - if !s.scope.BastionSpec().Enabled { - return s.Delete(ctx) + spec := s.scope.PublicIPSpec() + if err := spec.validate(); err != nil { + return err } - log.V(1).Info("Reconciling public IP resources") - publicIP, err := s.createOrGetPublicIP(ctx) + publicIP, err := s.ensurePublicIP(ctx, spec) if err != nil { return err } - serverID := s.scope.BastionStatus().ServerID - if serverID != "" && publicIP.NetworkInterfaceID == "" { - log.V(1).Info("Attaching public IP to server", "publicIPID", publicIP.ID, "serverID", serverID) - if err := s.iaasClient.DefaultAPI. - AddPublicIpToServer(ctx, s.scope.ProjectID(), s.scope.Region(), serverID, publicIP.ID). - Execute(); err != nil { - err := shared.ClassifySDKError("add public IP to server", err) - if !shared.IsConflict(err) { - log.Error(err, "Failed to attach public IP to server", "publicIPID", publicIP.ID, "serverID", serverID) - return err - } + if spec.ServerID != "" && publicIP.NetworkInterfaceID == "" { + if err := s.AttachToServer(ctx, spec.ServerID, publicIP.ID); err != nil { + return err } - publicIP, err = s.getPublicIP(ctx, publicIP.ID) + publicIP, err = s.Get(ctx, publicIP.ID) if err != nil { - log.Error(err, "Failed to get public IP after attachment", "publicIPID", publicIP.ID) return err } } - s.scope.SetBastionPublicIP(publicIP.ID, publicIP.IP) + if spec.SetPublicIP != nil { + spec.SetPublicIP(publicIP.ID, publicIP.IP) + } + log.V(1).Info("Reconciled public IP resources", "publicIPID", publicIP.ID) return nil } func (s *Service) Delete(ctx context.Context) error { log := logf.FromContext(ctx) - log.V(1).Info("Deleting public IP resources") + spec := s.scope.PublicIPSpec() + if spec.ID == "" && len(spec.Tags) == 0 { + return fmt.Errorf("%w: public IP ID or tags are required", shared.ErrInvalidInput) + } - publicIP, err := s.findPublicIP(ctx) + publicIP, err := s.resolvePublicIP(ctx, spec) if err != nil { return err } if publicIP == nil { - s.scope.SetBastionPublicIP("", "") + if spec.SetPublicIP != nil { + spec.SetPublicIP("", "") + } return nil } - serverID, err := s.resolveServerID(ctx) + serverID, err := s.resolveServerID(ctx, spec) if err != nil { return err } if serverID != "" { - log.V(1).Info("Detaching public IP from server", "publicIPID", publicIP.ID, "serverID", serverID) - if err := DetachFromServer(ctx, s.iaasClient, s.scope.ProjectID(), s.scope.Region(), serverID, publicIP.ID); err != nil { - log.Error(err, "Failed to detach public IP from server", "publicIPID", publicIP.ID, "serverID", serverID) + if err := s.DetachFromServer(ctx, serverID, publicIP.ID); err != nil { return err } } - log.V(1).Info("Deleting public IP", "publicIPID", publicIP.ID) - if err := DeleteByID(ctx, s.iaasClient, s.scope.ProjectID(), s.scope.Region(), publicIP.ID); err != nil { - log.Error(err, "Failed to delete public IP", "publicIPID", publicIP.ID) + if err := s.DeleteByID(ctx, publicIP.ID); err != nil { return err } + log.V(1).Info("Deleted public IP", "publicIPID", publicIP.ID) - s.scope.SetBastionPublicIP("", "") + if spec.SetPublicIP != nil { + spec.SetPublicIP("", "") + } return nil } -func (s *Service) createOrGetPublicIP(ctx context.Context) (*PublicIP, error) { - log := logf.FromContext(ctx) - - publicIP, err := s.findPublicIP(ctx) - if err != nil { - return nil, err - } - if publicIP != nil { - return publicIP, nil - } - - log.V(1).Info("Creating public IP") - payload := iaas.NewCreatePublicIPPayload() - if tags := s.scope.BastionTags(); len(tags) > 0 { - payload.SetLabels(shared.TagsToSDKLabels(tags)) +// Get retrieves a STACKIT public IP by ID. +func (c *Client) Get(ctx context.Context, id string) (*PublicIP, error) { + if id == "" { + return nil, fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) } - created, err := s.iaasClient.DefaultAPI. - CreatePublicIP(ctx, s.scope.ProjectID(), s.scope.Region()). - CreatePublicIPPayload(*payload). - Execute() + publicIP, err := c.iaasClient.DefaultAPI.GetPublicIP(ctx, c.projectID, c.region, id).Execute() if err != nil { - err := shared.ClassifySDKError("create public IP", err) - log.Error(err, "Failed to create public IP") - return nil, err + return nil, shared.ClassifySDKError("get public IP", err) } - return publicIPFromSDK(created), nil + return publicIPFromSDK(publicIP), nil } -func (s *Service) findPublicIP(ctx context.Context) (*PublicIP, error) { - log := logf.FromContext(ctx) - if id := s.scope.BastionStatus().PublicIPID; id != "" { - publicIP, err := s.getPublicIP(ctx, id) - if err == nil { - return publicIP, nil - } - if !shared.IsNotFound(err) { - log.Error(err, "Failed to get public IP", "publicIPID", id) - return nil, err - } - } - - matched, err := ListByTags(ctx, s.iaasClient, s.scope.ProjectID(), s.scope.Region(), s.scope.BastionTags()) +// FindByTags finds a single STACKIT public IP matching the given tag selector. +func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*PublicIP, error) { + matched, err := c.ListByTags(ctx, tags) if err != nil { - log.Error(err, "Failed to list public IPs by tags") return nil, err } if len(matched) == 0 { - return nil, nil + return nil, fmt.Errorf("%w: no public IP matching tags", shared.ErrNotFound) } if len(matched) > 1 { return nil, fmt.Errorf("%w: multiple public IPs matching tags", shared.ErrConflict) @@ -146,42 +116,12 @@ func (s *Service) findPublicIP(ctx context.Context) (*PublicIP, error) { return matched[0], nil } -func (s *Service) getPublicIP(ctx context.Context, id string) (*PublicIP, error) { - if id == "" { - return nil, fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) - } - publicIP, err := s.iaasClient.DefaultAPI.GetPublicIP(ctx, s.scope.ProjectID(), s.scope.Region(), id).Execute() - if err != nil { - return nil, shared.ClassifySDKError("get public IP", err) - } - return publicIPFromSDK(publicIP), nil -} - -func (s *Service) resolveServerID(ctx context.Context) (string, error) { - if id := s.scope.BastionStatus().ServerID; id != "" { - return id, nil - } - srv, err := server.NewClient(s.scope).FindByTags(ctx, s.scope.BastionTags()) - if err != nil { - if shared.IsNotFound(err) { - return "", nil - } - return "", err - } - return srv.ID, nil -} - // ListByTags lists STACKIT public IPs matching the given tag selector. -func ListByTags( - ctx context.Context, - iaasClient *iaas.APIClient, - projectID, region string, - tags map[string]string, -) ([]*PublicIP, error) { +func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*PublicIP, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } - resp, err := iaasClient.DefaultAPI.ListPublicIPs(ctx, projectID, region). + resp, err := c.iaasClient.DefaultAPI.ListPublicIPs(ctx, c.projectID, c.region). LabelSelector(shared.LabelSelector(tags)). Execute() if err != nil { @@ -197,36 +137,46 @@ func ListByTags( return matched, nil } +// AttachToServer attaches a public IP to a server idempotently, tolerating Conflict errors. +func (c *Client) AttachToServer(ctx context.Context, serverID, publicIPID string) error { + if serverID == "" || publicIPID == "" { + return fmt.Errorf("%w: server ID and public IP ID are required", shared.ErrInvalidInput) + } + if err := c.iaasClient.DefaultAPI. + AddPublicIpToServer(ctx, c.projectID, c.region, serverID, publicIPID). + Execute(); err != nil { + err := shared.ClassifySDKError("add public IP to server", err) + if !shared.IsConflict(err) { + return err + } + } + logf.FromContext(ctx).V(1).Info("Attached public IP to server", "publicIPID", publicIPID, "serverID", serverID) + return nil +} + // DetachFromServer detaches a public IP from a server, ignoring NotFound and Conflict errors. -func DetachFromServer( - ctx context.Context, - iaasClient *iaas.APIClient, - projectID, region, serverID, publicIPID string, -) error { +func (c *Client) DetachFromServer(ctx context.Context, serverID, publicIPID string) error { if serverID == "" || publicIPID == "" { return fmt.Errorf("%w: server ID and public IP ID are required", shared.ErrInvalidInput) } - if err := iaasClient.DefaultAPI. - RemovePublicIpFromServer(ctx, projectID, region, serverID, publicIPID). + if err := c.iaasClient.DefaultAPI. + RemovePublicIpFromServer(ctx, c.projectID, c.region, serverID, publicIPID). Execute(); err != nil { err := shared.ClassifySDKError("remove public IP from server", err) if !shared.IsNotFound(err) && !shared.IsConflict(err) { return err } } + logf.FromContext(ctx).V(1).Info("Detached public IP from server", "publicIPID", publicIPID, "serverID", serverID) return nil } // DeleteByID deletes a public IP by ID, ignoring NotFound errors. -func DeleteByID( - ctx context.Context, - iaasClient *iaas.APIClient, - projectID, region, id string, -) error { +func (c *Client) DeleteByID(ctx context.Context, id string) error { if id == "" { return fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) } - if err := iaasClient.DefaultAPI.DeletePublicIP(ctx, projectID, region, id).Execute(); err != nil { + if err := c.iaasClient.DefaultAPI.DeletePublicIP(ctx, c.projectID, c.region, id).Execute(); err != nil { err := shared.ClassifySDKError("delete public IP", err) if !shared.IsNotFound(err) { return err @@ -235,6 +185,78 @@ func DeleteByID( return nil } +func (c *Client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*PublicIP, error) { + publicIP, err := c.resolvePublicIP(ctx, spec) + if err != nil { + return nil, err + } + if publicIP != nil { + return publicIP, nil + } + + payload := iaas.NewCreatePublicIPPayload() + if len(spec.Tags) > 0 { + payload.SetLabels(shared.TagsToSDKLabels(spec.Tags)) + } + created, err := c.iaasClient.DefaultAPI. + CreatePublicIP(ctx, c.projectID, c.region). + CreatePublicIPPayload(*payload). + Execute() + if err != nil { + return nil, shared.ClassifySDKError("create public IP", err) + } + out := publicIPFromSDK(created) + logf.FromContext(ctx).V(1).Info("Created public IP", "publicIPID", out.ID) + return out, nil +} + +func (c *Client) resolvePublicIP(ctx context.Context, spec PublicIPSpec) (*PublicIP, error) { + if spec.ID != "" { + publicIP, err := c.Get(ctx, spec.ID) + if err == nil { + return publicIP, nil + } + if !shared.IsNotFound(err) { + return nil, err + } + } + if len(spec.Tags) == 0 { + return nil, nil + } + publicIP, err := c.FindByTags(ctx, spec.Tags) + if shared.IsNotFound(err) { + return nil, nil + } + if err != nil { + return nil, err + } + return publicIP, nil +} + +func (s *Service) resolveServerID(ctx context.Context, spec PublicIPSpec) (string, error) { + if spec.ServerID != "" { + return spec.ServerID, nil + } + if len(spec.ServerSelector) == 0 { + return "", nil + } + srv, err := server.NewClient(s.scope).FindByTags(ctx, spec.ServerSelector) + if err != nil { + if shared.IsNotFound(err) { + return "", nil + } + return "", err + } + return srv.ID, nil +} + +func (s PublicIPSpec) validate() error { + if len(s.Tags) == 0 { + return fmt.Errorf("%w: public IP tags are required", shared.ErrInvalidInput) + } + return nil +} + func publicIPFromSDK(in *iaas.PublicIp) *PublicIP { if in == nil { return nil diff --git a/cloud/services/publicip/reconcile_test.go b/cloud/services/publicip/reconcile_test.go index af2ad7e..b6fc52b 100644 --- a/cloud/services/publicip/reconcile_test.go +++ b/cloud/services/publicip/reconcile_test.go @@ -12,350 +12,410 @@ package publicip_test import ( "context" + "encoding/json" "errors" + "net/http" + "net/http/httptest" + "strings" "testing" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes/scheme" - clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" - "sigs.k8s.io/controller-runtime/pkg/client/fake" + "github.com/stackitcloud/stackit-sdk-go/core/config" + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" - infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud" - cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" - "github.com/stackitcloud/cluster-api-provider-stackit/scope" ) -func init() { - _ = clusterv1.AddToScheme(scheme.Scheme) - _ = infrav1.AddToScheme(scheme.Scheme) +const ( + testProjectID = "00000000-0000-0000-0000-000000000000" + testRegion = "eu01" + testPublicIPID = "11111111-1111-4111-8111-111111111111" + testOtherPublicIPID = "22222222-2222-4222-8222-222222222222" + testServerID = "33333333-3333-4333-8333-333333333333" + testNetworkInterface = "[VISA_REDACTED]-8444-444444444444" + testPublicIPAddress = "203.0.113.22" +) + +type testScope struct { + projectID string + region string + iaasClient *iaas.APIClient + spec publicip.PublicIPSpec } -func newTestClusterScope(t *testing.T, bastionEnabled bool, cloudClient cloud.Client) *scope.ClusterScope { - t.Helper() +func (s *testScope) ProjectID() string { + return s.projectID +} - cluster := &clusterv1.Cluster{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-cluster", - Namespace: "default", - }, - } - stackitCluster := &infrav1.StackitCluster{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-cluster", - Namespace: "default", - }, - Spec: infrav1.StackitClusterSpec{ - ProjectID: "00000000-0000-0000-0000-000000000000", - Region: "eu01", - Bastion: infrav1.StackitBastionSpec{ - Enabled: bastionEnabled, - }, - }, - } +func (s *testScope) Region() string { + return s.region +} - k8sClient := fake.NewClientBuilder(). - WithScheme(scheme.Scheme). - WithObjects(cluster, stackitCluster). - Build() +func (s *testScope) IaaSClient() *iaas.APIClient { + return s.iaasClient +} - clusterScope, err := scope.NewClusterScope(k8sClient, cluster, stackitCluster) +func (s *testScope) PublicIPSpec() publicip.PublicIPSpec { + return s.spec +} + +func newTestIaaSClient(t *testing.T, handler http.Handler) *iaas.APIClient { + t.Helper() + + srv := httptest.NewServer(handler) + t.Cleanup(srv.Close) + + iaasClient, err := iaas.NewAPIClient( + config.WithoutAuthentication(), + config.WithEndpoint(srv.URL), + ) if err != nil { - t.Fatalf("NewClusterScope() error = %v", err) + t.Fatalf("iaas.NewAPIClient() error = %v", err) } - clusterScope.SetCloudClient(cloudClient) - return clusterScope + return iaasClient } -func TestServiceReconcile(t *testing.T) { - t.Run("deletes existing public IP when bastion is disabled", func(t *testing.T) { - ctx := context.Background() - fakeCloud := cloudfake.New() - clusterScope := newTestClusterScope(t, false, fakeCloud) - - created, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) - if err != nil { - t.Fatalf("CreatePublicIP() error = %v", err) - } - clusterScope.SetBastionPublicIP(created.ID, created.IP) - - svc := publicip.New(clusterScope) - if err := svc.Reconcile(ctx); err != nil { - t.Fatalf("Reconcile() error = %v", err) - } +func newTestService(t *testing.T, spec publicip.PublicIPSpec, handler http.Handler) *publicip.Service { + t.Helper() - if fakeCloud.PublicIPCount() != 0 { - t.Fatalf("PublicIPCount() = %d, want 0", fakeCloud.PublicIPCount()) - } - if clusterScope.BastionStatus().PublicIPID != "" { - t.Fatalf("BastionStatus().PublicIPID = %q, want empty", clusterScope.BastionStatus().PublicIPID) - } + return publicip.New(&testScope{ + projectID: testProjectID, + region: testRegion, + iaasClient: newTestIaaSClient(t, handler), + spec: spec, }) +} - t.Run("creates public IP and attaches it to the bastion server", func(t *testing.T) { - ctx := context.Background() - fakeCloud := cloudfake.New() - clusterScope := newTestClusterScope(t, true, fakeCloud) - - server, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ - Name: "test-cluster-bastion", - Tags: clusterScope.BastionTags(), - }) - if err != nil { - t.Fatalf("CreateServer() error = %v", err) - } - clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID +func writeJSON(t *testing.T, w http.ResponseWriter, value any) { + t.Helper() + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(value); err != nil { + t.Fatalf("encode response body: %v", err) + } +} - svc := publicip.New(clusterScope) - if err := svc.Reconcile(ctx); err != nil { - t.Fatalf("Reconcile() error = %v", err) - } +func readJSON(t *testing.T, r *http.Request) map[string]any { + t.Helper() + var out map[string]any + if err := json.NewDecoder(r.Body).Decode(&out); err != nil { + t.Fatalf("decode request body: %v", err) + } + return out +} - if fakeCloud.PublicIPCount() != 1 { - t.Fatalf("PublicIPCount() = %d, want 1", fakeCloud.PublicIPCount()) +func TestServiceReconcile(t *testing.T) { + t.Run("creates public IP and attaches it to server when not found", func(t *testing.T) { + var ( + createPayload map[string]any + attachedPair string + recordedID string + recordedIP string + ) + spec := publicip.PublicIPSpec{ + Tags: map[string]string{"cluster": "test"}, + ServerID: testServerID, + SetPublicIP: func(id, ip string) { + recordedID = id + recordedIP = ip + }, } - if clusterScope.BastionStatus().PublicIPID == "" { - t.Fatal("BastionStatus().PublicIPID is empty, want set") + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips"): + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodPost && strings.HasSuffix(path, "/public-ips"): + createPayload = readJSON(t, r) + writeJSON(t, w, map[string]any{ + "id": testPublicIPID, + "ip": testPublicIPAddress, + }) + case r.Method == http.MethodPut && strings.HasSuffix(path, "/servers/"+testServerID+"/public-ips/"+testPublicIPID): + attachedPair = testServerID + ":" + testPublicIPID + w.WriteHeader(http.StatusNoContent) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips/"+testPublicIPID): + writeJSON(t, w, map[string]any{ + "id": testPublicIPID, + "ip": testPublicIPAddress, + "networkInterface": testNetworkInterface, + }) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) } - if clusterScope.StackitCluster.Status.Bastion.PublicIP != "203.0.113.22" { - t.Fatalf("Bastion.PublicIP = %q, want 203.0.113.22", clusterScope.StackitCluster.Status.Bastion.PublicIP) + if attachedPair != testServerID+":"+testPublicIPID { + t.Fatalf("attachedPair = %q, want %s:%s", attachedPair, testServerID, testPublicIPID) } - - gotIP, err := fakeCloud.GetPublicIP(ctx, clusterScope.BastionStatus().PublicIPID) - if err != nil { - t.Fatalf("GetPublicIP() error = %v", err) + if recordedID != testPublicIPID || recordedIP != testPublicIPAddress { + t.Fatalf("recorded = (%q, %q), want (%q, %q)", recordedID, recordedIP, testPublicIPID, testPublicIPAddress) } - if gotIP.NetworkInterfaceID == "" { - t.Fatal("PublicIP NetworkInterfaceID is empty, want attached to server") + labels, _ := createPayload["labels"].(map[string]any) + if labels["cluster"] != "test" { + t.Fatalf("createPayload labels = %#v, want cluster=test", labels) } }) - t.Run("reuses existing public IP found by status ID without listing by tags", func(t *testing.T) { - ctx := context.Background() - fakeCloud := cloudfake.New() - clusterScope := newTestClusterScope(t, true, fakeCloud) - - server, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ - Name: "test-cluster-bastion", - Tags: clusterScope.BastionTags(), - }) - if err != nil { - t.Fatalf("CreateServer() error = %v", err) + t.Run("reuses existing public IP found by ID without listing by tags", func(t *testing.T) { + var recordedID, recordedIP string + spec := publicip.PublicIPSpec{ + ID: testPublicIPID, + Tags: map[string]string{"cluster": "test"}, + ServerID: testServerID, + SetPublicIP: func(id, ip string) { + recordedID = id + recordedIP = ip + }, } - clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID - existing, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) - if err != nil { - t.Fatalf("CreatePublicIP() error = %v", err) - } - clusterScope.SetBastionPublicIP(existing.ID, existing.IP) - fakeCloud.FailNextListPublicIPs = shared.ErrTransient + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/public-ips/"+testPublicIPID) { + writeJSON(t, w, map[string]any{ + "id": testPublicIPID, + "ip": testPublicIPAddress, + "networkInterface": testNetworkInterface, + }) + return + } + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) - svc := publicip.New(clusterScope) - if err := svc.Reconcile(ctx); err != nil { + if err := svc.Reconcile(context.Background()); err != nil { t.Fatalf("Reconcile() error = %v", err) } - - if fakeCloud.PublicIPCount() != 1 { - t.Fatalf("PublicIPCount() = %d, want 1", fakeCloud.PublicIPCount()) - } - if clusterScope.BastionStatus().PublicIPID != existing.ID { - t.Fatalf("BastionStatus().PublicIPID = %q, want %q", clusterScope.BastionStatus().PublicIPID, existing.ID) + if recordedID != testPublicIPID || recordedIP != testPublicIPAddress { + t.Fatalf("recorded = (%q, %q), want (%q, %q)", recordedID, recordedIP, testPublicIPID, testPublicIPAddress) } }) +} +func TestServiceReconcileReuseAndErrors(t *testing.T) { t.Run("reuses existing public IP found by tags without creating duplicate", func(t *testing.T) { - ctx := context.Background() - fakeCloud := cloudfake.New() - clusterScope := newTestClusterScope(t, true, fakeCloud) - - server, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ - Name: "test-cluster-bastion", - Tags: clusterScope.BastionTags(), - }) - if err != nil { - t.Fatalf("CreateServer() error = %v", err) - } - clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID - - existing, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) - if err != nil { - t.Fatalf("CreatePublicIP() error = %v", err) + var recordedID string + spec := publicip.PublicIPSpec{ + Tags: map[string]string{"cluster": "test"}, + SetPublicIP: func(id, _ string) { + recordedID = id + }, } - svc := publicip.New(clusterScope) - if err := svc.Reconcile(ctx); err != nil { + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/public-ips") { + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{ + "id": testPublicIPID, + "ip": testPublicIPAddress, + "labels": map[string]any{"cluster": "test"}, + }, + }}) + return + } + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + if err := svc.Reconcile(context.Background()); err != nil { t.Fatalf("Reconcile() error = %v", err) } - - if fakeCloud.PublicIPCount() != 1 { - t.Fatalf("PublicIPCount() = %d, want 1", fakeCloud.PublicIPCount()) - } - if clusterScope.BastionStatus().PublicIPID != existing.ID { - t.Fatalf("BastionStatus().PublicIPID = %q, want %q", clusterScope.BastionStatus().PublicIPID, existing.ID) + if recordedID != testPublicIPID { + t.Fatalf("recordedID = %q, want %q", recordedID, testPublicIPID) } }) t.Run("tolerates conflict when attaching public IP to server", func(t *testing.T) { - ctx := context.Background() - fakeCloud := cloudfake.New() - clusterScope := newTestClusterScope(t, true, fakeCloud) - - server, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ - Name: "test-cluster-bastion", - Tags: clusterScope.BastionTags(), - }) - if err != nil { - t.Fatalf("CreateServer() error = %v", err) + var recordedID string + spec := publicip.PublicIPSpec{ + ID: testPublicIPID, + Tags: map[string]string{"cluster": "test"}, + ServerID: testServerID, + SetPublicIP: func(id, _ string) { + recordedID = id + }, } - clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID - fakeCloud.FailNextAddPublicIP = shared.ErrConflict - svc := publicip.New(clusterScope) - if err := svc.Reconcile(ctx); err != nil { + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips/"+testPublicIPID): + writeJSON(t, w, map[string]any{ + "id": testPublicIPID, + "ip": testPublicIPAddress, + }) + case r.Method == http.MethodPut && strings.HasSuffix(path, "/servers/"+testServerID+"/public-ips/"+testPublicIPID): + w.WriteHeader(http.StatusConflict) + writeJSON(t, w, map[string]any{"message": "conflict"}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { t.Fatalf("Reconcile() error = %v, want nil on conflict", err) } - if clusterScope.BastionStatus().PublicIPID == "" { - t.Fatal("BastionStatus().PublicIPID is empty, want set") + if recordedID != testPublicIPID { + t.Fatalf("recordedID = %q, want %q", recordedID, testPublicIPID) } }) - t.Run("returns conflict error when multiple public IPs match tags", func(t *testing.T) { - ctx := context.Background() - fakeCloud := cloudfake.New() - clusterScope := newTestClusterScope(t, true, fakeCloud) - - if _, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()); err != nil { - t.Fatalf("CreatePublicIP() error = %v", err) - } - if _, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()); err != nil { - t.Fatalf("CreatePublicIP() error = %v", err) + t.Run("returns ErrConflict when multiple public IPs match tags", func(t *testing.T) { + spec := publicip.PublicIPSpec{ + Tags: map[string]string{"cluster": "test"}, } - svc := publicip.New(clusterScope) - err := svc.Reconcile(ctx) + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testPublicIPID, "ip": testPublicIPAddress, "labels": map[string]any{"cluster": "test"}}, + map[string]any{"id": testOtherPublicIPID, "ip": "203.0.113.23", "labels": map[string]any{"cluster": "test"}}, + }}) + })) + + err := svc.Reconcile(context.Background()) if !errors.Is(err, shared.ErrConflict) { t.Fatalf("Reconcile() error = %v, want ErrConflict", err) } }) - t.Run("returns error when creating public IP fails", func(t *testing.T) { - ctx := context.Background() - fakeCloud := cloudfake.New() - clusterScope := newTestClusterScope(t, true, fakeCloud) - fakeCloud.FailNextCreatePublicIP = shared.ErrTransient + t.Run("returns ErrInvalidInput when tags are empty", func(t *testing.T) { + svc := newTestService(t, publicip.PublicIPSpec{}, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) - svc := publicip.New(clusterScope) - err := svc.Reconcile(ctx) - if !errors.Is(err, shared.ErrTransient) { - t.Fatalf("Reconcile() error = %v, want ErrTransient", err) + if err := svc.Reconcile(context.Background()); !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("Reconcile() error = %v, want ErrInvalidInput", err) } }) } func TestServiceDelete(t *testing.T) { - t.Run("detaches and deletes public IP using recorded status ID", func(t *testing.T) { - ctx := context.Background() - fakeCloud := cloudfake.New() - clusterScope := newTestClusterScope(t, true, fakeCloud) - - server, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ - Name: "test-cluster-bastion", - Tags: clusterScope.BastionTags(), - }) - if err != nil { - t.Fatalf("CreateServer() error = %v", err) - } - clusterScope.StackitCluster.Status.Bastion.ServerID = server.ID - - created, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) - if err != nil { - t.Fatalf("CreatePublicIP() error = %v", err) - } - if err := fakeCloud.AddPublicIPToServer(ctx, server.ID, created.ID); err != nil { - t.Fatalf("AddPublicIPToServer() error = %v", err) + t.Run("detaches and deletes public IP using recorded ID and clears state", func(t *testing.T) { + var ( + detachedPair string + deletedID string + clearedID = "not-cleared" + clearedIP = "not-cleared" + ) + spec := publicip.PublicIPSpec{ + ID: testPublicIPID, + ServerID: testServerID, + SetPublicIP: func(id, ip string) { + clearedID = id + clearedIP = ip + }, } - clusterScope.SetBastionPublicIP(created.ID, created.IP) - svc := publicip.New(clusterScope) - if err := svc.Delete(ctx); err != nil { + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips/"+testPublicIPID): + writeJSON(t, w, map[string]any{ + "id": testPublicIPID, + "ip": testPublicIPAddress, + "networkInterface": testNetworkInterface, + }) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/servers/"+testServerID+"/public-ips/"+testPublicIPID): + detachedPair = testServerID + ":" + testPublicIPID + w.WriteHeader(http.StatusNoContent) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/public-ips/"+testPublicIPID): + deletedID = testPublicIPID + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Delete(context.Background()); err != nil { t.Fatalf("Delete() error = %v", err) } - - if fakeCloud.PublicIPCount() != 0 { - t.Fatalf("PublicIPCount() = %d, want 0", fakeCloud.PublicIPCount()) + if detachedPair != testServerID+":"+testPublicIPID { + t.Fatalf("detachedPair = %q, want %s:%s", detachedPair, testServerID, testPublicIPID) } - if clusterScope.BastionStatus().PublicIPID != "" { - t.Fatalf("BastionStatus().PublicIPID = %q, want empty", clusterScope.BastionStatus().PublicIPID) + if deletedID != testPublicIPID { + t.Fatalf("deletedID = %q, want %q", deletedID, testPublicIPID) } - if clusterScope.StackitCluster.Status.Bastion.PublicIP != "" { - t.Fatalf("Bastion.PublicIP = %q, want empty", clusterScope.StackitCluster.Status.Bastion.PublicIP) + if clearedID != "" || clearedIP != "" { + t.Fatalf("cleared = (%q, %q), want both empty", clearedID, clearedIP) } }) - t.Run("falls back to tag lookup for both public IP and server when status IDs are empty", func(t *testing.T) { - ctx := context.Background() - fakeCloud := cloudfake.New() - clusterScope := newTestClusterScope(t, true, fakeCloud) - - server, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ - Name: "test-cluster-bastion", - Tags: clusterScope.BastionTags(), - }) - if err != nil { - t.Fatalf("CreateServer() error = %v", err) - } - created, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) - if err != nil { - t.Fatalf("CreatePublicIP() error = %v", err) - } - if err := fakeCloud.AddPublicIPToServer(ctx, server.ID, created.ID); err != nil { - t.Fatalf("AddPublicIPToServer() error = %v", err) - } - - svc := publicip.New(clusterScope) - if err := svc.Delete(ctx); err != nil { + t.Run("falls back to tag lookup for public IP and server when IDs are empty", func(t *testing.T) { + var ( + detachedPair string + deletedID string + ) + spec := publicip.PublicIPSpec{ + Tags: map[string]string{"cluster": "test"}, + ServerSelector: map[string]string{"cluster": "test"}, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{ + "id": testPublicIPID, + "ip": testPublicIPAddress, + "networkInterface": testNetworkInterface, + "labels": map[string]any{"cluster": "test"}, + }, + }}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{ + "id": testServerID, + "name": "bastion", + "machineType": "c2i.1", + "labels": map[string]any{"cluster": "test"}, + }, + }}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers/"+testServerID+"/nics"): + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/servers/"+testServerID+"/public-ips/"+testPublicIPID): + detachedPair = testServerID + ":" + testPublicIPID + w.WriteHeader(http.StatusNoContent) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/public-ips/"+testPublicIPID): + deletedID = testPublicIPID + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Delete(context.Background()); err != nil { t.Fatalf("Delete() error = %v", err) } - - if fakeCloud.PublicIPCount() != 0 { - t.Fatalf("PublicIPCount() = %d, want 0", fakeCloud.PublicIPCount()) + if detachedPair != testServerID+":"+testPublicIPID { + t.Fatalf("detachedPair = %q, want %s:%s", detachedPair, testServerID, testPublicIPID) + } + if deletedID != testPublicIPID { + t.Fatalf("deletedID = %q, want %q", deletedID, testPublicIPID) } }) t.Run("succeeds when no public IP exists", func(t *testing.T) { - ctx := context.Background() - fakeCloud := cloudfake.New() - clusterScope := newTestClusterScope(t, true, fakeCloud) + spec := publicip.PublicIPSpec{ + Tags: map[string]string{"cluster": "test"}, + } - svc := publicip.New(clusterScope) - if err := svc.Delete(ctx); err != nil { + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + writeJSON(t, w, map[string]any{"items": []any{}}) + })) + + if err := svc.Delete(context.Background()); err != nil { t.Fatalf("Delete() error = %v, want nil", err) } }) - t.Run("returns error when DeletePublicIP fails with non-NotFound error", func(t *testing.T) { - ctx := context.Background() - fakeCloud := cloudfake.New() - clusterScope := newTestClusterScope(t, true, fakeCloud) - - created, err := fakeCloud.CreatePublicIP(ctx, clusterScope.BastionTags()) - if err != nil { - t.Fatalf("CreatePublicIP() error = %v", err) - } - clusterScope.SetBastionPublicIP(created.ID, created.IP) - fakeCloud.FailNextDeletePublicIP = shared.ErrTransient + t.Run("returns ErrInvalidInput when both ID and tags are empty", func(t *testing.T) { + svc := newTestService(t, publicip.PublicIPSpec{}, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) - svc := publicip.New(clusterScope) - err = svc.Delete(ctx) - if !errors.Is(err, shared.ErrTransient) { - t.Fatalf("Delete() error = %v, want ErrTransient", err) - } - if clusterScope.BastionStatus().PublicIPID != created.ID { - t.Fatalf("BastionStatus().PublicIPID = %q, want preserved %q on error", clusterScope.BastionStatus().PublicIPID, created.ID) + if err := svc.Delete(context.Background()); !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("Delete() error = %v, want ErrInvalidInput", err) } }) } diff --git a/cloud/services/publicip/service.go b/cloud/services/publicip/service.go index 6fbb8d3..e167da2 100644 --- a/cloud/services/publicip/service.go +++ b/cloud/services/publicip/service.go @@ -10,11 +10,7 @@ You may obtain a copy of the License at package publicip -import ( - iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" - - infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" -) +import iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" // PublicIP describes a STACKIT public IP resource. type PublicIP struct { @@ -23,27 +19,54 @@ type PublicIP struct { NetworkInterfaceID string } -// Scope is an interface that provides cluster scope data needed for public IP reconciliation. -type Scope interface { +// PublicIPSpec describes the desired state of a STACKIT public IP. +type PublicIPSpec struct { + ID string + Tags map[string]string + ServerID string + ServerSelector map[string]string + SetPublicIP func(id, ip string) +} + +// ClientScope is an interface that provides the IaaS client and target scope for public IP operations. +type ClientScope interface { ProjectID() string Region() string IaaSClient() *iaas.APIClient - BastionSpec() infrav1.StackitBastionSpec - BastionStatus() infrav1.StackitBastionStatus - BastionTags() map[string]string - SetBastionPublicIP(id, ip string) +} + +// Scope is an interface that provides scope data needed for public IP reconciliation. +type Scope interface { + ClientScope + PublicIPSpec() PublicIPSpec +} + +// Client provides direct public IP operations against the STACKIT IaaS API. +type Client struct { + iaasClient *iaas.APIClient + projectID string + region string +} + +// NewClient returns a Client for the given scope. +func NewClient(scope ClientScope) *Client { + return &Client{ + iaasClient: scope.IaaSClient(), + projectID: scope.ProjectID(), + region: scope.Region(), + } } // Service implements public IPs reconciler. type Service struct { - scope Scope - iaasClient *iaas.APIClient + scope Scope + *Client } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ - scope: scope, - iaasClient: scope.IaaSClient(), + scope: scope, + Client: NewClient(scope), } } diff --git a/cloud/types.go b/cloud/types.go index f618eed..ac2d95a 100644 --- a/cloud/types.go +++ b/cloud/types.go @@ -15,6 +15,7 @@ package cloud import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" @@ -26,15 +27,8 @@ type Server = server.Server // Address is an IP or DNS endpoint of a Server. type Address = server.Address -// RootVolume describes the root disk of a VM. -type RootVolume = server.RootVolume - // Network is an existing STACKIT virtual network referenced by the provider. -type Network struct { - ID string - Name string - IPv4Prefixes []string -} +type Network = network.Network // LoadBalancer describes an API-server load balancer. type LoadBalancer = loadbalancer.LoadBalancer @@ -44,7 +38,3 @@ type PublicIP = publicip.PublicIP // SecurityGroup describes a STACKIT security group. type SecurityGroup = securitygroup.SecurityGroup - -// LoadBalancerTargetInput describes a VM target in the API-server load -// balancer target pool. -type LoadBalancerTargetInput = loadbalancer.Target diff --git a/controller/controller_test_helpers_test.go b/controller/controller_test_helpers_test.go index b8a77f9..8407de9 100644 --- a/controller/controller_test_helpers_test.go +++ b/controller/controller_test_helpers_test.go @@ -23,6 +23,7 @@ import ( clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" + "github.com/stackitcloud/cluster-api-provider-stackit/scope" ) const ( @@ -118,7 +119,7 @@ func createReadyStackitCluster(ctx context.Context, name, namespace, credentials Expect(k8sClient.Create(ctx, stackitCluster)).To(Succeed()) stackitCluster.Status.Ready = true stackitCluster.Status.APIServerEndpoint = clusterv1.APIEndpoint{Host: "203.0.113.10", Port: 6443} - stackitCluster.Status.FailureDomains = stackitFailureDomains(stackitCluster.Spec.Region) + stackitCluster.Status.FailureDomains = scope.DefaultFailureDomains(stackitCluster.Spec.Region) Expect(k8sClient.Status().Update(ctx, stackitCluster)).To(Succeed()) } diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index b94d69f..19bd7a0 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -14,15 +14,11 @@ import ( "context" "errors" "fmt" - "net/netip" - "slices" - "strings" "time" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" "sigs.k8s.io/cluster-api/util/collections" "sigs.k8s.io/cluster-api/util/record" ctrl "sigs.k8s.io/controller-runtime" @@ -31,7 +27,9 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/availabilityzone" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" @@ -52,7 +50,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS return ctrl.Result{}, fmt.Errorf("persist finalizer: %w", err) } } - clusterScope.SetFailureDomains(stackitFailureDomains(stackitCluster.Spec.Region)) + clusterScope.SetFailureDomains(scope.DefaultFailureDomains(stackitCluster.Spec.Region)) cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, stackitCluster) if err != nil { @@ -76,8 +74,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS infrav1.ClusterCredentialsReadyCondition, ) - zones, err := cloudClient.ListAvailabilityZones(ctx) - if err != nil { + if err := availabilityzone.New(clusterScope).Reconcile(ctx); err != nil { stackitCluster.Status.Ready = false return util.CloudFailureResult( &stackitCluster.Status.Conditions, @@ -89,10 +86,8 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS infrav1.ClusterReadyCondition, ) } - clusterScope.SetFailureDomains(failureDomainsFromZones(stackitCluster.Spec.Region, zones)) - network, err := cloudClient.GetNetwork(ctx, stackitCluster.Spec.Network.ID) - if err != nil { + if err := network.New(clusterScope).Reconcile(ctx); err != nil { log.Error(err, "Failed to get network", "networkID", stackitCluster.Spec.Network.ID, "region", stackitCluster.Spec.Region, @@ -149,7 +144,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS // come back with the control plane behind it. seed := targets if len(seed) == 0 { - seed = []loadbalancer.Target{loadbalancer.BootstrapTarget(bootstrapTargetIP(network))} + seed = []loadbalancer.Target{loadbalancer.BootstrapTarget(clusterScope.BootstrapTargetIP())} } clusterScope.SetAPIServerLoadBalancerTargets(targets, seed) @@ -277,78 +272,6 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS return ctrl.Result{}, nil } -func failureDomainsFromZones(region string, zones []string) []clusterv1.FailureDomain { - controlPlane := true - failureDomains := make([]clusterv1.FailureDomain, 0, len(zones)) - for _, zone := range zones { - // Filter out virtual/metro zones (such as eu01-m or any zone with suffix -m) - if strings.HasSuffix(zone, "-m") { - continue - } - failureDomains = append(failureDomains, clusterv1.FailureDomain{ - Name: zone, - ControlPlane: &controlPlane, - Attributes: map[string]string{ - "region": region, - }, - }) - } - if len(failureDomains) == 0 { - return stackitFailureDomains(region) - } - slices.SortFunc(failureDomains, func(a, b clusterv1.FailureDomain) int { - return strings.Compare(a.Name, b.Name) - }) - return failureDomains -} - -func stackitFailureDomains(region string) []clusterv1.FailureDomain { - controlPlane := true - return []clusterv1.FailureDomain{ - { - Name: region + "-1", - ControlPlane: &controlPlane, - Attributes: map[string]string{ - "region": region, - }, - }, - { - Name: region + "-2", - ControlPlane: &controlPlane, - Attributes: map[string]string{ - "region": region, - }, - }, - { - Name: region + "-3", - ControlPlane: &controlPlane, - Attributes: map[string]string{ - "region": region, - }, - }, - } -} - -func bootstrapTargetIP(network *cloud.Network) string { - if network == nil { - return "10.0.0.1" - } - for _, prefixValue := range network.IPv4Prefixes { - prefix, err := netip.ParsePrefix(prefixValue) - if err != nil || !prefix.Addr().Is4() { - continue - } - address := prefix.Masked().Addr() - for range 10 { - address = address.Next() - } - if prefix.Contains(address) { - return address.String() - } - } - return "10.0.0.1" -} - func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterScope *scope.ClusterScope) (ctrl.Result, error) { log := logf.FromContext(ctx) stackitCluster := clusterScope.StackitCluster diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index 371cb37..c4e6515 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -15,7 +15,9 @@ package scope import ( "context" + "net/netip" "slices" + "strings" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -25,7 +27,10 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/availabilityzone" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/util" @@ -42,6 +47,8 @@ type ClusterScope struct { StackitCluster *infrav1.StackitCluster CloudClient cloud.Client + network *network.Network + loadBalancerTargets []loadbalancer.Target loadBalancerSeedTargets []loadbalancer.Target @@ -245,18 +252,109 @@ func (s *ClusterScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec { } } -func (s *ClusterScope) BastionSpec() infrav1.StackitBastionSpec { - return s.StackitCluster.Spec.Bastion +func (s *ClusterScope) AvailabilityZoneSpec() availabilityzone.AvailabilityZoneSpec { + return availabilityzone.AvailabilityZoneSpec{ + SetZones: func(zones []string) { + s.SetFailureDomains(failureDomainsFromZones(s.StackitCluster.Spec.Region, zones)) + }, + } } -func (s *ClusterScope) BastionStatus() infrav1.StackitBastionStatus { - return s.StackitCluster.Status.Bastion +func (s *ClusterScope) NetworkSpec() network.NetworkSpec { + return network.NetworkSpec{ + ID: s.StackitCluster.Spec.Network.ID, + SetNetwork: func(net *network.Network) { + s.network = net + }, + } +} + +func (s *ClusterScope) BootstrapTargetIP() string { + if s.network == nil { + return "10.0.0.1" + } + for _, prefixValue := range s.network.IPv4Prefixes { + prefix, err := netip.ParsePrefix(prefixValue) + if err != nil || !prefix.Addr().Is4() { + continue + } + address := prefix.Masked().Addr() + for range 10 { + address = address.Next() + } + if prefix.Contains(address) { + return address.String() + } + } + return "10.0.0.1" +} + +func (s *ClusterScope) PublicIPSpec() publicip.PublicIPSpec { + return publicip.PublicIPSpec{ + ID: s.StackitCluster.Status.Bastion.PublicIPID, + Tags: s.BastionTags(), + ServerID: s.StackitCluster.Status.Bastion.ServerID, + ServerSelector: s.BastionTags(), + SetPublicIP: s.SetBastionPublicIP, + } } func (s *ClusterScope) BastionTags() map[string]string { return bastionTags(s.StackitCluster) } +func DefaultFailureDomains(region string) []clusterv1.FailureDomain { + controlPlane := true + return []clusterv1.FailureDomain{ + { + Name: region + "-1", + ControlPlane: &controlPlane, + Attributes: map[string]string{ + "region": region, + }, + }, + { + Name: region + "-2", + ControlPlane: &controlPlane, + Attributes: map[string]string{ + "region": region, + }, + }, + { + Name: region + "-3", + ControlPlane: &controlPlane, + Attributes: map[string]string{ + "region": region, + }, + }, + } +} + +func failureDomainsFromZones(region string, zones []string) []clusterv1.FailureDomain { + controlPlane := true + failureDomains := make([]clusterv1.FailureDomain, 0, len(zones)) + for _, zone := range zones { + // Filter out virtual/metro zones (such as eu01-m or any zone with suffix -m) + if strings.HasSuffix(zone, "-m") { + continue + } + failureDomains = append(failureDomains, clusterv1.FailureDomain{ + Name: zone, + ControlPlane: &controlPlane, + Attributes: map[string]string{ + "region": region, + }, + }) + } + if len(failureDomains) == 0 { + return DefaultFailureDomains(region) + } + slices.SortFunc(failureDomains, func(a, b clusterv1.FailureDomain) int { + return strings.Compare(a.Name, b.Name) + }) + return failureDomains +} + func bastionTags(stackitCluster *infrav1.StackitCluster) map[string]string { tags := util.ClusterTags(stackitCluster.Name, stackitCluster.Namespace, stackitCluster.Spec.AdditionalLabels) tags[util.LabelResourceRole] = util.ResourceRoleBastion diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index a545529..625b770 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -494,7 +494,7 @@ var _ = Describe("Manager", Ordered, func() { Eventually(func(g Gomega) { output := kubectlOutput(g, "get", "stackitcluster", clusterName, "-n", cfg.Namespace, "-o", "jsonpath={.status.bastion.publicIP}") g.Expect(output).NotTo(BeEmpty()) - publicIPs, err := publicip.ListByTags(ctx, cloudClient.IaaSClient(), cloudClient.ProjectID(), cloudClient.Region(), stackitE2EBastionTags(testID)) + publicIPs, err := publicip.NewClient(cloudClient).ListByTags(ctx, stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) g.Expect(publicIPs).To(HaveLen(1)) securityGroups, err := securitygroup.NewClient(cloudClient).ListByTags(ctx, stackitE2EBastionTags(testID)) @@ -560,7 +560,7 @@ var _ = Describe("Manager", Ordered, func() { loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) g.Expect(err).NotTo(HaveOccurred()) g.Expect(loadBalancers).To(BeEmpty()) - publicIPs, err := publicip.ListByTags(ctx, cloudClient.IaaSClient(), cloudClient.ProjectID(), cloudClient.Region(), stackitE2EBastionTags(testID)) + publicIPs, err := publicip.NewClient(cloudClient).ListByTags(ctx, stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) g.Expect(publicIPs).To(BeEmpty()) sgClient := securitygroup.NewClient(cloudClient) From 86ad4a93c1a1a55c2c9ee4458d19fc862aa8b6fc Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Thu, 12 Nov 2026 16:56:12 +0100 Subject: [PATCH 11/60] refactor: remove cloud middle-man wrappers and deduplicate service helpers --- cloud/cleanup.go | 5 +- cloud/errors.go | 41 ---- cloud/fake/client.go | 177 +++++++++--------- cloud/sdk_client.go | 11 +- cloud/sdk_client_integration_test.go | 9 +- cloud/sdk_client_test.go | 14 +- cloud/services/loadbalancer/reconcile.go | 8 +- cloud/services/network/reconcile.go | 2 +- cloud/services/network/service.go | 18 +- cloud/services/publicip/reconcile.go | 30 ++- cloud/services/publicip/service.go | 18 +- cloud/services/securitygroup/reconcile.go | 37 ++-- .../services/securitygroup/reconcile_test.go | 2 + cloud/services/securitygroup/rules.go | 8 +- cloud/services/securitygroup/service.go | 18 +- cloud/services/server/reconcile.go | 20 +- cloud/services/server/service.go | 18 +- cloud/services/shared/errors_test.go | 26 +++ cloud/services/shared/utils.go | 48 +++++ cloud/types.go | 40 ---- controller/stackitcluster_bastion.go | 18 +- controller/stackitcluster_controller_test.go | 22 ++- controller/stackitcluster_infrastructure.go | 12 +- controller/stackitmachine_controller_test.go | 22 ++- controller/stackitmachine_infrastructure.go | 8 +- scope/machine_scope.go | 2 +- test/e2e/e2e_test.go | 21 ++- util/conditions.go | 6 +- 28 files changed, 297 insertions(+), 364 deletions(-) delete mode 100644 cloud/errors.go delete mode 100644 cloud/types.go diff --git a/cloud/cleanup.go b/cloud/cleanup.go index 9e580fb..d55614c 100644 --- a/cloud/cleanup.go +++ b/cloud/cleanup.go @@ -20,6 +20,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) // CleanupByTags deletes provider-managed cloud resources matching tags without @@ -27,7 +28,7 @@ import ( // server deletion is not blocked by target attachments. func CleanupByTags(ctx context.Context, client Client, tags map[string]string) error { if len(tags) == 0 { - return fmt.Errorf("%w: empty cleanup tag selector", ErrInvalidInput) + return fmt.Errorf("%w: empty cleanup tag selector", shared.ErrInvalidInput) } var errs []string @@ -101,7 +102,7 @@ func CleanupByTags(ctx context.Context, client Client, tags map[string]string) e } if len(errs) > 0 { - return fmt.Errorf("%w: %s", ErrTransient, strings.Join(errs, "; ")) + return fmt.Errorf("%w: %s", shared.ErrTransient, strings.Join(errs, "; ")) } return nil } diff --git a/cloud/errors.go b/cloud/errors.go deleted file mode 100644 index e038288..0000000 --- a/cloud/errors.go +++ /dev/null @@ -1,41 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -package cloud - -import "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" - -// Sentinel errors returned by Client implementations, re-exported from shared. -// Controllers use errors.Is to classify them into requeue / condition-update behavior. -var ( - ErrNotFound = shared.ErrNotFound - ErrUnauthorized = shared.ErrUnauthorized - ErrInvalidInput = shared.ErrInvalidInput - ErrConflict = shared.ErrConflict - ErrTransient = shared.ErrTransient -) - -// IsNotFound reports whether err wraps ErrNotFound. -func IsNotFound(err error) bool { return shared.IsNotFound(err) } - -// IsUnauthorized reports whether err wraps ErrUnauthorized. -func IsUnauthorized(err error) bool { return shared.IsUnauthorized(err) } - -// IsInvalidInput reports whether err wraps ErrInvalidInput. -func IsInvalidInput(err error) bool { return shared.IsInvalidInput(err) } - -// IsConflict reports whether err wraps ErrConflict. -func IsConflict(err error) bool { return shared.IsConflict(err) } - -// IsTransient reports whether err wraps ErrTransient. -func IsTransient(err error) bool { return shared.IsTransient(err) } - -// IsRetryable reports whether err should trigger a requeue (conflict or transient). -func IsRetryable(err error) bool { return shared.IsRetryable(err) } diff --git a/cloud/fake/client.go b/cloud/fake/client.go index 0071c57..0432edd 100644 --- a/cloud/fake/client.go +++ b/cloud/fake/client.go @@ -24,10 +24,12 @@ import ( "strings" "sync" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/stackit-sdk-go/core/config" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" @@ -46,7 +48,7 @@ type Client struct { loadBalancers map[string]*lbEntry publicIPs map[string]*publicIPEntry securityGroups map[string]*securityGroupEntry - networks map[string]*cloud.Network + networks map[string]*network.Network iaasClient *iaas.APIClient lbClient *lb.APIClient @@ -91,20 +93,20 @@ type Client struct { } type serverEntry struct { - server *cloud.Server + server *server.Server tags map[string]string securityGroups map[string]struct{} userData []byte } type lbEntry struct { - lb *cloud.LoadBalancer + lb *loadbalancer.LoadBalancer tags map[string]string targets map[string]string } type publicIPEntry struct { - publicIP *cloud.PublicIP + publicIP *publicip.PublicIP tags map[string]string } @@ -114,7 +116,7 @@ type securityGroupRuleEntry struct { } type securityGroupEntry struct { - securityGroup *cloud.SecurityGroup + securityGroup *securitygroup.SecurityGroup description string tags map[string]string allowedCIDRs []string @@ -123,13 +125,13 @@ type securityGroupEntry struct { } // New returns a Client preconfigured with the given networks. -func New(networks ...cloud.Network) *Client { +func New(networks ...network.Network) *Client { c := &Client{ servers: make(map[string]*serverEntry), loadBalancers: make(map[string]*lbEntry), publicIPs: make(map[string]*publicIPEntry), securityGroups: make(map[string]*securityGroupEntry), - networks: make(map[string]*cloud.Network), + networks: make(map[string]*network.Network), } for i := range networks { n := networks[i] @@ -187,7 +189,7 @@ func (c *Client) genID() string { return fmt.Sprintf("00000000-0000-4000-8000-%012d", c.nextID) } -func (c *Client) GetServer(_ context.Context, id string) (*cloud.Server, error) { +func (c *Client) GetServer(_ context.Context, id string) (*server.Server, error) { c.mu.Lock() defer c.mu.Unlock() @@ -196,21 +198,21 @@ func (c *Client) GetServer(_ context.Context, id string) (*cloud.Server, error) } entry, ok := c.servers[id] if !ok { - return nil, fmt.Errorf("server %q: %w", id, cloud.ErrNotFound) + return nil, fmt.Errorf("server %q: %w", id, shared.ErrNotFound) } return cloneServer(entry.server), nil } -func (c *Client) ListServersByTags(_ context.Context, tags map[string]string) ([]*cloud.Server, error) { +func (c *Client) ListServersByTags(_ context.Context, tags map[string]string) ([]*server.Server, error) { c.mu.Lock() defer c.mu.Unlock() if len(tags) == 0 { - return nil, fmt.Errorf("empty tags: %w", cloud.ErrInvalidInput) + return nil, fmt.Errorf("empty tags: %w", shared.ErrInvalidInput) } - servers := []*cloud.Server{} + servers := []*server.Server{} for _, entry := range c.servers { - if mapContains(entry.tags, tags) { + if shared.MapContains(entry.tags, tags) { servers = append(servers, cloneServer(entry.server)) } } @@ -218,7 +220,7 @@ func (c *Client) ListServersByTags(_ context.Context, tags map[string]string) ([ } // CreateServer creates a server in the fake store. -func (c *Client) CreateServer(_ context.Context, input server.ServerSpec) (*cloud.Server, error) { +func (c *Client) CreateServer(_ context.Context, input server.ServerSpec) (*server.Server, error) { c.mu.Lock() if c.BeforeCreateServer != nil { c.mu.Unlock() @@ -231,7 +233,7 @@ func (c *Client) CreateServer(_ context.Context, input server.ServerSpec) (*clou return nil, err } for _, entry := range c.servers { - if len(input.Tags) > 0 && mapContains(entry.tags, input.Tags) { + if len(input.Tags) > 0 && shared.MapContains(entry.tags, input.Tags) { return cloneServer(entry.server), nil } } @@ -247,12 +249,12 @@ func (c *Client) createServerEntry(input server.ServerSpec) *serverEntry { if zone == "" { zone = "eu01-1" } - srv := &cloud.Server{ + srv := &server.Server{ ID: id, Name: input.Name, State: "ACTIVE", AvailabilityZone: zone, - Addresses: []cloud.Address{ + Addresses: []server.Address{ {Type: "InternalIP", Address: "10.0.0.10"}, }, } @@ -279,7 +281,7 @@ func (c *Client) DeleteServer(_ context.Context, id string) error { return nil } -func (c *Client) GetNetwork(_ context.Context, id string) (*cloud.Network, error) { +func (c *Client) GetNetwork(_ context.Context, id string) (*network.Network, error) { c.mu.Lock() if c.BeforeGetNetwork != nil { c.mu.Unlock() @@ -293,13 +295,13 @@ func (c *Client) GetNetwork(_ context.Context, id string) (*cloud.Network, error } n, ok := c.networks[id] if !ok { - return nil, fmt.Errorf("network %q: %w", id, cloud.ErrNotFound) + return nil, fmt.Errorf("network %q: %w", id, shared.ErrNotFound) } out := *n return &out, nil } -func (c *Client) getPublicIP(_ context.Context, id string) (*cloud.PublicIP, error) { +func (c *Client) getPublicIP(_ context.Context, id string) (*publicip.PublicIP, error) { c.mu.Lock() defer c.mu.Unlock() @@ -307,17 +309,17 @@ func (c *Client) getPublicIP(_ context.Context, id string) (*cloud.PublicIP, err return nil, err } if id == "" { - return nil, fmt.Errorf("public IP ID is empty: %w", cloud.ErrInvalidInput) + return nil, fmt.Errorf("public IP ID is empty: %w", shared.ErrInvalidInput) } entry, ok := c.publicIPs[id] if !ok { - return nil, fmt.Errorf("public IP %q: %w", id, cloud.ErrNotFound) + return nil, fmt.Errorf("public IP %q: %w", id, shared.ErrNotFound) } out := *entry.publicIP return &out, nil } -func (c *Client) ListPublicIPsByTags(_ context.Context, tags map[string]string) ([]*cloud.PublicIP, error) { +func (c *Client) ListPublicIPsByTags(_ context.Context, tags map[string]string) ([]*publicip.PublicIP, error) { c.mu.Lock() defer c.mu.Unlock() @@ -325,11 +327,11 @@ func (c *Client) ListPublicIPsByTags(_ context.Context, tags map[string]string) return nil, err } if len(tags) == 0 { - return nil, fmt.Errorf("empty tags: %w", cloud.ErrInvalidInput) + return nil, fmt.Errorf("empty tags: %w", shared.ErrInvalidInput) } - publicIPs := []*cloud.PublicIP{} + publicIPs := []*publicip.PublicIP{} for _, entry := range c.publicIPs { - if mapContains(entry.tags, tags) { + if shared.MapContains(entry.tags, tags) { out := *entry.publicIP publicIPs = append(publicIPs, &out) } @@ -337,7 +339,7 @@ func (c *Client) ListPublicIPsByTags(_ context.Context, tags map[string]string) return publicIPs, nil } -func (c *Client) CreatePublicIP(_ context.Context, tags map[string]string) (*cloud.PublicIP, error) { +func (c *Client) CreatePublicIP(_ context.Context, tags map[string]string) (*publicip.PublicIP, error) { c.mu.Lock() defer c.mu.Unlock() @@ -345,7 +347,7 @@ func (c *Client) CreatePublicIP(_ context.Context, tags map[string]string) (*clo return nil, err } id := c.genID() - publicIP := &cloud.PublicIP{ + publicIP := &publicip.PublicIP{ ID: id, IP: "203.0.113.22", } @@ -365,10 +367,10 @@ func (c *Client) deletePublicIP(_ context.Context, id string) error { return err } if id == "" { - return fmt.Errorf("public IP ID is empty: %w", cloud.ErrInvalidInput) + return fmt.Errorf("public IP ID is empty: %w", shared.ErrInvalidInput) } if _, ok := c.publicIPs[id]; !ok { - return fmt.Errorf("public IP %q: %w", id, cloud.ErrNotFound) + return fmt.Errorf("public IP %q: %w", id, shared.ErrNotFound) } delete(c.publicIPs, id) return nil @@ -382,14 +384,14 @@ func (c *Client) addPublicIPToServer(_ context.Context, serverID, publicIPID str return err } if serverID == "" || publicIPID == "" { - return fmt.Errorf("server ID and public IP ID are required: %w", cloud.ErrInvalidInput) + return fmt.Errorf("server ID and public IP ID are required: %w", shared.ErrInvalidInput) } if _, ok := c.servers[serverID]; !ok { - return fmt.Errorf("server %q: %w", serverID, cloud.ErrNotFound) + return fmt.Errorf("server %q: %w", serverID, shared.ErrNotFound) } entry, ok := c.publicIPs[publicIPID] if !ok { - return fmt.Errorf("public IP %q: %w", publicIPID, cloud.ErrNotFound) + return fmt.Errorf("public IP %q: %w", publicIPID, shared.ErrNotFound) } entry.publicIP.NetworkInterfaceID = "fake-nic-" + serverID return nil @@ -403,26 +405,26 @@ func (c *Client) removePublicIPFromServer(_ context.Context, serverID, publicIPI return err } if serverID == "" || publicIPID == "" { - return fmt.Errorf("server ID and public IP ID are required: %w", cloud.ErrInvalidInput) + return fmt.Errorf("server ID and public IP ID are required: %w", shared.ErrInvalidInput) } entry, ok := c.publicIPs[publicIPID] if !ok { - return fmt.Errorf("public IP %q: %w", publicIPID, cloud.ErrNotFound) + return fmt.Errorf("public IP %q: %w", publicIPID, shared.ErrNotFound) } entry.publicIP.NetworkInterfaceID = "" return nil } -func (c *Client) ListSecurityGroupsByTags(_ context.Context, tags map[string]string) ([]*cloud.SecurityGroup, error) { +func (c *Client) ListSecurityGroupsByTags(_ context.Context, tags map[string]string) ([]*securitygroup.SecurityGroup, error) { c.mu.Lock() defer c.mu.Unlock() if len(tags) == 0 { - return nil, fmt.Errorf("empty tags: %w", cloud.ErrInvalidInput) + return nil, fmt.Errorf("empty tags: %w", shared.ErrInvalidInput) } - securityGroups := []*cloud.SecurityGroup{} + securityGroups := []*securitygroup.SecurityGroup{} for _, entry := range c.securityGroups { - if mapContains(entry.tags, tags) { + if shared.MapContains(entry.tags, tags) { out := *entry.securityGroup securityGroups = append(securityGroups, &out) } @@ -430,7 +432,7 @@ func (c *Client) ListSecurityGroupsByTags(_ context.Context, tags map[string]str return securityGroups, nil } -func (c *Client) CreateSecurityGroup(_ context.Context, name string, tags map[string]string) (*cloud.SecurityGroup, error) { +func (c *Client) CreateSecurityGroup(_ context.Context, name string, tags map[string]string) (*securitygroup.SecurityGroup, error) { c.mu.Lock() defer c.mu.Unlock() @@ -442,7 +444,7 @@ func (c *Client) CreateSecurityGroup(_ context.Context, name string, tags map[st return &out, nil } -func (c *Client) CreateLoadBalancer(_ context.Context, name string, tags map[string]string) (*cloud.LoadBalancer, error) { +func (c *Client) CreateLoadBalancer(_ context.Context, name string, tags map[string]string) (*loadbalancer.LoadBalancer, error) { c.mu.Lock() defer c.mu.Unlock() @@ -454,17 +456,17 @@ func (c *Client) CreateLoadBalancer(_ context.Context, name string, tags map[str return &out, nil } -func (c *Client) ListLoadBalancersByTags(_ context.Context, tags map[string]string) ([]*cloud.LoadBalancer, error) { +func (c *Client) ListLoadBalancersByTags(_ context.Context, tags map[string]string) ([]*loadbalancer.LoadBalancer, error) { c.mu.Lock() defer c.mu.Unlock() if len(tags) == 0 { - return nil, fmt.Errorf("empty tags: %w", cloud.ErrInvalidInput) + return nil, fmt.Errorf("empty tags: %w", shared.ErrInvalidInput) } wantLabels := loadbalancer.Labels(tags) - loadBalancers := []*cloud.LoadBalancer{} + loadBalancers := []*loadbalancer.LoadBalancer{} for _, entry := range c.loadBalancers { - if mapContains(entry.tags, wantLabels) || mapContains(entry.tags, tags) { + if shared.MapContains(entry.tags, wantLabels) || shared.MapContains(entry.tags, tags) { out := *entry.lb loadBalancers = append(loadBalancers, &out) } @@ -594,15 +596,6 @@ func (c *Client) SetLoadBalancerIP(id, ip string) { } } -func mapContains(haystack, needle map[string]string) bool { - for k, v := range needle { - if haystack[k] != v { - return false - } - } - return true -} - func copyTags(in map[string]string) map[string]string { if in == nil { return nil @@ -641,10 +634,10 @@ func (c *Client) ListAvailabilityZones(_ context.Context) ([]string, error) { return []string{"eu01-1", "eu01-2", "eu01-3"}, nil } -func cloneServer(s *cloud.Server) *cloud.Server { +func cloneServer(s *server.Server) *server.Server { out := *s if s.Addresses != nil { - out.Addresses = make([]cloud.Address, len(s.Addresses)) + out.Addresses = make([]server.Address, len(s.Addresses)) copy(out.Addresses, s.Addresses) } return &out @@ -846,7 +839,7 @@ func (c *Client) getLoadBalancerItem(id string) (map[string]any, error) { } entry, ok := c.loadBalancers[id] if !ok { - return nil, fmt.Errorf("load balancer %q: %w", id, cloud.ErrNotFound) + return nil, fmt.Errorf("load balancer %q: %w", id, shared.ErrNotFound) } return fakeLoadBalancerMap(entry), nil } @@ -867,12 +860,12 @@ func (c *Client) createLoadBalancer(name string, port int32, labels map[string]s func (c *Client) createLoadBalancerEntry(name string, port int32, labels map[string]string, targets []loadbalancer.Target) (*lbEntry, error) { if name == "" { - return nil, fmt.Errorf("load balancer name is empty: %w", cloud.ErrInvalidInput) + return nil, fmt.Errorf("load balancer name is empty: %w", shared.ErrInvalidInput) } if port <= 0 { port = 6443 } - lbObj := &cloud.LoadBalancer{ + lbObj := &loadbalancer.LoadBalancer{ ID: name, Name: name, IP: "203.0.113.10", @@ -903,19 +896,19 @@ func (c *Client) updateLoadBalancerTargetPool(id string, port int32, targets []l return nil, err } if id == "" || port <= 0 { - return nil, fmt.Errorf("load balancer ID and target port are required: %w", cloud.ErrInvalidInput) + return nil, fmt.Errorf("load balancer ID and target port are required: %w", shared.ErrInvalidInput) } if len(targets) == 0 { - return nil, fmt.Errorf("at least one target is required: %w", cloud.ErrInvalidInput) + return nil, fmt.Errorf("at least one target is required: %w", shared.ErrInvalidInput) } entry, ok := c.loadBalancers[id] if !ok { - return nil, fmt.Errorf("load balancer %q: %w", id, cloud.ErrNotFound) + return nil, fmt.Errorf("load balancer %q: %w", id, shared.ErrNotFound) } replaced := make(map[string]string, len(targets)) for _, target := range targets { if target.Name == "" || target.IP == "" { - return nil, fmt.Errorf("target name and target IP are required: %w", cloud.ErrInvalidInput) + return nil, fmt.Errorf("target name and target IP are required: %w", shared.ErrInvalidInput) } replaced[target.Name] = target.IP } @@ -990,7 +983,7 @@ func (c *Client) handlePublicIPRequest(rec *httptest.ResponseRecorder, r *http.R case http.MethodDelete: err = c.removePublicIPFromServer(r.Context(), serverID, publicIPID) default: - err = cloud.ErrInvalidInput + err = shared.ErrInvalidInput } writeFakeStatus(rec, err) return true @@ -1055,7 +1048,7 @@ func (c *Client) handleSecurityGroupRequest(rec *httptest.ResponseRecorder, r *h case http.MethodDelete: err = c.removeSecurityGroupFromServer(serverID, securityGroupID) default: - err = cloud.ErrInvalidInput + err = shared.ErrInvalidInput } writeFakeStatus(rec, err) return true @@ -1173,18 +1166,18 @@ func (c *Client) listPublicIPItems(tags map[string]string) ([]map[string]any, er return nil, err } if len(tags) == 0 { - return nil, fmt.Errorf("empty tags: %w", cloud.ErrInvalidInput) + return nil, fmt.Errorf("empty tags: %w", shared.ErrInvalidInput) } items := make([]map[string]any, 0, len(c.publicIPs)) for _, entry := range c.publicIPs { - if mapContains(entry.tags, tags) { + if shared.MapContains(entry.tags, tags) { items = append(items, fakePublicIPMap(entry.publicIP, entry.tags)) } } return items, nil } -func fakePublicIPMap(p *cloud.PublicIP, tags map[string]string) map[string]any { +func fakePublicIPMap(p *publicip.PublicIP, tags map[string]string) map[string]any { m := map[string]any{ "id": p.ID, "ip": p.IP, @@ -1211,10 +1204,10 @@ func (c *Client) addSecurityGroupToServer(serverID, securityGroupID string) erro } serverEntry, ok := c.servers[serverID] if !ok { - return fmt.Errorf("server %q: %w", serverID, cloud.ErrNotFound) + return fmt.Errorf("server %q: %w", serverID, shared.ErrNotFound) } if _, ok := c.securityGroups[securityGroupID]; !ok { - return fmt.Errorf("security group %q: %w", securityGroupID, cloud.ErrNotFound) + return fmt.Errorf("security group %q: %w", securityGroupID, shared.ErrNotFound) } if serverEntry.securityGroups == nil { serverEntry.securityGroups = map[string]struct{}{} @@ -1281,7 +1274,7 @@ func (c *Client) deleteServerHTTP(id string) error { return err } if _, ok := c.servers[id]; !ok { - return fmt.Errorf("server %q: %w", id, cloud.ErrNotFound) + return fmt.Errorf("server %q: %w", id, shared.ErrNotFound) } delete(c.servers, id) return nil @@ -1372,7 +1365,7 @@ func (c *Client) getServerItem(id string) (map[string]any, error) { } entry, ok := c.servers[id] if !ok { - return nil, fmt.Errorf("server %q: %w", id, cloud.ErrNotFound) + return nil, fmt.Errorf("server %q: %w", id, shared.ErrNotFound) } return fakeServerMap(entry), nil } @@ -1383,7 +1376,7 @@ func (c *Client) listServerNICItems(serverID string) ([]map[string]any, error) { entry, ok := c.servers[serverID] if !ok { - return nil, fmt.Errorf("server %q: %w", serverID, cloud.ErrNotFound) + return nil, fmt.Errorf("server %q: %w", serverID, shared.ErrNotFound) } items := make([]map[string]any, 0, len(entry.server.Addresses)) for _, addr := range entry.server.Addresses { @@ -1401,7 +1394,7 @@ func (c *Client) listServerItems(tags map[string]string) ([]map[string]any, erro } items := make([]map[string]any, 0, len(c.servers)) for _, entry := range c.servers { - if len(tags) > 0 && !mapContains(entry.tags, tags) { + if len(tags) > 0 && !shared.MapContains(entry.tags, tags) { continue } items = append(items, fakeServerMap(entry)) @@ -1436,7 +1429,7 @@ func (c *Client) removeSecurityGroupFromServer(serverID, securityGroupID string) serverEntry, ok := c.servers[serverID] if !ok { - return fmt.Errorf("server %q: %w", serverID, cloud.ErrNotFound) + return fmt.Errorf("server %q: %w", serverID, shared.ErrNotFound) } delete(serverEntry.securityGroups, securityGroupID) return nil @@ -1455,10 +1448,10 @@ func (c *Client) createSecurityGroup(name, description string, tags map[string]s func (c *Client) createSecurityGroupEntry(name, description string, tags map[string]string) (*securityGroupEntry, error) { if name == "" { - return nil, fmt.Errorf("security group name is empty: %w", cloud.ErrInvalidInput) + return nil, fmt.Errorf("security group name is empty: %w", shared.ErrInvalidInput) } id := c.genID() - sg := &cloud.SecurityGroup{ + sg := &securitygroup.SecurityGroup{ ID: id, Name: name, } @@ -1477,11 +1470,11 @@ func (c *Client) listSecurityGroupItems(tags map[string]string) ([]map[string]an defer c.mu.Unlock() if len(tags) == 0 { - return nil, fmt.Errorf("empty tags: %w", cloud.ErrInvalidInput) + return nil, fmt.Errorf("empty tags: %w", shared.ErrInvalidInput) } items := make([]map[string]any, 0, len(c.securityGroups)) for _, entry := range c.securityGroups { - if mapContains(entry.tags, tags) { + if shared.MapContains(entry.tags, tags) { items = append(items, fakeSecurityGroupMap(entry)) } } @@ -1494,7 +1487,7 @@ func (c *Client) listSecurityGroupRuleItems(sgID string) ([]map[string]any, erro entry, ok := c.securityGroups[sgID] if !ok { - return nil, fmt.Errorf("security group %q: %w", sgID, cloud.ErrNotFound) + return nil, fmt.Errorf("security group %q: %w", sgID, shared.ErrNotFound) } items := make([]map[string]any, 0, len(entry.rules)) for _, rule := range entry.rules { @@ -1509,7 +1502,7 @@ func (c *Client) createSecurityGroupRule(sgID string, rule securityGroupRuleEntr entry, ok := c.securityGroups[sgID] if !ok { - return nil, fmt.Errorf("security group %q: %w", sgID, cloud.ErrNotFound) + return nil, fmt.Errorf("security group %q: %w", sgID, shared.ErrNotFound) } if entry.rules == nil { entry.rules = make(map[string]*securityGroupRuleEntry) @@ -1532,13 +1525,13 @@ func (c *Client) deleteSecurityGroupRule(sgID, ruleID string) error { entry, ok := c.securityGroups[sgID] if !ok { - return fmt.Errorf("security group %q: %w", sgID, cloud.ErrNotFound) + return fmt.Errorf("security group %q: %w", sgID, shared.ErrNotFound) } if entry.rules == nil { - return fmt.Errorf("rule %q: %w", ruleID, cloud.ErrNotFound) + return fmt.Errorf("rule %q: %w", ruleID, shared.ErrNotFound) } if _, ok := entry.rules[ruleID]; !ok { - return fmt.Errorf("rule %q: %w", ruleID, cloud.ErrNotFound) + return fmt.Errorf("rule %q: %w", ruleID, shared.ErrNotFound) } delete(entry.rules, ruleID) return nil @@ -1549,14 +1542,14 @@ func (c *Client) deleteSecurityGroup(id string) error { defer c.mu.Unlock() if id == "" { - return fmt.Errorf("security group ID is empty: %w", cloud.ErrInvalidInput) + return fmt.Errorf("security group ID is empty: %w", shared.ErrInvalidInput) } if _, ok := c.securityGroups[id]; !ok { - return fmt.Errorf("security group %q: %w", id, cloud.ErrNotFound) + return fmt.Errorf("security group %q: %w", id, shared.ErrNotFound) } for serverID, srv := range c.servers { if _, attached := srv.securityGroups[id]; attached { - return fmt.Errorf("security group %q is still attached to server %q: %w", id, serverID, cloud.ErrConflict) + return fmt.Errorf("security group %q is still attached to server %q: %w", id, serverID, shared.ErrConflict) } } delete(c.securityGroups, id) @@ -1617,13 +1610,13 @@ func parseLabelSelector(selector string) map[string]string { func writeFakeError(rec *httptest.ResponseRecorder, err error) { status := http.StatusInternalServerError switch { - case cloud.IsNotFound(err): + case shared.IsNotFound(err): status = http.StatusNotFound - case cloud.IsConflict(err): + case shared.IsConflict(err): status = http.StatusConflict - case cloud.IsInvalidInput(err): + case shared.IsInvalidInput(err): status = http.StatusBadRequest - case cloud.IsUnauthorized(err): + case shared.IsUnauthorized(err): status = http.StatusUnauthorized } rec.WriteHeader(status) diff --git a/cloud/sdk_client.go b/cloud/sdk_client.go index 8edebfd..45a06e2 100644 --- a/cloud/sdk_client.go +++ b/cloud/sdk_client.go @@ -15,6 +15,7 @@ import ( "fmt" "os" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/stackit-sdk-go/core/config" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" @@ -43,22 +44,22 @@ type SDKClient struct { // service-account key flow. func NewClient(_ context.Context, creds Credentials) (Client, error) { if creds.ProjectID == "" { - return nil, fmt.Errorf("%w: project ID is empty", ErrInvalidInput) + return nil, fmt.Errorf("%w: project ID is empty", shared.ErrInvalidInput) } if creds.Region == "" { - return nil, fmt.Errorf("%w: region is empty", ErrInvalidInput) + return nil, fmt.Errorf("%w: region is empty", shared.ErrInvalidInput) } if len(creds.ServiceAccountJSON) == 0 && os.Getenv(stackitNoAuthEnv) != "true" { - return nil, fmt.Errorf("%w: service account JSON is empty", ErrInvalidInput) + return nil, fmt.Errorf("%w: service account JSON is empty", shared.ErrInvalidInput) } iaasClient, err := iaas.NewAPIClient(stackitOptions(creds, os.Getenv(stackitIAASEndpointEnv))...) if err != nil { - return nil, fmt.Errorf("%w: create IAAS SDK client: %v", ErrInvalidInput, err) + return nil, fmt.Errorf("%w: create IAAS SDK client: %v", shared.ErrInvalidInput, err) } lbClient, err := lb.NewAPIClient(stackitOptions(creds, os.Getenv(stackitLBEndpointEnv))...) if err != nil { - return nil, fmt.Errorf("%w: create load balancer SDK client: %v", ErrInvalidInput, err) + return nil, fmt.Errorf("%w: create load balancer SDK client: %v", shared.ErrInvalidInput, err) } return &SDKClient{ diff --git a/cloud/sdk_client_integration_test.go b/cloud/sdk_client_integration_test.go index d33bf2b..5933d27 100644 --- a/cloud/sdk_client_integration_test.go +++ b/cloud/sdk_client_integration_test.go @@ -23,6 +23,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) const ( @@ -40,7 +41,7 @@ func TestSDKClientCredentialsIntegration(t *testing.T) { _, err := server.NewClient(client).FindByTags(context.Background(), map[string]string{ "capistackit-test": "credential-check", }) - if IsNotFound(err) { + if shared.IsNotFound(err) { return } if err != nil { @@ -90,7 +91,7 @@ func TestSDKClientLoadBalancerTargetIntegration(t *testing.T) { }) lbClient := loadbalancer.NewClient(client) t.Cleanup(func() { - if err := lbClient.DeleteByID(context.Background(), lbID); err != nil && !IsNotFound(err) { + if err := lbClient.DeleteByID(context.Background(), lbID); err != nil && !shared.IsNotFound(err) { t.Logf("DeleteByID() cleanup error = %v", err) } }) @@ -217,8 +218,8 @@ func createIntegrationLoadBalancer(t *testing.T, client Client, networkID string } t.Cleanup(func() { if err := loadbalancer.NewClient(client).DeleteByID(context.Background(), lbID); err != nil && - !IsNotFound(err) && - !errors.Is(err, ErrInvalidInput) { + !shared.IsNotFound(err) && + !errors.Is(err, shared.ErrInvalidInput) { t.Logf("DeleteByID() cleanup error = %v", err) } }) diff --git a/cloud/sdk_client_test.go b/cloud/sdk_client_test.go index f392c1d..d3d1856 100644 --- a/cloud/sdk_client_test.go +++ b/cloud/sdk_client_test.go @@ -14,6 +14,8 @@ import ( "context" "errors" "testing" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) const ( @@ -28,8 +30,8 @@ func TestNewClientValidation(t *testing.T) { Region: testSDKRegion, ServiceAccountJSON: []byte(`{}`), }) - if !errors.Is(err, ErrInvalidInput) { - t.Fatalf("NewClient() error = %v, want ErrInvalidInput", err) + if !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("NewClient() error = %v, want shared.ErrInvalidInput", err) } }) @@ -38,8 +40,8 @@ func TestNewClientValidation(t *testing.T) { ProjectID: testSDKProjectID, ServiceAccountJSON: []byte(`{}`), }) - if !errors.Is(err, ErrInvalidInput) { - t.Fatalf("NewClient() error = %v, want ErrInvalidInput", err) + if !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("NewClient() error = %v, want shared.ErrInvalidInput", err) } }) @@ -49,8 +51,8 @@ func TestNewClientValidation(t *testing.T) { ProjectID: testSDKProjectID, Region: testSDKRegion, }) - if !errors.Is(err, ErrInvalidInput) { - t.Fatalf("NewClient() error = %v, want ErrInvalidInput", err) + if !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("NewClient() error = %v, want shared.ErrInvalidInput", err) } }) diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index 4ecc124..31f8429 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -83,13 +83,7 @@ func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*LoadB if err != nil { return nil, err } - if len(matched) == 0 { - return nil, fmt.Errorf("%w: no load balancer matching tags", shared.ErrNotFound) - } - if len(matched) > 1 { - return nil, fmt.Errorf("%w: multiple load balancers matching tags", shared.ErrConflict) - } - return matched[0], nil + return shared.FindSingle(matched, "load balancer", "load balancers") } // ListByTags lists STACKIT load balancers matching the given tag selector. diff --git a/cloud/services/network/reconcile.go b/cloud/services/network/reconcile.go index 7f4b993..d5f7511 100644 --- a/cloud/services/network/reconcile.go +++ b/cloud/services/network/reconcile.go @@ -46,7 +46,7 @@ func (c *Client) Get(ctx context.Context, id string) (*Network, error) { if id == "" { return nil, fmt.Errorf("%w: network ID is empty", shared.ErrInvalidInput) } - net, err := c.iaasClient.DefaultAPI.GetNetwork(ctx, c.projectID, c.region, id).Execute() + net, err := c.IaaSClient().DefaultAPI.GetNetwork(ctx, c.ProjectID(), c.Region(), id).Execute() if err != nil { return nil, shared.ClassifySDKError("get network", err) } diff --git a/cloud/services/network/service.go b/cloud/services/network/service.go index a7f3554..0d31415 100644 --- a/cloud/services/network/service.go +++ b/cloud/services/network/service.go @@ -10,7 +10,7 @@ You may obtain a copy of the License at package network -import iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" +import "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" // Network describes an existing STACKIT virtual network referenced by the provider. type Network struct { @@ -26,11 +26,7 @@ type NetworkSpec struct { } // ClientScope is an interface that provides the IaaS client and target scope for network operations. -type ClientScope interface { - ProjectID() string - Region() string - IaaSClient() *iaas.APIClient -} +type ClientScope = shared.IaaSClientScope // Scope is an interface that provides scope data needed for network reconciliation. type Scope interface { @@ -40,18 +36,12 @@ type Scope interface { // Client provides direct network operations against the STACKIT IaaS API. type Client struct { - iaasClient *iaas.APIClient - projectID string - region string + shared.IaaSScope } // NewClient returns a Client for the given scope. func NewClient(scope ClientScope) *Client { - return &Client{ - iaasClient: scope.IaaSClient(), - projectID: scope.ProjectID(), - region: scope.Region(), - } + return &Client{IaaSScope: shared.NewIaaSScope(scope)} } // Service implements network reconciler. diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index 540deba..a330066 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -94,7 +94,7 @@ func (c *Client) Get(ctx context.Context, id string) (*PublicIP, error) { if id == "" { return nil, fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) } - publicIP, err := c.iaasClient.DefaultAPI.GetPublicIP(ctx, c.projectID, c.region, id).Execute() + publicIP, err := c.IaaSClient().DefaultAPI.GetPublicIP(ctx, c.ProjectID(), c.Region(), id).Execute() if err != nil { return nil, shared.ClassifySDKError("get public IP", err) } @@ -107,13 +107,7 @@ func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*Publi if err != nil { return nil, err } - if len(matched) == 0 { - return nil, fmt.Errorf("%w: no public IP matching tags", shared.ErrNotFound) - } - if len(matched) > 1 { - return nil, fmt.Errorf("%w: multiple public IPs matching tags", shared.ErrConflict) - } - return matched[0], nil + return shared.FindSingle(matched, "public IP", "public IPs") } // ListByTags lists STACKIT public IPs matching the given tag selector. @@ -121,7 +115,7 @@ func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*Pub if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } - resp, err := c.iaasClient.DefaultAPI.ListPublicIPs(ctx, c.projectID, c.region). + resp, err := c.IaaSClient().DefaultAPI.ListPublicIPs(ctx, c.ProjectID(), c.Region()). LabelSelector(shared.LabelSelector(tags)). Execute() if err != nil { @@ -142,8 +136,8 @@ func (c *Client) AttachToServer(ctx context.Context, serverID, publicIPID string if serverID == "" || publicIPID == "" { return fmt.Errorf("%w: server ID and public IP ID are required", shared.ErrInvalidInput) } - if err := c.iaasClient.DefaultAPI. - AddPublicIpToServer(ctx, c.projectID, c.region, serverID, publicIPID). + if err := c.IaaSClient().DefaultAPI. + AddPublicIpToServer(ctx, c.ProjectID(), c.Region(), serverID, publicIPID). Execute(); err != nil { err := shared.ClassifySDKError("add public IP to server", err) if !shared.IsConflict(err) { @@ -159,8 +153,8 @@ func (c *Client) DetachFromServer(ctx context.Context, serverID, publicIPID stri if serverID == "" || publicIPID == "" { return fmt.Errorf("%w: server ID and public IP ID are required", shared.ErrInvalidInput) } - if err := c.iaasClient.DefaultAPI. - RemovePublicIpFromServer(ctx, c.projectID, c.region, serverID, publicIPID). + if err := c.IaaSClient().DefaultAPI. + RemovePublicIpFromServer(ctx, c.ProjectID(), c.Region(), serverID, publicIPID). Execute(); err != nil { err := shared.ClassifySDKError("remove public IP from server", err) if !shared.IsNotFound(err) && !shared.IsConflict(err) { @@ -176,7 +170,7 @@ func (c *Client) DeleteByID(ctx context.Context, id string) error { if id == "" { return fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) } - if err := c.iaasClient.DefaultAPI.DeletePublicIP(ctx, c.projectID, c.region, id).Execute(); err != nil { + if err := c.IaaSClient().DefaultAPI.DeletePublicIP(ctx, c.ProjectID(), c.Region(), id).Execute(); err != nil { err := shared.ClassifySDKError("delete public IP", err) if !shared.IsNotFound(err) { return err @@ -198,8 +192,8 @@ func (c *Client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*Public if len(spec.Tags) > 0 { payload.SetLabels(shared.TagsToSDKLabels(spec.Tags)) } - created, err := c.iaasClient.DefaultAPI. - CreatePublicIP(ctx, c.projectID, c.region). + created, err := c.IaaSClient().DefaultAPI. + CreatePublicIP(ctx, c.ProjectID(), c.Region()). CreatePublicIPPayload(*payload). Execute() if err != nil { @@ -233,14 +227,14 @@ func (c *Client) resolvePublicIP(ctx context.Context, spec PublicIPSpec) (*Publi return publicIP, nil } -func (s *Service) resolveServerID(ctx context.Context, spec PublicIPSpec) (string, error) { +func (c *Client) resolveServerID(ctx context.Context, spec PublicIPSpec) (string, error) { if spec.ServerID != "" { return spec.ServerID, nil } if len(spec.ServerSelector) == 0 { return "", nil } - srv, err := server.NewClient(s.scope).FindByTags(ctx, spec.ServerSelector) + srv, err := server.NewClient(c).FindByTags(ctx, spec.ServerSelector) if err != nil { if shared.IsNotFound(err) { return "", nil diff --git a/cloud/services/publicip/service.go b/cloud/services/publicip/service.go index e167da2..f420848 100644 --- a/cloud/services/publicip/service.go +++ b/cloud/services/publicip/service.go @@ -10,7 +10,7 @@ You may obtain a copy of the License at package publicip -import iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" +import "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" // PublicIP describes a STACKIT public IP resource. type PublicIP struct { @@ -29,11 +29,7 @@ type PublicIPSpec struct { } // ClientScope is an interface that provides the IaaS client and target scope for public IP operations. -type ClientScope interface { - ProjectID() string - Region() string - IaaSClient() *iaas.APIClient -} +type ClientScope = shared.IaaSClientScope // Scope is an interface that provides scope data needed for public IP reconciliation. type Scope interface { @@ -43,18 +39,12 @@ type Scope interface { // Client provides direct public IP operations against the STACKIT IaaS API. type Client struct { - iaasClient *iaas.APIClient - projectID string - region string + shared.IaaSScope } // NewClient returns a Client for the given scope. func NewClient(scope ClientScope) *Client { - return &Client{ - iaasClient: scope.IaaSClient(), - projectID: scope.ProjectID(), - region: scope.Region(), - } + return &Client{IaaSScope: shared.NewIaaSScope(scope)} } // Service implements public IPs reconciler. diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index 75dbdc5..ddcdee9 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -18,6 +18,7 @@ import ( iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" logf "sigs.k8s.io/controller-runtime/pkg/log" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) @@ -49,13 +50,7 @@ func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*Secur if err != nil { return nil, err } - if len(matched) == 0 { - return nil, fmt.Errorf("%w: no security group matching tags", shared.ErrNotFound) - } - if len(matched) > 1 { - return nil, fmt.Errorf("%w: multiple security groups matching tags", shared.ErrConflict) - } - return matched[0], nil + return shared.FindSingle(matched, "security group", "security groups") } // ListByTags lists STACKIT security groups matching the given tag selector. @@ -63,7 +58,7 @@ func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*Sec if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } - resp, err := c.iaasClient.DefaultAPI.ListSecurityGroups(ctx, c.projectID, c.region). + resp, err := c.IaaSClient().DefaultAPI.ListSecurityGroups(ctx, c.ProjectID(), c.Region()). LabelSelector(shared.LabelSelector(tags)). Execute() if err != nil { @@ -97,8 +92,8 @@ func (c *Client) DeleteByID(ctx context.Context, id string) error { // - invalid input: duplicate item in security_groups list // - not found: server has no network port yet func (c *Client) AttachToServer(ctx context.Context, serverID, securityGroupID string) error { - if err := c.iaasClient.DefaultAPI. - AddSecurityGroupToServer(ctx, c.projectID, c.region, serverID, securityGroupID). + if err := c.IaaSClient().DefaultAPI. + AddSecurityGroupToServer(ctx, c.ProjectID(), c.Region(), serverID, securityGroupID). Execute(); err != nil { err := shared.ClassifySDKError("add security group to server", err) if !shared.IsConflict(err) && !shared.IsInvalidInput(err) && !shared.IsNotFound(err) { @@ -111,8 +106,8 @@ func (c *Client) AttachToServer(ctx context.Context, serverID, securityGroupID s // DetachFromServer detaches a security group from a server, ignoring NotFound and Conflict errors. func (c *Client) DetachFromServer(ctx context.Context, serverID, securityGroupID string) error { - if err := c.iaasClient.DefaultAPI. - RemoveSecurityGroupFromServer(ctx, c.projectID, c.region, serverID, securityGroupID). + if err := c.IaaSClient().DefaultAPI. + RemoveSecurityGroupFromServer(ctx, c.ProjectID(), c.Region(), serverID, securityGroupID). Execute(); err != nil { err := shared.ClassifySDKError("remove security group from server", err) if !shared.IsNotFound(err) && !shared.IsConflict(err) { @@ -213,7 +208,7 @@ func (c *Client) ensureSecurityGroup(ctx context.Context, spec SecurityGroupSpec } else if !shared.IsNotFound(err) { return nil, err } - securityGroup, err := c.iaasClient.DefaultAPI.CreateSecurityGroup(ctx, c.projectID, c.region). + securityGroup, err := c.IaaSClient().DefaultAPI.CreateSecurityGroup(ctx, c.ProjectID(), c.Region()). CreateSecurityGroupPayload(spec.toCreatePayload()). Execute() if err != nil { @@ -235,8 +230,8 @@ func (c *Client) deleteSecurityGroupWithRules( return err } } - if err := c.iaasClient.DefaultAPI. - DeleteSecurityGroup(ctx, c.projectID, c.region, id). + if err := c.IaaSClient().DefaultAPI. + DeleteSecurityGroup(ctx, c.ProjectID(), c.Region(), id). Execute(); err != nil { err := shared.ClassifySDKError("delete security group", err) if !shared.IsNotFound(err) { @@ -273,16 +268,14 @@ func (c *Client) detachServers(ctx context.Context, securityGroupID string, serv } func (c *Client) listServerIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { - resp, err := c.iaasClient.DefaultAPI.ListServers(ctx, c.projectID, c.region). - LabelSelector(shared.LabelSelector(tags)). - Execute() + servers, err := server.NewClient(c).ListByTags(ctx, tags) if err != nil { - return nil, shared.ClassifySDKError("list servers", err) + return nil, err } var serverIDs []string - for _, server := range resp.GetItems() { - if shared.LabelsContainTags(server.GetLabels(), tags) && server.GetId() != "" { - serverIDs = append(serverIDs, server.GetId()) + for _, srv := range servers { + if srv.ID != "" { + serverIDs = append(serverIDs, srv.ID) } } return serverIDs, nil diff --git a/cloud/services/securitygroup/reconcile_test.go b/cloud/services/securitygroup/reconcile_test.go index 3c44ad0..73fe0a7 100644 --- a/cloud/services/securitygroup/reconcile_test.go +++ b/cloud/services/securitygroup/reconcile_test.go @@ -662,6 +662,8 @@ func TestServiceDeleteStaleIDFallbackAndDetach(t *testing.T) { "labels": map[string]any{"cluster": "test"}, }, }}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers/"+testServerID+"/nics"): + writeJSON(t, w, map[string]any{"items": []any{}}) case r.Method == http.MethodDelete && strings.Contains(path, "/servers/") && strings.Contains(path, "/security-groups/"): parts := strings.Split(path, "/servers/") sub := strings.Split(parts[1], "/security-groups/") diff --git a/cloud/services/securitygroup/rules.go b/cloud/services/securitygroup/rules.go index 901f585..2fa0b1a 100644 --- a/cloud/services/securitygroup/rules.go +++ b/cloud/services/securitygroup/rules.go @@ -72,7 +72,7 @@ func (c *Client) resolveRule(ctx context.Context, rule Rule) (Rule, error) { } func (c *Client) createSecurityGroupRule(ctx context.Context, securityGroupID string, rule Rule) error { - if _, err := c.iaasClient.DefaultAPI.CreateSecurityGroupRule(ctx, c.projectID, c.region, securityGroupID). + if _, err := c.IaaSClient().DefaultAPI.CreateSecurityGroupRule(ctx, c.ProjectID(), c.Region(), securityGroupID). CreateSecurityGroupRulePayload(rule.toCreatePayload()). Execute(); err != nil { return shared.ClassifySDKError("create security group rule", err) @@ -81,7 +81,7 @@ func (c *Client) createSecurityGroupRule(ctx context.Context, securityGroupID st } func (c *Client) listSecurityGroupRules(ctx context.Context, securityGroupID string) ([]iaas.SecurityGroupRule, error) { - resp, err := c.iaasClient.DefaultAPI.ListSecurityGroupRules(ctx, c.projectID, c.region, securityGroupID).Execute() + resp, err := c.IaaSClient().DefaultAPI.ListSecurityGroupRules(ctx, c.ProjectID(), c.Region(), securityGroupID).Execute() if err != nil { return nil, shared.ClassifySDKError("list security group rules", err) } @@ -92,8 +92,8 @@ func (c *Client) deleteSecurityGroupRule(ctx context.Context, securityGroupID, r if ruleID == "" { return nil } - if err := c.iaasClient.DefaultAPI. - DeleteSecurityGroupRule(ctx, c.projectID, c.region, securityGroupID, ruleID). + if err := c.IaaSClient().DefaultAPI. + DeleteSecurityGroupRule(ctx, c.ProjectID(), c.Region(), securityGroupID, ruleID). Execute(); err != nil { err := shared.ClassifySDKError("delete security group rule", err) if !shared.IsNotFound(err) { diff --git a/cloud/services/securitygroup/service.go b/cloud/services/securitygroup/service.go index 584227a..5d3a128 100644 --- a/cloud/services/securitygroup/service.go +++ b/cloud/services/securitygroup/service.go @@ -10,7 +10,7 @@ You may obtain a copy of the License at package securitygroup -import iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" +import "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" // Direction specifies the traffic direction of a security group rule. type Direction string @@ -59,11 +59,7 @@ type SecurityGroupSpec struct { } // ClientScope is an interface that provides the IaaS client and target scope for security group operations. -type ClientScope interface { - ProjectID() string - Region() string - IaaSClient() *iaas.APIClient -} +type ClientScope = shared.IaaSClientScope // Scope is an interface that provides scope data needed for security group reconciliation. type Scope interface { @@ -73,18 +69,12 @@ type Scope interface { // Client provides direct security group operations against the STACKIT IaaS API. type Client struct { - iaasClient *iaas.APIClient - projectID string - region string + shared.IaaSScope } // NewClient returns a Client for the given scope. func NewClient(scope ClientScope) *Client { - return &Client{ - iaasClient: scope.IaaSClient(), - projectID: scope.ProjectID(), - region: scope.Region(), - } + return &Client{IaaSScope: shared.NewIaaSScope(scope)} } // Service implements security groups reconciler. diff --git a/cloud/services/server/reconcile.go b/cloud/services/server/reconcile.go index 83655a5..90fa4fe 100644 --- a/cloud/services/server/reconcile.go +++ b/cloud/services/server/reconcile.go @@ -64,7 +64,7 @@ func (c *Client) Get(ctx context.Context, id string) (*Server, error) { if id == "" { return nil, fmt.Errorf("%w: server ID is empty", shared.ErrInvalidInput) } - srv, err := c.iaasClient.DefaultAPI.GetServer(ctx, c.projectID, c.region, id).Execute() + srv, err := c.IaaSClient().DefaultAPI.GetServer(ctx, c.ProjectID(), c.Region(), id).Execute() if err != nil { return nil, shared.ClassifySDKError("get server", err) } @@ -77,13 +77,7 @@ func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*Serve if err != nil { return nil, err } - if len(matched) == 0 { - return nil, fmt.Errorf("%w: no server matching tags", shared.ErrNotFound) - } - if len(matched) > 1 { - return nil, fmt.Errorf("%w: multiple servers matching tags", shared.ErrConflict) - } - return matched[0], nil + return shared.FindSingle(matched, "server", "servers") } // ListByTags lists STACKIT servers matching the given tag selector. @@ -91,7 +85,7 @@ func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*Ser if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } - resp, err := c.iaasClient.DefaultAPI.ListServers(ctx, c.projectID, c.region). + resp, err := c.IaaSClient().DefaultAPI.ListServers(ctx, c.ProjectID(), c.Region()). LabelSelector(shared.LabelSelector(tags)). Details(true). Execute() @@ -113,7 +107,7 @@ func (c *Client) DeleteByID(ctx context.Context, id string) error { if id == "" { return fmt.Errorf("%w: server ID is empty", shared.ErrInvalidInput) } - if err := c.iaasClient.DefaultAPI.DeleteServer(ctx, c.projectID, c.region, id).Execute(); err != nil { + if err := c.IaaSClient().DefaultAPI.DeleteServer(ctx, c.ProjectID(), c.Region(), id).Execute(); err != nil { err := shared.ClassifySDKError("delete server", err) if !shared.IsNotFound(err) { return err @@ -149,7 +143,7 @@ func (c *Client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bo } payload := spec.toCreatePayload() - created, err := c.iaasClient.DefaultAPI.CreateServer(ctx, c.projectID, c.region). + created, err := c.IaaSClient().DefaultAPI.CreateServer(ctx, c.ProjectID(), c.Region()). CreateServerPayload(payload). Execute() if err != nil { @@ -162,7 +156,7 @@ func (c *Client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bo func (c *Client) deleteBySpec(ctx context.Context, spec ServerSpec) (string, error) { if spec.ID != "" { - err := c.iaasClient.DefaultAPI.DeleteServer(ctx, c.projectID, c.region, spec.ID).Execute() + err := c.IaaSClient().DefaultAPI.DeleteServer(ctx, c.ProjectID(), c.Region(), spec.ID).Execute() if err == nil { return spec.ID, nil } @@ -263,7 +257,7 @@ func (c *Client) serverFromSDK(ctx context.Context, srv *iaas.Server) *Server { PowerStatus: srv.GetPowerStatus(), AvailabilityZone: srv.GetAvailabilityZone(), } - nics, err := c.iaasClient.DefaultAPI.ListServerNICs(ctx, c.projectID, c.region, out.ID).Execute() + nics, err := c.IaaSClient().DefaultAPI.ListServerNICs(ctx, c.ProjectID(), c.Region(), out.ID).Execute() if err == nil { for _, nic := range nics.GetItems() { if ipv4 := nic.GetIpv4(); ipv4 != "" { diff --git a/cloud/services/server/service.go b/cloud/services/server/service.go index c9821b1..c62f7fd 100644 --- a/cloud/services/server/service.go +++ b/cloud/services/server/service.go @@ -13,7 +13,7 @@ package server import ( "errors" - iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) // ErrServerGone indicates that an already-provisioned server is missing in @@ -68,11 +68,7 @@ type ServerSpec struct { } // ClientScope is an interface that provides the IaaS client and target scope for server operations. -type ClientScope interface { - ProjectID() string - Region() string - IaaSClient() *iaas.APIClient -} +type ClientScope = shared.IaaSClientScope // Scope is an interface that provides scope data needed for server reconciliation. type Scope interface { @@ -82,18 +78,12 @@ type Scope interface { // Client provides direct server operations against the STACKIT IaaS API. type Client struct { - iaasClient *iaas.APIClient - projectID string - region string + shared.IaaSScope } // NewClient returns a Client for the given scope. func NewClient(scope ClientScope) *Client { - return &Client{ - iaasClient: scope.IaaSClient(), - projectID: scope.ProjectID(), - region: scope.Region(), - } + return &Client{IaaSScope: shared.NewIaaSScope(scope)} } // Service implements server reconciler. diff --git a/cloud/services/shared/errors_test.go b/cloud/services/shared/errors_test.go index 314eb25..71ba7f4 100644 --- a/cloud/services/shared/errors_test.go +++ b/cloud/services/shared/errors_test.go @@ -54,3 +54,29 @@ func TestIsRetryable(t *testing.T) { }) } } + +func TestFindSingle(t *testing.T) { + t.Run("returns ErrNotFound when empty", func(t *testing.T) { + _, err := FindSingle([]string{}, "server", "servers") + if !IsNotFound(err) { + t.Fatalf("FindSingle() error = %v, want ErrNotFound", err) + } + }) + + t.Run("returns ErrConflict when multiple", func(t *testing.T) { + _, err := FindSingle([]string{"a", "b"}, "server", "servers") + if !IsConflict(err) { + t.Fatalf("FindSingle() error = %v, want ErrConflict", err) + } + }) + + t.Run("returns single element", func(t *testing.T) { + got, err := FindSingle([]string{"a"}, "server", "servers") + if err != nil { + t.Fatalf("FindSingle() error = %v", err) + } + if got != "a" { + t.Fatalf("FindSingle() = %q, want %q", got, "a") + } + }) +} diff --git a/cloud/services/shared/utils.go b/cloud/services/shared/utils.go index cf555eb..d2547e9 100644 --- a/cloud/services/shared/utils.go +++ b/cloud/services/shared/utils.go @@ -17,8 +17,56 @@ import ( "strings" "github.com/stackitcloud/stackit-sdk-go/core/oapierror" + iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" ) +// IaaSClientScope is an interface that provides the IaaS client and target scope for IaaS operations. +type IaaSClientScope interface { + ProjectID() string + Region() string + IaaSClient() *iaas.APIClient +} + +// IaaSScope holds the IaaS API client and target project/region for IaaS service operations. +type IaaSScope struct { + iaasClient *iaas.APIClient + projectID string + region string +} + +// NewIaaSScope constructs an IaaSScope from the given scope. +func NewIaaSScope(scope IaaSClientScope) IaaSScope { + return IaaSScope{ + iaasClient: scope.IaaSClient(), + projectID: scope.ProjectID(), + region: scope.Region(), + } +} + +func (c IaaSScope) ProjectID() string { + return c.projectID +} + +func (c IaaSScope) Region() string { + return c.region +} + +func (c IaaSScope) IaaSClient() *iaas.APIClient { + return c.iaasClient +} + +// FindSingle returns the single item in matched, or ErrNotFound if empty, or ErrConflict if more than one. +func FindSingle[T any](matched []T, singular, plural string) (T, error) { + var zero T + if len(matched) == 0 { + return zero, fmt.Errorf("%w: no %s matching tags", ErrNotFound, singular) + } + if len(matched) > 1 { + return zero, fmt.Errorf("%w: multiple %s matching tags", ErrConflict, plural) + } + return matched[0], nil +} + func MapContains(haystack, needle map[string]string) bool { for k, v := range needle { if haystack[k] != v { diff --git a/cloud/types.go b/cloud/types.go deleted file mode 100644 index ac2d95a..0000000 --- a/cloud/types.go +++ /dev/null @@ -1,40 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -// Package cloud defines the abstraction layer between the controllers and the -// STACKIT SDK. Controllers must not reach into the SDK directly; all calls go -// through the Client interface in this package. -package cloud - -import ( - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" -) - -// Server describes a STACKIT compute instance in provider-neutral terms. -type Server = server.Server - -// Address is an IP or DNS endpoint of a Server. -type Address = server.Address - -// Network is an existing STACKIT virtual network referenced by the provider. -type Network = network.Network - -// LoadBalancer describes an API-server load balancer. -type LoadBalancer = loadbalancer.LoadBalancer - -// PublicIP describes a STACKIT public IP resource. -type PublicIP = publicip.PublicIP - -// SecurityGroup describes a STACKIT security group. -type SecurityGroup = securitygroup.SecurityGroup diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index 23e50b7..1cf3faa 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -27,10 +27,10 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/scope" ) @@ -158,20 +158,20 @@ func (r *StackitClusterReconciler) reconcileBastion( func validateBastionSpec(spec infrav1.StackitBastionSpec) error { if spec.ImageID == "" { - return fmt.Errorf("%w: bastion.imageID is required", cloud.ErrInvalidInput) + return fmt.Errorf("%w: bastion.imageID is required", shared.ErrInvalidInput) } if spec.MachineType == "" { - return fmt.Errorf("%w: bastion.machineType is required", cloud.ErrInvalidInput) + return fmt.Errorf("%w: bastion.machineType is required", shared.ErrInvalidInput) } if spec.SSHKeyName == "" { - return fmt.Errorf("%w: bastion.sshKeyName is required", cloud.ErrInvalidInput) + return fmt.Errorf("%w: bastion.sshKeyName is required", shared.ErrInvalidInput) } if len(spec.AllowedCIDRs) == 0 { - return fmt.Errorf("%w: bastion.allowedCIDRs is required", cloud.ErrInvalidInput) + return fmt.Errorf("%w: bastion.allowedCIDRs is required", shared.ErrInvalidInput) } for _, cidr := range spec.AllowedCIDRs { if _, err := netip.ParsePrefix(cidr); err != nil { - return fmt.Errorf("%w: bastion.allowedCIDRs contains invalid CIDR %q", cloud.ErrInvalidInput, cidr) + return fmt.Errorf("%w: bastion.allowedCIDRs contains invalid CIDR %q", shared.ErrInvalidInput, cidr) } } return nil @@ -187,17 +187,17 @@ func (r *StackitClusterReconciler) deleteBastionResources( ignoreNotFound bool, ) error { if err := publicip.New(clusterScope).Delete(ctx); err != nil { - if !ignoreNotFound || !cloud.IsNotFound(err) { + if !ignoreNotFound || !shared.IsNotFound(err) { return err } } if err := securitygroup.New(clusterScope).Delete(ctx); err != nil { - if !ignoreNotFound || !cloud.IsNotFound(err) { + if !ignoreNotFound || !shared.IsNotFound(err) { return err } } if err := server.New(clusterScope).Delete(ctx); err != nil { - if !ignoreNotFound || !cloud.IsNotFound(err) { + if !ignoreNotFound || !shared.IsNotFound(err) { return err } } diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index 6050f81..abaaff5 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -28,8 +28,10 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -54,7 +56,7 @@ var _ = Describe("StackitCluster Controller", func() { credentials = "stackit-credentials-" + clusterName stackitKey = types.NamespacedName{Namespace: namespace, Name: clusterName} request = reconcile.Request{NamespacedName: stackitKey} - fakeCloud = cloudfake.New(cloud.Network{ID: testNetworkID, Name: "network"}) + fakeCloud = cloudfake.New(network.Network{ID: testNetworkID, Name: "network"}) reconciler = &StackitClusterReconciler{ Client: k8sClient, Scheme: k8sClient.Scheme(), @@ -520,7 +522,7 @@ var _ = Describe("StackitCluster Controller", func() { }) It("marks conditions not ready when API server load balancer creation fails", func() { - fakeCloud.FailNextEnsureLB = fmt.Errorf("load balancer creation rejected: %w", cloud.ErrInvalidInput) + fakeCloud.FailNextEnsureLB = fmt.Errorf("load balancer creation rejected: %w", shared.ErrInvalidInput) result, err := reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) @@ -538,7 +540,7 @@ var _ = Describe("StackitCluster Controller", func() { got.Spec.Bastion = validBastionSpec() Expect(k8sClient.Update(ctx, got)).To(Succeed()) - fakeCloud.FailNextCreateServer = fmt.Errorf("bastion creation rejected: %w", cloud.ErrInvalidInput) + fakeCloud.FailNextCreateServer = fmt.Errorf("bastion creation rejected: %w", shared.ErrInvalidInput) result, err := reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) @@ -550,7 +552,7 @@ var _ = Describe("StackitCluster Controller", func() { }) It("requeues when network lookup returns a transient error", func() { - fakeCloud.FailNextGetNetwork = fmt.Errorf("temporary network lookup failure: %w", cloud.ErrTransient) + fakeCloud.FailNextGetNetwork = fmt.Errorf("temporary network lookup failure: %w", shared.ErrTransient) result, err := reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) @@ -566,7 +568,7 @@ var _ = Describe("StackitCluster Controller", func() { // enqueues the cluster once the credentials are rejected. It("requeues on unauthorized credentials and recovers once they are corrected", func() { reconciler.CloudClientFactory = func(context.Context, cloud.Credentials) (cloud.Client, error) { - return nil, fmt.Errorf("authenticate: %w", cloud.ErrUnauthorized) + return nil, fmt.Errorf("authenticate: %w", shared.ErrUnauthorized) } result, err := reconciler.Reconcile(ctx, request) @@ -669,7 +671,7 @@ var _ = Describe("StackitCluster Controller", func() { Expect(k8sClient.Delete(ctx, got)).To(Succeed()) - fakeCloud.FailNextDeleteLB = fmt.Errorf("delete load balancer timeout: %w", cloud.ErrTransient) + fakeCloud.FailNextDeleteLB = fmt.Errorf("delete load balancer timeout: %w", shared.ErrTransient) _, err = reconciler.Reconcile(ctx, request) Expect(err).To(HaveOccurred()) @@ -760,7 +762,7 @@ var _ = Describe("StackitCluster Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(got.Status.APIServerLoadBalancerID).NotTo(BeEmpty()) Expect(k8sClient.Delete(ctx, got)).To(Succeed()) - fakeCloud.FailNextDeleteLB = fmt.Errorf("delete load balancer: %w", cloud.ErrTransient) + fakeCloud.FailNextDeleteLB = fmt.Errorf("delete load balancer: %w", shared.ErrTransient) _, err = reconciler.Reconcile(ctx, request) Expect(err).To(HaveOccurred()) @@ -877,7 +879,7 @@ var _ = Describe("StackitCluster Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(got.Status.Ready).To(BeTrue()) - fakeCloud.FailNextSetTargets = fmt.Errorf("update target pool timeout: %w", cloud.ErrTransient) + fakeCloud.FailNextSetTargets = fmt.Errorf("update target pool timeout: %w", shared.ErrTransient) result, err := reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) Expect(result.RequeueAfter).To(BeNumerically(">", 0)) @@ -893,7 +895,7 @@ var _ = Describe("StackitCluster Controller", func() { It("requeues when the first target pool update fails permanently", func() { // Without the requeue nothing would drive another reconcile. createControlPlaneMachine(ctx, "cp-0-"+clusterName, clusterName, "10.0.0.11") - fakeCloud.FailNextSetTargets = fmt.Errorf("target pool rejected: %w", cloud.ErrInvalidInput) + fakeCloud.FailNextSetTargets = fmt.Errorf("target pool rejected: %w", shared.ErrInvalidInput) result, err := reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) @@ -1027,7 +1029,7 @@ var _ = Describe("StackitCluster Controller", func() { }) It("reports an error condition when listing availability zones fails", func() { - fakeCloud.FailNextListAZ = fmt.Errorf("cloud failure: %w", cloud.ErrTransient) + fakeCloud.FailNextListAZ = fmt.Errorf("cloud failure: %w", shared.ErrTransient) result, err := reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index 19bd7a0..a032dc2 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -26,13 +26,13 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/availabilityzone" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -160,7 +160,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS ) } if loadbalancer.IsTargetPoolUpdateError(err) { - if !cloud.IsRetryable(err) { + if !shared.IsRetryable(err) { record.Warnf( stackitCluster, "LoadBalancerTargetError", "Cannot update API server load balancer target pool: %v", err, @@ -326,7 +326,7 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS } clusterScope.SetCloudClient(cloudClient) loadBalancerID := stackitCluster.Status.APIServerLoadBalancerID - if err := loadbalancer.New(clusterScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { + if err := loadbalancer.New(clusterScope).Delete(ctx); err != nil && !shared.IsNotFound(err) { log.Error(err, "Failed to delete API server load balancer", "cluster", stackitCluster.Name, "loadBalancerID", loadBalancerID) record.Warnf(stackitCluster, "FailedDeleteLoadBalancer", "Failed to delete API server load balancer %s: %v", loadBalancerID, err) @@ -339,19 +339,19 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS ) } hadBastion := hasBastionStatus(stackitCluster.Status.Bastion) - if err := publicip.New(clusterScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { + if err := publicip.New(clusterScope).Delete(ctx); err != nil && !shared.IsNotFound(err) { log.Error(err, "Failed to delete bastion public IP", "cluster", stackitCluster.Name) record.Warnf(stackitCluster, "FailedDeleteBastion", "Failed to delete bastion public IP: %v", err) return ctrl.Result{}, err } - if err := securitygroup.New(clusterScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { + if err := securitygroup.New(clusterScope).Delete(ctx); err != nil && !shared.IsNotFound(err) { log.Error(err, "Failed to delete bastion security groups", "cluster", stackitCluster.Name) record.Warnf(stackitCluster, "FailedDeleteBastion", "Failed to delete bastion security groups: %v", err) return ctrl.Result{}, err } - if err := server.New(clusterScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { + if err := server.New(clusterScope).Delete(ctx); err != nil && !shared.IsNotFound(err) { log.Error(err, "Failed to delete bastion", "cluster", stackitCluster.Name) record.Warnf(stackitCluster, "FailedDeleteBastion", "Failed to delete bastion: %v", err) diff --git a/controller/stackitmachine_controller_test.go b/controller/stackitmachine_controller_test.go index 85b1b39..e940689 100644 --- a/controller/stackitmachine_controller_test.go +++ b/controller/stackitmachine_controller_test.go @@ -28,7 +28,9 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -59,7 +61,7 @@ var _ = Describe("StackitMachine Controller", func() { bootstrapName = fmt.Sprintf("bootstrap-%d", suffix) stackitKey = types.NamespacedName{Namespace: namespace, Name: stackitName} request = reconcile.Request{NamespacedName: stackitKey} - fakeCloud = cloudfake.New(cloud.Network{ID: testNetworkID, Name: "network"}) + fakeCloud = cloudfake.New(network.Network{ID: testNetworkID, Name: "network"}) reconciler = &StackitMachineReconciler{ Client: k8sClient, Scheme: k8sClient.Scheme(), @@ -270,7 +272,7 @@ var _ = Describe("StackitMachine Controller", func() { stackitCluster.Status.Bastion.SecurityGroupID = "bastion-sg" Expect(k8sClient.Status().Update(ctx, stackitCluster)).To(Succeed()) - fakeCloud.FailNextAddSecurityGroup = fmt.Errorf("node ssh access timeout: %w", cloud.ErrTransient) + fakeCloud.FailNextAddSecurityGroup = fmt.Errorf("node ssh access timeout: %w", shared.ErrTransient) result, err := reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) @@ -364,7 +366,7 @@ var _ = Describe("StackitMachine Controller", func() { It("requeues on unauthorized credentials and recovers once they are corrected", func() { reconciler.CloudClientFactory = func(context.Context, cloud.Credentials) (cloud.Client, error) { - return nil, fmt.Errorf("authenticate: %w", cloud.ErrUnauthorized) + return nil, fmt.Errorf("authenticate: %w", shared.ErrUnauthorized) } result, err := reconciler.Reconcile(ctx, request) @@ -437,7 +439,7 @@ var _ = Describe("StackitMachine Controller", func() { }) It("requeues when server lookup returns a conflict", func() { - fakeCloud.FailNextFindServer = fmt.Errorf("multiple matching servers: %w", cloud.ErrConflict) + fakeCloud.FailNextFindServer = fmt.Errorf("multiple matching servers: %w", shared.ErrConflict) result, err := reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) @@ -451,7 +453,7 @@ var _ = Describe("StackitMachine Controller", func() { }) It("requeues when VM creation returns a transient error", func() { - fakeCloud.FailNextCreateServer = fmt.Errorf("create server timeout: %w", cloud.ErrTransient) + fakeCloud.FailNextCreateServer = fmt.Errorf("create server timeout: %w", shared.ErrTransient) result, err := reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) @@ -465,7 +467,7 @@ var _ = Describe("StackitMachine Controller", func() { }) It("returns an error when VM creation is rejected", func() { - fakeCloud.FailNextCreateServer = fmt.Errorf("create server rejected: %w", cloud.ErrInvalidInput) + fakeCloud.FailNextCreateServer = fmt.Errorf("create server rejected: %w", shared.ErrInvalidInput) result, err := reconciler.Reconcile(ctx, request) Expect(err).To(HaveOccurred()) @@ -505,7 +507,7 @@ var _ = Describe("StackitMachine Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(k8sClient.Delete(ctx, got)).To(Succeed()) - fakeCloud.FailNextDeleteServer = fmt.Errorf("delete server timeout: %w", cloud.ErrTransient) + fakeCloud.FailNextDeleteServer = fmt.Errorf("delete server timeout: %w", shared.ErrTransient) _, err = reconciler.Reconcile(ctx, request) Expect(err).To(HaveOccurred()) @@ -555,7 +557,7 @@ var _ = Describe("StackitMachine Controller", func() { By("deleting the object while the cloud reports the server as gone") // The fake deletes an unknown ID without an error, so the not-found answer // of the cloud must be injected. - fakeCloud.FailNextDeleteServer = fmt.Errorf("delete server: %w", cloud.ErrNotFound) + fakeCloud.FailNextDeleteServer = fmt.Errorf("delete server: %w", shared.ErrNotFound) Expect(k8sClient.Delete(ctx, degraded)).To(Succeed()) @@ -610,12 +612,12 @@ var _ = Describe("StackitMachine Controller", func() { got := &infrav1.StackitMachine{} Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) - fakeCloud.FailNextDeleteServer = fmt.Errorf("delete server: %w", cloud.ErrTransient) + fakeCloud.FailNextDeleteServer = fmt.Errorf("delete server: %w", shared.ErrTransient) Expect(k8sClient.Delete(ctx, got)).To(Succeed()) _, err = reconciler.Reconcile(ctx, request) - Expect(err).To(MatchError(cloud.ErrTransient)) + Expect(err).To(MatchError(shared.ErrTransient)) Expect(fakeCloud.ServerCount()).To(Equal(1)) stillThere := &infrav1.StackitMachine{} Expect(k8sClient.Get(ctx, stackitKey, stillThere)).To(Succeed()) diff --git a/controller/stackitmachine_infrastructure.go b/controller/stackitmachine_infrastructure.go index 229a9d9..eb717d1 100644 --- a/controller/stackitmachine_infrastructure.go +++ b/controller/stackitmachine_infrastructure.go @@ -28,9 +28,9 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -240,7 +240,7 @@ func (r *StackitMachineReconciler) reconcileDelete(ctx context.Context, machineS machineScope.SetCloudClient(cloudClient) instanceID := stackitMachine.Status.InstanceID - if err := server.New(machineScope).Delete(ctx); err != nil && !cloud.IsNotFound(err) { + if err := server.New(machineScope).Delete(ctx); err != nil && !shared.IsNotFound(err) { log.Error(err, "Failed to delete server", "machine", stackitMachine.Name, "instanceID", instanceID) record.Warnf(stackitMachine, "FailedDeleteInstance", "Failed to delete server %s: %v", instanceID, err) @@ -282,10 +282,10 @@ func (r *StackitMachineReconciler) reconcileBastionNodeSSHAccess( return nil } if machineScope.StackitCluster.Status.Bastion.SecurityGroupID == "" { - return fmt.Errorf("%w: bastion security group ID is empty", cloud.ErrTransient) + return fmt.Errorf("%w: bastion security group ID is empty", shared.ErrTransient) } if machineScope.StackitMachine.Status.InstanceID == "" { - return fmt.Errorf("%w: server ID is empty", cloud.ErrTransient) + return fmt.Errorf("%w: server ID is empty", shared.ErrTransient) } return securitygroup.New(machineScope).Reconcile(ctx) } diff --git a/scope/machine_scope.go b/scope/machine_scope.go index 95fce63..b2a50c6 100644 --- a/scope/machine_scope.go +++ b/scope/machine_scope.go @@ -101,7 +101,7 @@ func (s *MachineScope) Tags() map[string]string { ) } -func (s *MachineScope) SetInstance(srv *cloud.Server) string { +func (s *MachineScope) SetInstance(srv *server.Server) string { providerID := cloud.NewProviderID(s.StackitCluster.Spec.ProjectID, s.StackitCluster.Spec.Region, srv.ID) s.StackitMachine.Status.InstanceID = srv.ID s.StackitMachine.Status.InstanceState = srv.State diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index 625b770..01ed836 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -38,6 +38,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/test/utils" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -439,7 +440,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying the VM was deleted from STACKIT") Eventually(func(g Gomega) { _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) + g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) By("verifying no tagged STACKIT resources remain for the e2e test ID") @@ -836,7 +837,7 @@ var _ = Describe("Manager", Ordered, func() { instanceID := instanceID Eventually(func(g Gomega) { _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) + g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -850,7 +851,7 @@ var _ = Describe("Manager", Ordered, func() { instanceID := instanceID Eventually(func(g Gomega) { _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) + g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -1006,7 +1007,7 @@ var _ = Describe("Manager", Ordered, func() { instanceID := instanceID Eventually(func(g Gomega) { _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) + g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -1114,7 +1115,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying the old worker VM was deleted from STACKIT") Eventually(func(g Gomega) { _, err := server.NewClient(cloudClient).Get(ctx, initialInstanceIDs[0]) - g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected old server %s to be deleted, got %v", initialInstanceIDs[0], err) + g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected old server %s to be deleted, got %v", initialInstanceIDs[0], err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) By("deleting the upgraded workload Cluster") @@ -1130,7 +1131,7 @@ var _ = Describe("Manager", Ordered, func() { instanceID := instanceID Eventually(func(g Gomega) { _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) + g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -1264,7 +1265,7 @@ var _ = Describe("Manager", Ordered, func() { instanceID := instanceID Eventually(func(g Gomega) { _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected old server %s to be deleted, got %v", instanceID, err) + g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected old server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -1389,7 +1390,7 @@ var _ = Describe("Manager", Ordered, func() { instanceID := instanceID Eventually(func(g Gomega) { _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(cloud.IsNotFound(err)).To(BeTrue(), "expected old server %s to be deleted, got %v", instanceID, err) + g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected old server %s to be deleted, got %v", instanceID, err) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -3117,14 +3118,14 @@ func appendUnique(values []string, additions ...string) []string { func cleanupCloudServersByID(ctx context.Context, cloudClient cloud.Client, instanceIDs []string) { for _, instanceID := range instanceIDs { - if err := server.NewClient(cloudClient).DeleteByID(ctx, instanceID); err != nil && !cloud.IsNotFound(err) { + if err := server.NewClient(cloudClient).DeleteByID(ctx, instanceID); err != nil && !shared.IsNotFound(err) { _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API server cleanup warning for %s: %v\n", instanceID, err) continue } deadline := time.Now().Add(10 * time.Minute) for time.Now().Before(deadline) { _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - if cloud.IsNotFound(err) { + if shared.IsNotFound(err) { break } if err != nil { diff --git a/util/conditions.go b/util/conditions.go index d22050e..3a59a06 100644 --- a/util/conditions.go +++ b/util/conditions.go @@ -14,7 +14,7 @@ import ( "errors" "time" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" @@ -93,7 +93,7 @@ func CredentialFailureResult( conditionTypes ...string, ) (ctrl.Result, error) { SetConditions(conditions, generation, metav1.ConditionFalse, "CredentialsInvalid", err.Error(), conditionTypes...) - if cloud.IsUnauthorized(err) || cloud.IsInvalidInput(err) || errors.Is(err, ErrCredentialsInvalid) { + if shared.IsUnauthorized(err) || shared.IsInvalidInput(err) || errors.Is(err, ErrCredentialsInvalid) { return ctrl.Result{RequeueAfter: requeueAfter}, nil } return ctrl.Result{}, err @@ -109,7 +109,7 @@ func CloudFailureResult( conditionTypes ...string, ) (ctrl.Result, error) { SetConditions(conditions, generation, metav1.ConditionFalse, reason, err.Error(), conditionTypes...) - if cloud.IsRetryable(err) { + if shared.IsRetryable(err) { return ctrl.Result{RequeueAfter: requeueAfter}, nil } if returnError { From 4c073142a5ba86207898d86fe707667da2469165 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Thu, 12 Nov 2026 17:08:57 +0100 Subject: [PATCH 12/60] refactor: deduplicate bastion teardown and server ID lookup by tags --- cloud/sdk_client_integration_test.go | 3 +- cloud/services/securitygroup/reconcile.go | 16 +------ .../services/securitygroup/reconcile_test.go | 2 - cloud/services/server/reconcile.go | 42 +++++++++++++++---- cloud/services/server/reconcile_test.go | 24 +++++++++++ controller/stackitcluster_bastion.go | 20 +++------ controller/stackitcluster_infrastructure.go | 19 +-------- controller/stackitmachine_infrastructure.go | 2 +- test/e2e/e2e_test.go | 2 +- 9 files changed, 70 insertions(+), 60 deletions(-) diff --git a/cloud/sdk_client_integration_test.go b/cloud/sdk_client_integration_test.go index 5933d27..82fba4c 100644 --- a/cloud/sdk_client_integration_test.go +++ b/cloud/sdk_client_integration_test.go @@ -91,7 +91,7 @@ func TestSDKClientLoadBalancerTargetIntegration(t *testing.T) { }) lbClient := loadbalancer.NewClient(client) t.Cleanup(func() { - if err := lbClient.DeleteByID(context.Background(), lbID); err != nil && !shared.IsNotFound(err) { + if err := lbClient.DeleteByID(context.Background(), lbID); err != nil { t.Logf("DeleteByID() cleanup error = %v", err) } }) @@ -218,7 +218,6 @@ func createIntegrationLoadBalancer(t *testing.T, client Client, networkID string } t.Cleanup(func() { if err := loadbalancer.NewClient(client).DeleteByID(context.Background(), lbID); err != nil && - !shared.IsNotFound(err) && !errors.Is(err, shared.ErrInvalidInput) { t.Logf("DeleteByID() cleanup error = %v", err) } diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index ddcdee9..f13c1cd 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -248,7 +248,7 @@ func (c *Client) detachSecurityGroupFromServers(ctx context.Context, securityGro if len(spec.ServerSelector) == 0 { return nil } - serverIDs, err := c.listServerIDsByTags(ctx, spec.ServerSelector) + serverIDs, err := server.NewClient(c).ListIDsByTags(ctx, spec.ServerSelector) if err != nil { return err } @@ -267,20 +267,6 @@ func (c *Client) detachServers(ctx context.Context, securityGroupID string, serv return nil } -func (c *Client) listServerIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { - servers, err := server.NewClient(c).ListByTags(ctx, tags) - if err != nil { - return nil, err - } - var serverIDs []string - for _, srv := range servers { - if srv.ID != "" { - serverIDs = append(serverIDs, srv.ID) - } - } - return serverIDs, nil -} - func (s SecurityGroupSpec) validate() error { if s.Name == "" { return fmt.Errorf("%w: security group name is required", shared.ErrInvalidInput) diff --git a/cloud/services/securitygroup/reconcile_test.go b/cloud/services/securitygroup/reconcile_test.go index 73fe0a7..3c44ad0 100644 --- a/cloud/services/securitygroup/reconcile_test.go +++ b/cloud/services/securitygroup/reconcile_test.go @@ -662,8 +662,6 @@ func TestServiceDeleteStaleIDFallbackAndDetach(t *testing.T) { "labels": map[string]any{"cluster": "test"}, }, }}) - case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers/"+testServerID+"/nics"): - writeJSON(t, w, map[string]any{"items": []any{}}) case r.Method == http.MethodDelete && strings.Contains(path, "/servers/") && strings.Contains(path, "/security-groups/"): parts := strings.Split(path, "/servers/") sub := strings.Split(parts[1], "/security-groups/") diff --git a/cloud/services/server/reconcile.go b/cloud/services/server/reconcile.go index 90fa4fe..fcf8225 100644 --- a/cloud/services/server/reconcile.go +++ b/cloud/services/server/reconcile.go @@ -82,21 +82,49 @@ func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*Serve // ListByTags lists STACKIT servers matching the given tag selector. func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*Server, error) { + items, err := c.listSDKServersByTags(ctx, tags, true) + if err != nil { + return nil, err + } + matched := make([]*Server, 0, len(items)) + for _, srv := range items { + matched = append(matched, c.serverFromSDK(ctx, &srv)) + } + return matched, nil +} + +// ListIDsByTags lists the IDs of STACKIT servers matching the given tag selector. +func (c *Client) ListIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { + items, err := c.listSDKServersByTags(ctx, tags, false) + if err != nil { + return nil, err + } + var ids []string + for _, srv := range items { + if id := srv.GetId(); id != "" { + ids = append(ids, id) + } + } + return ids, nil +} + +func (c *Client) listSDKServersByTags(ctx context.Context, tags map[string]string, details bool) ([]iaas.Server, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } - resp, err := c.IaaSClient().DefaultAPI.ListServers(ctx, c.ProjectID(), c.Region()). - LabelSelector(shared.LabelSelector(tags)). - Details(true). - Execute() + req := c.IaaSClient().DefaultAPI.ListServers(ctx, c.ProjectID(), c.Region()). + LabelSelector(shared.LabelSelector(tags)) + if details { + req = req.Details(true) + } + resp, err := req.Execute() if err != nil { return nil, shared.ClassifySDKError("list servers", err) } - matched := []*Server{} + var matched []iaas.Server for _, srv := range resp.GetItems() { if shared.LabelsContainTags(srv.GetLabels(), tags) { - srv := srv - matched = append(matched, c.serverFromSDK(ctx, &srv)) + matched = append(matched, srv) } } return matched, nil diff --git a/cloud/services/server/reconcile_test.go b/cloud/services/server/reconcile_test.go index ae01d09..509eca9 100644 --- a/cloud/services/server/reconcile_test.go +++ b/cloud/services/server/reconcile_test.go @@ -628,3 +628,27 @@ func TestServiceDelete(t *testing.T) { } }) } + +func TestListIDsByTags(t *testing.T) { + svc := newTestService(t, server.ServerSpec{}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers") { + if got := r.URL.Query().Get("details"); got != "false" { + t.Fatalf("details = %q, want false", got) + } + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{"id": testServerID, "name": "node-0", "machineType": "c2i.2", "labels": map[string]any{"cluster": "test"}}, + map[string]any{"id": testOtherServerID, "name": "node-1", "machineType": "c2i.2", "labels": map[string]any{"cluster": "other"}}, + }}) + return + } + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + ids, err := svc.ListIDsByTags(context.Background(), map[string]string{"cluster": "test"}) + if err != nil { + t.Fatalf("ListIDsByTags() error = %v", err) + } + if len(ids) != 1 || ids[0] != testServerID { + t.Fatalf("ListIDsByTags() = %v, want [%s]", ids, testServerID) + } +} diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index 1cf3faa..a5377dc 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -51,7 +51,7 @@ func (r *StackitClusterReconciler) reconcileBastion( // the status keeps the tag-based cleanup to once per cluster. condition := meta.FindStatusCondition(stackitCluster.Status.Conditions, infrav1.ClusterBastionReadyCondition) if condition == nil || condition.Reason != bastionDisabledReason { - if err := r.deleteBastionResources(ctx, clusterScope, false); err != nil { + if err := r.deleteBastionResources(ctx, clusterScope); err != nil { return ctrl.Result{}, false, err } clusterScope.ClearBastionStatus() @@ -93,7 +93,7 @@ func (r *StackitClusterReconciler) reconcileBastion( clusterScope.SetBastionCloudInit(cloudInit, bastionCloudInitHash(cloudInit)) if bastionNeedsRecreate(stackitCluster, cloudInit) { - if err := r.deleteBastionResources(ctx, clusterScope, true); err != nil { + if err := r.deleteBastionResources(ctx, clusterScope); err != nil { return ctrl.Result{}, false, err } clusterScope.ClearBastionStatus() @@ -184,24 +184,14 @@ func hasBastionStatus(status infrav1.StackitBastionStatus) bool { func (r *StackitClusterReconciler) deleteBastionResources( ctx context.Context, clusterScope *scope.ClusterScope, - ignoreNotFound bool, ) error { if err := publicip.New(clusterScope).Delete(ctx); err != nil { - if !ignoreNotFound || !shared.IsNotFound(err) { - return err - } + return err } if err := securitygroup.New(clusterScope).Delete(ctx); err != nil { - if !ignoreNotFound || !shared.IsNotFound(err) { - return err - } + return err } - if err := server.New(clusterScope).Delete(ctx); err != nil { - if !ignoreNotFound || !shared.IsNotFound(err) { - return err - } - } - return nil + return server.New(clusterScope).Delete(ctx) } func bastionNeedsRecreate(stackitCluster *infrav1.StackitCluster, cloudInit []byte) bool { diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index a032dc2..65f1eaa 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -29,9 +29,6 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/availabilityzone" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" @@ -326,7 +323,7 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS } clusterScope.SetCloudClient(cloudClient) loadBalancerID := stackitCluster.Status.APIServerLoadBalancerID - if err := loadbalancer.New(clusterScope).Delete(ctx); err != nil && !shared.IsNotFound(err) { + if err := loadbalancer.New(clusterScope).Delete(ctx); err != nil { log.Error(err, "Failed to delete API server load balancer", "cluster", stackitCluster.Name, "loadBalancerID", loadBalancerID) record.Warnf(stackitCluster, "FailedDeleteLoadBalancer", "Failed to delete API server load balancer %s: %v", loadBalancerID, err) @@ -339,19 +336,7 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS ) } hadBastion := hasBastionStatus(stackitCluster.Status.Bastion) - if err := publicip.New(clusterScope).Delete(ctx); err != nil && !shared.IsNotFound(err) { - log.Error(err, "Failed to delete bastion public IP", "cluster", stackitCluster.Name) - record.Warnf(stackitCluster, "FailedDeleteBastion", - "Failed to delete bastion public IP: %v", err) - return ctrl.Result{}, err - } - if err := securitygroup.New(clusterScope).Delete(ctx); err != nil && !shared.IsNotFound(err) { - log.Error(err, "Failed to delete bastion security groups", "cluster", stackitCluster.Name) - record.Warnf(stackitCluster, "FailedDeleteBastion", - "Failed to delete bastion security groups: %v", err) - return ctrl.Result{}, err - } - if err := server.New(clusterScope).Delete(ctx); err != nil && !shared.IsNotFound(err) { + if err := r.deleteBastionResources(ctx, clusterScope); err != nil { log.Error(err, "Failed to delete bastion", "cluster", stackitCluster.Name) record.Warnf(stackitCluster, "FailedDeleteBastion", "Failed to delete bastion: %v", err) diff --git a/controller/stackitmachine_infrastructure.go b/controller/stackitmachine_infrastructure.go index eb717d1..1d11b87 100644 --- a/controller/stackitmachine_infrastructure.go +++ b/controller/stackitmachine_infrastructure.go @@ -240,7 +240,7 @@ func (r *StackitMachineReconciler) reconcileDelete(ctx context.Context, machineS machineScope.SetCloudClient(cloudClient) instanceID := stackitMachine.Status.InstanceID - if err := server.New(machineScope).Delete(ctx); err != nil && !shared.IsNotFound(err) { + if err := server.New(machineScope).Delete(ctx); err != nil { log.Error(err, "Failed to delete server", "machine", stackitMachine.Name, "instanceID", instanceID) record.Warnf(stackitMachine, "FailedDeleteInstance", "Failed to delete server %s: %v", instanceID, err) diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index 01ed836..f5686fb 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -3118,7 +3118,7 @@ func appendUnique(values []string, additions ...string) []string { func cleanupCloudServersByID(ctx context.Context, cloudClient cloud.Client, instanceIDs []string) { for _, instanceID := range instanceIDs { - if err := server.NewClient(cloudClient).DeleteByID(ctx, instanceID); err != nil && !shared.IsNotFound(err) { + if err := server.NewClient(cloudClient).DeleteByID(ctx, instanceID); err != nil { _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API server cleanup warning for %s: %v\n", instanceID, err) continue } From cdda1718aea3077eff0bfb7c1856e2de089ef767 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Thu, 12 Nov 2026 17:21:47 +0100 Subject: [PATCH 13/60] refactor: remove unused interfaces, functions, and struct fields --- cloud/client.go | 6 --- cloud/providerid.go | 2 +- cloud/providerid_test.go | 8 ++-- cloud/services/availabilityzone/reconcile.go | 4 -- .../availabilityzone/reconcile_test.go | 10 ----- cloud/services/loadbalancer/service.go | 9 ++-- cloud/services/network/reconcile.go | 4 -- cloud/services/network/reconcile_test.go | 10 ----- cloud/services/server/service.go | 1 - scope/machine_scope.go | 2 +- util/conditions.go | 42 ------------------- 11 files changed, 10 insertions(+), 88 deletions(-) diff --git a/cloud/client.go b/cloud/client.go index 51500b9..3f9ffae 100644 --- a/cloud/client.go +++ b/cloud/client.go @@ -17,12 +17,6 @@ import ( lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" ) -// Reconciler is a generic interface used by components offering a type of service. -type Reconciler interface { - Reconcile(ctx context.Context) error - Delete(ctx context.Context) error -} - // Client is the abstraction over STACKIT cloud APIs used by the controllers. // // Implementations must: diff --git a/cloud/providerid.go b/cloud/providerid.go index 61c7307..2523112 100644 --- a/cloud/providerid.go +++ b/cloud/providerid.go @@ -33,7 +33,7 @@ var ErrInvalidProviderID = errors.New("invalid providerID") // cloud-provider-stackit controller gets project and region from its own // configuration and then resolves Node.spec.providerID via GetServer(projectID, // region, serverID). -func NewProviderID(projectID, region, serverID string) string { +func NewProviderID(serverID string) string { return fmt.Sprintf("%s%s", providerIDScheme, serverID) } diff --git a/cloud/providerid_test.go b/cloud/providerid_test.go index 452305e..7a06942 100644 --- a/cloud/providerid_test.go +++ b/cloud/providerid_test.go @@ -16,7 +16,7 @@ import ( ) func TestNewProviderID(t *testing.T) { - got := NewProviderID("project-id", "eu01", "server-id") + got := NewProviderID("server-id") want := "stackit://server-id" if got != want { t.Fatalf("NewProviderID() = %q, want %q", got, want) @@ -54,9 +54,9 @@ func TestParseProviderIDInvalid(t *testing.T) { } func TestProviderIDRoundTrip(t *testing.T) { - wantProjectID, wantRegion, wantServerID := "project-id", "eu01", "server-id" + wantServerID := "server-id" - gotProjectID, gotRegion, gotServerID, err := ParseProviderID(NewProviderID(wantProjectID, wantRegion, wantServerID)) + gotProjectID, gotRegion, gotServerID, err := ParseProviderID(NewProviderID(wantServerID)) if err != nil { t.Fatalf("ParseProviderID(NewProviderID()) error = %v", err) } @@ -67,7 +67,7 @@ func TestProviderIDRoundTrip(t *testing.T) { func TestProviderIDMatchesCloudProviderStackitFormat(t *testing.T) { serverID := "321a8b81-3660-43e5-bab8-6470b65ee4e8" - providerID := NewProviderID("project-id", "eu01", serverID) + providerID := NewProviderID(serverID) // Verified against the local cloud-provider-stackit repository: // pkg/ccm/instances.go uses fmt.Sprintf("%s://%s", ProviderName, server.GetId()). diff --git a/cloud/services/availabilityzone/reconcile.go b/cloud/services/availabilityzone/reconcile.go index d3e36eb..9579752 100644 --- a/cloud/services/availabilityzone/reconcile.go +++ b/cloud/services/availabilityzone/reconcile.go @@ -33,10 +33,6 @@ func (s *Service) Reconcile(ctx context.Context) error { return nil } -func (s *Service) Delete(_ context.Context) error { - return nil -} - // List retrieves the availability zones for the configured region. func (c *Client) List(ctx context.Context) ([]string, error) { if c.region == "" { diff --git a/cloud/services/availabilityzone/reconcile_test.go b/cloud/services/availabilityzone/reconcile_test.go index c906717..83d2878 100644 --- a/cloud/services/availabilityzone/reconcile_test.go +++ b/cloud/services/availabilityzone/reconcile_test.go @@ -119,14 +119,4 @@ func TestServiceReconcile(t *testing.T) { t.Fatalf("Reconcile() error = %v, want ErrTransient", err) } }) - - t.Run("Delete is a no-op", func(t *testing.T) { - svc := newTestService(t, availabilityzone.AvailabilityZoneSpec{}, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - })) - - if err := svc.Delete(context.Background()); err != nil { - t.Fatalf("Delete() error = %v, want nil", err) - } - }) } diff --git a/cloud/services/loadbalancer/service.go b/cloud/services/loadbalancer/service.go index a5d0e8e..e6b21a0 100644 --- a/cloud/services/loadbalancer/service.go +++ b/cloud/services/loadbalancer/service.go @@ -37,11 +37,10 @@ func IsTargetPoolUpdateError(err error) bool { // LoadBalancer describes an API-server load balancer. type LoadBalancer struct { - ID string - Name string - IP string - DNSName string - Port int32 + ID string + Name string + IP string + Port int32 } // Target describes a VM target in the API-server load balancer target pool. diff --git a/cloud/services/network/reconcile.go b/cloud/services/network/reconcile.go index d5f7511..e9e194a 100644 --- a/cloud/services/network/reconcile.go +++ b/cloud/services/network/reconcile.go @@ -37,10 +37,6 @@ func (s *Service) Reconcile(ctx context.Context) error { return nil } -func (s *Service) Delete(_ context.Context) error { - return nil -} - // Get retrieves a STACKIT network by ID. func (c *Client) Get(ctx context.Context, id string) (*Network, error) { if id == "" { diff --git a/cloud/services/network/reconcile_test.go b/cloud/services/network/reconcile_test.go index f7f2aa8..6094cdd 100644 --- a/cloud/services/network/reconcile_test.go +++ b/cloud/services/network/reconcile_test.go @@ -154,14 +154,4 @@ func TestServiceReconcile(t *testing.T) { t.Fatalf("Reconcile() error = %v, want ErrInvalidInput", err) } }) - - t.Run("Delete is a no-op", func(t *testing.T) { - svc := newTestService(t, network.NetworkSpec{}, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - })) - - if err := svc.Delete(context.Background()); err != nil { - t.Fatalf("Delete() error = %v, want nil", err) - } - }) } diff --git a/cloud/services/server/service.go b/cloud/services/server/service.go index c62f7fd..fbacbce 100644 --- a/cloud/services/server/service.go +++ b/cloud/services/server/service.go @@ -31,7 +31,6 @@ type Server struct { Name string State string PowerStatus string - ProviderID string AvailabilityZone string Addresses []Address } diff --git a/scope/machine_scope.go b/scope/machine_scope.go index b2a50c6..38b8e8a 100644 --- a/scope/machine_scope.go +++ b/scope/machine_scope.go @@ -102,7 +102,7 @@ func (s *MachineScope) Tags() map[string]string { } func (s *MachineScope) SetInstance(srv *server.Server) string { - providerID := cloud.NewProviderID(s.StackitCluster.Spec.ProjectID, s.StackitCluster.Spec.Region, srv.ID) + providerID := cloud.NewProviderID(srv.ID) s.StackitMachine.Status.InstanceID = srv.ID s.StackitMachine.Status.InstanceState = srv.State s.StackitMachine.Status.AvailabilityZone = srv.AvailabilityZone diff --git a/util/conditions.go b/util/conditions.go index 3a59a06..0523fa8 100644 --- a/util/conditions.go +++ b/util/conditions.go @@ -20,48 +20,6 @@ import ( ctrl "sigs.k8s.io/controller-runtime" ) -// SetCondition adds or updates a condition on conditions. -// -// The LastTransitionTime is only refreshed when the status actually changes, -// so callers can call this unconditionally on every reconcile. -func SetCondition( - conditions *[]metav1.Condition, - condType string, - status metav1.ConditionStatus, - reason, message string, - observedGeneration ...int64, -) { - now := metav1.Now() - var generation int64 - if len(observedGeneration) > 0 { - generation = observedGeneration[0] - } - for i, c := range *conditions { - if c.Type != condType { - continue - } - if c.Status == status && c.Reason == reason && c.Message == message && c.ObservedGeneration == generation { - return - } - (*conditions)[i].Status = status - (*conditions)[i].Reason = reason - (*conditions)[i].Message = message - (*conditions)[i].ObservedGeneration = generation - if c.Status != status { - (*conditions)[i].LastTransitionTime = now - } - return - } - *conditions = append(*conditions, metav1.Condition{ - Type: condType, - Status: status, - Reason: reason, - Message: message, - ObservedGeneration: generation, - LastTransitionTime: now, - }) -} - func SetConditions( conditions *[]metav1.Condition, generation int64, From 36df0582a7d2faf460b6aa08ca36555fc91ba00f Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Thu, 12 Nov 2026 17:28:37 +0100 Subject: [PATCH 14/60] refactor: remove leftover pre-HTTP-transport helpers and dead state in fake client --- cloud/cleanup_test.go | 48 +++--- cloud/fake/client.go | 169 +------------------ cloud/services/loadbalancer/reconcile.go | 5 +- cloud/services/loadbalancer/service.go | 4 +- cloud/services/loadbalancer/targets.go | 3 + cloud/services/server/reconcile.go | 23 ++- controller/stackitcluster_controller_test.go | 10 +- controller/stackitmachine_controller_test.go | 8 +- 8 files changed, 68 insertions(+), 202 deletions(-) diff --git a/cloud/cleanup_test.go b/cloud/cleanup_test.go index ef6566e..81fdd5f 100644 --- a/cloud/cleanup_test.go +++ b/cloud/cleanup_test.go @@ -16,6 +16,9 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud" cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" ) @@ -31,11 +34,16 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { "cluster-api-provider-stackit/test-id": "test-2", } - if _, err := client.CreateServer(ctx, server.ServerSpec{Name: "matched", Tags: tags}); err != nil { - t.Fatalf("CreateServer() error = %v", err) + srvClient := server.NewClient(client) + lbClient := loadbalancer.NewClient(client) + pubIPClient := publicip.NewClient(client) + sgClient := securitygroup.NewClient(client) + + if _, err := srvClient.Create(ctx, server.ServerSpec{Name: "matched", Tags: tags}); err != nil { + t.Fatalf("server.Create() error = %v", err) } - if _, err := client.CreateServer(ctx, server.ServerSpec{Name: "other", Tags: otherTags}); err != nil { - t.Fatalf("CreateServer() error = %v", err) + if _, err := srvClient.Create(ctx, server.ServerSpec{Name: "other", Tags: otherTags}); err != nil { + t.Fatalf("server.Create() error = %v", err) } if _, err := client.CreatePublicIP(ctx, tags); err != nil { t.Fatalf("CreatePublicIP() error = %v", err) @@ -60,58 +68,58 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { t.Fatalf("CleanupByTags() error = %v", err) } - matchedServers, err := client.ListServersByTags(ctx, tags) + matchedServers, err := srvClient.ListByTags(ctx, tags) if err != nil { - t.Fatalf("ListServersByTags() error = %v", err) + t.Fatalf("server.ListByTags() error = %v", err) } if len(matchedServers) != 0 { t.Fatalf("matched server count = %d, want 0", len(matchedServers)) } - otherServers, err := client.ListServersByTags(ctx, otherTags) + otherServers, err := srvClient.ListByTags(ctx, otherTags) if err != nil { - t.Fatalf("ListServersByTags(other) error = %v", err) + t.Fatalf("server.ListByTags(other) error = %v", err) } if len(otherServers) != 1 { t.Fatalf("other server count = %d, want 1", len(otherServers)) } - matchedLoadBalancers, err := client.ListLoadBalancersByTags(ctx, tags) + matchedLoadBalancers, err := lbClient.ListByTags(ctx, tags) if err != nil { - t.Fatalf("ListLoadBalancersByTags() error = %v", err) + t.Fatalf("loadbalancer.ListByTags() error = %v", err) } if len(matchedLoadBalancers) != 0 { t.Fatalf("matched load balancer count = %d, want 0", len(matchedLoadBalancers)) } - otherLoadBalancers, err := client.ListLoadBalancersByTags(ctx, otherTags) + otherLoadBalancers, err := lbClient.ListByTags(ctx, otherTags) if err != nil { - t.Fatalf("ListLoadBalancersByTags(other) error = %v", err) + t.Fatalf("loadbalancer.ListByTags(other) error = %v", err) } if len(otherLoadBalancers) != 1 { t.Fatalf("other load balancer count = %d, want 1", len(otherLoadBalancers)) } - matchedPublicIPs, err := client.ListPublicIPsByTags(ctx, tags) + matchedPublicIPs, err := pubIPClient.ListByTags(ctx, tags) if err != nil { - t.Fatalf("ListPublicIPsByTags() error = %v", err) + t.Fatalf("publicip.ListByTags() error = %v", err) } if len(matchedPublicIPs) != 0 { t.Fatalf("matched public IP count = %d, want 0", len(matchedPublicIPs)) } - otherPublicIPs, err := client.ListPublicIPsByTags(ctx, otherTags) + otherPublicIPs, err := pubIPClient.ListByTags(ctx, otherTags) if err != nil { - t.Fatalf("ListPublicIPsByTags(other) error = %v", err) + t.Fatalf("publicip.ListByTags(other) error = %v", err) } if len(otherPublicIPs) != 1 { t.Fatalf("other public IP count = %d, want 1", len(otherPublicIPs)) } - matchedSecurityGroups, err := client.ListSecurityGroupsByTags(ctx, tags) + matchedSecurityGroups, err := sgClient.ListByTags(ctx, tags) if err != nil { - t.Fatalf("ListSecurityGroupsByTags() error = %v", err) + t.Fatalf("securitygroup.ListByTags() error = %v", err) } if len(matchedSecurityGroups) != 0 { t.Fatalf("matched security group count = %d, want 0", len(matchedSecurityGroups)) } - otherSecurityGroups, err := client.ListSecurityGroupsByTags(ctx, otherTags) + otherSecurityGroups, err := sgClient.ListByTags(ctx, otherTags) if err != nil { - t.Fatalf("ListSecurityGroupsByTags(other) error = %v", err) + t.Fatalf("securitygroup.ListByTags(other) error = %v", err) } if len(otherSecurityGroups) != 1 { t.Fatalf("other security group count = %d, want 1", len(otherSecurityGroups)) diff --git a/cloud/fake/client.go b/cloud/fake/client.go index 0432edd..bcb43bf 100644 --- a/cloud/fake/client.go +++ b/cloud/fake/client.go @@ -35,11 +35,6 @@ import ( lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" ) -const ( - bootstrapTargetName = "capi-bootstrap-placeholder" - bootstrapTargetIP = "10.0.0.1" -) - // Client is an in-memory implementation of cloud.Client. type Client struct { mu sync.Mutex @@ -116,12 +111,10 @@ type securityGroupRuleEntry struct { } type securityGroupEntry struct { - securityGroup *securitygroup.SecurityGroup - description string - tags map[string]string - allowedCIDRs []string - remoteSecurityGroups []string - rules map[string]*securityGroupRuleEntry + securityGroup *securitygroup.SecurityGroup + description string + tags map[string]string + rules map[string]*securityGroupRuleEntry } // New returns a Client preconfigured with the given networks. @@ -189,59 +182,6 @@ func (c *Client) genID() string { return fmt.Sprintf("00000000-0000-4000-8000-%012d", c.nextID) } -func (c *Client) GetServer(_ context.Context, id string) (*server.Server, error) { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextGetServer); err != nil { - return nil, err - } - entry, ok := c.servers[id] - if !ok { - return nil, fmt.Errorf("server %q: %w", id, shared.ErrNotFound) - } - return cloneServer(entry.server), nil -} - -func (c *Client) ListServersByTags(_ context.Context, tags map[string]string) ([]*server.Server, error) { - c.mu.Lock() - defer c.mu.Unlock() - - if len(tags) == 0 { - return nil, fmt.Errorf("empty tags: %w", shared.ErrInvalidInput) - } - servers := []*server.Server{} - for _, entry := range c.servers { - if shared.MapContains(entry.tags, tags) { - servers = append(servers, cloneServer(entry.server)) - } - } - return servers, nil -} - -// CreateServer creates a server in the fake store. -func (c *Client) CreateServer(_ context.Context, input server.ServerSpec) (*server.Server, error) { - c.mu.Lock() - if c.BeforeCreateServer != nil { - c.mu.Unlock() - c.BeforeCreateServer() - c.mu.Lock() - } - defer c.mu.Unlock() - - if err := consume(&c.FailNextCreateServer); err != nil { - return nil, err - } - for _, entry := range c.servers { - if len(input.Tags) > 0 && shared.MapContains(entry.tags, input.Tags) { - return cloneServer(entry.server), nil - } - } - - entry := c.createServerEntry(input) - return cloneServer(entry.server), nil -} - func (c *Client) createServerEntry(input server.ServerSpec) *serverEntry { id := c.genID() c.LastCreateServerInput = input @@ -269,18 +209,6 @@ func (c *Client) createServerEntry(input server.ServerSpec) *serverEntry { return entry } -// DeleteServer removes a server. It returns nil if the server is already gone. -func (c *Client) DeleteServer(_ context.Context, id string) error { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextDeleteServer); err != nil { - return err - } - delete(c.servers, id) - return nil -} - func (c *Client) GetNetwork(_ context.Context, id string) (*network.Network, error) { c.mu.Lock() if c.BeforeGetNetwork != nil { @@ -319,26 +247,6 @@ func (c *Client) getPublicIP(_ context.Context, id string) (*publicip.PublicIP, return &out, nil } -func (c *Client) ListPublicIPsByTags(_ context.Context, tags map[string]string) ([]*publicip.PublicIP, error) { - c.mu.Lock() - defer c.mu.Unlock() - - if err := consume(&c.FailNextListPublicIPs); err != nil { - return nil, err - } - if len(tags) == 0 { - return nil, fmt.Errorf("empty tags: %w", shared.ErrInvalidInput) - } - publicIPs := []*publicip.PublicIP{} - for _, entry := range c.publicIPs { - if shared.MapContains(entry.tags, tags) { - out := *entry.publicIP - publicIPs = append(publicIPs, &out) - } - } - return publicIPs, nil -} - func (c *Client) CreatePublicIP(_ context.Context, tags map[string]string) (*publicip.PublicIP, error) { c.mu.Lock() defer c.mu.Unlock() @@ -415,23 +323,6 @@ func (c *Client) removePublicIPFromServer(_ context.Context, serverID, publicIPI return nil } -func (c *Client) ListSecurityGroupsByTags(_ context.Context, tags map[string]string) ([]*securitygroup.SecurityGroup, error) { - c.mu.Lock() - defer c.mu.Unlock() - - if len(tags) == 0 { - return nil, fmt.Errorf("empty tags: %w", shared.ErrInvalidInput) - } - securityGroups := []*securitygroup.SecurityGroup{} - for _, entry := range c.securityGroups { - if shared.MapContains(entry.tags, tags) { - out := *entry.securityGroup - securityGroups = append(securityGroups, &out) - } - } - return securityGroups, nil -} - func (c *Client) CreateSecurityGroup(_ context.Context, name string, tags map[string]string) (*securitygroup.SecurityGroup, error) { c.mu.Lock() defer c.mu.Unlock() @@ -456,24 +347,6 @@ func (c *Client) CreateLoadBalancer(_ context.Context, name string, tags map[str return &out, nil } -func (c *Client) ListLoadBalancersByTags(_ context.Context, tags map[string]string) ([]*loadbalancer.LoadBalancer, error) { - c.mu.Lock() - defer c.mu.Unlock() - - if len(tags) == 0 { - return nil, fmt.Errorf("empty tags: %w", shared.ErrInvalidInput) - } - wantLabels := loadbalancer.Labels(tags) - loadBalancers := []*loadbalancer.LoadBalancer{} - for _, entry := range c.loadBalancers { - if shared.MapContains(entry.tags, wantLabels) || shared.MapContains(entry.tags, tags) { - out := *entry.lb - loadBalancers = append(loadBalancers, &out) - } - } - return loadBalancers, nil -} - // ServerCount returns the number of currently tracked servers (test helper). func (c *Client) ServerCount() int { c.mu.Lock() @@ -537,18 +410,6 @@ func (c *Client) ServerUserData(serverID string) []byte { return append([]byte(nil), entry.userData...) } -// SecurityGroupRemoteSources returns the remote security groups allowed by the -// fake security group rules. -func (c *Client) SecurityGroupRemoteSources(securityGroupID string) []string { - c.mu.Lock() - defer c.mu.Unlock() - entry, ok := c.securityGroups[securityGroupID] - if !ok { - return nil - } - return append([]string(nil), entry.remoteSecurityGroups...) -} - // SecurityGroupRules returns the rules currently stored on a fake security group. func (c *Client) SecurityGroupRules(securityGroupID string) []securitygroup.Rule { c.mu.Lock() @@ -618,10 +479,6 @@ func securityGroupSet(ids []string) map[string]struct{} { return out } -func containsString(values []string, needle string) bool { - return slices.Contains(values, needle) -} - func (c *Client) ListAvailabilityZones(_ context.Context) ([]string, error) { c.mu.Lock() defer c.mu.Unlock() @@ -634,15 +491,6 @@ func (c *Client) ListAvailabilityZones(_ context.Context) ([]string, error) { return []string{"eu01-1", "eu01-2", "eu01-3"}, nil } -func cloneServer(s *server.Server) *server.Server { - out := *s - if s.Addresses != nil { - out.Addresses = make([]server.Address, len(s.Addresses)) - copy(out.Addresses, s.Addresses) - } - return &out -} - type roundTripperFunc func(*http.Request) (*http.Response, error) func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { @@ -873,7 +721,8 @@ func (c *Client) createLoadBalancerEntry(name string, port int32, labels map[str } targetMap := map[string]string{} if len(targets) == 0 { - targetMap[bootstrapTargetName] = bootstrapTargetIP + bootstrap := loadbalancer.BootstrapTarget("") + targetMap[bootstrap.Name] = bootstrap.IP } else { for _, target := range targets { targetMap[target.Name] = target.IP @@ -1510,12 +1359,6 @@ func (c *Client) createSecurityGroupRule(sgID string, rule securityGroupRuleEntr rule.id = c.genID() stored := rule entry.rules[rule.id] = &stored - if rule.IPRange != "" && !containsString(entry.allowedCIDRs, rule.IPRange) { - entry.allowedCIDRs = append(entry.allowedCIDRs, rule.IPRange) - } - if rule.RemoteSecurityGroupID != "" && !containsString(entry.remoteSecurityGroups, rule.RemoteSecurityGroupID) { - entry.remoteSecurityGroups = append(entry.remoteSecurityGroups, rule.RemoteSecurityGroupID) - } return fakeSecurityGroupRuleMap(&stored), nil } diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index 31f8429..6f8f08c 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -216,9 +216,10 @@ func (s LoadBalancerSpec) toCreatePayload(region string) lb.CreateLoadBalancerPa } targets := make([]lb.Target, 0, max(1, len(seed))) if len(seed) == 0 { + bootstrap := BootstrapTarget("") target := lb.NewTarget() - target.SetDisplayName(bootstrapTargetName) - target.SetIp(bootstrapTargetIP) + target.SetDisplayName(bootstrap.Name) + target.SetIp(bootstrap.IP) targets = append(targets, *target) } else { for _, targetInput := range seed { diff --git a/cloud/services/loadbalancer/service.go b/cloud/services/loadbalancer/service.go index e6b21a0..435e03b 100644 --- a/cloud/services/loadbalancer/service.go +++ b/cloud/services/loadbalancer/service.go @@ -20,10 +20,12 @@ const ( // DefaultAPIServerPort is the default TCP port for the Kubernetes API server. DefaultAPIServerPort int32 = 6443 + // DefaultBootstrapTargetIP is the placeholder IP used when no network prefix or target IP is available. + DefaultBootstrapTargetIP = "10.0.0.1" + apiserverTargetPoolName = "apiserver" apiserverListenerName = "apiserver" bootstrapTargetName = "capi-bootstrap-placeholder" - bootstrapTargetIP = "10.0.0.1" ) // ErrTargetPoolUpdate indicates that the load balancer was created/resolved diff --git a/cloud/services/loadbalancer/targets.go b/cloud/services/loadbalancer/targets.go index c831c41..6f3d65c 100644 --- a/cloud/services/loadbalancer/targets.go +++ b/cloud/services/loadbalancer/targets.go @@ -35,6 +35,9 @@ const ( // BootstrapTarget seeds a new load balancer; STACKIT rejects an empty pool. func BootstrapTarget(ip string) Target { + if ip == "" { + ip = DefaultBootstrapTargetIP + } return Target{ Name: bootstrapTargetName, IP: ip, diff --git a/cloud/services/server/reconcile.go b/cloud/services/server/reconcile.go index fcf8225..c6456a5 100644 --- a/cloud/services/server/reconcile.go +++ b/cloud/services/server/reconcile.go @@ -144,6 +144,20 @@ func (c *Client) DeleteByID(ctx context.Context, id string) error { return nil } +// Create creates a new STACKIT server from the given spec. +func (c *Client) Create(ctx context.Context, spec ServerSpec) (*Server, error) { + payload := spec.toCreatePayload() + created, err := c.IaaSClient().DefaultAPI.CreateServer(ctx, c.ProjectID(), c.Region()). + CreateServerPayload(payload). + Execute() + if err != nil { + return nil, shared.ClassifySDKError("create server", err) + } + out := c.serverFromSDK(ctx, created) + logf.FromContext(ctx).V(1).Info("Created server", "name", spec.Name, "serverID", out.ID) + return out, nil +} + func (c *Client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bool, error) { if spec.ID != "" { srv, err := c.Get(ctx, spec.ID) @@ -170,15 +184,10 @@ func (c *Client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bo ) } - payload := spec.toCreatePayload() - created, err := c.IaaSClient().DefaultAPI.CreateServer(ctx, c.ProjectID(), c.Region()). - CreateServerPayload(payload). - Execute() + out, err := c.Create(ctx, spec) if err != nil { - return nil, false, shared.ClassifySDKError("create server", err) + return nil, false, err } - out := c.serverFromSDK(ctx, created) - logf.FromContext(ctx).V(1).Info("Created server", "name", spec.Name, "serverID", out.ID) return out, true, nil } diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index abaaff5..758fa3e 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -153,7 +153,7 @@ var _ = Describe("StackitCluster Controller", func() { Direction: securitygroup.DirectionEgress, }, )) - nodeSGs, err := fakeCloud.ListSecurityGroupsByTags(ctx, scope.NodeSSHTags(got)) + nodeSGs, err := securitygroup.NewClient(fakeCloud).ListByTags(ctx, scope.NodeSSHTags(got)) Expect(err).NotTo(HaveOccurred()) Expect(nodeSGs).To(HaveLen(1)) Expect(fakeCloud.SecurityGroupRules(nodeSGs[0].ID)).To(ConsistOf( @@ -202,10 +202,10 @@ var _ = Describe("StackitCluster Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(fakeCloud.SecurityGroupCount()).To(Equal(2)) - nodeSGs, err := fakeCloud.ListSecurityGroupsByTags(ctx, scope.NodeSSHTags(got)) + nodeSGs, err := securitygroup.NewClient(fakeCloud).ListByTags(ctx, scope.NodeSSHTags(got)) Expect(err).NotTo(HaveOccurred()) Expect(nodeSGs).To(HaveLen(1)) - workerServer, err := fakeCloud.CreateServer(ctx, server.ServerSpec{ + workerServer, err := server.NewClient(fakeCloud).Create(ctx, server.ServerSpec{ Name: got.Name + "-worker-0", Tags: util.MachineTags(got.Name, got.Namespace, "worker-0", "uid-1", nil), SecurityGroups: []string{nodeSGs[0].ID}, @@ -1023,9 +1023,9 @@ var _ = Describe("StackitCluster Controller", func() { Expect(fakeCloud.LastCreateServerInput.AvailabilityZone).To(Equal("eu01-2")) Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) - server, err := fakeCloud.GetServer(ctx, got.Status.Bastion.ServerID) + srv, err := server.NewClient(fakeCloud).Get(ctx, got.Status.Bastion.ServerID) Expect(err).NotTo(HaveOccurred()) - Expect(server.AvailabilityZone).To(Equal("eu01-2")) + Expect(srv.AvailabilityZone).To(Equal("eu01-2")) }) It("reports an error condition when listing availability zones fails", func() { diff --git a/controller/stackitmachine_controller_test.go b/controller/stackitmachine_controller_test.go index e940689..1512f4b 100644 --- a/controller/stackitmachine_controller_test.go +++ b/controller/stackitmachine_controller_test.go @@ -30,6 +30,7 @@ import ( cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -156,7 +157,7 @@ var _ = Describe("StackitMachine Controller", func() { Expect(instanceID).NotTo(BeEmpty()) By("removing the server behind the provider's back") - Expect(fakeCloud.DeleteServer(ctx, instanceID)).To(Succeed()) + Expect(server.NewClient(fakeCloud).DeleteByID(ctx, instanceID)).To(Succeed()) Expect(fakeCloud.ServerCount()).To(Equal(0)) By("reconciling again") @@ -241,13 +242,12 @@ var _ = Describe("StackitMachine Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(got.Status.Ready).To(BeTrue()) Expect(fakeCloud.AddSecurityGroupCalls).To(Equal(1)) - securityGroups, err := fakeCloud.ListSecurityGroupsByTags(ctx, map[string]string{ + securityGroups, err := securitygroup.NewClient(fakeCloud).ListByTags(ctx, map[string]string{ util.LabelClusterName: clusterName, util.LabelResourceRole: util.ResourceRoleNodeSSH, }) Expect(err).NotTo(HaveOccurred()) Expect(securityGroups).To(HaveLen(1)) - Expect(fakeCloud.SecurityGroupRemoteSources(securityGroups[0].ID)).To(ConsistOf("bastion-sg")) Expect(fakeCloud.SecurityGroupRules(securityGroups[0].ID)).To(ConsistOf( securitygroup.Rule{ Direction: securitygroup.DirectionIngress, @@ -542,7 +542,7 @@ var _ = Describe("StackitMachine Controller", func() { Expect(instanceID).NotTo(BeEmpty()) By("removing the server behind the provider's back") - Expect(fakeCloud.DeleteServer(ctx, instanceID)).To(Succeed()) + Expect(server.NewClient(fakeCloud).DeleteByID(ctx, instanceID)).To(Succeed()) Expect(fakeCloud.ServerCount()).To(Equal(0)) By("reconciling into the terminal state") From 4ff52c6eac026759ecf6829e25e00226f5172875 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Thu, 12 Nov 2026 17:32:57 +0100 Subject: [PATCH 15/60] refactor: consolidate service ClientScope aliases and FindByTags in shared --- cloud/services/loadbalancer/reconcile.go | 6 +-- cloud/services/network/service.go | 7 +--- cloud/services/publicip/reconcile.go | 6 +-- cloud/services/publicip/service.go | 7 +--- cloud/services/securitygroup/reconcile.go | 6 +-- cloud/services/securitygroup/service.go | 7 +--- cloud/services/server/reconcile.go | 6 +-- cloud/services/server/service.go | 7 +--- cloud/services/shared/errors_test.go | 45 +++++++++++++++++++++++ cloud/services/shared/utils.go | 16 ++++++++ 10 files changed, 73 insertions(+), 40 deletions(-) diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index 6f8f08c..074d351 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -79,11 +79,7 @@ func (s *Service) Delete(ctx context.Context) error { // FindByTags finds a single STACKIT load balancer matching the given tag selector. func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*LoadBalancer, error) { - matched, err := c.ListByTags(ctx, tags) - if err != nil { - return nil, err - } - return shared.FindSingle(matched, "load balancer", "load balancers") + return shared.FindByTags(ctx, tags, c.ListByTags, "load balancer", "load balancers") } // ListByTags lists STACKIT load balancers matching the given tag selector. diff --git a/cloud/services/network/service.go b/cloud/services/network/service.go index 0d31415..ed1aa06 100644 --- a/cloud/services/network/service.go +++ b/cloud/services/network/service.go @@ -25,12 +25,9 @@ type NetworkSpec struct { SetNetwork func(network *Network) } -// ClientScope is an interface that provides the IaaS client and target scope for network operations. -type ClientScope = shared.IaaSClientScope - // Scope is an interface that provides scope data needed for network reconciliation. type Scope interface { - ClientScope + shared.IaaSClientScope NetworkSpec() NetworkSpec } @@ -40,7 +37,7 @@ type Client struct { } // NewClient returns a Client for the given scope. -func NewClient(scope ClientScope) *Client { +func NewClient(scope shared.IaaSClientScope) *Client { return &Client{IaaSScope: shared.NewIaaSScope(scope)} } diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index a330066..ea00ea5 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -103,11 +103,7 @@ func (c *Client) Get(ctx context.Context, id string) (*PublicIP, error) { // FindByTags finds a single STACKIT public IP matching the given tag selector. func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*PublicIP, error) { - matched, err := c.ListByTags(ctx, tags) - if err != nil { - return nil, err - } - return shared.FindSingle(matched, "public IP", "public IPs") + return shared.FindByTags(ctx, tags, c.ListByTags, "public IP", "public IPs") } // ListByTags lists STACKIT public IPs matching the given tag selector. diff --git a/cloud/services/publicip/service.go b/cloud/services/publicip/service.go index f420848..4556cca 100644 --- a/cloud/services/publicip/service.go +++ b/cloud/services/publicip/service.go @@ -28,12 +28,9 @@ type PublicIPSpec struct { SetPublicIP func(id, ip string) } -// ClientScope is an interface that provides the IaaS client and target scope for public IP operations. -type ClientScope = shared.IaaSClientScope - // Scope is an interface that provides scope data needed for public IP reconciliation. type Scope interface { - ClientScope + shared.IaaSClientScope PublicIPSpec() PublicIPSpec } @@ -43,7 +40,7 @@ type Client struct { } // NewClient returns a Client for the given scope. -func NewClient(scope ClientScope) *Client { +func NewClient(scope shared.IaaSClientScope) *Client { return &Client{IaaSScope: shared.NewIaaSScope(scope)} } diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index f13c1cd..170f104 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -46,11 +46,7 @@ func (s *Service) Delete(ctx context.Context) error { // FindByTags finds a single STACKIT security group matching the given tag selector. func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*SecurityGroup, error) { - matched, err := c.ListByTags(ctx, tags) - if err != nil { - return nil, err - } - return shared.FindSingle(matched, "security group", "security groups") + return shared.FindByTags(ctx, tags, c.ListByTags, "security group", "security groups") } // ListByTags lists STACKIT security groups matching the given tag selector. diff --git a/cloud/services/securitygroup/service.go b/cloud/services/securitygroup/service.go index 5d3a128..b7ebeee 100644 --- a/cloud/services/securitygroup/service.go +++ b/cloud/services/securitygroup/service.go @@ -58,12 +58,9 @@ type SecurityGroupSpec struct { SetID func(string) } -// ClientScope is an interface that provides the IaaS client and target scope for security group operations. -type ClientScope = shared.IaaSClientScope - // Scope is an interface that provides scope data needed for security group reconciliation. type Scope interface { - ClientScope + shared.IaaSClientScope SecurityGroupSpecs() []SecurityGroupSpec } @@ -73,7 +70,7 @@ type Client struct { } // NewClient returns a Client for the given scope. -func NewClient(scope ClientScope) *Client { +func NewClient(scope shared.IaaSClientScope) *Client { return &Client{IaaSScope: shared.NewIaaSScope(scope)} } diff --git a/cloud/services/server/reconcile.go b/cloud/services/server/reconcile.go index c6456a5..65956bf 100644 --- a/cloud/services/server/reconcile.go +++ b/cloud/services/server/reconcile.go @@ -73,11 +73,7 @@ func (c *Client) Get(ctx context.Context, id string) (*Server, error) { // FindByTags finds a single STACKIT server matching the given tag selector. func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*Server, error) { - matched, err := c.ListByTags(ctx, tags) - if err != nil { - return nil, err - } - return shared.FindSingle(matched, "server", "servers") + return shared.FindByTags(ctx, tags, c.ListByTags, "server", "servers") } // ListByTags lists STACKIT servers matching the given tag selector. diff --git a/cloud/services/server/service.go b/cloud/services/server/service.go index fbacbce..617df47 100644 --- a/cloud/services/server/service.go +++ b/cloud/services/server/service.go @@ -66,12 +66,9 @@ type ServerSpec struct { ClearServer func() } -// ClientScope is an interface that provides the IaaS client and target scope for server operations. -type ClientScope = shared.IaaSClientScope - // Scope is an interface that provides scope data needed for server reconciliation. type Scope interface { - ClientScope + shared.IaaSClientScope ServerSpec() ServerSpec } @@ -81,7 +78,7 @@ type Client struct { } // NewClient returns a Client for the given scope. -func NewClient(scope ClientScope) *Client { +func NewClient(scope shared.IaaSClientScope) *Client { return &Client{IaaSScope: shared.NewIaaSScope(scope)} } diff --git a/cloud/services/shared/errors_test.go b/cloud/services/shared/errors_test.go index 71ba7f4..23f3872 100644 --- a/cloud/services/shared/errors_test.go +++ b/cloud/services/shared/errors_test.go @@ -11,6 +11,7 @@ You may obtain a copy of the License at package shared import ( + "context" "fmt" "testing" ) @@ -80,3 +81,47 @@ func TestFindSingle(t *testing.T) { } }) } + +func TestFindByTags(t *testing.T) { + ctx := context.Background() + tags := map[string]string{"cluster": "test"} + + t.Run("propagates error from listByTags", func(t *testing.T) { + _, err := FindByTags(ctx, tags, func(context.Context, map[string]string) ([]string, error) { + return nil, ErrTransient + }, "server", "servers") + if !IsTransient(err) { + t.Fatalf("FindByTags() error = %v, want ErrTransient", err) + } + }) + + t.Run("returns ErrNotFound when empty", func(t *testing.T) { + _, err := FindByTags(ctx, tags, func(context.Context, map[string]string) ([]string, error) { + return nil, nil + }, "server", "servers") + if !IsNotFound(err) { + t.Fatalf("FindByTags() error = %v, want ErrNotFound", err) + } + }) + + t.Run("returns ErrConflict when multiple", func(t *testing.T) { + _, err := FindByTags(ctx, tags, func(context.Context, map[string]string) ([]string, error) { + return []string{"a", "b"}, nil + }, "server", "servers") + if !IsConflict(err) { + t.Fatalf("FindByTags() error = %v, want ErrConflict", err) + } + }) + + t.Run("returns single element", func(t *testing.T) { + got, err := FindByTags(ctx, tags, func(context.Context, map[string]string) ([]string, error) { + return []string{"a"}, nil + }, "server", "servers") + if err != nil { + t.Fatalf("FindByTags() error = %v", err) + } + if got != "a" { + t.Fatalf("FindByTags() = %q, want %q", got, "a") + } + }) +} diff --git a/cloud/services/shared/utils.go b/cloud/services/shared/utils.go index d2547e9..66bf7ee 100644 --- a/cloud/services/shared/utils.go +++ b/cloud/services/shared/utils.go @@ -11,6 +11,7 @@ You may obtain a copy of the License at package shared import ( + "context" "errors" "fmt" "net/http" @@ -55,6 +56,21 @@ func (c IaaSScope) IaaSClient() *iaas.APIClient { return c.iaasClient } +// FindByTags lists resources by tags and returns the single matching item, or ErrNotFound if empty, or ErrConflict if more than one. +func FindByTags[T any]( + ctx context.Context, + tags map[string]string, + listByTags func(context.Context, map[string]string) ([]T, error), + singular, plural string, +) (T, error) { + var zero T + matched, err := listByTags(ctx, tags) + if err != nil { + return zero, err + } + return FindSingle(matched, singular, plural) +} + // FindSingle returns the single item in matched, or ErrNotFound if empty, or ErrConflict if more than one. func FindSingle[T any](matched []T, singular, plural string) (T, error) { var zero T From 735a2651f36e38d57983681270237feef3171ed0 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Thu, 12 Nov 2026 18:05:59 +0100 Subject: [PATCH 16/60] fix: resolve spec.ID on reconcile in loadbalancer/securitygroup and use s.Region() in ClusterScope --- cloud/fake/client.go | 47 ++++++++++- cloud/services/loadbalancer/reconcile.go | 24 +++++- cloud/services/loadbalancer/reconcile_test.go | 74 ++++++++++++++++ cloud/services/securitygroup/reconcile.go | 21 +++++ .../services/securitygroup/reconcile_test.go | 84 +++++++++++++++++++ scope/cluster_scope.go | 2 +- scope/cluster_scope_test.go | 18 ++++ 7 files changed, 263 insertions(+), 7 deletions(-) diff --git a/cloud/fake/client.go b/cloud/fake/client.go index bcb43bf..106d9c2 100644 --- a/cloud/fake/client.go +++ b/cloud/fake/client.go @@ -682,14 +682,29 @@ func (c *Client) getLoadBalancerItem(id string) (map[string]any, error) { c.mu.Lock() defer c.mu.Unlock() - if err := consume(&c.FailNextSetTargets); err != nil { - return nil, err - } entry, ok := c.loadBalancers[id] if !ok { return nil, fmt.Errorf("load balancer %q: %w", id, shared.ErrNotFound) } - return fakeLoadBalancerMap(entry), nil + item := fakeLoadBalancerMap(entry) + // Force SetTargets to see a target diff and issue UpdateTargetPool so + // FailNextSetTargets is consumed on the target update call rather than Get. + if c.FailNextSetTargets != nil { + bootstrap := loadbalancer.BootstrapTarget("") + item["targetPools"] = []any{ + map[string]any{ + "name": "apiserver", + "targetPort": entry.lb.Port, + "targets": []any{ + map[string]any{ + "displayName": bootstrap.Name, + "ip": bootstrap.IP, + }, + }, + }, + } + } + return item, nil } func (c *Client) createLoadBalancer(name string, port int32, labels map[string]string, targets []loadbalancer.Target) (map[string]any, error) { @@ -950,6 +965,16 @@ func (c *Client) handleSecurityGroupRequest(rec *httptest.ResponseRecorder, r *h writeFakeStatus(rec, c.deleteSecurityGroupRule(parts[0], parts[1])) return true + case r.Method == http.MethodGet && strings.Contains(path, "/security-groups/"): + id := path[strings.LastIndex(path, "/security-groups/")+len("/security-groups/"):] + got, err := c.getSecurityGroupItem(id) + if err != nil { + writeFakeError(rec, err) + } else { + _ = json.NewEncoder(rec).Encode(got) + } + return true + case r.Method == http.MethodDelete && strings.Contains(path, "/security-groups/"): id := path[strings.LastIndex(path, "/security-groups/")+len("/security-groups/"):] writeFakeStatus(rec, c.deleteSecurityGroup(id)) @@ -1314,6 +1339,20 @@ func (c *Client) createSecurityGroupEntry(name, description string, tags map[str return entry, nil } +func (c *Client) getSecurityGroupItem(id string) (map[string]any, error) { + c.mu.Lock() + defer c.mu.Unlock() + + if id == "" { + return nil, fmt.Errorf("security group ID is empty: %w", shared.ErrInvalidInput) + } + entry, ok := c.securityGroups[id] + if !ok { + return nil, fmt.Errorf("security group %q: %w", id, shared.ErrNotFound) + } + return fakeSecurityGroupMap(entry), nil +} + func (c *Client) listSecurityGroupItems(tags map[string]string) ([]map[string]any, error) { c.mu.Lock() defer c.mu.Unlock() diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index 074d351..401023f 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -77,6 +77,18 @@ func (s *Service) Delete(ctx context.Context) error { return nil } +// Get retrieves a STACKIT load balancer by ID. +func (c *Client) Get(ctx context.Context, id string) (*LoadBalancer, error) { + if id == "" { + return nil, fmt.Errorf("%w: load balancer ID is empty", shared.ErrInvalidInput) + } + loadBalancer, err := c.lbClient.DefaultAPI.GetLoadBalancer(ctx, c.projectID, c.region, id).Execute() + if err != nil { + return nil, shared.ClassifySDKError("get load balancer", err) + } + return loadBalancerFromSDK(loadBalancer), nil +} + // FindByTags finds a single STACKIT load balancer matching the given tag selector. func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*LoadBalancer, error) { return shared.FindByTags(ctx, tags, c.ListByTags, "load balancer", "load balancers") @@ -121,6 +133,15 @@ func (c *Client) DeleteByID(ctx context.Context, id string) error { } func (c *Client) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) (*LoadBalancer, error) { + if spec.ID != "" { + loadBalancer, err := c.Get(ctx, spec.ID) + if err == nil { + return loadBalancer, nil + } + if !shared.IsNotFound(err) && !isLoadBalancerServiceNotEnabled(err) { + return nil, err + } + } if existing, err := c.FindByTags(ctx, spec.Tags); err == nil { return existing, nil } else if !shared.IsNotFound(err) && !isLoadBalancerServiceNotEnabled(err) { @@ -160,9 +181,8 @@ func (c *Client) resolveLoadBalancerForDelete(ctx context.Context, spec LoadBala } func (c *Client) loadBalancerExists(ctx context.Context, id string) (bool, error) { - _, err := c.lbClient.DefaultAPI.GetLoadBalancer(ctx, c.projectID, c.region, id).Execute() + _, err := c.Get(ctx, id) if err != nil { - err := shared.ClassifySDKError("get load balancer", err) if shared.IsNotFound(err) { return false, nil } diff --git a/cloud/services/loadbalancer/reconcile_test.go b/cloud/services/loadbalancer/reconcile_test.go index 81e7c1a..6aea256 100644 --- a/cloud/services/loadbalancer/reconcile_test.go +++ b/cloud/services/loadbalancer/reconcile_test.go @@ -246,6 +246,80 @@ func TestServiceReconcile(t *testing.T) { } func TestServiceReconcileReuseAndErrors(t *testing.T) { + t.Run("reuses existing load balancer found by ID without listing by tags", func(t *testing.T) { + var recordedID string + spec := loadbalancer.LoadBalancerSpec{ + ID: testLBName, + Name: testLBName, + NetworkID: testNetworkID, + Port: loadbalancer.DefaultAPIServerPort, + Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, + SetID: func(id string) { + recordedID = id + }, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/load-balancers/"+testLBName) { + writeJSON(t, w, sdkLoadBalancerJSON(testLBName, nil, nil)) + return + } + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if recordedID != testLBName { + t.Fatalf("recordedID = %q, want %q", recordedID, testLBName) + } + }) + + t.Run("falls back to tag lookup when recorded ID is not found during reconcile", func(t *testing.T) { + var ( + getByIDCalled bool + recordedID string + ) + spec := loadbalancer.LoadBalancerSpec{ + ID: "stale-lb", + Name: testLBName, + NetworkID: testNetworkID, + Port: loadbalancer.DefaultAPIServerPort, + Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, + SetID: func(id string) { + recordedID = id + }, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers/stale-lb"): + getByIDCalled = true + w.WriteHeader(http.StatusNotFound) + writeJSON(t, w, map[string]any{"message": "not found"}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers"): + writeJSON(t, w, map[string]any{"loadBalancers": []any{ + sdkLoadBalancerJSON(testLBName, map[string]any{ + "cluster.x-k8s.io.cluster-name": "test", + }, nil), + }}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if !getByIDCalled { + t.Fatal("expected GET by spec.ID before falling back to tag lookup") + } + if recordedID != testLBName { + t.Fatalf("recordedID = %q, want %q", recordedID, testLBName) + } + }) + t.Run("reuses existing load balancer found by tags without creating duplicate", func(t *testing.T) { var recordedID string spec := loadbalancer.LoadBalancerSpec{ diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index 170f104..0a093db 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -44,6 +44,18 @@ func (s *Service) Delete(ctx context.Context) error { return nil } +// Get retrieves a STACKIT security group by ID. +func (c *Client) Get(ctx context.Context, id string) (*SecurityGroup, error) { + if id == "" { + return nil, fmt.Errorf("%w: security group ID is empty", shared.ErrInvalidInput) + } + securityGroup, err := c.IaaSClient().DefaultAPI.GetSecurityGroup(ctx, c.ProjectID(), c.Region(), id).Execute() + if err != nil { + return nil, shared.ClassifySDKError("get security group", err) + } + return securityGroupFromSDK(securityGroup), nil +} + // FindByTags finds a single STACKIT security group matching the given tag selector. func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*SecurityGroup, error) { return shared.FindByTags(ctx, tags, c.ListByTags, "security group", "security groups") @@ -199,6 +211,15 @@ func (c *Client) listRulesIfGroupExists(ctx context.Context, id string) ([]iaas. } func (c *Client) ensureSecurityGroup(ctx context.Context, spec SecurityGroupSpec) (*SecurityGroup, error) { + if spec.ID != "" { + securityGroup, err := c.Get(ctx, spec.ID) + if err == nil { + return securityGroup, nil + } + if !shared.IsNotFound(err) { + return nil, err + } + } if existing, err := c.FindByTags(ctx, spec.Tags); err == nil { return existing, nil } else if !shared.IsNotFound(err) { diff --git a/cloud/services/securitygroup/reconcile_test.go b/cloud/services/securitygroup/reconcile_test.go index 3c44ad0..9662e01 100644 --- a/cloud/services/securitygroup/reconcile_test.go +++ b/cloud/services/securitygroup/reconcile_test.go @@ -177,6 +177,90 @@ func TestServiceReconcile(t *testing.T) { t.Fatalf("second rule payload = %#v", createdRulePayloads[1]) } }) +} + +func TestServiceReconcileReuseAndErrors(t *testing.T) { + t.Run("reuses existing security group found by ID without listing by tags", func(t *testing.T) { + var recordedID string + specs := []securitygroup.SecurityGroupSpec{{ + ID: testSecurityGroupID, + Name: "cluster-sg", + Tags: map[string]string{"cluster": "test"}, + SetID: func(id string) { + recordedID = id + }, + }} + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID): + writeJSON(t, w, map[string]any{ + "id": testSecurityGroupID, + "name": "cluster-sg", + }) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + writeJSON(t, w, map[string]any{"items": []any{}}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if recordedID != testSecurityGroupID { + t.Fatalf("recordedID = %q, want %q", recordedID, testSecurityGroupID) + } + }) + + t.Run("falls back to tag lookup when recorded ID is not found during reconcile", func(t *testing.T) { + var ( + getByIDCalled bool + recordedID string + ) + staleID := "99999999-9999-4999-8999-999999999999" + specs := []securitygroup.SecurityGroupSpec{{ + ID: staleID, + Name: "cluster-sg", + Tags: map[string]string{"cluster": "test"}, + SetID: func(id string) { + recordedID = id + }, + }} + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+staleID): + getByIDCalled = true + w.WriteHeader(http.StatusNotFound) + writeJSON(t, w, map[string]any{"message": "not found"}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{ + "id": testSecurityGroupID, + "name": "cluster-sg", + "labels": map[string]any{"cluster": "test"}, + }, + }}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + writeJSON(t, w, map[string]any{"items": []any{}}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) + } + if !getByIDCalled { + t.Fatal("expected GET by spec.ID before falling back to tag lookup") + } + if recordedID != testSecurityGroupID { + t.Fatalf("recordedID = %q, want %q", recordedID, testSecurityGroupID) + } + }) t.Run("reuses existing security group found by tags without creating duplicate", func(t *testing.T) { specs := []securitygroup.SecurityGroupSpec{{ diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index c4e6515..97b65de 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -255,7 +255,7 @@ func (s *ClusterScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec { func (s *ClusterScope) AvailabilityZoneSpec() availabilityzone.AvailabilityZoneSpec { return availabilityzone.AvailabilityZoneSpec{ SetZones: func(zones []string) { - s.SetFailureDomains(failureDomainsFromZones(s.StackitCluster.Spec.Region, zones)) + s.SetFailureDomains(failureDomainsFromZones(s.Region(), zones)) }, } } diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index d4fcfd3..e49fb83 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -16,6 +16,7 @@ import ( clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" + cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" ) func TestDefaultAvailabilityZone(t *testing.T) { @@ -66,3 +67,20 @@ func TestDefaultAvailabilityZone(t *testing.T) { }) } } + +func TestAvailabilityZoneSpecUsesResolvedRegion(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{}, + CloudClient: cloudfake.New(), + } + + clusterScope.AvailabilityZoneSpec().SetZones([]string{"eu01-1"}) + + if len(clusterScope.StackitCluster.Status.FailureDomains) != 1 { + t.Fatalf("FailureDomains = %#v, want 1 entry", clusterScope.StackitCluster.Status.FailureDomains) + } + gotRegion := clusterScope.StackitCluster.Status.FailureDomains[0].Attributes["region"] + if gotRegion != "eu01" { + t.Fatalf("FailureDomains[0].Attributes[region] = %q, want %q", gotRegion, "eu01") + } +} From 0f1b477441e3b5f6eef364a09fe27963ea524066 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Thu, 12 Nov 2026 18:34:39 +0100 Subject: [PATCH 17/60] fix: adjusted logmessages --- controller/stackitcluster_infrastructure.go | 2 +- controller/stackitmachine_infrastructure.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index 65f1eaa..fe6eac7 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -265,7 +265,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS } clusterScope.SetReady() - log.V(1).Info("StackitCluster ready", "endpoint", stackitCluster.Status.APIServerEndpoint) + log.V(1).Info("Reconciled StackitCluster", "endpoint", stackitCluster.Status.APIServerEndpoint) return ctrl.Result{}, nil } diff --git a/controller/stackitmachine_infrastructure.go b/controller/stackitmachine_infrastructure.go index 1d11b87..fad081f 100644 --- a/controller/stackitmachine_infrastructure.go +++ b/controller/stackitmachine_infrastructure.go @@ -176,7 +176,7 @@ func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineS } machineScope.SetReady() - log.V(1).Info("StackitMachine ready", "providerID", stackitMachine.Status.ProviderID) + log.V(1).Info("Reconciled StackitMachine", "providerID", stackitMachine.Status.ProviderID) return ctrl.Result{}, nil } From a43f5479ac6e2f40716206724193c8c9ed67085f Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Thu, 12 Nov 2026 19:20:36 +0100 Subject: [PATCH 18/60] refactor: unexport internal methods and fields in scope and cloud services --- cloud/cleanup_test.go | 35 ++++++-- cloud/sdk_client_integration_test.go | 24 +++++- cloud/services/availabilityzone/reconcile.go | 6 +- cloud/services/availabilityzone/service.go | 22 ++--- cloud/services/loadbalancer/reconcile.go | 26 +++--- cloud/services/loadbalancer/reconcile_test.go | 83 +++++++++++-------- cloud/services/loadbalancer/service.go | 24 +++--- cloud/services/loadbalancer/targets.go | 6 +- cloud/services/network/reconcile.go | 8 +- cloud/services/network/service.go | 18 ++-- cloud/services/publicip/reconcile.go | 50 +++++------ cloud/services/publicip/service.go | 10 +-- cloud/services/securitygroup/reconcile.go | 44 +++++----- .../services/securitygroup/reconcile_test.go | 28 +++++-- cloud/services/securitygroup/rules.go | 10 +-- cloud/services/securitygroup/service.go | 10 +-- cloud/services/server/reconcile.go | 24 +++--- cloud/services/server/reconcile_test.go | 14 +++- cloud/services/server/service.go | 18 ++-- cloud/services/shared/errors_test.go | 14 ++-- cloud/services/shared/utils.go | 6 +- controller/stackitcluster_controller_test.go | 22 +++-- scope/cluster_scope.go | 52 ++++++------ scope/cluster_scope_test.go | 2 +- scope/machine_scope.go | 41 +++++---- 25 files changed, 335 insertions(+), 262 deletions(-) diff --git a/cloud/cleanup_test.go b/cloud/cleanup_test.go index 81fdd5f..bb2bc6c 100644 --- a/cloud/cleanup_test.go +++ b/cloud/cleanup_test.go @@ -22,6 +22,33 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" ) +type testServerScope struct { + cloud.Client + spec server.ServerSpec +} + +func (s *testServerScope) ServerSpec() server.ServerSpec { + return s.spec +} + +func createTestServer(t *testing.T, client cloud.Client, name string, tags map[string]string) { + t.Helper() + + err := server.New(&testServerScope{ + Client: client, + spec: server.ServerSpec{ + Name: name, + ImageID: "22222222-2222-4222-8222-222222222222", + MachineType: "c2i.2", + NetworkID: "11111111-1111-4111-8111-111111111111", + Tags: tags, + }, + }).Reconcile(context.Background()) + if err != nil { + t.Fatalf("server.Reconcile() error = %v", err) + } +} + func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { ctx := context.Background() client := cloudfake.New() @@ -39,12 +66,8 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { pubIPClient := publicip.NewClient(client) sgClient := securitygroup.NewClient(client) - if _, err := srvClient.Create(ctx, server.ServerSpec{Name: "matched", Tags: tags}); err != nil { - t.Fatalf("server.Create() error = %v", err) - } - if _, err := srvClient.Create(ctx, server.ServerSpec{Name: "other", Tags: otherTags}); err != nil { - t.Fatalf("server.Create() error = %v", err) - } + createTestServer(t, client, "matched", tags) + createTestServer(t, client, "other", otherTags) if _, err := client.CreatePublicIP(ctx, tags); err != nil { t.Fatalf("CreatePublicIP() error = %v", err) } diff --git a/cloud/sdk_client_integration_test.go b/cloud/sdk_client_integration_test.go index 82fba4c..1b52e10 100644 --- a/cloud/sdk_client_integration_test.go +++ b/cloud/sdk_client_integration_test.go @@ -100,11 +100,27 @@ func TestSDKClientLoadBalancerTargetIntegration(t *testing.T) { {Name: "capistackit-initial-target", IP: initialIP}, {Name: "capistackit-integration-target", IP: targetIP}, } - if err := lbClient.SetTargets(context.Background(), lbID, 6443, targets); err != nil { - t.Fatalf("SetTargets() error = %v", err) + scope := &integrationLBScope{ + Client: client, + spec: loadbalancer.LoadBalancerSpec{ + ID: lbID, + Name: lbID, + NetworkID: networkID, + Port: 6443, + Targets: targets, + Tags: map[string]string{ + "capistackit-test": "true", + "capistackit-name": lbID, + }, + }, + } + svc := loadbalancer.New(scope) + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("loadbalancer.Reconcile() error = %v", err) } - if err := lbClient.SetTargets(context.Background(), lbID, 6443, targets[:1]); err != nil { - t.Fatalf("SetTargets() shrink error = %v", err) + scope.spec.Targets = targets[:1] + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("loadbalancer.Reconcile() shrink error = %v", err) } } diff --git a/cloud/services/availabilityzone/reconcile.go b/cloud/services/availabilityzone/reconcile.go index 9579752..4d7d2e6 100644 --- a/cloud/services/availabilityzone/reconcile.go +++ b/cloud/services/availabilityzone/reconcile.go @@ -21,7 +21,7 @@ import ( func (s *Service) Reconcile(ctx context.Context) error { log := logf.FromContext(ctx) - zones, err := s.List(ctx) + zones, err := s.client.list(ctx) if err != nil { return err } @@ -33,8 +33,8 @@ func (s *Service) Reconcile(ctx context.Context) error { return nil } -// List retrieves the availability zones for the configured region. -func (c *Client) List(ctx context.Context) ([]string, error) { +// list retrieves the availability zones for the configured region. +func (c *client) list(ctx context.Context) ([]string, error) { if c.region == "" { return nil, fmt.Errorf("%w: region is empty", shared.ErrInvalidInput) } diff --git a/cloud/services/availabilityzone/service.go b/cloud/services/availabilityzone/service.go index 8e79943..1b37e07 100644 --- a/cloud/services/availabilityzone/service.go +++ b/cloud/services/availabilityzone/service.go @@ -17,27 +17,27 @@ type AvailabilityZoneSpec struct { SetZones func(zones []string) } -// ClientScope is an interface that provides the IaaS client and target region for availability zone operations. -type ClientScope interface { +// clientScope is an interface that provides the IaaS client and target region for availability zone operations. +type clientScope interface { Region() string IaaSClient() *iaas.APIClient } // Scope is an interface that provides scope data needed for availability zone reconciliation. type Scope interface { - ClientScope + clientScope AvailabilityZoneSpec() AvailabilityZoneSpec } -// Client provides direct availability zone operations against the STACKIT IaaS API. -type Client struct { +// client provides direct availability zone operations against the STACKIT IaaS API. +type client struct { iaasClient *iaas.APIClient region string } -// NewClient returns a Client for the given scope. -func NewClient(scope ClientScope) *Client { - return &Client{ +// newClient returns a client for the given scope. +func newClient(scope clientScope) *client { + return &client{ iaasClient: scope.IaaSClient(), region: scope.Region(), } @@ -45,14 +45,14 @@ func NewClient(scope ClientScope) *Client { // Service implements availability zones reconciler. type Service struct { - scope Scope - *Client + scope Scope + client *client } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ scope: scope, - Client: NewClient(scope), + client: newClient(scope), } } diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index 401023f..3120f7e 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -28,7 +28,7 @@ func (s *Service) Reconcile(ctx context.Context) error { return err } - loadBalancer, err := s.ensureLoadBalancer(ctx, spec) + loadBalancer, err := s.client.ensureLoadBalancer(ctx, spec) if err != nil { return err } @@ -45,8 +45,8 @@ func (s *Service) Reconcile(ctx context.Context) error { // An empty set never overwrites a populated pool: cluster deletion filters // out every machine while the API server is still needed for drain. if len(spec.Targets) > 0 { - if err := s.SetTargets(ctx, loadBalancer.ID, spec.Port, spec.Targets); err != nil { - return fmt.Errorf("%w: %w", ErrTargetPoolUpdate, err) + if err := s.client.setTargets(ctx, loadBalancer.ID, spec.Port, spec.Targets); err != nil { + return fmt.Errorf("%w: %w", errTargetPoolUpdate, err) } log.V(1).Info("Reconciled API server load balancer target pool", "targets", len(spec.Targets)) } @@ -61,12 +61,12 @@ func (s *Service) Delete(ctx context.Context) error { return fmt.Errorf("%w: load balancer ID or tags are required", shared.ErrInvalidInput) } - loadBalancerID, err := s.resolveLoadBalancerForDelete(ctx, spec) + loadBalancerID, err := s.client.resolveLoadBalancerForDelete(ctx, spec) if err != nil { return err } if loadBalancerID != "" { - if err := s.DeleteByID(ctx, loadBalancerID); err != nil { + if err := s.client.DeleteByID(ctx, loadBalancerID); err != nil { return err } log.V(1).Info("Deleted load balancer", "loadBalancerID", loadBalancerID) @@ -77,8 +77,8 @@ func (s *Service) Delete(ctx context.Context) error { return nil } -// Get retrieves a STACKIT load balancer by ID. -func (c *Client) Get(ctx context.Context, id string) (*LoadBalancer, error) { +// get retrieves a STACKIT load balancer by ID. +func (c *Client) get(ctx context.Context, id string) (*LoadBalancer, error) { if id == "" { return nil, fmt.Errorf("%w: load balancer ID is empty", shared.ErrInvalidInput) } @@ -89,8 +89,8 @@ func (c *Client) Get(ctx context.Context, id string) (*LoadBalancer, error) { return loadBalancerFromSDK(loadBalancer), nil } -// FindByTags finds a single STACKIT load balancer matching the given tag selector. -func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*LoadBalancer, error) { +// findByTags finds a single STACKIT load balancer matching the given tag selector. +func (c *Client) findByTags(ctx context.Context, tags map[string]string) (*LoadBalancer, error) { return shared.FindByTags(ctx, tags, c.ListByTags, "load balancer", "load balancers") } @@ -134,7 +134,7 @@ func (c *Client) DeleteByID(ctx context.Context, id string) error { func (c *Client) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) (*LoadBalancer, error) { if spec.ID != "" { - loadBalancer, err := c.Get(ctx, spec.ID) + loadBalancer, err := c.get(ctx, spec.ID) if err == nil { return loadBalancer, nil } @@ -142,7 +142,7 @@ func (c *Client) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) return nil, err } } - if existing, err := c.FindByTags(ctx, spec.Tags); err == nil { + if existing, err := c.findByTags(ctx, spec.Tags); err == nil { return existing, nil } else if !shared.IsNotFound(err) && !isLoadBalancerServiceNotEnabled(err) { return nil, err @@ -170,7 +170,7 @@ func (c *Client) resolveLoadBalancerForDelete(ctx context.Context, spec LoadBala if len(spec.Tags) == 0 { return "", nil } - loadBalancer, err := c.FindByTags(ctx, spec.Tags) + loadBalancer, err := c.findByTags(ctx, spec.Tags) if shared.IsNotFound(err) { return "", nil } @@ -181,7 +181,7 @@ func (c *Client) resolveLoadBalancerForDelete(ctx context.Context, spec LoadBala } func (c *Client) loadBalancerExists(ctx context.Context, id string) (bool, error) { - _, err := c.Get(ctx, id) + _, err := c.get(ctx, id) if err != nil { if shared.IsNotFound(err) { return false, nil diff --git a/cloud/services/loadbalancer/reconcile_test.go b/cloud/services/loadbalancer/reconcile_test.go index 6aea256..04c2ad1 100644 --- a/cloud/services/loadbalancer/reconcile_test.go +++ b/cloud/services/loadbalancer/reconcile_test.go @@ -429,63 +429,78 @@ func TestSetTargets(t *testing.T) { } var updatePayloads []map[string]any - svc := newTestService(t, loadbalancer.LoadBalancerSpec{}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - mu.Lock() - defer mu.Unlock() - - switch { - case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/load-balancers/"+testLBName): - writeJSON(t, w, sdkLoadBalancerJSON(testLBName, nil, []any{ - map[string]any{"name": "apiserver", "targetPort": 6443, "targets": targets}, - })) - case r.Method == http.MethodPut && strings.HasSuffix(r.URL.Path, "/load-balancers/"+testLBName+"/target-pools/apiserver"): - payload := readJSON(t, r) - updatePayloads = append(updatePayloads, payload) - next, ok := payload["targets"].([]any) - if !ok { - t.Fatalf("update target payload missing targets: %#v", payload) + scope := &testScope{ + projectID: testProjectID, + region: testRegion, + lbClient: newTestLBClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + defer mu.Unlock() + + switch { + case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/load-balancers/"+testLBName): + writeJSON(t, w, sdkLoadBalancerJSON(testLBName, nil, []any{ + map[string]any{"name": "apiserver", "targetPort": 6443, "targets": targets}, + })) + case r.Method == http.MethodPut && strings.HasSuffix(r.URL.Path, "/load-balancers/"+testLBName+"/target-pools/apiserver"): + payload := readJSON(t, r) + updatePayloads = append(updatePayloads, payload) + next, ok := payload["targets"].([]any) + if !ok { + t.Fatalf("update target payload missing targets: %#v", payload) + } + targets = next + writeJSON(t, w, map[string]any{ + "name": "apiserver", + "targetPort": 6443, + "targets": targets, + }) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) } - targets = next - writeJSON(t, w, map[string]any{ - "name": "apiserver", - "targetPort": 6443, - "targets": targets, - }) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - } - })) + })), + spec: loadbalancer.LoadBalancerSpec{ + ID: testLBName, + Name: testLBName, + NetworkID: testNetworkID, + Port: 6443, + Tags: map[string]string{"cluster": "test"}, + }, + } + svc := loadbalancer.New(scope) desired := []loadbalancer.Target{ {Name: "cp-0", IP: "10.0.0.10"}, {Name: "cp-1", IP: "10.0.0.11"}, } - if err := svc.SetTargets(context.Background(), testLBName, 6443, desired); err != nil { - t.Fatalf("SetTargets() error = %v", err) + scope.spec.Targets = desired + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() error = %v", err) } if len(updatePayloads) != 1 { t.Fatalf("got %d update payloads, want 1", len(updatePayloads)) } // Unchanged pool must not trigger another write. - if err := svc.SetTargets(context.Background(), testLBName, 6443, desired); err != nil { - t.Fatalf("SetTargets() repeat error = %v", err) + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() repeat error = %v", err) } if len(updatePayloads) != 1 { t.Fatalf("got %d update payloads after unchanged set, want 1", len(updatePayloads)) } // Shrinking pool updates targets. - if err := svc.SetTargets(context.Background(), testLBName, 6443, desired[:1]); err != nil { - t.Fatalf("SetTargets() shrink error = %v", err) + scope.spec.Targets = desired[:1] + if err := svc.Reconcile(context.Background()); err != nil { + t.Fatalf("Reconcile() shrink error = %v", err) } if len(updatePayloads) != 2 { t.Fatalf("got %d update payloads, want 2", len(updatePayloads)) } - // Empty pool is rejected. - if err := svc.SetTargets(context.Background(), testLBName, 6443, nil); !errors.Is(err, shared.ErrInvalidInput) { - t.Fatalf("SetTargets(nil) error = %v, want ErrInvalidInput", err) + // Target missing name or IP is rejected. + scope.spec.Targets = []loadbalancer.Target{{Name: "", IP: "10.0.0.10"}} + if err := svc.Reconcile(context.Background()); !errors.Is(err, shared.ErrInvalidInput) { + t.Fatalf("Reconcile(invalid target) error = %v, want ErrInvalidInput", err) } } diff --git a/cloud/services/loadbalancer/service.go b/cloud/services/loadbalancer/service.go index 435e03b..29998f0 100644 --- a/cloud/services/loadbalancer/service.go +++ b/cloud/services/loadbalancer/service.go @@ -20,21 +20,19 @@ const ( // DefaultAPIServerPort is the default TCP port for the Kubernetes API server. DefaultAPIServerPort int32 = 6443 - // DefaultBootstrapTargetIP is the placeholder IP used when no network prefix or target IP is available. - DefaultBootstrapTargetIP = "10.0.0.1" - - apiserverTargetPoolName = "apiserver" - apiserverListenerName = "apiserver" - bootstrapTargetName = "capi-bootstrap-placeholder" + defaultBootstrapTargetIP = "10.0.0.1" + apiserverTargetPoolName = "apiserver" + apiserverListenerName = "apiserver" + bootstrapTargetName = "capi-bootstrap-placeholder" ) -// ErrTargetPoolUpdate indicates that the load balancer was created/resolved +// errTargetPoolUpdate indicates that the load balancer was created/resolved // successfully, but updating its target pool failed. -var ErrTargetPoolUpdate = errors.New("load balancer target pool update failed") +var errTargetPoolUpdate = errors.New("load balancer target pool update failed") -// IsTargetPoolUpdateError reports whether err wraps ErrTargetPoolUpdate. +// IsTargetPoolUpdateError reports whether err wraps errTargetPoolUpdate. func IsTargetPoolUpdateError(err error) bool { - return errors.Is(err, ErrTargetPoolUpdate) + return errors.Is(err, errTargetPoolUpdate) } // LoadBalancer describes an API-server load balancer. @@ -95,14 +93,14 @@ func NewClient(scope ClientScope) *Client { // Service implements load balancer reconciler. type Service struct { - scope Scope - *Client + scope Scope + client *Client } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ scope: scope, - Client: NewClient(scope), + client: NewClient(scope), } } diff --git a/cloud/services/loadbalancer/targets.go b/cloud/services/loadbalancer/targets.go index 6f3d65c..4e996d6 100644 --- a/cloud/services/loadbalancer/targets.go +++ b/cloud/services/loadbalancer/targets.go @@ -36,7 +36,7 @@ const ( // BootstrapTarget seeds a new load balancer; STACKIT rejects an empty pool. func BootstrapTarget(ip string) Target { if ip == "" { - ip = DefaultBootstrapTargetIP + ip = defaultBootstrapTargetIP } return Target{ Name: bootstrapTargetName, @@ -112,8 +112,8 @@ func firstInternalIP(addresses []clusterv1.MachineAddress) string { return "" } -// SetTargets replaces the contents of the API-server target pool. STACKIT rejects an empty pool. -func (c *Client) SetTargets( +// setTargets replaces the contents of the API-server target pool. STACKIT rejects an empty pool. +func (c *Client) setTargets( ctx context.Context, loadBalancerID string, port int32, diff --git a/cloud/services/network/reconcile.go b/cloud/services/network/reconcile.go index e9e194a..20bbe73 100644 --- a/cloud/services/network/reconcile.go +++ b/cloud/services/network/reconcile.go @@ -26,7 +26,7 @@ func (s *Service) Reconcile(ctx context.Context) error { return err } - net, err := s.Get(ctx, spec.ID) + net, err := s.client.get(ctx, spec.ID) if err != nil { return err } @@ -37,12 +37,12 @@ func (s *Service) Reconcile(ctx context.Context) error { return nil } -// Get retrieves a STACKIT network by ID. -func (c *Client) Get(ctx context.Context, id string) (*Network, error) { +// get retrieves a STACKIT network by ID. +func (c *client) get(ctx context.Context, id string) (*Network, error) { if id == "" { return nil, fmt.Errorf("%w: network ID is empty", shared.ErrInvalidInput) } - net, err := c.IaaSClient().DefaultAPI.GetNetwork(ctx, c.ProjectID(), c.Region(), id).Execute() + net, err := c.scope.IaaSClient().DefaultAPI.GetNetwork(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute() if err != nil { return nil, shared.ClassifySDKError("get network", err) } diff --git a/cloud/services/network/service.go b/cloud/services/network/service.go index ed1aa06..f2d4de8 100644 --- a/cloud/services/network/service.go +++ b/cloud/services/network/service.go @@ -31,26 +31,26 @@ type Scope interface { NetworkSpec() NetworkSpec } -// Client provides direct network operations against the STACKIT IaaS API. -type Client struct { - shared.IaaSScope +// client provides direct network operations against the STACKIT IaaS API. +type client struct { + scope shared.IaaSScope } -// NewClient returns a Client for the given scope. -func NewClient(scope shared.IaaSClientScope) *Client { - return &Client{IaaSScope: shared.NewIaaSScope(scope)} +// newClient returns a client for the given scope. +func newClient(scope shared.IaaSClientScope) *client { + return &client{scope: shared.NewIaaSScope(scope)} } // Service implements network reconciler. type Service struct { - scope Scope - *Client + scope Scope + client *client } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ scope: scope, - Client: NewClient(scope), + client: newClient(scope), } } diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index ea00ea5..b9c023f 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -28,16 +28,16 @@ func (s *Service) Reconcile(ctx context.Context) error { return err } - publicIP, err := s.ensurePublicIP(ctx, spec) + publicIP, err := s.client.ensurePublicIP(ctx, spec) if err != nil { return err } if spec.ServerID != "" && publicIP.NetworkInterfaceID == "" { - if err := s.AttachToServer(ctx, spec.ServerID, publicIP.ID); err != nil { + if err := s.client.attachToServer(ctx, spec.ServerID, publicIP.ID); err != nil { return err } - publicIP, err = s.Get(ctx, publicIP.ID) + publicIP, err = s.client.get(ctx, publicIP.ID) if err != nil { return err } @@ -57,7 +57,7 @@ func (s *Service) Delete(ctx context.Context) error { return fmt.Errorf("%w: public IP ID or tags are required", shared.ErrInvalidInput) } - publicIP, err := s.resolvePublicIP(ctx, spec) + publicIP, err := s.client.resolvePublicIP(ctx, spec) if err != nil { return err } @@ -68,17 +68,17 @@ func (s *Service) Delete(ctx context.Context) error { return nil } - serverID, err := s.resolveServerID(ctx, spec) + serverID, err := s.client.resolveServerID(ctx, spec) if err != nil { return err } if serverID != "" { - if err := s.DetachFromServer(ctx, serverID, publicIP.ID); err != nil { + if err := s.client.DetachFromServer(ctx, serverID, publicIP.ID); err != nil { return err } } - if err := s.DeleteByID(ctx, publicIP.ID); err != nil { + if err := s.client.DeleteByID(ctx, publicIP.ID); err != nil { return err } log.V(1).Info("Deleted public IP", "publicIPID", publicIP.ID) @@ -89,20 +89,20 @@ func (s *Service) Delete(ctx context.Context) error { return nil } -// Get retrieves a STACKIT public IP by ID. -func (c *Client) Get(ctx context.Context, id string) (*PublicIP, error) { +// get retrieves a STACKIT public IP by ID. +func (c *Client) get(ctx context.Context, id string) (*PublicIP, error) { if id == "" { return nil, fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) } - publicIP, err := c.IaaSClient().DefaultAPI.GetPublicIP(ctx, c.ProjectID(), c.Region(), id).Execute() + publicIP, err := c.scope.IaaSClient().DefaultAPI.GetPublicIP(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute() if err != nil { return nil, shared.ClassifySDKError("get public IP", err) } return publicIPFromSDK(publicIP), nil } -// FindByTags finds a single STACKIT public IP matching the given tag selector. -func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*PublicIP, error) { +// findByTags finds a single STACKIT public IP matching the given tag selector. +func (c *Client) findByTags(ctx context.Context, tags map[string]string) (*PublicIP, error) { return shared.FindByTags(ctx, tags, c.ListByTags, "public IP", "public IPs") } @@ -111,7 +111,7 @@ func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*Pub if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } - resp, err := c.IaaSClient().DefaultAPI.ListPublicIPs(ctx, c.ProjectID(), c.Region()). + resp, err := c.scope.IaaSClient().DefaultAPI.ListPublicIPs(ctx, c.scope.ProjectID(), c.scope.Region()). LabelSelector(shared.LabelSelector(tags)). Execute() if err != nil { @@ -127,13 +127,13 @@ func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*Pub return matched, nil } -// AttachToServer attaches a public IP to a server idempotently, tolerating Conflict errors. -func (c *Client) AttachToServer(ctx context.Context, serverID, publicIPID string) error { +// attachToServer attaches a public IP to a server idempotently, tolerating Conflict errors. +func (c *Client) attachToServer(ctx context.Context, serverID, publicIPID string) error { if serverID == "" || publicIPID == "" { return fmt.Errorf("%w: server ID and public IP ID are required", shared.ErrInvalidInput) } - if err := c.IaaSClient().DefaultAPI. - AddPublicIpToServer(ctx, c.ProjectID(), c.Region(), serverID, publicIPID). + if err := c.scope.IaaSClient().DefaultAPI. + AddPublicIpToServer(ctx, c.scope.ProjectID(), c.scope.Region(), serverID, publicIPID). Execute(); err != nil { err := shared.ClassifySDKError("add public IP to server", err) if !shared.IsConflict(err) { @@ -149,8 +149,8 @@ func (c *Client) DetachFromServer(ctx context.Context, serverID, publicIPID stri if serverID == "" || publicIPID == "" { return fmt.Errorf("%w: server ID and public IP ID are required", shared.ErrInvalidInput) } - if err := c.IaaSClient().DefaultAPI. - RemovePublicIpFromServer(ctx, c.ProjectID(), c.Region(), serverID, publicIPID). + if err := c.scope.IaaSClient().DefaultAPI. + RemovePublicIpFromServer(ctx, c.scope.ProjectID(), c.scope.Region(), serverID, publicIPID). Execute(); err != nil { err := shared.ClassifySDKError("remove public IP from server", err) if !shared.IsNotFound(err) && !shared.IsConflict(err) { @@ -166,7 +166,7 @@ func (c *Client) DeleteByID(ctx context.Context, id string) error { if id == "" { return fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) } - if err := c.IaaSClient().DefaultAPI.DeletePublicIP(ctx, c.ProjectID(), c.Region(), id).Execute(); err != nil { + if err := c.scope.IaaSClient().DefaultAPI.DeletePublicIP(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute(); err != nil { err := shared.ClassifySDKError("delete public IP", err) if !shared.IsNotFound(err) { return err @@ -188,8 +188,8 @@ func (c *Client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*Public if len(spec.Tags) > 0 { payload.SetLabels(shared.TagsToSDKLabels(spec.Tags)) } - created, err := c.IaaSClient().DefaultAPI. - CreatePublicIP(ctx, c.ProjectID(), c.Region()). + created, err := c.scope.IaaSClient().DefaultAPI. + CreatePublicIP(ctx, c.scope.ProjectID(), c.scope.Region()). CreatePublicIPPayload(*payload). Execute() if err != nil { @@ -202,7 +202,7 @@ func (c *Client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*Public func (c *Client) resolvePublicIP(ctx context.Context, spec PublicIPSpec) (*PublicIP, error) { if spec.ID != "" { - publicIP, err := c.Get(ctx, spec.ID) + publicIP, err := c.get(ctx, spec.ID) if err == nil { return publicIP, nil } @@ -213,7 +213,7 @@ func (c *Client) resolvePublicIP(ctx context.Context, spec PublicIPSpec) (*Publi if len(spec.Tags) == 0 { return nil, nil } - publicIP, err := c.FindByTags(ctx, spec.Tags) + publicIP, err := c.findByTags(ctx, spec.Tags) if shared.IsNotFound(err) { return nil, nil } @@ -230,7 +230,7 @@ func (c *Client) resolveServerID(ctx context.Context, spec PublicIPSpec) (string if len(spec.ServerSelector) == 0 { return "", nil } - srv, err := server.NewClient(c).FindByTags(ctx, spec.ServerSelector) + srv, err := server.NewClient(c.scope).FindByTags(ctx, spec.ServerSelector) if err != nil { if shared.IsNotFound(err) { return "", nil diff --git a/cloud/services/publicip/service.go b/cloud/services/publicip/service.go index 4556cca..f2abab7 100644 --- a/cloud/services/publicip/service.go +++ b/cloud/services/publicip/service.go @@ -36,24 +36,24 @@ type Scope interface { // Client provides direct public IP operations against the STACKIT IaaS API. type Client struct { - shared.IaaSScope + scope shared.IaaSScope } // NewClient returns a Client for the given scope. func NewClient(scope shared.IaaSClientScope) *Client { - return &Client{IaaSScope: shared.NewIaaSScope(scope)} + return &Client{scope: shared.NewIaaSScope(scope)} } // Service implements public IPs reconciler. type Service struct { - scope Scope - *Client + scope Scope + client *Client } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ scope: scope, - Client: NewClient(scope), + client: NewClient(scope), } } diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index 0a093db..7c3058c 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -25,7 +25,7 @@ import ( func (s *Service) Reconcile(ctx context.Context) error { log := logf.FromContext(ctx) for _, spec := range s.scope.SecurityGroupSpecs() { - if err := s.reconcileSecurityGroup(ctx, spec); err != nil { + if err := s.client.reconcileSecurityGroup(ctx, spec); err != nil { return err } } @@ -36,7 +36,7 @@ func (s *Service) Reconcile(ctx context.Context) error { func (s *Service) Delete(ctx context.Context) error { log := logf.FromContext(ctx) for _, spec := range slices.Backward(s.scope.SecurityGroupSpecs()) { - if err := s.deleteSecurityGroup(ctx, spec); err != nil { + if err := s.client.deleteSecurityGroup(ctx, spec); err != nil { return err } } @@ -44,20 +44,20 @@ func (s *Service) Delete(ctx context.Context) error { return nil } -// Get retrieves a STACKIT security group by ID. -func (c *Client) Get(ctx context.Context, id string) (*SecurityGroup, error) { +// get retrieves a STACKIT security group by ID. +func (c *Client) get(ctx context.Context, id string) (*SecurityGroup, error) { if id == "" { return nil, fmt.Errorf("%w: security group ID is empty", shared.ErrInvalidInput) } - securityGroup, err := c.IaaSClient().DefaultAPI.GetSecurityGroup(ctx, c.ProjectID(), c.Region(), id).Execute() + securityGroup, err := c.scope.IaaSClient().DefaultAPI.GetSecurityGroup(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute() if err != nil { return nil, shared.ClassifySDKError("get security group", err) } return securityGroupFromSDK(securityGroup), nil } -// FindByTags finds a single STACKIT security group matching the given tag selector. -func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*SecurityGroup, error) { +// findByTags finds a single STACKIT security group matching the given tag selector. +func (c *Client) findByTags(ctx context.Context, tags map[string]string) (*SecurityGroup, error) { return shared.FindByTags(ctx, tags, c.ListByTags, "security group", "security groups") } @@ -66,7 +66,7 @@ func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*Sec if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } - resp, err := c.IaaSClient().DefaultAPI.ListSecurityGroups(ctx, c.ProjectID(), c.Region()). + resp, err := c.scope.IaaSClient().DefaultAPI.ListSecurityGroups(ctx, c.scope.ProjectID(), c.scope.Region()). LabelSelector(shared.LabelSelector(tags)). Execute() if err != nil { @@ -94,14 +94,14 @@ func (c *Client) DeleteByID(ctx context.Context, id string) error { return c.deleteSecurityGroupWithRules(ctx, id, rules) } -// AttachToServer attaches a security group to a server idempotently. +// attachToServer attaches a security group to a server idempotently. // Three error responses are tolerated: // - conflict: already attached // - invalid input: duplicate item in security_groups list // - not found: server has no network port yet -func (c *Client) AttachToServer(ctx context.Context, serverID, securityGroupID string) error { - if err := c.IaaSClient().DefaultAPI. - AddSecurityGroupToServer(ctx, c.ProjectID(), c.Region(), serverID, securityGroupID). +func (c *Client) attachToServer(ctx context.Context, serverID, securityGroupID string) error { + if err := c.scope.IaaSClient().DefaultAPI. + AddSecurityGroupToServer(ctx, c.scope.ProjectID(), c.scope.Region(), serverID, securityGroupID). Execute(); err != nil { err := shared.ClassifySDKError("add security group to server", err) if !shared.IsConflict(err) && !shared.IsInvalidInput(err) && !shared.IsNotFound(err) { @@ -114,8 +114,8 @@ func (c *Client) AttachToServer(ctx context.Context, serverID, securityGroupID s // DetachFromServer detaches a security group from a server, ignoring NotFound and Conflict errors. func (c *Client) DetachFromServer(ctx context.Context, serverID, securityGroupID string) error { - if err := c.IaaSClient().DefaultAPI. - RemoveSecurityGroupFromServer(ctx, c.ProjectID(), c.Region(), serverID, securityGroupID). + if err := c.scope.IaaSClient().DefaultAPI. + RemoveSecurityGroupFromServer(ctx, c.scope.ProjectID(), c.scope.Region(), serverID, securityGroupID). Execute(); err != nil { err := shared.ClassifySDKError("remove security group from server", err) if !shared.IsNotFound(err) && !shared.IsConflict(err) { @@ -144,7 +144,7 @@ func (c *Client) reconcileSecurityGroup(ctx context.Context, spec SecurityGroupS if serverID == "" { continue } - if err := c.AttachToServer(ctx, serverID, securityGroup.ID); err != nil { + if err := c.attachToServer(ctx, serverID, securityGroup.ID); err != nil { return err } } @@ -188,7 +188,7 @@ func (c *Client) resolveSecurityGroupForDelete( if len(spec.Tags) == 0 { return "", nil, nil } - securityGroup, err := c.FindByTags(ctx, spec.Tags) + securityGroup, err := c.findByTags(ctx, spec.Tags) if shared.IsNotFound(err) { return "", nil, nil } @@ -212,7 +212,7 @@ func (c *Client) listRulesIfGroupExists(ctx context.Context, id string) ([]iaas. func (c *Client) ensureSecurityGroup(ctx context.Context, spec SecurityGroupSpec) (*SecurityGroup, error) { if spec.ID != "" { - securityGroup, err := c.Get(ctx, spec.ID) + securityGroup, err := c.get(ctx, spec.ID) if err == nil { return securityGroup, nil } @@ -220,12 +220,12 @@ func (c *Client) ensureSecurityGroup(ctx context.Context, spec SecurityGroupSpec return nil, err } } - if existing, err := c.FindByTags(ctx, spec.Tags); err == nil { + if existing, err := c.findByTags(ctx, spec.Tags); err == nil { return existing, nil } else if !shared.IsNotFound(err) { return nil, err } - securityGroup, err := c.IaaSClient().DefaultAPI.CreateSecurityGroup(ctx, c.ProjectID(), c.Region()). + securityGroup, err := c.scope.IaaSClient().DefaultAPI.CreateSecurityGroup(ctx, c.scope.ProjectID(), c.scope.Region()). CreateSecurityGroupPayload(spec.toCreatePayload()). Execute() if err != nil { @@ -247,8 +247,8 @@ func (c *Client) deleteSecurityGroupWithRules( return err } } - if err := c.IaaSClient().DefaultAPI. - DeleteSecurityGroup(ctx, c.ProjectID(), c.Region(), id). + if err := c.scope.IaaSClient().DefaultAPI. + DeleteSecurityGroup(ctx, c.scope.ProjectID(), c.scope.Region(), id). Execute(); err != nil { err := shared.ClassifySDKError("delete security group", err) if !shared.IsNotFound(err) { @@ -265,7 +265,7 @@ func (c *Client) detachSecurityGroupFromServers(ctx context.Context, securityGro if len(spec.ServerSelector) == 0 { return nil } - serverIDs, err := server.NewClient(c).ListIDsByTags(ctx, spec.ServerSelector) + serverIDs, err := server.NewClient(c.scope).ListIDsByTags(ctx, spec.ServerSelector) if err != nil { return err } diff --git a/cloud/services/securitygroup/reconcile_test.go b/cloud/services/securitygroup/reconcile_test.go index 9662e01..fe9b6ef 100644 --- a/cloud/services/securitygroup/reconcile_test.go +++ b/cloud/services/securitygroup/reconcile_test.go @@ -76,6 +76,16 @@ func newTestIaaSClient(t *testing.T, handler http.Handler) *iaas.APIClient { return iaasClient } +func newTestClient(t *testing.T, handler http.Handler) *securitygroup.Client { + t.Helper() + + return securitygroup.NewClient(&testScope{ + projectID: testProjectID, + region: testRegion, + iaasClient: newTestIaaSClient(t, handler), + }) +} + func newTestService(t *testing.T, specs []securitygroup.SecurityGroupSpec, handler http.Handler) *securitygroup.Service { t.Helper() @@ -773,20 +783,20 @@ func TestServiceDeleteStaleIDFallbackAndDetach(t *testing.T) { } } -func TestFindByTagsAndDeleteByID(t *testing.T) { - t.Run("FindByTags returns matching security group", func(t *testing.T) { - svc := newTestService(t, nil, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { +func TestListByTagsAndDeleteByID(t *testing.T) { + t.Run("ListByTags returns matching security groups", func(t *testing.T) { + client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { writeJSON(t, w, map[string]any{"items": []any{ map[string]any{"id": testSecurityGroupID, "name": "bastion-ssh", "labels": map[string]any{"cluster": "test"}}, }}) })) - got, err := svc.FindByTags(context.Background(), map[string]string{"cluster": "test"}) + got, err := client.ListByTags(context.Background(), map[string]string{"cluster": "test"}) if err != nil { - t.Fatalf("FindByTags() error = %v", err) + t.Fatalf("ListByTags() error = %v", err) } - if got.ID != testSecurityGroupID { - t.Fatalf("FindByTags().ID = %q, want %q", got.ID, testSecurityGroupID) + if len(got) != 1 || got[0].ID != testSecurityGroupID { + t.Fatalf("ListByTags() = %#v, want [%s]", got, testSecurityGroupID) } }) @@ -795,7 +805,7 @@ func TestFindByTagsAndDeleteByID(t *testing.T) { deletedRules []string deletedSGID string ) - svc := newTestService(t, nil, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path switch { case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): @@ -814,7 +824,7 @@ func TestFindByTagsAndDeleteByID(t *testing.T) { } })) - if err := svc.DeleteByID(context.Background(), testSecurityGroupID); err != nil { + if err := client.DeleteByID(context.Background(), testSecurityGroupID); err != nil { t.Fatalf("DeleteByID() error = %v", err) } if !slices.Equal(deletedRules, []string{testRuleID1}) { diff --git a/cloud/services/securitygroup/rules.go b/cloud/services/securitygroup/rules.go index 2fa0b1a..2878933 100644 --- a/cloud/services/securitygroup/rules.go +++ b/cloud/services/securitygroup/rules.go @@ -61,7 +61,7 @@ func (c *Client) ensureSecurityGroupRules(ctx context.Context, securityGroupID s func (c *Client) resolveRule(ctx context.Context, rule Rule) (Rule, error) { rule = rule.normalize() if rule.RemoteSecurityGroupID == "" && len(rule.RemoteSecurityGroupTags) > 0 { - remoteSG, err := c.FindByTags(ctx, rule.RemoteSecurityGroupTags) + remoteSG, err := c.findByTags(ctx, rule.RemoteSecurityGroupTags) if err != nil { return Rule{}, err } @@ -72,7 +72,7 @@ func (c *Client) resolveRule(ctx context.Context, rule Rule) (Rule, error) { } func (c *Client) createSecurityGroupRule(ctx context.Context, securityGroupID string, rule Rule) error { - if _, err := c.IaaSClient().DefaultAPI.CreateSecurityGroupRule(ctx, c.ProjectID(), c.Region(), securityGroupID). + if _, err := c.scope.IaaSClient().DefaultAPI.CreateSecurityGroupRule(ctx, c.scope.ProjectID(), c.scope.Region(), securityGroupID). CreateSecurityGroupRulePayload(rule.toCreatePayload()). Execute(); err != nil { return shared.ClassifySDKError("create security group rule", err) @@ -81,7 +81,7 @@ func (c *Client) createSecurityGroupRule(ctx context.Context, securityGroupID st } func (c *Client) listSecurityGroupRules(ctx context.Context, securityGroupID string) ([]iaas.SecurityGroupRule, error) { - resp, err := c.IaaSClient().DefaultAPI.ListSecurityGroupRules(ctx, c.ProjectID(), c.Region(), securityGroupID).Execute() + resp, err := c.scope.IaaSClient().DefaultAPI.ListSecurityGroupRules(ctx, c.scope.ProjectID(), c.scope.Region(), securityGroupID).Execute() if err != nil { return nil, shared.ClassifySDKError("list security group rules", err) } @@ -92,8 +92,8 @@ func (c *Client) deleteSecurityGroupRule(ctx context.Context, securityGroupID, r if ruleID == "" { return nil } - if err := c.IaaSClient().DefaultAPI. - DeleteSecurityGroupRule(ctx, c.ProjectID(), c.Region(), securityGroupID, ruleID). + if err := c.scope.IaaSClient().DefaultAPI. + DeleteSecurityGroupRule(ctx, c.scope.ProjectID(), c.scope.Region(), securityGroupID, ruleID). Execute(); err != nil { err := shared.ClassifySDKError("delete security group rule", err) if !shared.IsNotFound(err) { diff --git a/cloud/services/securitygroup/service.go b/cloud/services/securitygroup/service.go index b7ebeee..ff23043 100644 --- a/cloud/services/securitygroup/service.go +++ b/cloud/services/securitygroup/service.go @@ -66,24 +66,24 @@ type Scope interface { // Client provides direct security group operations against the STACKIT IaaS API. type Client struct { - shared.IaaSScope + scope shared.IaaSScope } // NewClient returns a Client for the given scope. func NewClient(scope shared.IaaSClientScope) *Client { - return &Client{IaaSScope: shared.NewIaaSScope(scope)} + return &Client{scope: shared.NewIaaSScope(scope)} } // Service implements security groups reconciler. type Service struct { - scope Scope - *Client + scope Scope + client *Client } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ scope: scope, - Client: NewClient(scope), + client: NewClient(scope), } } diff --git a/cloud/services/server/reconcile.go b/cloud/services/server/reconcile.go index 65956bf..b3eead5 100644 --- a/cloud/services/server/reconcile.go +++ b/cloud/services/server/reconcile.go @@ -28,7 +28,7 @@ func (s *Service) Reconcile(ctx context.Context) error { return err } - srv, created, err := s.ensureServer(ctx, spec) + srv, created, err := s.client.ensureServer(ctx, spec) if err != nil { return err } @@ -46,7 +46,7 @@ func (s *Service) Delete(ctx context.Context) error { return fmt.Errorf("%w: server ID or tags are required", shared.ErrInvalidInput) } - deletedID, err := s.deleteBySpec(ctx, spec) + deletedID, err := s.client.deleteBySpec(ctx, spec) if err != nil { return err } @@ -64,7 +64,7 @@ func (c *Client) Get(ctx context.Context, id string) (*Server, error) { if id == "" { return nil, fmt.Errorf("%w: server ID is empty", shared.ErrInvalidInput) } - srv, err := c.IaaSClient().DefaultAPI.GetServer(ctx, c.ProjectID(), c.Region(), id).Execute() + srv, err := c.scope.IaaSClient().DefaultAPI.GetServer(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute() if err != nil { return nil, shared.ClassifySDKError("get server", err) } @@ -108,7 +108,7 @@ func (c *Client) listSDKServersByTags(ctx context.Context, tags map[string]strin if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } - req := c.IaaSClient().DefaultAPI.ListServers(ctx, c.ProjectID(), c.Region()). + req := c.scope.IaaSClient().DefaultAPI.ListServers(ctx, c.scope.ProjectID(), c.scope.Region()). LabelSelector(shared.LabelSelector(tags)) if details { req = req.Details(true) @@ -131,7 +131,7 @@ func (c *Client) DeleteByID(ctx context.Context, id string) error { if id == "" { return fmt.Errorf("%w: server ID is empty", shared.ErrInvalidInput) } - if err := c.IaaSClient().DefaultAPI.DeleteServer(ctx, c.ProjectID(), c.Region(), id).Execute(); err != nil { + if err := c.scope.IaaSClient().DefaultAPI.DeleteServer(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute(); err != nil { err := shared.ClassifySDKError("delete server", err) if !shared.IsNotFound(err) { return err @@ -140,10 +140,10 @@ func (c *Client) DeleteByID(ctx context.Context, id string) error { return nil } -// Create creates a new STACKIT server from the given spec. -func (c *Client) Create(ctx context.Context, spec ServerSpec) (*Server, error) { +// create creates a new STACKIT server from the given spec. +func (c *Client) create(ctx context.Context, spec ServerSpec) (*Server, error) { payload := spec.toCreatePayload() - created, err := c.IaaSClient().DefaultAPI.CreateServer(ctx, c.ProjectID(), c.Region()). + created, err := c.scope.IaaSClient().DefaultAPI.CreateServer(ctx, c.scope.ProjectID(), c.scope.Region()). CreateServerPayload(payload). Execute() if err != nil { @@ -176,11 +176,11 @@ func (c *Client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bo if spec.DisallowRecreate { return nil, false, fmt.Errorf( "%w: server %s; the Machine must be replaced", - ErrServerGone, spec.ID, + errServerGone, spec.ID, ) } - out, err := c.Create(ctx, spec) + out, err := c.create(ctx, spec) if err != nil { return nil, false, err } @@ -189,7 +189,7 @@ func (c *Client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bo func (c *Client) deleteBySpec(ctx context.Context, spec ServerSpec) (string, error) { if spec.ID != "" { - err := c.IaaSClient().DefaultAPI.DeleteServer(ctx, c.ProjectID(), c.Region(), spec.ID).Execute() + err := c.scope.IaaSClient().DefaultAPI.DeleteServer(ctx, c.scope.ProjectID(), c.scope.Region(), spec.ID).Execute() if err == nil { return spec.ID, nil } @@ -290,7 +290,7 @@ func (c *Client) serverFromSDK(ctx context.Context, srv *iaas.Server) *Server { PowerStatus: srv.GetPowerStatus(), AvailabilityZone: srv.GetAvailabilityZone(), } - nics, err := c.IaaSClient().DefaultAPI.ListServerNICs(ctx, c.ProjectID(), c.Region(), out.ID).Execute() + nics, err := c.scope.IaaSClient().DefaultAPI.ListServerNICs(ctx, c.scope.ProjectID(), c.scope.Region(), out.ID).Execute() if err == nil { for _, nic := range nics.GetItems() { if ipv4 := nic.GetIpv4(); ipv4 != "" { diff --git a/cloud/services/server/reconcile_test.go b/cloud/services/server/reconcile_test.go index 509eca9..5081b7e 100644 --- a/cloud/services/server/reconcile_test.go +++ b/cloud/services/server/reconcile_test.go @@ -76,6 +76,16 @@ func newTestIaaSClient(t *testing.T, handler http.Handler) *iaas.APIClient { return iaasClient } +func newTestClient(t *testing.T, handler http.Handler) *server.Client { + t.Helper() + + return server.NewClient(&testScope{ + projectID: testProjectID, + region: testRegion, + iaasClient: newTestIaaSClient(t, handler), + }) +} + func newTestService(t *testing.T, spec server.ServerSpec, handler http.Handler) *server.Service { t.Helper() @@ -630,7 +640,7 @@ func TestServiceDelete(t *testing.T) { } func TestListIDsByTags(t *testing.T) { - svc := newTestService(t, server.ServerSpec{}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers") { if got := r.URL.Query().Get("details"); got != "false" { t.Fatalf("details = %q, want false", got) @@ -644,7 +654,7 @@ func TestListIDsByTags(t *testing.T) { t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) })) - ids, err := svc.ListIDsByTags(context.Background(), map[string]string{"cluster": "test"}) + ids, err := client.ListIDsByTags(context.Background(), map[string]string{"cluster": "test"}) if err != nil { t.Fatalf("ListIDsByTags() error = %v", err) } diff --git a/cloud/services/server/service.go b/cloud/services/server/service.go index 617df47..8547576 100644 --- a/cloud/services/server/service.go +++ b/cloud/services/server/service.go @@ -16,13 +16,13 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) -// ErrServerGone indicates that an already-provisioned server is missing in +// errServerGone indicates that an already-provisioned server is missing in // STACKIT and must not be recreated with stale bootstrap data. -var ErrServerGone = errors.New("server gone") +var errServerGone = errors.New("server gone") -// IsServerGone reports whether err wraps ErrServerGone. +// IsServerGone reports whether err wraps errServerGone. func IsServerGone(err error) bool { - return errors.Is(err, ErrServerGone) + return errors.Is(err, errServerGone) } // Server describes a STACKIT compute instance in provider-neutral terms. @@ -74,24 +74,24 @@ type Scope interface { // Client provides direct server operations against the STACKIT IaaS API. type Client struct { - shared.IaaSScope + scope shared.IaaSScope } // NewClient returns a Client for the given scope. func NewClient(scope shared.IaaSClientScope) *Client { - return &Client{IaaSScope: shared.NewIaaSScope(scope)} + return &Client{scope: shared.NewIaaSScope(scope)} } // Service implements server reconciler. type Service struct { - scope Scope - *Client + scope Scope + client *Client } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ scope: scope, - Client: NewClient(scope), + client: NewClient(scope), } } diff --git a/cloud/services/shared/errors_test.go b/cloud/services/shared/errors_test.go index 23f3872..30c00dd 100644 --- a/cloud/services/shared/errors_test.go +++ b/cloud/services/shared/errors_test.go @@ -58,26 +58,26 @@ func TestIsRetryable(t *testing.T) { func TestFindSingle(t *testing.T) { t.Run("returns ErrNotFound when empty", func(t *testing.T) { - _, err := FindSingle([]string{}, "server", "servers") + _, err := findSingle([]string{}, "server", "servers") if !IsNotFound(err) { - t.Fatalf("FindSingle() error = %v, want ErrNotFound", err) + t.Fatalf("findSingle() error = %v, want ErrNotFound", err) } }) t.Run("returns ErrConflict when multiple", func(t *testing.T) { - _, err := FindSingle([]string{"a", "b"}, "server", "servers") + _, err := findSingle([]string{"a", "b"}, "server", "servers") if !IsConflict(err) { - t.Fatalf("FindSingle() error = %v, want ErrConflict", err) + t.Fatalf("findSingle() error = %v, want ErrConflict", err) } }) t.Run("returns single element", func(t *testing.T) { - got, err := FindSingle([]string{"a"}, "server", "servers") + got, err := findSingle([]string{"a"}, "server", "servers") if err != nil { - t.Fatalf("FindSingle() error = %v", err) + t.Fatalf("findSingle() error = %v", err) } if got != "a" { - t.Fatalf("FindSingle() = %q, want %q", got, "a") + t.Fatalf("findSingle() = %q, want %q", got, "a") } }) } diff --git a/cloud/services/shared/utils.go b/cloud/services/shared/utils.go index 66bf7ee..994a061 100644 --- a/cloud/services/shared/utils.go +++ b/cloud/services/shared/utils.go @@ -68,11 +68,11 @@ func FindByTags[T any]( if err != nil { return zero, err } - return FindSingle(matched, singular, plural) + return findSingle(matched, singular, plural) } -// FindSingle returns the single item in matched, or ErrNotFound if empty, or ErrConflict if more than one. -func FindSingle[T any](matched []T, singular, plural string) (T, error) { +// findSingle returns the single item in matched, or ErrNotFound if empty, or ErrConflict if more than one. +func findSingle[T any](matched []T, singular, plural string) (T, error) { var zero T if len(matched) == 0 { return zero, fmt.Errorf("%w: no %s matching tags", ErrNotFound, singular) diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index 758fa3e..10a3c8f 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -33,7 +33,6 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/scope" - "github.com/stackitcloud/cluster-api-provider-stackit/util" ) var _ = Describe("StackitCluster Controller", func() { @@ -205,13 +204,20 @@ var _ = Describe("StackitCluster Controller", func() { nodeSGs, err := securitygroup.NewClient(fakeCloud).ListByTags(ctx, scope.NodeSSHTags(got)) Expect(err).NotTo(HaveOccurred()) Expect(nodeSGs).To(HaveLen(1)) - workerServer, err := server.NewClient(fakeCloud).Create(ctx, server.ServerSpec{ - Name: got.Name + "-worker-0", - Tags: util.MachineTags(got.Name, got.Namespace, "worker-0", "uid-1", nil), - SecurityGroups: []string{nodeSGs[0].ID}, - }) + stackitMachine := newStackitMachine(got.Name+"-worker-0", got.Namespace, "worker-0") + stackitMachine.Spec.SecurityGroups = []string{nodeSGs[0].ID} + machineScope, err := scope.NewMachineScope( + k8sClient, + &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{Name: got.Name, Namespace: got.Namespace}}, + &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: "worker-0", UID: "uid-1"}}, + got, + stackitMachine, + ) Expect(err).NotTo(HaveOccurred()) - Expect(fakeCloud.ServerHasSecurityGroup(workerServer.ID, nodeSGs[0].ID)).To(BeTrue()) + machineScope.SetCloudClient(fakeCloud) + Expect(server.New(machineScope).Reconcile(ctx)).To(Succeed()) + workerServerID := stackitMachine.Status.InstanceID + Expect(fakeCloud.ServerHasSecurityGroup(workerServerID, nodeSGs[0].ID)).To(BeTrue()) got.Spec.Bastion.Enabled = false Expect(k8sClient.Update(ctx, got)).To(Succeed()) @@ -222,7 +228,7 @@ var _ = Describe("StackitCluster Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(got.Status.Bastion).To(Equal(infrav1.StackitBastionStatus{})) - Expect(fakeCloud.ServerHasSecurityGroup(workerServer.ID, nodeSGs[0].ID)).To(BeFalse()) + Expect(fakeCloud.ServerHasSecurityGroup(workerServerID, nodeSGs[0].ID)).To(BeFalse()) Expect(fakeCloud.ServerCount()).To(Equal(1)) Expect(fakeCloud.PublicIPCount()).To(Equal(0)) Expect(fakeCloud.SecurityGroupCount()).To(Equal(0)) diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index 97b65de..ba72717 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -42,10 +42,9 @@ const bastionSSHPort = 22 // ClusterScope holds the per-reconcile state for a StackitCluster. type ClusterScope struct { - Client client.Client Cluster *clusterv1.Cluster StackitCluster *infrav1.StackitCluster - CloudClient cloud.Client + cloudClient cloud.Client network *network.Network @@ -72,7 +71,6 @@ func NewClusterScope( return nil, err } return &ClusterScope{ - Client: k8sClient, Cluster: cluster, StackitCluster: stackitCluster, patchHelper: patchHelper, @@ -105,7 +103,7 @@ func (s *ClusterScope) SetNotReady(reason, message string, conditionTypes ...str s.SetConditions(metav1.ConditionFalse, reason, message, conditionTypes...) } -func (s *ClusterScope) SetAPIServerEndpoint(endpoint clusterv1.APIEndpoint) { +func (s *ClusterScope) setAPIServerEndpoint(endpoint clusterv1.APIEndpoint) { s.StackitCluster.Spec.ControlPlaneEndpoint = endpoint s.StackitCluster.Status.APIServerEndpoint = endpoint } @@ -125,11 +123,11 @@ func (s *ClusterScope) ServerSpec() server.ServerSpec { Name: s.StackitCluster.Name + "-bastion", ImageID: s.StackitCluster.Spec.Bastion.ImageID, MachineType: s.StackitCluster.Spec.Bastion.MachineType, - AvailabilityZone: DefaultAvailabilityZone(s.StackitCluster), + AvailabilityZone: defaultAvailabilityZone(s.StackitCluster), SSHKeyName: s.StackitCluster.Spec.Bastion.SSHKeyName, NetworkID: s.StackitCluster.Spec.Network.ID, UserData: s.bastionCloudInit, - Tags: s.BastionTags(), + Tags: s.bastionTags(), RootVolume: rootVolumeFromSpec(s.StackitCluster.Spec.Bastion.RootVolume), SetServer: func(srv *server.Server, _ bool) { s.bastionServerState = srv.State @@ -156,7 +154,7 @@ func rootVolumeFromSpec(in infrav1.StackitRootVolumeSpec) server.RootVolume { } } -func (s *ClusterScope) SetBastionSecurityGroupID(id string) { +func (s *ClusterScope) setBastionSecurityGroupID(id string) { s.StackitCluster.Status.Bastion.SecurityGroupID = id } @@ -168,11 +166,11 @@ func (s *ClusterScope) SetFailureDomains(failureDomains []clusterv1.FailureDomai s.StackitCluster.Status.FailureDomains = failureDomains } -// DefaultAvailabilityZone returns the default single availability zone for a +// defaultAvailabilityZone returns the default single availability zone for a // StackitCluster (the alphabetically first published failure domain, falling // back to -1). This prevents the STACKIT IaaS API from defaulting to // the expensive metro zone (-m). -func DefaultAvailabilityZone(stackitCluster *infrav1.StackitCluster) string { +func defaultAvailabilityZone(stackitCluster *infrav1.StackitCluster) string { if stackitCluster == nil { return "" } @@ -191,36 +189,36 @@ func DefaultAvailabilityZone(stackitCluster *infrav1.StackitCluster) string { } func (s *ClusterScope) SetCloudClient(cloudClient cloud.Client) { - s.CloudClient = cloudClient + s.cloudClient = cloudClient } func (s *ClusterScope) ProjectID() string { if s.StackitCluster.Spec.ProjectID != "" { return s.StackitCluster.Spec.ProjectID } - return s.CloudClient.ProjectID() + return s.cloudClient.ProjectID() } func (s *ClusterScope) Region() string { if s.StackitCluster.Spec.Region != "" { return s.StackitCluster.Spec.Region } - return s.CloudClient.Region() + return s.cloudClient.Region() } func (s *ClusterScope) IaaSClient() *iaas.APIClient { - return s.CloudClient.IaaSClient() + return s.cloudClient.IaaSClient() } func (s *ClusterScope) LoadBalancerClient() *lb.APIClient { - return s.CloudClient.LoadBalancerClient() + return s.cloudClient.LoadBalancerClient() } -func (s *ClusterScope) APIServerLoadBalancerTags() map[string]string { +func (s *ClusterScope) apiServerLoadBalancerTags() map[string]string { return util.ClusterTags(s.StackitCluster.Name, s.StackitCluster.Namespace, s.StackitCluster.Spec.AdditionalLabels) } -func (s *ClusterScope) SetAPIServerLoadBalancerID(id string) { +func (s *ClusterScope) setAPIServerLoadBalancerID(id string) { s.StackitCluster.Status.APIServerLoadBalancerID = id } @@ -235,16 +233,16 @@ func (s *ClusterScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec { Name: s.StackitCluster.Name + "-apiserver", NetworkID: s.StackitCluster.Spec.Network.ID, Port: loadbalancer.DefaultAPIServerPort, - Tags: s.APIServerLoadBalancerTags(), + Tags: s.apiServerLoadBalancerTags(), Targets: s.loadBalancerTargets, SeedTargets: s.loadBalancerSeedTargets, - SetID: s.SetAPIServerLoadBalancerID, + SetID: s.setAPIServerLoadBalancerID, SetAddress: func(ip string, port int32) { if ip == "" { s.StackitCluster.Status.APIServerEndpoint = clusterv1.APIEndpoint{} return } - s.SetAPIServerEndpoint(clusterv1.APIEndpoint{ + s.setAPIServerEndpoint(clusterv1.APIEndpoint{ Host: ip, Port: port, }) @@ -292,14 +290,14 @@ func (s *ClusterScope) BootstrapTargetIP() string { func (s *ClusterScope) PublicIPSpec() publicip.PublicIPSpec { return publicip.PublicIPSpec{ ID: s.StackitCluster.Status.Bastion.PublicIPID, - Tags: s.BastionTags(), + Tags: s.bastionTags(), ServerID: s.StackitCluster.Status.Bastion.ServerID, - ServerSelector: s.BastionTags(), - SetPublicIP: s.SetBastionPublicIP, + ServerSelector: s.bastionTags(), + SetPublicIP: s.setBastionPublicIP, } } -func (s *ClusterScope) BastionTags() map[string]string { +func (s *ClusterScope) bastionTags() map[string]string { return bastionTags(s.StackitCluster) } @@ -367,7 +365,7 @@ func NodeSSHTags(stackitCluster *infrav1.StackitCluster) map[string]string { return tags } -func (s *ClusterScope) SetBastionPublicIP(id, ip string) { +func (s *ClusterScope) setBastionPublicIP(id, ip string) { s.StackitCluster.Status.Bastion.PublicIPID = id s.StackitCluster.Status.Bastion.PublicIP = ip } @@ -394,11 +392,11 @@ func (s *ClusterScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { ID: s.StackitCluster.Status.Bastion.SecurityGroupID, Name: s.StackitCluster.Name + "-bastion-ssh", Description: "Cluster API Provider STACKIT bastion SSH access", - Tags: s.BastionTags(), + Tags: s.bastionTags(), Rules: rules, ServerIDs: []string{s.StackitCluster.Status.Bastion.ServerID}, - ServerSelector: s.BastionTags(), - SetID: s.SetBastionSecurityGroupID, + ServerSelector: s.bastionTags(), + SetID: s.setBastionSecurityGroupID, }, nodeSSHSecurityGroupSpec(s.StackitCluster, ""), } diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index e49fb83..6be2895 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -71,7 +71,7 @@ func TestDefaultAvailabilityZone(t *testing.T) { func TestAvailabilityZoneSpecUsesResolvedRegion(t *testing.T) { clusterScope := &ClusterScope{ StackitCluster: &infrav1.StackitCluster{}, - CloudClient: cloudfake.New(), + cloudClient: cloudfake.New(), } clusterScope.AvailabilityZoneSpec().SetZones([]string{"eu01-1"}) diff --git a/scope/machine_scope.go b/scope/machine_scope.go index 38b8e8a..e7b308b 100644 --- a/scope/machine_scope.go +++ b/scope/machine_scope.go @@ -29,12 +29,11 @@ import ( // MachineScope holds the per-reconcile state for a StackitMachine. type MachineScope struct { - Client client.Client - Cluster *clusterv1.Cluster Machine *clusterv1.Machine StackitCluster *infrav1.StackitCluster StackitMachine *infrav1.StackitMachine - CloudClient cloud.Client + cluster *clusterv1.Cluster + cloudClient cloud.Client bootstrapData []byte instancePowerStatus string @@ -57,11 +56,10 @@ func NewMachineScope( return nil, err } return &MachineScope{ - Client: k8sClient, - Cluster: cluster, Machine: machine, StackitCluster: stackitCluster, StackitMachine: stackitMachine, + cluster: cluster, patchHelper: patchHelper, }, nil } @@ -91,17 +89,17 @@ func (s *MachineScope) SetNotReady(reason, message string, conditionTypes ...str s.SetConditions(metav1.ConditionFalse, reason, message, conditionTypes...) } -func (s *MachineScope) Tags() map[string]string { +func (s *MachineScope) tags() map[string]string { return util.MachineTags( - s.Cluster.Name, - s.Cluster.Namespace, + s.cluster.Name, + s.cluster.Namespace, s.Machine.Name, string(s.Machine.UID), s.StackitMachine.Spec.AdditionalLabels, ) } -func (s *MachineScope) SetInstance(srv *server.Server) string { +func (s *MachineScope) setInstance(srv *server.Server) { providerID := cloud.NewProviderID(srv.ID) s.StackitMachine.Status.InstanceID = srv.ID s.StackitMachine.Status.InstanceState = srv.State @@ -109,16 +107,15 @@ func (s *MachineScope) SetInstance(srv *server.Server) string { s.StackitMachine.Spec.ProviderID = &providerID s.StackitMachine.Status.ProviderID = providerID s.StackitMachine.Status.Initialization.Provisioned = true - return providerID } -func (s *MachineScope) ClearInstance() { +func (s *MachineScope) clearInstance() { s.StackitMachine.Status.InstanceID = "" s.StackitMachine.Status.InstanceState = "" s.StackitMachine.Status.AvailabilityZone = "" } -// AvailabilityZone returns the resolved availability zone for the machine. +// availabilityZone returns the resolved availability zone for the machine. // Following Cluster API conventions: // 1. If Machine.Spec.FailureDomain is set (e.g. by KCP spreading control plane nodes or MachineDeployment), // it is used. @@ -126,7 +123,7 @@ func (s *MachineScope) ClearInstance() { // 3. Otherwise, if neither is set, it defaults to the first available failure domain // from StackitCluster.Status.FailureDomains (sorted alphabetically). This prevents the STACKIT IaaS // API from defaulting to the expensive metro zone (eu01-m). -func (s *MachineScope) AvailabilityZone() string { +func (s *MachineScope) availabilityZone() string { if s.Machine != nil && s.Machine.Spec.FailureDomain != "" { return s.Machine.Spec.FailureDomain } @@ -135,29 +132,29 @@ func (s *MachineScope) AvailabilityZone() string { return s.StackitMachine.Spec.AvailabilityZone } - return DefaultAvailabilityZone(s.StackitCluster) + return defaultAvailabilityZone(s.StackitCluster) } func (s *MachineScope) SetCloudClient(cloudClient cloud.Client) { - s.CloudClient = cloudClient + s.cloudClient = cloudClient } func (s *MachineScope) ProjectID() string { if s.StackitCluster.Spec.ProjectID != "" { return s.StackitCluster.Spec.ProjectID } - return s.CloudClient.ProjectID() + return s.cloudClient.ProjectID() } func (s *MachineScope) Region() string { if s.StackitCluster.Spec.Region != "" { return s.StackitCluster.Spec.Region } - return s.CloudClient.Region() + return s.cloudClient.Region() } func (s *MachineScope) IaaSClient() *iaas.APIClient { - return s.CloudClient.IaaSClient() + return s.cloudClient.IaaSClient() } func (s *MachineScope) SetBootstrapData(data []byte) { @@ -178,21 +175,21 @@ func (s *MachineScope) ServerSpec() server.ServerSpec { Name: s.StackitMachine.Name, ImageID: s.StackitMachine.Spec.ImageID, MachineType: s.StackitMachine.Spec.MachineType, - AvailabilityZone: s.AvailabilityZone(), + AvailabilityZone: s.availabilityZone(), SSHKeyName: s.StackitMachine.Spec.SSHKeyName, NetworkID: s.StackitMachine.Spec.Network.ID, SecurityGroups: s.StackitMachine.Spec.SecurityGroups, UserData: s.bootstrapData, - Tags: s.Tags(), + Tags: s.tags(), RootVolume: rootVolumeFromSpec(s.StackitMachine.Spec.RootVolume), DisallowRecreate: s.StackitMachine.Status.Initialization.Provisioned, SetServer: func(srv *server.Server, created bool) { s.instanceCreated = created s.instancePowerStatus = srv.PowerStatus s.StackitMachine.Status.Addresses = machineAddressesFromServer(srv.Addresses) - s.SetInstance(srv) + s.setInstance(srv) }, - ClearServer: s.ClearInstance, + ClearServer: s.clearInstance, } } From 1c84b1681b68c293894b9f1d4ba0a0e861f2eb11 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 10:39:10 +0100 Subject: [PATCH 19/60] refactor: use service Delete in CleanupByTags and unexport service clients --- cloud/cleanup.go | 108 ++++----- cloud/cleanup_test.go | 203 +++++++++------- cloud/fake/client.go | 48 ++-- cloud/sdk_client_integration_test.go | 57 ++--- cloud/services/availabilityzone/reconcile.go | 4 +- .../availabilityzone/reconcile_test.go | 5 + cloud/services/availabilityzone/service.go | 20 +- cloud/services/loadbalancer/reconcile.go | 60 ++--- cloud/services/loadbalancer/service.go | 33 +-- cloud/services/loadbalancer/targets.go | 10 +- cloud/services/network/service.go | 4 +- cloud/services/publicip/reconcile.go | 81 ++++--- cloud/services/publicip/reconcile_test.go | 2 - cloud/services/publicip/service.go | 16 +- cloud/services/securitygroup/reconcile.go | 104 +++++---- .../services/securitygroup/reconcile_test.go | 63 ----- cloud/services/securitygroup/rules.go | 10 +- cloud/services/securitygroup/service.go | 16 +- cloud/services/server/reconcile.go | 99 +++----- cloud/services/server/reconcile_test.go | 38 --- cloud/services/server/service.go | 16 +- cloud/services/shared/utils.go | 48 ++-- controller/stackitcluster_controller_test.go | 22 +- controller/stackitmachine_controller_test.go | 14 +- test/e2e/e2e_test.go | 219 +++++++++--------- 25 files changed, 587 insertions(+), 713 deletions(-) diff --git a/cloud/cleanup.go b/cloud/cleanup.go index d55614c..ee6cc10 100644 --- a/cloud/cleanup.go +++ b/cloud/cleanup.go @@ -13,7 +13,6 @@ package cloud import ( "context" "fmt" - "maps" "strings" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" @@ -23,81 +22,60 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) -// CleanupByTags deletes provider-managed cloud resources matching tags without -// relying on Kubernetes objects. Load balancers are deleted before servers so -// server deletion is not blocked by target attachments. -func CleanupByTags(ctx context.Context, client Client, tags map[string]string) error { - if len(tags) == 0 { - return fmt.Errorf("%w: empty cleanup tag selector", shared.ErrInvalidInput) - } - - var errs []string - srvClient := server.NewClient(client) - sgClient := securitygroup.NewClient(client) - pubIPClient := publicip.NewClient(client) +type cleanupScope struct { + Client + tags map[string]string +} - bastionTags := make(map[string]string, len(tags)+1) - maps.Copy(bastionTags, tags) - bastionTags["cluster-api-provider-stackit/resource-role"] = "bastion" - bastionServers, err := srvClient.ListByTags(ctx, bastionTags) - if err != nil { - errs = append(errs, err.Error()) - } - publicIPs, err := pubIPClient.ListByTags(ctx, tags) - if err != nil { - errs = append(errs, err.Error()) - } else { - for _, pubIP := range publicIPs { - for _, srv := range bastionServers { - if err := pubIPClient.DetachFromServer(ctx, srv.ID, pubIP.ID); err != nil { - errs = append(errs, fmt.Sprintf("detach public IP %q from server %q: %v", pubIP.ID, srv.ID, err)) - } - } - if err := pubIPClient.DeleteByID(ctx, pubIP.ID); err != nil { - errs = append(errs, fmt.Sprintf("delete public IP %q: %v", pubIP.ID, err)) - } - } +func (s *cleanupScope) PublicIPSpec() publicip.PublicIPSpec { + return publicip.PublicIPSpec{ + Tags: s.tags, + ServerSelector: s.tags, } +} - lbClient := loadbalancer.NewClient(client) - loadBalancers, err := lbClient.ListByTags(ctx, tags) - if err != nil { - errs = append(errs, err.Error()) - } else { - for _, lb := range loadBalancers { - if err := lbClient.DeleteByID(ctx, lb.ID); err != nil { - errs = append(errs, fmt.Sprintf("delete load balancer %q: %v", lb.ID, err)) - } - } +func (s *cleanupScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec { + return loadbalancer.LoadBalancerSpec{ + Tags: s.tags, } +} - servers, err := srvClient.ListByTags(ctx, tags) - if err != nil { - errs = append(errs, err.Error()) +func (s *cleanupScope) ServerSpec() server.ServerSpec { + return server.ServerSpec{ + Tags: s.tags, } +} - securityGroups, err := sgClient.ListByTags(ctx, tags) - if err != nil { - errs = append(errs, err.Error()) - } else { - for _, sg := range securityGroups { - for _, srv := range servers { - if err := sgClient.DetachFromServer(ctx, srv.ID, sg.ID); err != nil { - errs = append(errs, fmt.Sprintf("detach security group %q from server %q: %v", sg.ID, srv.ID, err)) - } - } - } +func (s *cleanupScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { + return []securitygroup.SecurityGroupSpec{ + { + Tags: s.tags, + ServerSelector: s.tags, + }, } +} - for _, srv := range servers { - if err := srvClient.DeleteByID(ctx, srv.ID); err != nil { - errs = append(errs, fmt.Sprintf("delete server %q: %v", srv.ID, err)) - } +// CleanupByTags deletes provider-managed cloud resources matching tags without +// relying on Kubernetes objects. Load balancers are deleted before servers so +// server deletion is not blocked by target attachments. +func CleanupByTags(ctx context.Context, client Client, tags map[string]string) error { + if len(tags) == 0 { + return fmt.Errorf("%w: empty cleanup tag selector", shared.ErrInvalidInput) } - for _, sg := range securityGroups { - if err := sgClient.DeleteByID(ctx, sg.ID); err != nil { - errs = append(errs, fmt.Sprintf("delete security group %q: %v", sg.ID, err)) + scope := &cleanupScope{ + Client: client, + tags: tags, + } + var errs []string + for _, deleteFn := range []func(context.Context) error{ + publicip.New(scope).Delete, + loadbalancer.New(scope).Delete, + securitygroup.New(scope).Delete, + server.New(scope).Delete, + } { + if err := deleteFn(ctx); err != nil { + errs = append(errs, err.Error()) } } diff --git a/cloud/cleanup_test.go b/cloud/cleanup_test.go index bb2bc6c..19245ec 100644 --- a/cloud/cleanup_test.go +++ b/cloud/cleanup_test.go @@ -12,6 +12,7 @@ package cloud_test import ( "context" + "maps" "testing" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" @@ -20,32 +21,123 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" + "github.com/stackitcloud/cluster-api-provider-stackit/util" ) -type testServerScope struct { +type testResourceScope struct { cloud.Client - spec server.ServerSpec + serverSpec server.ServerSpec + publicIPSpec publicip.PublicIPSpec + loadBalancerSpec loadbalancer.LoadBalancerSpec + securityGroupSpec []securitygroup.SecurityGroupSpec } -func (s *testServerScope) ServerSpec() server.ServerSpec { - return s.spec +func (s *testResourceScope) ServerSpec() server.ServerSpec { + return s.serverSpec } -func createTestServer(t *testing.T, client cloud.Client, name string, tags map[string]string) { +func (s *testResourceScope) PublicIPSpec() publicip.PublicIPSpec { + return s.publicIPSpec +} + +func (s *testResourceScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec { + return s.loadBalancerSpec +} + +func (s *testResourceScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { + return s.securityGroupSpec +} + +func createTestResources(t *testing.T, client cloud.Client, prefix string, tags map[string]string) { t.Helper() + ctx := context.Background() - err := server.New(&testServerScope{ + var workerServerID, bastionServerID string + if err := server.New(&testResourceScope{ Client: client, - spec: server.ServerSpec{ - Name: name, + serverSpec: server.ServerSpec{ + Name: prefix + "-worker", ImageID: "22222222-2222-4222-8222-222222222222", MachineType: "c2i.2", NetworkID: "11111111-1111-4111-8111-111111111111", Tags: tags, + SetServer: func(srv *server.Server, _ bool) { + workerServerID = srv.ID + }, + }, + }).Reconcile(ctx); err != nil { + t.Fatalf("server.Reconcile(worker) error = %v", err) + } + + bastionTags := make(map[string]string, len(tags)+1) + maps.Copy(bastionTags, tags) + bastionTags[util.LabelResourceRole] = util.ResourceRoleBastion + if err := server.New(&testResourceScope{ + Client: client, + serverSpec: server.ServerSpec{ + Name: prefix + "-bastion", + ImageID: "22222222-2222-4222-8222-222222222222", + MachineType: "c2i.2", + NetworkID: "11111111-1111-4111-8111-111111111111", + Tags: bastionTags, + SetServer: func(srv *server.Server, _ bool) { + bastionServerID = srv.ID + }, + }, + }).Reconcile(ctx); err != nil { + t.Fatalf("server.Reconcile(bastion) error = %v", err) + } + + if err := publicip.New(&testResourceScope{ + Client: client, + publicIPSpec: publicip.PublicIPSpec{ + Tags: bastionTags, + ServerID: bastionServerID, + }, + }).Reconcile(ctx); err != nil { + t.Fatalf("publicip.Reconcile() error = %v", err) + } + + nodeSSHTags := make(map[string]string, len(tags)+1) + maps.Copy(nodeSSHTags, tags) + nodeSSHTags[util.LabelResourceRole] = util.ResourceRoleNodeSSH + if err := securitygroup.New(&testResourceScope{ + Client: client, + securityGroupSpec: []securitygroup.SecurityGroupSpec{ + { + Name: prefix + "-bastion-sg", + Tags: bastionTags, + ServerIDs: []string{bastionServerID}, + }, + { + Name: prefix + "-node-ssh-sg", + Tags: nodeSSHTags, + ServerIDs: []string{workerServerID}, + Rules: []securitygroup.Rule{ + { + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 22, + PortMax: 22, + RemoteSecurityGroupTags: bastionTags, + }, + }, + }, }, - }).Reconcile(context.Background()) - if err != nil { - t.Fatalf("server.Reconcile() error = %v", err) + }).Reconcile(ctx); err != nil { + t.Fatalf("securitygroup.Reconcile() error = %v", err) + } + + if err := loadbalancer.New(&testResourceScope{ + Client: client, + loadBalancerSpec: loadbalancer.LoadBalancerSpec{ + Name: prefix + "-apiserver", + NetworkID: "11111111-1111-4111-8111-111111111111", + Port: loadbalancer.DefaultAPIServerPort, + Tags: tags, + }, + }).Reconcile(ctx); err != nil { + t.Fatalf("loadbalancer.Reconcile() error = %v", err) } } @@ -61,90 +153,29 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { "cluster-api-provider-stackit/test-id": "test-2", } - srvClient := server.NewClient(client) - lbClient := loadbalancer.NewClient(client) - pubIPClient := publicip.NewClient(client) - sgClient := securitygroup.NewClient(client) - - createTestServer(t, client, "matched", tags) - createTestServer(t, client, "other", otherTags) - if _, err := client.CreatePublicIP(ctx, tags); err != nil { - t.Fatalf("CreatePublicIP() error = %v", err) - } - if _, err := client.CreatePublicIP(ctx, otherTags); err != nil { - t.Fatalf("CreatePublicIP() error = %v", err) - } - if _, err := client.CreateSecurityGroup(ctx, "matched-sg", tags); err != nil { - t.Fatalf("CreateSecurityGroup() error = %v", err) - } - if _, err := client.CreateSecurityGroup(ctx, "other-sg", otherTags); err != nil { - t.Fatalf("CreateSecurityGroup() error = %v", err) - } - if _, err := client.CreateLoadBalancer(ctx, "matched", tags); err != nil { - t.Fatalf("CreateLoadBalancer() error = %v", err) - } - if _, err := client.CreateLoadBalancer(ctx, "other", otherTags); err != nil { - t.Fatalf("CreateLoadBalancer() error = %v", err) - } + createTestResources(t, client, "matched", tags) + createTestResources(t, client, "other", otherTags) if err := cloud.CleanupByTags(ctx, client, tags); err != nil { t.Fatalf("CleanupByTags() error = %v", err) } - matchedServers, err := srvClient.ListByTags(ctx, tags) - if err != nil { - t.Fatalf("server.ListByTags() error = %v", err) - } - if len(matchedServers) != 0 { - t.Fatalf("matched server count = %d, want 0", len(matchedServers)) - } - otherServers, err := srvClient.ListByTags(ctx, otherTags) - if err != nil { - t.Fatalf("server.ListByTags(other) error = %v", err) - } - if len(otherServers) != 1 { - t.Fatalf("other server count = %d, want 1", len(otherServers)) - } - matchedLoadBalancers, err := lbClient.ListByTags(ctx, tags) - if err != nil { - t.Fatalf("loadbalancer.ListByTags() error = %v", err) - } - if len(matchedLoadBalancers) != 0 { - t.Fatalf("matched load balancer count = %d, want 0", len(matchedLoadBalancers)) - } - otherLoadBalancers, err := lbClient.ListByTags(ctx, otherTags) - if err != nil { - t.Fatalf("loadbalancer.ListByTags(other) error = %v", err) - } - if len(otherLoadBalancers) != 1 { - t.Fatalf("other load balancer count = %d, want 1", len(otherLoadBalancers)) - } - matchedPublicIPs, err := pubIPClient.ListByTags(ctx, tags) - if err != nil { - t.Fatalf("publicip.ListByTags() error = %v", err) - } - if len(matchedPublicIPs) != 0 { - t.Fatalf("matched public IP count = %d, want 0", len(matchedPublicIPs)) - } - otherPublicIPs, err := pubIPClient.ListByTags(ctx, otherTags) - if err != nil { - t.Fatalf("publicip.ListByTags(other) error = %v", err) + if got := client.ServerCount(); got != 2 { + t.Fatalf("remaining server count = %d, want 2", got) } - if len(otherPublicIPs) != 1 { - t.Fatalf("other public IP count = %d, want 1", len(otherPublicIPs)) + if got := client.LoadBalancerCount(); got != 1 { + t.Fatalf("remaining load balancer count = %d, want 1", got) } - matchedSecurityGroups, err := sgClient.ListByTags(ctx, tags) - if err != nil { - t.Fatalf("securitygroup.ListByTags() error = %v", err) + if got := client.PublicIPCount(); got != 1 { + t.Fatalf("remaining public IP count = %d, want 1", got) } - if len(matchedSecurityGroups) != 0 { - t.Fatalf("matched security group count = %d, want 0", len(matchedSecurityGroups)) + if got := client.SecurityGroupCount(); got != 2 { + t.Fatalf("remaining security group count = %d, want 2", got) } - otherSecurityGroups, err := sgClient.ListByTags(ctx, otherTags) - if err != nil { - t.Fatalf("securitygroup.ListByTags(other) error = %v", err) + if sg := client.FindSecurityGroupByTags(tags); sg != nil { + t.Fatalf("unexpected remaining matched security group: %+v", sg) } - if len(otherSecurityGroups) != 1 { - t.Fatalf("other security group count = %d, want 1", len(otherSecurityGroups)) + if sg := client.FindSecurityGroupByTags(otherTags); sg == nil { + t.Fatal("expected other security group to remain") } } diff --git a/cloud/fake/client.go b/cloud/fake/client.go index 106d9c2..646a209 100644 --- a/cloud/fake/client.go +++ b/cloud/fake/client.go @@ -247,7 +247,7 @@ func (c *Client) getPublicIP(_ context.Context, id string) (*publicip.PublicIP, return &out, nil } -func (c *Client) CreatePublicIP(_ context.Context, tags map[string]string) (*publicip.PublicIP, error) { +func (c *Client) createPublicIP(_ context.Context, tags map[string]string) (*publicip.PublicIP, error) { c.mu.Lock() defer c.mu.Unlock() @@ -323,30 +323,6 @@ func (c *Client) removePublicIPFromServer(_ context.Context, serverID, publicIPI return nil } -func (c *Client) CreateSecurityGroup(_ context.Context, name string, tags map[string]string) (*securitygroup.SecurityGroup, error) { - c.mu.Lock() - defer c.mu.Unlock() - - entry, err := c.createSecurityGroupEntry(name, "", tags) - if err != nil { - return nil, err - } - out := *entry.securityGroup - return &out, nil -} - -func (c *Client) CreateLoadBalancer(_ context.Context, name string, tags map[string]string) (*loadbalancer.LoadBalancer, error) { - c.mu.Lock() - defer c.mu.Unlock() - - entry, err := c.createLoadBalancerEntry(name, 6443, loadbalancer.Labels(tags), nil) - if err != nil { - return nil, err - } - out := *entry.lb - return &out, nil -} - // ServerCount returns the number of currently tracked servers (test helper). func (c *Client) ServerCount() int { c.mu.Lock() @@ -410,6 +386,26 @@ func (c *Client) ServerUserData(serverID string) []byte { return append([]byte(nil), entry.userData...) } +// FindSecurityGroupByTags returns the single security group matching tags (test helper). +func (c *Client) FindSecurityGroupByTags(tags map[string]string) *securitygroup.SecurityGroup { + c.mu.Lock() + defer c.mu.Unlock() + for _, entry := range c.securityGroups { + if shared.MapContains(entry.tags, tags) { + out := *entry.securityGroup + return &out + } + } + return nil +} + +// DeleteServer removes a server from the fake store by ID (test helper). +func (c *Client) DeleteServer(serverID string) { + c.mu.Lock() + defer c.mu.Unlock() + delete(c.servers, serverID) +} + // SecurityGroupRules returns the rules currently stored on a fake security group. func (c *Client) SecurityGroupRules(securityGroupID string) []securitygroup.Rule { c.mu.Lock() @@ -859,7 +855,7 @@ func (c *Client) handlePublicIPRequest(rec *httptest.ResponseRecorder, r *http.R if r.Body != nil { _ = json.NewDecoder(r.Body).Decode(&body) } - created, err := c.CreatePublicIP(r.Context(), body.Labels) + created, err := c.createPublicIP(r.Context(), body.Labels) if err != nil { writeFakeError(rec, err) } else { diff --git a/cloud/sdk_client_integration_test.go b/cloud/sdk_client_integration_test.go index 1b52e10..d1a3cfd 100644 --- a/cloud/sdk_client_integration_test.go +++ b/cloud/sdk_client_integration_test.go @@ -22,7 +22,6 @@ import ( "time" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) @@ -38,16 +37,15 @@ const ( func TestSDKClientCredentialsIntegration(t *testing.T) { client := newIntegrationClient(t) - _, err := server.NewClient(client).FindByTags(context.Background(), map[string]string{ + ids, err := shared.ListServerIDsByTags(context.Background(), client, map[string]string{ "capistackit-test": "credential-check", }) - if shared.IsNotFound(err) { - return - } if err != nil { - t.Fatalf("FindByTags() error = %v", err) + t.Fatalf("ListServerIDsByTags() error = %v", err) + } + if len(ids) > 0 { + t.Fatal("ListServerIDsByTags() unexpectedly found a server matching integration-test tags") } - t.Fatal("FindByTags() unexpectedly found a server matching integration-test tags") } func TestSDKClientListNetworksIntegration(t *testing.T) { @@ -70,13 +68,13 @@ func TestSDKClientLoadBalancerCreateDeleteIntegration(t *testing.T) { client := newIntegrationClient(t) networkID := requiredIntegrationEnv(t, envIntegrationNetworkID) targetIP := requiredIntegrationEnv(t, envIntegrationTargetIP) - lbID := createIntegrationLoadBalancer(t, client, networkID, loadbalancer.Target{ + scope := createIntegrationLoadBalancer(t, client, networkID, loadbalancer.Target{ Name: "capistackit-initial-target", IP: targetIP, }) - if err := loadbalancer.NewClient(client).DeleteByID(context.Background(), lbID); err != nil { - t.Fatalf("DeleteByID() error = %v", err) + if err := loadbalancer.New(scope).Delete(context.Background()); err != nil { + t.Fatalf("loadbalancer.Delete() error = %v", err) } } @@ -85,35 +83,16 @@ func TestSDKClientLoadBalancerTargetIntegration(t *testing.T) { networkID := requiredIntegrationEnv(t, envIntegrationNetworkID) targetIP := requiredIntegrationEnv(t, envIntegrationTargetIP) initialIP := integrationInitialTargetIP(targetIP) - lbID := createIntegrationLoadBalancer(t, client, networkID, loadbalancer.Target{ + scope := createIntegrationLoadBalancer(t, client, networkID, loadbalancer.Target{ Name: "capistackit-initial-target", IP: initialIP, }) - lbClient := loadbalancer.NewClient(client) - t.Cleanup(func() { - if err := lbClient.DeleteByID(context.Background(), lbID); err != nil { - t.Logf("DeleteByID() cleanup error = %v", err) - } - }) targets := []loadbalancer.Target{ {Name: "capistackit-initial-target", IP: initialIP}, {Name: "capistackit-integration-target", IP: targetIP}, } - scope := &integrationLBScope{ - Client: client, - spec: loadbalancer.LoadBalancerSpec{ - ID: lbID, - Name: lbID, - NetworkID: networkID, - Port: 6443, - Targets: targets, - Tags: map[string]string{ - "capistackit-test": "true", - "capistackit-name": lbID, - }, - }, - } + scope.spec.Targets = targets svc := loadbalancer.New(scope) if err := svc.Reconcile(context.Background()); err != nil { t.Fatalf("loadbalancer.Reconcile() error = %v", err) @@ -205,12 +184,12 @@ func (s *integrationLBScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec { return s.spec } -func createIntegrationLoadBalancer(t *testing.T, client Client, networkID string, target loadbalancer.Target) string { +func createIntegrationLoadBalancer(t *testing.T, client Client, networkID string, target loadbalancer.Target) *integrationLBScope { t.Helper() name := fmt.Sprintf("capistackit-it-%d", time.Now().UnixNano()) - var lbID string - scope := &integrationLBScope{ + var scope *integrationLBScope + scope = &integrationLBScope{ Client: client, spec: loadbalancer.LoadBalancerSpec{ Name: name, @@ -222,23 +201,23 @@ func createIntegrationLoadBalancer(t *testing.T, client Client, networkID string "capistackit-name": name, }, SetID: func(id string) { - lbID = id + scope.spec.ID = id }, }, } if err := loadbalancer.New(scope).Reconcile(context.Background()); err != nil { t.Fatalf("loadbalancer.Reconcile() error = %v", err) } - if lbID == "" { + if scope.spec.ID == "" { t.Fatal("loadbalancer.Reconcile() did not record a load balancer ID") } t.Cleanup(func() { - if err := loadbalancer.NewClient(client).DeleteByID(context.Background(), lbID); err != nil && + if err := loadbalancer.New(scope).Delete(context.Background()); err != nil && !errors.Is(err, shared.ErrInvalidInput) { - t.Logf("DeleteByID() cleanup error = %v", err) + t.Logf("loadbalancer.Delete() cleanup error = %v", err) } }) - return lbID + return scope } func integrationInitialTargetIP(targetIP string) string { diff --git a/cloud/services/availabilityzone/reconcile.go b/cloud/services/availabilityzone/reconcile.go index 4d7d2e6..b4b3ac3 100644 --- a/cloud/services/availabilityzone/reconcile.go +++ b/cloud/services/availabilityzone/reconcile.go @@ -35,10 +35,10 @@ func (s *Service) Reconcile(ctx context.Context) error { // list retrieves the availability zones for the configured region. func (c *client) list(ctx context.Context) ([]string, error) { - if c.region == "" { + if c.scope.Region() == "" { return nil, fmt.Errorf("%w: region is empty", shared.ErrInvalidInput) } - resp, err := c.iaasClient.DefaultAPI.ListAvailabilityZones(ctx, c.region).Execute() + resp, err := c.scope.IaaSClient().DefaultAPI.ListAvailabilityZones(ctx, c.scope.Region()).Execute() if err != nil { return nil, shared.ClassifySDKError("list availability zones", err) } diff --git a/cloud/services/availabilityzone/reconcile_test.go b/cloud/services/availabilityzone/reconcile_test.go index 83d2878..89ab47c 100644 --- a/cloud/services/availabilityzone/reconcile_test.go +++ b/cloud/services/availabilityzone/reconcile_test.go @@ -30,11 +30,16 @@ import ( const testRegion = "eu01" type testScope struct { + projectID string region string iaasClient *iaas.APIClient spec availabilityzone.AvailabilityZoneSpec } +func (s *testScope) ProjectID() string { + return s.projectID +} + func (s *testScope) Region() string { return s.region } diff --git a/cloud/services/availabilityzone/service.go b/cloud/services/availabilityzone/service.go index 1b37e07..3ff544c 100644 --- a/cloud/services/availabilityzone/service.go +++ b/cloud/services/availabilityzone/service.go @@ -10,37 +10,27 @@ You may obtain a copy of the License at package availabilityzone -import iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" +import "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" // AvailabilityZoneSpec describes the callback used to record discovered availability zones. type AvailabilityZoneSpec struct { SetZones func(zones []string) } -// clientScope is an interface that provides the IaaS client and target region for availability zone operations. -type clientScope interface { - Region() string - IaaSClient() *iaas.APIClient -} - // Scope is an interface that provides scope data needed for availability zone reconciliation. type Scope interface { - clientScope + shared.IaaSClientScope AvailabilityZoneSpec() AvailabilityZoneSpec } // client provides direct availability zone operations against the STACKIT IaaS API. type client struct { - iaasClient *iaas.APIClient - region string + scope shared.IaaSClientScope } // newClient returns a client for the given scope. -func newClient(scope clientScope) *client { - return &client{ - iaasClient: scope.IaaSClient(), - region: scope.Region(), - } +func newClient(scope shared.IaaSClientScope) *client { + return &client{scope: scope} } // Service implements availability zones reconciler. diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index 3120f7e..ba59579 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -61,12 +61,12 @@ func (s *Service) Delete(ctx context.Context) error { return fmt.Errorf("%w: load balancer ID or tags are required", shared.ErrInvalidInput) } - loadBalancerID, err := s.client.resolveLoadBalancerForDelete(ctx, spec) + loadBalancerIDs, err := s.client.resolveLoadBalancersForDelete(ctx, spec) if err != nil { return err } - if loadBalancerID != "" { - if err := s.client.DeleteByID(ctx, loadBalancerID); err != nil { + for _, loadBalancerID := range loadBalancerIDs { + if err := s.client.deleteByID(ctx, loadBalancerID); err != nil { return err } log.V(1).Info("Deleted load balancer", "loadBalancerID", loadBalancerID) @@ -78,11 +78,11 @@ func (s *Service) Delete(ctx context.Context) error { } // get retrieves a STACKIT load balancer by ID. -func (c *Client) get(ctx context.Context, id string) (*LoadBalancer, error) { +func (c *client) get(ctx context.Context, id string) (*LoadBalancer, error) { if id == "" { return nil, fmt.Errorf("%w: load balancer ID is empty", shared.ErrInvalidInput) } - loadBalancer, err := c.lbClient.DefaultAPI.GetLoadBalancer(ctx, c.projectID, c.region, id).Execute() + loadBalancer, err := c.scope.LoadBalancerClient().DefaultAPI.GetLoadBalancer(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute() if err != nil { return nil, shared.ClassifySDKError("get load balancer", err) } @@ -90,16 +90,16 @@ func (c *Client) get(ctx context.Context, id string) (*LoadBalancer, error) { } // findByTags finds a single STACKIT load balancer matching the given tag selector. -func (c *Client) findByTags(ctx context.Context, tags map[string]string) (*LoadBalancer, error) { - return shared.FindByTags(ctx, tags, c.ListByTags, "load balancer", "load balancers") +func (c *client) findByTags(ctx context.Context, tags map[string]string) (*LoadBalancer, error) { + return shared.FindByTags(ctx, tags, c.listByTags, "load balancer", "load balancers") } -// ListByTags lists STACKIT load balancers matching the given tag selector. -func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*LoadBalancer, error) { +// listByTags lists STACKIT load balancers matching the given tag selector. +func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*LoadBalancer, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } - resp, err := c.lbClient.DefaultAPI.ListLoadBalancers(ctx, c.projectID, c.region).Execute() + resp, err := c.scope.LoadBalancerClient().DefaultAPI.ListLoadBalancers(ctx, c.scope.ProjectID(), c.scope.Region()).Execute() if err != nil { err := shared.ClassifySDKError("list load balancers", err) if isLoadBalancerServiceNotEnabled(err) { @@ -118,12 +118,12 @@ func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*Loa return matched, nil } -// DeleteByID deletes a load balancer by ID, ignoring NotFound errors. -func (c *Client) DeleteByID(ctx context.Context, id string) error { +// deleteByID deletes a load balancer by ID, ignoring NotFound errors. +func (c *client) deleteByID(ctx context.Context, id string) error { if id == "" { return fmt.Errorf("%w: load balancer ID is empty", shared.ErrInvalidInput) } - if _, err := c.lbClient.DefaultAPI.DeleteLoadBalancer(ctx, c.projectID, c.region, id).Execute(); err != nil { + if _, err := c.scope.LoadBalancerClient().DefaultAPI.DeleteLoadBalancer(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute(); err != nil { err := shared.ClassifySDKError("delete load balancer", err) if !shared.IsNotFound(err) { return err @@ -132,7 +132,7 @@ func (c *Client) DeleteByID(ctx context.Context, id string) error { return nil } -func (c *Client) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) (*LoadBalancer, error) { +func (c *client) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) (*LoadBalancer, error) { if spec.ID != "" { loadBalancer, err := c.get(ctx, spec.ID) if err == nil { @@ -148,8 +148,8 @@ func (c *Client) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) return nil, err } - payload := spec.toCreatePayload(c.region) - lbResp, err := c.lbClient.DefaultAPI.CreateLoadBalancer(ctx, c.projectID, c.region). + payload := spec.toCreatePayload(c.scope.Region()) + lbResp, err := c.scope.LoadBalancerClient().DefaultAPI.CreateLoadBalancer(ctx, c.scope.ProjectID(), c.scope.Region()). CreateLoadBalancerPayload(payload). Execute() if err != nil { @@ -160,27 +160,33 @@ func (c *Client) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) return out, nil } -func (c *Client) resolveLoadBalancerForDelete(ctx context.Context, spec LoadBalancerSpec) (string, error) { +func (c *client) resolveLoadBalancersForDelete(ctx context.Context, spec LoadBalancerSpec) ([]string, error) { if spec.ID != "" { exists, err := c.loadBalancerExists(ctx, spec.ID) - if err != nil || exists { - return spec.ID, err + if err != nil { + return nil, err + } + if exists { + return []string{spec.ID}, nil } } if len(spec.Tags) == 0 { - return "", nil - } - loadBalancer, err := c.findByTags(ctx, spec.Tags) - if shared.IsNotFound(err) { - return "", nil + return nil, nil } + loadBalancers, err := c.listByTags(ctx, spec.Tags) if err != nil { - return "", err + return nil, err + } + ids := make([]string, 0, len(loadBalancers)) + for _, loadBalancer := range loadBalancers { + if loadBalancer.ID != "" { + ids = append(ids, loadBalancer.ID) + } } - return loadBalancer.ID, nil + return ids, nil } -func (c *Client) loadBalancerExists(ctx context.Context, id string) (bool, error) { +func (c *client) loadBalancerExists(ctx context.Context, id string) (bool, error) { _, err := c.get(ctx, id) if err != nil { if shared.IsNotFound(err) { diff --git a/cloud/services/loadbalancer/service.go b/cloud/services/loadbalancer/service.go index 29998f0..e402fac 100644 --- a/cloud/services/loadbalancer/service.go +++ b/cloud/services/loadbalancer/service.go @@ -13,7 +13,7 @@ package loadbalancer import ( "errors" - lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) const ( @@ -62,45 +62,32 @@ type LoadBalancerSpec struct { SetAddress func(ip string, port int32) } -// ClientScope is an interface that provides the LoadBalancer client and target scope for load balancer operations. -type ClientScope interface { - ProjectID() string - Region() string - LoadBalancerClient() *lb.APIClient -} - // Scope is an interface that provides scope data needed for load balancer reconciliation. type Scope interface { - ClientScope + shared.LoadBalancerClientScope LoadBalancerSpec() LoadBalancerSpec } -// Client provides direct load balancer operations against the STACKIT Load Balancer API. -type Client struct { - lbClient *lb.APIClient - projectID string - region string +// client provides direct load balancer operations against the STACKIT Load Balancer API. +type client struct { + scope shared.LoadBalancerClientScope } -// NewClient returns a Client for the given scope. -func NewClient(scope ClientScope) *Client { - return &Client{ - lbClient: scope.LoadBalancerClient(), - projectID: scope.ProjectID(), - region: scope.Region(), - } +// newClient returns a client for the given scope. +func newClient(scope shared.LoadBalancerClientScope) *client { + return &client{scope: scope} } // Service implements load balancer reconciler. type Service struct { scope Scope - client *Client + client *client } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ scope: scope, - client: NewClient(scope), + client: newClient(scope), } } diff --git a/cloud/services/loadbalancer/targets.go b/cloud/services/loadbalancer/targets.go index 4e996d6..28567aa 100644 --- a/cloud/services/loadbalancer/targets.go +++ b/cloud/services/loadbalancer/targets.go @@ -113,7 +113,7 @@ func firstInternalIP(addresses []clusterv1.MachineAddress) string { } // setTargets replaces the contents of the API-server target pool. STACKIT rejects an empty pool. -func (c *Client) setTargets( +func (c *client) setTargets( ctx context.Context, loadBalancerID string, port int32, @@ -137,7 +137,7 @@ func (c *Client) setTargets( desired = append(desired, *target) } - loadBalancer, err := c.lbClient.DefaultAPI.GetLoadBalancer(ctx, c.projectID, c.region, loadBalancerID).Execute() + loadBalancer, err := c.scope.LoadBalancerClient().DefaultAPI.GetLoadBalancer(ctx, c.scope.ProjectID(), c.scope.Region(), loadBalancerID).Execute() if err != nil { return shared.ClassifySDKError("get load balancer", err) } @@ -159,7 +159,7 @@ func (c *Client) setTargets( return c.updateAPIServerTargetPool(ctx, loadBalancerID, targetPool, desired, port) } -func (c *Client) updateAPIServerTargetPool( +func (c *client) updateAPIServerTargetPool( ctx context.Context, loadBalancerID string, current *lb.TargetPool, @@ -174,8 +174,8 @@ func (c *Client) updateAPIServerTargetPool( payload.SetTargetPort(current.GetTargetPort()) } payload.SetTargets(targets) - if _, err := c.lbClient.DefaultAPI. - UpdateTargetPool(ctx, c.projectID, c.region, loadBalancerID, apiserverTargetPoolName). + if _, err := c.scope.LoadBalancerClient().DefaultAPI. + UpdateTargetPool(ctx, c.scope.ProjectID(), c.scope.Region(), loadBalancerID, apiserverTargetPoolName). UpdateTargetPoolPayload(*payload). Execute(); err != nil { return shared.ClassifySDKError("update load balancer target pool", err) diff --git a/cloud/services/network/service.go b/cloud/services/network/service.go index f2d4de8..7b9eb09 100644 --- a/cloud/services/network/service.go +++ b/cloud/services/network/service.go @@ -33,12 +33,12 @@ type Scope interface { // client provides direct network operations against the STACKIT IaaS API. type client struct { - scope shared.IaaSScope + scope shared.IaaSClientScope } // newClient returns a client for the given scope. func newClient(scope shared.IaaSClientScope) *client { - return &client{scope: shared.NewIaaSScope(scope)} + return &client{scope: scope} } // Service implements network reconciler. diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index b9c023f..9117c64 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -17,7 +17,6 @@ import ( iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" logf "sigs.k8s.io/controller-runtime/pkg/log" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) @@ -57,32 +56,33 @@ func (s *Service) Delete(ctx context.Context) error { return fmt.Errorf("%w: public IP ID or tags are required", shared.ErrInvalidInput) } - publicIP, err := s.client.resolvePublicIP(ctx, spec) + publicIPs, err := s.client.resolvePublicIPsForDelete(ctx, spec) if err != nil { return err } - if publicIP == nil { + if len(publicIPs) == 0 { if spec.SetPublicIP != nil { spec.SetPublicIP("", "") } return nil } - serverID, err := s.client.resolveServerID(ctx, spec) + serverIDs, err := s.client.resolveServerIDs(ctx, spec) if err != nil { return err } - if serverID != "" { - if err := s.client.DetachFromServer(ctx, serverID, publicIP.ID); err != nil { + for _, publicIP := range publicIPs { + for _, serverID := range serverIDs { + if err := s.client.detachFromServer(ctx, serverID, publicIP.ID); err != nil { + return err + } + } + if err := s.client.deleteByID(ctx, publicIP.ID); err != nil { return err } + log.V(1).Info("Deleted public IP", "publicIPID", publicIP.ID) } - if err := s.client.DeleteByID(ctx, publicIP.ID); err != nil { - return err - } - log.V(1).Info("Deleted public IP", "publicIPID", publicIP.ID) - if spec.SetPublicIP != nil { spec.SetPublicIP("", "") } @@ -90,7 +90,7 @@ func (s *Service) Delete(ctx context.Context) error { } // get retrieves a STACKIT public IP by ID. -func (c *Client) get(ctx context.Context, id string) (*PublicIP, error) { +func (c *client) get(ctx context.Context, id string) (*PublicIP, error) { if id == "" { return nil, fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) } @@ -102,12 +102,12 @@ func (c *Client) get(ctx context.Context, id string) (*PublicIP, error) { } // findByTags finds a single STACKIT public IP matching the given tag selector. -func (c *Client) findByTags(ctx context.Context, tags map[string]string) (*PublicIP, error) { - return shared.FindByTags(ctx, tags, c.ListByTags, "public IP", "public IPs") +func (c *client) findByTags(ctx context.Context, tags map[string]string) (*PublicIP, error) { + return shared.FindByTags(ctx, tags, c.listByTags, "public IP", "public IPs") } -// ListByTags lists STACKIT public IPs matching the given tag selector. -func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*PublicIP, error) { +// listByTags lists STACKIT public IPs matching the given tag selector. +func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*PublicIP, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } @@ -128,7 +128,7 @@ func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*Pub } // attachToServer attaches a public IP to a server idempotently, tolerating Conflict errors. -func (c *Client) attachToServer(ctx context.Context, serverID, publicIPID string) error { +func (c *client) attachToServer(ctx context.Context, serverID, publicIPID string) error { if serverID == "" || publicIPID == "" { return fmt.Errorf("%w: server ID and public IP ID are required", shared.ErrInvalidInput) } @@ -144,8 +144,8 @@ func (c *Client) attachToServer(ctx context.Context, serverID, publicIPID string return nil } -// DetachFromServer detaches a public IP from a server, ignoring NotFound and Conflict errors. -func (c *Client) DetachFromServer(ctx context.Context, serverID, publicIPID string) error { +// detachFromServer detaches a public IP from a server, ignoring NotFound and Conflict errors. +func (c *client) detachFromServer(ctx context.Context, serverID, publicIPID string) error { if serverID == "" || publicIPID == "" { return fmt.Errorf("%w: server ID and public IP ID are required", shared.ErrInvalidInput) } @@ -161,8 +161,8 @@ func (c *Client) DetachFromServer(ctx context.Context, serverID, publicIPID stri return nil } -// DeleteByID deletes a public IP by ID, ignoring NotFound errors. -func (c *Client) DeleteByID(ctx context.Context, id string) error { +// deleteByID deletes a public IP by ID, ignoring NotFound errors. +func (c *client) deleteByID(ctx context.Context, id string) error { if id == "" { return fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) } @@ -175,7 +175,7 @@ func (c *Client) DeleteByID(ctx context.Context, id string) error { return nil } -func (c *Client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*PublicIP, error) { +func (c *client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*PublicIP, error) { publicIP, err := c.resolvePublicIP(ctx, spec) if err != nil { return nil, err @@ -200,7 +200,7 @@ func (c *Client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*Public return out, nil } -func (c *Client) resolvePublicIP(ctx context.Context, spec PublicIPSpec) (*PublicIP, error) { +func (c *client) resolvePublicIP(ctx context.Context, spec PublicIPSpec) (*PublicIP, error) { if spec.ID != "" { publicIP, err := c.get(ctx, spec.ID) if err == nil { @@ -223,21 +223,40 @@ func (c *Client) resolvePublicIP(ctx context.Context, spec PublicIPSpec) (*Publi return publicIP, nil } -func (c *Client) resolveServerID(ctx context.Context, spec PublicIPSpec) (string, error) { +func (c *client) resolvePublicIPsForDelete(ctx context.Context, spec PublicIPSpec) ([]*PublicIP, error) { + if spec.ID != "" { + publicIP, err := c.get(ctx, spec.ID) + if err == nil { + return []*PublicIP{publicIP}, nil + } + if !shared.IsNotFound(err) { + return nil, err + } + } + if len(spec.Tags) == 0 { + return nil, nil + } + return c.listByTags(ctx, spec.Tags) +} + +func (c *client) resolveServerIDs(ctx context.Context, spec PublicIPSpec) ([]string, error) { + var ids []string if spec.ServerID != "" { - return spec.ServerID, nil + ids = append(ids, spec.ServerID) } if len(spec.ServerSelector) == 0 { - return "", nil + return ids, nil } - srv, err := server.NewClient(c.scope).FindByTags(ctx, spec.ServerSelector) + selectedIDs, err := shared.ListServerIDsByTags(ctx, c.scope, spec.ServerSelector) if err != nil { - if shared.IsNotFound(err) { - return "", nil + return nil, err + } + for _, id := range selectedIDs { + if id != spec.ServerID { + ids = append(ids, id) } - return "", err } - return srv.ID, nil + return ids, nil } func (s PublicIPSpec) validate() error { diff --git a/cloud/services/publicip/reconcile_test.go b/cloud/services/publicip/reconcile_test.go index b6fc52b..274c990 100644 --- a/cloud/services/publicip/reconcile_test.go +++ b/cloud/services/publicip/reconcile_test.go @@ -371,8 +371,6 @@ func TestServiceDelete(t *testing.T) { "labels": map[string]any{"cluster": "test"}, }, }}) - case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers/"+testServerID+"/nics"): - writeJSON(t, w, map[string]any{"items": []any{}}) case r.Method == http.MethodDelete && strings.HasSuffix(path, "/servers/"+testServerID+"/public-ips/"+testPublicIPID): detachedPair = testServerID + ":" + testPublicIPID w.WriteHeader(http.StatusNoContent) diff --git a/cloud/services/publicip/service.go b/cloud/services/publicip/service.go index f2abab7..f65d8b9 100644 --- a/cloud/services/publicip/service.go +++ b/cloud/services/publicip/service.go @@ -34,26 +34,26 @@ type Scope interface { PublicIPSpec() PublicIPSpec } -// Client provides direct public IP operations against the STACKIT IaaS API. -type Client struct { - scope shared.IaaSScope +// client provides direct public IP operations against the STACKIT IaaS API. +type client struct { + scope shared.IaaSClientScope } -// NewClient returns a Client for the given scope. -func NewClient(scope shared.IaaSClientScope) *Client { - return &Client{scope: shared.NewIaaSScope(scope)} +// newClient returns a client for the given scope. +func newClient(scope shared.IaaSClientScope) *client { + return &client{scope: scope} } // Service implements public IPs reconciler. type Service struct { scope Scope - client *Client + client *client } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ scope: scope, - client: NewClient(scope), + client: newClient(scope), } } diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index 7c3058c..c6f855b 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -18,7 +18,6 @@ import ( iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" logf "sigs.k8s.io/controller-runtime/pkg/log" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) @@ -45,7 +44,7 @@ func (s *Service) Delete(ctx context.Context) error { } // get retrieves a STACKIT security group by ID. -func (c *Client) get(ctx context.Context, id string) (*SecurityGroup, error) { +func (c *client) get(ctx context.Context, id string) (*SecurityGroup, error) { if id == "" { return nil, fmt.Errorf("%w: security group ID is empty", shared.ErrInvalidInput) } @@ -57,12 +56,12 @@ func (c *Client) get(ctx context.Context, id string) (*SecurityGroup, error) { } // findByTags finds a single STACKIT security group matching the given tag selector. -func (c *Client) findByTags(ctx context.Context, tags map[string]string) (*SecurityGroup, error) { - return shared.FindByTags(ctx, tags, c.ListByTags, "security group", "security groups") +func (c *client) findByTags(ctx context.Context, tags map[string]string) (*SecurityGroup, error) { + return shared.FindByTags(ctx, tags, c.listByTags, "security group", "security groups") } -// ListByTags lists STACKIT security groups matching the given tag selector. -func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*SecurityGroup, error) { +// listByTags lists STACKIT security groups matching the given tag selector. +func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*SecurityGroup, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } @@ -82,24 +81,12 @@ func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*Sec return matched, nil } -// DeleteByID deletes a security group and its rules by ID, ignoring NotFound errors. -func (c *Client) DeleteByID(ctx context.Context, id string) error { - if id == "" { - return fmt.Errorf("%w: security group ID is empty", shared.ErrInvalidInput) - } - rules, ok, err := c.listRulesIfGroupExists(ctx, id) - if err != nil || !ok { - return err - } - return c.deleteSecurityGroupWithRules(ctx, id, rules) -} - // attachToServer attaches a security group to a server idempotently. // Three error responses are tolerated: // - conflict: already attached // - invalid input: duplicate item in security_groups list // - not found: server has no network port yet -func (c *Client) attachToServer(ctx context.Context, serverID, securityGroupID string) error { +func (c *client) attachToServer(ctx context.Context, serverID, securityGroupID string) error { if err := c.scope.IaaSClient().DefaultAPI. AddSecurityGroupToServer(ctx, c.scope.ProjectID(), c.scope.Region(), serverID, securityGroupID). Execute(); err != nil { @@ -112,8 +99,8 @@ func (c *Client) attachToServer(ctx context.Context, serverID, securityGroupID s return nil } -// DetachFromServer detaches a security group from a server, ignoring NotFound and Conflict errors. -func (c *Client) DetachFromServer(ctx context.Context, serverID, securityGroupID string) error { +// detachFromServer detaches a security group from a server, ignoring NotFound and Conflict errors. +func (c *client) detachFromServer(ctx context.Context, serverID, securityGroupID string) error { if err := c.scope.IaaSClient().DefaultAPI. RemoveSecurityGroupFromServer(ctx, c.scope.ProjectID(), c.scope.Region(), serverID, securityGroupID). Execute(); err != nil { @@ -126,7 +113,7 @@ func (c *Client) DetachFromServer(ctx context.Context, serverID, securityGroupID return nil } -func (c *Client) reconcileSecurityGroup(ctx context.Context, spec SecurityGroupSpec) error { +func (c *client) reconcileSecurityGroup(ctx context.Context, spec SecurityGroupSpec) error { if err := spec.validate(); err != nil { return err } @@ -151,23 +138,33 @@ func (c *Client) reconcileSecurityGroup(ctx context.Context, spec SecurityGroupS return nil } -func (c *Client) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpec) error { +type resolvedSecurityGroup struct { + id string + rules []iaas.SecurityGroupRule +} + +func (c *client) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpec) error { if spec.ID == "" && len(spec.Tags) == 0 { return fmt.Errorf("%w: security group ID or tags are required", shared.ErrInvalidInput) } - securityGroupID, rules, err := c.resolveSecurityGroupForDelete(ctx, spec) + resolved, err := c.resolveSecurityGroupsForDelete(ctx, spec) if err != nil { return err } - if securityGroupID != "" { - if err := c.detachSecurityGroupFromServers(ctx, securityGroupID, spec); err != nil { + for _, sg := range resolved { + if err := c.detachSecurityGroupFromServers(ctx, sg.id, spec); err != nil { + return err + } + if err := c.deleteSecurityGroupRules(ctx, sg.id, sg.rules); err != nil { return err } - if err := c.deleteSecurityGroupWithRules(ctx, securityGroupID, rules); err != nil { + } + for _, sg := range resolved { + if err := c.deleteSecurityGroupByID(ctx, sg.id); err != nil { return err } - logf.FromContext(ctx).V(1).Info("Deleted security group", "securityGroupID", securityGroupID) + logf.FromContext(ctx).V(1).Info("Deleted security group", "securityGroupID", sg.id) } if spec.SetID != nil { spec.SetID("") @@ -175,31 +172,40 @@ func (c *Client) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpec return nil } -func (c *Client) resolveSecurityGroupForDelete( +func (c *client) resolveSecurityGroupsForDelete( ctx context.Context, spec SecurityGroupSpec, -) (string, []iaas.SecurityGroupRule, error) { +) ([]resolvedSecurityGroup, error) { if spec.ID != "" { rules, ok, err := c.listRulesIfGroupExists(ctx, spec.ID) - if err != nil || ok { - return spec.ID, rules, err + if err != nil { + return nil, err + } + if ok { + return []resolvedSecurityGroup{{id: spec.ID, rules: rules}}, nil } } if len(spec.Tags) == 0 { - return "", nil, nil - } - securityGroup, err := c.findByTags(ctx, spec.Tags) - if shared.IsNotFound(err) { - return "", nil, nil + return nil, nil } + securityGroups, err := c.listByTags(ctx, spec.Tags) if err != nil { - return "", nil, err + return nil, err } - rules, _, err := c.listRulesIfGroupExists(ctx, securityGroup.ID) - return securityGroup.ID, rules, err + resolved := make([]resolvedSecurityGroup, 0, len(securityGroups)) + for _, sg := range securityGroups { + rules, ok, err := c.listRulesIfGroupExists(ctx, sg.ID) + if err != nil { + return nil, err + } + if ok { + resolved = append(resolved, resolvedSecurityGroup{id: sg.ID, rules: rules}) + } + } + return resolved, nil } -func (c *Client) listRulesIfGroupExists(ctx context.Context, id string) ([]iaas.SecurityGroupRule, bool, error) { +func (c *client) listRulesIfGroupExists(ctx context.Context, id string) ([]iaas.SecurityGroupRule, bool, error) { rules, err := c.listSecurityGroupRules(ctx, id) if shared.IsNotFound(err) { return nil, false, nil @@ -210,7 +216,7 @@ func (c *Client) listRulesIfGroupExists(ctx context.Context, id string) ([]iaas. return rules, true, nil } -func (c *Client) ensureSecurityGroup(ctx context.Context, spec SecurityGroupSpec) (*SecurityGroup, error) { +func (c *client) ensureSecurityGroup(ctx context.Context, spec SecurityGroupSpec) (*SecurityGroup, error) { if spec.ID != "" { securityGroup, err := c.get(ctx, spec.ID) if err == nil { @@ -237,7 +243,7 @@ func (c *Client) ensureSecurityGroup(ctx context.Context, spec SecurityGroupSpec return out, nil } -func (c *Client) deleteSecurityGroupWithRules( +func (c *client) deleteSecurityGroupRules( ctx context.Context, id string, rules []iaas.SecurityGroupRule, @@ -247,6 +253,10 @@ func (c *Client) deleteSecurityGroupWithRules( return err } } + return nil +} + +func (c *client) deleteSecurityGroupByID(ctx context.Context, id string) error { if err := c.scope.IaaSClient().DefaultAPI. DeleteSecurityGroup(ctx, c.scope.ProjectID(), c.scope.Region(), id). Execute(); err != nil { @@ -258,26 +268,26 @@ func (c *Client) deleteSecurityGroupWithRules( return nil } -func (c *Client) detachSecurityGroupFromServers(ctx context.Context, securityGroupID string, spec SecurityGroupSpec) error { +func (c *client) detachSecurityGroupFromServers(ctx context.Context, securityGroupID string, spec SecurityGroupSpec) error { if err := c.detachServers(ctx, securityGroupID, spec.ServerIDs); err != nil { return err } if len(spec.ServerSelector) == 0 { return nil } - serverIDs, err := server.NewClient(c.scope).ListIDsByTags(ctx, spec.ServerSelector) + serverIDs, err := shared.ListServerIDsByTags(ctx, c.scope, spec.ServerSelector) if err != nil { return err } return c.detachServers(ctx, securityGroupID, serverIDs) } -func (c *Client) detachServers(ctx context.Context, securityGroupID string, serverIDs []string) error { +func (c *client) detachServers(ctx context.Context, securityGroupID string, serverIDs []string) error { for _, serverID := range serverIDs { if serverID == "" { continue } - if err := c.DetachFromServer(ctx, serverID, securityGroupID); err != nil { + if err := c.detachFromServer(ctx, serverID, securityGroupID); err != nil { return err } } diff --git a/cloud/services/securitygroup/reconcile_test.go b/cloud/services/securitygroup/reconcile_test.go index fe9b6ef..3d1d53e 100644 --- a/cloud/services/securitygroup/reconcile_test.go +++ b/cloud/services/securitygroup/reconcile_test.go @@ -76,16 +76,6 @@ func newTestIaaSClient(t *testing.T, handler http.Handler) *iaas.APIClient { return iaasClient } -func newTestClient(t *testing.T, handler http.Handler) *securitygroup.Client { - t.Helper() - - return securitygroup.NewClient(&testScope{ - projectID: testProjectID, - region: testRegion, - iaasClient: newTestIaaSClient(t, handler), - }) -} - func newTestService(t *testing.T, specs []securitygroup.SecurityGroupSpec, handler http.Handler) *securitygroup.Service { t.Helper() @@ -782,56 +772,3 @@ func TestServiceDeleteStaleIDFallbackAndDetach(t *testing.T) { t.Fatalf("deletedSGIDs = %v, want [%s %s] (reverse order: node-ssh then bastion-ssh)", deletedSGIDs, testSecurityGroupID, testRemoteSGID) } } - -func TestListByTagsAndDeleteByID(t *testing.T) { - t.Run("ListByTags returns matching security groups", func(t *testing.T) { - client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - writeJSON(t, w, map[string]any{"items": []any{ - map[string]any{"id": testSecurityGroupID, "name": "bastion-ssh", "labels": map[string]any{"cluster": "test"}}, - }}) - })) - - got, err := client.ListByTags(context.Background(), map[string]string{"cluster": "test"}) - if err != nil { - t.Fatalf("ListByTags() error = %v", err) - } - if len(got) != 1 || got[0].ID != testSecurityGroupID { - t.Fatalf("ListByTags() = %#v, want [%s]", got, testSecurityGroupID) - } - }) - - t.Run("DeleteByID deletes rules and security group and ignores NotFound", func(t *testing.T) { - var ( - deletedRules []string - deletedSGID string - ) - client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - path := r.URL.Path - switch { - case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): - writeJSON(t, w, map[string]any{"items": []any{ - map[string]any{"id": testRuleID1, "direction": "ingress"}, - }}) - case r.Method == http.MethodDelete && strings.Contains(path, "/rules/"): - parts := strings.Split(strings.TrimSuffix(path, "/"), "/") - deletedRules = append(deletedRules, parts[len(parts)-1]) - w.WriteHeader(http.StatusNoContent) - case r.Method == http.MethodDelete && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID): - deletedSGID = testSecurityGroupID - w.WriteHeader(http.StatusNoContent) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - } - })) - - if err := client.DeleteByID(context.Background(), testSecurityGroupID); err != nil { - t.Fatalf("DeleteByID() error = %v", err) - } - if !slices.Equal(deletedRules, []string{testRuleID1}) { - t.Fatalf("deletedRules = %v, want [%s]", deletedRules, testRuleID1) - } - if deletedSGID != testSecurityGroupID { - t.Fatalf("deletedSGID = %q, want %q", deletedSGID, testSecurityGroupID) - } - }) -} diff --git a/cloud/services/securitygroup/rules.go b/cloud/services/securitygroup/rules.go index 2878933..3d0b0e4 100644 --- a/cloud/services/securitygroup/rules.go +++ b/cloud/services/securitygroup/rules.go @@ -21,7 +21,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) -func (c *Client) ensureSecurityGroupRules(ctx context.Context, securityGroupID string, rules []Rule) error { +func (c *client) ensureSecurityGroupRules(ctx context.Context, securityGroupID string, rules []Rule) error { if securityGroupID == "" { return fmt.Errorf("%w: security group ID is required", shared.ErrInvalidInput) } @@ -58,7 +58,7 @@ func (c *Client) ensureSecurityGroupRules(ctx context.Context, securityGroupID s return nil } -func (c *Client) resolveRule(ctx context.Context, rule Rule) (Rule, error) { +func (c *client) resolveRule(ctx context.Context, rule Rule) (Rule, error) { rule = rule.normalize() if rule.RemoteSecurityGroupID == "" && len(rule.RemoteSecurityGroupTags) > 0 { remoteSG, err := c.findByTags(ctx, rule.RemoteSecurityGroupTags) @@ -71,7 +71,7 @@ func (c *Client) resolveRule(ctx context.Context, rule Rule) (Rule, error) { return rule, nil } -func (c *Client) createSecurityGroupRule(ctx context.Context, securityGroupID string, rule Rule) error { +func (c *client) createSecurityGroupRule(ctx context.Context, securityGroupID string, rule Rule) error { if _, err := c.scope.IaaSClient().DefaultAPI.CreateSecurityGroupRule(ctx, c.scope.ProjectID(), c.scope.Region(), securityGroupID). CreateSecurityGroupRulePayload(rule.toCreatePayload()). Execute(); err != nil { @@ -80,7 +80,7 @@ func (c *Client) createSecurityGroupRule(ctx context.Context, securityGroupID st return nil } -func (c *Client) listSecurityGroupRules(ctx context.Context, securityGroupID string) ([]iaas.SecurityGroupRule, error) { +func (c *client) listSecurityGroupRules(ctx context.Context, securityGroupID string) ([]iaas.SecurityGroupRule, error) { resp, err := c.scope.IaaSClient().DefaultAPI.ListSecurityGroupRules(ctx, c.scope.ProjectID(), c.scope.Region(), securityGroupID).Execute() if err != nil { return nil, shared.ClassifySDKError("list security group rules", err) @@ -88,7 +88,7 @@ func (c *Client) listSecurityGroupRules(ctx context.Context, securityGroupID str return resp.GetItems(), nil } -func (c *Client) deleteSecurityGroupRule(ctx context.Context, securityGroupID, ruleID string) error { +func (c *client) deleteSecurityGroupRule(ctx context.Context, securityGroupID, ruleID string) error { if ruleID == "" { return nil } diff --git a/cloud/services/securitygroup/service.go b/cloud/services/securitygroup/service.go index ff23043..1a1e97b 100644 --- a/cloud/services/securitygroup/service.go +++ b/cloud/services/securitygroup/service.go @@ -64,26 +64,26 @@ type Scope interface { SecurityGroupSpecs() []SecurityGroupSpec } -// Client provides direct security group operations against the STACKIT IaaS API. -type Client struct { - scope shared.IaaSScope +// client provides direct security group operations against the STACKIT IaaS API. +type client struct { + scope shared.IaaSClientScope } -// NewClient returns a Client for the given scope. -func NewClient(scope shared.IaaSClientScope) *Client { - return &Client{scope: shared.NewIaaSScope(scope)} +// newClient returns a client for the given scope. +func newClient(scope shared.IaaSClientScope) *client { + return &client{scope: scope} } // Service implements security groups reconciler. type Service struct { scope Scope - client *Client + client *client } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ scope: scope, - client: NewClient(scope), + client: newClient(scope), } } diff --git a/cloud/services/server/reconcile.go b/cloud/services/server/reconcile.go index b3eead5..873b99a 100644 --- a/cloud/services/server/reconcile.go +++ b/cloud/services/server/reconcile.go @@ -46,11 +46,11 @@ func (s *Service) Delete(ctx context.Context) error { return fmt.Errorf("%w: server ID or tags are required", shared.ErrInvalidInput) } - deletedID, err := s.client.deleteBySpec(ctx, spec) + deletedIDs, err := s.client.deleteBySpec(ctx, spec) if err != nil { return err } - if deletedID != "" { + for _, deletedID := range deletedIDs { log.V(1).Info("Deleted server", "serverID", deletedID) } if spec.ClearServer != nil { @@ -59,8 +59,8 @@ func (s *Service) Delete(ctx context.Context) error { return nil } -// Get retrieves a STACKIT server by ID. -func (c *Client) Get(ctx context.Context, id string) (*Server, error) { +// get retrieves a STACKIT server by ID. +func (c *client) get(ctx context.Context, id string) (*Server, error) { if id == "" { return nil, fmt.Errorf("%w: server ID is empty", shared.ErrInvalidInput) } @@ -71,63 +71,35 @@ func (c *Client) Get(ctx context.Context, id string) (*Server, error) { return c.serverFromSDK(ctx, srv), nil } -// FindByTags finds a single STACKIT server matching the given tag selector. -func (c *Client) FindByTags(ctx context.Context, tags map[string]string) (*Server, error) { - return shared.FindByTags(ctx, tags, c.ListByTags, "server", "servers") +// findByTags finds a single STACKIT server matching the given tag selector. +func (c *client) findByTags(ctx context.Context, tags map[string]string) (*Server, error) { + return shared.FindByTags(ctx, tags, c.listByTags, "server", "servers") } -// ListByTags lists STACKIT servers matching the given tag selector. -func (c *Client) ListByTags(ctx context.Context, tags map[string]string) ([]*Server, error) { - items, err := c.listSDKServersByTags(ctx, tags, true) - if err != nil { - return nil, err - } - matched := make([]*Server, 0, len(items)) - for _, srv := range items { - matched = append(matched, c.serverFromSDK(ctx, &srv)) - } - return matched, nil -} - -// ListIDsByTags lists the IDs of STACKIT servers matching the given tag selector. -func (c *Client) ListIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { - items, err := c.listSDKServersByTags(ctx, tags, false) - if err != nil { - return nil, err - } - var ids []string - for _, srv := range items { - if id := srv.GetId(); id != "" { - ids = append(ids, id) - } - } - return ids, nil -} - -func (c *Client) listSDKServersByTags(ctx context.Context, tags map[string]string, details bool) ([]iaas.Server, error) { +// listByTags lists STACKIT servers matching the given tag selector. +func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*Server, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } - req := c.scope.IaaSClient().DefaultAPI.ListServers(ctx, c.scope.ProjectID(), c.scope.Region()). - LabelSelector(shared.LabelSelector(tags)) - if details { - req = req.Details(true) - } - resp, err := req.Execute() + resp, err := c.scope.IaaSClient().DefaultAPI.ListServers(ctx, c.scope.ProjectID(), c.scope.Region()). + LabelSelector(shared.LabelSelector(tags)). + Details(true). + Execute() if err != nil { return nil, shared.ClassifySDKError("list servers", err) } - var matched []iaas.Server + matched := []*Server{} for _, srv := range resp.GetItems() { if shared.LabelsContainTags(srv.GetLabels(), tags) { - matched = append(matched, srv) + srv := srv + matched = append(matched, c.serverFromSDK(ctx, &srv)) } } return matched, nil } -// DeleteByID deletes a server by ID, ignoring NotFound errors. -func (c *Client) DeleteByID(ctx context.Context, id string) error { +// deleteByID deletes a server by ID, ignoring NotFound errors. +func (c *client) deleteByID(ctx context.Context, id string) error { if id == "" { return fmt.Errorf("%w: server ID is empty", shared.ErrInvalidInput) } @@ -141,7 +113,7 @@ func (c *Client) DeleteByID(ctx context.Context, id string) error { } // create creates a new STACKIT server from the given spec. -func (c *Client) create(ctx context.Context, spec ServerSpec) (*Server, error) { +func (c *client) create(ctx context.Context, spec ServerSpec) (*Server, error) { payload := spec.toCreatePayload() created, err := c.scope.IaaSClient().DefaultAPI.CreateServer(ctx, c.scope.ProjectID(), c.scope.Region()). CreateServerPayload(payload). @@ -154,9 +126,9 @@ func (c *Client) create(ctx context.Context, spec ServerSpec) (*Server, error) { return out, nil } -func (c *Client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bool, error) { +func (c *client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bool, error) { if spec.ID != "" { - srv, err := c.Get(ctx, spec.ID) + srv, err := c.get(ctx, spec.ID) if err == nil { return srv, false, nil } @@ -164,7 +136,7 @@ func (c *Client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bo return nil, false, err } } - if srv, err := c.FindByTags(ctx, spec.Tags); err == nil { + if srv, err := c.findByTags(ctx, spec.Tags); err == nil { return srv, false, nil } else if !shared.IsNotFound(err) { return nil, false, err @@ -187,31 +159,32 @@ func (c *Client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bo return out, true, nil } -func (c *Client) deleteBySpec(ctx context.Context, spec ServerSpec) (string, error) { +func (c *client) deleteBySpec(ctx context.Context, spec ServerSpec) ([]string, error) { if spec.ID != "" { err := c.scope.IaaSClient().DefaultAPI.DeleteServer(ctx, c.scope.ProjectID(), c.scope.Region(), spec.ID).Execute() if err == nil { - return spec.ID, nil + return []string{spec.ID}, nil } err = shared.ClassifySDKError("delete server", err) if !shared.IsNotFound(err) { - return "", err + return nil, err } } if len(spec.Tags) == 0 { - return "", nil + return nil, nil } - srv, err := c.FindByTags(ctx, spec.Tags) + ids, err := shared.ListServerIDsByTags(ctx, c.scope, spec.Tags) if err != nil { - if shared.IsNotFound(err) { - return "", nil - } - return "", err + return nil, err } - if err := c.DeleteByID(ctx, srv.ID); err != nil { - return "", err + var deletedIDs []string + for _, id := range ids { + if err := c.deleteByID(ctx, id); err != nil { + return nil, err + } + deletedIDs = append(deletedIDs, id) } - return srv.ID, nil + return deletedIDs, nil } func (s ServerSpec) validate() error { @@ -279,7 +252,7 @@ func (s ServerSpec) toCreatePayload() iaas.CreateServerPayload { return *payload } -func (c *Client) serverFromSDK(ctx context.Context, srv *iaas.Server) *Server { +func (c *client) serverFromSDK(ctx context.Context, srv *iaas.Server) *Server { if srv == nil { return nil } diff --git a/cloud/services/server/reconcile_test.go b/cloud/services/server/reconcile_test.go index 5081b7e..e0f4544 100644 --- a/cloud/services/server/reconcile_test.go +++ b/cloud/services/server/reconcile_test.go @@ -76,16 +76,6 @@ func newTestIaaSClient(t *testing.T, handler http.Handler) *iaas.APIClient { return iaasClient } -func newTestClient(t *testing.T, handler http.Handler) *server.Client { - t.Helper() - - return server.NewClient(&testScope{ - projectID: testProjectID, - region: testRegion, - iaasClient: newTestIaaSClient(t, handler), - }) -} - func newTestService(t *testing.T, spec server.ServerSpec, handler http.Handler) *server.Service { t.Helper() @@ -562,8 +552,6 @@ func TestServiceDelete(t *testing.T) { writeJSON(t, w, map[string]any{"items": []any{ map[string]any{"id": testServerID, "name": "node-0", "machineType": "c2i.2", "labels": map[string]any{"cluster": "test"}}, }}) - case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID+"/nics"): - writeJSON(t, w, map[string]any{"items": []any{}}) case r.Method == http.MethodDelete && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID): deletedID = testServerID w.WriteHeader(http.StatusNoContent) @@ -596,8 +584,6 @@ func TestServiceDelete(t *testing.T) { writeJSON(t, w, map[string]any{"items": []any{ map[string]any{"id": testServerID, "name": "node-0", "machineType": "c2i.2", "labels": map[string]any{"cluster": "test"}}, }}) - case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID+"/nics"): - writeJSON(t, w, map[string]any{"items": []any{}}) case r.Method == http.MethodDelete && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID): deletedID = testServerID w.WriteHeader(http.StatusNoContent) @@ -638,27 +624,3 @@ func TestServiceDelete(t *testing.T) { } }) } - -func TestListIDsByTags(t *testing.T) { - client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers") { - if got := r.URL.Query().Get("details"); got != "false" { - t.Fatalf("details = %q, want false", got) - } - writeJSON(t, w, map[string]any{"items": []any{ - map[string]any{"id": testServerID, "name": "node-0", "machineType": "c2i.2", "labels": map[string]any{"cluster": "test"}}, - map[string]any{"id": testOtherServerID, "name": "node-1", "machineType": "c2i.2", "labels": map[string]any{"cluster": "other"}}, - }}) - return - } - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - })) - - ids, err := client.ListIDsByTags(context.Background(), map[string]string{"cluster": "test"}) - if err != nil { - t.Fatalf("ListIDsByTags() error = %v", err) - } - if len(ids) != 1 || ids[0] != testServerID { - t.Fatalf("ListIDsByTags() = %v, want [%s]", ids, testServerID) - } -} diff --git a/cloud/services/server/service.go b/cloud/services/server/service.go index 8547576..d5336cf 100644 --- a/cloud/services/server/service.go +++ b/cloud/services/server/service.go @@ -72,26 +72,26 @@ type Scope interface { ServerSpec() ServerSpec } -// Client provides direct server operations against the STACKIT IaaS API. -type Client struct { - scope shared.IaaSScope +// client provides direct server operations against the STACKIT IaaS API. +type client struct { + scope shared.IaaSClientScope } -// NewClient returns a Client for the given scope. -func NewClient(scope shared.IaaSClientScope) *Client { - return &Client{scope: shared.NewIaaSScope(scope)} +// newClient returns a client for the given scope. +func newClient(scope shared.IaaSClientScope) *client { + return &client{scope: scope} } // Service implements server reconciler. type Service struct { scope Scope - client *Client + client *client } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ scope: scope, - client: NewClient(scope), + client: newClient(scope), } } diff --git a/cloud/services/shared/utils.go b/cloud/services/shared/utils.go index 994a061..a496792 100644 --- a/cloud/services/shared/utils.go +++ b/cloud/services/shared/utils.go @@ -19,6 +19,7 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/oapierror" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" ) // IaaSClientScope is an interface that provides the IaaS client and target scope for IaaS operations. @@ -28,32 +29,33 @@ type IaaSClientScope interface { IaaSClient() *iaas.APIClient } -// IaaSScope holds the IaaS API client and target project/region for IaaS service operations. -type IaaSScope struct { - iaasClient *iaas.APIClient - projectID string - region string +// LoadBalancerClientScope is an interface that provides the LoadBalancer client and target scope for load balancer operations. +type LoadBalancerClientScope interface { + ProjectID() string + Region() string + LoadBalancerClient() *lb.APIClient } -// NewIaaSScope constructs an IaaSScope from the given scope. -func NewIaaSScope(scope IaaSClientScope) IaaSScope { - return IaaSScope{ - iaasClient: scope.IaaSClient(), - projectID: scope.ProjectID(), - region: scope.Region(), +// ListServerIDsByTags lists the IDs of STACKIT servers matching the given tag selector. +func ListServerIDsByTags(ctx context.Context, scope IaaSClientScope, tags map[string]string) ([]string, error) { + if len(tags) == 0 { + return nil, fmt.Errorf("%w: empty tag selector", ErrInvalidInput) } -} - -func (c IaaSScope) ProjectID() string { - return c.projectID -} - -func (c IaaSScope) Region() string { - return c.region -} - -func (c IaaSScope) IaaSClient() *iaas.APIClient { - return c.iaasClient + resp, err := scope.IaaSClient().DefaultAPI.ListServers(ctx, scope.ProjectID(), scope.Region()). + LabelSelector(LabelSelector(tags)). + Execute() + if err != nil { + return nil, ClassifySDKError("list servers", err) + } + var ids []string + for _, srv := range resp.GetItems() { + if LabelsContainTags(srv.GetLabels(), tags) { + if id := srv.GetId(); id != "" { + ids = append(ids, id) + } + } + } + return ids, nil } // FindByTags lists resources by tags and returns the single matching item, or ErrNotFound if empty, or ErrConflict if more than one. diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index 10a3c8f..39a2259 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -152,10 +152,9 @@ var _ = Describe("StackitCluster Controller", func() { Direction: securitygroup.DirectionEgress, }, )) - nodeSGs, err := securitygroup.NewClient(fakeCloud).ListByTags(ctx, scope.NodeSSHTags(got)) - Expect(err).NotTo(HaveOccurred()) - Expect(nodeSGs).To(HaveLen(1)) - Expect(fakeCloud.SecurityGroupRules(nodeSGs[0].ID)).To(ConsistOf( + nodeSG := fakeCloud.FindSecurityGroupByTags(scope.NodeSSHTags(got)) + Expect(nodeSG).NotTo(BeNil()) + Expect(fakeCloud.SecurityGroupRules(nodeSG.ID)).To(ConsistOf( securitygroup.Rule{ Direction: securitygroup.DirectionIngress, Protocol: securitygroup.ProtocolTCP, @@ -201,11 +200,10 @@ var _ = Describe("StackitCluster Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(fakeCloud.SecurityGroupCount()).To(Equal(2)) - nodeSGs, err := securitygroup.NewClient(fakeCloud).ListByTags(ctx, scope.NodeSSHTags(got)) - Expect(err).NotTo(HaveOccurred()) - Expect(nodeSGs).To(HaveLen(1)) + nodeSG := fakeCloud.FindSecurityGroupByTags(scope.NodeSSHTags(got)) + Expect(nodeSG).NotTo(BeNil()) stackitMachine := newStackitMachine(got.Name+"-worker-0", got.Namespace, "worker-0") - stackitMachine.Spec.SecurityGroups = []string{nodeSGs[0].ID} + stackitMachine.Spec.SecurityGroups = []string{nodeSG.ID} machineScope, err := scope.NewMachineScope( k8sClient, &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{Name: got.Name, Namespace: got.Namespace}}, @@ -217,7 +215,7 @@ var _ = Describe("StackitCluster Controller", func() { machineScope.SetCloudClient(fakeCloud) Expect(server.New(machineScope).Reconcile(ctx)).To(Succeed()) workerServerID := stackitMachine.Status.InstanceID - Expect(fakeCloud.ServerHasSecurityGroup(workerServerID, nodeSGs[0].ID)).To(BeTrue()) + Expect(fakeCloud.ServerHasSecurityGroup(workerServerID, nodeSG.ID)).To(BeTrue()) got.Spec.Bastion.Enabled = false Expect(k8sClient.Update(ctx, got)).To(Succeed()) @@ -228,7 +226,7 @@ var _ = Describe("StackitCluster Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(got.Status.Bastion).To(Equal(infrav1.StackitBastionStatus{})) - Expect(fakeCloud.ServerHasSecurityGroup(workerServerID, nodeSGs[0].ID)).To(BeFalse()) + Expect(fakeCloud.ServerHasSecurityGroup(workerServerID, nodeSG.ID)).To(BeFalse()) Expect(fakeCloud.ServerCount()).To(Equal(1)) Expect(fakeCloud.PublicIPCount()).To(Equal(0)) Expect(fakeCloud.SecurityGroupCount()).To(Equal(0)) @@ -1029,9 +1027,7 @@ var _ = Describe("StackitCluster Controller", func() { Expect(fakeCloud.LastCreateServerInput.AvailabilityZone).To(Equal("eu01-2")) Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) - srv, err := server.NewClient(fakeCloud).Get(ctx, got.Status.Bastion.ServerID) - Expect(err).NotTo(HaveOccurred()) - Expect(srv.AvailabilityZone).To(Equal("eu01-2")) + Expect(got.Status.Bastion.ServerID).NotTo(BeEmpty()) }) It("reports an error condition when listing availability zones fails", func() { diff --git a/controller/stackitmachine_controller_test.go b/controller/stackitmachine_controller_test.go index 1512f4b..f95fdbc 100644 --- a/controller/stackitmachine_controller_test.go +++ b/controller/stackitmachine_controller_test.go @@ -30,7 +30,6 @@ import ( cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -157,7 +156,7 @@ var _ = Describe("StackitMachine Controller", func() { Expect(instanceID).NotTo(BeEmpty()) By("removing the server behind the provider's back") - Expect(server.NewClient(fakeCloud).DeleteByID(ctx, instanceID)).To(Succeed()) + fakeCloud.DeleteServer(instanceID) Expect(fakeCloud.ServerCount()).To(Equal(0)) By("reconciling again") @@ -242,13 +241,12 @@ var _ = Describe("StackitMachine Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(got.Status.Ready).To(BeTrue()) Expect(fakeCloud.AddSecurityGroupCalls).To(Equal(1)) - securityGroups, err := securitygroup.NewClient(fakeCloud).ListByTags(ctx, map[string]string{ + securityGroup := fakeCloud.FindSecurityGroupByTags(map[string]string{ util.LabelClusterName: clusterName, util.LabelResourceRole: util.ResourceRoleNodeSSH, }) - Expect(err).NotTo(HaveOccurred()) - Expect(securityGroups).To(HaveLen(1)) - Expect(fakeCloud.SecurityGroupRules(securityGroups[0].ID)).To(ConsistOf( + Expect(securityGroup).NotTo(BeNil()) + Expect(fakeCloud.SecurityGroupRules(securityGroup.ID)).To(ConsistOf( securitygroup.Rule{ Direction: securitygroup.DirectionIngress, Protocol: securitygroup.ProtocolTCP, @@ -260,7 +258,7 @@ var _ = Describe("StackitMachine Controller", func() { Direction: securitygroup.DirectionEgress, }, )) - Expect(fakeCloud.ServerHasSecurityGroup(got.Status.InstanceID, securityGroups[0].ID)).To(BeTrue()) + Expect(fakeCloud.ServerHasSecurityGroup(got.Status.InstanceID, securityGroup.ID)).To(BeTrue()) }) It("marks conditions not ready when bastion node SSH access fails", func() { @@ -542,7 +540,7 @@ var _ = Describe("StackitMachine Controller", func() { Expect(instanceID).NotTo(BeEmpty()) By("removing the server behind the provider's back") - Expect(server.NewClient(fakeCloud).DeleteByID(ctx, instanceID)).To(Succeed()) + fakeCloud.DeleteServer(instanceID) Expect(fakeCloud.ServerCount()).To(Equal(0)) By("reconciling into the terminal state") diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index f5686fb..e8e030b 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -35,9 +35,6 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/test/utils" "github.com/stackitcloud/cluster-api-provider-stackit/util" @@ -427,9 +424,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying the VM exists in STACKIT") Eventually(func(g Gomega) { - server, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(server.ID).To(Equal(instanceID)) + expectServerExistsInCloud(ctx, g, cloudClient, instanceID) }, 5*time.Minute, 15*time.Second).Should(Succeed()) By("deleting the StackitMachine to trigger VM cleanup") @@ -439,18 +434,12 @@ var _ = Describe("Manager", Ordered, func() { By("verifying the VM was deleted from STACKIT") Eventually(func(g Gomega) { - _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) + expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) }, 20*time.Minute, 15*time.Second).Should(Succeed()) By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(servers).To(BeEmpty()) - loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(loadBalancers).To(BeEmpty()) + expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) }, 5*time.Minute, 15*time.Second).Should(Succeed()) }) @@ -495,12 +484,12 @@ var _ = Describe("Manager", Ordered, func() { Eventually(func(g Gomega) { output := kubectlOutput(g, "get", "stackitcluster", clusterName, "-n", cfg.Namespace, "-o", "jsonpath={.status.bastion.publicIP}") g.Expect(output).NotTo(BeEmpty()) - publicIPs, err := publicip.NewClient(cloudClient).ListByTags(ctx, stackitE2EBastionTags(testID)) + publicIPCount, err := countPublicIPsByTags(ctx, cloudClient, stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) - g.Expect(publicIPs).To(HaveLen(1)) - securityGroups, err := securitygroup.NewClient(cloudClient).ListByTags(ctx, stackitE2EBastionTags(testID)) + g.Expect(publicIPCount).To(Equal(1)) + sgCount, err := countSecurityGroupsByTags(ctx, cloudClient, stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) - g.Expect(securityGroups).To(HaveLen(1)) + g.Expect(sgCount).To(Equal(1)) }, 10*time.Minute, 15*time.Second).Should(Succeed()) } @@ -520,9 +509,9 @@ var _ = Describe("Manager", Ordered, func() { if cfg.BastionEnabled { By("verifying the node SSH security group exists") Eventually(func(g Gomega) { - securityGroups, err := securitygroup.NewClient(cloudClient).ListByTags(ctx, stackitE2ENodeSSHTags(testID)) + sgCount, err := countSecurityGroupsByTags(ctx, cloudClient, stackitE2ENodeSSHTags(testID)) g.Expect(err).NotTo(HaveOccurred()) - g.Expect(securityGroups).To(HaveLen(1)) + g.Expect(sgCount).To(Equal(1)) }, 10*time.Minute, 15*time.Second).Should(Succeed()) } @@ -530,9 +519,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range instanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - server, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(server.ID).To(Equal(instanceID)) + expectServerExistsInCloud(ctx, g, cloudClient, instanceID) }, 5*time.Minute, 15*time.Second).Should(Succeed()) } @@ -555,22 +542,16 @@ var _ = Describe("Manager", Ordered, func() { By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) + expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) + publicIPCount, err := countPublicIPsByTags(ctx, cloudClient, stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) - g.Expect(servers).To(BeEmpty()) - loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) + g.Expect(publicIPCount).To(Equal(0)) + sgCount, err := countSecurityGroupsByTags(ctx, cloudClient, stackitE2EBastionTags(testID)) g.Expect(err).NotTo(HaveOccurred()) - g.Expect(loadBalancers).To(BeEmpty()) - publicIPs, err := publicip.NewClient(cloudClient).ListByTags(ctx, stackitE2EBastionTags(testID)) + g.Expect(sgCount).To(Equal(0)) + sgCount, err = countSecurityGroupsByTags(ctx, cloudClient, stackitE2ENodeSSHTags(testID)) g.Expect(err).NotTo(HaveOccurred()) - g.Expect(publicIPs).To(BeEmpty()) - sgClient := securitygroup.NewClient(cloudClient) - securityGroups, err := sgClient.ListByTags(ctx, stackitE2EBastionTags(testID)) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(securityGroups).To(BeEmpty()) - securityGroups, err = sgClient.ListByTags(ctx, stackitE2ENodeSSHTags(testID)) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(securityGroups).To(BeEmpty()) + g.Expect(sgCount).To(Equal(0)) }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) @@ -633,12 +614,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(servers).To(BeEmpty()) - loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(loadBalancers).To(BeEmpty()) + expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) @@ -728,12 +704,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(servers).To(BeEmpty()) - loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(loadBalancers).To(BeEmpty()) + expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) @@ -809,9 +780,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range scaledUpInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - server, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(server.ID).To(Equal(instanceID)) + expectServerExistsInCloud(ctx, g, cloudClient, instanceID) }, 5*time.Minute, 15*time.Second).Should(Succeed()) } @@ -836,8 +805,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range deletedInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) + expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -850,19 +818,13 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range observedInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) + expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(servers).To(BeEmpty()) - loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(loadBalancers).To(BeEmpty()) + expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) @@ -966,9 +928,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range scaledUpInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - server, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(server.ID).To(Equal(instanceID)) + expectServerExistsInCloud(ctx, g, cloudClient, instanceID) }, 5*time.Minute, 15*time.Second).Should(Succeed()) } @@ -1006,8 +966,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range deletedInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) + expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -1018,12 +977,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(servers).To(BeEmpty()) - loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(loadBalancers).To(BeEmpty()) + expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) @@ -1114,8 +1068,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying the old worker VM was deleted from STACKIT") Eventually(func(g Gomega) { - _, err := server.NewClient(cloudClient).Get(ctx, initialInstanceIDs[0]) - g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected old server %s to be deleted, got %v", initialInstanceIDs[0], err) + expectServerGoneFromCloud(ctx, g, cloudClient, initialInstanceIDs[0]) }, 20*time.Minute, 15*time.Second).Should(Succeed()) By("deleting the upgraded workload Cluster") @@ -1130,19 +1083,13 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range observedInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) + expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(servers).To(BeEmpty()) - loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(loadBalancers).To(BeEmpty()) + expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) @@ -1264,8 +1211,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range deletedInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected old server %s to be deleted, got %v", instanceID, err) + expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -1276,12 +1222,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(servers).To(BeEmpty()) - loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(loadBalancers).To(BeEmpty()) + expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) @@ -1389,8 +1330,7 @@ var _ = Describe("Manager", Ordered, func() { for _, instanceID := range deletedInstanceIDs { instanceID := instanceID Eventually(func(g Gomega) { - _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - g.Expect(shared.IsNotFound(err)).To(BeTrue(), "expected old server %s to be deleted, got %v", instanceID, err) + expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) }, 20*time.Minute, 15*time.Second).Should(Succeed()) } @@ -1401,12 +1341,7 @@ var _ = Describe("Manager", Ordered, func() { By("verifying no tagged STACKIT resources remain for the e2e test ID") Eventually(func(g Gomega) { - servers, err := server.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(servers).To(BeEmpty()) - loadBalancers, err := loadbalancer.NewClient(cloudClient).ListByTags(ctx, leakTags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(loadBalancers).To(BeEmpty()) + expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) }) @@ -3116,20 +3051,92 @@ func appendUnique(values []string, additions ...string) []string { return values } +func expectServerExistsInCloud(ctx context.Context, g Gomega, cloudClient cloud.Client, instanceID string) { + srv, err := cloudClient.IaaSClient().DefaultAPI.GetServer(ctx, cloudClient.ProjectID(), cloudClient.Region(), instanceID).Execute() + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(srv.GetId()).To(Equal(instanceID)) +} + +func expectServerGoneFromCloud(ctx context.Context, g Gomega, cloudClient cloud.Client, instanceID string) { + _, err := cloudClient.IaaSClient().DefaultAPI.GetServer(ctx, cloudClient.ProjectID(), cloudClient.Region(), instanceID).Execute() + g.Expect(err).To(HaveOccurred()) + g.Expect(shared.IsNotFound(shared.ClassifySDKError("get server", err))).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) +} + +func expectNoLeakedServersOrLoadBalancers(ctx context.Context, g Gomega, cloudClient cloud.Client, tags map[string]string) { + serverIDs, err := shared.ListServerIDsByTags(ctx, cloudClient, tags) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(serverIDs).To(BeEmpty()) + lbCount, err := countLoadBalancersByTags(ctx, cloudClient, tags) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(lbCount).To(Equal(0)) +} + +func countLoadBalancersByTags(ctx context.Context, cloudClient cloud.Client, tags map[string]string) (int, error) { + resp, err := cloudClient.LoadBalancerClient().DefaultAPI.ListLoadBalancers(ctx, cloudClient.ProjectID(), cloudClient.Region()).Execute() + if err != nil { + return 0, shared.ClassifySDKError("list load balancers", err) + } + labels := loadbalancer.Labels(tags) + count := 0 + for _, candidate := range resp.GetLoadBalancers() { + if shared.MapContains(candidate.GetLabels(), labels) { + count++ + } + } + return count, nil +} + +func countPublicIPsByTags(ctx context.Context, cloudClient cloud.Client, tags map[string]string) (int, error) { + resp, err := cloudClient.IaaSClient().DefaultAPI.ListPublicIPs(ctx, cloudClient.ProjectID(), cloudClient.Region()). + LabelSelector(shared.LabelSelector(tags)). + Execute() + if err != nil { + return 0, shared.ClassifySDKError("list public IPs", err) + } + count := 0 + for _, item := range resp.GetItems() { + if shared.LabelsContainTags(item.GetLabels(), tags) { + count++ + } + } + return count, nil +} + +func countSecurityGroupsByTags(ctx context.Context, cloudClient cloud.Client, tags map[string]string) (int, error) { + resp, err := cloudClient.IaaSClient().DefaultAPI.ListSecurityGroups(ctx, cloudClient.ProjectID(), cloudClient.Region()). + LabelSelector(shared.LabelSelector(tags)). + Execute() + if err != nil { + return 0, shared.ClassifySDKError("list security groups", err) + } + count := 0 + for _, item := range resp.GetItems() { + if shared.LabelsContainTags(item.GetLabels(), tags) { + count++ + } + } + return count, nil +} + func cleanupCloudServersByID(ctx context.Context, cloudClient cloud.Client, instanceIDs []string) { for _, instanceID := range instanceIDs { - if err := server.NewClient(cloudClient).DeleteByID(ctx, instanceID); err != nil { - _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API server cleanup warning for %s: %v\n", instanceID, err) - continue + if err := cloudClient.IaaSClient().DefaultAPI.DeleteServer(ctx, cloudClient.ProjectID(), cloudClient.Region(), instanceID).Execute(); err != nil { + classified := shared.ClassifySDKError("delete server", err) + if !shared.IsNotFound(classified) { + _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API server cleanup warning for %s: %v\n", instanceID, classified) + continue + } } deadline := time.Now().Add(10 * time.Minute) for time.Now().Before(deadline) { - _, err := server.NewClient(cloudClient).Get(ctx, instanceID) - if shared.IsNotFound(err) { - break - } + _, err := cloudClient.IaaSClient().DefaultAPI.GetServer(ctx, cloudClient.ProjectID(), cloudClient.Region(), instanceID).Execute() if err != nil { - _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API server cleanup verification warning for %s: %v\n", instanceID, err) + classified := shared.ClassifySDKError("get server", err) + if shared.IsNotFound(classified) { + break + } + _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API server cleanup verification warning for %s: %v\n", instanceID, classified) break } time.Sleep(15 * time.Second) From dbab6b9d0e9c3c4c2c8e8238d698d103e079e2ba Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 10:54:27 +0100 Subject: [PATCH 20/60] fix: continue on partial delete errors and delete servers before security groups in CleanupByTags --- cloud/cleanup.go | 14 +++--- cloud/cleanup_test.go | 34 ++++++++++++++ cloud/services/loadbalancer/reconcile.go | 8 +++- cloud/services/publicip/reconcile.go | 16 ++++++- cloud/services/securitygroup/reconcile.go | 55 ++++++++++++++++------- cloud/services/server/reconcile.go | 17 ++++--- 6 files changed, 112 insertions(+), 32 deletions(-) diff --git a/cloud/cleanup.go b/cloud/cleanup.go index ee6cc10..7f9bbed 100644 --- a/cloud/cleanup.go +++ b/cloud/cleanup.go @@ -12,8 +12,8 @@ package cloud import ( "context" + "errors" "fmt" - "strings" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" @@ -57,7 +57,9 @@ func (s *cleanupScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { // CleanupByTags deletes provider-managed cloud resources matching tags without // relying on Kubernetes objects. Load balancers are deleted before servers so -// server deletion is not blocked by target attachments. +// server deletion is not blocked by target attachments, and servers are +// deleted before security groups so attached servers do not block security +// group deletion if server detachment fails or lags. func CleanupByTags(ctx context.Context, client Client, tags map[string]string) error { if len(tags) == 0 { return fmt.Errorf("%w: empty cleanup tag selector", shared.ErrInvalidInput) @@ -67,20 +69,20 @@ func CleanupByTags(ctx context.Context, client Client, tags map[string]string) e Client: client, tags: tags, } - var errs []string + var errs []error for _, deleteFn := range []func(context.Context) error{ publicip.New(scope).Delete, loadbalancer.New(scope).Delete, - securitygroup.New(scope).Delete, server.New(scope).Delete, + securitygroup.New(scope).Delete, } { if err := deleteFn(ctx); err != nil { - errs = append(errs, err.Error()) + errs = append(errs, err) } } if len(errs) > 0 { - return fmt.Errorf("%w: %s", shared.ErrTransient, strings.Join(errs, "; ")) + return fmt.Errorf("%w: %w", shared.ErrTransient, errors.Join(errs...)) } return nil } diff --git a/cloud/cleanup_test.go b/cloud/cleanup_test.go index 19245ec..3c701c1 100644 --- a/cloud/cleanup_test.go +++ b/cloud/cleanup_test.go @@ -12,6 +12,7 @@ package cloud_test import ( "context" + "fmt" "maps" "testing" @@ -179,3 +180,36 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) { t.Fatal("expected other security group to remain") } } + +func TestCleanupByTagsContinuesWhenIndividualDeletesFail(t *testing.T) { + ctx := context.Background() + client := cloudfake.New() + tags := map[string]string{ + "cluster-api-provider-stackit/e2e": "true", + "cluster-api-provider-stackit/test-id": "test-partial-fail", + } + + createTestResources(t, client, "matched", tags) + + // Inject a transient failure on the first server delete; the remaining + // server, load balancer, public IP, and unattached security group should + // still be cleaned up in the same pass. + client.FailNextDeleteServer = fmt.Errorf("injected server delete failure") + + if err := cloud.CleanupByTags(ctx, client, tags); err == nil { + t.Fatal("CleanupByTags() error = nil, want non-nil when server delete fails") + } + + if got := client.ServerCount(); got != 1 { + t.Fatalf("remaining server count = %d, want 1 (the one that failed to delete)", got) + } + if got := client.LoadBalancerCount(); got != 0 { + t.Fatalf("remaining load balancer count = %d, want 0", got) + } + if got := client.PublicIPCount(); got != 0 { + t.Fatalf("remaining public IP count = %d, want 0", got) + } + if got := client.SecurityGroupCount(); got != 0 { + t.Fatalf("remaining security group count = %d, want 0", got) + } +} diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index ba59579..7edca7e 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -12,6 +12,7 @@ package loadbalancer import ( "context" + "errors" "fmt" "strings" @@ -65,12 +66,17 @@ func (s *Service) Delete(ctx context.Context) error { if err != nil { return err } + var errs []error for _, loadBalancerID := range loadBalancerIDs { if err := s.client.deleteByID(ctx, loadBalancerID); err != nil { - return err + errs = append(errs, err) + continue } log.V(1).Info("Deleted load balancer", "loadBalancerID", loadBalancerID) } + if len(errs) > 0 { + return errors.Join(errs...) + } if spec.SetID != nil { spec.SetID("") } diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index 9117c64..7e18990 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -12,6 +12,7 @@ package publicip import ( "context" + "errors" "fmt" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" @@ -71,17 +72,28 @@ func (s *Service) Delete(ctx context.Context) error { if err != nil { return err } + var errs []error for _, publicIP := range publicIPs { + var detachFailed bool for _, serverID := range serverIDs { if err := s.client.detachFromServer(ctx, serverID, publicIP.ID); err != nil { - return err + errs = append(errs, err) + detachFailed = true + break } } + if detachFailed { + continue + } if err := s.client.deleteByID(ctx, publicIP.ID); err != nil { - return err + errs = append(errs, err) + continue } log.V(1).Info("Deleted public IP", "publicIPID", publicIP.ID) } + if len(errs) > 0 { + return errors.Join(errs...) + } if spec.SetPublicIP != nil { spec.SetPublicIP("", "") diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index c6f855b..998f775 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -12,6 +12,7 @@ package securitygroup import ( "context" + "errors" "fmt" "slices" @@ -34,11 +35,15 @@ func (s *Service) Reconcile(ctx context.Context) error { func (s *Service) Delete(ctx context.Context) error { log := logf.FromContext(ctx) + var errs []error for _, spec := range slices.Backward(s.scope.SecurityGroupSpecs()) { if err := s.client.deleteSecurityGroup(ctx, spec); err != nil { - return err + errs = append(errs, err) } } + if len(errs) > 0 { + return errors.Join(errs...) + } log.V(1).Info("Deleted security group resources") return nil } @@ -148,24 +153,32 @@ func (c *client) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpec return fmt.Errorf("%w: security group ID or tags are required", shared.ErrInvalidInput) } - resolved, err := c.resolveSecurityGroupsForDelete(ctx, spec) - if err != nil { - return err + resolved, resolveErr := c.resolveSecurityGroupsForDelete(ctx, spec) + if resolveErr != nil && len(resolved) == 0 { + return resolveErr + } + var errs []error + if resolveErr != nil { + errs = append(errs, resolveErr) } for _, sg := range resolved { if err := c.detachSecurityGroupFromServers(ctx, sg.id, spec); err != nil { - return err + errs = append(errs, err) } if err := c.deleteSecurityGroupRules(ctx, sg.id, sg.rules); err != nil { - return err + errs = append(errs, err) } } for _, sg := range resolved { if err := c.deleteSecurityGroupByID(ctx, sg.id); err != nil { - return err + errs = append(errs, err) + continue } logf.FromContext(ctx).V(1).Info("Deleted security group", "securityGroupID", sg.id) } + if len(errs) > 0 { + return errors.Join(errs...) + } if spec.SetID != nil { spec.SetID("") } @@ -193,16 +206,18 @@ func (c *client) resolveSecurityGroupsForDelete( return nil, err } resolved := make([]resolvedSecurityGroup, 0, len(securityGroups)) + var errs []error for _, sg := range securityGroups { rules, ok, err := c.listRulesIfGroupExists(ctx, sg.ID) if err != nil { - return nil, err + errs = append(errs, err) + continue } if ok { resolved = append(resolved, resolvedSecurityGroup{id: sg.ID, rules: rules}) } } - return resolved, nil + return resolved, errors.Join(errs...) } func (c *client) listRulesIfGroupExists(ctx context.Context, id string) ([]iaas.SecurityGroupRule, bool, error) { @@ -248,12 +263,13 @@ func (c *client) deleteSecurityGroupRules( id string, rules []iaas.SecurityGroupRule, ) error { + var errs []error for _, rule := range rules { if err := c.deleteSecurityGroupRule(ctx, id, rule.GetId()); err != nil { - return err + errs = append(errs, err) } } - return nil + return errors.Join(errs...) } func (c *client) deleteSecurityGroupByID(ctx context.Context, id string) error { @@ -269,29 +285,34 @@ func (c *client) deleteSecurityGroupByID(ctx context.Context, id string) error { } func (c *client) detachSecurityGroupFromServers(ctx context.Context, securityGroupID string, spec SecurityGroupSpec) error { + var errs []error if err := c.detachServers(ctx, securityGroupID, spec.ServerIDs); err != nil { - return err + errs = append(errs, err) } if len(spec.ServerSelector) == 0 { - return nil + return errors.Join(errs...) } serverIDs, err := shared.ListServerIDsByTags(ctx, c.scope, spec.ServerSelector) if err != nil { - return err + return errors.Join(append(errs, err)...) + } + if err := c.detachServers(ctx, securityGroupID, serverIDs); err != nil { + errs = append(errs, err) } - return c.detachServers(ctx, securityGroupID, serverIDs) + return errors.Join(errs...) } func (c *client) detachServers(ctx context.Context, securityGroupID string, serverIDs []string) error { + var errs []error for _, serverID := range serverIDs { if serverID == "" { continue } if err := c.detachFromServer(ctx, serverID, securityGroupID); err != nil { - return err + errs = append(errs, err) } } - return nil + return errors.Join(errs...) } func (s SecurityGroupSpec) validate() error { diff --git a/cloud/services/server/reconcile.go b/cloud/services/server/reconcile.go index 873b99a..6ccb42c 100644 --- a/cloud/services/server/reconcile.go +++ b/cloud/services/server/reconcile.go @@ -13,6 +13,7 @@ package server import ( "context" "encoding/base64" + "errors" "fmt" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" @@ -47,12 +48,12 @@ func (s *Service) Delete(ctx context.Context) error { } deletedIDs, err := s.client.deleteBySpec(ctx, spec) - if err != nil { - return err - } for _, deletedID := range deletedIDs { log.V(1).Info("Deleted server", "serverID", deletedID) } + if err != nil { + return err + } if spec.ClearServer != nil { spec.ClearServer() } @@ -177,14 +178,18 @@ func (c *client) deleteBySpec(ctx context.Context, spec ServerSpec) ([]string, e if err != nil { return nil, err } - var deletedIDs []string + var ( + deletedIDs []string + errs []error + ) for _, id := range ids { if err := c.deleteByID(ctx, id); err != nil { - return nil, err + errs = append(errs, err) + continue } deletedIDs = append(deletedIDs, id) } - return deletedIDs, nil + return deletedIDs, errors.Join(errs...) } func (s ServerSpec) validate() error { From eb45e5b61d588a7a3e90b0271e31eb28288781b3 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 11:12:54 +0100 Subject: [PATCH 21/60] refactor: defer NIC lookup in server listByTags until single server is matched --- cloud/services/server/reconcile.go | 12 ++++++++---- cloud/services/server/reconcile_test.go | 9 +++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cloud/services/server/reconcile.go b/cloud/services/server/reconcile.go index 6ccb42c..29438a3 100644 --- a/cloud/services/server/reconcile.go +++ b/cloud/services/server/reconcile.go @@ -74,11 +74,15 @@ func (c *client) get(ctx context.Context, id string) (*Server, error) { // findByTags finds a single STACKIT server matching the given tag selector. func (c *client) findByTags(ctx context.Context, tags map[string]string) (*Server, error) { - return shared.FindByTags(ctx, tags, c.listByTags, "server", "servers") + srv, err := shared.FindByTags(ctx, tags, c.listByTags, "server", "servers") + if err != nil { + return nil, err + } + return c.serverFromSDK(ctx, srv), nil } // listByTags lists STACKIT servers matching the given tag selector. -func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*Server, error) { +func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*iaas.Server, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } @@ -89,11 +93,11 @@ func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*Ser if err != nil { return nil, shared.ClassifySDKError("list servers", err) } - matched := []*Server{} + matched := []*iaas.Server{} for _, srv := range resp.GetItems() { if shared.LabelsContainTags(srv.GetLabels(), tags) { srv := srv - matched = append(matched, c.serverFromSDK(ctx, &srv)) + matched = append(matched, &srv) } } return matched, nil diff --git a/cloud/services/server/reconcile_test.go b/cloud/services/server/reconcile_test.go index e0f4544..9d86347 100644 --- a/cloud/services/server/reconcile_test.go +++ b/cloud/services/server/reconcile_test.go @@ -421,17 +421,14 @@ func TestServiceReconcileReuseAndErrors(t *testing.T) { } svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - switch { - case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers"): + if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers") { writeJSON(t, w, map[string]any{"items": []any{ map[string]any{"id": testServerID, "name": "node-0", "machineType": "c2i.2", "labels": map[string]any{"cluster": "test"}}, map[string]any{"id": testOtherServerID, "name": "node-1", "machineType": "c2i.2", "labels": map[string]any{"cluster": "test"}}, }}) - case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/nics"): - writeJSON(t, w, map[string]any{"items": []any{}}) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + return } + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) })) err := svc.Reconcile(context.Background()) From a0953b55a0f1b411bf84850c5d5ae860e4a0a25a Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 11:39:58 +0100 Subject: [PATCH 22/60] fix: preserve bastion CloudInitHash on tag adoption and gate bastion events --- controller/stackitcluster_bastion.go | 10 +++-- controller/stackitcluster_controller_test.go | 46 ++++++++++++++++++++ scope/cluster_scope.go | 32 +++++++++----- scope/cluster_scope_test.go | 26 +++++++++++ 4 files changed, 100 insertions(+), 14 deletions(-) diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index a5377dc..5af8ddb 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -51,11 +51,14 @@ func (r *StackitClusterReconciler) reconcileBastion( // the status keeps the tag-based cleanup to once per cluster. condition := meta.FindStatusCondition(stackitCluster.Status.Conditions, infrav1.ClusterBastionReadyCondition) if condition == nil || condition.Reason != bastionDisabledReason { + hadBastion := hasBastionStatus(stackitCluster.Status.Bastion) if err := r.deleteBastionResources(ctx, clusterScope); err != nil { return ctrl.Result{}, false, err } clusterScope.ClearBastionStatus() - record.Event(stackitCluster, "BastionDeleted", "Deleted bastion") + if hadBastion { + record.Event(stackitCluster, "BastionDeleted", "Deleted bastion") + } } clusterScope.SetConditions( metav1.ConditionTrue, @@ -110,11 +113,10 @@ func (r *StackitClusterReconciler) reconcileBastion( return ctrl.Result{RequeueAfter: retryableErrorRequeueAfter}, false, nil } - hadBastionServer := stackitCluster.Status.Bastion.ServerID != "" if err := server.New(clusterScope).Reconcile(ctx); err != nil { return ctrl.Result{}, false, err } - if !hadBastionServer && stackitCluster.Status.Bastion.ServerID != "" { + if clusterScope.BastionServerCreated() { record.Eventf( stackitCluster, "BastionCreated", "Created bastion %s", stackitCluster.Status.Bastion.ServerID, ) @@ -178,7 +180,7 @@ func validateBastionSpec(spec infrav1.StackitBastionSpec) error { } func hasBastionStatus(status infrav1.StackitBastionStatus) bool { - return status.ServerID != "" || status.PublicIPID != "" || status.PublicIP != "" || status.SecurityGroupID != "" + return status.ServerID != "" || status.PublicIPID != "" || status.PublicIP != "" || status.SecurityGroupID != "" || status.CloudInitHash != "" } func (r *StackitClusterReconciler) deleteBastionResources( diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index 39a2259..902ca68 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -509,6 +509,52 @@ var _ = Describe("StackitCluster Controller", func() { Expect(string(fakeCloud.ServerUserData(got.Status.Bastion.ServerID))).To(Equal(configMap.Data["userData"])) }) + It("recreates the bastion when cloud-init changes even after bastion ServerID was lost from status", func() { + cloudInitName := "bastion-cloud-init-lost-id-" + clusterName + expectBastionCloudInit( + ctx, reconciler, request, stackitKey, fakeCloud, + "ConfigMap", cloudInitName, + "#cloud-config\npackages:\n- htop\n", createCloudInitConfigMap, + ) + + got := &infrav1.StackitCluster{} + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + oldServerID := got.Status.Bastion.ServerID + + By("losing Status.Bastion.ServerID while cloud-init changes") + got.Status.Bastion.ServerID = "" + Expect(k8sClient.Status().Update(ctx, got)).To(Succeed()) + + configMap := &corev1.ConfigMap{} + Expect(k8sClient.Get(ctx, types.NamespacedName{Name: cloudInitName, Namespace: namespace}, configMap)).To(Succeed()) + configMap.Data["userData"] = "#cloud-config\npackages:\n- jq\n" + Expect(k8sClient.Update(ctx, configMap)).To(Succeed()) + + By("adopting the existing server without overwriting CloudInitHash") + result, err := reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + Expect(result).To(Equal(reconcile.Result{})) + + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + Expect(got.Status.Bastion.ServerID).To(Equal(oldServerID)) + Expect(got.Status.Bastion.CloudInitHash).NotTo(Equal(bastionCloudInitHash([]byte(configMap.Data["userData"])))) + + By("detecting the cloud-init drift on the subsequent reconcile and recreating the bastion") + result, err = reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + Expect(result.RequeueAfter).To(BeNumerically(">", 0)) + Expect(fakeCloud.ServerCount()).To(Equal(0)) + + result, err = reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + Expect(result).To(Equal(reconcile.Result{})) + + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + Expect(got.Status.Bastion.ServerID).NotTo(Equal(oldServerID)) + Expect(got.Status.Bastion.CloudInitHash).To(Equal(bastionCloudInitHash([]byte(configMap.Data["userData"])))) + Expect(string(fakeCloud.ServerUserData(got.Status.Bastion.ServerID))).To(Equal(configMap.Data["userData"])) + }) + It("marks the network not ready when the configured network does not exist", func() { got := &infrav1.StackitCluster{} Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index ba72717..3a584b2 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -55,6 +55,7 @@ type ClusterScope struct { bastionCloudInitHash string bastionServerState string bastionServerPowerStatus string + bastionServerCreated bool patchHelper *patch.Helper } @@ -117,6 +118,25 @@ func (s *ClusterScope) BastionServerState() (state, powerStatus string) { return s.bastionServerState, s.bastionServerPowerStatus } +func (s *ClusterScope) BastionServerCreated() bool { + return s.bastionServerCreated +} + +func (s *ClusterScope) setBastionServer(srv *server.Server, created bool) { + s.bastionServerCreated = created + s.bastionServerState = srv.State + s.bastionServerPowerStatus = srv.PowerStatus + s.StackitCluster.Status.Bastion.ServerID = srv.ID + if created { + s.StackitCluster.Status.Bastion.CloudInitHash = s.bastionCloudInitHash + } +} + +func (s *ClusterScope) clearBastionServer() { + s.StackitCluster.Status.Bastion.ServerID = "" + s.StackitCluster.Status.Bastion.CloudInitHash = "" +} + func (s *ClusterScope) ServerSpec() server.ServerSpec { return server.ServerSpec{ ID: s.StackitCluster.Status.Bastion.ServerID, @@ -129,16 +149,8 @@ func (s *ClusterScope) ServerSpec() server.ServerSpec { UserData: s.bastionCloudInit, Tags: s.bastionTags(), RootVolume: rootVolumeFromSpec(s.StackitCluster.Spec.Bastion.RootVolume), - SetServer: func(srv *server.Server, _ bool) { - s.bastionServerState = srv.State - s.bastionServerPowerStatus = srv.PowerStatus - s.StackitCluster.Status.Bastion.ServerID = srv.ID - s.StackitCluster.Status.Bastion.CloudInitHash = s.bastionCloudInitHash - }, - ClearServer: func() { - s.StackitCluster.Status.Bastion.ServerID = "" - s.StackitCluster.Status.Bastion.CloudInitHash = "" - }, + SetServer: s.setBastionServer, + ClearServer: s.clearBastionServer, } } diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index 6be2895..fae1165 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -17,6 +17,7 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" ) func TestDefaultAvailabilityZone(t *testing.T) { @@ -84,3 +85,28 @@ func TestAvailabilityZoneSpecUsesResolvedRegion(t *testing.T) { t.Fatalf("FailureDomains[0].Attributes[region] = %q, want %q", gotRegion, "eu01") } } + +func TestServerSpecSetServerPreservesCloudInitHashOnAdoptedServer(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{}, + } + clusterScope.SetBastionCloudInit([]byte("#cloud-config\npackages:\n- jq\n"), "new-hash") + + spec := clusterScope.ServerSpec() + spec.SetServer(&server.Server{ID: "srv-adopted", State: "ACTIVE", PowerStatus: "RUNNING"}, false) + + if clusterScope.BastionServerCreated() { + t.Fatal("BastionServerCreated() = true on adopted server, want false") + } + if got := clusterScope.StackitCluster.Status.Bastion.CloudInitHash; got != "" { + t.Fatalf("Status.Bastion.CloudInitHash on adopted server = %q, want empty", got) + } + + spec.SetServer(&server.Server{ID: "srv-created", State: "ACTIVE", PowerStatus: "RUNNING"}, true) + if !clusterScope.BastionServerCreated() { + t.Fatal("BastionServerCreated() = false on created server, want true") + } + if got := clusterScope.StackitCluster.Status.Bastion.CloudInitHash; got != "new-hash" { + t.Fatalf("Status.Bastion.CloudInitHash on created server = %q, want %q", got, "new-hash") + } +} From 26af476e9490a62563c6ee30423000b5caa986b3 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 11:53:45 +0100 Subject: [PATCH 23/60] fix: include AdditionalLabels in nodeSSHSecurityGroupSpec ServerSelector --- scope/cluster_scope.go | 2 +- scope/cluster_scope_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index 3a584b2..c13bf66 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -430,7 +430,7 @@ func nodeSSHSecurityGroupSpec(stackitCluster *infrav1.StackitCluster, remoteSecu Description: "Cluster API Provider STACKIT node SSH access from bastion", Tags: NodeSSHTags(stackitCluster), ServerIDs: serverIDs, - ServerSelector: util.ClusterTags(stackitCluster.Name, stackitCluster.Namespace, nil), + ServerSelector: util.ClusterTags(stackitCluster.Name, stackitCluster.Namespace, stackitCluster.Spec.AdditionalLabels), Rules: []securitygroup.Rule{ ingressRule, { diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index fae1165..a8899f1 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -11,13 +11,16 @@ You may obtain a copy of the License at package scope import ( + "reflect" "testing" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" + "github.com/stackitcloud/cluster-api-provider-stackit/util" ) func TestDefaultAvailabilityZone(t *testing.T) { @@ -110,3 +113,29 @@ func TestServerSpecSetServerPreservesCloudInitHashOnAdoptedServer(t *testing.T) t.Fatalf("Status.Bastion.CloudInitHash on created server = %q, want %q", got, "new-hash") } } + +func TestNodeSSHSecurityGroupSpecIncludesAdditionalLabelsInServerSelector(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-cluster", + Namespace: "my-ns", + }, + Spec: infrav1.StackitClusterSpec{ + AdditionalLabels: map[string]string{ + "env": "staging", + }, + }, + }, + } + + specs := clusterScope.SecurityGroupSpecs() + if len(specs) != 2 { + t.Fatalf("SecurityGroupSpecs() count = %d, want 2", len(specs)) + } + + wantSelector := util.ClusterTags("my-cluster", "my-ns", map[string]string{"env": "staging"}) + if got := specs[1].ServerSelector; !reflect.DeepEqual(got, wantSelector) { + t.Fatalf("nodeSSHSecurityGroupSpec ServerSelector = %#v, want %#v", got, wantSelector) + } +} From 157dff0f39c00f99721ca47c9a7828abd9c6d75d Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 12:16:34 +0100 Subject: [PATCH 24/60] refactor: move bastion enablement guard into MachineScope.SecurityGroupSpecs --- controller/stackitmachine_infrastructure.go | 19 +--------- scope/cluster_scope_test.go | 42 +++++++++++++++++++++ scope/machine_scope.go | 3 ++ 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/controller/stackitmachine_infrastructure.go b/controller/stackitmachine_infrastructure.go index fad081f..9cd5b66 100644 --- a/controller/stackitmachine_infrastructure.go +++ b/controller/stackitmachine_infrastructure.go @@ -30,7 +30,6 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -157,7 +156,7 @@ func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineS return ctrl.Result{RequeueAfter: 15 * time.Second}, nil } - if err := r.reconcileBastionNodeSSHAccess(ctx, machineScope); err != nil { + if err := securitygroup.New(machineScope).Reconcile(ctx); err != nil { log.Error(err, "Failed to configure bastion SSH access for node", "machine", stackitMachine.Name) record.Warnf( stackitMachine, "FailedNodeSSHAccess", @@ -274,22 +273,6 @@ func (r *StackitMachineReconciler) fetchBootstrapData(ctx context.Context, machi return data, metav1.ConditionTrue, "Available", "" } -func (r *StackitMachineReconciler) reconcileBastionNodeSSHAccess( - ctx context.Context, - machineScope *scope.MachineScope, -) error { - if !machineScope.StackitCluster.Spec.Bastion.Enabled { - return nil - } - if machineScope.StackitCluster.Status.Bastion.SecurityGroupID == "" { - return fmt.Errorf("%w: bastion security group ID is empty", shared.ErrTransient) - } - if machineScope.StackitMachine.Status.InstanceID == "" { - return fmt.Errorf("%w: server ID is empty", shared.ErrTransient) - } - return securitygroup.New(machineScope).Reconcile(ctx) -} - func (r *StackitMachineReconciler) getStackitCluster(ctx context.Context, cluster *clusterv1.Cluster) (*infrav1.StackitCluster, error) { if cluster.Spec.InfrastructureRef.Name == "" { return nil, nil diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index a8899f1..c264b07 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -139,3 +139,45 @@ func TestNodeSSHSecurityGroupSpecIncludesAdditionalLabelsInServerSelector(t *tes t.Fatalf("nodeSSHSecurityGroupSpec ServerSelector = %#v, want %#v", got, wantSelector) } } + +func TestMachineScopeSecurityGroupSpecsRespectsBastionEnabled(t *testing.T) { + machineScope := &MachineScope{ + StackitCluster: &infrav1.StackitCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-cluster", + Namespace: "my-ns", + }, + Spec: infrav1.StackitClusterSpec{ + Bastion: infrav1.StackitBastionSpec{ + Enabled: false, + }, + }, + Status: infrav1.StackitClusterStatus{ + Bastion: infrav1.StackitBastionStatus{ + SecurityGroupID: "bastion-sg-id", + }, + }, + }, + StackitMachine: &infrav1.StackitMachine{ + Status: infrav1.StackitMachineStatus{ + InstanceID: "srv-1", + }, + }, + } + + if got := machineScope.SecurityGroupSpecs(); len(got) != 0 { + t.Fatalf("SecurityGroupSpecs() when bastion disabled = %#v, want empty", got) + } + + machineScope.StackitCluster.Spec.Bastion.Enabled = true + got := machineScope.SecurityGroupSpecs() + if len(got) != 1 { + t.Fatalf("SecurityGroupSpecs() when bastion enabled count = %d, want 1", len(got)) + } + if !reflect.DeepEqual(got[0].ServerIDs, []string{"srv-1"}) { + t.Fatalf("SecurityGroupSpecs()[0].ServerIDs = %#v, want [srv-1]", got[0].ServerIDs) + } + if len(got[0].Rules) == 0 || got[0].Rules[0].RemoteSecurityGroupID != "bastion-sg-id" { + t.Fatalf("SecurityGroupSpecs()[0].Rules = %#v, want RemoteSecurityGroupID=bastion-sg-id", got[0].Rules) + } +} diff --git a/scope/machine_scope.go b/scope/machine_scope.go index e7b308b..1adefc2 100644 --- a/scope/machine_scope.go +++ b/scope/machine_scope.go @@ -194,6 +194,9 @@ func (s *MachineScope) ServerSpec() server.ServerSpec { } func (s *MachineScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { + if !s.StackitCluster.Spec.Bastion.Enabled { + return nil + } return []securitygroup.SecurityGroupSpec{ nodeSSHSecurityGroupSpec( s.StackitCluster, From b248891eb046ba734357337c1b06c2d6d1790b79 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 12:31:02 +0100 Subject: [PATCH 25/60] refactor: deduplicate resource lookup and deletion in server and loadbalancer services --- cloud/services/loadbalancer/reconcile.go | 79 +++----- cloud/services/loadbalancer/reconcile_test.go | 12 +- cloud/services/publicip/reconcile.go | 25 +-- cloud/services/securitygroup/reconcile.go | 21 +-- cloud/services/server/reconcile.go | 92 ++-------- cloud/services/shared/errors_test.go | 169 ++++++++++++++++++ cloud/services/shared/utils.go | 105 ++++++++++- 7 files changed, 321 insertions(+), 182 deletions(-) diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index 7edca7e..43b8fa6 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -12,7 +12,6 @@ package loadbalancer import ( "context" - "errors" "fmt" "strings" @@ -62,21 +61,13 @@ func (s *Service) Delete(ctx context.Context) error { return fmt.Errorf("%w: load balancer ID or tags are required", shared.ErrInvalidInput) } - loadBalancerIDs, err := s.client.resolveLoadBalancersForDelete(ctx, spec) + deletedIDs, err := shared.DeleteByIDOrTags(ctx, spec.ID, spec.Tags, s.client.deleteByID, s.client.listIDsByTags) + for _, deletedID := range deletedIDs { + log.V(1).Info("Deleted load balancer", "loadBalancerID", deletedID) + } if err != nil { return err } - var errs []error - for _, loadBalancerID := range loadBalancerIDs { - if err := s.client.deleteByID(ctx, loadBalancerID); err != nil { - errs = append(errs, err) - continue - } - log.V(1).Info("Deleted load balancer", "loadBalancerID", loadBalancerID) - } - if len(errs) > 0 { - return errors.Join(errs...) - } if spec.SetID != nil { spec.SetID("") } @@ -124,35 +115,30 @@ func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*Loa return matched, nil } -// deleteByID deletes a load balancer by ID, ignoring NotFound errors. -func (c *client) deleteByID(ctx context.Context, id string) error { +// deleteByID deletes a load balancer by ID, returning true if the load balancer existed and was deleted, +// or false if it was already absent. +func (c *client) deleteByID(ctx context.Context, id string) (bool, error) { if id == "" { - return fmt.Errorf("%w: load balancer ID is empty", shared.ErrInvalidInput) + return false, fmt.Errorf("%w: load balancer ID is empty", shared.ErrInvalidInput) } if _, err := c.scope.LoadBalancerClient().DefaultAPI.DeleteLoadBalancer(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute(); err != nil { err := shared.ClassifySDKError("delete load balancer", err) - if !shared.IsNotFound(err) { - return err + if shared.IsNotFound(err) { + return false, nil } + return false, err } - return nil + return true, nil } func (c *client) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) (*LoadBalancer, error) { - if spec.ID != "" { - loadBalancer, err := c.get(ctx, spec.ID) - if err == nil { - return loadBalancer, nil - } - if !shared.IsNotFound(err) && !isLoadBalancerServiceNotEnabled(err) { - return nil, err - } - } - if existing, err := c.findByTags(ctx, spec.Tags); err == nil { - return existing, nil - } else if !shared.IsNotFound(err) && !isLoadBalancerServiceNotEnabled(err) { + loadBalancer, err := shared.ResolveExisting(ctx, spec.ID, spec.Tags, c.get, c.findByTags, isIgnorableLookupError) + if err != nil { return nil, err } + if loadBalancer != nil { + return loadBalancer, nil + } payload := spec.toCreatePayload(c.scope.Region()) lbResp, err := c.scope.LoadBalancerClient().DefaultAPI.CreateLoadBalancer(ctx, c.scope.ProjectID(), c.scope.Region()). @@ -166,20 +152,8 @@ func (c *client) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) return out, nil } -func (c *client) resolveLoadBalancersForDelete(ctx context.Context, spec LoadBalancerSpec) ([]string, error) { - if spec.ID != "" { - exists, err := c.loadBalancerExists(ctx, spec.ID) - if err != nil { - return nil, err - } - if exists { - return []string{spec.ID}, nil - } - } - if len(spec.Tags) == 0 { - return nil, nil - } - loadBalancers, err := c.listByTags(ctx, spec.Tags) +func (c *client) listIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { + loadBalancers, err := c.listByTags(ctx, tags) if err != nil { return nil, err } @@ -192,17 +166,6 @@ func (c *client) resolveLoadBalancersForDelete(ctx context.Context, spec LoadBal return ids, nil } -func (c *client) loadBalancerExists(ctx context.Context, id string) (bool, error) { - _, err := c.get(ctx, id) - if err != nil { - if shared.IsNotFound(err) { - return false, nil - } - return false, err - } - return true, nil -} - func (s LoadBalancerSpec) validate() error { if s.Name == "" { return fmt.Errorf("%w: load balancer name is required", shared.ErrInvalidInput) @@ -291,6 +254,10 @@ func listenerPort(listeners []lb.Listener) int32 { return 0 } +func isIgnorableLookupError(err error) bool { + return shared.IsNotFound(err) || isLoadBalancerServiceNotEnabled(err) +} + func isLoadBalancerServiceNotEnabled(err error) bool { return shared.IsUnauthorized(err) && strings.Contains(err.Error(), "Service not enabled") } diff --git a/cloud/services/loadbalancer/reconcile_test.go b/cloud/services/loadbalancer/reconcile_test.go index 04c2ad1..7001b15 100644 --- a/cloud/services/loadbalancer/reconcile_test.go +++ b/cloud/services/loadbalancer/reconcile_test.go @@ -571,16 +571,12 @@ func TestServiceDelete(t *testing.T) { } svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - path := r.URL.Path - switch { - case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers/"+testLBName): - writeJSON(t, w, sdkLoadBalancerJSON(testLBName, nil, nil)) - case r.Method == http.MethodDelete && strings.HasSuffix(path, "/load-balancers/"+testLBName): + if r.Method == http.MethodDelete && strings.HasSuffix(r.URL.Path, "/load-balancers/"+testLBName) { deletedID = testLBName writeJSON(t, w, map[string]any{}) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + return } + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) })) if err := svc.Delete(context.Background()); err != nil { @@ -635,7 +631,7 @@ func TestServiceDelete(t *testing.T) { svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path switch { - case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers/stale-lb"): + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/load-balancers/stale-lb"): w.WriteHeader(http.StatusNotFound) writeJSON(t, w, map[string]any{"message": "not found"}) case r.Method == http.MethodGet && strings.HasSuffix(path, "/load-balancers"): diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index 7e18990..f15befe 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -188,7 +188,7 @@ func (c *client) deleteByID(ctx context.Context, id string) error { } func (c *client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*PublicIP, error) { - publicIP, err := c.resolvePublicIP(ctx, spec) + publicIP, err := shared.ResolveExisting(ctx, spec.ID, spec.Tags, c.get, c.findByTags, shared.IsNotFound) if err != nil { return nil, err } @@ -212,29 +212,6 @@ func (c *client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*Public return out, nil } -func (c *client) resolvePublicIP(ctx context.Context, spec PublicIPSpec) (*PublicIP, error) { - if spec.ID != "" { - publicIP, err := c.get(ctx, spec.ID) - if err == nil { - return publicIP, nil - } - if !shared.IsNotFound(err) { - return nil, err - } - } - if len(spec.Tags) == 0 { - return nil, nil - } - publicIP, err := c.findByTags(ctx, spec.Tags) - if shared.IsNotFound(err) { - return nil, nil - } - if err != nil { - return nil, err - } - return publicIP, nil -} - func (c *client) resolvePublicIPsForDelete(ctx context.Context, spec PublicIPSpec) ([]*PublicIP, error) { if spec.ID != "" { publicIP, err := c.get(ctx, spec.ID) diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index 998f775..f893a24 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -232,27 +232,20 @@ func (c *client) listRulesIfGroupExists(ctx context.Context, id string) ([]iaas. } func (c *client) ensureSecurityGroup(ctx context.Context, spec SecurityGroupSpec) (*SecurityGroup, error) { - if spec.ID != "" { - securityGroup, err := c.get(ctx, spec.ID) - if err == nil { - return securityGroup, nil - } - if !shared.IsNotFound(err) { - return nil, err - } - } - if existing, err := c.findByTags(ctx, spec.Tags); err == nil { - return existing, nil - } else if !shared.IsNotFound(err) { + securityGroup, err := shared.ResolveExisting(ctx, spec.ID, spec.Tags, c.get, c.findByTags, shared.IsNotFound) + if err != nil { return nil, err } - securityGroup, err := c.scope.IaaSClient().DefaultAPI.CreateSecurityGroup(ctx, c.scope.ProjectID(), c.scope.Region()). + if securityGroup != nil { + return securityGroup, nil + } + securityGroupResp, err := c.scope.IaaSClient().DefaultAPI.CreateSecurityGroup(ctx, c.scope.ProjectID(), c.scope.Region()). CreateSecurityGroupPayload(spec.toCreatePayload()). Execute() if err != nil { return nil, shared.ClassifySDKError("create security group", err) } - out := securityGroupFromSDK(securityGroup) + out := securityGroupFromSDK(securityGroupResp) log := logf.FromContext(ctx) log.V(1).Info("Created security group", "name", spec.Name, "securityGroupID", out.ID) return out, nil diff --git a/cloud/services/server/reconcile.go b/cloud/services/server/reconcile.go index 29438a3..91c11cc 100644 --- a/cloud/services/server/reconcile.go +++ b/cloud/services/server/reconcile.go @@ -13,7 +13,6 @@ package server import ( "context" "encoding/base64" - "errors" "fmt" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" @@ -47,7 +46,9 @@ func (s *Service) Delete(ctx context.Context) error { return fmt.Errorf("%w: server ID or tags are required", shared.ErrInvalidInput) } - deletedIDs, err := s.client.deleteBySpec(ctx, spec) + deletedIDs, err := shared.DeleteByIDOrTags(ctx, spec.ID, spec.Tags, s.client.deleteByID, func(ctx context.Context, tags map[string]string) ([]string, error) { + return shared.ListServerIDsByTags(ctx, s.client.scope, tags) + }) for _, deletedID := range deletedIDs { log.V(1).Info("Deleted server", "serverID", deletedID) } @@ -74,47 +75,29 @@ func (c *client) get(ctx context.Context, id string) (*Server, error) { // findByTags finds a single STACKIT server matching the given tag selector. func (c *client) findByTags(ctx context.Context, tags map[string]string) (*Server, error) { - srv, err := shared.FindByTags(ctx, tags, c.listByTags, "server", "servers") + srv, err := shared.FindByTags(ctx, tags, func(ctx context.Context, tags map[string]string) ([]*iaas.Server, error) { + return shared.ListServersByTags(ctx, c.scope, tags) + }, "server", "servers") if err != nil { return nil, err } return c.serverFromSDK(ctx, srv), nil } -// listByTags lists STACKIT servers matching the given tag selector. -func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*iaas.Server, error) { - if len(tags) == 0 { - return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) - } - resp, err := c.scope.IaaSClient().DefaultAPI.ListServers(ctx, c.scope.ProjectID(), c.scope.Region()). - LabelSelector(shared.LabelSelector(tags)). - Details(true). - Execute() - if err != nil { - return nil, shared.ClassifySDKError("list servers", err) - } - matched := []*iaas.Server{} - for _, srv := range resp.GetItems() { - if shared.LabelsContainTags(srv.GetLabels(), tags) { - srv := srv - matched = append(matched, &srv) - } - } - return matched, nil -} - -// deleteByID deletes a server by ID, ignoring NotFound errors. -func (c *client) deleteByID(ctx context.Context, id string) error { +// deleteByID deletes a server by ID, returning true if the server existed and was deleted, +// or false if it was already absent. +func (c *client) deleteByID(ctx context.Context, id string) (bool, error) { if id == "" { - return fmt.Errorf("%w: server ID is empty", shared.ErrInvalidInput) + return false, fmt.Errorf("%w: server ID is empty", shared.ErrInvalidInput) } if err := c.scope.IaaSClient().DefaultAPI.DeleteServer(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute(); err != nil { err := shared.ClassifySDKError("delete server", err) - if !shared.IsNotFound(err) { - return err + if shared.IsNotFound(err) { + return false, nil } + return false, err } - return nil + return true, nil } // create creates a new STACKIT server from the given spec. @@ -132,19 +115,12 @@ func (c *client) create(ctx context.Context, spec ServerSpec) (*Server, error) { } func (c *client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bool, error) { - if spec.ID != "" { - srv, err := c.get(ctx, spec.ID) - if err == nil { - return srv, false, nil - } - if !shared.IsNotFound(err) { - return nil, false, err - } + srv, err := shared.ResolveExisting(ctx, spec.ID, spec.Tags, c.get, c.findByTags, shared.IsNotFound) + if err != nil { + return nil, false, err } - if srv, err := c.findByTags(ctx, spec.Tags); err == nil { + if srv != nil { return srv, false, nil - } else if !shared.IsNotFound(err) { - return nil, false, err } // Recreating an already-provisioned server would replay bootstrap data @@ -164,38 +140,6 @@ func (c *client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bo return out, true, nil } -func (c *client) deleteBySpec(ctx context.Context, spec ServerSpec) ([]string, error) { - if spec.ID != "" { - err := c.scope.IaaSClient().DefaultAPI.DeleteServer(ctx, c.scope.ProjectID(), c.scope.Region(), spec.ID).Execute() - if err == nil { - return []string{spec.ID}, nil - } - err = shared.ClassifySDKError("delete server", err) - if !shared.IsNotFound(err) { - return nil, err - } - } - if len(spec.Tags) == 0 { - return nil, nil - } - ids, err := shared.ListServerIDsByTags(ctx, c.scope, spec.Tags) - if err != nil { - return nil, err - } - var ( - deletedIDs []string - errs []error - ) - for _, id := range ids { - if err := c.deleteByID(ctx, id); err != nil { - errs = append(errs, err) - continue - } - deletedIDs = append(deletedIDs, id) - } - return deletedIDs, errors.Join(errs...) -} - func (s ServerSpec) validate() error { if s.Name == "" { return fmt.Errorf("%w: server name is required", shared.ErrInvalidInput) diff --git a/cloud/services/shared/errors_test.go b/cloud/services/shared/errors_test.go index 30c00dd..a75885a 100644 --- a/cloud/services/shared/errors_test.go +++ b/cloud/services/shared/errors_test.go @@ -125,3 +125,172 @@ func TestFindByTags(t *testing.T) { } }) } + +func TestResolveExisting(t *testing.T) { + const ( + testID1 = "id-1" + testByTags = "by-tags" + ) + ctx := context.Background() + tags := map[string]string{"cluster": "test"} + + t.Run("returns item by ID without calling findByTags", func(t *testing.T) { + findByTagsCalled := false + got, err := ResolveExisting( + ctx, + testID1, + tags, + func(_ context.Context, id string) (string, error) { + return "found-" + id, nil + }, + func(_ context.Context, _ map[string]string) (string, error) { + findByTagsCalled = true + return testByTags, nil + }, + IsNotFound, + ) + if err != nil { + t.Fatalf("ResolveExisting() error = %v", err) + } + if got != "found-id-1" { + t.Fatalf("ResolveExisting() = %q, want %q", got, "found-id-1") + } + if findByTagsCalled { + t.Fatal("findByTags should not be called when getByID succeeds") + } + }) + + t.Run("falls back to findByTags when getByID returns NotFound", func(t *testing.T) { + got, err := ResolveExisting( + ctx, + "stale-id", + tags, + func(_ context.Context, _ string) (string, error) { + return "", ErrNotFound + }, + func(_ context.Context, _ map[string]string) (string, error) { + return testByTags, nil + }, + IsNotFound, + ) + if err != nil { + t.Fatalf("ResolveExisting() error = %v", err) + } + if got != testByTags { + t.Fatalf("ResolveExisting() = %q, want %q", got, testByTags) + } + }) + + t.Run("returns zero value and nil error when both ID and tag lookup return NotFound", func(t *testing.T) { + got, err := ResolveExisting( + ctx, + "stale-id", + tags, + func(_ context.Context, _ string) (string, error) { + return "", ErrNotFound + }, + func(_ context.Context, _ map[string]string) (string, error) { + return "", ErrNotFound + }, + IsNotFound, + ) + if err != nil { + t.Fatalf("ResolveExisting() error = %v, want nil", err) + } + if got != "" { + t.Fatalf("ResolveExisting() = %q, want empty", got) + } + }) + + t.Run("propagates non-ignorable error from getByID", func(t *testing.T) { + _, err := ResolveExisting( + ctx, + testID1, + tags, + func(_ context.Context, _ string) (string, error) { + return "", ErrTransient + }, + func(_ context.Context, _ map[string]string) (string, error) { + return testByTags, nil + }, + IsNotFound, + ) + if !IsTransient(err) { + t.Fatalf("ResolveExisting() error = %v, want ErrTransient", err) + } + }) +} + +func TestDeleteByIDOrTags(t *testing.T) { + const testID1 = "id-1" + ctx := context.Background() + tags := map[string]string{"cluster": "test"} + + t.Run("deletes by ID when found without listing by tags", func(t *testing.T) { + listCalled := false + deletedIDs, err := DeleteByIDOrTags( + ctx, + testID1, + tags, + func(_ context.Context, id string) (bool, error) { + return id == testID1, nil + }, + func(_ context.Context, _ map[string]string) ([]string, error) { + listCalled = true + return []string{"id-2"}, nil + }, + ) + if err != nil { + t.Fatalf("DeleteByIDOrTags() error = %v", err) + } + if len(deletedIDs) != 1 || deletedIDs[0] != testID1 { + t.Fatalf("DeleteByIDOrTags() = %v, want [id-1]", deletedIDs) + } + if listCalled { + t.Fatal("listIDsByTags should not be called when deleteByID deletes spec.ID") + } + }) + + t.Run("falls back to tag lookup when spec.ID is already absent", func(t *testing.T) { + deletedIDs, err := DeleteByIDOrTags( + ctx, + "stale-id", + tags, + func(_ context.Context, id string) (bool, error) { + return id != "stale-id", nil + }, + func(_ context.Context, _ map[string]string) ([]string, error) { + return []string{testID1, "id-2"}, nil + }, + ) + if err != nil { + t.Fatalf("DeleteByIDOrTags() error = %v", err) + } + if len(deletedIDs) != 2 || deletedIDs[0] != testID1 || deletedIDs[1] != "id-2" { + t.Fatalf("DeleteByIDOrTags() = %v, want [id-1 id-2]", deletedIDs) + } + }) + + t.Run("continues deleting remaining tag matches on partial error", func(t *testing.T) { + deletedIDs, err := DeleteByIDOrTags( + ctx, + "", + tags, + func(_ context.Context, id string) (bool, error) { + if id == "bad-id" { + return false, ErrTransient + } + return true, nil + }, + func(_ context.Context, _ map[string]string) ([]string, error) { + return []string{"bad-id", "good-id"}, nil + }, + ) + if !IsTransient(err) { + t.Fatalf("DeleteByIDOrTags() error = %v, want ErrTransient", err) + } + if len(deletedIDs) != 1 || deletedIDs[0] != "good-id" { + t.Fatalf("DeleteByIDOrTags() = %v, want [good-id]", deletedIDs) + } + }) +} diff --git a/cloud/services/shared/utils.go b/cloud/services/shared/utils.go index a496792..44da69e 100644 --- a/cloud/services/shared/utils.go +++ b/cloud/services/shared/utils.go @@ -36,23 +36,38 @@ type LoadBalancerClientScope interface { LoadBalancerClient() *lb.APIClient } -// ListServerIDsByTags lists the IDs of STACKIT servers matching the given tag selector. -func ListServerIDsByTags(ctx context.Context, scope IaaSClientScope, tags map[string]string) ([]string, error) { +// ListServersByTags lists STACKIT servers matching the given tag selector. +func ListServersByTags(ctx context.Context, scope IaaSClientScope, tags map[string]string) ([]*iaas.Server, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", ErrInvalidInput) } resp, err := scope.IaaSClient().DefaultAPI.ListServers(ctx, scope.ProjectID(), scope.Region()). LabelSelector(LabelSelector(tags)). + Details(true). Execute() if err != nil { return nil, ClassifySDKError("list servers", err) } - var ids []string + matched := []*iaas.Server{} for _, srv := range resp.GetItems() { if LabelsContainTags(srv.GetLabels(), tags) { - if id := srv.GetId(); id != "" { - ids = append(ids, id) - } + srv := srv + matched = append(matched, &srv) + } + } + return matched, nil +} + +// ListServerIDsByTags lists the IDs of STACKIT servers matching the given tag selector. +func ListServerIDsByTags(ctx context.Context, scope IaaSClientScope, tags map[string]string) ([]string, error) { + servers, err := ListServersByTags(ctx, scope, tags) + if err != nil { + return nil, err + } + ids := make([]string, 0, len(servers)) + for _, srv := range servers { + if id := srv.GetId(); id != "" { + ids = append(ids, id) } } return ids, nil @@ -73,6 +88,84 @@ func FindByTags[T any]( return findSingle(matched, singular, plural) } +// ResolveExisting looks up an existing resource by ID first (ignoring errors matched by ignoreLookupErr), +// falling back to tag lookup when the ID is empty or absent. It returns the zero value and nil error when not found. +func ResolveExisting[T any]( + ctx context.Context, + id string, + tags map[string]string, + getByID func(context.Context, string) (T, error), + findByTags func(context.Context, map[string]string) (T, error), + ignoreLookupErr func(error) bool, +) (T, error) { + var zero T + if id != "" { + item, err := getByID(ctx, id) + if err == nil { + return item, nil + } + if !ignoreLookupErr(err) { + return zero, err + } + } + if len(tags) == 0 { + return zero, nil + } + item, err := findByTags(ctx, tags) + if err == nil { + return item, nil + } + if ignoreLookupErr(err) { + return zero, nil + } + return zero, err +} + +// DeleteByIDOrTags deletes a resource by ID when provided, falling back to deleting all resources matching tags +// if the ID is empty or already absent. +func DeleteByIDOrTags( + ctx context.Context, + id string, + tags map[string]string, + deleteByID func(context.Context, string) (bool, error), + listIDsByTags func(context.Context, map[string]string) ([]string, error), +) ([]string, error) { + if id != "" { + deleted, err := deleteByID(ctx, id) + if err != nil { + return nil, err + } + if deleted { + return []string{id}, nil + } + } + if len(tags) == 0 { + return nil, nil + } + ids, err := listIDsByTags(ctx, tags) + if err != nil { + return nil, err + } + var ( + deletedIDs []string + errs []error + ) + for _, targetID := range ids { + if targetID == "" { + continue + } + deleted, err := deleteByID(ctx, targetID) + if err != nil { + errs = append(errs, err) + continue + } + if deleted { + deletedIDs = append(deletedIDs, targetID) + } + } + return deletedIDs, errors.Join(errs...) +} + // findSingle returns the single item in matched, or ErrNotFound if empty, or ErrConflict if more than one. func findSingle[T any](matched []T, singular, plural string) (T, error) { var zero T From cfd074f56ad2861bad7d1381cd75096e27dfdf2f Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 13:05:24 +0100 Subject: [PATCH 26/60] refactor: replace hardcoded bootstrap target IP and protocol literals with constants --- cloud/services/loadbalancer/reconcile.go | 2 +- cloud/services/loadbalancer/service.go | 12 +++++++---- cloud/services/loadbalancer/targets.go | 2 +- scope/cluster_scope.go | 4 ++-- scope/cluster_scope_test.go | 27 ++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index 43b8fa6..32f169f 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -226,7 +226,7 @@ func (s LoadBalancerSpec) toCreatePayload(region string) lb.CreateLoadBalancerPa listener := lb.NewListener() listener.SetDisplayName(apiserverListenerName) listener.SetPort(s.Port) - listener.SetProtocol("PROTOCOL_TCP") + listener.SetProtocol(listenerProtocolTCP) listener.SetTargetPool(apiserverTargetPoolName) payload.SetListeners([]lb.Listener{*listener}) diff --git a/cloud/services/loadbalancer/service.go b/cloud/services/loadbalancer/service.go index e402fac..c0bcf08 100644 --- a/cloud/services/loadbalancer/service.go +++ b/cloud/services/loadbalancer/service.go @@ -20,10 +20,14 @@ const ( // DefaultAPIServerPort is the default TCP port for the Kubernetes API server. DefaultAPIServerPort int32 = 6443 - defaultBootstrapTargetIP = "10.0.0.1" - apiserverTargetPoolName = "apiserver" - apiserverListenerName = "apiserver" - bootstrapTargetName = "capi-bootstrap-placeholder" + // DefaultBootstrapTargetIP is the fallback IPv4 address used for the + // bootstrap placeholder target when no network prefix can be derived. + DefaultBootstrapTargetIP = "10.0.0.1" + + apiserverTargetPoolName = "apiserver" + apiserverListenerName = "apiserver" + bootstrapTargetName = "capi-bootstrap-placeholder" + listenerProtocolTCP = "PROTOCOL_TCP" ) // errTargetPoolUpdate indicates that the load balancer was created/resolved diff --git a/cloud/services/loadbalancer/targets.go b/cloud/services/loadbalancer/targets.go index 28567aa..4c71c6a 100644 --- a/cloud/services/loadbalancer/targets.go +++ b/cloud/services/loadbalancer/targets.go @@ -36,7 +36,7 @@ const ( // BootstrapTarget seeds a new load balancer; STACKIT rejects an empty pool. func BootstrapTarget(ip string) Target { if ip == "" { - ip = defaultBootstrapTargetIP + ip = DefaultBootstrapTargetIP } return Target{ Name: bootstrapTargetName, diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index c13bf66..165277e 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -281,7 +281,7 @@ func (s *ClusterScope) NetworkSpec() network.NetworkSpec { func (s *ClusterScope) BootstrapTargetIP() string { if s.network == nil { - return "10.0.0.1" + return loadbalancer.DefaultBootstrapTargetIP } for _, prefixValue := range s.network.IPv4Prefixes { prefix, err := netip.ParsePrefix(prefixValue) @@ -296,7 +296,7 @@ func (s *ClusterScope) BootstrapTargetIP() string { return address.String() } } - return "10.0.0.1" + return loadbalancer.DefaultBootstrapTargetIP } func (s *ClusterScope) PublicIPSpec() publicip.PublicIPSpec { diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index c264b07..442c245 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -19,6 +19,8 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -181,3 +183,28 @@ func TestMachineScopeSecurityGroupSpecsRespectsBastionEnabled(t *testing.T) { t.Fatalf("SecurityGroupSpecs()[0].Rules = %#v, want RemoteSecurityGroupID=bastion-sg-id", got[0].Rules) } } + +func TestBootstrapTargetIP(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{}, + } + if got := clusterScope.BootstrapTargetIP(); got != loadbalancer.DefaultBootstrapTargetIP { + t.Fatalf("BootstrapTargetIP() with nil network = %q, want %q", got, loadbalancer.DefaultBootstrapTargetIP) + } + + clusterScope.NetworkSpec().SetNetwork(&network.Network{ + ID: "net-1", + IPv4Prefixes: []string{"192.168.10.0/24"}, + }) + if got := clusterScope.BootstrapTargetIP(); got != "192.168.10.10" { + t.Fatalf("BootstrapTargetIP() with /24 network = %q, want %q", got, "192.168.10.10") + } + + clusterScope.NetworkSpec().SetNetwork(&network.Network{ + ID: "net-2", + IPv4Prefixes: []string{"invalid"}, + }) + if got := clusterScope.BootstrapTargetIP(); got != loadbalancer.DefaultBootstrapTargetIP { + t.Fatalf("BootstrapTargetIP() with invalid prefix = %q, want %q", got, loadbalancer.DefaultBootstrapTargetIP) + } +} From b14ce802f3671035a0c97d2225104b21d5a92a84 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 13:40:55 +0100 Subject: [PATCH 27/60] refactor: remove duplicated STACKIT SDK helpers from e2e tests --- cloud/services/loadbalancer/reconcile_test.go | 19 +- cloud/services/publicip/reconcile_test.go | 123 ++--- .../services/securitygroup/reconcile_test.go | 17 +- cloud/services/server/reconcile_test.go | 15 +- test/e2e/e2e_test.go | 429 +++--------------- 5 files changed, 157 insertions(+), 446 deletions(-) diff --git a/cloud/services/loadbalancer/reconcile_test.go b/cloud/services/loadbalancer/reconcile_test.go index 7001b15..c1f2e90 100644 --- a/cloud/services/loadbalancer/reconcile_test.go +++ b/cloud/services/loadbalancer/reconcile_test.go @@ -590,8 +590,8 @@ func TestServiceDelete(t *testing.T) { } }) - t.Run("falls back to tag lookup when ID is empty", func(t *testing.T) { - var deletedID string + t.Run("falls back to tag lookup when ID is empty and deletes all matching load balancers", func(t *testing.T) { + var deletedIDs []string spec := loadbalancer.LoadBalancerSpec{ Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, } @@ -604,9 +604,18 @@ func TestServiceDelete(t *testing.T) { sdkLoadBalancerJSON(testLBName, map[string]any{ "cluster.x-k8s.io.cluster-name": "test", }, nil), + sdkLoadBalancerJSON("second-lb", map[string]any{ + "cluster.x-k8s.io.cluster-name": "test", + }, nil), + sdkLoadBalancerJSON("other-lb", map[string]any{ + "cluster.x-k8s.io.cluster-name": "other", + }, nil), }}) case r.Method == http.MethodDelete && strings.HasSuffix(path, "/load-balancers/"+testLBName): - deletedID = testLBName + deletedIDs = append(deletedIDs, testLBName) + writeJSON(t, w, map[string]any{}) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/load-balancers/second-lb"): + deletedIDs = append(deletedIDs, "second-lb") writeJSON(t, w, map[string]any{}) default: t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) @@ -616,8 +625,8 @@ func TestServiceDelete(t *testing.T) { if err := svc.Delete(context.Background()); err != nil { t.Fatalf("Delete() error = %v", err) } - if deletedID != testLBName { - t.Fatalf("deletedID = %q, want %q", deletedID, testLBName) + if len(deletedIDs) != 2 || deletedIDs[0] != testLBName || deletedIDs[1] != "second-lb" { + t.Fatalf("deletedIDs = %v, want [%s second-lb]", deletedIDs, testLBName) } }) diff --git a/cloud/services/publicip/reconcile_test.go b/cloud/services/publicip/reconcile_test.go index 274c990..967656c 100644 --- a/cloud/services/publicip/reconcile_test.go +++ b/cloud/services/publicip/reconcile_test.go @@ -340,59 +340,6 @@ func TestServiceDelete(t *testing.T) { } }) - t.Run("falls back to tag lookup for public IP and server when IDs are empty", func(t *testing.T) { - var ( - detachedPair string - deletedID string - ) - spec := publicip.PublicIPSpec{ - Tags: map[string]string{"cluster": "test"}, - ServerSelector: map[string]string{"cluster": "test"}, - } - - svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - path := r.URL.Path - switch { - case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips"): - writeJSON(t, w, map[string]any{"items": []any{ - map[string]any{ - "id": testPublicIPID, - "ip": testPublicIPAddress, - "networkInterface": testNetworkInterface, - "labels": map[string]any{"cluster": "test"}, - }, - }}) - case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers"): - writeJSON(t, w, map[string]any{"items": []any{ - map[string]any{ - "id": testServerID, - "name": "bastion", - "machineType": "c2i.1", - "labels": map[string]any{"cluster": "test"}, - }, - }}) - case r.Method == http.MethodDelete && strings.HasSuffix(path, "/servers/"+testServerID+"/public-ips/"+testPublicIPID): - detachedPair = testServerID + ":" + testPublicIPID - w.WriteHeader(http.StatusNoContent) - case r.Method == http.MethodDelete && strings.HasSuffix(path, "/public-ips/"+testPublicIPID): - deletedID = testPublicIPID - w.WriteHeader(http.StatusNoContent) - default: - t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) - } - })) - - if err := svc.Delete(context.Background()); err != nil { - t.Fatalf("Delete() error = %v", err) - } - if detachedPair != testServerID+":"+testPublicIPID { - t.Fatalf("detachedPair = %q, want %s:%s", detachedPair, testServerID, testPublicIPID) - } - if deletedID != testPublicIPID { - t.Fatalf("deletedID = %q, want %q", deletedID, testPublicIPID) - } - }) - t.Run("succeeds when no public IP exists", func(t *testing.T) { spec := publicip.PublicIPSpec{ Tags: map[string]string{"cluster": "test"}, @@ -417,3 +364,73 @@ func TestServiceDelete(t *testing.T) { } }) } + +func TestServiceDeleteTagFallback(t *testing.T) { + var ( + detachedPairs []string + deletedIDs []string + ) + spec := publicip.PublicIPSpec{ + Tags: map[string]string{"cluster": "test"}, + ServerSelector: map[string]string{"cluster": "test"}, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{ + "id": testPublicIPID, + "ip": testPublicIPAddress, + "networkInterface": testNetworkInterface, + "labels": map[string]any{"cluster": "test"}, + }, + map[string]any{ + "id": testOtherPublicIPID, + "ip": "203.0.113.23", + "networkInterface": testNetworkInterface, + "labels": map[string]any{"cluster": "test"}, + }, + map[string]any{ + "id": "ignored-pip", + "ip": "203.0.113.24", + "labels": map[string]any{"cluster": "other"}, + }, + }}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/servers"): + writeJSON(t, w, map[string]any{"items": []any{ + map[string]any{ + "id": testServerID, + "name": "bastion", + "machineType": "c2i.1", + "labels": map[string]any{"cluster": "test"}, + }, + }}) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/servers/"+testServerID+"/public-ips/"+testPublicIPID): + detachedPairs = append(detachedPairs, testServerID+":"+testPublicIPID) + w.WriteHeader(http.StatusNoContent) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/servers/"+testServerID+"/public-ips/"+testOtherPublicIPID): + detachedPairs = append(detachedPairs, testServerID+":"+testOtherPublicIPID) + w.WriteHeader(http.StatusNoContent) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/public-ips/"+testPublicIPID): + deletedIDs = append(deletedIDs, testPublicIPID) + w.WriteHeader(http.StatusNoContent) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/public-ips/"+testOtherPublicIPID): + deletedIDs = append(deletedIDs, testOtherPublicIPID) + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Delete(context.Background()); err != nil { + t.Fatalf("Delete() error = %v", err) + } + if len(detachedPairs) != 2 || detachedPairs[0] != testServerID+":"+testPublicIPID || detachedPairs[1] != testServerID+":"+testOtherPublicIPID { + t.Fatalf("detachedPairs = %v, want [%s:%s %s:%s]", detachedPairs, testServerID, testPublicIPID, testServerID, testOtherPublicIPID) + } + if len(deletedIDs) != 2 || deletedIDs[0] != testPublicIPID || deletedIDs[1] != testOtherPublicIPID { + t.Fatalf("deletedIDs = %v, want [%s %s]", deletedIDs, testPublicIPID, testOtherPublicIPID) + } +} diff --git a/cloud/services/securitygroup/reconcile_test.go b/cloud/services/securitygroup/reconcile_test.go index 3d1d53e..30df352 100644 --- a/cloud/services/securitygroup/reconcile_test.go +++ b/cloud/services/securitygroup/reconcile_test.go @@ -634,10 +634,10 @@ func TestServiceDelete(t *testing.T) { } }) - t.Run("deletes rules and security group found by tags", func(t *testing.T) { + t.Run("deletes rules and all security groups found by tags", func(t *testing.T) { var ( deletedRules []string - deletedSGID string + deletedSGIDs []string ) specs := []securitygroup.SecurityGroupSpec{{ Tags: map[string]string{"cluster": "test"}, @@ -649,18 +649,25 @@ func TestServiceDelete(t *testing.T) { case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): writeJSON(t, w, map[string]any{"items": []any{ map[string]any{"id": testSecurityGroupID, "name": "cluster-sg", "labels": map[string]any{"cluster": "test"}}, + map[string]any{"id": testRemoteSGID, "name": "bastion-sg", "labels": map[string]any{"cluster": "test"}}, + map[string]any{"id": "ignored-sg", "name": "other-sg", "labels": map[string]any{"cluster": "other"}}, }}) case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): writeJSON(t, w, map[string]any{"items": []any{ map[string]any{"id": testRuleID1, "direction": "ingress"}, map[string]any{"id": testRuleID2, "direction": "egress"}, }}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testRemoteSGID+"/rules"): + writeJSON(t, w, map[string]any{"items": []any{}}) case r.Method == http.MethodDelete && strings.Contains(path, "/rules/"): parts := strings.Split(strings.TrimSuffix(path, "/"), "/") deletedRules = append(deletedRules, parts[len(parts)-1]) w.WriteHeader(http.StatusNoContent) case r.Method == http.MethodDelete && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID): - deletedSGID = testSecurityGroupID + deletedSGIDs = append(deletedSGIDs, testSecurityGroupID) + w.WriteHeader(http.StatusNoContent) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/security-groups/"+testRemoteSGID): + deletedSGIDs = append(deletedSGIDs, testRemoteSGID) w.WriteHeader(http.StatusNoContent) default: t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) @@ -673,8 +680,8 @@ func TestServiceDelete(t *testing.T) { if !slices.Equal(deletedRules, []string{testRuleID1, testRuleID2}) { t.Fatalf("deletedRules = %v, want [%s %s]", deletedRules, testRuleID1, testRuleID2) } - if deletedSGID != testSecurityGroupID { - t.Fatalf("deletedSGID = %q, want %q", deletedSGID, testSecurityGroupID) + if !slices.Equal(deletedSGIDs, []string{testSecurityGroupID, testRemoteSGID}) { + t.Fatalf("deletedSGIDs = %v, want [%s %s]", deletedSGIDs, testSecurityGroupID, testRemoteSGID) } }) diff --git a/cloud/services/server/reconcile_test.go b/cloud/services/server/reconcile_test.go index 9d86347..4ee8bb4 100644 --- a/cloud/services/server/reconcile_test.go +++ b/cloud/services/server/reconcile_test.go @@ -537,8 +537,8 @@ func TestServiceDelete(t *testing.T) { } }) - t.Run("falls back to tag lookup when ID is empty", func(t *testing.T) { - var deletedID string + t.Run("falls back to tag lookup when ID is empty and deletes all matching servers", func(t *testing.T) { + var deletedIDs []string spec := server.ServerSpec{ Tags: map[string]string{"cluster": "test"}, } @@ -548,9 +548,14 @@ func TestServiceDelete(t *testing.T) { case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers"): writeJSON(t, w, map[string]any{"items": []any{ map[string]any{"id": testServerID, "name": "node-0", "machineType": "c2i.2", "labels": map[string]any{"cluster": "test"}}, + map[string]any{"id": testOtherServerID, "name": "node-1", "machineType": "c2i.2", "labels": map[string]any{"cluster": "test"}}, + map[string]any{"id": "ignored-id", "name": "other", "machineType": "c2i.2", "labels": map[string]any{"cluster": "other"}}, }}) case r.Method == http.MethodDelete && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID): - deletedID = testServerID + deletedIDs = append(deletedIDs, testServerID) + w.WriteHeader(http.StatusNoContent) + case r.Method == http.MethodDelete && strings.HasSuffix(r.URL.Path, "/servers/"+testOtherServerID): + deletedIDs = append(deletedIDs, testOtherServerID) w.WriteHeader(http.StatusNoContent) default: t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) @@ -560,8 +565,8 @@ func TestServiceDelete(t *testing.T) { if err := svc.Delete(context.Background()); err != nil { t.Fatalf("Delete() error = %v", err) } - if deletedID != testServerID { - t.Fatalf("deletedID = %q, want %q", deletedID, testServerID) + if len(deletedIDs) != 2 || deletedIDs[0] != testServerID || deletedIDs[1] != testOtherServerID { + t.Fatalf("deletedIDs = %v, want [%s %s]", deletedIDs, testServerID, testOtherServerID) } }) diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index e8e030b..9c39078 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -34,8 +34,6 @@ import ( . "github.com/onsi/gomega" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/test/utils" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -414,33 +412,17 @@ var _ = Describe("Manager", Ordered, func() { }, 10*time.Minute, 10*time.Second).Should(Succeed()) By("waiting for the StackitMachine to provision a VM") - var instanceID string Eventually(func(g Gomega) { ready := kubectlOutput(g, "get", "stackitmachine", machineName, "-n", cfg.Namespace, "-o", "jsonpath={.status.ready}") g.Expect(ready).To(Equal("true")) - instanceID = kubectlOutput(g, "get", "stackitmachine", machineName, "-n", cfg.Namespace, "-o", "jsonpath={.status.instanceID}") + instanceID := kubectlOutput(g, "get", "stackitmachine", machineName, "-n", cfg.Namespace, "-o", "jsonpath={.status.instanceID}") g.Expect(instanceID).NotTo(BeEmpty()) }, 25*time.Minute, 15*time.Second).Should(Succeed()) - By("verifying the VM exists in STACKIT") - Eventually(func(g Gomega) { - expectServerExistsInCloud(ctx, g, cloudClient, instanceID) - }, 5*time.Minute, 15*time.Second).Should(Succeed()) - By("deleting the StackitMachine to trigger VM cleanup") cmd = exec.Command("kubectl", "delete", "stackitmachine", machineName, "-n", cfg.Namespace, "--wait=true", "--timeout=20m") _, err = utils.Run(cmd) Expect(err).NotTo(HaveOccurred(), "Failed to delete StackitMachine") - - By("verifying the VM was deleted from STACKIT") - Eventually(func(g Gomega) { - expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) - - By("verifying no tagged STACKIT resources remain for the e2e test ID") - Eventually(func(g Gomega) { - expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) - }, 5*time.Minute, 15*time.Second).Should(Succeed()) }) It("should create and delete a 1 control-plane / 1 worker workload Cluster without STACKIT leaks", func() { @@ -480,49 +462,23 @@ var _ = Describe("Manager", Ordered, func() { }, 15*time.Minute, 10*time.Second).Should(Succeed()) if cfg.BastionEnabled { - By("verifying the bastion public IP and security group exist") + By("verifying the bastion status is populated") Eventually(func(g Gomega) { output := kubectlOutput(g, "get", "stackitcluster", clusterName, "-n", cfg.Namespace, "-o", "jsonpath={.status.bastion.publicIP}") g.Expect(output).NotTo(BeEmpty()) - publicIPCount, err := countPublicIPsByTags(ctx, cloudClient, stackitE2EBastionTags(testID)) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(publicIPCount).To(Equal(1)) - sgCount, err := countSecurityGroupsByTags(ctx, cloudClient, stackitE2EBastionTags(testID)) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(sgCount).To(Equal(1)) }, 10*time.Minute, 15*time.Second).Should(Succeed()) } By("waiting for one control-plane and one worker StackitMachine to provision") - var instanceIDs []string Eventually(func(g Gomega) { machines := stackitMachinesForTestID(g, cfg.Namespace, testID) g.Expect(machines).To(HaveLen(2)) - instanceIDs = instanceIDs[:0] for _, machine := range machines { g.Expect(machine.Status.Ready).To(BeTrue(), "StackitMachine %s is not ready", machine.Metadata.Name) g.Expect(machine.Status.InstanceID).NotTo(BeEmpty(), "StackitMachine %s has no instanceID", machine.Metadata.Name) - instanceIDs = append(instanceIDs, machine.Status.InstanceID) } }, 45*time.Minute, 15*time.Second).Should(Succeed()) - if cfg.BastionEnabled { - By("verifying the node SSH security group exists") - Eventually(func(g Gomega) { - sgCount, err := countSecurityGroupsByTags(ctx, cloudClient, stackitE2ENodeSSHTags(testID)) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(sgCount).To(Equal(1)) - }, 10*time.Minute, 15*time.Second).Should(Succeed()) - } - - By("verifying both VMs exist in STACKIT") - for _, instanceID := range instanceIDs { - instanceID := instanceID - Eventually(func(g Gomega) { - expectServerExistsInCloud(ctx, g, cloudClient, instanceID) - }, 5*time.Minute, 15*time.Second).Should(Succeed()) - } - By("deleting the workload Cluster") cmd = exec.Command("kubectl", "delete", "cluster", clusterName, "-n", cfg.Namespace, "--wait=true", "--timeout=45m") _, err = utils.Run(cmd) @@ -539,20 +495,6 @@ var _ = Describe("Manager", Ordered, func() { g.Expect(output).To(BeEmpty()) g.Expect(stackitMachinesForTestID(g, cfg.Namespace, testID)).To(BeEmpty()) }, 5*time.Minute, 10*time.Second).Should(Succeed()) - - By("verifying no tagged STACKIT resources remain for the e2e test ID") - Eventually(func(g Gomega) { - expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) - publicIPCount, err := countPublicIPsByTags(ctx, cloudClient, stackitE2EBastionTags(testID)) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(publicIPCount).To(Equal(0)) - sgCount, err := countSecurityGroupsByTags(ctx, cloudClient, stackitE2EBastionTags(testID)) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(sgCount).To(Equal(0)) - sgCount, err = countSecurityGroupsByTags(ctx, cloudClient, stackitE2ENodeSSHTags(testID)) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(sgCount).To(Equal(0)) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) It("should align StackitMachine, Machine, and Node providerIDs in a real workload Cluster", func() { @@ -571,7 +513,6 @@ var _ = Describe("Manager", Ordered, func() { leakTags := stackitE2ETags(testID) kubernetesVersion := envDefault("KUBERNETES_VERSION", defaultKubernetesVersion) validateSupportedKubernetesVersion(kubernetesVersion) - observedInstanceIDs := []string{} By("cleaning up stale STACKIT resources for the e2e test ID") Expect(cloud.CleanupByTags(ctx, cloudClient, leakTags)).To(Succeed()) @@ -582,7 +523,6 @@ var _ = Describe("Manager", Ordered, func() { var workloadKubeconfig string defer func() { cleanupStackitKubeadmClusterFixture(clusterName, cfg.Namespace) - cleanupCloudServersByID(ctx, cloudClient, observedInstanceIDs) if err := cloud.CleanupByTags(ctx, cloudClient, leakTags); err != nil { _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API cleanup warning: %v\n", err) } @@ -597,25 +537,19 @@ var _ = Describe("Manager", Ordered, func() { Expect(err).NotTo(HaveOccurred(), "Failed to apply STACKIT NodeRef fixture") waitForKubeadmWorkloadClusterReady(workloadReadinessOptions{ - Namespace: cfg.Namespace, - ClusterName: clusterName, - TestID: testID, - WantNodes: 2, - WantMachines: 2, - InstallCNI: true, - WaitForCCM: true, - ObservedInstanceIDs: &observedInstanceIDs, + Namespace: cfg.Namespace, + ClusterName: clusterName, + TestID: testID, + WantNodes: 2, + WantMachines: 2, + InstallCNI: true, + WaitForCCM: true, }, &workloadKubeconfig) By("deleting the workload Cluster") cmd = exec.Command("kubectl", "delete", "cluster", clusterName, "-n", cfg.Namespace, "--wait=true", "--timeout=45m") _, err = utils.Run(cmd) Expect(err).NotTo(HaveOccurred(), "Failed to delete NodeRef workload Cluster") - - By("verifying no tagged STACKIT resources remain for the e2e test ID") - Eventually(func(g Gomega) { - expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) It("should create a topology workload Cluster with Ready Nodes", func() { @@ -634,7 +568,6 @@ var _ = Describe("Manager", Ordered, func() { leakTags := stackitE2ETags(testID) kubernetesVersion := envDefault("KUBERNETES_VERSION", defaultKubernetesVersion) validateSupportedKubernetesVersion(kubernetesVersion) - observedInstanceIDs := []string{} By("cleaning up stale STACKIT resources for the e2e test ID") Expect(cloud.CleanupByTags(ctx, cloudClient, leakTags)).To(Succeed()) @@ -658,7 +591,6 @@ var _ = Describe("Manager", Ordered, func() { var workloadKubeconfig string defer func() { cleanupStackitTopologyClusterFixture(clusterName, cfg.Namespace) - cleanupCloudServersByID(ctx, cloudClient, observedInstanceIDs) if err := cloud.CleanupByTags(ctx, cloudClient, leakTags); err != nil { _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API cleanup warning: %v\n", err) } @@ -674,15 +606,14 @@ var _ = Describe("Manager", Ordered, func() { Expect(err).NotTo(HaveOccurred(), "Failed to apply STACKIT topology workload fixture") waitForKubeadmWorkloadClusterReady(workloadReadinessOptions{ - Namespace: cfg.Namespace, - ClusterName: clusterName, - TestID: testID, - WantNodes: 2, - WantMachines: 2, - InstallCNI: true, - WaitForCCM: true, - Topology: true, - ObservedInstanceIDs: &observedInstanceIDs, + Namespace: cfg.Namespace, + ClusterName: clusterName, + TestID: testID, + WantNodes: 2, + WantMachines: 2, + InstallCNI: true, + WaitForCCM: true, + Topology: true, }, &workloadKubeconfig) By("verifying topology template metadata is propagated to generated infrastructure resources") @@ -701,11 +632,6 @@ var _ = Describe("Manager", Ordered, func() { g.Expect(kubectlOutput(g, "get", "stackitmachine", "-n", cfg.Namespace, "-l", "cluster.x-k8s.io/cluster-name="+clusterName, "-o", "name")).To(BeEmpty()) g.Expect(kubectlOutput(g, "get", "stackitcluster", "-n", cfg.Namespace, "-l", "cluster.x-k8s.io/cluster-name="+clusterName, "-o", "name")).To(BeEmpty()) }, 10*time.Minute, 15*time.Second).Should(Succeed()) - - By("verifying no tagged STACKIT resources remain for the e2e test ID") - Eventually(func(g Gomega) { - expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) It("should scale a worker MachineDeployment up and down without STACKIT leaks", func() { @@ -720,7 +646,6 @@ var _ = Describe("Manager", Ordered, func() { testID := envDefault("STACKIT_E2E_TEST_ID", clusterName) leakTags := stackitE2ETags(testID) machineDeploymentName := clusterName + "-md-0" - observedInstanceIDs := []string{} By("cleaning up stale STACKIT resources for the e2e test ID") Expect(cloud.CleanupByTags(ctx, cloudClient, leakTags)).To(Succeed()) @@ -732,7 +657,6 @@ var _ = Describe("Manager", Ordered, func() { fixturePath := writeTempManifest("stackit-md-scale-e2e-*.yaml", fixture) defer func() { cleanupStackitMachineDeploymentScaleFixture(clusterName, cfg.Namespace) - cleanupCloudServersByID(ctx, cloudClient, observedInstanceIDs) if err := cloud.CleanupByTags(ctx, cloudClient, leakTags); err != nil { _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API cleanup warning: %v\n", err) } @@ -754,7 +678,6 @@ var _ = Describe("Manager", Ordered, func() { Eventually(func(g Gomega) { initialInstanceIDs = readyStackitMachineInstanceIDs(g, cfg.Namespace, testID, 1) }, 45*time.Minute, 15*time.Second).Should(Succeed()) - observedInstanceIDs = appendUnique(observedInstanceIDs, initialInstanceIDs...) Eventually(func(g Gomega) { expectCAPIMachinesWithProviderIDs(g, cfg.Namespace, clusterName, 1) }, 5*time.Minute, 10*time.Second).Should(Succeed()) @@ -768,7 +691,6 @@ var _ = Describe("Manager", Ordered, func() { Eventually(func(g Gomega) { scaledUpInstanceIDs = readyStackitMachineInstanceIDs(g, cfg.Namespace, testID, 3) }, 45*time.Minute, 15*time.Second).Should(Succeed()) - observedInstanceIDs = appendUnique(observedInstanceIDs, scaledUpInstanceIDs...) Eventually(func(g Gomega) { expectCAPIMachinesWithProviderIDs(g, cfg.Namespace, clusterName, 3) }, 5*time.Minute, 10*time.Second).Should(Succeed()) @@ -776,14 +698,6 @@ var _ = Describe("Manager", Ordered, func() { Expect(scaledUpInstanceIDs).To(ContainElement(instanceID)) } - By("verifying all scaled-up worker VMs exist in STACKIT") - for _, instanceID := range scaledUpInstanceIDs { - instanceID := instanceID - Eventually(func(g Gomega) { - expectServerExistsInCloud(ctx, g, cloudClient, instanceID) - }, 5*time.Minute, 15*time.Second).Should(Succeed()) - } - By("scaling workers back down to one replica") cmd = exec.Command("kubectl", "scale", "machinedeployment", machineDeploymentName, "-n", cfg.Namespace, "--replicas=1") _, err = utils.Run(cmd) @@ -793,7 +707,6 @@ var _ = Describe("Manager", Ordered, func() { Eventually(func(g Gomega) { remainingInstanceIDs = readyStackitMachineInstanceIDs(g, cfg.Namespace, testID, 1) }, 30*time.Minute, 15*time.Second).Should(Succeed()) - observedInstanceIDs = appendUnique(observedInstanceIDs, remainingInstanceIDs...) Eventually(func(g Gomega) { expectCAPIMachinesWithProviderIDs(g, cfg.Namespace, clusterName, 1) }, 5*time.Minute, 10*time.Second).Should(Succeed()) @@ -801,31 +714,10 @@ var _ = Describe("Manager", Ordered, func() { deletedInstanceIDs := difference(scaledUpInstanceIDs, remainingInstanceIDs) Expect(deletedInstanceIDs).To(HaveLen(2)) - By("verifying scaled-down worker VMs were deleted from STACKIT") - for _, instanceID := range deletedInstanceIDs { - instanceID := instanceID - Eventually(func(g Gomega) { - expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) - } - By("deleting the scale test workload Cluster") cmd = exec.Command("kubectl", "delete", "cluster", clusterName, "-n", cfg.Namespace, "--wait=true", "--timeout=45m") _, err = utils.Run(cmd) Expect(err).NotTo(HaveOccurred(), "Failed to delete scale test workload Cluster") - - By("verifying all observed worker VMs were deleted from STACKIT") - for _, instanceID := range observedInstanceIDs { - instanceID := instanceID - Eventually(func(g Gomega) { - expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) - } - - By("verifying no tagged STACKIT resources remain for the e2e test ID") - Eventually(func(g Gomega) { - expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) It("should scale workload worker Nodes up and down without STACKIT leaks", func() { @@ -845,7 +737,6 @@ var _ = Describe("Manager", Ordered, func() { machineDeploymentName := clusterName + "-md-0" kubernetesVersion := envDefault("KUBERNETES_VERSION", defaultKubernetesVersion) validateSupportedKubernetesVersion(kubernetesVersion) - observedInstanceIDs := []string{} By("cleaning up stale STACKIT resources for the e2e test ID") Expect(cloud.CleanupByTags(ctx, cloudClient, leakTags)).To(Succeed()) @@ -866,7 +757,6 @@ var _ = Describe("Manager", Ordered, func() { var workloadKubeconfig string defer func() { cleanupStackitKubeadmClusterFixture(clusterName, cfg.Namespace) - cleanupCloudServersByID(ctx, cloudClient, observedInstanceIDs) if err := cloud.CleanupByTags(ctx, cloudClient, leakTags); err != nil { _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API cleanup warning: %v\n", err) } @@ -881,14 +771,13 @@ var _ = Describe("Manager", Ordered, func() { Expect(err).NotTo(HaveOccurred(), "Failed to apply STACKIT workload scale fixture") waitForKubeadmWorkloadClusterReady(workloadReadinessOptions{ - Namespace: cfg.Namespace, - ClusterName: clusterName, - TestID: testID, - WantNodes: 2, - WantMachines: 2, - InstallCNI: true, - WaitForCCM: true, - ObservedInstanceIDs: &observedInstanceIDs, + Namespace: cfg.Namespace, + ClusterName: clusterName, + TestID: testID, + WantNodes: 2, + WantMachines: 2, + InstallCNI: true, + WaitForCCM: true, }, &workloadKubeconfig) var initialWorkerNodeNames []string @@ -918,20 +807,11 @@ var _ = Describe("Manager", Ordered, func() { expectWorkloadNodesReady(g, workloadKubeconfig, 4) scaledUpWorkerNodeNames = nodeNamesForMachines(workers) }, 45*time.Minute, 15*time.Second).Should(Succeed()) - observedInstanceIDs = appendUnique(observedInstanceIDs, scaledUpInstanceIDs...) Expect(scaledUpWorkerNodeNames).To(HaveLen(3)) for _, nodeName := range initialWorkerNodeNames { Expect(scaledUpWorkerNodeNames).To(ContainElement(nodeName)) } - By("verifying all scaled-up workload VMs exist in STACKIT") - for _, instanceID := range scaledUpInstanceIDs { - instanceID := instanceID - Eventually(func(g Gomega) { - expectServerExistsInCloud(ctx, g, cloudClient, instanceID) - }, 5*time.Minute, 15*time.Second).Should(Succeed()) - } - By("scaling workload workers back down to one replica") cmd = exec.Command("kubectl", "scale", "machinedeployment", machineDeploymentName, "-n", cfg.Namespace, "--replicas=1") _, err = utils.Run(cmd) @@ -949,7 +829,6 @@ var _ = Describe("Manager", Ordered, func() { expectWorkloadNodesReady(g, workloadKubeconfig, 2) remainingWorkerNodeNames = nodeNamesForMachines(workers) }, 30*time.Minute, 15*time.Second).Should(Succeed()) - observedInstanceIDs = appendUnique(observedInstanceIDs, remainingInstanceIDs...) removedWorkerNodeNames := difference(scaledUpWorkerNodeNames, remainingWorkerNodeNames) Expect(removedWorkerNodeNames).To(HaveLen(2)) @@ -962,23 +841,10 @@ var _ = Describe("Manager", Ordered, func() { deletedInstanceIDs := difference(scaledUpInstanceIDs, remainingInstanceIDs) Expect(deletedInstanceIDs).To(HaveLen(2)) - By("verifying scaled-down workload VMs were deleted from STACKIT") - for _, instanceID := range deletedInstanceIDs { - instanceID := instanceID - Eventually(func(g Gomega) { - expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) - } - By("deleting the workload scale Cluster") cmd = exec.Command("kubectl", "delete", "cluster", clusterName, "-n", cfg.Namespace, "--wait=true", "--timeout=45m") _, err = utils.Run(cmd) Expect(err).NotTo(HaveOccurred(), "Failed to delete workload scale Cluster") - - By("verifying no tagged STACKIT resources remain for the e2e test ID") - Eventually(func(g Gomega) { - expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) It("should replace worker VMs during a MachineDeployment version upgrade without STACKIT leaks", func() { @@ -997,7 +863,6 @@ var _ = Describe("Manager", Ordered, func() { upgradeTo := envDefault("STACKIT_E2E_UPGRADE_TO", "v1.34.8") validateSupportedKubernetesVersion(upgradeFrom) validateSupportedKubernetesVersion(upgradeTo) - observedInstanceIDs := []string{} By("cleaning up stale STACKIT resources for the e2e test ID") Expect(cloud.CleanupByTags(ctx, cloudClient, leakTags)).To(Succeed()) @@ -1007,7 +872,6 @@ var _ = Describe("Manager", Ordered, func() { fixturePath := writeTempManifest("stackit-md-upgrade-e2e-*.yaml", fixture) defer func() { cleanupStackitMachineDeploymentScaleFixture(clusterName, cfg.Namespace) - cleanupCloudServersByID(ctx, cloudClient, observedInstanceIDs) if err := cloud.CleanupByTags(ctx, cloudClient, leakTags); err != nil { _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API cleanup warning: %v\n", err) } @@ -1029,7 +893,6 @@ var _ = Describe("Manager", Ordered, func() { Eventually(func(g Gomega) { initialInstanceIDs = readyStackitMachineInstanceIDs(g, cfg.Namespace, testID, 1) }, 45*time.Minute, 15*time.Second).Should(Succeed()) - observedInstanceIDs = appendUnique(observedInstanceIDs, initialInstanceIDs...) Eventually(func(g Gomega) { expectCAPIMachinesWithProviderIDs(g, cfg.Namespace, clusterName, 1) }, 5*time.Minute, 10*time.Second).Should(Succeed()) @@ -1047,7 +910,6 @@ var _ = Describe("Manager", Ordered, func() { g.Expect(instanceIDs(upgradedMachines)).To(ContainElement(initialInstanceIDs[0])) }, 45*time.Minute, 15*time.Second).Should(Succeed()) upgradedInstanceIDs := instanceIDs(upgradedMachines) - observedInstanceIDs = appendUnique(observedInstanceIDs, upgradedInstanceIDs...) Eventually(func(g Gomega) { expectCAPIMachinesWithProviderIDs(g, cfg.Namespace, clusterName, 2) }, 5*time.Minute, 10*time.Second).Should(Succeed()) @@ -1066,31 +928,10 @@ var _ = Describe("Manager", Ordered, func() { g.Expect(remainingInstanceIDs).To(Equal(replacementInstanceIDs)) }, 20*time.Minute, 15*time.Second).Should(Succeed()) - By("verifying the old worker VM was deleted from STACKIT") - Eventually(func(g Gomega) { - expectServerGoneFromCloud(ctx, g, cloudClient, initialInstanceIDs[0]) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) - By("deleting the upgraded workload Cluster") cmd = exec.Command("kubectl", "delete", "cluster", clusterName, "-n", cfg.Namespace, "--wait=true", "--timeout=45m") _, err = utils.Run(cmd) Expect(err).NotTo(HaveOccurred(), "Failed to delete upgraded workload Cluster") - - By("ensuring all observed worker VMs are deleted via the STACKIT API") - cleanupCloudServersByID(ctx, cloudClient, observedInstanceIDs) - - By("verifying all observed worker VMs were deleted from STACKIT") - for _, instanceID := range observedInstanceIDs { - instanceID := instanceID - Eventually(func(g Gomega) { - expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) - } - - By("verifying no tagged STACKIT resources remain for the e2e test ID") - Eventually(func(g Gomega) { - expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) It("should complete a workload worker MachineDeployment upgrade with Ready Nodes", func() { @@ -1112,7 +953,6 @@ var _ = Describe("Manager", Ordered, func() { upgradeTo := envDefault("STACKIT_E2E_UPGRADE_TO", "v1.35.4") validateSupportedKubernetesVersion(upgradeFrom) validateSupportedKubernetesVersion(upgradeTo) - observedInstanceIDs := []string{} By("cleaning up stale STACKIT resources for the e2e test ID") Expect(cloud.CleanupByTags(ctx, cloudClient, leakTags)).To(Succeed()) @@ -1133,7 +973,6 @@ var _ = Describe("Manager", Ordered, func() { var workloadKubeconfig string defer func() { cleanupStackitKubeadmClusterFixture(clusterName, cfg.Namespace) - cleanupCloudServersByID(ctx, cloudClient, observedInstanceIDs) if err := cloud.CleanupByTags(ctx, cloudClient, leakTags); err != nil { _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API cleanup warning: %v\n", err) } @@ -1148,14 +987,13 @@ var _ = Describe("Manager", Ordered, func() { Expect(err).NotTo(HaveOccurred(), "Failed to apply STACKIT workload worker upgrade fixture") waitForKubeadmWorkloadClusterReady(workloadReadinessOptions{ - Namespace: cfg.Namespace, - ClusterName: clusterName, - TestID: testID, - WantNodes: 3, - WantMachines: 3, - InstallCNI: true, - WaitForCCM: true, - ObservedInstanceIDs: &observedInstanceIDs, + Namespace: cfg.Namespace, + ClusterName: clusterName, + TestID: testID, + WantNodes: 3, + WantMachines: 3, + InstallCNI: true, + WaitForCCM: true, }, &workloadKubeconfig) var initialWorkerNodeNames []string @@ -1171,7 +1009,6 @@ var _ = Describe("Manager", Ordered, func() { }, 5*time.Minute, 10*time.Second).Should(Succeed()) Expect(initialWorkerNodeNames).To(HaveLen(2)) Expect(initialWorkerInstanceIDs).To(HaveLen(2)) - observedInstanceIDs = appendUnique(observedInstanceIDs, initialWorkerInstanceIDs...) By("patching the workload worker MachineDeployment Kubernetes version") patch := fmt.Sprintf(`{"spec":{"template":{"spec":{"version":"%s"}}}}`, upgradeTo) @@ -1183,8 +1020,7 @@ var _ = Describe("Manager", Ordered, func() { var upgradedWorkerInstanceIDs []string Eventually(func(g Gomega) { expectMachineDeploymentRollout(g, cfg.Namespace, machineDeploymentName, 2) - machines := readyStackitMachines(g, cfg.Namespace, testID, 3) - observedInstanceIDs = appendUnique(observedInstanceIDs, instanceIDs(machines)...) + _ = readyStackitMachines(g, cfg.Namespace, testID, 3) workers := workerMachinesForDeployment(g, cfg.Namespace, clusterName, machineDeploymentName) g.Expect(workers).To(HaveLen(2)) expectMachinesVersion(g, workers, upgradeTo) @@ -1207,23 +1043,10 @@ var _ = Describe("Manager", Ordered, func() { deletedInstanceIDs := difference(initialWorkerInstanceIDs, upgradedWorkerInstanceIDs) Expect(deletedInstanceIDs).To(HaveLen(2)) - By("verifying old workload worker VMs were deleted from STACKIT") - for _, instanceID := range deletedInstanceIDs { - instanceID := instanceID - Eventually(func(g Gomega) { - expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) - } - By("deleting the workload worker upgrade Cluster") cmd = exec.Command("kubectl", "delete", "cluster", clusterName, "-n", cfg.Namespace, "--wait=true", "--timeout=45m") _, err = utils.Run(cmd) Expect(err).NotTo(HaveOccurred(), "Failed to delete workload worker upgrade Cluster") - - By("verifying no tagged STACKIT resources remain for the e2e test ID") - Eventually(func(g Gomega) { - expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) It("should complete a workload control-plane upgrade with a Ready Node", func() { @@ -1245,7 +1068,6 @@ var _ = Describe("Manager", Ordered, func() { upgradeTo := envDefault("STACKIT_E2E_UPGRADE_TO", "v1.35.4") validateSupportedKubernetesVersion(upgradeFrom) validateSupportedKubernetesVersion(upgradeTo) - observedInstanceIDs := []string{} By("cleaning up stale STACKIT resources for the e2e test ID") Expect(cloud.CleanupByTags(ctx, cloudClient, leakTags)).To(Succeed()) @@ -1266,7 +1088,6 @@ var _ = Describe("Manager", Ordered, func() { var workloadKubeconfig string defer func() { cleanupStackitKubeadmClusterFixture(clusterName, cfg.Namespace) - cleanupCloudServersByID(ctx, cloudClient, observedInstanceIDs) if err := cloud.CleanupByTags(ctx, cloudClient, leakTags); err != nil { _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API cleanup warning: %v\n", err) } @@ -1281,14 +1102,13 @@ var _ = Describe("Manager", Ordered, func() { Expect(err).NotTo(HaveOccurred(), "Failed to apply STACKIT workload control-plane upgrade fixture") waitForKubeadmWorkloadClusterReady(workloadReadinessOptions{ - Namespace: cfg.Namespace, - ClusterName: clusterName, - TestID: testID, - WantNodes: 2, - WantMachines: 2, - InstallCNI: true, - WaitForCCM: true, - ObservedInstanceIDs: &observedInstanceIDs, + Namespace: cfg.Namespace, + ClusterName: clusterName, + TestID: testID, + WantNodes: 2, + WantMachines: 2, + InstallCNI: true, + WaitForCCM: true, }, &workloadKubeconfig) var initialControlPlaneInstanceIDs []string @@ -1301,7 +1121,6 @@ var _ = Describe("Manager", Ordered, func() { initialControlPlaneInstanceIDs = stackitInstanceIDsForMachines(g, cfg.Namespace, testID, controlPlanes) }, 5*time.Minute, 10*time.Second).Should(Succeed()) Expect(initialControlPlaneInstanceIDs).To(HaveLen(1)) - observedInstanceIDs = appendUnique(observedInstanceIDs, initialControlPlaneInstanceIDs...) By("patching the workload KubeadmControlPlane Kubernetes version") patch := fmt.Sprintf(`{"spec":{"version":"%s"}}`, upgradeTo) @@ -1314,8 +1133,7 @@ var _ = Describe("Manager", Ordered, func() { output := kubectlOutputWithKubeconfig(g, workloadKubeconfig, "get", "ns", "kube-system", "-o", "name") g.Expect(output).To(Equal("namespace/kube-system")) expectKubeadmControlPlaneRollout(g, cfg.Namespace, kubeadmControlPlaneName, 1) - machines := readyStackitMachines(g, cfg.Namespace, testID, 2) - observedInstanceIDs = appendUnique(observedInstanceIDs, instanceIDs(machines)...) + _ = readyStackitMachines(g, cfg.Namespace, testID, 2) controlPlanes := controlPlaneMachinesForCluster(g, cfg.Namespace, clusterName) g.Expect(controlPlanes).To(HaveLen(1)) expectMachinesVersion(g, controlPlanes, upgradeTo) @@ -1325,24 +1143,10 @@ var _ = Describe("Manager", Ordered, func() { }, 60*time.Minute, 15*time.Second).Should(Succeed()) Expect(upgradedControlPlaneInstanceIDs).To(HaveLen(1)) - deletedInstanceIDs := difference(initialControlPlaneInstanceIDs, upgradedControlPlaneInstanceIDs) - By("verifying old workload control-plane VMs were deleted from STACKIT when replaced") - for _, instanceID := range deletedInstanceIDs { - instanceID := instanceID - Eventually(func(g Gomega) { - expectServerGoneFromCloud(ctx, g, cloudClient, instanceID) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) - } - By("deleting the workload control-plane upgrade Cluster") cmd = exec.Command("kubectl", "delete", "cluster", clusterName, "-n", cfg.Namespace, "--wait=true", "--timeout=45m") _, err = utils.Run(cmd) Expect(err).NotTo(HaveOccurred(), "Failed to delete workload control-plane upgrade Cluster") - - By("verifying no tagged STACKIT resources remain for the e2e test ID") - Eventually(func(g Gomega) { - expectNoLeakedServersOrLoadBalancers(ctx, g, cloudClient, leakTags) - }, 20*time.Minute, 15*time.Second).Should(Succeed()) }) }) }) @@ -1389,15 +1193,14 @@ type topologyWorkloadFixtureOptions struct { } type workloadReadinessOptions struct { - Namespace string - ClusterName string - TestID string - WantNodes int - WantMachines int - InstallCNI bool - WaitForCCM bool - Topology bool - ObservedInstanceIDs *[]string + Namespace string + ClusterName string + TestID string + WantNodes int + WantMachines int + InstallCNI bool + WaitForCCM bool + Topology bool } func stackitVMConfigFromEnv() stackitVMConfig { @@ -2292,18 +2095,6 @@ func stackitE2ETags(testID string) map[string]string { } } -func stackitE2EBastionTags(testID string) map[string]string { - tags := stackitE2ETags(testID) - tags[util.LabelResourceRole] = util.ResourceRoleBastion - return tags -} - -func stackitE2ENodeSSHTags(testID string) map[string]string { - tags := stackitE2ETags(testID) - tags[util.LabelResourceRole] = util.ResourceRoleNodeSSH - return tags -} - func cleanupStackitClusterFixture(clusterName, namespace string) { for _, args := range [][]string{ {"delete", "cluster", clusterName, "-n", namespace, "--ignore-not-found", "--wait=true", "--timeout=45m"}, @@ -2661,14 +2452,12 @@ func waitForKubeadmWorkloadClusterReady(opts workloadReadinessOptions, workloadK if opts.Topology { By("waiting for the topology control-plane VM to provision") Eventually(func(g Gomega) { - machine := readyTopologyControlPlaneStackitMachine(g, opts.Namespace, opts.ClusterName, opts.TestID) - appendObservedInstanceIDs(opts.ObservedInstanceIDs, machine.Status.InstanceID) + _ = readyTopologyControlPlaneStackitMachine(g, opts.Namespace, opts.ClusterName, opts.TestID) }, 45*time.Minute, 15*time.Second).Should(Succeed()) } else { By("waiting for the control-plane VM to provision") Eventually(func(g Gomega) { - machine := readyStackitMachineByNamePart(g, opts.Namespace, opts.TestID, "control-plane") - appendObservedInstanceIDs(opts.ObservedInstanceIDs, machine.Status.InstanceID) + _ = readyStackitMachineByNamePart(g, opts.Namespace, opts.TestID, "control-plane") }, 45*time.Minute, 15*time.Second).Should(Succeed()) } @@ -2691,8 +2480,7 @@ func waitForKubeadmWorkloadClusterReady(opts workloadReadinessOptions, workloadK By("waiting for workload StackitMachines to provision") Eventually(func(g Gomega) { - machines := readyStackitMachines(g, opts.Namespace, opts.TestID, opts.WantMachines) - appendObservedInstanceIDs(opts.ObservedInstanceIDs, instanceIDs(machines)...) + _ = readyStackitMachines(g, opts.Namespace, opts.TestID, opts.WantMachines) }, 45*time.Minute, 15*time.Second).Should(Succeed()) By("waiting for workload Nodes to be Ready") @@ -2706,13 +2494,6 @@ func waitForKubeadmWorkloadClusterReady(opts workloadReadinessOptions, workloadK }, 15*time.Minute, 10*time.Second).Should(Succeed()) } -func appendObservedInstanceIDs(observed *[]string, instanceIDs ...string) { - if observed == nil { - return - } - *observed = appendUnique(*observed, instanceIDs...) -} - func waitForWorkloadCCMRollout(kubeconfig string) { Eventually(func(g Gomega) { cmd := exec.Command("kubectl", "--kubeconfig", kubeconfig, "-n", "kube-system", "rollout", "status", @@ -3036,114 +2817,6 @@ func difference(left, right []string) []string { return out } -func appendUnique(values []string, additions ...string) []string { - seen := map[string]struct{}{} - for _, value := range values { - seen[value] = struct{}{} - } - for _, value := range additions { - if _, ok := seen[value]; ok { - continue - } - seen[value] = struct{}{} - values = append(values, value) - } - return values -} - -func expectServerExistsInCloud(ctx context.Context, g Gomega, cloudClient cloud.Client, instanceID string) { - srv, err := cloudClient.IaaSClient().DefaultAPI.GetServer(ctx, cloudClient.ProjectID(), cloudClient.Region(), instanceID).Execute() - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(srv.GetId()).To(Equal(instanceID)) -} - -func expectServerGoneFromCloud(ctx context.Context, g Gomega, cloudClient cloud.Client, instanceID string) { - _, err := cloudClient.IaaSClient().DefaultAPI.GetServer(ctx, cloudClient.ProjectID(), cloudClient.Region(), instanceID).Execute() - g.Expect(err).To(HaveOccurred()) - g.Expect(shared.IsNotFound(shared.ClassifySDKError("get server", err))).To(BeTrue(), "expected server %s to be deleted, got %v", instanceID, err) -} - -func expectNoLeakedServersOrLoadBalancers(ctx context.Context, g Gomega, cloudClient cloud.Client, tags map[string]string) { - serverIDs, err := shared.ListServerIDsByTags(ctx, cloudClient, tags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(serverIDs).To(BeEmpty()) - lbCount, err := countLoadBalancersByTags(ctx, cloudClient, tags) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(lbCount).To(Equal(0)) -} - -func countLoadBalancersByTags(ctx context.Context, cloudClient cloud.Client, tags map[string]string) (int, error) { - resp, err := cloudClient.LoadBalancerClient().DefaultAPI.ListLoadBalancers(ctx, cloudClient.ProjectID(), cloudClient.Region()).Execute() - if err != nil { - return 0, shared.ClassifySDKError("list load balancers", err) - } - labels := loadbalancer.Labels(tags) - count := 0 - for _, candidate := range resp.GetLoadBalancers() { - if shared.MapContains(candidate.GetLabels(), labels) { - count++ - } - } - return count, nil -} - -func countPublicIPsByTags(ctx context.Context, cloudClient cloud.Client, tags map[string]string) (int, error) { - resp, err := cloudClient.IaaSClient().DefaultAPI.ListPublicIPs(ctx, cloudClient.ProjectID(), cloudClient.Region()). - LabelSelector(shared.LabelSelector(tags)). - Execute() - if err != nil { - return 0, shared.ClassifySDKError("list public IPs", err) - } - count := 0 - for _, item := range resp.GetItems() { - if shared.LabelsContainTags(item.GetLabels(), tags) { - count++ - } - } - return count, nil -} - -func countSecurityGroupsByTags(ctx context.Context, cloudClient cloud.Client, tags map[string]string) (int, error) { - resp, err := cloudClient.IaaSClient().DefaultAPI.ListSecurityGroups(ctx, cloudClient.ProjectID(), cloudClient.Region()). - LabelSelector(shared.LabelSelector(tags)). - Execute() - if err != nil { - return 0, shared.ClassifySDKError("list security groups", err) - } - count := 0 - for _, item := range resp.GetItems() { - if shared.LabelsContainTags(item.GetLabels(), tags) { - count++ - } - } - return count, nil -} - -func cleanupCloudServersByID(ctx context.Context, cloudClient cloud.Client, instanceIDs []string) { - for _, instanceID := range instanceIDs { - if err := cloudClient.IaaSClient().DefaultAPI.DeleteServer(ctx, cloudClient.ProjectID(), cloudClient.Region(), instanceID).Execute(); err != nil { - classified := shared.ClassifySDKError("delete server", err) - if !shared.IsNotFound(classified) { - _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API server cleanup warning for %s: %v\n", instanceID, classified) - continue - } - } - deadline := time.Now().Add(10 * time.Minute) - for time.Now().Before(deadline) { - _, err := cloudClient.IaaSClient().DefaultAPI.GetServer(ctx, cloudClient.ProjectID(), cloudClient.Region(), instanceID).Execute() - if err != nil { - classified := shared.ClassifySDKError("get server", err) - if shared.IsNotFound(classified) { - break - } - _, _ = fmt.Fprintf(GinkgoWriter, "STACKIT API server cleanup verification warning for %s: %v\n", instanceID, classified) - break - } - time.Sleep(15 * time.Second) - } - } -} - func cleanupStackitVMFixture(clusterName, machineName, namespace string) { for _, args := range [][]string{ {"delete", "stackitmachine", machineName, "-n", namespace, "--ignore-not-found", "--wait=true", "--timeout=20m"}, From c5215282e77fbc8e916fd8ee5ec4785d43b47469 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 13:58:41 +0100 Subject: [PATCH 28/60] docs: remove comments --- test/utils/utils.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/test/utils/utils.go b/test/utils/utils.go index 9600e4b..a408630 100644 --- a/test/utils/utils.go +++ b/test/utils/utils.go @@ -35,12 +35,10 @@ const ( defaultKindCluster = "kind" ) -// TODO: unreachable func warnError(err error) { _, _ = fmt.Fprintf(GinkgoWriter, "warning: %v\n", err) } -// TODO: unreachable // Run executes the provided command within this context func Run(cmd *exec.Cmd) (string, error) { dir, _ := GetProjectDir() @@ -61,7 +59,6 @@ func Run(cmd *exec.Cmd) (string, error) { return string(output), nil } -// TODO: unreachable // UninstallCertManager uninstalls the cert manager func UninstallCertManager() { url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion) @@ -84,7 +81,6 @@ func UninstallCertManager() { } } -// TODO: unreachable // InstallCertManager installs the cert manager bundle. func InstallCertManager() error { url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion) @@ -104,7 +100,6 @@ func InstallCertManager() error { return err } -// TODO: unreachable // IsCertManagerCRDsInstalled checks if any Cert Manager CRDs are installed // by verifying the existence of key CRDs related to Cert Manager. func IsCertManagerCRDsInstalled() bool { @@ -138,7 +133,6 @@ func IsCertManagerCRDsInstalled() bool { return false } -// TODO: unreachable // LoadImageToKindClusterWithName loads a local docker image to the kind cluster func LoadImageToKindClusterWithName(name string) error { cluster := defaultKindCluster @@ -155,7 +149,6 @@ func LoadImageToKindClusterWithName(name string) error { return err } -// TODO: unreachable // GetNonEmptyLines converts given command output string into individual objects // according to line breakers, and ignores the empty elements in it. func GetNonEmptyLines(output string) []string { @@ -170,7 +163,6 @@ func GetNonEmptyLines(output string) []string { return res } -// TODO: unreachable // GetProjectDir will return the directory where the project is func GetProjectDir() (string, error) { wd, err := os.Getwd() @@ -181,7 +173,6 @@ func GetProjectDir() (string, error) { return wd, nil } -// TODO: unreachable // UncommentCode searches for target in the file and remove the comment prefix // of the target content. The target content may span multiple lines. func UncommentCode(filename, target, prefix string) error { From cab79ba77826bf24c0a68569d0867fdf45484216 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 14:15:01 +0100 Subject: [PATCH 29/60] refactor: remove unused return fields on ParseProviderID --- cloud/providerid.go | 8 ++++---- cloud/providerid_test.go | 26 +++++++++----------------- webhook/v1alpha1/validation_helpers.go | 2 +- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/cloud/providerid.go b/cloud/providerid.go index 2523112..66ae6c4 100644 --- a/cloud/providerid.go +++ b/cloud/providerid.go @@ -41,13 +41,13 @@ func NewProviderID(serverID string) string { // STACKIT providerID format does not contain project or region, so those return // values are empty. It returns ErrInvalidProviderID if any encoded component is // empty or the scheme is wrong. -func ParseProviderID(providerID string) (projectID, region, serverID string, err error) { +func ParseProviderID(providerID string) (serverID string, err error) { if !strings.HasPrefix(providerID, providerIDScheme) { - return "", "", "", fmt.Errorf("%w: missing scheme %q", ErrInvalidProviderID, providerIDScheme) + return "", fmt.Errorf("%w: missing scheme %q", ErrInvalidProviderID, providerIDScheme) } serverID = strings.TrimPrefix(providerID, providerIDScheme) if serverID == "" || strings.Contains(serverID, "/") { - return "", "", "", fmt.Errorf("%w: expected , got %q", ErrInvalidProviderID, serverID) + return "", fmt.Errorf("%w: expected , got %q", ErrInvalidProviderID, serverID) } - return "", "", serverID, nil + return serverID, nil } diff --git a/cloud/providerid_test.go b/cloud/providerid_test.go index 7a06942..cf21cd7 100644 --- a/cloud/providerid_test.go +++ b/cloud/providerid_test.go @@ -24,12 +24,12 @@ func TestNewProviderID(t *testing.T) { } func TestParseProviderID(t *testing.T) { - projectID, region, serverID, err := ParseProviderID("stackit://server-id") + serverID, err := ParseProviderID("stackit://server-id") if err != nil { t.Fatalf("ParseProviderID() error = %v", err) } - if projectID != "" || region != "" || serverID != "server-id" { - t.Fatalf("ParseProviderID() = %q, %q, %q", projectID, region, serverID) + if serverID != "server-id" { + t.Fatalf("ParseProviderID() = %q", serverID) } } @@ -45,7 +45,7 @@ func TestParseProviderIDInvalid(t *testing.T) { for _, tt := range tests { t.Run(tt, func(t *testing.T) { - _, _, _, err := ParseProviderID(tt) + _, err := ParseProviderID(tt) if !errors.Is(err, ErrInvalidProviderID) { t.Fatalf("ParseProviderID(%q) error = %v, want ErrInvalidProviderID", tt, err) } @@ -56,12 +56,12 @@ func TestParseProviderIDInvalid(t *testing.T) { func TestProviderIDRoundTrip(t *testing.T) { wantServerID := "server-id" - gotProjectID, gotRegion, gotServerID, err := ParseProviderID(NewProviderID(wantServerID)) + gotServerID, err := ParseProviderID(NewProviderID(wantServerID)) if err != nil { t.Fatalf("ParseProviderID(NewProviderID()) error = %v", err) } - if gotProjectID != "" || gotRegion != "" || gotServerID != wantServerID { - t.Fatalf("roundtrip = %q, %q, %q", gotProjectID, gotRegion, gotServerID) + if gotServerID != wantServerID { + t.Fatalf("roundtrip = %q", gotServerID) } } @@ -75,19 +75,11 @@ func TestProviderIDMatchesCloudProviderStackitFormat(t *testing.T) { t.Fatalf("NewProviderID() = %q, want cloud-provider-stackit format %q", providerID, "stackit://"+serverID) } - projectID, region, parsedServerID, err := ParseProviderID(providerID) + parsedServerID, err := ParseProviderID(providerID) if err != nil { t.Fatalf("ParseProviderID(%q) error = %v", providerID, err) } - if projectID != "" || region != "" { - t.Fatalf( - "ParseProviderID(%q) returned project/region %q/%q, want empty because cloud-provider-stackit does not encode them", - providerID, - projectID, - region, - ) - } if parsedServerID != serverID { - t.Fatalf("ParseProviderID(%q) serverID = %q, want %q", providerID, parsedServerID, serverID) + t.Fatalf("ParseProviderID(%q) serverID = %q, want %q", providerID, serverID, parsedServerID) } } diff --git a/webhook/v1alpha1/validation_helpers.go b/webhook/v1alpha1/validation_helpers.go index d44d3f8..1386c54 100644 --- a/webhook/v1alpha1/validation_helpers.go +++ b/webhook/v1alpha1/validation_helpers.go @@ -192,7 +192,7 @@ func validateStackitMachineSpec(spec infrav1.StackitMachineSpec, fldPath *field. if spec.ProviderID != nil { if !allowProviderID { allErrs = append(allErrs, field.Forbidden(fldPath.Child("providerID"), "providerID is generated per machine and must not be set in a template")) - } else if _, _, _, err := cloud.ParseProviderID(*spec.ProviderID); err != nil { + } else if _, err := cloud.ParseProviderID(*spec.ProviderID); err != nil { allErrs = append(allErrs, field.Invalid(fldPath.Child("providerID"), *spec.ProviderID, "must use STACKIT providerID format stackit://")) } } From 2c741471d8a1eccdae7e7e3f80a134003cdeed52 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 14:18:30 +0100 Subject: [PATCH 30/60] refactor: remove unused receiver from deleteBastionResources --- controller/stackitcluster_bastion.go | 6 +++--- controller/stackitcluster_infrastructure.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index 5af8ddb..cbea64e 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -52,7 +52,7 @@ func (r *StackitClusterReconciler) reconcileBastion( condition := meta.FindStatusCondition(stackitCluster.Status.Conditions, infrav1.ClusterBastionReadyCondition) if condition == nil || condition.Reason != bastionDisabledReason { hadBastion := hasBastionStatus(stackitCluster.Status.Bastion) - if err := r.deleteBastionResources(ctx, clusterScope); err != nil { + if err := deleteBastionResources(ctx, clusterScope); err != nil { return ctrl.Result{}, false, err } clusterScope.ClearBastionStatus() @@ -96,7 +96,7 @@ func (r *StackitClusterReconciler) reconcileBastion( clusterScope.SetBastionCloudInit(cloudInit, bastionCloudInitHash(cloudInit)) if bastionNeedsRecreate(stackitCluster, cloudInit) { - if err := r.deleteBastionResources(ctx, clusterScope); err != nil { + if err := deleteBastionResources(ctx, clusterScope); err != nil { return ctrl.Result{}, false, err } clusterScope.ClearBastionStatus() @@ -183,7 +183,7 @@ func hasBastionStatus(status infrav1.StackitBastionStatus) bool { return status.ServerID != "" || status.PublicIPID != "" || status.PublicIP != "" || status.SecurityGroupID != "" || status.CloudInitHash != "" } -func (r *StackitClusterReconciler) deleteBastionResources( +func deleteBastionResources( ctx context.Context, clusterScope *scope.ClusterScope, ) error { diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index fe6eac7..71e41bc 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -336,7 +336,7 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS ) } hadBastion := hasBastionStatus(stackitCluster.Status.Bastion) - if err := r.deleteBastionResources(ctx, clusterScope); err != nil { + if err := deleteBastionResources(ctx, clusterScope); err != nil { log.Error(err, "Failed to delete bastion", "cluster", stackitCluster.Name) record.Warnf(stackitCluster, "FailedDeleteBastion", "Failed to delete bastion: %v", err) From dbccb0cdade55c7960c5c17264ed7a365a834ffb Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 14:27:53 +0100 Subject: [PATCH 31/60] fix: treat missing tag-referenced remote security group as transient error --- .../services/securitygroup/reconcile_test.go | 34 +++++++++++++++++++ cloud/services/securitygroup/rules.go | 3 ++ controller/stackitmachine_controller_test.go | 19 +++++++++++ 3 files changed, 56 insertions(+) diff --git a/cloud/services/securitygroup/reconcile_test.go b/cloud/services/securitygroup/reconcile_test.go index 30df352..8e2dafb 100644 --- a/cloud/services/securitygroup/reconcile_test.go +++ b/cloud/services/securitygroup/reconcile_test.go @@ -309,6 +309,40 @@ func TestServiceReconcileReuseAndErrors(t *testing.T) { t.Fatalf("Reconcile() error = %v, want ErrConflict", err) } }) + + t.Run("returns retryable ErrTransient when remote security group referenced by tags does not exist yet", func(t *testing.T) { + specs := []securitygroup.SecurityGroupSpec{{ + ID: testSecurityGroupID, + Name: "node-ssh", + Tags: map[string]string{"role": "node-ssh"}, + Rules: []securitygroup.Rule{{ + Direction: securitygroup.DirectionIngress, + Protocol: securitygroup.ProtocolTCP, + PortMin: 22, + PortMax: 22, + RemoteSecurityGroupTags: map[string]string{"role": "bastion"}, + }}, + }} + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID): + writeJSON(t, w, map[string]any{"id": testSecurityGroupID, "name": "node-ssh"}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups"): + writeJSON(t, w, map[string]any{"items": []any{}}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + err := svc.Reconcile(context.Background()) + if !errors.Is(err, shared.ErrTransient) { + t.Fatalf("Reconcile() error = %v, want ErrTransient", err) + } + }) } func TestServiceReconcileBastionAndNodeSSH(t *testing.T) { diff --git a/cloud/services/securitygroup/rules.go b/cloud/services/securitygroup/rules.go index 3d0b0e4..bd085bf 100644 --- a/cloud/services/securitygroup/rules.go +++ b/cloud/services/securitygroup/rules.go @@ -63,6 +63,9 @@ func (c *client) resolveRule(ctx context.Context, rule Rule) (Rule, error) { if rule.RemoteSecurityGroupID == "" && len(rule.RemoteSecurityGroupTags) > 0 { remoteSG, err := c.findByTags(ctx, rule.RemoteSecurityGroupTags) if err != nil { + if shared.IsNotFound(err) { + return Rule{}, fmt.Errorf("%w: remote security group not ready: %w", shared.ErrTransient, err) + } return Rule{}, err } rule.RemoteSecurityGroupID = remoteSG.ID diff --git a/controller/stackitmachine_controller_test.go b/controller/stackitmachine_controller_test.go index f95fdbc..084b725 100644 --- a/controller/stackitmachine_controller_test.go +++ b/controller/stackitmachine_controller_test.go @@ -261,6 +261,25 @@ var _ = Describe("StackitMachine Controller", func() { Expect(fakeCloud.ServerHasSecurityGroup(got.Status.InstanceID, securityGroup.ID)).To(BeTrue()) }) + It("requeues when bastion is enabled and bastion security group does not exist yet", func() { + stackitCluster := &infrav1.StackitCluster{} + Expect(k8sClient.Get(ctx, types.NamespacedName{Name: clusterName, Namespace: namespace}, stackitCluster)).To(Succeed()) + stackitCluster.Spec.Bastion = validBastionSpec() + Expect(k8sClient.Update(ctx, stackitCluster)).To(Succeed()) + stackitCluster.Status.Ready = true + stackitCluster.Status.Bastion.SecurityGroupID = "" + Expect(k8sClient.Status().Update(ctx, stackitCluster)).To(Succeed()) + + result, err := reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + Expect(result.RequeueAfter).To(BeNumerically(">", 0)) + + got := &infrav1.StackitMachine{} + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + Expect(got.Status.Ready).To(BeFalse()) + expectCondition(got.Status.Conditions, infrav1.MachineReadyCondition, metav1.ConditionFalse, "BastionSSHAccessError") + }) + It("marks conditions not ready when bastion node SSH access fails", func() { stackitCluster := &infrav1.StackitCluster{} Expect(k8sClient.Get(ctx, types.NamespacedName{Name: clusterName, Namespace: namespace}, stackitCluster)).To(Succeed()) From 42733723ef11f87c98e92a4dd042d9a6ef43f144 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 14:36:07 +0100 Subject: [PATCH 32/60] fix: avoid dual sentinel wrapping on missing remote security group --- cloud/services/securitygroup/rules.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud/services/securitygroup/rules.go b/cloud/services/securitygroup/rules.go index bd085bf..2948cf7 100644 --- a/cloud/services/securitygroup/rules.go +++ b/cloud/services/securitygroup/rules.go @@ -64,7 +64,7 @@ func (c *client) resolveRule(ctx context.Context, rule Rule) (Rule, error) { remoteSG, err := c.findByTags(ctx, rule.RemoteSecurityGroupTags) if err != nil { if shared.IsNotFound(err) { - return Rule{}, fmt.Errorf("%w: remote security group not ready: %w", shared.ErrTransient, err) + return Rule{}, fmt.Errorf("%w: remote security group not ready: %v", shared.ErrTransient, err) } return Rule{}, err } From 5c7a84754c60dadeb7eb7b03c638291549ba4726 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 15:17:13 +0100 Subject: [PATCH 33/60] refactor: remove unused ParseProviderID function --- cloud/providerid.go | 21 --------- cloud/providerid_test.go | 51 --------------------- docs/src/development/architecture.md | 1 - webhook/v1alpha1/validation_helpers.go | 9 +--- webhook/v1alpha1/webhook_validation_test.go | 9 ---- 5 files changed, 2 insertions(+), 89 deletions(-) diff --git a/cloud/providerid.go b/cloud/providerid.go index 66ae6c4..e0d60e9 100644 --- a/cloud/providerid.go +++ b/cloud/providerid.go @@ -11,23 +11,17 @@ You may obtain a copy of the License at package cloud import ( - "errors" "fmt" - "strings" ) // providerIDScheme is the URL scheme used for STACKIT providerIDs. const providerIDScheme = "stackit://" -// ErrInvalidProviderID is returned by ParseProviderID for malformed values. -var ErrInvalidProviderID = errors.New("invalid providerID") - // NewProviderID returns a providerID string in the STACKIT format used by // cloud-provider-stackit. // // Format verified against the local cloud-provider-stackit repository: // - pkg/ccm/instances.go: Instances.makeInstanceID returns stackit:// -// - pkg/ccm/instances.go: instanceIDFromProviderID parses only the server ID // // Project ID and region are intentionally not encoded in the providerID. The // cloud-provider-stackit controller gets project and region from its own @@ -36,18 +30,3 @@ var ErrInvalidProviderID = errors.New("invalid providerID") func NewProviderID(serverID string) string { return fmt.Sprintf("%s%s", providerIDScheme, serverID) } - -// ParseProviderID splits a providerID string into its components. The current -// STACKIT providerID format does not contain project or region, so those return -// values are empty. It returns ErrInvalidProviderID if any encoded component is -// empty or the scheme is wrong. -func ParseProviderID(providerID string) (serverID string, err error) { - if !strings.HasPrefix(providerID, providerIDScheme) { - return "", fmt.Errorf("%w: missing scheme %q", ErrInvalidProviderID, providerIDScheme) - } - serverID = strings.TrimPrefix(providerID, providerIDScheme) - if serverID == "" || strings.Contains(serverID, "/") { - return "", fmt.Errorf("%w: expected , got %q", ErrInvalidProviderID, serverID) - } - return serverID, nil -} diff --git a/cloud/providerid_test.go b/cloud/providerid_test.go index cf21cd7..5d25203 100644 --- a/cloud/providerid_test.go +++ b/cloud/providerid_test.go @@ -11,7 +11,6 @@ You may obtain a copy of the License at package cloud import ( - "errors" "testing" ) @@ -23,48 +22,6 @@ func TestNewProviderID(t *testing.T) { } } -func TestParseProviderID(t *testing.T) { - serverID, err := ParseProviderID("stackit://server-id") - if err != nil { - t.Fatalf("ParseProviderID() error = %v", err) - } - if serverID != "server-id" { - t.Fatalf("ParseProviderID() = %q", serverID) - } -} - -func TestParseProviderIDInvalid(t *testing.T) { - tests := []string{ - "", - "aws://server-id", - "stackit://project-id/server-id", - "stackit:///server-id", - "stackit://server-id/extra", - "stackit://", - } - - for _, tt := range tests { - t.Run(tt, func(t *testing.T) { - _, err := ParseProviderID(tt) - if !errors.Is(err, ErrInvalidProviderID) { - t.Fatalf("ParseProviderID(%q) error = %v, want ErrInvalidProviderID", tt, err) - } - }) - } -} - -func TestProviderIDRoundTrip(t *testing.T) { - wantServerID := "server-id" - - gotServerID, err := ParseProviderID(NewProviderID(wantServerID)) - if err != nil { - t.Fatalf("ParseProviderID(NewProviderID()) error = %v", err) - } - if gotServerID != wantServerID { - t.Fatalf("roundtrip = %q", gotServerID) - } -} - func TestProviderIDMatchesCloudProviderStackitFormat(t *testing.T) { serverID := "321a8b81-3660-43e5-bab8-6470b65ee4e8" providerID := NewProviderID(serverID) @@ -74,12 +31,4 @@ func TestProviderIDMatchesCloudProviderStackitFormat(t *testing.T) { if providerID != "stackit://"+serverID { t.Fatalf("NewProviderID() = %q, want cloud-provider-stackit format %q", providerID, "stackit://"+serverID) } - - parsedServerID, err := ParseProviderID(providerID) - if err != nil { - t.Fatalf("ParseProviderID(%q) error = %v", providerID, err) - } - if parsedServerID != serverID { - t.Fatalf("ParseProviderID(%q) serverID = %q, want %q", providerID, serverID, parsedServerID) - } } diff --git a/docs/src/development/architecture.md b/docs/src/development/architecture.md index 80c5327..5d4d2bc 100644 --- a/docs/src/development/architecture.md +++ b/docs/src/development/architecture.md @@ -101,7 +101,6 @@ repository. Relevant reference points: - `pkg/ccm/instances.go`: `Instances.makeInstanceID` returns `stackit://`. -- `pkg/ccm/instances.go`: `instanceIDFromProviderID` parses `stackit://` with no project or region component. - `pkg/ccm/instances.go`: `getInstance` resolves nodes with `GetServer(projectID, region, serverID)`, where project and region come from the cloud-controller-manager configuration. - `pkg/ccm/instances_test.go`: the "new providerID" table entry expects `stackit://hello-server`. diff --git a/webhook/v1alpha1/validation_helpers.go b/webhook/v1alpha1/validation_helpers.go index 1386c54..d65daee 100644 --- a/webhook/v1alpha1/validation_helpers.go +++ b/webhook/v1alpha1/validation_helpers.go @@ -31,7 +31,6 @@ import ( clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud" ) const ( @@ -189,12 +188,8 @@ func validateBastionCloudInitRef(ref infrav1.StackitBastionCloudInitRef, fldPath func validateStackitMachineSpec(spec infrav1.StackitMachineSpec, fldPath *field.Path, allowProviderID bool) field.ErrorList { var allErrs field.ErrorList - if spec.ProviderID != nil { - if !allowProviderID { - allErrs = append(allErrs, field.Forbidden(fldPath.Child("providerID"), "providerID is generated per machine and must not be set in a template")) - } else if _, err := cloud.ParseProviderID(*spec.ProviderID); err != nil { - allErrs = append(allErrs, field.Invalid(fldPath.Child("providerID"), *spec.ProviderID, "must use STACKIT providerID format stackit://")) - } + if spec.ProviderID != nil && !allowProviderID { + allErrs = append(allErrs, field.Forbidden(fldPath.Child("providerID"), "providerID is generated per machine and must not be set in a template")) } allErrs = append(allErrs, validateRequiredPattern(spec.ImageID, uuidPattern, fldPath.Child("imageID"), "must be a STACKIT image UUID")...) diff --git a/webhook/v1alpha1/webhook_validation_test.go b/webhook/v1alpha1/webhook_validation_test.go index c16f632..5a6923e 100644 --- a/webhook/v1alpha1/webhook_validation_test.go +++ b/webhook/v1alpha1/webhook_validation_test.go @@ -144,15 +144,6 @@ var _ = Describe("STACKIT admission webhooks", func() { Expect(err).NotTo(HaveOccurred()) }) - It("rejects invalid providerID", func() { - machine := validStackitMachine("invalid-providerid") - providerID := "stackit://" - machine.Spec.ProviderID = &providerID - - _, err := validator.ValidateCreate(ctx, machine) - expectInvalidField(err, "spec.providerID") - }) - It("defaults machine root volume deleteOnTermination idempotently", func() { machine := validStackitMachine("default-machine") From 623188ff29317d5b6fcd894fd134bd07c5c7fb91 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 15:35:12 +0100 Subject: [PATCH 34/60] refactor: remove redundant ClearBastionStatus after bastion service deletion --- controller/stackitcluster_bastion.go | 2 -- controller/stackitcluster_infrastructure.go | 1 - scope/cluster_scope.go | 4 ---- 3 files changed, 7 deletions(-) diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index cbea64e..d1d6d71 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -55,7 +55,6 @@ func (r *StackitClusterReconciler) reconcileBastion( if err := deleteBastionResources(ctx, clusterScope); err != nil { return ctrl.Result{}, false, err } - clusterScope.ClearBastionStatus() if hadBastion { record.Event(stackitCluster, "BastionDeleted", "Deleted bastion") } @@ -99,7 +98,6 @@ func (r *StackitClusterReconciler) reconcileBastion( if err := deleteBastionResources(ctx, clusterScope); err != nil { return ctrl.Result{}, false, err } - clusterScope.ClearBastionStatus() clusterScope.SetNotReady( "Recreating", "recreating bastion because cloudInitRef content changed", diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index 71e41bc..368b80d 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -343,7 +343,6 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS return ctrl.Result{}, err } if hadBastion { - clusterScope.ClearBastionStatus() record.Event(stackitCluster, "BastionDeleted", "Deleted bastion") } controllerutil.RemoveFinalizer(stackitCluster, infrav1.ClusterFinalizer) diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index 165277e..94ab212 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -170,10 +170,6 @@ func (s *ClusterScope) setBastionSecurityGroupID(id string) { s.StackitCluster.Status.Bastion.SecurityGroupID = id } -func (s *ClusterScope) ClearBastionStatus() { - s.StackitCluster.Status.Bastion = infrav1.StackitBastionStatus{} -} - func (s *ClusterScope) SetFailureDomains(failureDomains []clusterv1.FailureDomain) { s.StackitCluster.Status.FailureDomains = failureDomains } From 1dc195d444ed2d45bd9bb64e0f5154647b6a9bc9 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 15:42:21 +0100 Subject: [PATCH 35/60] refactor: deduplicate bastion deletion event handling and move HasBastionStatus to ClusterScope --- controller/stackitcluster_bastion.go | 20 +++++++++++++------- controller/stackitcluster_controller_test.go | 2 +- controller/stackitcluster_infrastructure.go | 6 +----- scope/cluster_scope.go | 5 +++++ scope/cluster_scope_test.go | 14 ++++++++++++++ 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index d1d6d71..b08b359 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -51,13 +51,9 @@ func (r *StackitClusterReconciler) reconcileBastion( // the status keeps the tag-based cleanup to once per cluster. condition := meta.FindStatusCondition(stackitCluster.Status.Conditions, infrav1.ClusterBastionReadyCondition) if condition == nil || condition.Reason != bastionDisabledReason { - hadBastion := hasBastionStatus(stackitCluster.Status.Bastion) - if err := deleteBastionResources(ctx, clusterScope); err != nil { + if err := deleteBastion(ctx, clusterScope); err != nil { return ctrl.Result{}, false, err } - if hadBastion { - record.Event(stackitCluster, "BastionDeleted", "Deleted bastion") - } } clusterScope.SetConditions( metav1.ConditionTrue, @@ -177,8 +173,18 @@ func validateBastionSpec(spec infrav1.StackitBastionSpec) error { return nil } -func hasBastionStatus(status infrav1.StackitBastionStatus) bool { - return status.ServerID != "" || status.PublicIPID != "" || status.PublicIP != "" || status.SecurityGroupID != "" || status.CloudInitHash != "" +func deleteBastion( + ctx context.Context, + clusterScope *scope.ClusterScope, +) error { + hadBastion := clusterScope.HasBastionStatus() + if err := deleteBastionResources(ctx, clusterScope); err != nil { + return err + } + if hadBastion { + record.Event(clusterScope.StackitCluster, "BastionDeleted", "Deleted bastion") + } + return nil } func deleteBastionResources( diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index 902ca68..c2be065 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -285,7 +285,7 @@ var _ = Describe("StackitCluster Controller", func() { got.Status.Bastion = infrav1.StackitBastionStatus{} Expect(k8sClient.Status().Update(ctx, got)).To(Succeed()) Expect(k8sClient.Get(ctx, key, got)).To(Succeed()) - Expect(hasBastionStatus(got.Status.Bastion)).To(BeFalse()) + Expect(got.Status.Bastion).To(Equal(infrav1.StackitBastionStatus{})) Expect(got.Status.APIServerLoadBalancerID).To(BeEmpty()) By("deleting the cluster") diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index 368b80d..10fc660 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -335,16 +335,12 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS "Deleted API server load balancer %s", loadBalancerID, ) } - hadBastion := hasBastionStatus(stackitCluster.Status.Bastion) - if err := deleteBastionResources(ctx, clusterScope); err != nil { + if err := deleteBastion(ctx, clusterScope); err != nil { log.Error(err, "Failed to delete bastion", "cluster", stackitCluster.Name) record.Warnf(stackitCluster, "FailedDeleteBastion", "Failed to delete bastion: %v", err) return ctrl.Result{}, err } - if hadBastion { - record.Event(stackitCluster, "BastionDeleted", "Deleted bastion") - } controllerutil.RemoveFinalizer(stackitCluster, infrav1.ClusterFinalizer) return ctrl.Result{}, nil } diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index 94ab212..0376505 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -170,6 +170,11 @@ func (s *ClusterScope) setBastionSecurityGroupID(id string) { s.StackitCluster.Status.Bastion.SecurityGroupID = id } +func (s *ClusterScope) HasBastionStatus() bool { + status := s.StackitCluster.Status.Bastion + return status.ServerID != "" || status.PublicIPID != "" || status.PublicIP != "" || status.SecurityGroupID != "" || status.CloudInitHash != "" +} + func (s *ClusterScope) SetFailureDomains(failureDomains []clusterv1.FailureDomain) { s.StackitCluster.Status.FailureDomains = failureDomains } diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index 442c245..723689c 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -208,3 +208,17 @@ func TestBootstrapTargetIP(t *testing.T) { t.Fatalf("BootstrapTargetIP() with invalid prefix = %q, want %q", got, loadbalancer.DefaultBootstrapTargetIP) } } + +func TestHasBastionStatus(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{}, + } + if clusterScope.HasBastionStatus() { + t.Fatal("HasBastionStatus() = true on empty status, want false") + } + + clusterScope.StackitCluster.Status.Bastion.ServerID = "srv-1" + if !clusterScope.HasBastionStatus() { + t.Fatal("HasBastionStatus() = false when ServerID is set, want true") + } +} From 21f94e0474fb4eba2085cbbced26fdfea970ecc5 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 16:05:39 +0100 Subject: [PATCH 36/60] fix: only delete bastion server when recreating on cloud-init change --- cloud/fake/client.go | 16 +++++++++ controller/stackitcluster_bastion.go | 23 +++++-------- controller/stackitcluster_controller_test.go | 35 +++++++++++++++++--- 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/cloud/fake/client.go b/cloud/fake/client.go index 646a209..2046ad3 100644 --- a/cloud/fake/client.go +++ b/cloud/fake/client.go @@ -345,6 +345,17 @@ func (c *Client) PublicIPCount() int { return len(c.publicIPs) } +// PublicIPAttachedToServer reports whether the public IP is attached to the server (test helper). +func (c *Client) PublicIPAttachedToServer(publicIPID, serverID string) bool { + c.mu.Lock() + defer c.mu.Unlock() + entry, ok := c.publicIPs[publicIPID] + if !ok { + return false + } + return entry.publicIP.NetworkInterfaceID == "fake-nic-"+serverID +} + // SecurityGroupCount returns the number of currently tracked security groups. func (c *Client) SecurityGroupCount() int { c.mu.Lock() @@ -1146,6 +1157,11 @@ func (c *Client) deleteServerHTTP(id string) error { if _, ok := c.servers[id]; !ok { return fmt.Errorf("server %q: %w", id, shared.ErrNotFound) } + for _, entry := range c.publicIPs { + if entry.publicIP.NetworkInterfaceID == "fake-nic-"+id { + entry.publicIP.NetworkInterfaceID = "" + } + } delete(c.servers, id) return nil } diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index b08b359..2368724 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -91,7 +91,7 @@ func (r *StackitClusterReconciler) reconcileBastion( clusterScope.SetBastionCloudInit(cloudInit, bastionCloudInitHash(cloudInit)) if bastionNeedsRecreate(stackitCluster, cloudInit) { - if err := deleteBastionResources(ctx, clusterScope); err != nil { + if err := server.New(clusterScope).Delete(ctx); err != nil { return ctrl.Result{}, false, err } clusterScope.SetNotReady( @@ -178,26 +178,19 @@ func deleteBastion( clusterScope *scope.ClusterScope, ) error { hadBastion := clusterScope.HasBastionStatus() - if err := deleteBastionResources(ctx, clusterScope); err != nil { - return err - } - if hadBastion { - record.Event(clusterScope.StackitCluster, "BastionDeleted", "Deleted bastion") - } - return nil -} - -func deleteBastionResources( - ctx context.Context, - clusterScope *scope.ClusterScope, -) error { if err := publicip.New(clusterScope).Delete(ctx); err != nil { return err } if err := securitygroup.New(clusterScope).Delete(ctx); err != nil { return err } - return server.New(clusterScope).Delete(ctx) + if err := server.New(clusterScope).Delete(ctx); err != nil { + return err + } + if hadBastion { + record.Event(clusterScope.StackitCluster, "BastionDeleted", "Deleted bastion") + } + return nil } func bastionNeedsRecreate(stackitCluster *infrav1.StackitCluster, cloudInit []byte) bool { diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index c2be065..09d938c 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -464,7 +464,7 @@ var _ = Describe("StackitCluster Controller", func() { expectCondition(got.Status.Conditions, infrav1.ClusterBastionReadyCondition, metav1.ConditionFalse, "CloudInitRefError") }) - It("recreates the bastion and node SSH access when referenced cloud-init changes", func() { + It("recreates the bastion without tearing down security groups or public IP when referenced cloud-init changes", func() { got := &infrav1.StackitCluster{} Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) cloudInitName := "bastion-cloud-init-" + clusterName @@ -483,8 +483,29 @@ var _ = Describe("StackitCluster Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) oldServerID := got.Status.Bastion.ServerID + oldBastionSGID := got.Status.Bastion.SecurityGroupID + oldPublicIPID := got.Status.Bastion.PublicIPID Expect(oldServerID).NotTo(BeEmpty()) + Expect(oldBastionSGID).NotTo(BeEmpty()) + Expect(oldPublicIPID).NotTo(BeEmpty()) Expect(fakeCloud.SecurityGroupCount()).To(Equal(2)) + nodeSG := fakeCloud.FindSecurityGroupByTags(scope.NodeSSHTags(got)) + Expect(nodeSG).NotTo(BeNil()) + + stackitMachine := newStackitMachine(got.Name+"-worker-0", got.Namespace, "worker-0") + stackitMachine.Spec.SecurityGroups = []string{nodeSG.ID} + machineScope, err := scope.NewMachineScope( + k8sClient, + &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{Name: got.Name, Namespace: got.Namespace}}, + &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: "worker-0", UID: "uid-1"}}, + got, + stackitMachine, + ) + Expect(err).NotTo(HaveOccurred()) + machineScope.SetCloudClient(fakeCloud) + Expect(server.New(machineScope).Reconcile(ctx)).To(Succeed()) + workerServerID := stackitMachine.Status.InstanceID + Expect(fakeCloud.ServerHasSecurityGroup(workerServerID, nodeSG.ID)).To(BeTrue()) configMap := &corev1.ConfigMap{} Expect(k8sClient.Get(ctx, types.NamespacedName{Name: cloudInitName, Namespace: namespace}, configMap)).To(Succeed()) @@ -494,9 +515,10 @@ var _ = Describe("StackitCluster Controller", func() { result, err = reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) Expect(result.RequeueAfter).To(BeNumerically(">", 0)) - Expect(fakeCloud.ServerCount()).To(Equal(0)) - Expect(fakeCloud.PublicIPCount()).To(Equal(0)) - Expect(fakeCloud.SecurityGroupCount()).To(Equal(0)) + Expect(fakeCloud.ServerCount()).To(Equal(1)) + Expect(fakeCloud.PublicIPCount()).To(Equal(1)) + Expect(fakeCloud.SecurityGroupCount()).To(Equal(2)) + Expect(fakeCloud.ServerHasSecurityGroup(workerServerID, nodeSG.ID)).To(BeTrue()) result, err = reconciler.Reconcile(ctx, request) Expect(err).NotTo(HaveOccurred()) @@ -505,6 +527,11 @@ var _ = Describe("StackitCluster Controller", func() { Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) Expect(got.Status.Bastion.ServerID).NotTo(BeEmpty()) Expect(got.Status.Bastion.ServerID).NotTo(Equal(oldServerID)) + Expect(got.Status.Bastion.SecurityGroupID).To(Equal(oldBastionSGID)) + Expect(got.Status.Bastion.PublicIPID).To(Equal(oldPublicIPID)) + Expect(fakeCloud.ServerHasSecurityGroup(workerServerID, nodeSG.ID)).To(BeTrue()) + Expect(fakeCloud.ServerHasSecurityGroup(got.Status.Bastion.ServerID, oldBastionSGID)).To(BeTrue()) + Expect(fakeCloud.PublicIPAttachedToServer(oldPublicIPID, got.Status.Bastion.ServerID)).To(BeTrue()) Expect(got.Status.Bastion.CloudInitHash).To(Equal(bastionCloudInitHash([]byte(configMap.Data["userData"])))) Expect(string(fakeCloud.ServerUserData(got.Status.Bastion.ServerID))).To(Equal(configMap.Data["userData"])) }) From 92acb6318ac9d7507b291b4cc9fda7297110fab5 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Fri, 13 Nov 2026 16:43:20 +0100 Subject: [PATCH 37/60] refactor: use shared.DeleteByIDOrTags in publicip.Delete --- cloud/services/publicip/reconcile.go | 90 ++++++++++------------- cloud/services/publicip/reconcile_test.go | 14 ++-- 2 files changed, 45 insertions(+), 59 deletions(-) diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index f15befe..a9629b2 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -12,7 +12,6 @@ package publicip import ( "context" - "errors" "fmt" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" @@ -57,43 +56,19 @@ func (s *Service) Delete(ctx context.Context) error { return fmt.Errorf("%w: public IP ID or tags are required", shared.ErrInvalidInput) } - publicIPs, err := s.client.resolvePublicIPsForDelete(ctx, spec) + serverIDs, err := s.client.resolveServerIDs(ctx, spec) if err != nil { return err } - if len(publicIPs) == 0 { - if spec.SetPublicIP != nil { - spec.SetPublicIP("", "") - } - return nil + deletedIDs, err := shared.DeleteByIDOrTags(ctx, spec.ID, spec.Tags, func(ctx context.Context, id string) (bool, error) { + return s.client.detachAndDeleteByID(ctx, id, serverIDs) + }, s.client.listIDsByTags) + for _, deletedID := range deletedIDs { + log.V(1).Info("Deleted public IP", "publicIPID", deletedID) } - - serverIDs, err := s.client.resolveServerIDs(ctx, spec) if err != nil { return err } - var errs []error - for _, publicIP := range publicIPs { - var detachFailed bool - for _, serverID := range serverIDs { - if err := s.client.detachFromServer(ctx, serverID, publicIP.ID); err != nil { - errs = append(errs, err) - detachFailed = true - break - } - } - if detachFailed { - continue - } - if err := s.client.deleteByID(ctx, publicIP.ID); err != nil { - errs = append(errs, err) - continue - } - log.V(1).Info("Deleted public IP", "publicIPID", publicIP.ID) - } - if len(errs) > 0 { - return errors.Join(errs...) - } if spec.SetPublicIP != nil { spec.SetPublicIP("", "") @@ -173,18 +148,43 @@ func (c *client) detachFromServer(ctx context.Context, serverID, publicIPID stri return nil } -// deleteByID deletes a public IP by ID, ignoring NotFound errors. -func (c *client) deleteByID(ctx context.Context, id string) error { +// deleteByID deletes a public IP by ID, returning true if the public IP existed and was deleted, +// or false if it was already absent. +func (c *client) deleteByID(ctx context.Context, id string) (bool, error) { if id == "" { - return fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) + return false, fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) } if err := c.scope.IaaSClient().DefaultAPI.DeletePublicIP(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute(); err != nil { err := shared.ClassifySDKError("delete public IP", err) - if !shared.IsNotFound(err) { - return err + if shared.IsNotFound(err) { + return false, nil } + return false, err } - return nil + return true, nil +} + +func (c *client) detachAndDeleteByID(ctx context.Context, id string, serverIDs []string) (bool, error) { + for _, serverID := range serverIDs { + if err := c.detachFromServer(ctx, serverID, id); err != nil { + return false, err + } + } + return c.deleteByID(ctx, id) +} + +func (c *client) listIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { + publicIPs, err := c.listByTags(ctx, tags) + if err != nil { + return nil, err + } + ids := make([]string, 0, len(publicIPs)) + for _, publicIP := range publicIPs { + if publicIP.ID != "" { + ids = append(ids, publicIP.ID) + } + } + return ids, nil } func (c *client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*PublicIP, error) { @@ -212,22 +212,6 @@ func (c *client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*Public return out, nil } -func (c *client) resolvePublicIPsForDelete(ctx context.Context, spec PublicIPSpec) ([]*PublicIP, error) { - if spec.ID != "" { - publicIP, err := c.get(ctx, spec.ID) - if err == nil { - return []*PublicIP{publicIP}, nil - } - if !shared.IsNotFound(err) { - return nil, err - } - } - if len(spec.Tags) == 0 { - return nil, nil - } - return c.listByTags(ctx, spec.Tags) -} - func (c *client) resolveServerIDs(ctx context.Context, spec PublicIPSpec) ([]string, error) { var ids []string if spec.ServerID != "" { diff --git a/cloud/services/publicip/reconcile_test.go b/cloud/services/publicip/reconcile_test.go index 967656c..e4eb806 100644 --- a/cloud/services/publicip/reconcile_test.go +++ b/cloud/services/publicip/reconcile_test.go @@ -31,6 +31,7 @@ const ( testRegion = "eu01" testPublicIPID = "11111111-1111-4111-8111-111111111111" testOtherPublicIPID = "22222222-2222-4222-8222-222222222222" + testMissingPublicIP = "99999999-9999-4999-8999-999999999999" testServerID = "33333333-3333-4333-8333-333333333333" testNetworkInterface = "[VISA_REDACTED]-8444-444444444444" testPublicIPAddress = "203.0.113.22" @@ -309,12 +310,6 @@ func TestServiceDelete(t *testing.T) { svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path switch { - case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips/"+testPublicIPID): - writeJSON(t, w, map[string]any{ - "id": testPublicIPID, - "ip": testPublicIPAddress, - "networkInterface": testNetworkInterface, - }) case r.Method == http.MethodDelete && strings.HasSuffix(path, "/servers/"+testServerID+"/public-ips/"+testPublicIPID): detachedPair = testServerID + ":" + testPublicIPID w.WriteHeader(http.StatusNoContent) @@ -371,6 +366,7 @@ func TestServiceDeleteTagFallback(t *testing.T) { deletedIDs []string ) spec := publicip.PublicIPSpec{ + ID: testMissingPublicIP, Tags: map[string]string{"cluster": "test"}, ServerSelector: map[string]string{"cluster": "test"}, } @@ -378,6 +374,12 @@ func TestServiceDeleteTagFallback(t *testing.T) { svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path switch { + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/servers/"+testServerID+"/public-ips/"+testMissingPublicIP): + w.WriteHeader(http.StatusNotFound) + writeJSON(t, w, map[string]any{"message": "not found"}) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/public-ips/"+testMissingPublicIP): + w.WriteHeader(http.StatusNotFound) + writeJSON(t, w, map[string]any{"message": "not found"}) case r.Method == http.MethodGet && strings.HasSuffix(path, "/public-ips"): writeJSON(t, w, map[string]any{"items": []any{ map[string]any{ From 90caba1c2aa3b68525cf94c0c35294f70b744cd8 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Mon, 16 Nov 2026 11:58:46 +0100 Subject: [PATCH 38/60] refactor: remove redundant client struct across cloud services --- cloud/services/availabilityzone/reconcile.go | 8 +- cloud/services/availabilityzone/service.go | 16 +--- cloud/services/loadbalancer/reconcile.go | 34 ++++---- cloud/services/loadbalancer/service.go | 16 +--- cloud/services/loadbalancer/targets.go | 12 +-- cloud/services/network/reconcile.go | 6 +- cloud/services/network/service.go | 16 +--- cloud/services/publicip/reconcile.go | 62 +++++++------- cloud/services/publicip/service.go | 16 +--- cloud/services/securitygroup/reconcile.go | 86 ++++++++++---------- cloud/services/securitygroup/rules.go | 28 +++---- cloud/services/securitygroup/service.go | 16 +--- cloud/services/server/reconcile.go | 38 ++++----- cloud/services/server/service.go | 16 +--- 14 files changed, 149 insertions(+), 221 deletions(-) diff --git a/cloud/services/availabilityzone/reconcile.go b/cloud/services/availabilityzone/reconcile.go index b4b3ac3..d14b7d7 100644 --- a/cloud/services/availabilityzone/reconcile.go +++ b/cloud/services/availabilityzone/reconcile.go @@ -21,7 +21,7 @@ import ( func (s *Service) Reconcile(ctx context.Context) error { log := logf.FromContext(ctx) - zones, err := s.client.list(ctx) + zones, err := s.list(ctx) if err != nil { return err } @@ -34,11 +34,11 @@ func (s *Service) Reconcile(ctx context.Context) error { } // list retrieves the availability zones for the configured region. -func (c *client) list(ctx context.Context) ([]string, error) { - if c.scope.Region() == "" { +func (s *Service) list(ctx context.Context) ([]string, error) { + if s.scope.Region() == "" { return nil, fmt.Errorf("%w: region is empty", shared.ErrInvalidInput) } - resp, err := c.scope.IaaSClient().DefaultAPI.ListAvailabilityZones(ctx, c.scope.Region()).Execute() + resp, err := s.scope.IaaSClient().DefaultAPI.ListAvailabilityZones(ctx, s.scope.Region()).Execute() if err != nil { return nil, shared.ClassifySDKError("list availability zones", err) } diff --git a/cloud/services/availabilityzone/service.go b/cloud/services/availabilityzone/service.go index 3ff544c..f3c0c86 100644 --- a/cloud/services/availabilityzone/service.go +++ b/cloud/services/availabilityzone/service.go @@ -23,26 +23,14 @@ type Scope interface { AvailabilityZoneSpec() AvailabilityZoneSpec } -// client provides direct availability zone operations against the STACKIT IaaS API. -type client struct { - scope shared.IaaSClientScope -} - -// newClient returns a client for the given scope. -func newClient(scope shared.IaaSClientScope) *client { - return &client{scope: scope} -} - // Service implements availability zones reconciler. type Service struct { - scope Scope - client *client + scope Scope } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ - scope: scope, - client: newClient(scope), + scope: scope, } } diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index 32f169f..07aab8c 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -28,7 +28,7 @@ func (s *Service) Reconcile(ctx context.Context) error { return err } - loadBalancer, err := s.client.ensureLoadBalancer(ctx, spec) + loadBalancer, err := s.ensureLoadBalancer(ctx, spec) if err != nil { return err } @@ -45,7 +45,7 @@ func (s *Service) Reconcile(ctx context.Context) error { // An empty set never overwrites a populated pool: cluster deletion filters // out every machine while the API server is still needed for drain. if len(spec.Targets) > 0 { - if err := s.client.setTargets(ctx, loadBalancer.ID, spec.Port, spec.Targets); err != nil { + if err := s.setTargets(ctx, loadBalancer.ID, spec.Port, spec.Targets); err != nil { return fmt.Errorf("%w: %w", errTargetPoolUpdate, err) } log.V(1).Info("Reconciled API server load balancer target pool", "targets", len(spec.Targets)) @@ -61,7 +61,7 @@ func (s *Service) Delete(ctx context.Context) error { return fmt.Errorf("%w: load balancer ID or tags are required", shared.ErrInvalidInput) } - deletedIDs, err := shared.DeleteByIDOrTags(ctx, spec.ID, spec.Tags, s.client.deleteByID, s.client.listIDsByTags) + deletedIDs, err := shared.DeleteByIDOrTags(ctx, spec.ID, spec.Tags, s.deleteByID, s.listIDsByTags) for _, deletedID := range deletedIDs { log.V(1).Info("Deleted load balancer", "loadBalancerID", deletedID) } @@ -75,11 +75,11 @@ func (s *Service) Delete(ctx context.Context) error { } // get retrieves a STACKIT load balancer by ID. -func (c *client) get(ctx context.Context, id string) (*LoadBalancer, error) { +func (s *Service) get(ctx context.Context, id string) (*LoadBalancer, error) { if id == "" { return nil, fmt.Errorf("%w: load balancer ID is empty", shared.ErrInvalidInput) } - loadBalancer, err := c.scope.LoadBalancerClient().DefaultAPI.GetLoadBalancer(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute() + loadBalancer, err := s.scope.LoadBalancerClient().DefaultAPI.GetLoadBalancer(ctx, s.scope.ProjectID(), s.scope.Region(), id).Execute() if err != nil { return nil, shared.ClassifySDKError("get load balancer", err) } @@ -87,16 +87,16 @@ func (c *client) get(ctx context.Context, id string) (*LoadBalancer, error) { } // findByTags finds a single STACKIT load balancer matching the given tag selector. -func (c *client) findByTags(ctx context.Context, tags map[string]string) (*LoadBalancer, error) { - return shared.FindByTags(ctx, tags, c.listByTags, "load balancer", "load balancers") +func (s *Service) findByTags(ctx context.Context, tags map[string]string) (*LoadBalancer, error) { + return shared.FindByTags(ctx, tags, s.listByTags, "load balancer", "load balancers") } // listByTags lists STACKIT load balancers matching the given tag selector. -func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*LoadBalancer, error) { +func (s *Service) listByTags(ctx context.Context, tags map[string]string) ([]*LoadBalancer, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } - resp, err := c.scope.LoadBalancerClient().DefaultAPI.ListLoadBalancers(ctx, c.scope.ProjectID(), c.scope.Region()).Execute() + resp, err := s.scope.LoadBalancerClient().DefaultAPI.ListLoadBalancers(ctx, s.scope.ProjectID(), s.scope.Region()).Execute() if err != nil { err := shared.ClassifySDKError("list load balancers", err) if isLoadBalancerServiceNotEnabled(err) { @@ -117,11 +117,11 @@ func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*Loa // deleteByID deletes a load balancer by ID, returning true if the load balancer existed and was deleted, // or false if it was already absent. -func (c *client) deleteByID(ctx context.Context, id string) (bool, error) { +func (s *Service) deleteByID(ctx context.Context, id string) (bool, error) { if id == "" { return false, fmt.Errorf("%w: load balancer ID is empty", shared.ErrInvalidInput) } - if _, err := c.scope.LoadBalancerClient().DefaultAPI.DeleteLoadBalancer(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute(); err != nil { + if _, err := s.scope.LoadBalancerClient().DefaultAPI.DeleteLoadBalancer(ctx, s.scope.ProjectID(), s.scope.Region(), id).Execute(); err != nil { err := shared.ClassifySDKError("delete load balancer", err) if shared.IsNotFound(err) { return false, nil @@ -131,8 +131,8 @@ func (c *client) deleteByID(ctx context.Context, id string) (bool, error) { return true, nil } -func (c *client) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) (*LoadBalancer, error) { - loadBalancer, err := shared.ResolveExisting(ctx, spec.ID, spec.Tags, c.get, c.findByTags, isIgnorableLookupError) +func (s *Service) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) (*LoadBalancer, error) { + loadBalancer, err := shared.ResolveExisting(ctx, spec.ID, spec.Tags, s.get, s.findByTags, isIgnorableLookupError) if err != nil { return nil, err } @@ -140,8 +140,8 @@ func (c *client) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) return loadBalancer, nil } - payload := spec.toCreatePayload(c.scope.Region()) - lbResp, err := c.scope.LoadBalancerClient().DefaultAPI.CreateLoadBalancer(ctx, c.scope.ProjectID(), c.scope.Region()). + payload := spec.toCreatePayload(s.scope.Region()) + lbResp, err := s.scope.LoadBalancerClient().DefaultAPI.CreateLoadBalancer(ctx, s.scope.ProjectID(), s.scope.Region()). CreateLoadBalancerPayload(payload). Execute() if err != nil { @@ -152,8 +152,8 @@ func (c *client) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) return out, nil } -func (c *client) listIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { - loadBalancers, err := c.listByTags(ctx, tags) +func (s *Service) listIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { + loadBalancers, err := s.listByTags(ctx, tags) if err != nil { return nil, err } diff --git a/cloud/services/loadbalancer/service.go b/cloud/services/loadbalancer/service.go index c0bcf08..f8a3795 100644 --- a/cloud/services/loadbalancer/service.go +++ b/cloud/services/loadbalancer/service.go @@ -72,26 +72,14 @@ type Scope interface { LoadBalancerSpec() LoadBalancerSpec } -// client provides direct load balancer operations against the STACKIT Load Balancer API. -type client struct { - scope shared.LoadBalancerClientScope -} - -// newClient returns a client for the given scope. -func newClient(scope shared.LoadBalancerClientScope) *client { - return &client{scope: scope} -} - // Service implements load balancer reconciler. type Service struct { - scope Scope - client *client + scope Scope } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ - scope: scope, - client: newClient(scope), + scope: scope, } } diff --git a/cloud/services/loadbalancer/targets.go b/cloud/services/loadbalancer/targets.go index 4c71c6a..5706f26 100644 --- a/cloud/services/loadbalancer/targets.go +++ b/cloud/services/loadbalancer/targets.go @@ -113,7 +113,7 @@ func firstInternalIP(addresses []clusterv1.MachineAddress) string { } // setTargets replaces the contents of the API-server target pool. STACKIT rejects an empty pool. -func (c *client) setTargets( +func (s *Service) setTargets( ctx context.Context, loadBalancerID string, port int32, @@ -137,7 +137,7 @@ func (c *client) setTargets( desired = append(desired, *target) } - loadBalancer, err := c.scope.LoadBalancerClient().DefaultAPI.GetLoadBalancer(ctx, c.scope.ProjectID(), c.scope.Region(), loadBalancerID).Execute() + loadBalancer, err := s.scope.LoadBalancerClient().DefaultAPI.GetLoadBalancer(ctx, s.scope.ProjectID(), s.scope.Region(), loadBalancerID).Execute() if err != nil { return shared.ClassifySDKError("get load balancer", err) } @@ -156,10 +156,10 @@ func (c *client) setTargets( if targetPool.GetTargetPort() == port && sameTargets(targetPool.GetTargets(), desired) { return nil } - return c.updateAPIServerTargetPool(ctx, loadBalancerID, targetPool, desired, port) + return s.updateAPIServerTargetPool(ctx, loadBalancerID, targetPool, desired, port) } -func (c *client) updateAPIServerTargetPool( +func (s *Service) updateAPIServerTargetPool( ctx context.Context, loadBalancerID string, current *lb.TargetPool, @@ -174,8 +174,8 @@ func (c *client) updateAPIServerTargetPool( payload.SetTargetPort(current.GetTargetPort()) } payload.SetTargets(targets) - if _, err := c.scope.LoadBalancerClient().DefaultAPI. - UpdateTargetPool(ctx, c.scope.ProjectID(), c.scope.Region(), loadBalancerID, apiserverTargetPoolName). + if _, err := s.scope.LoadBalancerClient().DefaultAPI. + UpdateTargetPool(ctx, s.scope.ProjectID(), s.scope.Region(), loadBalancerID, apiserverTargetPoolName). UpdateTargetPoolPayload(*payload). Execute(); err != nil { return shared.ClassifySDKError("update load balancer target pool", err) diff --git a/cloud/services/network/reconcile.go b/cloud/services/network/reconcile.go index 20bbe73..eb262d2 100644 --- a/cloud/services/network/reconcile.go +++ b/cloud/services/network/reconcile.go @@ -26,7 +26,7 @@ func (s *Service) Reconcile(ctx context.Context) error { return err } - net, err := s.client.get(ctx, spec.ID) + net, err := s.get(ctx, spec.ID) if err != nil { return err } @@ -38,11 +38,11 @@ func (s *Service) Reconcile(ctx context.Context) error { } // get retrieves a STACKIT network by ID. -func (c *client) get(ctx context.Context, id string) (*Network, error) { +func (s *Service) get(ctx context.Context, id string) (*Network, error) { if id == "" { return nil, fmt.Errorf("%w: network ID is empty", shared.ErrInvalidInput) } - net, err := c.scope.IaaSClient().DefaultAPI.GetNetwork(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute() + net, err := s.scope.IaaSClient().DefaultAPI.GetNetwork(ctx, s.scope.ProjectID(), s.scope.Region(), id).Execute() if err != nil { return nil, shared.ClassifySDKError("get network", err) } diff --git a/cloud/services/network/service.go b/cloud/services/network/service.go index 7b9eb09..23aa3ad 100644 --- a/cloud/services/network/service.go +++ b/cloud/services/network/service.go @@ -31,26 +31,14 @@ type Scope interface { NetworkSpec() NetworkSpec } -// client provides direct network operations against the STACKIT IaaS API. -type client struct { - scope shared.IaaSClientScope -} - -// newClient returns a client for the given scope. -func newClient(scope shared.IaaSClientScope) *client { - return &client{scope: scope} -} - // Service implements network reconciler. type Service struct { - scope Scope - client *client + scope Scope } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ - scope: scope, - client: newClient(scope), + scope: scope, } } diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index a9629b2..256f5e8 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -27,16 +27,16 @@ func (s *Service) Reconcile(ctx context.Context) error { return err } - publicIP, err := s.client.ensurePublicIP(ctx, spec) + publicIP, err := s.ensurePublicIP(ctx, spec) if err != nil { return err } if spec.ServerID != "" && publicIP.NetworkInterfaceID == "" { - if err := s.client.attachToServer(ctx, spec.ServerID, publicIP.ID); err != nil { + if err := s.attachToServer(ctx, spec.ServerID, publicIP.ID); err != nil { return err } - publicIP, err = s.client.get(ctx, publicIP.ID) + publicIP, err = s.get(ctx, publicIP.ID) if err != nil { return err } @@ -56,13 +56,13 @@ func (s *Service) Delete(ctx context.Context) error { return fmt.Errorf("%w: public IP ID or tags are required", shared.ErrInvalidInput) } - serverIDs, err := s.client.resolveServerIDs(ctx, spec) + serverIDs, err := s.resolveServerIDs(ctx, spec) if err != nil { return err } deletedIDs, err := shared.DeleteByIDOrTags(ctx, spec.ID, spec.Tags, func(ctx context.Context, id string) (bool, error) { - return s.client.detachAndDeleteByID(ctx, id, serverIDs) - }, s.client.listIDsByTags) + return s.detachAndDeleteByID(ctx, id, serverIDs) + }, s.listIDsByTags) for _, deletedID := range deletedIDs { log.V(1).Info("Deleted public IP", "publicIPID", deletedID) } @@ -77,11 +77,11 @@ func (s *Service) Delete(ctx context.Context) error { } // get retrieves a STACKIT public IP by ID. -func (c *client) get(ctx context.Context, id string) (*PublicIP, error) { +func (s *Service) get(ctx context.Context, id string) (*PublicIP, error) { if id == "" { return nil, fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) } - publicIP, err := c.scope.IaaSClient().DefaultAPI.GetPublicIP(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute() + publicIP, err := s.scope.IaaSClient().DefaultAPI.GetPublicIP(ctx, s.scope.ProjectID(), s.scope.Region(), id).Execute() if err != nil { return nil, shared.ClassifySDKError("get public IP", err) } @@ -89,16 +89,16 @@ func (c *client) get(ctx context.Context, id string) (*PublicIP, error) { } // findByTags finds a single STACKIT public IP matching the given tag selector. -func (c *client) findByTags(ctx context.Context, tags map[string]string) (*PublicIP, error) { - return shared.FindByTags(ctx, tags, c.listByTags, "public IP", "public IPs") +func (s *Service) findByTags(ctx context.Context, tags map[string]string) (*PublicIP, error) { + return shared.FindByTags(ctx, tags, s.listByTags, "public IP", "public IPs") } // listByTags lists STACKIT public IPs matching the given tag selector. -func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*PublicIP, error) { +func (s *Service) listByTags(ctx context.Context, tags map[string]string) ([]*PublicIP, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } - resp, err := c.scope.IaaSClient().DefaultAPI.ListPublicIPs(ctx, c.scope.ProjectID(), c.scope.Region()). + resp, err := s.scope.IaaSClient().DefaultAPI.ListPublicIPs(ctx, s.scope.ProjectID(), s.scope.Region()). LabelSelector(shared.LabelSelector(tags)). Execute() if err != nil { @@ -115,12 +115,12 @@ func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*Pub } // attachToServer attaches a public IP to a server idempotently, tolerating Conflict errors. -func (c *client) attachToServer(ctx context.Context, serverID, publicIPID string) error { +func (s *Service) attachToServer(ctx context.Context, serverID, publicIPID string) error { if serverID == "" || publicIPID == "" { return fmt.Errorf("%w: server ID and public IP ID are required", shared.ErrInvalidInput) } - if err := c.scope.IaaSClient().DefaultAPI. - AddPublicIpToServer(ctx, c.scope.ProjectID(), c.scope.Region(), serverID, publicIPID). + if err := s.scope.IaaSClient().DefaultAPI. + AddPublicIpToServer(ctx, s.scope.ProjectID(), s.scope.Region(), serverID, publicIPID). Execute(); err != nil { err := shared.ClassifySDKError("add public IP to server", err) if !shared.IsConflict(err) { @@ -132,12 +132,12 @@ func (c *client) attachToServer(ctx context.Context, serverID, publicIPID string } // detachFromServer detaches a public IP from a server, ignoring NotFound and Conflict errors. -func (c *client) detachFromServer(ctx context.Context, serverID, publicIPID string) error { +func (s *Service) detachFromServer(ctx context.Context, serverID, publicIPID string) error { if serverID == "" || publicIPID == "" { return fmt.Errorf("%w: server ID and public IP ID are required", shared.ErrInvalidInput) } - if err := c.scope.IaaSClient().DefaultAPI. - RemovePublicIpFromServer(ctx, c.scope.ProjectID(), c.scope.Region(), serverID, publicIPID). + if err := s.scope.IaaSClient().DefaultAPI. + RemovePublicIpFromServer(ctx, s.scope.ProjectID(), s.scope.Region(), serverID, publicIPID). Execute(); err != nil { err := shared.ClassifySDKError("remove public IP from server", err) if !shared.IsNotFound(err) && !shared.IsConflict(err) { @@ -150,11 +150,11 @@ func (c *client) detachFromServer(ctx context.Context, serverID, publicIPID stri // deleteByID deletes a public IP by ID, returning true if the public IP existed and was deleted, // or false if it was already absent. -func (c *client) deleteByID(ctx context.Context, id string) (bool, error) { +func (s *Service) deleteByID(ctx context.Context, id string) (bool, error) { if id == "" { return false, fmt.Errorf("%w: public IP ID is empty", shared.ErrInvalidInput) } - if err := c.scope.IaaSClient().DefaultAPI.DeletePublicIP(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute(); err != nil { + if err := s.scope.IaaSClient().DefaultAPI.DeletePublicIP(ctx, s.scope.ProjectID(), s.scope.Region(), id).Execute(); err != nil { err := shared.ClassifySDKError("delete public IP", err) if shared.IsNotFound(err) { return false, nil @@ -164,17 +164,17 @@ func (c *client) deleteByID(ctx context.Context, id string) (bool, error) { return true, nil } -func (c *client) detachAndDeleteByID(ctx context.Context, id string, serverIDs []string) (bool, error) { +func (s *Service) detachAndDeleteByID(ctx context.Context, id string, serverIDs []string) (bool, error) { for _, serverID := range serverIDs { - if err := c.detachFromServer(ctx, serverID, id); err != nil { + if err := s.detachFromServer(ctx, serverID, id); err != nil { return false, err } } - return c.deleteByID(ctx, id) + return s.deleteByID(ctx, id) } -func (c *client) listIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { - publicIPs, err := c.listByTags(ctx, tags) +func (s *Service) listIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { + publicIPs, err := s.listByTags(ctx, tags) if err != nil { return nil, err } @@ -187,8 +187,8 @@ func (c *client) listIDsByTags(ctx context.Context, tags map[string]string) ([]s return ids, nil } -func (c *client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*PublicIP, error) { - publicIP, err := shared.ResolveExisting(ctx, spec.ID, spec.Tags, c.get, c.findByTags, shared.IsNotFound) +func (s *Service) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*PublicIP, error) { + publicIP, err := shared.ResolveExisting(ctx, spec.ID, spec.Tags, s.get, s.findByTags, shared.IsNotFound) if err != nil { return nil, err } @@ -200,8 +200,8 @@ func (c *client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*Public if len(spec.Tags) > 0 { payload.SetLabels(shared.TagsToSDKLabels(spec.Tags)) } - created, err := c.scope.IaaSClient().DefaultAPI. - CreatePublicIP(ctx, c.scope.ProjectID(), c.scope.Region()). + created, err := s.scope.IaaSClient().DefaultAPI. + CreatePublicIP(ctx, s.scope.ProjectID(), s.scope.Region()). CreatePublicIPPayload(*payload). Execute() if err != nil { @@ -212,7 +212,7 @@ func (c *client) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*Public return out, nil } -func (c *client) resolveServerIDs(ctx context.Context, spec PublicIPSpec) ([]string, error) { +func (s *Service) resolveServerIDs(ctx context.Context, spec PublicIPSpec) ([]string, error) { var ids []string if spec.ServerID != "" { ids = append(ids, spec.ServerID) @@ -220,7 +220,7 @@ func (c *client) resolveServerIDs(ctx context.Context, spec PublicIPSpec) ([]str if len(spec.ServerSelector) == 0 { return ids, nil } - selectedIDs, err := shared.ListServerIDsByTags(ctx, c.scope, spec.ServerSelector) + selectedIDs, err := shared.ListServerIDsByTags(ctx, s.scope, spec.ServerSelector) if err != nil { return nil, err } diff --git a/cloud/services/publicip/service.go b/cloud/services/publicip/service.go index f65d8b9..6e22fca 100644 --- a/cloud/services/publicip/service.go +++ b/cloud/services/publicip/service.go @@ -34,26 +34,14 @@ type Scope interface { PublicIPSpec() PublicIPSpec } -// client provides direct public IP operations against the STACKIT IaaS API. -type client struct { - scope shared.IaaSClientScope -} - -// newClient returns a client for the given scope. -func newClient(scope shared.IaaSClientScope) *client { - return &client{scope: scope} -} - // Service implements public IPs reconciler. type Service struct { - scope Scope - client *client + scope Scope } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ - scope: scope, - client: newClient(scope), + scope: scope, } } diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index f893a24..cb84673 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -25,7 +25,7 @@ import ( func (s *Service) Reconcile(ctx context.Context) error { log := logf.FromContext(ctx) for _, spec := range s.scope.SecurityGroupSpecs() { - if err := s.client.reconcileSecurityGroup(ctx, spec); err != nil { + if err := s.reconcileSecurityGroup(ctx, spec); err != nil { return err } } @@ -37,7 +37,7 @@ func (s *Service) Delete(ctx context.Context) error { log := logf.FromContext(ctx) var errs []error for _, spec := range slices.Backward(s.scope.SecurityGroupSpecs()) { - if err := s.client.deleteSecurityGroup(ctx, spec); err != nil { + if err := s.deleteSecurityGroup(ctx, spec); err != nil { errs = append(errs, err) } } @@ -49,11 +49,11 @@ func (s *Service) Delete(ctx context.Context) error { } // get retrieves a STACKIT security group by ID. -func (c *client) get(ctx context.Context, id string) (*SecurityGroup, error) { +func (s *Service) get(ctx context.Context, id string) (*SecurityGroup, error) { if id == "" { return nil, fmt.Errorf("%w: security group ID is empty", shared.ErrInvalidInput) } - securityGroup, err := c.scope.IaaSClient().DefaultAPI.GetSecurityGroup(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute() + securityGroup, err := s.scope.IaaSClient().DefaultAPI.GetSecurityGroup(ctx, s.scope.ProjectID(), s.scope.Region(), id).Execute() if err != nil { return nil, shared.ClassifySDKError("get security group", err) } @@ -61,16 +61,16 @@ func (c *client) get(ctx context.Context, id string) (*SecurityGroup, error) { } // findByTags finds a single STACKIT security group matching the given tag selector. -func (c *client) findByTags(ctx context.Context, tags map[string]string) (*SecurityGroup, error) { - return shared.FindByTags(ctx, tags, c.listByTags, "security group", "security groups") +func (s *Service) findByTags(ctx context.Context, tags map[string]string) (*SecurityGroup, error) { + return shared.FindByTags(ctx, tags, s.listByTags, "security group", "security groups") } // listByTags lists STACKIT security groups matching the given tag selector. -func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*SecurityGroup, error) { +func (s *Service) listByTags(ctx context.Context, tags map[string]string) ([]*SecurityGroup, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) } - resp, err := c.scope.IaaSClient().DefaultAPI.ListSecurityGroups(ctx, c.scope.ProjectID(), c.scope.Region()). + resp, err := s.scope.IaaSClient().DefaultAPI.ListSecurityGroups(ctx, s.scope.ProjectID(), s.scope.Region()). LabelSelector(shared.LabelSelector(tags)). Execute() if err != nil { @@ -91,9 +91,9 @@ func (c *client) listByTags(ctx context.Context, tags map[string]string) ([]*Sec // - conflict: already attached // - invalid input: duplicate item in security_groups list // - not found: server has no network port yet -func (c *client) attachToServer(ctx context.Context, serverID, securityGroupID string) error { - if err := c.scope.IaaSClient().DefaultAPI. - AddSecurityGroupToServer(ctx, c.scope.ProjectID(), c.scope.Region(), serverID, securityGroupID). +func (s *Service) attachToServer(ctx context.Context, serverID, securityGroupID string) error { + if err := s.scope.IaaSClient().DefaultAPI. + AddSecurityGroupToServer(ctx, s.scope.ProjectID(), s.scope.Region(), serverID, securityGroupID). Execute(); err != nil { err := shared.ClassifySDKError("add security group to server", err) if !shared.IsConflict(err) && !shared.IsInvalidInput(err) && !shared.IsNotFound(err) { @@ -105,9 +105,9 @@ func (c *client) attachToServer(ctx context.Context, serverID, securityGroupID s } // detachFromServer detaches a security group from a server, ignoring NotFound and Conflict errors. -func (c *client) detachFromServer(ctx context.Context, serverID, securityGroupID string) error { - if err := c.scope.IaaSClient().DefaultAPI. - RemoveSecurityGroupFromServer(ctx, c.scope.ProjectID(), c.scope.Region(), serverID, securityGroupID). +func (s *Service) detachFromServer(ctx context.Context, serverID, securityGroupID string) error { + if err := s.scope.IaaSClient().DefaultAPI. + RemoveSecurityGroupFromServer(ctx, s.scope.ProjectID(), s.scope.Region(), serverID, securityGroupID). Execute(); err != nil { err := shared.ClassifySDKError("remove security group from server", err) if !shared.IsNotFound(err) && !shared.IsConflict(err) { @@ -118,25 +118,25 @@ func (c *client) detachFromServer(ctx context.Context, serverID, securityGroupID return nil } -func (c *client) reconcileSecurityGroup(ctx context.Context, spec SecurityGroupSpec) error { +func (s *Service) reconcileSecurityGroup(ctx context.Context, spec SecurityGroupSpec) error { if err := spec.validate(); err != nil { return err } - securityGroup, err := c.ensureSecurityGroup(ctx, spec) + securityGroup, err := s.ensureSecurityGroup(ctx, spec) if err != nil { return err } if spec.SetID != nil { spec.SetID(securityGroup.ID) } - if err := c.ensureSecurityGroupRules(ctx, securityGroup.ID, spec.Rules); err != nil { + if err := s.ensureSecurityGroupRules(ctx, securityGroup.ID, spec.Rules); err != nil { return err } for _, serverID := range spec.ServerIDs { if serverID == "" { continue } - if err := c.attachToServer(ctx, serverID, securityGroup.ID); err != nil { + if err := s.attachToServer(ctx, serverID, securityGroup.ID); err != nil { return err } } @@ -148,12 +148,12 @@ type resolvedSecurityGroup struct { rules []iaas.SecurityGroupRule } -func (c *client) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpec) error { +func (s *Service) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpec) error { if spec.ID == "" && len(spec.Tags) == 0 { return fmt.Errorf("%w: security group ID or tags are required", shared.ErrInvalidInput) } - resolved, resolveErr := c.resolveSecurityGroupsForDelete(ctx, spec) + resolved, resolveErr := s.resolveSecurityGroupsForDelete(ctx, spec) if resolveErr != nil && len(resolved) == 0 { return resolveErr } @@ -162,15 +162,15 @@ func (c *client) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpec errs = append(errs, resolveErr) } for _, sg := range resolved { - if err := c.detachSecurityGroupFromServers(ctx, sg.id, spec); err != nil { + if err := s.detachSecurityGroupFromServers(ctx, sg.id, spec); err != nil { errs = append(errs, err) } - if err := c.deleteSecurityGroupRules(ctx, sg.id, sg.rules); err != nil { + if err := s.deleteSecurityGroupRules(ctx, sg.id, sg.rules); err != nil { errs = append(errs, err) } } for _, sg := range resolved { - if err := c.deleteSecurityGroupByID(ctx, sg.id); err != nil { + if err := s.deleteSecurityGroupByID(ctx, sg.id); err != nil { errs = append(errs, err) continue } @@ -185,12 +185,12 @@ func (c *client) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpec return nil } -func (c *client) resolveSecurityGroupsForDelete( +func (s *Service) resolveSecurityGroupsForDelete( ctx context.Context, spec SecurityGroupSpec, ) ([]resolvedSecurityGroup, error) { if spec.ID != "" { - rules, ok, err := c.listRulesIfGroupExists(ctx, spec.ID) + rules, ok, err := s.listRulesIfGroupExists(ctx, spec.ID) if err != nil { return nil, err } @@ -201,14 +201,14 @@ func (c *client) resolveSecurityGroupsForDelete( if len(spec.Tags) == 0 { return nil, nil } - securityGroups, err := c.listByTags(ctx, spec.Tags) + securityGroups, err := s.listByTags(ctx, spec.Tags) if err != nil { return nil, err } resolved := make([]resolvedSecurityGroup, 0, len(securityGroups)) var errs []error for _, sg := range securityGroups { - rules, ok, err := c.listRulesIfGroupExists(ctx, sg.ID) + rules, ok, err := s.listRulesIfGroupExists(ctx, sg.ID) if err != nil { errs = append(errs, err) continue @@ -220,8 +220,8 @@ func (c *client) resolveSecurityGroupsForDelete( return resolved, errors.Join(errs...) } -func (c *client) listRulesIfGroupExists(ctx context.Context, id string) ([]iaas.SecurityGroupRule, bool, error) { - rules, err := c.listSecurityGroupRules(ctx, id) +func (s *Service) listRulesIfGroupExists(ctx context.Context, id string) ([]iaas.SecurityGroupRule, bool, error) { + rules, err := s.listSecurityGroupRules(ctx, id) if shared.IsNotFound(err) { return nil, false, nil } @@ -231,15 +231,15 @@ func (c *client) listRulesIfGroupExists(ctx context.Context, id string) ([]iaas. return rules, true, nil } -func (c *client) ensureSecurityGroup(ctx context.Context, spec SecurityGroupSpec) (*SecurityGroup, error) { - securityGroup, err := shared.ResolveExisting(ctx, spec.ID, spec.Tags, c.get, c.findByTags, shared.IsNotFound) +func (s *Service) ensureSecurityGroup(ctx context.Context, spec SecurityGroupSpec) (*SecurityGroup, error) { + securityGroup, err := shared.ResolveExisting(ctx, spec.ID, spec.Tags, s.get, s.findByTags, shared.IsNotFound) if err != nil { return nil, err } if securityGroup != nil { return securityGroup, nil } - securityGroupResp, err := c.scope.IaaSClient().DefaultAPI.CreateSecurityGroup(ctx, c.scope.ProjectID(), c.scope.Region()). + securityGroupResp, err := s.scope.IaaSClient().DefaultAPI.CreateSecurityGroup(ctx, s.scope.ProjectID(), s.scope.Region()). CreateSecurityGroupPayload(spec.toCreatePayload()). Execute() if err != nil { @@ -251,23 +251,23 @@ func (c *client) ensureSecurityGroup(ctx context.Context, spec SecurityGroupSpec return out, nil } -func (c *client) deleteSecurityGroupRules( +func (s *Service) deleteSecurityGroupRules( ctx context.Context, id string, rules []iaas.SecurityGroupRule, ) error { var errs []error for _, rule := range rules { - if err := c.deleteSecurityGroupRule(ctx, id, rule.GetId()); err != nil { + if err := s.deleteSecurityGroupRule(ctx, id, rule.GetId()); err != nil { errs = append(errs, err) } } return errors.Join(errs...) } -func (c *client) deleteSecurityGroupByID(ctx context.Context, id string) error { - if err := c.scope.IaaSClient().DefaultAPI. - DeleteSecurityGroup(ctx, c.scope.ProjectID(), c.scope.Region(), id). +func (s *Service) deleteSecurityGroupByID(ctx context.Context, id string) error { + if err := s.scope.IaaSClient().DefaultAPI. + DeleteSecurityGroup(ctx, s.scope.ProjectID(), s.scope.Region(), id). Execute(); err != nil { err := shared.ClassifySDKError("delete security group", err) if !shared.IsNotFound(err) { @@ -277,31 +277,31 @@ func (c *client) deleteSecurityGroupByID(ctx context.Context, id string) error { return nil } -func (c *client) detachSecurityGroupFromServers(ctx context.Context, securityGroupID string, spec SecurityGroupSpec) error { +func (s *Service) detachSecurityGroupFromServers(ctx context.Context, securityGroupID string, spec SecurityGroupSpec) error { var errs []error - if err := c.detachServers(ctx, securityGroupID, spec.ServerIDs); err != nil { + if err := s.detachServers(ctx, securityGroupID, spec.ServerIDs); err != nil { errs = append(errs, err) } if len(spec.ServerSelector) == 0 { return errors.Join(errs...) } - serverIDs, err := shared.ListServerIDsByTags(ctx, c.scope, spec.ServerSelector) + serverIDs, err := shared.ListServerIDsByTags(ctx, s.scope, spec.ServerSelector) if err != nil { return errors.Join(append(errs, err)...) } - if err := c.detachServers(ctx, securityGroupID, serverIDs); err != nil { + if err := s.detachServers(ctx, securityGroupID, serverIDs); err != nil { errs = append(errs, err) } return errors.Join(errs...) } -func (c *client) detachServers(ctx context.Context, securityGroupID string, serverIDs []string) error { +func (s *Service) detachServers(ctx context.Context, securityGroupID string, serverIDs []string) error { var errs []error for _, serverID := range serverIDs { if serverID == "" { continue } - if err := c.detachFromServer(ctx, serverID, securityGroupID); err != nil { + if err := s.detachFromServer(ctx, serverID, securityGroupID); err != nil { errs = append(errs, err) } } diff --git a/cloud/services/securitygroup/rules.go b/cloud/services/securitygroup/rules.go index 2948cf7..3ca4d8d 100644 --- a/cloud/services/securitygroup/rules.go +++ b/cloud/services/securitygroup/rules.go @@ -21,17 +21,17 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" ) -func (c *client) ensureSecurityGroupRules(ctx context.Context, securityGroupID string, rules []Rule) error { +func (s *Service) ensureSecurityGroupRules(ctx context.Context, securityGroupID string, rules []Rule) error { if securityGroupID == "" { return fmt.Errorf("%w: security group ID is required", shared.ErrInvalidInput) } - existingRules, err := c.listSecurityGroupRules(ctx, securityGroupID) + existingRules, err := s.listSecurityGroupRules(ctx, securityGroupID) if err != nil { return err } desired := make([]Rule, 0, len(rules)) for _, rule := range rules { - rule, err := c.resolveRule(ctx, rule) + rule, err := s.resolveRule(ctx, rule) if err != nil { return err } @@ -42,7 +42,7 @@ func (c *client) ensureSecurityGroupRules(ctx context.Context, securityGroupID s if hasRule(existingRules, rule) { continue } - if err := c.createSecurityGroupRule(ctx, securityGroupID, rule); err != nil { + if err := s.createSecurityGroupRule(ctx, securityGroupID, rule); err != nil { return err } } @@ -51,17 +51,17 @@ func (c *client) ensureSecurityGroupRules(ctx context.Context, securityGroupID s if slices.ContainsFunc(desired, ruleFromSDK(existing).equal) { continue } - if err := c.deleteSecurityGroupRule(ctx, securityGroupID, existing.GetId()); err != nil { + if err := s.deleteSecurityGroupRule(ctx, securityGroupID, existing.GetId()); err != nil { return err } } return nil } -func (c *client) resolveRule(ctx context.Context, rule Rule) (Rule, error) { +func (s *Service) resolveRule(ctx context.Context, rule Rule) (Rule, error) { rule = rule.normalize() if rule.RemoteSecurityGroupID == "" && len(rule.RemoteSecurityGroupTags) > 0 { - remoteSG, err := c.findByTags(ctx, rule.RemoteSecurityGroupTags) + remoteSG, err := s.findByTags(ctx, rule.RemoteSecurityGroupTags) if err != nil { if shared.IsNotFound(err) { return Rule{}, fmt.Errorf("%w: remote security group not ready: %v", shared.ErrTransient, err) @@ -74,8 +74,8 @@ func (c *client) resolveRule(ctx context.Context, rule Rule) (Rule, error) { return rule, nil } -func (c *client) createSecurityGroupRule(ctx context.Context, securityGroupID string, rule Rule) error { - if _, err := c.scope.IaaSClient().DefaultAPI.CreateSecurityGroupRule(ctx, c.scope.ProjectID(), c.scope.Region(), securityGroupID). +func (s *Service) createSecurityGroupRule(ctx context.Context, securityGroupID string, rule Rule) error { + if _, err := s.scope.IaaSClient().DefaultAPI.CreateSecurityGroupRule(ctx, s.scope.ProjectID(), s.scope.Region(), securityGroupID). CreateSecurityGroupRulePayload(rule.toCreatePayload()). Execute(); err != nil { return shared.ClassifySDKError("create security group rule", err) @@ -83,20 +83,20 @@ func (c *client) createSecurityGroupRule(ctx context.Context, securityGroupID st return nil } -func (c *client) listSecurityGroupRules(ctx context.Context, securityGroupID string) ([]iaas.SecurityGroupRule, error) { - resp, err := c.scope.IaaSClient().DefaultAPI.ListSecurityGroupRules(ctx, c.scope.ProjectID(), c.scope.Region(), securityGroupID).Execute() +func (s *Service) listSecurityGroupRules(ctx context.Context, securityGroupID string) ([]iaas.SecurityGroupRule, error) { + resp, err := s.scope.IaaSClient().DefaultAPI.ListSecurityGroupRules(ctx, s.scope.ProjectID(), s.scope.Region(), securityGroupID).Execute() if err != nil { return nil, shared.ClassifySDKError("list security group rules", err) } return resp.GetItems(), nil } -func (c *client) deleteSecurityGroupRule(ctx context.Context, securityGroupID, ruleID string) error { +func (s *Service) deleteSecurityGroupRule(ctx context.Context, securityGroupID, ruleID string) error { if ruleID == "" { return nil } - if err := c.scope.IaaSClient().DefaultAPI. - DeleteSecurityGroupRule(ctx, c.scope.ProjectID(), c.scope.Region(), securityGroupID, ruleID). + if err := s.scope.IaaSClient().DefaultAPI. + DeleteSecurityGroupRule(ctx, s.scope.ProjectID(), s.scope.Region(), securityGroupID, ruleID). Execute(); err != nil { err := shared.ClassifySDKError("delete security group rule", err) if !shared.IsNotFound(err) { diff --git a/cloud/services/securitygroup/service.go b/cloud/services/securitygroup/service.go index 1a1e97b..b332c35 100644 --- a/cloud/services/securitygroup/service.go +++ b/cloud/services/securitygroup/service.go @@ -64,26 +64,14 @@ type Scope interface { SecurityGroupSpecs() []SecurityGroupSpec } -// client provides direct security group operations against the STACKIT IaaS API. -type client struct { - scope shared.IaaSClientScope -} - -// newClient returns a client for the given scope. -func newClient(scope shared.IaaSClientScope) *client { - return &client{scope: scope} -} - // Service implements security groups reconciler. type Service struct { - scope Scope - client *client + scope Scope } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ - scope: scope, - client: newClient(scope), + scope: scope, } } diff --git a/cloud/services/server/reconcile.go b/cloud/services/server/reconcile.go index 91c11cc..1df4e95 100644 --- a/cloud/services/server/reconcile.go +++ b/cloud/services/server/reconcile.go @@ -28,7 +28,7 @@ func (s *Service) Reconcile(ctx context.Context) error { return err } - srv, created, err := s.client.ensureServer(ctx, spec) + srv, created, err := s.ensureServer(ctx, spec) if err != nil { return err } @@ -46,8 +46,8 @@ func (s *Service) Delete(ctx context.Context) error { return fmt.Errorf("%w: server ID or tags are required", shared.ErrInvalidInput) } - deletedIDs, err := shared.DeleteByIDOrTags(ctx, spec.ID, spec.Tags, s.client.deleteByID, func(ctx context.Context, tags map[string]string) ([]string, error) { - return shared.ListServerIDsByTags(ctx, s.client.scope, tags) + deletedIDs, err := shared.DeleteByIDOrTags(ctx, spec.ID, spec.Tags, s.deleteByID, func(ctx context.Context, tags map[string]string) ([]string, error) { + return shared.ListServerIDsByTags(ctx, s.scope, tags) }) for _, deletedID := range deletedIDs { log.V(1).Info("Deleted server", "serverID", deletedID) @@ -62,35 +62,35 @@ func (s *Service) Delete(ctx context.Context) error { } // get retrieves a STACKIT server by ID. -func (c *client) get(ctx context.Context, id string) (*Server, error) { +func (s *Service) get(ctx context.Context, id string) (*Server, error) { if id == "" { return nil, fmt.Errorf("%w: server ID is empty", shared.ErrInvalidInput) } - srv, err := c.scope.IaaSClient().DefaultAPI.GetServer(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute() + srv, err := s.scope.IaaSClient().DefaultAPI.GetServer(ctx, s.scope.ProjectID(), s.scope.Region(), id).Execute() if err != nil { return nil, shared.ClassifySDKError("get server", err) } - return c.serverFromSDK(ctx, srv), nil + return s.serverFromSDK(ctx, srv), nil } // findByTags finds a single STACKIT server matching the given tag selector. -func (c *client) findByTags(ctx context.Context, tags map[string]string) (*Server, error) { +func (s *Service) findByTags(ctx context.Context, tags map[string]string) (*Server, error) { srv, err := shared.FindByTags(ctx, tags, func(ctx context.Context, tags map[string]string) ([]*iaas.Server, error) { - return shared.ListServersByTags(ctx, c.scope, tags) + return shared.ListServersByTags(ctx, s.scope, tags) }, "server", "servers") if err != nil { return nil, err } - return c.serverFromSDK(ctx, srv), nil + return s.serverFromSDK(ctx, srv), nil } // deleteByID deletes a server by ID, returning true if the server existed and was deleted, // or false if it was already absent. -func (c *client) deleteByID(ctx context.Context, id string) (bool, error) { +func (s *Service) deleteByID(ctx context.Context, id string) (bool, error) { if id == "" { return false, fmt.Errorf("%w: server ID is empty", shared.ErrInvalidInput) } - if err := c.scope.IaaSClient().DefaultAPI.DeleteServer(ctx, c.scope.ProjectID(), c.scope.Region(), id).Execute(); err != nil { + if err := s.scope.IaaSClient().DefaultAPI.DeleteServer(ctx, s.scope.ProjectID(), s.scope.Region(), id).Execute(); err != nil { err := shared.ClassifySDKError("delete server", err) if shared.IsNotFound(err) { return false, nil @@ -101,21 +101,21 @@ func (c *client) deleteByID(ctx context.Context, id string) (bool, error) { } // create creates a new STACKIT server from the given spec. -func (c *client) create(ctx context.Context, spec ServerSpec) (*Server, error) { +func (s *Service) create(ctx context.Context, spec ServerSpec) (*Server, error) { payload := spec.toCreatePayload() - created, err := c.scope.IaaSClient().DefaultAPI.CreateServer(ctx, c.scope.ProjectID(), c.scope.Region()). + created, err := s.scope.IaaSClient().DefaultAPI.CreateServer(ctx, s.scope.ProjectID(), s.scope.Region()). CreateServerPayload(payload). Execute() if err != nil { return nil, shared.ClassifySDKError("create server", err) } - out := c.serverFromSDK(ctx, created) + out := s.serverFromSDK(ctx, created) logf.FromContext(ctx).V(1).Info("Created server", "name", spec.Name, "serverID", out.ID) return out, nil } -func (c *client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bool, error) { - srv, err := shared.ResolveExisting(ctx, spec.ID, spec.Tags, c.get, c.findByTags, shared.IsNotFound) +func (s *Service) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bool, error) { + srv, err := shared.ResolveExisting(ctx, spec.ID, spec.Tags, s.get, s.findByTags, shared.IsNotFound) if err != nil { return nil, false, err } @@ -133,7 +133,7 @@ func (c *client) ensureServer(ctx context.Context, spec ServerSpec) (*Server, bo ) } - out, err := c.create(ctx, spec) + out, err := s.create(ctx, spec) if err != nil { return nil, false, err } @@ -205,7 +205,7 @@ func (s ServerSpec) toCreatePayload() iaas.CreateServerPayload { return *payload } -func (c *client) serverFromSDK(ctx context.Context, srv *iaas.Server) *Server { +func (s *Service) serverFromSDK(ctx context.Context, srv *iaas.Server) *Server { if srv == nil { return nil } @@ -216,7 +216,7 @@ func (c *client) serverFromSDK(ctx context.Context, srv *iaas.Server) *Server { PowerStatus: srv.GetPowerStatus(), AvailabilityZone: srv.GetAvailabilityZone(), } - nics, err := c.scope.IaaSClient().DefaultAPI.ListServerNICs(ctx, c.scope.ProjectID(), c.scope.Region(), out.ID).Execute() + nics, err := s.scope.IaaSClient().DefaultAPI.ListServerNICs(ctx, s.scope.ProjectID(), s.scope.Region(), out.ID).Execute() if err == nil { for _, nic := range nics.GetItems() { if ipv4 := nic.GetIpv4(); ipv4 != "" { diff --git a/cloud/services/server/service.go b/cloud/services/server/service.go index d5336cf..4b5fff0 100644 --- a/cloud/services/server/service.go +++ b/cloud/services/server/service.go @@ -72,26 +72,14 @@ type Scope interface { ServerSpec() ServerSpec } -// client provides direct server operations against the STACKIT IaaS API. -type client struct { - scope shared.IaaSClientScope -} - -// newClient returns a client for the given scope. -func newClient(scope shared.IaaSClientScope) *client { - return &client{scope: scope} -} - // Service implements server reconciler. type Service struct { - scope Scope - client *client + scope Scope } // New returns Service from given scope. func New(scope Scope) *Service { return &Service{ - scope: scope, - client: newClient(scope), + scope: scope, } } From 9a072008c6a0684a3a892368e431cb4fdde6d4e6 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Mon, 16 Nov 2026 13:41:23 +0100 Subject: [PATCH 39/60] refactor: move bastion SSH key and CIDR validation into server and securitygroup services --- .../services/securitygroup/reconcile_test.go | 8 +++++ cloud/services/securitygroup/rules.go | 16 ++++++--- cloud/services/server/reconcile.go | 3 ++ cloud/services/server/reconcile_test.go | 11 ++++++ cloud/services/server/service.go | 1 + controller/stackitcluster_bastion.go | 35 ------------------- controller/stackitcluster_controller_test.go | 8 ----- scope/cluster_scope.go | 1 + 8 files changed, 35 insertions(+), 48 deletions(-) diff --git a/cloud/services/securitygroup/reconcile_test.go b/cloud/services/securitygroup/reconcile_test.go index 8e2dafb..d3ff90d 100644 --- a/cloud/services/securitygroup/reconcile_test.go +++ b/cloud/services/securitygroup/reconcile_test.go @@ -611,6 +611,14 @@ func TestServiceReconcileValidation(t *testing.T) { Rules: []securitygroup.Rule{{Direction: securitygroup.DirectionIngress, PortMin: 80, PortMax: 80, IPRange: "0.0.0.0/0"}}, }, }, + { + name: "invalid ipRange CIDR", + spec: securitygroup.SecurityGroupSpec{ + Name: "cluster-sg", + Tags: map[string]string{"cluster": "test"}, + Rules: []securitygroup.Rule{{Direction: securitygroup.DirectionIngress, Protocol: securitygroup.ProtocolTCP, PortMin: 22, PortMax: 22, IPRange: "not-a-cidr"}}, + }, + }, } for _, tt := range tests { diff --git a/cloud/services/securitygroup/rules.go b/cloud/services/securitygroup/rules.go index 3ca4d8d..ccf0fb2 100644 --- a/cloud/services/securitygroup/rules.go +++ b/cloud/services/securitygroup/rules.go @@ -13,6 +13,7 @@ package securitygroup import ( "context" "fmt" + "net/netip" "slices" "strings" @@ -115,11 +116,16 @@ func (r Rule) validate() error { DirectionEgress, ) } - if r.IPRange != "" && (r.RemoteSecurityGroupID != "" || len(r.RemoteSecurityGroupTags) > 0) { - return fmt.Errorf( - "%w: security group rule cannot specify both ipRange and remoteSecurityGroupID", - shared.ErrInvalidInput, - ) + if r.IPRange != "" { + if r.RemoteSecurityGroupID != "" || len(r.RemoteSecurityGroupTags) > 0 { + return fmt.Errorf( + "%w: security group rule cannot specify both ipRange and remoteSecurityGroupID", + shared.ErrInvalidInput, + ) + } + if _, err := netip.ParsePrefix(r.IPRange); err != nil { + return fmt.Errorf("%w: security group rule has invalid ipRange CIDR %q", shared.ErrInvalidInput, r.IPRange) + } } if r.hasInvalidPortRange() { return fmt.Errorf("%w: invalid security group rule port range", shared.ErrInvalidInput) diff --git a/cloud/services/server/reconcile.go b/cloud/services/server/reconcile.go index 1df4e95..51504a7 100644 --- a/cloud/services/server/reconcile.go +++ b/cloud/services/server/reconcile.go @@ -150,6 +150,9 @@ func (s ServerSpec) validate() error { if s.MachineType == "" { return fmt.Errorf("%w: server machine type is required", shared.ErrInvalidInput) } + if s.RequireSSHKey && s.SSHKeyName == "" { + return fmt.Errorf("%w: server SSH key name is required", shared.ErrInvalidInput) + } if s.NetworkID == "" { return fmt.Errorf("%w: server network ID is required", shared.ErrInvalidInput) } diff --git a/cloud/services/server/reconcile_test.go b/cloud/services/server/reconcile_test.go index 4ee8bb4..e5468ea 100644 --- a/cloud/services/server/reconcile_test.go +++ b/cloud/services/server/reconcile_test.go @@ -470,6 +470,17 @@ func TestServiceReconcileValidation(t *testing.T) { Tags: map[string]string{"cluster": "test"}, }, }, + { + name: "missing SSH key name when required", + spec: server.ServerSpec{ + Name: "node-0", + ImageID: testImageID, + MachineType: "c2i.2", + RequireSSHKey: true, + NetworkID: testNetworkID, + Tags: map[string]string{"cluster": "test"}, + }, + }, { name: "missing network ID", spec: server.ServerSpec{ diff --git a/cloud/services/server/service.go b/cloud/services/server/service.go index 4b5fff0..3bf15b2 100644 --- a/cloud/services/server/service.go +++ b/cloud/services/server/service.go @@ -56,6 +56,7 @@ type ServerSpec struct { MachineType string AvailabilityZone string SSHKeyName string + RequireSSHKey bool NetworkID string SecurityGroups []string UserData []byte diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index 2368724..5f09bf3 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -14,7 +14,6 @@ import ( "context" "crypto/sha256" "fmt" - "net/netip" "time" corev1 "k8s.io/api/core/v1" @@ -30,7 +29,6 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/scope" ) @@ -64,18 +62,6 @@ func (r *StackitClusterReconciler) reconcileBastion( return ctrl.Result{}, true, nil } - if err := validateBastionSpec(stackitCluster.Spec.Bastion); err != nil { - log.Error(err, "Invalid bastion spec", "cluster", stackitCluster.Name) - record.Warnf(stackitCluster, "InvalidBastionSpec", "Invalid bastion spec: %v", err) - clusterScope.SetNotReady( - "InvalidBastionSpec", - err.Error(), - infrav1.ClusterBastionReadyCondition, - infrav1.ClusterReadyCondition, - ) - return ctrl.Result{}, false, nil - } - cloudInit, err := r.resolveBastionCloudInit(ctx, stackitCluster) if err != nil { log.Error(err, "Failed to resolve bastion cloud-init", "cluster", stackitCluster.Name) @@ -152,27 +138,6 @@ func (r *StackitClusterReconciler) reconcileBastion( return ctrl.Result{}, true, nil } -func validateBastionSpec(spec infrav1.StackitBastionSpec) error { - if spec.ImageID == "" { - return fmt.Errorf("%w: bastion.imageID is required", shared.ErrInvalidInput) - } - if spec.MachineType == "" { - return fmt.Errorf("%w: bastion.machineType is required", shared.ErrInvalidInput) - } - if spec.SSHKeyName == "" { - return fmt.Errorf("%w: bastion.sshKeyName is required", shared.ErrInvalidInput) - } - if len(spec.AllowedCIDRs) == 0 { - return fmt.Errorf("%w: bastion.allowedCIDRs is required", shared.ErrInvalidInput) - } - for _, cidr := range spec.AllowedCIDRs { - if _, err := netip.ParsePrefix(cidr); err != nil { - return fmt.Errorf("%w: bastion.allowedCIDRs contains invalid CIDR %q", shared.ErrInvalidInput, cidr) - } - } - return nil -} - func deleteBastion( ctx context.Context, clusterScope *scope.ClusterScope, diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index 09d938c..7a0e0d9 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -419,14 +419,6 @@ var _ = Describe("StackitCluster Controller", func() { "load balancer leaked because cleanup was gated on spec and status") }) - It("validates bastion specs", func() { - spec := validBastionSpec() - Expect(validateBastionSpec(spec)).To(Succeed()) - - spec.AllowedCIDRs = []string{"not-a-cidr"} - Expect(validateBastionSpec(spec)).To(MatchError(ContainSubstring("invalid CIDR"))) - }) - It("creates the bastion with cloud-init user data from a ConfigMap", func() { expectBastionCloudInit( ctx, reconciler, request, stackitKey, fakeCloud, diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index 0376505..fc2d350 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -145,6 +145,7 @@ func (s *ClusterScope) ServerSpec() server.ServerSpec { MachineType: s.StackitCluster.Spec.Bastion.MachineType, AvailabilityZone: defaultAvailabilityZone(s.StackitCluster), SSHKeyName: s.StackitCluster.Spec.Bastion.SSHKeyName, + RequireSSHKey: true, NetworkID: s.StackitCluster.Spec.Network.ID, UserData: s.bastionCloudInit, Tags: s.bastionTags(), From 77c32ebcdd02746db14d3105f7c2b7b77fa75bf6 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Mon, 16 Nov 2026 13:57:01 +0100 Subject: [PATCH 40/60] refactor: remove duplicate port range check --- cloud/services/loadbalancer/targets.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/cloud/services/loadbalancer/targets.go b/cloud/services/loadbalancer/targets.go index 5706f26..80befb7 100644 --- a/cloud/services/loadbalancer/targets.go +++ b/cloud/services/loadbalancer/targets.go @@ -168,11 +168,6 @@ func (s *Service) updateAPIServerTargetPool( ) error { payload := lb.NewUpdateTargetPoolPayload() payload.SetName(apiserverTargetPoolName) - if port > 0 { - payload.SetTargetPort(port) - } else if current.GetTargetPort() > 0 { - payload.SetTargetPort(current.GetTargetPort()) - } payload.SetTargets(targets) if _, err := s.scope.LoadBalancerClient().DefaultAPI. UpdateTargetPool(ctx, s.scope.ProjectID(), s.scope.Region(), loadBalancerID, apiserverTargetPoolName). From 40bc54b3ea23d8f8138bf795e256ee01cfe1060a Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Mon, 16 Nov 2026 14:20:55 +0100 Subject: [PATCH 41/60] refactor: extract generic shared.ListIDsByTags helper --- cloud/services/loadbalancer/reconcile.go | 14 +++--------- cloud/services/publicip/reconcile.go | 14 +++--------- cloud/services/shared/errors_test.go | 27 ++++++++++++++++++++++++ cloud/services/shared/utils.go | 20 ++++++++++++++---- 4 files changed, 49 insertions(+), 26 deletions(-) diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index 07aab8c..ba603d7 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -153,17 +153,9 @@ func (s *Service) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) } func (s *Service) listIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { - loadBalancers, err := s.listByTags(ctx, tags) - if err != nil { - return nil, err - } - ids := make([]string, 0, len(loadBalancers)) - for _, loadBalancer := range loadBalancers { - if loadBalancer.ID != "" { - ids = append(ids, loadBalancer.ID) - } - } - return ids, nil + return shared.ListIDsByTags(ctx, tags, s.listByTags, func(loadBalancer *LoadBalancer) string { + return loadBalancer.ID + }) } func (s LoadBalancerSpec) validate() error { diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index 256f5e8..6d31b15 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -174,17 +174,9 @@ func (s *Service) detachAndDeleteByID(ctx context.Context, id string, serverIDs } func (s *Service) listIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { - publicIPs, err := s.listByTags(ctx, tags) - if err != nil { - return nil, err - } - ids := make([]string, 0, len(publicIPs)) - for _, publicIP := range publicIPs { - if publicIP.ID != "" { - ids = append(ids, publicIP.ID) - } - } - return ids, nil + return shared.ListIDsByTags(ctx, tags, s.listByTags, func(publicIP *PublicIP) string { + return publicIP.ID + }) } func (s *Service) ensurePublicIP(ctx context.Context, spec PublicIPSpec) (*PublicIP, error) { diff --git a/cloud/services/shared/errors_test.go b/cloud/services/shared/errors_test.go index a75885a..d292fdf 100644 --- a/cloud/services/shared/errors_test.go +++ b/cloud/services/shared/errors_test.go @@ -126,6 +126,33 @@ func TestFindByTags(t *testing.T) { }) } +func TestListIDsByTags(t *testing.T) { + ctx := context.Background() + tags := map[string]string{"cluster": "test"} + type item struct{ id string } + + t.Run("propagates error from listByTags", func(t *testing.T) { + _, err := ListIDsByTags(ctx, tags, func(context.Context, map[string]string) ([]item, error) { + return nil, ErrTransient + }, func(i item) string { return i.id }) + if !IsTransient(err) { + t.Fatalf("ListIDsByTags() error = %v, want ErrTransient", err) + } + }) + + t.Run("collects non-empty IDs", func(t *testing.T) { + ids, err := ListIDsByTags(ctx, tags, func(context.Context, map[string]string) ([]item, error) { + return []item{{id: "id-1"}, {id: ""}, {id: "id-2"}}, nil + }, func(i item) string { return i.id }) + if err != nil { + t.Fatalf("ListIDsByTags() error = %v", err) + } + if len(ids) != 2 || ids[0] != "id-1" || ids[1] != "id-2" { + t.Fatalf("ListIDsByTags() = %v, want [id-1 id-2]", ids) + } + }) +} + func TestResolveExisting(t *testing.T) { const ( testID1 = "id-1" diff --git a/cloud/services/shared/utils.go b/cloud/services/shared/utils.go index 44da69e..66ed9c5 100644 --- a/cloud/services/shared/utils.go +++ b/cloud/services/shared/utils.go @@ -60,13 +60,25 @@ func ListServersByTags(ctx context.Context, scope IaaSClientScope, tags map[stri // ListServerIDsByTags lists the IDs of STACKIT servers matching the given tag selector. func ListServerIDsByTags(ctx context.Context, scope IaaSClientScope, tags map[string]string) ([]string, error) { - servers, err := ListServersByTags(ctx, scope, tags) + return ListIDsByTags(ctx, tags, func(ctx context.Context, tags map[string]string) ([]*iaas.Server, error) { + return ListServersByTags(ctx, scope, tags) + }, (*iaas.Server).GetId) +} + +// ListIDsByTags lists resources by tags and collects their non-empty IDs. +func ListIDsByTags[T any]( + ctx context.Context, + tags map[string]string, + listByTags func(context.Context, map[string]string) ([]T, error), + getID func(T) string, +) ([]string, error) { + items, err := listByTags(ctx, tags) if err != nil { return nil, err } - ids := make([]string, 0, len(servers)) - for _, srv := range servers { - if id := srv.GetId(); id != "" { + ids := make([]string, 0, len(items)) + for _, item := range items { + if id := getID(item); id != "" { ids = append(ids, id) } } From 2deb8d5b2e1f25ec3618eabc8223d390a51ca623 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Mon, 16 Nov 2026 14:35:03 +0100 Subject: [PATCH 42/60] fix: require ingress rule source and set target port on pool update --- cloud/services/loadbalancer/targets.go | 4 ++-- .../services/securitygroup/reconcile_test.go | 8 +++++++ cloud/services/securitygroup/rules.go | 9 +++++++- scope/cluster_scope.go | 11 +++++----- scope/cluster_scope_test.go | 22 +++++++++++++++++++ 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/cloud/services/loadbalancer/targets.go b/cloud/services/loadbalancer/targets.go index 80befb7..19b0d57 100644 --- a/cloud/services/loadbalancer/targets.go +++ b/cloud/services/loadbalancer/targets.go @@ -156,18 +156,18 @@ func (s *Service) setTargets( if targetPool.GetTargetPort() == port && sameTargets(targetPool.GetTargets(), desired) { return nil } - return s.updateAPIServerTargetPool(ctx, loadBalancerID, targetPool, desired, port) + return s.updateAPIServerTargetPool(ctx, loadBalancerID, desired, port) } func (s *Service) updateAPIServerTargetPool( ctx context.Context, loadBalancerID string, - current *lb.TargetPool, targets []lb.Target, port int32, ) error { payload := lb.NewUpdateTargetPoolPayload() payload.SetName(apiserverTargetPoolName) + payload.SetTargetPort(port) payload.SetTargets(targets) if _, err := s.scope.LoadBalancerClient().DefaultAPI. UpdateTargetPool(ctx, s.scope.ProjectID(), s.scope.Region(), loadBalancerID, apiserverTargetPoolName). diff --git a/cloud/services/securitygroup/reconcile_test.go b/cloud/services/securitygroup/reconcile_test.go index d3ff90d..e6f9dcb 100644 --- a/cloud/services/securitygroup/reconcile_test.go +++ b/cloud/services/securitygroup/reconcile_test.go @@ -619,6 +619,14 @@ func TestServiceReconcileValidation(t *testing.T) { Rules: []securitygroup.Rule{{Direction: securitygroup.DirectionIngress, Protocol: securitygroup.ProtocolTCP, PortMin: 22, PortMax: 22, IPRange: "not-a-cidr"}}, }, }, + { + name: "ingress rule without ipRange or remoteSecurityGroup", + spec: securitygroup.SecurityGroupSpec{ + Name: "cluster-sg", + Tags: map[string]string{"cluster": "test"}, + Rules: []securitygroup.Rule{{Direction: securitygroup.DirectionIngress, Protocol: securitygroup.ProtocolTCP, PortMin: 22, PortMax: 22}}, + }, + }, } for _, tt := range tests { diff --git a/cloud/services/securitygroup/rules.go b/cloud/services/securitygroup/rules.go index ccf0fb2..73514f4 100644 --- a/cloud/services/securitygroup/rules.go +++ b/cloud/services/securitygroup/rules.go @@ -116,8 +116,15 @@ func (r Rule) validate() error { DirectionEgress, ) } + hasRemoteSecurityGroup := r.RemoteSecurityGroupID != "" || len(r.RemoteSecurityGroupTags) > 0 + if r.Direction == DirectionIngress && r.IPRange == "" && !hasRemoteSecurityGroup { + return fmt.Errorf( + "%w: ingress security group rule requires ipRange or remoteSecurityGroupID", + shared.ErrInvalidInput, + ) + } if r.IPRange != "" { - if r.RemoteSecurityGroupID != "" || len(r.RemoteSecurityGroupTags) > 0 { + if hasRemoteSecurityGroup { return fmt.Errorf( "%w: security group rule cannot specify both ipRange and remoteSecurityGroupID", shared.ErrInvalidInput, diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index fc2d350..aa82298 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -385,11 +385,12 @@ func (s *ClusterScope) setBastionPublicIP(id, ip string) { } func (s *ClusterScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { - rules := make([]securitygroup.Rule, 0, len(s.StackitCluster.Spec.Bastion.AllowedCIDRs)+1) - for _, cidr := range s.StackitCluster.Spec.Bastion.AllowedCIDRs { - if cidr == "" { - continue - } + allowedCIDRs := s.StackitCluster.Spec.Bastion.AllowedCIDRs + if len(allowedCIDRs) == 0 { + allowedCIDRs = []string{""} + } + rules := make([]securitygroup.Rule, 0, len(allowedCIDRs)+1) + for _, cidr := range allowedCIDRs { rules = append(rules, securitygroup.Rule{ Direction: securitygroup.DirectionIngress, Protocol: securitygroup.ProtocolTCP, diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index 723689c..a4d1232 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -142,6 +142,28 @@ func TestNodeSSHSecurityGroupSpecIncludesAdditionalLabelsInServerSelector(t *tes } } +func TestClusterScopeSecurityGroupSpecsIncludesEmptyIngressRuleWhenAllowedCIDRsMissing(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-cluster", + Namespace: "my-ns", + }, + }, + } + + specs := clusterScope.SecurityGroupSpecs() + if len(specs) != 2 { + t.Fatalf("SecurityGroupSpecs() count = %d, want 2", len(specs)) + } + if len(specs[0].Rules) != 2 { + t.Fatalf("bastion SecurityGroupSpec Rules count = %d, want 2 (1 ingress + 1 egress)", len(specs[0].Rules)) + } + if specs[0].Rules[0].Direction != "ingress" || specs[0].Rules[0].IPRange != "" { + t.Fatalf("bastion SecurityGroupSpec Rules[0] = %#v, want ingress rule with empty IPRange", specs[0].Rules[0]) + } +} + func TestMachineScopeSecurityGroupSpecsRespectsBastionEnabled(t *testing.T) { machineScope := &MachineScope{ StackitCluster: &infrav1.StackitCluster{ From 0f25312ff1d3fa4552047708335b9a110a08aae0 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Mon, 16 Nov 2026 14:55:26 +0100 Subject: [PATCH 43/60] test: cover RequireSSHKey in scope ServerSpec and use DirectionIngress constant --- scope/cluster_scope_test.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index a4d1232..f8e3b91 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -21,6 +21,7 @@ import ( cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -159,11 +160,40 @@ func TestClusterScopeSecurityGroupSpecsIncludesEmptyIngressRuleWhenAllowedCIDRsM if len(specs[0].Rules) != 2 { t.Fatalf("bastion SecurityGroupSpec Rules count = %d, want 2 (1 ingress + 1 egress)", len(specs[0].Rules)) } - if specs[0].Rules[0].Direction != "ingress" || specs[0].Rules[0].IPRange != "" { + if specs[0].Rules[0].Direction != securitygroup.DirectionIngress || specs[0].Rules[0].IPRange != "" { t.Fatalf("bastion SecurityGroupSpec Rules[0] = %#v, want ingress rule with empty IPRange", specs[0].Rules[0]) } } +func TestServerSpecRequireSSHKey(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{ + Spec: infrav1.StackitClusterSpec{ + Bastion: infrav1.StackitBastionSpec{ + SSHKeyName: "my-key", + }, + }, + }, + } + clusterServerSpec := clusterScope.ServerSpec() + if !clusterServerSpec.RequireSSHKey { + t.Fatal("ClusterScope.ServerSpec().RequireSSHKey = false, want true") + } + if clusterServerSpec.SSHKeyName != "my-key" { + t.Fatalf("ClusterScope.ServerSpec().SSHKeyName = %q, want %q", clusterServerSpec.SSHKeyName, "my-key") + } + + machineScope := &MachineScope{ + cluster: &clusterv1.Cluster{}, + Machine: &clusterv1.Machine{}, + StackitCluster: &infrav1.StackitCluster{}, + StackitMachine: &infrav1.StackitMachine{}, + } + if machineScope.ServerSpec().RequireSSHKey { + t.Fatal("MachineScope.ServerSpec().RequireSSHKey = true, want false") + } +} + func TestMachineScopeSecurityGroupSpecsRespectsBastionEnabled(t *testing.T) { machineScope := &MachineScope{ StackitCluster: &infrav1.StackitCluster{ From ee506cb135c6c871664006eab30c0cb94252a9bb Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Mon, 16 Nov 2026 15:42:16 +0100 Subject: [PATCH 44/60] refactor: use shared.DeleteByIDOrTags in securitygroup.deleteSecurityGroup --- cloud/services/securitygroup/reconcile.go | 89 ++++++++--------------- 1 file changed, 31 insertions(+), 58 deletions(-) diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index cb84673..f9b58a1 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -143,41 +143,19 @@ func (s *Service) reconcileSecurityGroup(ctx context.Context, spec SecurityGroup return nil } -type resolvedSecurityGroup struct { - id string - rules []iaas.SecurityGroupRule -} - func (s *Service) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpec) error { if spec.ID == "" && len(spec.Tags) == 0 { return fmt.Errorf("%w: security group ID or tags are required", shared.ErrInvalidInput) } - resolved, resolveErr := s.resolveSecurityGroupsForDelete(ctx, spec) - if resolveErr != nil && len(resolved) == 0 { - return resolveErr - } - var errs []error - if resolveErr != nil { - errs = append(errs, resolveErr) - } - for _, sg := range resolved { - if err := s.detachSecurityGroupFromServers(ctx, sg.id, spec); err != nil { - errs = append(errs, err) - } - if err := s.deleteSecurityGroupRules(ctx, sg.id, sg.rules); err != nil { - errs = append(errs, err) - } + deletedIDs, err := shared.DeleteByIDOrTags(ctx, spec.ID, spec.Tags, func(ctx context.Context, id string) (bool, error) { + return s.deleteByID(ctx, id, spec) + }, s.listIDsByTags) + for _, deletedID := range deletedIDs { + logf.FromContext(ctx).V(1).Info("Deleted security group", "securityGroupID", deletedID) } - for _, sg := range resolved { - if err := s.deleteSecurityGroupByID(ctx, sg.id); err != nil { - errs = append(errs, err) - continue - } - logf.FromContext(ctx).V(1).Info("Deleted security group", "securityGroupID", sg.id) - } - if len(errs) > 0 { - return errors.Join(errs...) + if err != nil { + return err } if spec.SetID != nil { spec.SetID("") @@ -185,39 +163,34 @@ func (s *Service) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpe return nil } -func (s *Service) resolveSecurityGroupsForDelete( - ctx context.Context, - spec SecurityGroupSpec, -) ([]resolvedSecurityGroup, error) { - if spec.ID != "" { - rules, ok, err := s.listRulesIfGroupExists(ctx, spec.ID) - if err != nil { - return nil, err - } - if ok { - return []resolvedSecurityGroup{{id: spec.ID, rules: rules}}, nil - } - } - if len(spec.Tags) == 0 { - return nil, nil +func (s *Service) deleteByID(ctx context.Context, id string, spec SecurityGroupSpec) (bool, error) { + if id == "" { + return false, fmt.Errorf("%w: security group ID is empty", shared.ErrInvalidInput) } - securityGroups, err := s.listByTags(ctx, spec.Tags) - if err != nil { - return nil, err + rules, ok, err := s.listRulesIfGroupExists(ctx, id) + if err != nil || !ok { + return false, err } - resolved := make([]resolvedSecurityGroup, 0, len(securityGroups)) var errs []error - for _, sg := range securityGroups { - rules, ok, err := s.listRulesIfGroupExists(ctx, sg.ID) - if err != nil { - errs = append(errs, err) - continue - } - if ok { - resolved = append(resolved, resolvedSecurityGroup{id: sg.ID, rules: rules}) - } + if err := s.detachSecurityGroupFromServers(ctx, id, spec); err != nil { + errs = append(errs, err) + } + if err := s.deleteSecurityGroupRules(ctx, id, rules); err != nil { + errs = append(errs, err) + } + if err := s.deleteSecurityGroupByID(ctx, id); err != nil { + errs = append(errs, err) } - return resolved, errors.Join(errs...) + if len(errs) > 0 { + return false, errors.Join(errs...) + } + return true, nil +} + +func (s *Service) listIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { + return shared.ListIDsByTags(ctx, tags, s.listByTags, func(securityGroup *SecurityGroup) string { + return securityGroup.ID + }) } func (s *Service) listRulesIfGroupExists(ctx context.Context, id string) ([]iaas.SecurityGroupRule, bool, error) { From 95d85c1f29f6feb81f7925430ec84d797e5f4bb9 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Mon, 16 Nov 2026 15:46:29 +0100 Subject: [PATCH 45/60] refactor: align securitygroup deleteByID with other cloud services --- cloud/services/securitygroup/reconcile.go | 53 +++++++++++------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index f9b58a1..85988ae 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -118,6 +118,24 @@ func (s *Service) detachFromServer(ctx context.Context, serverID, securityGroupI return nil } +// deleteByID deletes a security group by ID, returning true if the security group existed and was deleted, +// or false if it was already absent. +func (s *Service) deleteByID(ctx context.Context, id string) (bool, error) { + if id == "" { + return false, fmt.Errorf("%w: security group ID is empty", shared.ErrInvalidInput) + } + if err := s.scope.IaaSClient().DefaultAPI. + DeleteSecurityGroup(ctx, s.scope.ProjectID(), s.scope.Region(), id). + Execute(); err != nil { + err := shared.ClassifySDKError("delete security group", err) + if shared.IsNotFound(err) { + return false, nil + } + return false, err + } + return true, nil +} + func (s *Service) reconcileSecurityGroup(ctx context.Context, spec SecurityGroupSpec) error { if err := spec.validate(); err != nil { return err @@ -149,7 +167,7 @@ func (s *Service) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpe } deletedIDs, err := shared.DeleteByIDOrTags(ctx, spec.ID, spec.Tags, func(ctx context.Context, id string) (bool, error) { - return s.deleteByID(ctx, id, spec) + return s.cleanupAndDeleteByID(ctx, id, spec) }, s.listIDsByTags) for _, deletedID := range deletedIDs { logf.FromContext(ctx).V(1).Info("Deleted security group", "securityGroupID", deletedID) @@ -163,7 +181,7 @@ func (s *Service) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpe return nil } -func (s *Service) deleteByID(ctx context.Context, id string, spec SecurityGroupSpec) (bool, error) { +func (s *Service) cleanupAndDeleteByID(ctx context.Context, id string, spec SecurityGroupSpec) (bool, error) { if id == "" { return false, fmt.Errorf("%w: security group ID is empty", shared.ErrInvalidInput) } @@ -171,20 +189,13 @@ func (s *Service) deleteByID(ctx context.Context, id string, spec SecurityGroupS if err != nil || !ok { return false, err } - var errs []error - if err := s.detachSecurityGroupFromServers(ctx, id, spec); err != nil { - errs = append(errs, err) - } - if err := s.deleteSecurityGroupRules(ctx, id, rules); err != nil { - errs = append(errs, err) - } - if err := s.deleteSecurityGroupByID(ctx, id); err != nil { - errs = append(errs, err) - } - if len(errs) > 0 { - return false, errors.Join(errs...) + if err := errors.Join( + s.detachSecurityGroupFromServers(ctx, id, spec), + s.deleteSecurityGroupRules(ctx, id, rules), + ); err != nil { + return false, err } - return true, nil + return s.deleteByID(ctx, id) } func (s *Service) listIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { @@ -238,18 +249,6 @@ func (s *Service) deleteSecurityGroupRules( return errors.Join(errs...) } -func (s *Service) deleteSecurityGroupByID(ctx context.Context, id string) error { - if err := s.scope.IaaSClient().DefaultAPI. - DeleteSecurityGroup(ctx, s.scope.ProjectID(), s.scope.Region(), id). - Execute(); err != nil { - err := shared.ClassifySDKError("delete security group", err) - if !shared.IsNotFound(err) { - return err - } - } - return nil -} - func (s *Service) detachSecurityGroupFromServers(ctx context.Context, securityGroupID string, spec SecurityGroupSpec) error { var errs []error if err := s.detachServers(ctx, securityGroupID, spec.ServerIDs); err != nil { From 1babd52a1eddf1ab9a75ba1dd21436c6bba640a8 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Mon, 16 Nov 2026 15:48:49 +0100 Subject: [PATCH 46/60] refactor: remove redundant empty ID check in cleanupAndDeleteByID --- cloud/services/securitygroup/reconcile.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index 85988ae..bcbfecd 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -182,9 +182,6 @@ func (s *Service) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpe } func (s *Service) cleanupAndDeleteByID(ctx context.Context, id string, spec SecurityGroupSpec) (bool, error) { - if id == "" { - return false, fmt.Errorf("%w: security group ID is empty", shared.ErrInvalidInput) - } rules, ok, err := s.listRulesIfGroupExists(ctx, id) if err != nil || !ok { return false, err From 688cd3fb105c962c57b94926c258141542e03da0 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Mon, 16 Nov 2026 16:21:47 +0100 Subject: [PATCH 47/60] fix: log no-op SDK calls in attachToServer and detachFromServer --- cloud/services/publicip/reconcile.go | 4 + cloud/services/publicip/reconcile_test.go | 58 ++++++++++++- cloud/services/securitygroup/reconcile.go | 4 + .../services/securitygroup/reconcile_test.go | 84 +++++++++++++++++++ 4 files changed, 148 insertions(+), 2 deletions(-) diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index 6d31b15..45781e2 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -126,6 +126,8 @@ func (s *Service) attachToServer(ctx context.Context, serverID, publicIPID strin if !shared.IsConflict(err) { return err } + logf.FromContext(ctx).V(1).Info("Public IP already attached to server", "publicIPID", publicIPID, "serverID", serverID, "err", err) + return nil } logf.FromContext(ctx).V(1).Info("Attached public IP to server", "publicIPID", publicIPID, "serverID", serverID) return nil @@ -143,6 +145,8 @@ func (s *Service) detachFromServer(ctx context.Context, serverID, publicIPID str if !shared.IsNotFound(err) && !shared.IsConflict(err) { return err } + logf.FromContext(ctx).V(1).Info("Public IP already detached from server", "publicIPID", publicIPID, "serverID", serverID, "err", err) + return nil } logf.FromContext(ctx).V(1).Info("Detached public IP from server", "publicIPID", publicIPID, "serverID", serverID) return nil diff --git a/cloud/services/publicip/reconcile_test.go b/cloud/services/publicip/reconcile_test.go index e4eb806..904f49e 100644 --- a/cloud/services/publicip/reconcile_test.go +++ b/cloud/services/publicip/reconcile_test.go @@ -19,8 +19,11 @@ import ( "strings" "testing" + "github.com/go-logr/logr" + "github.com/go-logr/logr/funcr" "github.com/stackitcloud/stackit-sdk-go/core/config" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + logf "sigs.k8s.io/controller-runtime/pkg/log" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" @@ -104,6 +107,13 @@ func readJSON(t *testing.T, r *http.Request) map[string]any { return out } +func contextWithLogCapture(logs *[]string) context.Context { + logger := funcr.New(func(_, args string) { + *logs = append(*logs, args) + }, funcr.Options{Verbosity: 1}) + return logf.IntoContext(context.Background(), logr.New(logger.GetSink())) +} + func TestServiceReconcile(t *testing.T) { t.Run("creates public IP and attaches it to server when not found", func(t *testing.T) { var ( @@ -227,7 +237,10 @@ func TestServiceReconcileReuseAndErrors(t *testing.T) { }) t.Run("tolerates conflict when attaching public IP to server", func(t *testing.T) { - var recordedID string + var ( + recordedID string + logs []string + ) spec := publicip.PublicIPSpec{ ID: testPublicIPID, Tags: map[string]string{"cluster": "test"}, @@ -253,12 +266,19 @@ func TestServiceReconcileReuseAndErrors(t *testing.T) { } })) - if err := svc.Reconcile(context.Background()); err != nil { + if err := svc.Reconcile(contextWithLogCapture(&logs)); err != nil { t.Fatalf("Reconcile() error = %v, want nil on conflict", err) } if recordedID != testPublicIPID { t.Fatalf("recordedID = %q, want %q", recordedID, testPublicIPID) } + joined := strings.Join(logs, "\n") + if !strings.Contains(joined, "Public IP already attached to server") || !strings.Contains(joined, `"err"=`) { + t.Fatalf("logs = %v, want no-op attach log with error", logs) + } + if strings.Contains(joined, "Attached public IP to server") { + t.Fatalf("logs = %v, did not expect Attached public IP to server on conflict", logs) + } }) t.Run("returns ErrConflict when multiple public IPs match tags", func(t *testing.T) { @@ -349,6 +369,40 @@ func TestServiceDelete(t *testing.T) { } }) + t.Run("logs no-op when detaching public IP returns conflict or not found", func(t *testing.T) { + for _, status := range []int{http.StatusNotFound, http.StatusConflict} { + var logs []string + spec := publicip.PublicIPSpec{ + ID: testPublicIPID, + ServerID: testServerID, + } + + svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/servers/"+testServerID+"/public-ips/"+testPublicIPID): + w.WriteHeader(status) + writeJSON(t, w, map[string]any{"message": "no-op"}) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/public-ips/"+testPublicIPID): + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Delete(contextWithLogCapture(&logs)); err != nil { + t.Fatalf("Delete() error = %v", err) + } + joined := strings.Join(logs, "\n") + if !strings.Contains(joined, "Public IP already detached from server") || !strings.Contains(joined, `"err"=`) { + t.Fatalf("status %d: logs = %v, want no-op detach log with error", status, logs) + } + if strings.Contains(joined, "Detached public IP from server") { + t.Fatalf("status %d: logs = %v, did not expect Detached public IP from server", status, logs) + } + } + }) + t.Run("returns ErrInvalidInput when both ID and tags are empty", func(t *testing.T) { svc := newTestService(t, publicip.PublicIPSpec{}, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index bcbfecd..469f7ad 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -99,6 +99,8 @@ func (s *Service) attachToServer(ctx context.Context, serverID, securityGroupID if !shared.IsConflict(err) && !shared.IsInvalidInput(err) && !shared.IsNotFound(err) { return err } + logf.FromContext(ctx).V(1).Info("Security group already attached to server", "securityGroupID", securityGroupID, "serverID", serverID, "err", err) + return nil } logf.FromContext(ctx).V(1).Info("Attached security group to server", "securityGroupID", securityGroupID, "serverID", serverID) return nil @@ -113,6 +115,8 @@ func (s *Service) detachFromServer(ctx context.Context, serverID, securityGroupI if !shared.IsNotFound(err) && !shared.IsConflict(err) { return err } + logf.FromContext(ctx).V(1).Info("Security group already detached from server", "securityGroupID", securityGroupID, "serverID", serverID, "err", err) + return nil } logf.FromContext(ctx).V(1).Info("Detached security group from server", "securityGroupID", securityGroupID, "serverID", serverID) return nil diff --git a/cloud/services/securitygroup/reconcile_test.go b/cloud/services/securitygroup/reconcile_test.go index e6f9dcb..84a4acc 100644 --- a/cloud/services/securitygroup/reconcile_test.go +++ b/cloud/services/securitygroup/reconcile_test.go @@ -20,8 +20,11 @@ import ( "strings" "testing" + "github.com/go-logr/logr" + "github.com/go-logr/logr/funcr" "github.com/stackitcloud/stackit-sdk-go/core/config" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" + logf "sigs.k8s.io/controller-runtime/pkg/log" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" @@ -104,6 +107,13 @@ func readJSON(t *testing.T, r *http.Request) map[string]any { return out } +func contextWithLogCapture(logs *[]string) context.Context { + logger := funcr.New(func(_, args string) { + *logs = append(*logs, args) + }, funcr.Options{Verbosity: 1}) + return logf.IntoContext(context.Background(), logr.New(logger.GetSink())) +} + func TestServiceReconcile(t *testing.T) { t.Run("creates security group and generic rules when not found", func(t *testing.T) { var ( @@ -345,6 +355,44 @@ func TestServiceReconcileReuseAndErrors(t *testing.T) { }) } +func TestServiceReconcileAttachNoOpLogging(t *testing.T) { + for _, status := range []int{http.StatusConflict, http.StatusBadRequest, http.StatusNotFound} { + var logs []string + specs := []securitygroup.SecurityGroupSpec{{ + ID: testSecurityGroupID, + Name: "cluster-sg", + Tags: map[string]string{"cluster": "test"}, + ServerIDs: []string{testServerID}, + }} + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID): + writeJSON(t, w, map[string]any{"id": testSecurityGroupID, "name": "cluster-sg"}) + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodPut && strings.HasSuffix(path, "/servers/"+testServerID+"/security-groups/"+testSecurityGroupID): + w.WriteHeader(status) + writeJSON(t, w, map[string]any{"message": "no-op"}) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Reconcile(contextWithLogCapture(&logs)); err != nil { + t.Fatalf("status %d: Reconcile() error = %v", status, err) + } + joined := strings.Join(logs, "\n") + if !strings.Contains(joined, "Security group already attached to server") || !strings.Contains(joined, `"err"=`) { + t.Fatalf("status %d: logs = %v, want no-op attach log with error", status, logs) + } + if strings.Contains(joined, "Attached security group to server") { + t.Fatalf("status %d: logs = %v, did not expect Attached security group to server", status, logs) + } + } +} + func TestServiceReconcileBastionAndNodeSSH(t *testing.T) { var ( bastionSGID string @@ -761,6 +809,42 @@ func TestServiceDelete(t *testing.T) { }) } +func TestServiceDeleteDetachNoOpLogging(t *testing.T) { + for _, status := range []int{http.StatusNotFound, http.StatusConflict} { + var logs []string + specs := []securitygroup.SecurityGroupSpec{{ + ID: testSecurityGroupID, + ServerIDs: []string{testServerID}, + }} + + svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch { + case r.Method == http.MethodGet && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID+"/rules"): + writeJSON(t, w, map[string]any{"items": []any{}}) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/servers/"+testServerID+"/security-groups/"+testSecurityGroupID): + w.WriteHeader(status) + writeJSON(t, w, map[string]any{"message": "no-op"}) + case r.Method == http.MethodDelete && strings.HasSuffix(path, "/security-groups/"+testSecurityGroupID): + w.WriteHeader(http.StatusNoContent) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) + } + })) + + if err := svc.Delete(contextWithLogCapture(&logs)); err != nil { + t.Fatalf("status %d: Delete() error = %v", status, err) + } + joined := strings.Join(logs, "\n") + if !strings.Contains(joined, "Security group already detached from server") || !strings.Contains(joined, `"err"=`) { + t.Fatalf("status %d: logs = %v, want no-op detach log with error", status, logs) + } + if strings.Contains(joined, "Detached security group from server") { + t.Fatalf("status %d: logs = %v, did not expect Detached security group from server", status, logs) + } + } +} + func TestServiceDeleteStaleIDFallbackAndDetach(t *testing.T) { var ( detachedPairs []string From f1e1a9d4e9cbfbdbf26a216083af360404882cb2 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Mon, 16 Nov 2026 17:01:50 +0100 Subject: [PATCH 48/60] refactor: move CloudFailureResult and CredentialFailureResult onto ClusterScope and MachineScope --- controller/stackitcluster_infrastructure.go | 25 +-- controller/stackitmachine_infrastructure.go | 23 +-- scope/cluster_scope.go | 46 +++++ scope/cluster_scope_test.go | 186 ++++++++++++++++++++ scope/machine_scope.go | 23 +++ util/conditions.go | 43 ----- 6 files changed, 264 insertions(+), 82 deletions(-) diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index 10fc660..d6d62b8 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -53,10 +53,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS if err != nil { log.Error(err, "Failed to build cloud client for cluster", "cluster", stackitCluster.Name) record.Warnf(stackitCluster, "InvalidCredentials", "Failed to build cloud client: %v", err) - stackitCluster.Status.Ready = false - return util.CredentialFailureResult( - &stackitCluster.Status.Conditions, - stackitCluster.Generation, + return clusterScope.CredentialFailureResult( err, credentialsRetryRequeueAfter, infrav1.ClusterCredentialsReadyCondition, @@ -72,10 +69,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS ) if err := availabilityzone.New(clusterScope).Reconcile(ctx); err != nil { - stackitCluster.Status.Ready = false - return util.CloudFailureResult( - &stackitCluster.Status.Conditions, - stackitCluster.Generation, + return clusterScope.CloudFailureResult( "AvailabilityZonesError", err, retryableErrorRequeueAfter, @@ -94,10 +88,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS "Failed to get network %s in region %s: %v", stackitCluster.Spec.Network.ID, stackitCluster.Spec.Region, err, ) - stackitCluster.Status.Ready = false - return util.CloudFailureResult( - &stackitCluster.Status.Conditions, - stackitCluster.Generation, + return clusterScope.CloudFailureResult( "NetworkNotFound", err, retryableErrorRequeueAfter, @@ -186,10 +177,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS stackitCluster, "FailedCreateLoadBalancer", "Failed to create API server load balancer: %v", err, ) - stackitCluster.Status.Ready = false - return util.CloudFailureResult( - &stackitCluster.Status.Conditions, - stackitCluster.Generation, + return clusterScope.CloudFailureResult( "LoadBalancerError", err, retryableErrorRequeueAfter, @@ -249,10 +237,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS stackitCluster, "FailedCreateBastion", "Failed to reconcile bastion: %v", err, ) - stackitCluster.Status.Ready = false - return util.CloudFailureResult( - &stackitCluster.Status.Conditions, - stackitCluster.Generation, + return clusterScope.CloudFailureResult( "BastionError", err, retryableErrorRequeueAfter, diff --git a/controller/stackitmachine_infrastructure.go b/controller/stackitmachine_infrastructure.go index 9cd5b66..85fceb7 100644 --- a/controller/stackitmachine_infrastructure.go +++ b/controller/stackitmachine_infrastructure.go @@ -80,9 +80,7 @@ func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineS if err != nil { log.Error(err, "Failed to build cloud client for machine", "machine", stackitMachine.Name) record.Warnf(stackitMachine, "InvalidCredentials", "Failed to build cloud client: %v", err) - return util.CredentialFailureResult( - &stackitMachine.Status.Conditions, - stackitMachine.Generation, + return machineScope.CredentialFailureResult( err, credentialsRetryRequeueAfter, infrav1.MachineCredentialsReadyCondition, @@ -120,15 +118,7 @@ func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineS stackitMachine.Name, stackitMachine.Spec.ImageID, stackitMachine.Spec.Network.ID, machineScope.StackitCluster.Spec.Region, err, ) - machineScope.SetNotReady( - "InstanceError", - err.Error(), - infrav1.MachineInstanceReadyCondition, - infrav1.MachineReadyCondition, - ) - return util.CloudFailureResult( - &stackitMachine.Status.Conditions, - stackitMachine.Generation, + return machineScope.CloudFailureResult( "InstanceError", err, retryableErrorRequeueAfter, @@ -162,10 +152,7 @@ func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineS stackitMachine, "FailedNodeSSHAccess", "Failed to configure bastion SSH access for server %s: %v", stackitMachine.Status.InstanceID, err, ) - machineScope.SetNotReady("BastionSSHAccessError", err.Error(), infrav1.MachineReadyCondition) - return util.CloudFailureResult( - &stackitMachine.Status.Conditions, - stackitMachine.Generation, + return machineScope.CloudFailureResult( "BastionSSHAccessError", err, retryableErrorRequeueAfter, @@ -227,9 +214,7 @@ func (r *StackitMachineReconciler) reconcileDelete(ctx context.Context, machineS log.Error(err, "Failed to build cloud client for machine deletion", "machine", stackitMachine.Name) record.Warnf(stackitMachine, "InvalidCredentials", "Failed to build cloud client for deletion: %v", err) - _, resultErr := util.CredentialFailureResult( - &stackitMachine.Status.Conditions, - stackitMachine.Generation, + _, resultErr := machineScope.CredentialFailureResult( err, credentialsRetryRequeueAfter, infrav1.MachineCredentialsReadyCondition, diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index aa82298..0ce0143 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -15,11 +15,14 @@ package scope import ( "context" + "errors" "net/netip" "slices" "strings" + "time" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" @@ -33,6 +36,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/util" iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" @@ -104,6 +108,48 @@ func (s *ClusterScope) SetNotReady(reason, message string, conditionTypes ...str s.SetConditions(metav1.ConditionFalse, reason, message, conditionTypes...) } +// CredentialFailureResult records the failure and decides how to retry. +// +// Invalid credentials need an operator to fix the Secret, so they requeue on a +// slow timer rather than returning an error and spinning through the controller +// backoff. The reconcile reads the Secret again, which is what picks up the fix. +func (s *ClusterScope) CredentialFailureResult( + err error, + requeueAfter time.Duration, + conditionTypes ...string, +) (ctrl.Result, error) { + s.SetNotReady("CredentialsInvalid", err.Error(), conditionTypes...) + return credentialFailureResult(err, requeueAfter) +} + +func (s *ClusterScope) CloudFailureResult( + reason string, + err error, + requeueAfter time.Duration, + returnError bool, + conditionTypes ...string, +) (ctrl.Result, error) { + s.SetNotReady(reason, err.Error(), conditionTypes...) + return cloudFailureResult(err, requeueAfter, returnError) +} + +func credentialFailureResult(err error, requeueAfter time.Duration) (ctrl.Result, error) { + if shared.IsUnauthorized(err) || shared.IsInvalidInput(err) || errors.Is(err, util.ErrCredentialsInvalid) { + return ctrl.Result{RequeueAfter: requeueAfter}, nil + } + return ctrl.Result{}, err +} + +func cloudFailureResult(err error, requeueAfter time.Duration, returnError bool) (ctrl.Result, error) { + if shared.IsRetryable(err) { + return ctrl.Result{RequeueAfter: requeueAfter}, nil + } + if returnError { + return ctrl.Result{}, err + } + return ctrl.Result{}, nil +} + func (s *ClusterScope) setAPIServerEndpoint(endpoint clusterv1.APIEndpoint) { s.StackitCluster.Spec.ControlPlaneEndpoint = endpoint s.StackitCluster.Status.APIServerEndpoint = endpoint diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index f8e3b91..7459ccf 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -11,11 +11,16 @@ You may obtain a copy of the License at package scope import ( + "errors" + "fmt" "reflect" "testing" + "time" + "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" + ctrl "sigs.k8s.io/controller-runtime" infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" @@ -23,6 +28,7 @@ import ( "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -274,3 +280,183 @@ func TestHasBastionStatus(t *testing.T) { t.Fatal("HasBastionStatus() = false when ServerID is set, want true") } } + +func TestClusterScopeCredentialFailureResult(t *testing.T) { + requeueAfter := time.Minute + + t.Run("requeues on invalid credentials and marks cluster not ready", func(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{ + ObjectMeta: metav1.ObjectMeta{Generation: 3}, + Status: infrav1.StackitClusterStatus{Ready: true}, + }, + } + err := fmt.Errorf("auth failed: %w", shared.ErrUnauthorized) + + result, gotErr := clusterScope.CredentialFailureResult( + err, + requeueAfter, + infrav1.ClusterCredentialsReadyCondition, + infrav1.ClusterReadyCondition, + ) + if gotErr != nil { + t.Fatalf("CredentialFailureResult() error = %v, want nil", gotErr) + } + if result != (ctrl.Result{RequeueAfter: requeueAfter}) { + t.Fatalf("CredentialFailureResult() result = %#v, want RequeueAfter=%v", result, requeueAfter) + } + if clusterScope.StackitCluster.Status.Ready { + t.Fatal("StackitCluster.Status.Ready = true, want false") + } + assertConditionFalse(t, clusterScope.StackitCluster.Status.Conditions, infrav1.ClusterCredentialsReadyCondition, "CredentialsInvalid", err.Error(), 3) + assertConditionFalse(t, clusterScope.StackitCluster.Status.Conditions, infrav1.ClusterReadyCondition, "CredentialsInvalid", err.Error(), 3) + }) + + t.Run("returns error on non-credential error", func(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{ + ObjectMeta: metav1.ObjectMeta{Generation: 2}, + }, + } + err := errors.New("k8s api error") + + result, gotErr := clusterScope.CredentialFailureResult(err, requeueAfter, infrav1.ClusterCredentialsReadyCondition) + if !errors.Is(gotErr, err) { + t.Fatalf("CredentialFailureResult() error = %v, want %v", gotErr, err) + } + if result != (ctrl.Result{}) { + t.Fatalf("CredentialFailureResult() result = %#v, want empty", result) + } + }) +} + +func TestClusterScopeCloudFailureResult(t *testing.T) { + requeueAfter := 5 * time.Second + + t.Run("requeues on retryable error and marks cluster not ready", func(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{ + ObjectMeta: metav1.ObjectMeta{Generation: 4}, + Status: infrav1.StackitClusterStatus{Ready: true}, + }, + } + err := fmt.Errorf("temporary failure: %w", shared.ErrTransient) + + result, gotErr := clusterScope.CloudFailureResult( + "NetworkNotFound", + err, + requeueAfter, + false, + infrav1.ClusterNetworkReadyCondition, + infrav1.ClusterReadyCondition, + ) + if gotErr != nil { + t.Fatalf("CloudFailureResult() error = %v, want nil", gotErr) + } + if result != (ctrl.Result{RequeueAfter: requeueAfter}) { + t.Fatalf("CloudFailureResult() result = %#v, want RequeueAfter=%v", result, requeueAfter) + } + if clusterScope.StackitCluster.Status.Ready { + t.Fatal("StackitCluster.Status.Ready = true, want false") + } + assertConditionFalse(t, clusterScope.StackitCluster.Status.Conditions, infrav1.ClusterNetworkReadyCondition, "NetworkNotFound", err.Error(), 4) + assertConditionFalse(t, clusterScope.StackitCluster.Status.Conditions, infrav1.ClusterReadyCondition, "NetworkNotFound", err.Error(), 4) + }) + + t.Run("returns nil on non-retryable error when returnError is false", func(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{ + ObjectMeta: metav1.ObjectMeta{Generation: 1}, + }, + } + err := fmt.Errorf("not found: %w", shared.ErrNotFound) + + result, gotErr := clusterScope.CloudFailureResult("NetworkNotFound", err, requeueAfter, false, infrav1.ClusterReadyCondition) + if gotErr != nil { + t.Fatalf("CloudFailureResult() error = %v, want nil", gotErr) + } + if result != (ctrl.Result{}) { + t.Fatalf("CloudFailureResult() result = %#v, want empty", result) + } + }) + + t.Run("returns error on non-retryable error when returnError is true", func(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{ + ObjectMeta: metav1.ObjectMeta{Generation: 1}, + }, + } + err := fmt.Errorf("invalid input: %w", shared.ErrInvalidInput) + + result, gotErr := clusterScope.CloudFailureResult("AvailabilityZonesError", err, requeueAfter, true, infrav1.ClusterReadyCondition) + if !errors.Is(gotErr, err) { + t.Fatalf("CloudFailureResult() error = %v, want %v", gotErr, err) + } + if result != (ctrl.Result{}) { + t.Fatalf("CloudFailureResult() result = %#v, want empty", result) + } + }) +} + +func TestMachineScopeFailureResults(t *testing.T) { + requeueAfter := 5 * time.Second + + machineScope := &MachineScope{ + StackitMachine: &infrav1.StackitMachine{ + ObjectMeta: metav1.ObjectMeta{Generation: 5}, + Status: infrav1.StackitMachineStatus{Ready: true}, + }, + } + credErr := fmt.Errorf("invalid secret: %w", util.ErrCredentialsInvalid) + result, gotErr := machineScope.CredentialFailureResult( + credErr, + requeueAfter, + infrav1.MachineCredentialsReadyCondition, + ) + if gotErr != nil || result != (ctrl.Result{RequeueAfter: requeueAfter}) { + t.Fatalf("CredentialFailureResult() = (%#v, %v), want (RequeueAfter=%v, nil)", result, gotErr, requeueAfter) + } + if machineScope.StackitMachine.Status.Ready { + t.Fatal("StackitMachine.Status.Ready = true after CredentialFailureResult, want false") + } + assertConditionFalse(t, machineScope.StackitMachine.Status.Conditions, infrav1.MachineCredentialsReadyCondition, "CredentialsInvalid", credErr.Error(), 5) + + machineScope.SetReady() + cloudErr := fmt.Errorf("create server rejected: %w", shared.ErrInvalidInput) + result, gotErr = machineScope.CloudFailureResult( + "InstanceError", + cloudErr, + requeueAfter, + true, + infrav1.MachineInstanceReadyCondition, + infrav1.MachineReadyCondition, + ) + if !errors.Is(gotErr, cloudErr) || result != (ctrl.Result{}) { + t.Fatalf("CloudFailureResult() = (%#v, %v), want (empty, %v)", result, gotErr, cloudErr) + } + if machineScope.StackitMachine.Status.Ready { + t.Fatal("StackitMachine.Status.Ready = true after CloudFailureResult, want false") + } + assertConditionFalse(t, machineScope.StackitMachine.Status.Conditions, infrav1.MachineInstanceReadyCondition, "InstanceError", cloudErr.Error(), 5) + assertConditionFalse(t, machineScope.StackitMachine.Status.Conditions, infrav1.MachineReadyCondition, "InstanceError", cloudErr.Error(), 5) +} + +func assertConditionFalse(t *testing.T, conditions []metav1.Condition, condType, wantReason, wantMessage string, wantGeneration int64) { + t.Helper() + cond := meta.FindStatusCondition(conditions, condType) + if cond == nil { + t.Fatalf("condition %q not found", condType) + } + if cond.Status != metav1.ConditionFalse { + t.Fatalf("condition %q status = %q, want %q", condType, cond.Status, metav1.ConditionFalse) + } + if cond.Reason != wantReason { + t.Fatalf("condition %q reason = %q, want %q", condType, cond.Reason, wantReason) + } + if cond.Message != wantMessage { + t.Fatalf("condition %q message = %q, want %q", condType, cond.Message, wantMessage) + } + if cond.ObservedGeneration != wantGeneration { + t.Fatalf("condition %q observedGeneration = %d, want %d", condType, cond.ObservedGeneration, wantGeneration) + } +} diff --git a/scope/machine_scope.go b/scope/machine_scope.go index 1adefc2..d86bff8 100644 --- a/scope/machine_scope.go +++ b/scope/machine_scope.go @@ -12,8 +12,10 @@ package scope import ( "context" + "time" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" @@ -89,6 +91,27 @@ func (s *MachineScope) SetNotReady(reason, message string, conditionTypes ...str s.SetConditions(metav1.ConditionFalse, reason, message, conditionTypes...) } +// CredentialFailureResult records the failure and decides how to retry. +func (s *MachineScope) CredentialFailureResult( + err error, + requeueAfter time.Duration, + conditionTypes ...string, +) (ctrl.Result, error) { + s.SetNotReady("CredentialsInvalid", err.Error(), conditionTypes...) + return credentialFailureResult(err, requeueAfter) +} + +func (s *MachineScope) CloudFailureResult( + reason string, + err error, + requeueAfter time.Duration, + returnError bool, + conditionTypes ...string, +) (ctrl.Result, error) { + s.SetNotReady(reason, err.Error(), conditionTypes...) + return cloudFailureResult(err, requeueAfter, returnError) +} + func (s *MachineScope) tags() map[string]string { return util.MachineTags( s.cluster.Name, diff --git a/util/conditions.go b/util/conditions.go index 0523fa8..4712ef2 100644 --- a/util/conditions.go +++ b/util/conditions.go @@ -11,13 +11,8 @@ You may obtain a copy of the License at package util import ( - "errors" - "time" - - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - ctrl "sigs.k8s.io/controller-runtime" ) func SetConditions( @@ -37,41 +32,3 @@ func SetConditions( }) } } - -// CredentialFailureResult records the failure and decides how to retry. -// -// Invalid credentials need an operator to fix the Secret, so they requeue on a -// slow timer rather than returning an error and spinning through the controller -// backoff. The reconcile reads the Secret again, which is what picks up the fix. -func CredentialFailureResult( - conditions *[]metav1.Condition, - generation int64, - err error, - requeueAfter time.Duration, - conditionTypes ...string, -) (ctrl.Result, error) { - SetConditions(conditions, generation, metav1.ConditionFalse, "CredentialsInvalid", err.Error(), conditionTypes...) - if shared.IsUnauthorized(err) || shared.IsInvalidInput(err) || errors.Is(err, ErrCredentialsInvalid) { - return ctrl.Result{RequeueAfter: requeueAfter}, nil - } - return ctrl.Result{}, err -} - -func CloudFailureResult( - conditions *[]metav1.Condition, - generation int64, - reason string, - err error, - requeueAfter time.Duration, - returnError bool, - conditionTypes ...string, -) (ctrl.Result, error) { - SetConditions(conditions, generation, metav1.ConditionFalse, reason, err.Error(), conditionTypes...) - if shared.IsRetryable(err) { - return ctrl.Result{RequeueAfter: requeueAfter}, nil - } - if returnError { - return ctrl.Result{}, err - } - return ctrl.Result{}, nil -} From 80e1ea1d631c57ab77597681eca6f298cd3d396e Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Tue, 17 Nov 2026 10:38:42 +0100 Subject: [PATCH 49/60] docs: update architecture.md to reflect scope and service packages --- docs/src/development/architecture.md | 70 +++++++++++++++------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/docs/src/development/architecture.md b/docs/src/development/architecture.md index 5d4d2bc..98cbdf0 100644 --- a/docs/src/development/architecture.md +++ b/docs/src/development/architecture.md @@ -2,11 +2,12 @@ ## Overview -The provider has four main areas: +The provider has five main areas: - API types and CRDs - Controllers -- Cloud abstraction layer +- Scopes (`ClusterScope` and `MachineScope`) +- Cloud client and resource services - clusterctl templates and release assets Key packages: @@ -14,16 +15,16 @@ Key packages: ```text api/v1alpha1/ Provider API types controller/ Reconciliation logic -cloud/ Cloud client interface and SDK implementation -cloud/fake/ In-memory fake for tests +scope/ Per-reconcile state, status patching, and service specs +cloud/ Cloud client factory, provider ID helpers, and tag-based cleanup +cloud/services/ Resource services (availabilityzone, network, loadbalancer, publicip, securitygroup, server, shared) +cloud/fake/ In-memory fake HTTP transport and client for tests util/ Shared helpers templates/ clusterctl templates config/ Kubebuilder manifests ``` -The controllers do not call the STACKIT SDK directly. They use the cloud client -interface, which keeps reconciliation testable and allows fake-client envtest -coverage. +Controllers construct a `ClusterScope` or `MachineScope` for each reconciliation pass and delegate cloud operations to the resource services under `cloud/services/`. Both the real `SDKClient` and the `fake.Client` implement `cloud.Client` (exposing the configured project ID, region, and STACKIT IaaS / Load Balancer SDK clients), keeping reconciliation testable with envtest and in-memory HTTP fakes. ## Cluster API Contract @@ -49,49 +50,54 @@ Important contract behavior: infrastructure cluster contract when the provider manages the API server load balancer endpoint. -## Controllers +## Controllers and Scopes + +`ClusterScope` (`scope/cluster_scope.go`) and `MachineScope` (`scope/machine_scope.go`) hold the Kubernetes objects and cloud client for a single reconcile pass, patch status and conditions on close, and translate Kubernetes spec/status fields into service spec structs (`AvailabilityZoneSpec`, `NetworkSpec`, `LoadBalancerSpec`, `PublicIPSpec`, `SecurityGroupSpecs`, `ServerSpec`). `StackitClusterReconciler` is responsible for: -- Reading credentials -- Looking up the configured network -- Managing the optional API server load balancer -- Maintaining its target pool from the control-plane machines of the cluster -- Publishing failure domains +- Reading credentials and building the cloud client +- Reconciling availability zones and publishing failure domains +- Reconciling (validating) the configured network +- Reconciling the optional API server load balancer and its control-plane target pool +- Reconciling the optional bastion host (server, security groups, and public IP) - Updating readiness and contract status -- Cleaning up provider-managed load balancers on deletion +- Cleaning up provider-managed bastion and load balancer resources on deletion `StackitMachineReconciler` is responsible for: +- Validating the machine's availability zone against cluster failure domains - Waiting for bootstrap data -- Creating STACKIT servers -- Setting provider IDs and addresses -- Deleting servers on teardown +- Reconciling the STACKIT server instance, provider ID, and addresses +- Reconciling node SSH security group attachment when the bastion is enabled +- Deleting the server on teardown Reconciliation must be idempotent. Re-running the same reconcile loop should be safe and should not create duplicate cloud resources. -## Cloud Client +## Cloud Client and Services + +`cloud.Client` provides the project ID, region, and configured STACKIT IaaS (`*iaas.APIClient`) and Load Balancer (`*lb.APIClient`) SDK clients. -The cloud client abstraction hides STACKIT SDK details from controllers. +Cloud resource management is organized into service packages under `cloud/services/`: -It covers: +- `availabilityzone`: Lists availability zones (`Reconcile`) +- `network`: Looks up and validates the cluster network (`Reconcile`) +- `loadbalancer`: Creates, adopts by tags, updates target pools, and deletes the API server load balancer (`Reconcile`, `Delete`) +- `publicip`: Creates, adopts by tags, associates/disassociates, and deletes public IPs (`Reconcile`, `Delete`) +- `securitygroup`: Creates, adopts by tags, reconciles rules and server attachments, and deletes security groups (`Reconcile`, `Delete`) +- `server`: Creates, adopts by tags, inspects state/addresses, and deletes servers (`Reconcile`, `Delete`) +- `shared`: Sentinel errors (`ErrNotFound`, `ErrUnauthorized`, `ErrInvalidInput`, `ErrConflict`, `ErrTransient`), SDK error classification, and tag lookup/deletion helpers -- Server create, get, list, and delete -- Network lookup -- Load balancer create, list, delete, and target-pool updates -- Provider ID helpers -- Error classification +`cloud.CleanupByTags` uses the `publicip`, `loadbalancer`, `server`, and `securitygroup` services to delete all provider-managed resources matching a tag selector without requiring Kubernetes objects. -Controllers should handle classified errors differently: +Controllers and scopes handle classified errors from `cloud/services/shared` differently: -- transient errors should be retried -- terminal validation errors should set clear conditions -- not-found errors during deletion should be treated as successful cleanup where - appropriate +- transient and conflict errors (`shared.IsRetryable`) trigger a requeue +- terminal validation or credential errors set clear conditions and avoid tight retry loops +- not-found errors during deletion are treated as successful cleanup -Tests use an in-memory fake cloud client for deterministic reconciliation -coverage. SDK integration tests are opt-in and require real STACKIT credentials. +Tests use `cloud/fake.Client` (which backs the STACKIT SDK clients with an in-memory HTTP handler) for deterministic unit and envtest coverage. SDK integration tests are opt-in and require real STACKIT credentials. ## STACKIT ProviderID Compatibility From f5f44aa8a67294b77e783e846ea2dc6cb86f1c95 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Tue, 17 Nov 2026 10:46:42 +0100 Subject: [PATCH 50/60] docs: refine architecture.md formatting and spec struct name --- docs/src/development/architecture.md | 60 +++++++++++++++++++--------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/docs/src/development/architecture.md b/docs/src/development/architecture.md index 98cbdf0..d486582 100644 --- a/docs/src/development/architecture.md +++ b/docs/src/development/architecture.md @@ -24,7 +24,12 @@ templates/ clusterctl templates config/ Kubebuilder manifests ``` -Controllers construct a `ClusterScope` or `MachineScope` for each reconciliation pass and delegate cloud operations to the resource services under `cloud/services/`. Both the real `SDKClient` and the `fake.Client` implement `cloud.Client` (exposing the configured project ID, region, and STACKIT IaaS / Load Balancer SDK clients), keeping reconciliation testable with envtest and in-memory HTTP fakes. +Controllers construct a `ClusterScope` or `MachineScope` for each +reconciliation pass and delegate cloud operations to the resource services +under `cloud/services/`. Both the real `SDKClient` and `fake.Client` implement +`cloud.Client` (exposing the configured project ID, region, and STACKIT IaaS / +Load Balancer SDK clients), keeping reconciliation testable with envtest and +in-memory HTTP fakes. ## Cluster API Contract @@ -52,14 +57,20 @@ balancer endpoint. ## Controllers and Scopes -`ClusterScope` (`scope/cluster_scope.go`) and `MachineScope` (`scope/machine_scope.go`) hold the Kubernetes objects and cloud client for a single reconcile pass, patch status and conditions on close, and translate Kubernetes spec/status fields into service spec structs (`AvailabilityZoneSpec`, `NetworkSpec`, `LoadBalancerSpec`, `PublicIPSpec`, `SecurityGroupSpecs`, `ServerSpec`). +`ClusterScope` (`scope/cluster_scope.go`) and `MachineScope` +(`scope/machine_scope.go`) hold the Kubernetes objects and cloud client for a +single reconcile pass, patch status and conditions on close, and translate +Kubernetes spec/status fields into service spec structs +(`AvailabilityZoneSpec`, `NetworkSpec`, `LoadBalancerSpec`, `PublicIPSpec`, +`SecurityGroupSpec`, `ServerSpec`). `StackitClusterReconciler` is responsible for: - Reading credentials and building the cloud client - Reconciling availability zones and publishing failure domains - Reconciling (validating) the configured network -- Reconciling the optional API server load balancer and its control-plane target pool +- Reconciling the optional API server load balancer and its control-plane + target pool - Reconciling the optional bastion host (server, security groups, and public IP) - Updating readiness and contract status - Cleaning up provider-managed bastion and load balancer resources on deletion @@ -77,27 +88,40 @@ safe and should not create duplicate cloud resources. ## Cloud Client and Services -`cloud.Client` provides the project ID, region, and configured STACKIT IaaS (`*iaas.APIClient`) and Load Balancer (`*lb.APIClient`) SDK clients. +`cloud.Client` provides the project ID, region, and configured STACKIT IaaS +(`*iaas.APIClient`) and Load Balancer (`*lb.APIClient`) SDK clients. -Cloud resource management is organized into service packages under `cloud/services/`: +Cloud resource management is organized into service packages under +`cloud/services/`: - `availabilityzone`: Lists availability zones (`Reconcile`) - `network`: Looks up and validates the cluster network (`Reconcile`) -- `loadbalancer`: Creates, adopts by tags, updates target pools, and deletes the API server load balancer (`Reconcile`, `Delete`) -- `publicip`: Creates, adopts by tags, associates/disassociates, and deletes public IPs (`Reconcile`, `Delete`) -- `securitygroup`: Creates, adopts by tags, reconciles rules and server attachments, and deletes security groups (`Reconcile`, `Delete`) -- `server`: Creates, adopts by tags, inspects state/addresses, and deletes servers (`Reconcile`, `Delete`) -- `shared`: Sentinel errors (`ErrNotFound`, `ErrUnauthorized`, `ErrInvalidInput`, `ErrConflict`, `ErrTransient`), SDK error classification, and tag lookup/deletion helpers - -`cloud.CleanupByTags` uses the `publicip`, `loadbalancer`, `server`, and `securitygroup` services to delete all provider-managed resources matching a tag selector without requiring Kubernetes objects. - -Controllers and scopes handle classified errors from `cloud/services/shared` differently: - -- transient and conflict errors (`shared.IsRetryable`) trigger a requeue -- terminal validation or credential errors set clear conditions and avoid tight retry loops +- `loadbalancer`: Creates, adopts by tags, updates target pools, and deletes + the API server load balancer (`Reconcile`, `Delete`) +- `publicip`: Creates, adopts by tags, associates/disassociates, and deletes + public IPs (`Reconcile`, `Delete`) +- `securitygroup`: Creates, adopts by tags, reconciles rules and server + attachments, and deletes security groups (`Reconcile`, `Delete`) +- `server`: Creates, adopts by tags, inspects state/addresses, and deletes + servers (`Reconcile`, `Delete`) +- `shared`: Sentinel errors (`ErrNotFound`, `ErrUnauthorized`, + `ErrInvalidInput`, `ErrConflict`, `ErrTransient`), SDK error classification, + and tag lookup/deletion helpers + +`cloud.CleanupByTags` uses the `publicip`, `loadbalancer`, `server`, and +`securitygroup` services to delete all provider-managed resources matching a +tag selector without requiring Kubernetes objects. + +Controllers and scopes handle classified errors from `cloud/services/shared` +differently: + +- transient and conflict errors (`shared.IsRetryable`) trigger a timed requeue +- terminal validation or credential errors set clear conditions - not-found errors during deletion are treated as successful cleanup -Tests use `cloud/fake.Client` (which backs the STACKIT SDK clients with an in-memory HTTP handler) for deterministic unit and envtest coverage. SDK integration tests are opt-in and require real STACKIT credentials. +Tests use `cloud/fake.Client` (which backs the STACKIT SDK clients with an +in-memory HTTP handler) for deterministic unit and envtest coverage. SDK +integration tests are opt-in and require real STACKIT credentials. ## STACKIT ProviderID Compatibility From 85d4a30259e6c25019bd11e145e2e99784c8cf07 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Tue, 17 Nov 2026 16:39:34 +0100 Subject: [PATCH 51/60] refactor: unify state clearing callbacks across server and loadbalancer services --- cloud/services/loadbalancer/reconcile.go | 3 + cloud/services/loadbalancer/reconcile_test.go | 15 +++- cloud/services/server/reconcile.go | 4 +- cloud/services/server/reconcile_test.go | 21 +++-- cloud/services/server/service.go | 1 - scope/cluster_scope.go | 36 ++++---- scope/cluster_scope_test.go | 83 +++++++++++++++++++ scope/machine_scope.go | 28 +++---- 8 files changed, 148 insertions(+), 43 deletions(-) diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index ba603d7..ee2b5fb 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -71,6 +71,9 @@ func (s *Service) Delete(ctx context.Context) error { if spec.SetID != nil { spec.SetID("") } + if spec.SetAddress != nil { + spec.SetAddress("", 0) + } return nil } diff --git a/cloud/services/loadbalancer/reconcile_test.go b/cloud/services/loadbalancer/reconcile_test.go index c1f2e90..c4f48f0 100644 --- a/cloud/services/loadbalancer/reconcile_test.go +++ b/cloud/services/loadbalancer/reconcile_test.go @@ -558,16 +558,22 @@ func TestServiceReconcileValidation(t *testing.T) { } func TestServiceDelete(t *testing.T) { - t.Run("deletes load balancer using ID when provided and clears ID", func(t *testing.T) { + t.Run("deletes load balancer using ID when provided and clears ID and address", func(t *testing.T) { var ( - deletedID string - clearedID = "not-cleared" + deletedID string + clearedID = "not-cleared" + clearedIP = "not-cleared" + clearedPort = int32(6443) ) spec := loadbalancer.LoadBalancerSpec{ ID: testLBName, SetID: func(id string) { clearedID = id }, + SetAddress: func(ip string, port int32) { + clearedIP = ip + clearedPort = port + }, } svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -588,6 +594,9 @@ func TestServiceDelete(t *testing.T) { if clearedID != "" { t.Fatalf("clearedID = %q, want empty", clearedID) } + if clearedIP != "" || clearedPort != 0 { + t.Fatalf("cleared address = (%q, %d), want (\"\", 0)", clearedIP, clearedPort) + } }) t.Run("falls back to tag lookup when ID is empty and deletes all matching load balancers", func(t *testing.T) { diff --git a/cloud/services/server/reconcile.go b/cloud/services/server/reconcile.go index 51504a7..5dd1926 100644 --- a/cloud/services/server/reconcile.go +++ b/cloud/services/server/reconcile.go @@ -55,8 +55,8 @@ func (s *Service) Delete(ctx context.Context) error { if err != nil { return err } - if spec.ClearServer != nil { - spec.ClearServer() + if spec.SetServer != nil { + spec.SetServer(nil, false) } return nil } diff --git a/cloud/services/server/reconcile_test.go b/cloud/services/server/reconcile_test.go index e5468ea..90a676a 100644 --- a/cloud/services/server/reconcile_test.go +++ b/cloud/services/server/reconcile_test.go @@ -516,15 +516,19 @@ func TestServiceReconcileValidation(t *testing.T) { } func TestServiceDelete(t *testing.T) { - t.Run("deletes server by ID when provided and calls ClearServer", func(t *testing.T) { + t.Run("deletes server by ID when provided and clears state via SetServer", func(t *testing.T) { var ( - deletedID string - cleared bool + deletedID string + setServerCalls int + clearedServer *server.Server + clearedCreated = true ) spec := server.ServerSpec{ ID: testServerID, - ClearServer: func() { - cleared = true + SetServer: func(srv *server.Server, created bool) { + setServerCalls++ + clearedServer = srv + clearedCreated = created }, } @@ -543,8 +547,11 @@ func TestServiceDelete(t *testing.T) { if deletedID != testServerID { t.Fatalf("deletedID = %q, want %q", deletedID, testServerID) } - if !cleared { - t.Fatal("ClearServer was not called") + if setServerCalls != 1 { + t.Fatalf("SetServer calls = %d, want 1", setServerCalls) + } + if clearedServer != nil || clearedCreated { + t.Fatalf("SetServer called with (%#v, %v), want (nil, false)", clearedServer, clearedCreated) } }) diff --git a/cloud/services/server/service.go b/cloud/services/server/service.go index 3bf15b2..5fdcd1b 100644 --- a/cloud/services/server/service.go +++ b/cloud/services/server/service.go @@ -64,7 +64,6 @@ type ServerSpec struct { RootVolume RootVolume DisallowRecreate bool SetServer func(server *Server, created bool) - ClearServer func() } // Scope is an interface that provides scope data needed for server reconciliation. diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index 0ce0143..1f30e0c 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -169,6 +169,14 @@ func (s *ClusterScope) BastionServerCreated() bool { } func (s *ClusterScope) setBastionServer(srv *server.Server, created bool) { + if srv == nil { + s.bastionServerCreated = false + s.bastionServerState = "" + s.bastionServerPowerStatus = "" + s.StackitCluster.Status.Bastion.ServerID = "" + s.StackitCluster.Status.Bastion.CloudInitHash = "" + return + } s.bastionServerCreated = created s.bastionServerState = srv.State s.bastionServerPowerStatus = srv.PowerStatus @@ -178,11 +186,6 @@ func (s *ClusterScope) setBastionServer(srv *server.Server, created bool) { } } -func (s *ClusterScope) clearBastionServer() { - s.StackitCluster.Status.Bastion.ServerID = "" - s.StackitCluster.Status.Bastion.CloudInitHash = "" -} - func (s *ClusterScope) ServerSpec() server.ServerSpec { return server.ServerSpec{ ID: s.StackitCluster.Status.Bastion.ServerID, @@ -197,7 +200,6 @@ func (s *ClusterScope) ServerSpec() server.ServerSpec { Tags: s.bastionTags(), RootVolume: rootVolumeFromSpec(s.StackitCluster.Spec.Bastion.RootVolume), SetServer: s.setBastionServer, - ClearServer: s.clearBastionServer, } } @@ -282,6 +284,17 @@ func (s *ClusterScope) setAPIServerLoadBalancerID(id string) { s.StackitCluster.Status.APIServerLoadBalancerID = id } +func (s *ClusterScope) setAPIServerLoadBalancerAddress(ip string, port int32) { + if ip == "" { + s.StackitCluster.Status.APIServerEndpoint = clusterv1.APIEndpoint{} + return + } + s.setAPIServerEndpoint(clusterv1.APIEndpoint{ + Host: ip, + Port: port, + }) +} + func (s *ClusterScope) SetAPIServerLoadBalancerTargets(targets, seedTargets []loadbalancer.Target) { s.loadBalancerTargets = targets s.loadBalancerSeedTargets = seedTargets @@ -297,16 +310,7 @@ func (s *ClusterScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec { Targets: s.loadBalancerTargets, SeedTargets: s.loadBalancerSeedTargets, SetID: s.setAPIServerLoadBalancerID, - SetAddress: func(ip string, port int32) { - if ip == "" { - s.StackitCluster.Status.APIServerEndpoint = clusterv1.APIEndpoint{} - return - } - s.setAPIServerEndpoint(clusterv1.APIEndpoint{ - Host: ip, - Port: port, - }) - }, + SetAddress: s.setAPIServerLoadBalancerAddress, } } diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index 7459ccf..6adb122 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -121,6 +121,89 @@ func TestServerSpecSetServerPreservesCloudInitHashOnAdoptedServer(t *testing.T) if got := clusterScope.StackitCluster.Status.Bastion.CloudInitHash; got != "new-hash" { t.Fatalf("Status.Bastion.CloudInitHash on created server = %q, want %q", got, "new-hash") } + + spec.SetServer(nil, false) + if clusterScope.BastionServerCreated() { + t.Fatal("BastionServerCreated() = true after SetServer(nil, false), want false") + } + if state, powerStatus := clusterScope.BastionServerState(); state != "" || powerStatus != "" { + t.Fatalf("BastionServerState() after SetServer(nil, false) = (%q, %q), want empty", state, powerStatus) + } + if got := clusterScope.StackitCluster.Status.Bastion.ServerID; got != "" { + t.Fatalf("Status.Bastion.ServerID after SetServer(nil, false) = %q, want empty", got) + } + if got := clusterScope.StackitCluster.Status.Bastion.CloudInitHash; got != "" { + t.Fatalf("Status.Bastion.CloudInitHash after SetServer(nil, false) = %q, want empty", got) + } +} + +func TestLoadBalancerSpecSetIDAndSetAddressClearsOnEmpty(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{}, + } + + spec := clusterScope.LoadBalancerSpec() + spec.SetID("lb-1") + spec.SetAddress("203.0.113.10", 6443) + + if clusterScope.StackitCluster.Status.APIServerLoadBalancerID != "lb-1" { + t.Fatalf("Status.APIServerLoadBalancerID = %q, want lb-1", clusterScope.StackitCluster.Status.APIServerLoadBalancerID) + } + wantEndpoint := clusterv1.APIEndpoint{Host: "203.0.113.10", Port: 6443} + if clusterScope.StackitCluster.Spec.ControlPlaneEndpoint != wantEndpoint { + t.Fatalf("Spec.ControlPlaneEndpoint = %#v, want %#v", clusterScope.StackitCluster.Spec.ControlPlaneEndpoint, wantEndpoint) + } + if clusterScope.StackitCluster.Status.APIServerEndpoint != wantEndpoint { + t.Fatalf("Status.APIServerEndpoint = %#v, want %#v", clusterScope.StackitCluster.Status.APIServerEndpoint, wantEndpoint) + } + + spec.SetID("") + spec.SetAddress("", 0) + if clusterScope.StackitCluster.Status.APIServerLoadBalancerID != "" { + t.Fatalf("Status.APIServerLoadBalancerID after clear = %q, want empty", clusterScope.StackitCluster.Status.APIServerLoadBalancerID) + } + if clusterScope.StackitCluster.Status.APIServerEndpoint != (clusterv1.APIEndpoint{}) { + t.Fatalf("Status.APIServerEndpoint after clear = %#v, want empty", clusterScope.StackitCluster.Status.APIServerEndpoint) + } + if clusterScope.StackitCluster.Spec.ControlPlaneEndpoint != wantEndpoint { + t.Fatalf("Spec.ControlPlaneEndpoint after clear = %#v, want preserved %#v", clusterScope.StackitCluster.Spec.ControlPlaneEndpoint, wantEndpoint) + } +} + +func TestMachineScopeServerSpecSetServerClearsOnNil(t *testing.T) { + machineScope := &MachineScope{ + cluster: &clusterv1.Cluster{}, + Machine: &clusterv1.Machine{}, + StackitCluster: &infrav1.StackitCluster{}, + StackitMachine: &infrav1.StackitMachine{}, + } + + spec := machineScope.ServerSpec() + spec.SetServer(&server.Server{ + ID: "srv-1", + State: "ACTIVE", + PowerStatus: "RUNNING", + AvailabilityZone: "eu01-1", + Addresses: []server.Address{{Type: "InternalIP", Address: "10.0.0.5"}}, + }, true) + + if !machineScope.InstanceCreated() || machineScope.InstancePowerStatus() != "RUNNING" { + t.Fatalf("InstanceCreated=%v, InstancePowerStatus=%q", machineScope.InstanceCreated(), machineScope.InstancePowerStatus()) + } + if machineScope.StackitMachine.Status.InstanceID != "srv-1" { + t.Fatalf("Status.InstanceID = %q, want srv-1", machineScope.StackitMachine.Status.InstanceID) + } + + spec.SetServer(nil, false) + if machineScope.InstanceCreated() || machineScope.InstancePowerStatus() != "" { + t.Fatalf("after SetServer(nil, false): InstanceCreated=%v, InstancePowerStatus=%q, want false and empty", machineScope.InstanceCreated(), machineScope.InstancePowerStatus()) + } + if machineScope.StackitMachine.Status.InstanceID != "" || + machineScope.StackitMachine.Status.InstanceState != "" || + machineScope.StackitMachine.Status.AvailabilityZone != "" || + len(machineScope.StackitMachine.Status.Addresses) != 0 { + t.Fatalf("Status after SetServer(nil, false) = %#v, want cleared instance fields", machineScope.StackitMachine.Status) + } } func TestNodeSSHSecurityGroupSpecIncludesAdditionalLabelsInServerSelector(t *testing.T) { diff --git a/scope/machine_scope.go b/scope/machine_scope.go index d86bff8..0531f24 100644 --- a/scope/machine_scope.go +++ b/scope/machine_scope.go @@ -122,7 +122,19 @@ func (s *MachineScope) tags() map[string]string { ) } -func (s *MachineScope) setInstance(srv *server.Server) { +func (s *MachineScope) setInstance(srv *server.Server, created bool) { + if srv == nil { + s.instanceCreated = false + s.instancePowerStatus = "" + s.StackitMachine.Status.Addresses = nil + s.StackitMachine.Status.InstanceID = "" + s.StackitMachine.Status.InstanceState = "" + s.StackitMachine.Status.AvailabilityZone = "" + return + } + s.instanceCreated = created + s.instancePowerStatus = srv.PowerStatus + s.StackitMachine.Status.Addresses = machineAddressesFromServer(srv.Addresses) providerID := cloud.NewProviderID(srv.ID) s.StackitMachine.Status.InstanceID = srv.ID s.StackitMachine.Status.InstanceState = srv.State @@ -132,12 +144,6 @@ func (s *MachineScope) setInstance(srv *server.Server) { s.StackitMachine.Status.Initialization.Provisioned = true } -func (s *MachineScope) clearInstance() { - s.StackitMachine.Status.InstanceID = "" - s.StackitMachine.Status.InstanceState = "" - s.StackitMachine.Status.AvailabilityZone = "" -} - // availabilityZone returns the resolved availability zone for the machine. // Following Cluster API conventions: // 1. If Machine.Spec.FailureDomain is set (e.g. by KCP spreading control plane nodes or MachineDeployment), @@ -206,13 +212,7 @@ func (s *MachineScope) ServerSpec() server.ServerSpec { Tags: s.tags(), RootVolume: rootVolumeFromSpec(s.StackitMachine.Spec.RootVolume), DisallowRecreate: s.StackitMachine.Status.Initialization.Provisioned, - SetServer: func(srv *server.Server, created bool) { - s.instanceCreated = created - s.instancePowerStatus = srv.PowerStatus - s.StackitMachine.Status.Addresses = machineAddressesFromServer(srv.Addresses) - s.setInstance(srv) - }, - ClearServer: s.clearInstance, + SetServer: s.setInstance, } } From 639a463f6bc29443e22b94ccf5f1009b7c6e12f6 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Tue, 17 Nov 2026 17:05:13 +0100 Subject: [PATCH 52/60] refactor: use scope condition and credential failure helpers across controllers --- controller/stackitcluster_controller.go | 4 +-- controller/stackitcluster_controller_test.go | 35 ++++++++++++++++++++ controller/stackitcluster_infrastructure.go | 18 +++------- controller/stackitmachine_controller.go | 4 +-- scope/cluster_scope.go | 33 +++++++++++++++++- scope/cluster_scope_test.go | 35 ++++++++++++++++++-- scope/machine_scope.go | 6 +++- util/conditions.go | 34 ------------------- util/reconcile.go | 9 ----- 9 files changed, 114 insertions(+), 64 deletions(-) delete mode 100644 util/conditions.go diff --git a/controller/stackitcluster_controller.go b/controller/stackitcluster_controller.go index 8b35147..4c7f637 100644 --- a/controller/stackitcluster_controller.go +++ b/controller/stackitcluster_controller.go @@ -95,10 +95,10 @@ func (r *StackitClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reque }() if paused, message := util.ReconciliationPaused(cluster, stackitCluster); paused { - util.SetPausedCondition(&stackitCluster.Status.Conditions, stackitCluster.Generation, true, message) + clusterScope.SetPausedCondition(true, message) return ctrl.Result{}, nil } - util.SetPausedCondition(&stackitCluster.Status.Conditions, stackitCluster.Generation, false, "") + clusterScope.SetPausedCondition(false, "") if !stackitCluster.DeletionTimestamp.IsZero() { return r.reconcileDelete(ctx, clusterScope) diff --git a/controller/stackitcluster_controller_test.go b/controller/stackitcluster_controller_test.go index 7a0e0d9..f0c620a 100644 --- a/controller/stackitcluster_controller_test.go +++ b/controller/stackitcluster_controller_test.go @@ -334,6 +334,41 @@ var _ = Describe("StackitCluster Controller", func() { }).Should(BeTrue(), "cluster stayed in Terminating because the finalizer was never removed") }) + It("requeues on invalid credentials during deletion and keeps the finalizer until corrected", func() { + _, err := reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + Expect(fakeCloud.LoadBalancerCount()).To(Equal(1)) + + got := &infrav1.StackitCluster{} + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + Expect(k8sClient.Delete(ctx, got)).To(Succeed()) + + reconciler.CloudClientFactory = func(context.Context, cloud.Credentials) (cloud.Client, error) { + return nil, fmt.Errorf("authenticate: %w", shared.ErrUnauthorized) + } + + result, err := reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + Expect(result.RequeueAfter).To(Equal(credentialsRetryRequeueAfter)) + + Expect(k8sClient.Get(ctx, stackitKey, got)).To(Succeed()) + Expect(got.Status.Ready).To(BeFalse()) + Expect(got.Finalizers).To(ContainElement(infrav1.ClusterFinalizer)) + expectCondition(got.Status.Conditions, infrav1.ClusterCredentialsReadyCondition, metav1.ConditionFalse, "CredentialsInvalid") + + By("correcting the credentials so deletion succeeds") + reconciler.CloudClientFactory = func(context.Context, cloud.Credentials) (cloud.Client, error) { + return fakeCloud, nil + } + + _, err = reconciler.Reconcile(ctx, request) + Expect(err).NotTo(HaveOccurred()) + Expect(fakeCloud.LoadBalancerCount()).To(Equal(0)) + Eventually(func() bool { + return apierrors.IsNotFound(k8sClient.Get(ctx, stackitKey, &infrav1.StackitCluster{})) + }).Should(BeTrue()) + }) + It("tears the bastion down when disabled even if its status was never persisted", func() { // With the status lost, a status-gated teardown would report the bastion // as disabled while leaving port 22 open. diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index d6d62b8..85684e8 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -116,9 +116,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS collections.ActiveMachines, ) if err != nil { - util.SetConditions( - &stackitCluster.Status.Conditions, - stackitCluster.Generation, + clusterScope.SetConditions( metav1.ConditionFalse, "MachineListError", err.Error(), @@ -156,9 +154,7 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS } // Scoped to the load balancer condition: failing the cluster would // stop the machine reconciler from replacing the broken machine. - util.SetConditions( - &stackitCluster.Status.Conditions, - stackitCluster.Generation, + clusterScope.SetConditions( metav1.ConditionFalse, "LoadBalancerTargetError", err.Error(), @@ -296,15 +292,11 @@ func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterS log.Error(err, "Failed to build cloud client for cluster deletion", "cluster", stackitCluster.Name) record.Warnf(stackitCluster, "InvalidCredentials", "Failed to build cloud client for deletion: %v", err) - util.SetConditions( - &stackitCluster.Status.Conditions, - stackitCluster.Generation, - metav1.ConditionFalse, - "CredentialsInvalid", - err.Error(), + return clusterScope.CredentialFailureResult( + err, + credentialsRetryRequeueAfter, infrav1.ClusterCredentialsReadyCondition, ) - return ctrl.Result{}, err } clusterScope.SetCloudClient(cloudClient) loadBalancerID := stackitCluster.Status.APIServerLoadBalancerID diff --git a/controller/stackitmachine_controller.go b/controller/stackitmachine_controller.go index cf71067..29c60d8 100644 --- a/controller/stackitmachine_controller.go +++ b/controller/stackitmachine_controller.go @@ -104,10 +104,10 @@ func (r *StackitMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque }() if paused, message := util.ReconciliationPaused(cluster, stackitMachine); paused { - util.SetPausedCondition(&stackitMachine.Status.Conditions, stackitMachine.Generation, true, message) + machineScope.SetPausedCondition(true, message) return ctrl.Result{}, nil } - util.SetPausedCondition(&stackitMachine.Status.Conditions, stackitMachine.Generation, false, "") + machineScope.SetPausedCondition(false, "") if !stackitMachine.DeletionTimestamp.IsZero() { return ctrl.Result{}, r.reconcileDelete(ctx, machineScope) diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index 1f30e0c..27cc6ce 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -21,6 +21,7 @@ import ( "strings" "time" + "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -94,7 +95,37 @@ func (s *ClusterScope) PatchObject(ctx context.Context) error { } func (s *ClusterScope) SetConditions(status metav1.ConditionStatus, reason, message string, conditionTypes ...string) { - util.SetConditions(&s.StackitCluster.Status.Conditions, s.StackitCluster.Generation, status, reason, message, conditionTypes...) + setConditions(&s.StackitCluster.Status.Conditions, s.StackitCluster.Generation, status, reason, message, conditionTypes...) +} + +func (s *ClusterScope) SetPausedCondition(paused bool, message string) { + setPausedCondition(&s.StackitCluster.Status.Conditions, s.StackitCluster.Generation, paused, message) +} + +func setConditions( + conditions *[]metav1.Condition, + generation int64, + status metav1.ConditionStatus, + reason, message string, + conditionTypes ...string, +) { + for _, conditionType := range conditionTypes { + meta.SetStatusCondition(conditions, metav1.Condition{ + Type: conditionType, + Status: status, + Reason: reason, + Message: message, + ObservedGeneration: generation, + }) + } +} + +func setPausedCondition(conditions *[]metav1.Condition, generation int64, paused bool, message string) { + if paused { + setConditions(conditions, generation, metav1.ConditionTrue, clusterv1.PausedReason, message, clusterv1.PausedCondition) + return + } + setConditions(conditions, generation, metav1.ConditionFalse, clusterv1.NotPausedReason, "", clusterv1.PausedCondition) } func (s *ClusterScope) SetReady() { diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index 6adb122..65d6de6 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -524,14 +524,45 @@ func TestMachineScopeFailureResults(t *testing.T) { assertConditionFalse(t, machineScope.StackitMachine.Status.Conditions, infrav1.MachineReadyCondition, "InstanceError", cloudErr.Error(), 5) } +func TestScopeSetPausedCondition(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{ + ObjectMeta: metav1.ObjectMeta{Generation: 2}, + }, + } + + clusterScope.SetPausedCondition(true, "Cluster spec.paused is set to true") + assertCondition(t, clusterScope.StackitCluster.Status.Conditions, clusterv1.PausedCondition, metav1.ConditionTrue, clusterv1.PausedReason, "Cluster spec.paused is set to true", 2) + + clusterScope.SetPausedCondition(false, "") + assertCondition(t, clusterScope.StackitCluster.Status.Conditions, clusterv1.PausedCondition, metav1.ConditionFalse, clusterv1.NotPausedReason, "", 2) + + machineScope := &MachineScope{ + StackitMachine: &infrav1.StackitMachine{ + ObjectMeta: metav1.ObjectMeta{Generation: 6}, + }, + } + + machineScope.SetPausedCondition(true, "object has the cluster.x-k8s.io/paused annotation") + assertCondition(t, machineScope.StackitMachine.Status.Conditions, clusterv1.PausedCondition, metav1.ConditionTrue, clusterv1.PausedReason, "object has the cluster.x-k8s.io/paused annotation", 6) + + machineScope.SetPausedCondition(false, "") + assertCondition(t, machineScope.StackitMachine.Status.Conditions, clusterv1.PausedCondition, metav1.ConditionFalse, clusterv1.NotPausedReason, "", 6) +} + func assertConditionFalse(t *testing.T, conditions []metav1.Condition, condType, wantReason, wantMessage string, wantGeneration int64) { + t.Helper() + assertCondition(t, conditions, condType, metav1.ConditionFalse, wantReason, wantMessage, wantGeneration) +} + +func assertCondition(t *testing.T, conditions []metav1.Condition, condType string, wantStatus metav1.ConditionStatus, wantReason, wantMessage string, wantGeneration int64) { t.Helper() cond := meta.FindStatusCondition(conditions, condType) if cond == nil { t.Fatalf("condition %q not found", condType) } - if cond.Status != metav1.ConditionFalse { - t.Fatalf("condition %q status = %q, want %q", condType, cond.Status, metav1.ConditionFalse) + if cond.Status != wantStatus { + t.Fatalf("condition %q status = %q, want %q", condType, cond.Status, wantStatus) } if cond.Reason != wantReason { t.Fatalf("condition %q reason = %q, want %q", condType, cond.Reason, wantReason) diff --git a/scope/machine_scope.go b/scope/machine_scope.go index 0531f24..50cb0fa 100644 --- a/scope/machine_scope.go +++ b/scope/machine_scope.go @@ -78,7 +78,11 @@ func (s *MachineScope) PatchObject(ctx context.Context) error { } func (s *MachineScope) SetConditions(status metav1.ConditionStatus, reason, message string, conditionTypes ...string) { - util.SetConditions(&s.StackitMachine.Status.Conditions, s.StackitMachine.Generation, status, reason, message, conditionTypes...) + setConditions(&s.StackitMachine.Status.Conditions, s.StackitMachine.Generation, status, reason, message, conditionTypes...) +} + +func (s *MachineScope) SetPausedCondition(paused bool, message string) { + setPausedCondition(&s.StackitMachine.Status.Conditions, s.StackitMachine.Generation, paused, message) } func (s *MachineScope) SetReady() { diff --git a/util/conditions.go b/util/conditions.go deleted file mode 100644 index 4712ef2..0000000 --- a/util/conditions.go +++ /dev/null @@ -1,34 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -package util - -import ( - "k8s.io/apimachinery/pkg/api/meta" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func SetConditions( - conditions *[]metav1.Condition, - generation int64, - status metav1.ConditionStatus, - reason, message string, - conditionTypes ...string, -) { - for _, conditionType := range conditionTypes { - meta.SetStatusCondition(conditions, metav1.Condition{ - Type: conditionType, - Status: status, - Reason: reason, - Message: message, - ObservedGeneration: generation, - }) - } -} diff --git a/util/reconcile.go b/util/reconcile.go index 6a8b4b4..84eff37 100644 --- a/util/reconcile.go +++ b/util/reconcile.go @@ -17,7 +17,6 @@ import ( "strings" corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/utils/ptr" clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" @@ -49,14 +48,6 @@ func ReconciliationPaused(cluster *clusterv1.Cluster, obj client.Object) (bool, return true, strings.Join(reasons, ", ") } -func SetPausedCondition(conditions *[]metav1.Condition, generation int64, paused bool, message string) { - if paused { - SetConditions(conditions, generation, metav1.ConditionTrue, clusterv1.PausedReason, message, clusterv1.PausedCondition) - return - } - SetConditions(conditions, generation, metav1.ConditionFalse, clusterv1.NotPausedReason, "", clusterv1.PausedCondition) -} - func BuildCloudClient( ctx context.Context, k8sClient client.Client, From 983690b7441448fa6b60cd67a864af3f396a8038 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Tue, 17 Nov 2026 17:19:53 +0100 Subject: [PATCH 53/60] refactor: align deletion order for bastion resources --- controller/stackitcluster_bastion.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index 5f09bf3..aea9021 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -146,15 +146,15 @@ func deleteBastion( if err := publicip.New(clusterScope).Delete(ctx); err != nil { return err } - if err := securitygroup.New(clusterScope).Delete(ctx); err != nil { - return err - } if err := server.New(clusterScope).Delete(ctx); err != nil { return err } if hadBastion { record.Event(clusterScope.StackitCluster, "BastionDeleted", "Deleted bastion") } + if err := securitygroup.New(clusterScope).Delete(ctx); err != nil { + return err + } return nil } From 08efbfa220c2d1d317ed51e9c0e058088657d1e3 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Tue, 17 Nov 2026 17:39:17 +0100 Subject: [PATCH 54/60] refactor: combine LoadBalancerSpec SetID and SetAddress into SetLoadBalancer --- cloud/sdk_client_integration_test.go | 6 +- cloud/services/loadbalancer/reconcile.go | 39 +++------- cloud/services/loadbalancer/reconcile_test.go | 77 ++++++++++--------- cloud/services/loadbalancer/service.go | 17 ++-- controller/stackitcluster_infrastructure.go | 3 +- scope/cluster_scope.go | 38 +++++---- scope/cluster_scope_test.go | 18 +++-- 7 files changed, 100 insertions(+), 98 deletions(-) diff --git a/cloud/sdk_client_integration_test.go b/cloud/sdk_client_integration_test.go index d1a3cfd..8cb1e14 100644 --- a/cloud/sdk_client_integration_test.go +++ b/cloud/sdk_client_integration_test.go @@ -200,8 +200,10 @@ func createIntegrationLoadBalancer(t *testing.T, client Client, networkID string "capistackit-test": "true", "capistackit-name": name, }, - SetID: func(id string) { - scope.spec.ID = id + SetLoadBalancer: func(apiServerLB *loadbalancer.LoadBalancer, _ bool) { + if apiServerLB != nil { + scope.spec.ID = apiServerLB.ID + } }, }, } diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index ee2b5fb..362e51b 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -28,15 +28,13 @@ func (s *Service) Reconcile(ctx context.Context) error { return err } - loadBalancer, err := s.ensureLoadBalancer(ctx, spec) + loadBalancer, created, err := s.ensureLoadBalancer(ctx, spec) if err != nil { return err } - if spec.SetID != nil { - spec.SetID(loadBalancer.ID) - } - if spec.SetAddress != nil { - spec.SetAddress(loadBalancer.IP, spec.Port) + loadBalancer.Port = spec.Port + if spec.SetLoadBalancer != nil { + spec.SetLoadBalancer(loadBalancer, created) } if loadBalancer.IP == "" { return nil @@ -50,7 +48,7 @@ func (s *Service) Reconcile(ctx context.Context) error { } log.V(1).Info("Reconciled API server load balancer target pool", "targets", len(spec.Targets)) } - log.V(1).Info("Reconciled load balancer resources", "loadBalancerID", loadBalancer.ID) + log.V(1).Info("Reconciled load balancer resources", "loadBalancerID", loadBalancer.ID, "created", created) return nil } @@ -68,11 +66,8 @@ func (s *Service) Delete(ctx context.Context) error { if err != nil { return err } - if spec.SetID != nil { - spec.SetID("") - } - if spec.SetAddress != nil { - spec.SetAddress("", 0) + if spec.SetLoadBalancer != nil { + spec.SetLoadBalancer(nil, false) } return nil } @@ -134,13 +129,13 @@ func (s *Service) deleteByID(ctx context.Context, id string) (bool, error) { return true, nil } -func (s *Service) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) (*LoadBalancer, error) { +func (s *Service) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) (*LoadBalancer, bool, error) { loadBalancer, err := shared.ResolveExisting(ctx, spec.ID, spec.Tags, s.get, s.findByTags, isIgnorableLookupError) if err != nil { - return nil, err + return nil, false, err } if loadBalancer != nil { - return loadBalancer, nil + return loadBalancer, false, nil } payload := spec.toCreatePayload(s.scope.Region()) @@ -148,11 +143,11 @@ func (s *Service) ensureLoadBalancer(ctx context.Context, spec LoadBalancerSpec) CreateLoadBalancerPayload(payload). Execute() if err != nil { - return nil, shared.ClassifySDKError("create load balancer", err) + return nil, false, shared.ClassifySDKError("create load balancer", err) } out := loadBalancerFromSDK(lbResp) logf.FromContext(ctx).V(1).Info("Created load balancer", "name", spec.Name, "loadBalancerID", out.ID) - return out, nil + return out, true, nil } func (s *Service) listIDsByTags(ctx context.Context, tags map[string]string) ([]string, error) { @@ -236,17 +231,7 @@ func loadBalancerFromSDK(in *lb.LoadBalancer) *LoadBalancer { ID: in.GetName(), Name: in.GetName(), IP: shared.FirstNonEmpty(in.GetExternalAddress(), in.GetPrivateAddress()), - Port: listenerPort(in.GetListeners()), - } -} - -func listenerPort(listeners []lb.Listener) int32 { - for _, listener := range listeners { - if port := listener.GetPort(); port > 0 { - return port - } } - return 0 } func isIgnorableLookupError(err error) bool { diff --git a/cloud/services/loadbalancer/reconcile_test.go b/cloud/services/loadbalancer/reconcile_test.go index c4f48f0..8df0b30 100644 --- a/cloud/services/loadbalancer/reconcile_test.go +++ b/cloud/services/loadbalancer/reconcile_test.go @@ -120,10 +120,11 @@ func sdkLoadBalancerJSON(name string, labels map[string]any, targetPools []any) func TestServiceReconcile(t *testing.T) { t.Run("creates load balancer with expected payload and updates target pool", func(t *testing.T) { var ( - createPayload map[string]any - recordedID string - recordedIP string - recordedPort int32 + createPayload map[string]any + recordedID string + recordedIP string + recordedPort int32 + recordedCreated bool ) spec := loadbalancer.LoadBalancerSpec{ Name: testLBName, @@ -139,12 +140,11 @@ func TestServiceReconcile(t *testing.T) { SeedTargets: []loadbalancer.Target{ {Name: "cp-0", IP: "10.0.0.10"}, }, - SetID: func(id string) { - recordedID = id - }, - SetAddress: func(ip string, port int32) { - recordedIP = ip - recordedPort = port + SetLoadBalancer: func(apiServerLB *loadbalancer.LoadBalancer, created bool) { + recordedID = apiServerLB.ID + recordedIP = apiServerLB.IP + recordedPort = apiServerLB.Port + recordedCreated = created }, } @@ -183,6 +183,9 @@ func TestServiceReconcile(t *testing.T) { if recordedIP != testPublicIP || recordedPort != 6443 { t.Fatalf("recorded address = %s:%d, want %s:6443", recordedIP, recordedPort, testPublicIP) } + if !recordedCreated { + t.Fatal("recordedCreated = false, want true") + } if createPayload["name"] != testLBName { t.Fatalf("payload name = %v, want %q", createPayload["name"], testLBName) } @@ -247,15 +250,19 @@ func TestServiceReconcile(t *testing.T) { func TestServiceReconcileReuseAndErrors(t *testing.T) { t.Run("reuses existing load balancer found by ID without listing by tags", func(t *testing.T) { - var recordedID string + var ( + recordedID string + recordedCreated = true + ) spec := loadbalancer.LoadBalancerSpec{ ID: testLBName, Name: testLBName, NetworkID: testNetworkID, Port: loadbalancer.DefaultAPIServerPort, Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, - SetID: func(id string) { - recordedID = id + SetLoadBalancer: func(apiServerLB *loadbalancer.LoadBalancer, created bool) { + recordedID = apiServerLB.ID + recordedCreated = created }, } @@ -273,6 +280,9 @@ func TestServiceReconcileReuseAndErrors(t *testing.T) { if recordedID != testLBName { t.Fatalf("recordedID = %q, want %q", recordedID, testLBName) } + if recordedCreated { + t.Fatal("recordedCreated = true on reused load balancer, want false") + } }) t.Run("falls back to tag lookup when recorded ID is not found during reconcile", func(t *testing.T) { @@ -286,8 +296,8 @@ func TestServiceReconcileReuseAndErrors(t *testing.T) { NetworkID: testNetworkID, Port: loadbalancer.DefaultAPIServerPort, Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, - SetID: func(id string) { - recordedID = id + SetLoadBalancer: func(apiServerLB *loadbalancer.LoadBalancer, _ bool) { + recordedID = apiServerLB.ID }, } @@ -327,8 +337,8 @@ func TestServiceReconcileReuseAndErrors(t *testing.T) { NetworkID: testNetworkID, Port: loadbalancer.DefaultAPIServerPort, Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, - SetID: func(id string) { - recordedID = id + SetLoadBalancer: func(apiServerLB *loadbalancer.LoadBalancer, _ bool) { + recordedID = apiServerLB.ID }, } @@ -384,11 +394,9 @@ func TestServiceReconcileReuseAndErrors(t *testing.T) { Port: loadbalancer.DefaultAPIServerPort, Tags: map[string]string{"cluster.x-k8s.io/cluster-name": "test"}, Targets: []loadbalancer.Target{{Name: "cp-0", IP: "10.0.0.10"}}, - SetID: func(id string) { - recordedID = id - }, - SetAddress: func(ip string, _ int32) { - recordedIP = ip + SetLoadBalancer: func(apiServerLB *loadbalancer.LoadBalancer, _ bool) { + recordedID = apiServerLB.ID + recordedIP = apiServerLB.IP }, } @@ -558,21 +566,17 @@ func TestServiceReconcileValidation(t *testing.T) { } func TestServiceDelete(t *testing.T) { - t.Run("deletes load balancer using ID when provided and clears ID and address", func(t *testing.T) { + t.Run("deletes load balancer using ID when provided and clears state", func(t *testing.T) { var ( - deletedID string - clearedID = "not-cleared" - clearedIP = "not-cleared" - clearedPort = int32(6443) + deletedID string + clearedLB = &loadbalancer.LoadBalancer{ID: "not-cleared"} + clearedCreated = true ) spec := loadbalancer.LoadBalancerSpec{ ID: testLBName, - SetID: func(id string) { - clearedID = id - }, - SetAddress: func(ip string, port int32) { - clearedIP = ip - clearedPort = port + SetLoadBalancer: func(apiServerLB *loadbalancer.LoadBalancer, created bool) { + clearedLB = apiServerLB + clearedCreated = created }, } @@ -591,11 +595,8 @@ func TestServiceDelete(t *testing.T) { if deletedID != testLBName { t.Fatalf("deletedID = %q, want %q", deletedID, testLBName) } - if clearedID != "" { - t.Fatalf("clearedID = %q, want empty", clearedID) - } - if clearedIP != "" || clearedPort != 0 { - t.Fatalf("cleared address = (%q, %d), want (\"\", 0)", clearedIP, clearedPort) + if clearedLB != nil || clearedCreated { + t.Fatalf("cleared state = (%#v, %v), want (nil, false)", clearedLB, clearedCreated) } }) diff --git a/cloud/services/loadbalancer/service.go b/cloud/services/loadbalancer/service.go index f8a3795..1f12001 100644 --- a/cloud/services/loadbalancer/service.go +++ b/cloud/services/loadbalancer/service.go @@ -55,15 +55,14 @@ type Target struct { // LoadBalancerSpec describes the desired state of a STACKIT API server load balancer. type LoadBalancerSpec struct { - ID string - Name string - NetworkID string - Port int32 - Tags map[string]string - Targets []Target - SeedTargets []Target - SetID func(id string) - SetAddress func(ip string, port int32) + ID string + Name string + NetworkID string + Port int32 + Tags map[string]string + Targets []Target + SeedTargets []Target + SetLoadBalancer func(loadBalancer *LoadBalancer, created bool) } // Scope is an interface that provides scope data needed for load balancer reconciliation. diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go index 85684e8..d38e332 100644 --- a/controller/stackitcluster_infrastructure.go +++ b/controller/stackitcluster_infrastructure.go @@ -134,12 +134,11 @@ func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterS } clusterScope.SetAPIServerLoadBalancerTargets(targets, seed) - hadLoadBalancerID := stackitCluster.Status.APIServerLoadBalancerID != "" previousEndpointHost := stackitCluster.Status.APIServerEndpoint.Host wasLBReady := meta.IsStatusConditionTrue(stackitCluster.Status.Conditions, infrav1.ClusterLoadBalancerReadyCondition) err = loadbalancer.New(clusterScope).Reconcile(ctx) - if !hadLoadBalancerID && stackitCluster.Status.APIServerLoadBalancerID != "" { + if clusterScope.APIServerLoadBalancerCreated() { record.Eventf( stackitCluster, "LoadBalancerCreated", "Created API server load balancer %s", stackitCluster.Status.APIServerLoadBalancerID, diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index 27cc6ce..c1e9806 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -55,6 +55,7 @@ type ClusterScope struct { loadBalancerTargets []loadbalancer.Target loadBalancerSeedTargets []loadbalancer.Target + loadBalancerCreated bool bastionCloudInit []byte bastionCloudInitHash string @@ -311,18 +312,26 @@ func (s *ClusterScope) apiServerLoadBalancerTags() map[string]string { return util.ClusterTags(s.StackitCluster.Name, s.StackitCluster.Namespace, s.StackitCluster.Spec.AdditionalLabels) } -func (s *ClusterScope) setAPIServerLoadBalancerID(id string) { - s.StackitCluster.Status.APIServerLoadBalancerID = id +func (s *ClusterScope) APIServerLoadBalancerCreated() bool { + return s.loadBalancerCreated } -func (s *ClusterScope) setAPIServerLoadBalancerAddress(ip string, port int32) { - if ip == "" { +func (s *ClusterScope) setAPIServerLoadBalancer(apiServerLB *loadbalancer.LoadBalancer, created bool) { + if apiServerLB == nil { + s.loadBalancerCreated = false + s.StackitCluster.Status.APIServerLoadBalancerID = "" + s.StackitCluster.Status.APIServerEndpoint = clusterv1.APIEndpoint{} + return + } + s.loadBalancerCreated = created + s.StackitCluster.Status.APIServerLoadBalancerID = apiServerLB.ID + if apiServerLB.IP == "" { s.StackitCluster.Status.APIServerEndpoint = clusterv1.APIEndpoint{} return } s.setAPIServerEndpoint(clusterv1.APIEndpoint{ - Host: ip, - Port: port, + Host: apiServerLB.IP, + Port: apiServerLB.Port, }) } @@ -333,15 +342,14 @@ func (s *ClusterScope) SetAPIServerLoadBalancerTargets(targets, seedTargets []lo func (s *ClusterScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec { return loadbalancer.LoadBalancerSpec{ - ID: s.StackitCluster.Status.APIServerLoadBalancerID, - Name: s.StackitCluster.Name + "-apiserver", - NetworkID: s.StackitCluster.Spec.Network.ID, - Port: loadbalancer.DefaultAPIServerPort, - Tags: s.apiServerLoadBalancerTags(), - Targets: s.loadBalancerTargets, - SeedTargets: s.loadBalancerSeedTargets, - SetID: s.setAPIServerLoadBalancerID, - SetAddress: s.setAPIServerLoadBalancerAddress, + ID: s.StackitCluster.Status.APIServerLoadBalancerID, + Name: s.StackitCluster.Name + "-apiserver", + NetworkID: s.StackitCluster.Spec.Network.ID, + Port: loadbalancer.DefaultAPIServerPort, + Tags: s.apiServerLoadBalancerTags(), + Targets: s.loadBalancerTargets, + SeedTargets: s.loadBalancerSeedTargets, + SetLoadBalancer: s.setAPIServerLoadBalancer, } } diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index 65d6de6..60937aa 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -137,15 +137,21 @@ func TestServerSpecSetServerPreservesCloudInitHashOnAdoptedServer(t *testing.T) } } -func TestLoadBalancerSpecSetIDAndSetAddressClearsOnEmpty(t *testing.T) { +func TestLoadBalancerSpecSetLoadBalancerClearsOnNil(t *testing.T) { clusterScope := &ClusterScope{ StackitCluster: &infrav1.StackitCluster{}, } spec := clusterScope.LoadBalancerSpec() - spec.SetID("lb-1") - spec.SetAddress("203.0.113.10", 6443) + spec.SetLoadBalancer(&loadbalancer.LoadBalancer{ + ID: "lb-1", + IP: "203.0.113.10", + Port: 6443, + }, true) + if !clusterScope.APIServerLoadBalancerCreated() { + t.Fatal("APIServerLoadBalancerCreated() = false on created load balancer, want true") + } if clusterScope.StackitCluster.Status.APIServerLoadBalancerID != "lb-1" { t.Fatalf("Status.APIServerLoadBalancerID = %q, want lb-1", clusterScope.StackitCluster.Status.APIServerLoadBalancerID) } @@ -157,8 +163,10 @@ func TestLoadBalancerSpecSetIDAndSetAddressClearsOnEmpty(t *testing.T) { t.Fatalf("Status.APIServerEndpoint = %#v, want %#v", clusterScope.StackitCluster.Status.APIServerEndpoint, wantEndpoint) } - spec.SetID("") - spec.SetAddress("", 0) + spec.SetLoadBalancer(nil, false) + if clusterScope.APIServerLoadBalancerCreated() { + t.Fatal("APIServerLoadBalancerCreated() = true after SetLoadBalancer(nil, false), want false") + } if clusterScope.StackitCluster.Status.APIServerLoadBalancerID != "" { t.Fatalf("Status.APIServerLoadBalancerID after clear = %q, want empty", clusterScope.StackitCluster.Status.APIServerLoadBalancerID) } From 36dbda0783c1fbff0437886cdf4ec1c0692cfa4b Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Tue, 17 Nov 2026 18:03:34 +0100 Subject: [PATCH 55/60] refactor: unify PublicIPSpec and SecurityGroupSpec callbacks to pass domain pointers --- cloud/services/publicip/reconcile.go | 4 +- cloud/services/publicip/reconcile_test.go | 43 +++++++-------- cloud/services/publicip/service.go | 2 +- cloud/services/securitygroup/reconcile.go | 8 +-- .../services/securitygroup/reconcile_test.go | 19 ++++--- cloud/services/securitygroup/service.go | 16 +++--- scope/cluster_scope.go | 35 ++++++++----- scope/cluster_scope_test.go | 52 +++++++++++++++++++ 8 files changed, 121 insertions(+), 58 deletions(-) diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index 45781e2..aa47c37 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -43,7 +43,7 @@ func (s *Service) Reconcile(ctx context.Context) error { } if spec.SetPublicIP != nil { - spec.SetPublicIP(publicIP.ID, publicIP.IP) + spec.SetPublicIP(publicIP) } log.V(1).Info("Reconciled public IP resources", "publicIPID", publicIP.ID) return nil @@ -71,7 +71,7 @@ func (s *Service) Delete(ctx context.Context) error { } if spec.SetPublicIP != nil { - spec.SetPublicIP("", "") + spec.SetPublicIP(nil) } return nil } diff --git a/cloud/services/publicip/reconcile_test.go b/cloud/services/publicip/reconcile_test.go index 904f49e..78af6ac 100644 --- a/cloud/services/publicip/reconcile_test.go +++ b/cloud/services/publicip/reconcile_test.go @@ -119,15 +119,13 @@ func TestServiceReconcile(t *testing.T) { var ( createPayload map[string]any attachedPair string - recordedID string - recordedIP string + recorded *publicip.PublicIP ) spec := publicip.PublicIPSpec{ Tags: map[string]string{"cluster": "test"}, ServerID: testServerID, - SetPublicIP: func(id, ip string) { - recordedID = id - recordedIP = ip + SetPublicIP: func(pip *publicip.PublicIP) { + recorded = pip }, } @@ -162,8 +160,8 @@ func TestServiceReconcile(t *testing.T) { if attachedPair != testServerID+":"+testPublicIPID { t.Fatalf("attachedPair = %q, want %s:%s", attachedPair, testServerID, testPublicIPID) } - if recordedID != testPublicIPID || recordedIP != testPublicIPAddress { - t.Fatalf("recorded = (%q, %q), want (%q, %q)", recordedID, recordedIP, testPublicIPID, testPublicIPAddress) + if recorded == nil || recorded.ID != testPublicIPID || recorded.IP != testPublicIPAddress { + t.Fatalf("recorded = %#v, want ID=%q IP=%q", recorded, testPublicIPID, testPublicIPAddress) } labels, _ := createPayload["labels"].(map[string]any) if labels["cluster"] != "test" { @@ -172,14 +170,13 @@ func TestServiceReconcile(t *testing.T) { }) t.Run("reuses existing public IP found by ID without listing by tags", func(t *testing.T) { - var recordedID, recordedIP string + var recorded *publicip.PublicIP spec := publicip.PublicIPSpec{ ID: testPublicIPID, Tags: map[string]string{"cluster": "test"}, ServerID: testServerID, - SetPublicIP: func(id, ip string) { - recordedID = id - recordedIP = ip + SetPublicIP: func(pip *publicip.PublicIP) { + recorded = pip }, } @@ -198,8 +195,8 @@ func TestServiceReconcile(t *testing.T) { if err := svc.Reconcile(context.Background()); err != nil { t.Fatalf("Reconcile() error = %v", err) } - if recordedID != testPublicIPID || recordedIP != testPublicIPAddress { - t.Fatalf("recorded = (%q, %q), want (%q, %q)", recordedID, recordedIP, testPublicIPID, testPublicIPAddress) + if recorded == nil || recorded.ID != testPublicIPID || recorded.IP != testPublicIPAddress { + t.Fatalf("recorded = %#v, want ID=%q IP=%q", recorded, testPublicIPID, testPublicIPAddress) } }) } @@ -209,8 +206,8 @@ func TestServiceReconcileReuseAndErrors(t *testing.T) { var recordedID string spec := publicip.PublicIPSpec{ Tags: map[string]string{"cluster": "test"}, - SetPublicIP: func(id, _ string) { - recordedID = id + SetPublicIP: func(pip *publicip.PublicIP) { + recordedID = pip.ID }, } @@ -245,8 +242,8 @@ func TestServiceReconcileReuseAndErrors(t *testing.T) { ID: testPublicIPID, Tags: map[string]string{"cluster": "test"}, ServerID: testServerID, - SetPublicIP: func(id, _ string) { - recordedID = id + SetPublicIP: func(pip *publicip.PublicIP) { + recordedID = pip.ID }, } @@ -315,15 +312,13 @@ func TestServiceDelete(t *testing.T) { var ( detachedPair string deletedID string - clearedID = "not-cleared" - clearedIP = "not-cleared" + cleared = &publicip.PublicIP{ID: "not-cleared", IP: "not-cleared"} ) spec := publicip.PublicIPSpec{ ID: testPublicIPID, ServerID: testServerID, - SetPublicIP: func(id, ip string) { - clearedID = id - clearedIP = ip + SetPublicIP: func(pip *publicip.PublicIP) { + cleared = pip }, } @@ -350,8 +345,8 @@ func TestServiceDelete(t *testing.T) { if deletedID != testPublicIPID { t.Fatalf("deletedID = %q, want %q", deletedID, testPublicIPID) } - if clearedID != "" || clearedIP != "" { - t.Fatalf("cleared = (%q, %q), want both empty", clearedID, clearedIP) + if cleared != nil { + t.Fatalf("cleared = %#v, want nil", cleared) } }) diff --git a/cloud/services/publicip/service.go b/cloud/services/publicip/service.go index 6e22fca..e1181b3 100644 --- a/cloud/services/publicip/service.go +++ b/cloud/services/publicip/service.go @@ -25,7 +25,7 @@ type PublicIPSpec struct { Tags map[string]string ServerID string ServerSelector map[string]string - SetPublicIP func(id, ip string) + SetPublicIP func(publicIP *PublicIP) } // Scope is an interface that provides scope data needed for public IP reconciliation. diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index 469f7ad..3eff6d0 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -148,8 +148,8 @@ func (s *Service) reconcileSecurityGroup(ctx context.Context, spec SecurityGroup if err != nil { return err } - if spec.SetID != nil { - spec.SetID(securityGroup.ID) + if spec.SetSecurityGroup != nil { + spec.SetSecurityGroup(securityGroup) } if err := s.ensureSecurityGroupRules(ctx, securityGroup.ID, spec.Rules); err != nil { return err @@ -179,8 +179,8 @@ func (s *Service) deleteSecurityGroup(ctx context.Context, spec SecurityGroupSpe if err != nil { return err } - if spec.SetID != nil { - spec.SetID("") + if spec.SetSecurityGroup != nil { + spec.SetSecurityGroup(nil) } return nil } diff --git a/cloud/services/securitygroup/reconcile_test.go b/cloud/services/securitygroup/reconcile_test.go index 84a4acc..3a01ee7 100644 --- a/cloud/services/securitygroup/reconcile_test.go +++ b/cloud/services/securitygroup/reconcile_test.go @@ -196,8 +196,8 @@ func TestServiceReconcileReuseAndErrors(t *testing.T) { ID: testSecurityGroupID, Name: "cluster-sg", Tags: map[string]string{"cluster": "test"}, - SetID: func(id string) { - recordedID = id + SetSecurityGroup: func(sg *securitygroup.SecurityGroup) { + recordedID = sg.ID }, }} @@ -234,8 +234,8 @@ func TestServiceReconcileReuseAndErrors(t *testing.T) { ID: staleID, Name: "cluster-sg", Tags: map[string]string{"cluster": "test"}, - SetID: func(id string) { - recordedID = id + SetSecurityGroup: func(sg *securitygroup.SecurityGroup) { + recordedID = sg.ID }, }} @@ -404,8 +404,8 @@ func TestServiceReconcileBastionAndNodeSSH(t *testing.T) { { Name: "cluster-bastion-ssh", Tags: map[string]string{"role": "bastion"}, - SetID: func(id string) { - bastionSGID = id + SetSecurityGroup: func(sg *securitygroup.SecurityGroup) { + bastionSGID = sg.ID }, Rules: []securitygroup.Rule{ { @@ -696,9 +696,13 @@ func TestServiceDelete(t *testing.T) { var ( deletedRules []string deletedSGID string + cleared = &securitygroup.SecurityGroup{ID: "not-cleared", Name: "not-cleared"} ) specs := []securitygroup.SecurityGroupSpec{{ ID: testSecurityGroupID, + SetSecurityGroup: func(sg *securitygroup.SecurityGroup) { + cleared = sg + }, }} svc := newTestService(t, specs, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -730,6 +734,9 @@ func TestServiceDelete(t *testing.T) { if deletedSGID != testSecurityGroupID { t.Fatalf("deletedSGID = %q, want %q", deletedSGID, testSecurityGroupID) } + if cleared != nil { + t.Fatalf("cleared = %#v, want nil", cleared) + } }) t.Run("deletes rules and all security groups found by tags", func(t *testing.T) { diff --git a/cloud/services/securitygroup/service.go b/cloud/services/securitygroup/service.go index b332c35..309098a 100644 --- a/cloud/services/securitygroup/service.go +++ b/cloud/services/securitygroup/service.go @@ -48,14 +48,14 @@ type Rule struct { // SecurityGroupSpec describes the desired state of a STACKIT security group and its rules. type SecurityGroupSpec struct { - ID string - Name string - Description string - Tags map[string]string - Rules []Rule - ServerIDs []string - ServerSelector map[string]string - SetID func(string) + ID string + Name string + Description string + Tags map[string]string + Rules []Rule + ServerIDs []string + ServerSelector map[string]string + SetSecurityGroup func(securityGroup *SecurityGroup) } // Scope is an interface that provides scope data needed for security group reconciliation. diff --git a/scope/cluster_scope.go b/scope/cluster_scope.go index c1e9806..e2d5a65 100644 --- a/scope/cluster_scope.go +++ b/scope/cluster_scope.go @@ -247,8 +247,12 @@ func rootVolumeFromSpec(in infrav1.StackitRootVolumeSpec) server.RootVolume { } } -func (s *ClusterScope) setBastionSecurityGroupID(id string) { - s.StackitCluster.Status.Bastion.SecurityGroupID = id +func (s *ClusterScope) setBastionSecurityGroup(sg *securitygroup.SecurityGroup) { + if sg == nil { + s.StackitCluster.Status.Bastion.SecurityGroupID = "" + return + } + s.StackitCluster.Status.Bastion.SecurityGroupID = sg.ID } func (s *ClusterScope) HasBastionStatus() bool { @@ -468,9 +472,14 @@ func NodeSSHTags(stackitCluster *infrav1.StackitCluster) map[string]string { return tags } -func (s *ClusterScope) setBastionPublicIP(id, ip string) { - s.StackitCluster.Status.Bastion.PublicIPID = id - s.StackitCluster.Status.Bastion.PublicIP = ip +func (s *ClusterScope) setBastionPublicIP(pip *publicip.PublicIP) { + if pip == nil { + s.StackitCluster.Status.Bastion.PublicIPID = "" + s.StackitCluster.Status.Bastion.PublicIP = "" + return + } + s.StackitCluster.Status.Bastion.PublicIPID = pip.ID + s.StackitCluster.Status.Bastion.PublicIP = pip.IP } func (s *ClusterScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { @@ -493,14 +502,14 @@ func (s *ClusterScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec { }) return []securitygroup.SecurityGroupSpec{ { - ID: s.StackitCluster.Status.Bastion.SecurityGroupID, - Name: s.StackitCluster.Name + "-bastion-ssh", - Description: "Cluster API Provider STACKIT bastion SSH access", - Tags: s.bastionTags(), - Rules: rules, - ServerIDs: []string{s.StackitCluster.Status.Bastion.ServerID}, - ServerSelector: s.bastionTags(), - SetID: s.setBastionSecurityGroupID, + ID: s.StackitCluster.Status.Bastion.SecurityGroupID, + Name: s.StackitCluster.Name + "-bastion-ssh", + Description: "Cluster API Provider STACKIT bastion SSH access", + Tags: s.bastionTags(), + Rules: rules, + ServerIDs: []string{s.StackitCluster.Status.Bastion.ServerID}, + ServerSelector: s.bastionTags(), + SetSecurityGroup: s.setBastionSecurityGroup, }, nodeSSHSecurityGroupSpec(s.StackitCluster, ""), } diff --git a/scope/cluster_scope_test.go b/scope/cluster_scope_test.go index 60937aa..50ce944 100644 --- a/scope/cluster_scope_test.go +++ b/scope/cluster_scope_test.go @@ -26,6 +26,7 @@ import ( cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" @@ -178,6 +179,57 @@ func TestLoadBalancerSpecSetLoadBalancerClearsOnNil(t *testing.T) { } } +func TestPublicIPSpecSetPublicIPClearsOnNil(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{}, + } + + spec := clusterScope.PublicIPSpec() + spec.SetPublicIP(&publicip.PublicIP{ + ID: "pip-1", + IP: "203.0.113.22", + }) + + if clusterScope.StackitCluster.Status.Bastion.PublicIPID != "pip-1" { + t.Fatalf("Status.Bastion.PublicIPID = %q, want pip-1", clusterScope.StackitCluster.Status.Bastion.PublicIPID) + } + if clusterScope.StackitCluster.Status.Bastion.PublicIP != "203.0.113.22" { + t.Fatalf("Status.Bastion.PublicIP = %q, want 203.0.113.22", clusterScope.StackitCluster.Status.Bastion.PublicIP) + } + + spec.SetPublicIP(nil) + if clusterScope.StackitCluster.Status.Bastion.PublicIPID != "" { + t.Fatalf("Status.Bastion.PublicIPID after clear = %q, want empty", clusterScope.StackitCluster.Status.Bastion.PublicIPID) + } + if clusterScope.StackitCluster.Status.Bastion.PublicIP != "" { + t.Fatalf("Status.Bastion.PublicIP after clear = %q, want empty", clusterScope.StackitCluster.Status.Bastion.PublicIP) + } +} + +func TestSecurityGroupSpecsSetSecurityGroupClearsOnNil(t *testing.T) { + clusterScope := &ClusterScope{ + StackitCluster: &infrav1.StackitCluster{}, + } + + specs := clusterScope.SecurityGroupSpecs() + if len(specs) != 2 { + t.Fatalf("SecurityGroupSpecs() count = %d, want 2", len(specs)) + } + + specs[0].SetSecurityGroup(&securitygroup.SecurityGroup{ + ID: "sg-1", + Name: "bastion-ssh", + }) + if clusterScope.StackitCluster.Status.Bastion.SecurityGroupID != "sg-1" { + t.Fatalf("Status.Bastion.SecurityGroupID = %q, want sg-1", clusterScope.StackitCluster.Status.Bastion.SecurityGroupID) + } + + specs[0].SetSecurityGroup(nil) + if clusterScope.StackitCluster.Status.Bastion.SecurityGroupID != "" { + t.Fatalf("Status.Bastion.SecurityGroupID after clear = %q, want empty", clusterScope.StackitCluster.Status.Bastion.SecurityGroupID) + } +} + func TestMachineScopeServerSpecSetServerClearsOnNil(t *testing.T) { machineScope := &MachineScope{ cluster: &clusterv1.Cluster{}, From bcdb36743d1b0baece2613a3cc1e6813edde500e Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Tue, 17 Nov 2026 18:12:43 +0100 Subject: [PATCH 56/60] refactor: extract shared.ListByTags for tag-filtered SDK listing --- cloud/services/publicip/reconcile.go | 24 ++++------ cloud/services/securitygroup/reconcile.go | 24 ++++------ cloud/services/shared/errors_test.go | 57 +++++++++++++++++++++++ cloud/services/shared/utils.go | 51 ++++++++++++++------ 4 files changed, 110 insertions(+), 46 deletions(-) diff --git a/cloud/services/publicip/reconcile.go b/cloud/services/publicip/reconcile.go index aa47c37..d3ee05c 100644 --- a/cloud/services/publicip/reconcile.go +++ b/cloud/services/publicip/reconcile.go @@ -95,23 +95,15 @@ func (s *Service) findByTags(ctx context.Context, tags map[string]string) (*Publ // listByTags lists STACKIT public IPs matching the given tag selector. func (s *Service) listByTags(ctx context.Context, tags map[string]string) ([]*PublicIP, error) { - if len(tags) == 0 { - return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) - } - resp, err := s.scope.IaaSClient().DefaultAPI.ListPublicIPs(ctx, s.scope.ProjectID(), s.scope.Region()). - LabelSelector(shared.LabelSelector(tags)). - Execute() - if err != nil { - return nil, shared.ClassifySDKError("list public IPs", err) - } - matched := []*PublicIP{} - for _, publicIP := range resp.GetItems() { - if shared.LabelsContainTags(publicIP.GetLabels(), tags) { - publicIP := publicIP - matched = append(matched, publicIPFromSDK(&publicIP)) + return shared.ListByTags(tags, func(selector string) ([]iaas.PublicIp, error) { + resp, err := s.scope.IaaSClient().DefaultAPI.ListPublicIPs(ctx, s.scope.ProjectID(), s.scope.Region()). + LabelSelector(selector). + Execute() + if err != nil { + return nil, shared.ClassifySDKError("list public IPs", err) } - } - return matched, nil + return resp.GetItems(), nil + }, publicIPFromSDK) } // attachToServer attaches a public IP to a server idempotently, tolerating Conflict errors. diff --git a/cloud/services/securitygroup/reconcile.go b/cloud/services/securitygroup/reconcile.go index 3eff6d0..325cec8 100644 --- a/cloud/services/securitygroup/reconcile.go +++ b/cloud/services/securitygroup/reconcile.go @@ -67,23 +67,15 @@ func (s *Service) findByTags(ctx context.Context, tags map[string]string) (*Secu // listByTags lists STACKIT security groups matching the given tag selector. func (s *Service) listByTags(ctx context.Context, tags map[string]string) ([]*SecurityGroup, error) { - if len(tags) == 0 { - return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) - } - resp, err := s.scope.IaaSClient().DefaultAPI.ListSecurityGroups(ctx, s.scope.ProjectID(), s.scope.Region()). - LabelSelector(shared.LabelSelector(tags)). - Execute() - if err != nil { - return nil, shared.ClassifySDKError("list security groups", err) - } - matched := []*SecurityGroup{} - for _, securityGroup := range resp.GetItems() { - if shared.LabelsContainTags(securityGroup.GetLabels(), tags) { - securityGroup := securityGroup - matched = append(matched, securityGroupFromSDK(&securityGroup)) + return shared.ListByTags(tags, func(selector string) ([]iaas.SecurityGroup, error) { + resp, err := s.scope.IaaSClient().DefaultAPI.ListSecurityGroups(ctx, s.scope.ProjectID(), s.scope.Region()). + LabelSelector(selector). + Execute() + if err != nil { + return nil, shared.ClassifySDKError("list security groups", err) } - } - return matched, nil + return resp.GetItems(), nil + }, securityGroupFromSDK) } // attachToServer attaches a security group to a server idempotently. diff --git a/cloud/services/shared/errors_test.go b/cloud/services/shared/errors_test.go index d292fdf..1f3389b 100644 --- a/cloud/services/shared/errors_test.go +++ b/cloud/services/shared/errors_test.go @@ -153,6 +153,63 @@ func TestListIDsByTags(t *testing.T) { }) } +type fakeLabeledItem struct { + id string + labels map[string]any +} + +func (f *fakeLabeledItem) GetLabels() map[string]any { + return f.labels +} + +func TestListByTags(t *testing.T) { + tags := map[string]string{"cluster": "c1", "role": "bastion"} + + t.Run("returns ErrInvalidInput when tags are empty", func(t *testing.T) { + listCalled := false + _, err := ListByTags(nil, func(string) ([]fakeLabeledItem, error) { + listCalled = true + return nil, nil + }, func(item *fakeLabeledItem) string { return item.id }) + if !IsInvalidInput(err) { + t.Fatalf("ListByTags() error = %v, want ErrInvalidInput", err) + } + if listCalled { + t.Fatal("list should not be called when tags are empty") + } + }) + + t.Run("propagates error from list", func(t *testing.T) { + _, err := ListByTags(tags, func(string) ([]fakeLabeledItem, error) { + return nil, ErrTransient + }, func(item *fakeLabeledItem) string { return item.id }) + if !IsTransient(err) { + t.Fatalf("ListByTags() error = %v, want ErrTransient", err) + } + }) + + t.Run("passes label selector and filters items matching all tags", func(t *testing.T) { + var gotSelector string + got, err := ListByTags(tags, func(selector string) ([]fakeLabeledItem, error) { + gotSelector = selector + return []fakeLabeledItem{ + {id: "match", labels: map[string]any{"cluster": "c1", "role": "bastion", "extra": "ok"}}, + {id: "partial", labels: map[string]any{"cluster": "c1"}}, + {id: "other", labels: map[string]any{"cluster": "c2", "role": "bastion"}}, + }, nil + }, func(item *fakeLabeledItem) string { return item.id }) + if err != nil { + t.Fatalf("ListByTags() error = %v", err) + } + if gotSelector != "cluster=c1,role=bastion" && gotSelector != "role=bastion,cluster=c1" { + t.Fatalf("selector = %q, want both tag pairs", gotSelector) + } + if len(got) != 1 || got[0] != "match" { + t.Fatalf("ListByTags() = %v, want [match]", got) + } + }) +} + func TestResolveExisting(t *testing.T) { const ( testID1 = "id-1" diff --git a/cloud/services/shared/utils.go b/cloud/services/shared/utils.go index 66ed9c5..73cb0fe 100644 --- a/cloud/services/shared/utils.go +++ b/cloud/services/shared/utils.go @@ -36,28 +36,51 @@ type LoadBalancerClientScope interface { LoadBalancerClient() *lb.APIClient } -// ListServersByTags lists STACKIT servers matching the given tag selector. -func ListServersByTags(ctx context.Context, scope IaaSClientScope, tags map[string]string) ([]*iaas.Server, error) { +// labeledItem is implemented by pointers to STACKIT SDK models that carry a label map. +type labeledItem[T any] interface { + *T + GetLabels() map[string]any +} + +// ListByTags validates that tags are non-empty, lists SDK items using the tag label selector, +// filters items whose labels contain all tags, and maps each matching item via convert. +func ListByTags[T any, PT labeledItem[T], Out any]( + tags map[string]string, + list func(labelSelector string) ([]T, error), + convert func(*T) Out, +) ([]Out, error) { if len(tags) == 0 { return nil, fmt.Errorf("%w: empty tag selector", ErrInvalidInput) } - resp, err := scope.IaaSClient().DefaultAPI.ListServers(ctx, scope.ProjectID(), scope.Region()). - LabelSelector(LabelSelector(tags)). - Details(true). - Execute() + items, err := list(labelSelector(tags)) if err != nil { - return nil, ClassifySDKError("list servers", err) + return nil, err } - matched := []*iaas.Server{} - for _, srv := range resp.GetItems() { - if LabelsContainTags(srv.GetLabels(), tags) { - srv := srv - matched = append(matched, &srv) + matched := []Out{} + for _, item := range items { + if labelsContainTags(PT(&item).GetLabels(), tags) { + matched = append(matched, convert(&item)) } } return matched, nil } +// ListServersByTags lists STACKIT servers matching the given tag selector. +func ListServersByTags(ctx context.Context, scope IaaSClientScope, tags map[string]string) ([]*iaas.Server, error) { + return ListByTags(tags, func(selector string) ([]iaas.Server, error) { + resp, err := scope.IaaSClient().DefaultAPI.ListServers(ctx, scope.ProjectID(), scope.Region()). + LabelSelector(selector). + Details(true). + Execute() + if err != nil { + return nil, ClassifySDKError("list servers", err) + } + return resp.GetItems(), nil + }, func(srv *iaas.Server) *iaas.Server { + return srv + }) +} + // ListServerIDsByTags lists the IDs of STACKIT servers matching the given tag selector. func ListServerIDsByTags(ctx context.Context, scope IaaSClientScope, tags map[string]string) ([]string, error) { return ListIDsByTags(ctx, tags, func(ctx context.Context, tags map[string]string) ([]*iaas.Server, error) { @@ -208,7 +231,7 @@ func FirstNonEmpty(values ...string) string { return "" } -func LabelSelector(tags map[string]string) string { +func labelSelector(tags map[string]string) string { parts := make([]string, 0, len(tags)) for k, v := range tags { parts = append(parts, k+"="+v) @@ -216,7 +239,7 @@ func LabelSelector(tags map[string]string) string { return strings.Join(parts, ",") } -func LabelsContainTags(labels map[string]any, tags map[string]string) bool { +func labelsContainTags(labels map[string]any, tags map[string]string) bool { for k, want := range tags { got, ok := labels[k] if !ok || fmt.Sprint(got) != want { From e81734e776a57239dbfd2026b94587978ccd7b58 Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Tue, 17 Nov 2026 18:28:46 +0100 Subject: [PATCH 57/60] refactor: remove single-use shared helpers and update cloud.Client doc --- cloud/client.go | 9 +------- cloud/services/loadbalancer/reconcile.go | 3 ++- cloud/services/shared/errors_test.go | 26 --------------------- cloud/services/shared/utils.go | 29 ++++++------------------ 4 files changed, 10 insertions(+), 57 deletions(-) diff --git a/cloud/client.go b/cloud/client.go index 3f9ffae..f44c051 100644 --- a/cloud/client.go +++ b/cloud/client.go @@ -17,14 +17,7 @@ import ( lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" ) -// Client is the abstraction over STACKIT cloud APIs used by the controllers. -// -// Implementations must: -// - return ErrNotFound when a resource does not exist -// - return ErrUnauthorized for permanent auth failures -// - return ErrInvalidInput for permanent input validation errors -// - return ErrConflict for retryable conflict errors -// - return ErrTransient for retryable transient errors +// Client provides the target STACKIT project, region, and SDK API clients. type Client interface { ProjectID() string diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index 362e51b..ee87736 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -11,6 +11,7 @@ You may obtain a copy of the License at package loadbalancer import ( + "cmp" "context" "fmt" "strings" @@ -230,7 +231,7 @@ func loadBalancerFromSDK(in *lb.LoadBalancer) *LoadBalancer { return &LoadBalancer{ ID: in.GetName(), Name: in.GetName(), - IP: shared.FirstNonEmpty(in.GetExternalAddress(), in.GetPrivateAddress()), + IP: cmp.Or(in.GetExternalAddress(), in.GetPrivateAddress()), } } diff --git a/cloud/services/shared/errors_test.go b/cloud/services/shared/errors_test.go index 1f3389b..4154c50 100644 --- a/cloud/services/shared/errors_test.go +++ b/cloud/services/shared/errors_test.go @@ -56,32 +56,6 @@ func TestIsRetryable(t *testing.T) { } } -func TestFindSingle(t *testing.T) { - t.Run("returns ErrNotFound when empty", func(t *testing.T) { - _, err := findSingle([]string{}, "server", "servers") - if !IsNotFound(err) { - t.Fatalf("findSingle() error = %v, want ErrNotFound", err) - } - }) - - t.Run("returns ErrConflict when multiple", func(t *testing.T) { - _, err := findSingle([]string{"a", "b"}, "server", "servers") - if !IsConflict(err) { - t.Fatalf("findSingle() error = %v, want ErrConflict", err) - } - }) - - t.Run("returns single element", func(t *testing.T) { - got, err := findSingle([]string{"a"}, "server", "servers") - if err != nil { - t.Fatalf("findSingle() error = %v", err) - } - if got != "a" { - t.Fatalf("findSingle() = %q, want %q", got, "a") - } - }) -} - func TestFindByTags(t *testing.T) { ctx := context.Background() tags := map[string]string{"cluster": "test"} diff --git a/cloud/services/shared/utils.go b/cloud/services/shared/utils.go index 73cb0fe..551222a 100644 --- a/cloud/services/shared/utils.go +++ b/cloud/services/shared/utils.go @@ -120,7 +120,13 @@ func FindByTags[T any]( if err != nil { return zero, err } - return findSingle(matched, singular, plural) + if len(matched) == 0 { + return zero, fmt.Errorf("%w: no %s matching tags", ErrNotFound, singular) + } + if len(matched) > 1 { + return zero, fmt.Errorf("%w: multiple %s matching tags", ErrConflict, plural) + } + return matched[0], nil } // ResolveExisting looks up an existing resource by ID first (ignoring errors matched by ignoreLookupErr), @@ -201,18 +207,6 @@ func DeleteByIDOrTags( return deletedIDs, errors.Join(errs...) } -// findSingle returns the single item in matched, or ErrNotFound if empty, or ErrConflict if more than one. -func findSingle[T any](matched []T, singular, plural string) (T, error) { - var zero T - if len(matched) == 0 { - return zero, fmt.Errorf("%w: no %s matching tags", ErrNotFound, singular) - } - if len(matched) > 1 { - return zero, fmt.Errorf("%w: multiple %s matching tags", ErrConflict, plural) - } - return matched[0], nil -} - func MapContains(haystack, needle map[string]string) bool { for k, v := range needle { if haystack[k] != v { @@ -222,15 +216,6 @@ func MapContains(haystack, needle map[string]string) bool { return true } -func FirstNonEmpty(values ...string) string { - for _, value := range values { - if value != "" { - return value - } - } - return "" -} - func labelSelector(tags map[string]string) string { parts := make([]string, 0, len(tags)) for k, v := range tags { From 6d1db2f74c2063b0e34befeab34c2e39120d6fae Mon Sep 17 00:00:00 2001 From: Leonhard Schulze Date: Tue, 17 Nov 2026 18:40:15 +0100 Subject: [PATCH 58/60] refactor: unify MapContains with labelsContainTags and use ListByTags in loadbalancer --- cloud/services/loadbalancer/reconcile.go | 29 +++++++----------- cloud/services/shared/errors_test.go | 35 +++++++++++++++------- cloud/services/shared/utils.go | 38 +++++++++++------------- 3 files changed, 52 insertions(+), 50 deletions(-) diff --git a/cloud/services/loadbalancer/reconcile.go b/cloud/services/loadbalancer/reconcile.go index ee87736..419a88c 100644 --- a/cloud/services/loadbalancer/reconcile.go +++ b/cloud/services/loadbalancer/reconcile.go @@ -92,26 +92,17 @@ func (s *Service) findByTags(ctx context.Context, tags map[string]string) (*Load // listByTags lists STACKIT load balancers matching the given tag selector. func (s *Service) listByTags(ctx context.Context, tags map[string]string) ([]*LoadBalancer, error) { - if len(tags) == 0 { - return nil, fmt.Errorf("%w: empty tag selector", shared.ErrInvalidInput) - } - resp, err := s.scope.LoadBalancerClient().DefaultAPI.ListLoadBalancers(ctx, s.scope.ProjectID(), s.scope.Region()).Execute() - if err != nil { - err := shared.ClassifySDKError("list load balancers", err) - if isLoadBalancerServiceNotEnabled(err) { - return nil, nil + return shared.ListByTags(Labels(tags), func(_ string) ([]lb.LoadBalancer, error) { + resp, err := s.scope.LoadBalancerClient().DefaultAPI.ListLoadBalancers(ctx, s.scope.ProjectID(), s.scope.Region()).Execute() + if err != nil { + err := shared.ClassifySDKError("list load balancers", err) + if isLoadBalancerServiceNotEnabled(err) { + return nil, nil + } + return nil, err } - return nil, err - } - matched := []*LoadBalancer{} - labels := Labels(tags) - for _, candidate := range resp.GetLoadBalancers() { - if shared.MapContains(candidate.GetLabels(), labels) { - candidate := candidate - matched = append(matched, loadBalancerFromSDK(&candidate)) - } - } - return matched, nil + return resp.GetLoadBalancers(), nil + }, loadBalancerFromSDK) } // deleteByID deletes a load balancer by ID, returning true if the load balancer existed and was deleted, diff --git a/cloud/services/shared/errors_test.go b/cloud/services/shared/errors_test.go index 4154c50..1add3ba 100644 --- a/cloud/services/shared/errors_test.go +++ b/cloud/services/shared/errors_test.go @@ -127,12 +127,12 @@ func TestListIDsByTags(t *testing.T) { }) } -type fakeLabeledItem struct { +type fakeLabeledItem[V any] struct { id string - labels map[string]any + labels map[string]V } -func (f *fakeLabeledItem) GetLabels() map[string]any { +func (f *fakeLabeledItem[V]) GetLabels() map[string]V { return f.labels } @@ -141,10 +141,10 @@ func TestListByTags(t *testing.T) { t.Run("returns ErrInvalidInput when tags are empty", func(t *testing.T) { listCalled := false - _, err := ListByTags(nil, func(string) ([]fakeLabeledItem, error) { + _, err := ListByTags(nil, func(string) ([]fakeLabeledItem[any], error) { listCalled = true return nil, nil - }, func(item *fakeLabeledItem) string { return item.id }) + }, func(item *fakeLabeledItem[any]) string { return item.id }) if !IsInvalidInput(err) { t.Fatalf("ListByTags() error = %v, want ErrInvalidInput", err) } @@ -154,9 +154,9 @@ func TestListByTags(t *testing.T) { }) t.Run("propagates error from list", func(t *testing.T) { - _, err := ListByTags(tags, func(string) ([]fakeLabeledItem, error) { + _, err := ListByTags(tags, func(string) ([]fakeLabeledItem[any], error) { return nil, ErrTransient - }, func(item *fakeLabeledItem) string { return item.id }) + }, func(item *fakeLabeledItem[any]) string { return item.id }) if !IsTransient(err) { t.Fatalf("ListByTags() error = %v, want ErrTransient", err) } @@ -164,14 +164,14 @@ func TestListByTags(t *testing.T) { t.Run("passes label selector and filters items matching all tags", func(t *testing.T) { var gotSelector string - got, err := ListByTags(tags, func(selector string) ([]fakeLabeledItem, error) { + got, err := ListByTags(tags, func(selector string) ([]fakeLabeledItem[any], error) { gotSelector = selector - return []fakeLabeledItem{ + return []fakeLabeledItem[any]{ {id: "match", labels: map[string]any{"cluster": "c1", "role": "bastion", "extra": "ok"}}, {id: "partial", labels: map[string]any{"cluster": "c1"}}, {id: "other", labels: map[string]any{"cluster": "c2", "role": "bastion"}}, }, nil - }, func(item *fakeLabeledItem) string { return item.id }) + }, func(item *fakeLabeledItem[any]) string { return item.id }) if err != nil { t.Fatalf("ListByTags() error = %v", err) } @@ -182,6 +182,21 @@ func TestListByTags(t *testing.T) { t.Fatalf("ListByTags() = %v, want [match]", got) } }) + + t.Run("filters string-labeled items matching all tags", func(t *testing.T) { + got, err := ListByTags(tags, func(string) ([]fakeLabeledItem[string], error) { + return []fakeLabeledItem[string]{ + {id: "match", labels: map[string]string{"cluster": "c1", "role": "bastion"}}, + {id: "partial", labels: map[string]string{"cluster": "c1"}}, + }, nil + }, func(item *fakeLabeledItem[string]) string { return item.id }) + if err != nil { + t.Fatalf("ListByTags() error = %v", err) + } + if len(got) != 1 || got[0] != "match" { + t.Fatalf("ListByTags() = %v, want [match]", got) + } + }) } func TestResolveExisting(t *testing.T) { diff --git a/cloud/services/shared/utils.go b/cloud/services/shared/utils.go index 551222a..79e0464 100644 --- a/cloud/services/shared/utils.go +++ b/cloud/services/shared/utils.go @@ -36,15 +36,9 @@ type LoadBalancerClientScope interface { LoadBalancerClient() *lb.APIClient } -// labeledItem is implemented by pointers to STACKIT SDK models that carry a label map. -type labeledItem[T any] interface { - *T - GetLabels() map[string]any -} - // ListByTags validates that tags are non-empty, lists SDK items using the tag label selector, // filters items whose labels contain all tags, and maps each matching item via convert. -func ListByTags[T any, PT labeledItem[T], Out any]( +func ListByTags[T any, Out any]( tags map[string]string, list func(labelSelector string) ([]T, error), convert func(*T) Out, @@ -58,7 +52,7 @@ func ListByTags[T any, PT labeledItem[T], Out any]( } matched := []Out{} for _, item := range items { - if labelsContainTags(PT(&item).GetLabels(), tags) { + if itemContainsTags(&item, tags) { matched = append(matched, convert(&item)) } } @@ -207,9 +201,21 @@ func DeleteByIDOrTags( return deletedIDs, errors.Join(errs...) } -func MapContains(haystack, needle map[string]string) bool { - for k, v := range needle { - if haystack[k] != v { +func itemContainsTags(item any, tags map[string]string) bool { + switch labeled := item.(type) { + case interface{ GetLabels() map[string]any }: + return MapContains(labeled.GetLabels(), tags) + case interface{ GetLabels() map[string]string }: + return MapContains(labeled.GetLabels(), tags) + default: + return false + } +} + +func MapContains[V any](haystack map[string]V, needle map[string]string) bool { + for k, want := range needle { + got, ok := haystack[k] + if !ok || fmt.Sprint(got) != want { return false } } @@ -224,16 +230,6 @@ func labelSelector(tags map[string]string) string { return strings.Join(parts, ",") } -func labelsContainTags(labels map[string]any, tags map[string]string) bool { - for k, want := range tags { - got, ok := labels[k] - if !ok || fmt.Sprint(got) != want { - return false - } - } - return true -} - func TagsToSDKLabels(tags map[string]string) map[string]any { out := make(map[string]any, len(tags)) for k, v := range tags { From d88ae614145ddc062de507aae71438c7daf30500 Mon Sep 17 00:00:00 2001 From: Jan Larwig Date: Wed, 18 Nov 2026 15:34:35 +0100 Subject: [PATCH 59/60] refactor: infrastructure into controllers and server state into service package Signed-off-by: Jan Larwig --- .../services/server/state.go | 12 +- cloud/services/server/state_test.go | 201 ++++++++++ controller/server_state_test.go | 56 --- controller/stackitcluster_bastion.go | 2 +- controller/stackitcluster_controller.go | 375 +++++++++++++++++- controller/stackitcluster_infrastructure.go | 372 ----------------- controller/stackitmachine_controller.go | 356 ++++++++++++++++- controller/stackitmachine_infrastructure.go | 274 ------------- controller/stackitmachine_watches.go | 115 ------ go.mod | 2 +- 10 files changed, 917 insertions(+), 848 deletions(-) rename controller/server_state.go => cloud/services/server/state.go (90%) create mode 100644 cloud/services/server/state_test.go delete mode 100644 controller/server_state_test.go delete mode 100644 controller/stackitcluster_infrastructure.go delete mode 100644 controller/stackitmachine_infrastructure.go delete mode 100644 controller/stackitmachine_watches.go diff --git a/controller/server_state.go b/cloud/services/server/state.go similarity index 90% rename from controller/server_state.go rename to cloud/services/server/state.go index aaa18e0..8204854 100644 --- a/controller/server_state.go +++ b/cloud/services/server/state.go @@ -8,11 +8,11 @@ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 */ -package controller +package server import "fmt" -type serverStateInfo struct { +type stateInfo struct { reason string description string // warn marks states that do not resolve on their own. Their reason must @@ -24,7 +24,7 @@ type serverStateInfo struct { // serverStates maps the documented STACKIT server states. // https://docs.api.stackit.cloud/documentation/iaas/version/v2#tag/Servers/operation/V2GetServer // https://github.com/stackitcloud/stackit-sdk-go/blob/services/iaas/v1.14.5/services/iaas/v2api/model_server.go#L63-L64 -var serverStates = map[string]serverStateInfo{ +var serverStates = map[string]stateInfo{ "ACTIVE": {"InstanceActive", "server is active", false}, "BACKING-UP": {"InstanceBusy", "server is temporarily unavailable", false}, "CREATING": {"InstanceStarting", "server is starting", false}, @@ -51,16 +51,16 @@ var serverStates = map[string]serverStateInfo{ "UPDATING": {"InstanceBusy", "server is temporarily unavailable", false}, } -// serverStateCondition maps a server state and power status to a condition +// StateCondition maps a server state and power status to a condition // reason and message. ready is true when the server can be used. -func serverStateCondition(state, powerStatus string) (ready bool, reason, message string, warn bool) { +func StateCondition(state, powerStatus string) (ready bool, reason, message string, warn bool) { if state == "" { ready = true return } info, ok := serverStates[state] if !ok { - info = serverStateInfo{"InstanceNotActive", "server is not active", false} + info = stateInfo{"InstanceNotActive", "server is not active", false} } ready, warn, reason, description := state == "ACTIVE", info.warn, info.reason, info.description if state == "ACTIVE" { diff --git a/cloud/services/server/state_test.go b/cloud/services/server/state_test.go new file mode 100644 index 0000000..879cf0c --- /dev/null +++ b/cloud/services/server/state_test.go @@ -0,0 +1,201 @@ +/* +Copyright 2026. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +*/ + +package server_test + +import ( + "testing" + + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" +) + +func TestStateCondition(t *testing.T) { + tests := []struct { + name string + state string + powerStatus string + wantReady bool + wantReason string + wantMessage string + wantWarn bool + }{ + { + name: "active and running", + state: "ACTIVE", + powerStatus: "RUNNING", + wantReady: true, + wantReason: "InstanceActive", + wantMessage: "server is active (state ACTIVE, power status RUNNING)", + wantWarn: false, + }, + { + name: "unknown state", + state: "", + powerStatus: "", + wantReady: true, + wantReason: "", + wantMessage: "", + wantWarn: false, + }, + { + name: "active but crashed", + state: "ACTIVE", + powerStatus: "CRASHED", + wantReady: false, + wantReason: "InstanceCrashed", + wantMessage: "server crashed (state ACTIVE, power status CRASHED)", + wantWarn: true, + }, + { + name: "active with power error", + state: "ACTIVE", + powerStatus: "ERROR", + wantReady: false, + wantReason: "InstancePowerError", + wantMessage: "server power error (state ACTIVE, power status ERROR)", + wantWarn: true, + }, + { + name: "creating", + state: "CREATING", + powerStatus: "", + wantReady: false, + wantReason: "InstanceStarting", + wantMessage: "server is starting (state CREATING)", + wantWarn: false, + }, + { + name: "rebooting", + state: "REBOOTING", + powerStatus: "RUNNING", + wantReady: false, + wantReason: "InstanceBusy", + wantMessage: "server is temporarily unavailable (state REBOOTING, power status RUNNING)", + wantWarn: false, + }, + { + name: "stopping", + state: "STOPPING", + powerStatus: "RUNNING", + wantReady: false, + wantReason: "InstanceStopping", + wantMessage: "server is stopping (state STOPPING, power status RUNNING)", + wantWarn: false, + }, + { + name: "stopped", + state: "INACTIVE", + powerStatus: "STOPPED", + wantReady: false, + wantReason: "InstanceStopped", + wantMessage: "server is stopped (state INACTIVE, power status STOPPED)", + wantWarn: true, + }, + { + name: "deallocating", + state: "DEALLOCATING", + powerStatus: "STOPPED", + wantReady: false, + wantReason: "InstanceDeallocating", + wantMessage: "server is being deallocated (state DEALLOCATING, power status STOPPED)", + wantWarn: false, + }, + { + name: "deallocated", + state: "DEALLOCATED", + powerStatus: "STOPPED", + wantReady: false, + wantReason: "InstanceDeallocated", + wantMessage: "server is deallocated (state DEALLOCATED, power status STOPPED)", + wantWarn: true, + }, + { + name: "paused", + state: "PAUSED", + powerStatus: "", + wantReady: false, + wantReason: "InstancePaused", + wantMessage: "server is paused (state PAUSED)", + wantWarn: true, + }, + { + name: "entering rescue", + state: "RESCUING", + powerStatus: "", + wantReady: false, + wantReason: "InstanceEnteringRescue", + wantMessage: "server is entering rescue mode (state RESCUING)", + wantWarn: false, + }, + { + name: "leaving rescue", + state: "UNRESCUING", + powerStatus: "", + wantReady: false, + wantReason: "InstanceLeavingRescue", + wantMessage: "server is leaving rescue mode (state UNRESCUING)", + wantWarn: false, + }, + { + name: "rescue", + state: "RESCUE", + powerStatus: "RUNNING", + wantReady: false, + wantReason: "InstanceInRescue", + wantMessage: "server is in rescue mode (state RESCUE, power status RUNNING)", + wantWarn: true, + }, + { + name: "deleting", + state: "DELETING", + powerStatus: "", + wantReady: false, + wantReason: "InstanceDeleting", + wantMessage: "server is being deleted (state DELETING)", + wantWarn: false, + }, + { + name: "error", + state: "ERROR", + powerStatus: "ERROR", + wantReady: false, + wantReason: "InstanceFailed", + wantMessage: "server failed (state ERROR, power status ERROR)", + wantWarn: true, + }, + { + name: "undocumented state", + state: "SOMETHING", + powerStatus: "", + wantReady: false, + wantReason: "InstanceNotActive", + wantMessage: "server is not active (state SOMETHING)", + wantWarn: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ready, reason, message, warn := server.StateCondition(tt.state, tt.powerStatus) + if ready != tt.wantReady { + t.Errorf("StateCondition(%q, %q) ready = %v, want %v", tt.state, tt.powerStatus, ready, tt.wantReady) + } + if reason != tt.wantReason { + t.Errorf("StateCondition(%q, %q) reason = %q, want %q", tt.state, tt.powerStatus, reason, tt.wantReason) + } + if message != tt.wantMessage { + t.Errorf("StateCondition(%q, %q) message = %q, want %q", tt.state, tt.powerStatus, message, tt.wantMessage) + } + if warn != tt.wantWarn { + t.Errorf("StateCondition(%q, %q) warn = %v, want %v", tt.state, tt.powerStatus, warn, tt.wantWarn) + } + }) + } +} diff --git a/controller/server_state_test.go b/controller/server_state_test.go deleted file mode 100644 index 7ed2faa..0000000 --- a/controller/server_state_test.go +++ /dev/null @@ -1,56 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -package controller - -import ( - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -var _ = Describe("serverStateCondition", func() { - DescribeTable("maps server state and power status", - func(state, powerStatus string, wantReady bool, wantReason, wantMessage string, wantWarn bool) { - ready, reason, message, warn := serverStateCondition(state, powerStatus) - Expect(ready).To(Equal(wantReady)) - Expect(reason).To(Equal(wantReason)) - Expect(message).To(Equal(wantMessage)) - Expect(warn).To(Equal(wantWarn)) - }, - Entry("active and running", "ACTIVE", "RUNNING", true, "InstanceActive", "server is active (state ACTIVE, power status RUNNING)", false), - Entry("unknown state", "", "", true, "", "", false), - Entry("active but crashed", "ACTIVE", "CRASHED", false, "InstanceCrashed", - "server crashed (state ACTIVE, power status CRASHED)", true), - Entry("active with power error", "ACTIVE", "ERROR", false, "InstancePowerError", - "server power error (state ACTIVE, power status ERROR)", true), - Entry("creating", "CREATING", "", false, "InstanceStarting", "server is starting (state CREATING)", false), - Entry("rebooting", "REBOOTING", "RUNNING", false, "InstanceBusy", - "server is temporarily unavailable (state REBOOTING, power status RUNNING)", false), - Entry("stopping", "STOPPING", "RUNNING", false, "InstanceStopping", - "server is stopping (state STOPPING, power status RUNNING)", false), - Entry("stopped", "INACTIVE", "STOPPED", false, "InstanceStopped", - "server is stopped (state INACTIVE, power status STOPPED)", true), - Entry("deallocating", "DEALLOCATING", "STOPPED", false, "InstanceDeallocating", - "server is being deallocated (state DEALLOCATING, power status STOPPED)", false), - Entry("deallocated", "DEALLOCATED", "STOPPED", false, "InstanceDeallocated", - "server is deallocated (state DEALLOCATED, power status STOPPED)", true), - Entry("paused", "PAUSED", "", false, "InstancePaused", "server is paused (state PAUSED)", true), - Entry("entering rescue", "RESCUING", "", false, "InstanceEnteringRescue", - "server is entering rescue mode (state RESCUING)", false), - Entry("leaving rescue", "UNRESCUING", "", false, "InstanceLeavingRescue", - "server is leaving rescue mode (state UNRESCUING)", false), - Entry("rescue", "RESCUE", "RUNNING", false, "InstanceInRescue", - "server is in rescue mode (state RESCUE, power status RUNNING)", true), - Entry("deleting", "DELETING", "", false, "InstanceDeleting", "server is being deleted (state DELETING)", false), - Entry("error", "ERROR", "ERROR", false, "InstanceFailed", "server failed (state ERROR, power status ERROR)", true), - Entry("undocumented state", "SOMETHING", "", false, "InstanceNotActive", - "server is not active (state SOMETHING)", false), - ) -}) diff --git a/controller/stackitcluster_bastion.go b/controller/stackitcluster_bastion.go index aea9021..42de08a 100644 --- a/controller/stackitcluster_bastion.go +++ b/controller/stackitcluster_bastion.go @@ -108,7 +108,7 @@ func (r *StackitClusterReconciler) reconcileBastion( return ctrl.Result{}, false, err } state, powerStatus := clusterScope.BastionServerState() - if ready, reason, message, warn := serverStateCondition(state, powerStatus); !ready { + if ready, reason, message, warn := server.StateCondition(state, powerStatus); !ready { previousReason := "" if previous := meta.FindStatusCondition(stackitCluster.Status.Conditions, infrav1.ClusterBastionReadyCondition); previous != nil { previousReason = previous.Reason diff --git a/controller/stackitcluster_controller.go b/controller/stackitcluster_controller.go index 4c7f637..5db54df 100644 --- a/controller/stackitcluster_controller.go +++ b/controller/stackitcluster_controller.go @@ -22,16 +22,22 @@ import ( "fmt" "maps" "slices" + "time" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" clusterutil "sigs.k8s.io/cluster-api/util" + "sigs.k8s.io/cluster-api/util/collections" + "sigs.k8s.io/cluster-api/util/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/handler" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -40,6 +46,10 @@ import ( infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/availabilityzone" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -54,6 +64,22 @@ type StackitClusterReconciler struct { CloudClientFactory cloud.Factory } +// SetupWithManager registers the controller with the manager. +func (r *StackitClusterReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&infrav1.StackitCluster{}). + Watches(&clusterv1.Cluster{}, handler.EnqueueRequestsFromMapFunc(r.filterForStackitClusterResources)). + Watches( + &clusterv1.Machine{}, + handler.EnqueueRequestsFromMapFunc(r.loadBalancerTargetsForControlPlanes), + builder.WithPredicates(predicate.Funcs{UpdateFunc: machineTargetPoolChanged}), + ). + Watches(&corev1.ConfigMap{}, handler.EnqueueRequestsFromMapFunc(r.stackitClusterRequestsForCloudInitRef)). + Watches(&corev1.Secret{}, handler.EnqueueRequestsFromMapFunc(r.stackitClusterRequestsForCloudInitRef)). + Named("stackitcluster"). + Complete(r) +} + // +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=stackitclusters,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=stackitclusters/status,verbs=get;update;patch // +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=stackitclusters/finalizers,verbs=update @@ -188,18 +214,339 @@ func (r *StackitClusterReconciler) stackitClusterRequestsForCloudInitRef(ctx con return requests } -// SetupWithManager registers the controller with the manager. -func (r *StackitClusterReconciler) SetupWithManager(mgr ctrl.Manager) error { - return ctrl.NewControllerManagedBy(mgr). - For(&infrav1.StackitCluster{}). - Watches(&clusterv1.Cluster{}, handler.EnqueueRequestsFromMapFunc(r.filterForStackitClusterResources)). - Watches( - &clusterv1.Machine{}, - handler.EnqueueRequestsFromMapFunc(r.loadBalancerTargetsForControlPlanes), - builder.WithPredicates(predicate.Funcs{UpdateFunc: machineTargetPoolChanged}), - ). - Watches(&corev1.ConfigMap{}, handler.EnqueueRequestsFromMapFunc(r.stackitClusterRequestsForCloudInitRef)). - Watches(&corev1.Secret{}, handler.EnqueueRequestsFromMapFunc(r.stackitClusterRequestsForCloudInitRef)). - Named("stackitcluster"). - Complete(r) +func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterScope *scope.ClusterScope) (ctrl.Result, error) { + log := logf.FromContext(ctx) + stackitCluster := clusterScope.StackitCluster + + if !controllerutil.ContainsFinalizer(stackitCluster, infrav1.ClusterFinalizer) { + controllerutil.AddFinalizer(stackitCluster, infrav1.ClusterFinalizer) + // Persisted immediately, before any cloud resource can be created, to + // ensure nothing is running behind an object that carries no finalizer to + // clean it up. + if err := clusterScope.PatchObject(ctx); err != nil { + return ctrl.Result{}, fmt.Errorf("persist finalizer: %w", err) + } + } + clusterScope.SetFailureDomains(scope.DefaultFailureDomains(stackitCluster.Spec.Region)) + + cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, stackitCluster) + if err != nil { + log.Error(err, "Failed to build cloud client for cluster", "cluster", stackitCluster.Name) + record.Warnf(stackitCluster, "InvalidCredentials", "Failed to build cloud client: %v", err) + return clusterScope.CredentialFailureResult( + err, + credentialsRetryRequeueAfter, + infrav1.ClusterCredentialsReadyCondition, + infrav1.ClusterReadyCondition, + ) + } + clusterScope.SetCloudClient(cloudClient) + clusterScope.SetConditions( + metav1.ConditionTrue, + "Available", + "", + infrav1.ClusterCredentialsReadyCondition, + ) + + if err := availabilityzone.New(clusterScope).Reconcile(ctx); err != nil { + return clusterScope.CloudFailureResult( + "AvailabilityZonesError", + err, + retryableErrorRequeueAfter, + true, + infrav1.ClusterReadyCondition, + ) + } + + if err := network.New(clusterScope).Reconcile(ctx); err != nil { + if stackitCluster.Spec.Network.ID != "" { + log.Error(err, "Failed to get network", + "networkID", stackitCluster.Spec.Network.ID, + "region", stackitCluster.Spec.Region, + ) + record.Warnf( + stackitCluster, "FailedGetNetwork", + "Failed to get network %s in region %s: %v", + stackitCluster.Spec.Network.ID, stackitCluster.Spec.Region, err, + ) + return clusterScope.CloudFailureResult( + "NetworkNotFound", + err, + retryableErrorRequeueAfter, + false, + infrav1.ClusterNetworkReadyCondition, + infrav1.ClusterReadyCondition, + ) + } + log.Error(err, "Failed to reconcile managed network", + "cluster", stackitCluster.Name, + "region", stackitCluster.Spec.Region, + ) + record.Warnf( + stackitCluster, "FailedCreateNetwork", + "Failed to reconcile managed network in region %s: %v", + stackitCluster.Spec.Region, err, + ) + return clusterScope.CloudFailureResult( + "NetworkCreateError", + err, + retryableErrorRequeueAfter, + false, + infrav1.ClusterNetworkReadyCondition, + infrav1.ClusterReadyCondition, + ) + } + if clusterScope.NetworkCreated() { + record.Eventf(stackitCluster, "NetworkCreated", "Created managed network %s", stackitCluster.Status.Network.ID) + } + clusterScope.SetConditions( + metav1.ConditionTrue, + "Available", + "", + infrav1.ClusterNetworkReadyCondition, + ) + + if stackitCluster.Spec.APIServerLoadBalancer.Enabled { + // STACKIT takes an explicit target list rather than label-based + // membership, so exactly one reconciler owns the pool. Machines being + // deleted drop out here, before the node is drained. + machines, err := collections.GetFilteredMachinesForCluster( + ctx, + r.Client, + clusterScope.Cluster, + collections.ControlPlaneMachines(clusterScope.Cluster.Name), + collections.ActiveMachines, + ) + if err != nil { + clusterScope.SetConditions( + metav1.ConditionFalse, + "MachineListError", + err.Error(), + infrav1.ClusterLoadBalancerReadyCondition, + ) + return ctrl.Result{}, fmt.Errorf("list control plane machines: %w", err) + } + targets := loadbalancer.APIServerTargets(machines.UnsortedList()) + + // Seeding with the real targets lets a load balancer deleted out of band + // come back with the control plane behind it. + seed := targets + if len(seed) == 0 { + seed = []loadbalancer.Target{loadbalancer.BootstrapTarget(clusterScope.BootstrapTargetIP())} + } + clusterScope.SetAPIServerLoadBalancerTargets(targets, seed) + + previousEndpointHost := stackitCluster.Status.APIServerEndpoint.Host + wasLBReady := meta.IsStatusConditionTrue(stackitCluster.Status.Conditions, infrav1.ClusterLoadBalancerReadyCondition) + + err = loadbalancer.New(clusterScope).Reconcile(ctx) + if clusterScope.APIServerLoadBalancerCreated() { + record.Eventf( + stackitCluster, "LoadBalancerCreated", + "Created API server load balancer %s", stackitCluster.Status.APIServerLoadBalancerID, + ) + } + if loadbalancer.IsTargetPoolUpdateError(err) { + if !shared.IsRetryable(err) { + record.Warnf( + stackitCluster, "LoadBalancerTargetError", + "Cannot update API server load balancer target pool: %v", err, + ) + } + // Scoped to the load balancer condition: failing the cluster would + // stop the machine reconciler from replacing the broken machine. + clusterScope.SetConditions( + metav1.ConditionFalse, + "LoadBalancerTargetError", + err.Error(), + infrav1.ClusterLoadBalancerReadyCondition, + ) + // Requeue even on a permanent error: this return skips SetReady, so + // no machine exists yet whose events could retry it. + return ctrl.Result{RequeueAfter: retryableErrorRequeueAfter}, nil + } + if err != nil { + log.Error(err, "Failed to create API server load balancer", + "cluster", stackitCluster.Name, + "region", stackitCluster.Spec.Region, + ) + record.Warnf( + stackitCluster, "FailedCreateLoadBalancer", + "Failed to create API server load balancer: %v", err, + ) + return clusterScope.CloudFailureResult( + "LoadBalancerError", + err, + retryableErrorRequeueAfter, + false, + infrav1.ClusterLoadBalancerReadyCondition, + infrav1.ClusterReadyCondition, + ) + } + if stackitCluster.Status.APIServerEndpoint.Host == "" { + clusterScope.SetNotReady( + "Provisioning", + "waiting for API server load balancer IP address", + infrav1.ClusterLoadBalancerReadyCondition, + infrav1.ClusterReadyCondition, + ) + return ctrl.Result{RequeueAfter: 10 * time.Second}, nil + } + + isLBReady := wasLBReady && previousEndpointHost == stackitCluster.Status.APIServerEndpoint.Host + if !isLBReady { + clusterScope.SetConditions( + metav1.ConditionTrue, + "Available", + "", + infrav1.ClusterLoadBalancerReadyCondition, + ) + record.Eventf( + stackitCluster, "LoadBalancerReady", + "API server load balancer is ready at %s", stackitCluster.Status.APIServerEndpoint.Host, + ) + } + } else if stackitCluster.Spec.ControlPlaneEndpoint.Host != "" { + stackitCluster.Status.APIServerEndpoint = stackitCluster.Spec.ControlPlaneEndpoint + clusterScope.SetConditions( + metav1.ConditionTrue, + "Skipped", + "external endpoint provided", + infrav1.ClusterLoadBalancerReadyCondition, + ) + } else { + log.Error(errors.New("controlPlaneEndpoint host is empty"), "API server load balancer is disabled and controlPlaneEndpoint is empty", "cluster", stackitCluster.Name) + record.Warn(stackitCluster, "EndpointMissing", "apiServerLoadBalancer.enabled is false and controlPlaneEndpoint is empty") + clusterScope.SetNotReady( + "EndpointMissing", + "apiServerLoadBalancer.enabled is false and controlPlaneEndpoint is empty", + infrav1.ClusterLoadBalancerReadyCondition, + infrav1.ClusterReadyCondition, + ) + return ctrl.Result{}, nil + } + + if result, ready, err := r.reconcileBastion(ctx, clusterScope); err != nil { + log.Error(err, "Failed to reconcile bastion", + "cluster", stackitCluster.Name, + ) + record.Warnf( + stackitCluster, "FailedCreateBastion", + "Failed to reconcile bastion: %v", err, + ) + return clusterScope.CloudFailureResult( + "BastionError", + err, + retryableErrorRequeueAfter, + false, + infrav1.ClusterBastionReadyCondition, + infrav1.ClusterReadyCondition, + ) + } else if !ready { + return result, nil + } + + clusterScope.SetReady() + log.V(1).Info("Reconciled StackitCluster", "endpoint", stackitCluster.Status.APIServerEndpoint) + return ctrl.Result{}, nil +} + +func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterScope *scope.ClusterScope) (ctrl.Result, error) { + log := logf.FromContext(ctx) + stackitCluster := clusterScope.StackitCluster + + // Machines resolve their credentials and project context through this + // StackitCluster, so the finalizer has to stay while any of them remain. + machines, err := collections.GetFilteredMachinesForCluster(ctx, r.Client, clusterScope.Cluster) + if err != nil { + log.Error(err, "Failed to list Machines for cluster during deletion", "cluster", clusterScope.Cluster.Name) + record.Warnf(stackitCluster, "FailedListMachines", + "Failed to list Machines for cluster %s: %v", clusterScope.Cluster.Name, err) + return ctrl.Result{}, fmt.Errorf("list Machines for cluster %s: %w", clusterScope.Cluster.Name, err) + } + if machines.Len() > 0 { + log.Info( + "Waiting for Machines to be deleted before removing the StackitCluster finalizer", + "remainingMachines", machines.Len(), + ) + return ctrl.Result{RequeueAfter: deleteRequeueAfter}, nil + } + + // Cleanup runs unconditionally: neither spec nor status is a trustworthy + // record of what exists in the cloud. The load balancer, public IP, + // security group, server, and network services fall back to tag lookups and + // tolerate NotFound. + cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, stackitCluster) + if err != nil { + // A missing credentials Secret cannot be recovered from and commonly + // disappears first during namespace teardown, so finalize and make the + // possible leak loud rather than stranding the cluster in Terminating. + // Every other credentials problem is fixable and keeps retrying. + if apierrors.IsNotFound(err) { + log.Error(err, "Credentials secret is gone; finalizing without cloud cleanup", "cluster", stackitCluster.Name) + record.Warnf(stackitCluster, "CleanupSkipped", + "Credentials Secret is gone; finalizing without cloud cleanup. "+ + "Any remaining STACKIT resources for this cluster must be removed manually: %v", err) + controllerutil.RemoveFinalizer(stackitCluster, infrav1.ClusterFinalizer) + return ctrl.Result{}, nil + } + log.Error(err, "Failed to build cloud client for cluster deletion", "cluster", stackitCluster.Name) + record.Warnf(stackitCluster, "InvalidCredentials", + "Failed to build cloud client for deletion: %v", err) + return clusterScope.CredentialFailureResult( + err, + credentialsRetryRequeueAfter, + infrav1.ClusterCredentialsReadyCondition, + ) + } + clusterScope.SetCloudClient(cloudClient) + loadBalancerID := stackitCluster.Status.APIServerLoadBalancerID + if err := loadbalancer.New(clusterScope).Delete(ctx); err != nil { + log.Error(err, "Failed to delete API server load balancer", "cluster", stackitCluster.Name, "loadBalancerID", loadBalancerID) + record.Warnf(stackitCluster, "FailedDeleteLoadBalancer", + "Failed to delete API server load balancer %s: %v", loadBalancerID, err) + return ctrl.Result{}, err + } + if loadBalancerID != "" { + record.Eventf( + stackitCluster, "LoadBalancerDeleted", + "Deleted API server load balancer %s", loadBalancerID, + ) + } + if err := deleteBastion(ctx, clusterScope); err != nil { + log.Error(err, "Failed to delete bastion", "cluster", stackitCluster.Name) + record.Warnf(stackitCluster, "FailedDeleteBastion", + "Failed to delete bastion: %v", err) + return ctrl.Result{}, err + } + networkID := stackitCluster.Status.Network.ID + if err := network.New(clusterScope).Delete(ctx); err != nil { + if shared.IsConflict(err) { + log.Info("Managed network still has resources attached; requeueing deletion", + "cluster", stackitCluster.Name, + "networkID", networkID, + ) + clusterScope.SetConditions( + metav1.ConditionFalse, + "NetworkInUse", + err.Error(), + infrav1.ClusterNetworkReadyCondition, + ) + record.Warnf(stackitCluster, "NetworkInUse", + "Managed network %s still has active resources attached; retrying deletion: %v", networkID, err) + return ctrl.Result{RequeueAfter: deleteRequeueAfter}, nil + } + log.Error(err, "Failed to delete managed network", + "cluster", stackitCluster.Name, + "networkID", networkID, + ) + record.Warnf(stackitCluster, "FailedDeleteNetwork", + "Failed to delete managed network %s: %v", networkID, err) + return ctrl.Result{}, err + } + if networkID != "" && stackitCluster.Spec.Network.ID == "" { + record.Eventf(stackitCluster, "NetworkDeleted", "Deleted managed network %s", networkID) + } + controllerutil.RemoveFinalizer(stackitCluster, infrav1.ClusterFinalizer) + return ctrl.Result{}, nil } diff --git a/controller/stackitcluster_infrastructure.go b/controller/stackitcluster_infrastructure.go deleted file mode 100644 index db44116..0000000 --- a/controller/stackitcluster_infrastructure.go +++ /dev/null @@ -1,372 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -package controller - -import ( - "context" - "errors" - "fmt" - "time" - - apierrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/meta" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/cluster-api/util/collections" - "sigs.k8s.io/cluster-api/util/record" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - logf "sigs.k8s.io/controller-runtime/pkg/log" - - infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/availabilityzone" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared" - "github.com/stackitcloud/cluster-api-provider-stackit/scope" - "github.com/stackitcloud/cluster-api-provider-stackit/util" -) - -func (r *StackitClusterReconciler) reconcileNormal(ctx context.Context, clusterScope *scope.ClusterScope) (ctrl.Result, error) { - log := logf.FromContext(ctx) - stackitCluster := clusterScope.StackitCluster - - if !controllerutil.ContainsFinalizer(stackitCluster, infrav1.ClusterFinalizer) { - controllerutil.AddFinalizer(stackitCluster, infrav1.ClusterFinalizer) - // Persisted immediately, before any cloud resource can be created, to - // ensure nothing is running behind an object that carries no finalizer to - // clean it up. - if err := clusterScope.PatchObject(ctx); err != nil { - return ctrl.Result{}, fmt.Errorf("persist finalizer: %w", err) - } - } - clusterScope.SetFailureDomains(scope.DefaultFailureDomains(stackitCluster.Spec.Region)) - - cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, stackitCluster) - if err != nil { - log.Error(err, "Failed to build cloud client for cluster", "cluster", stackitCluster.Name) - record.Warnf(stackitCluster, "InvalidCredentials", "Failed to build cloud client: %v", err) - return clusterScope.CredentialFailureResult( - err, - credentialsRetryRequeueAfter, - infrav1.ClusterCredentialsReadyCondition, - infrav1.ClusterReadyCondition, - ) - } - clusterScope.SetCloudClient(cloudClient) - clusterScope.SetConditions( - metav1.ConditionTrue, - "Available", - "", - infrav1.ClusterCredentialsReadyCondition, - ) - - if err := availabilityzone.New(clusterScope).Reconcile(ctx); err != nil { - return clusterScope.CloudFailureResult( - "AvailabilityZonesError", - err, - retryableErrorRequeueAfter, - true, - infrav1.ClusterReadyCondition, - ) - } - - if err := network.New(clusterScope).Reconcile(ctx); err != nil { - if stackitCluster.Spec.Network.ID != "" { - log.Error(err, "Failed to get network", - "networkID", stackitCluster.Spec.Network.ID, - "region", stackitCluster.Spec.Region, - ) - record.Warnf( - stackitCluster, "FailedGetNetwork", - "Failed to get network %s in region %s: %v", - stackitCluster.Spec.Network.ID, stackitCluster.Spec.Region, err, - ) - return clusterScope.CloudFailureResult( - "NetworkNotFound", - err, - retryableErrorRequeueAfter, - false, - infrav1.ClusterNetworkReadyCondition, - infrav1.ClusterReadyCondition, - ) - } - log.Error(err, "Failed to reconcile managed network", - "cluster", stackitCluster.Name, - "region", stackitCluster.Spec.Region, - ) - record.Warnf( - stackitCluster, "FailedCreateNetwork", - "Failed to reconcile managed network in region %s: %v", - stackitCluster.Spec.Region, err, - ) - return clusterScope.CloudFailureResult( - "NetworkCreateError", - err, - retryableErrorRequeueAfter, - false, - infrav1.ClusterNetworkReadyCondition, - infrav1.ClusterReadyCondition, - ) - } - if clusterScope.NetworkCreated() { - record.Eventf(stackitCluster, "NetworkCreated", "Created managed network %s", stackitCluster.Status.Network.ID) - } - clusterScope.SetConditions( - metav1.ConditionTrue, - "Available", - "", - infrav1.ClusterNetworkReadyCondition, - ) - - if stackitCluster.Spec.APIServerLoadBalancer.Enabled { - // STACKIT takes an explicit target list rather than label-based - // membership, so exactly one reconciler owns the pool. Machines being - // deleted drop out here, before the node is drained. - machines, err := collections.GetFilteredMachinesForCluster( - ctx, - r.Client, - clusterScope.Cluster, - collections.ControlPlaneMachines(clusterScope.Cluster.Name), - collections.ActiveMachines, - ) - if err != nil { - clusterScope.SetConditions( - metav1.ConditionFalse, - "MachineListError", - err.Error(), - infrav1.ClusterLoadBalancerReadyCondition, - ) - return ctrl.Result{}, fmt.Errorf("list control plane machines: %w", err) - } - targets := loadbalancer.APIServerTargets(machines.UnsortedList()) - - // Seeding with the real targets lets a load balancer deleted out of band - // come back with the control plane behind it. - seed := targets - if len(seed) == 0 { - seed = []loadbalancer.Target{loadbalancer.BootstrapTarget(clusterScope.BootstrapTargetIP())} - } - clusterScope.SetAPIServerLoadBalancerTargets(targets, seed) - - previousEndpointHost := stackitCluster.Status.APIServerEndpoint.Host - wasLBReady := meta.IsStatusConditionTrue(stackitCluster.Status.Conditions, infrav1.ClusterLoadBalancerReadyCondition) - - err = loadbalancer.New(clusterScope).Reconcile(ctx) - if clusterScope.APIServerLoadBalancerCreated() { - record.Eventf( - stackitCluster, "LoadBalancerCreated", - "Created API server load balancer %s", stackitCluster.Status.APIServerLoadBalancerID, - ) - } - if loadbalancer.IsTargetPoolUpdateError(err) { - if !shared.IsRetryable(err) { - record.Warnf( - stackitCluster, "LoadBalancerTargetError", - "Cannot update API server load balancer target pool: %v", err, - ) - } - // Scoped to the load balancer condition: failing the cluster would - // stop the machine reconciler from replacing the broken machine. - clusterScope.SetConditions( - metav1.ConditionFalse, - "LoadBalancerTargetError", - err.Error(), - infrav1.ClusterLoadBalancerReadyCondition, - ) - // Requeue even on a permanent error: this return skips SetReady, so - // no machine exists yet whose events could retry it. - return ctrl.Result{RequeueAfter: retryableErrorRequeueAfter}, nil - } - if err != nil { - log.Error(err, "Failed to create API server load balancer", - "cluster", stackitCluster.Name, - "region", stackitCluster.Spec.Region, - ) - record.Warnf( - stackitCluster, "FailedCreateLoadBalancer", - "Failed to create API server load balancer: %v", err, - ) - return clusterScope.CloudFailureResult( - "LoadBalancerError", - err, - retryableErrorRequeueAfter, - false, - infrav1.ClusterLoadBalancerReadyCondition, - infrav1.ClusterReadyCondition, - ) - } - if stackitCluster.Status.APIServerEndpoint.Host == "" { - clusterScope.SetNotReady( - "Provisioning", - "waiting for API server load balancer IP address", - infrav1.ClusterLoadBalancerReadyCondition, - infrav1.ClusterReadyCondition, - ) - return ctrl.Result{RequeueAfter: 10 * time.Second}, nil - } - - isLBReady := wasLBReady && previousEndpointHost == stackitCluster.Status.APIServerEndpoint.Host - if !isLBReady { - clusterScope.SetConditions( - metav1.ConditionTrue, - "Available", - "", - infrav1.ClusterLoadBalancerReadyCondition, - ) - record.Eventf( - stackitCluster, "LoadBalancerReady", - "API server load balancer is ready at %s", stackitCluster.Status.APIServerEndpoint.Host, - ) - } - } else if stackitCluster.Spec.ControlPlaneEndpoint.Host != "" { - stackitCluster.Status.APIServerEndpoint = stackitCluster.Spec.ControlPlaneEndpoint - clusterScope.SetConditions( - metav1.ConditionTrue, - "Skipped", - "external endpoint provided", - infrav1.ClusterLoadBalancerReadyCondition, - ) - } else { - log.Error(errors.New("controlPlaneEndpoint host is empty"), "API server load balancer is disabled and controlPlaneEndpoint is empty", "cluster", stackitCluster.Name) - record.Warn(stackitCluster, "EndpointMissing", "apiServerLoadBalancer.enabled is false and controlPlaneEndpoint is empty") - clusterScope.SetNotReady( - "EndpointMissing", - "apiServerLoadBalancer.enabled is false and controlPlaneEndpoint is empty", - infrav1.ClusterLoadBalancerReadyCondition, - infrav1.ClusterReadyCondition, - ) - return ctrl.Result{}, nil - } - - if result, ready, err := r.reconcileBastion(ctx, clusterScope); err != nil { - log.Error(err, "Failed to reconcile bastion", - "cluster", stackitCluster.Name, - ) - record.Warnf( - stackitCluster, "FailedCreateBastion", - "Failed to reconcile bastion: %v", err, - ) - return clusterScope.CloudFailureResult( - "BastionError", - err, - retryableErrorRequeueAfter, - false, - infrav1.ClusterBastionReadyCondition, - infrav1.ClusterReadyCondition, - ) - } else if !ready { - return result, nil - } - - clusterScope.SetReady() - log.V(1).Info("Reconciled StackitCluster", "endpoint", stackitCluster.Status.APIServerEndpoint) - return ctrl.Result{}, nil -} - -func (r *StackitClusterReconciler) reconcileDelete(ctx context.Context, clusterScope *scope.ClusterScope) (ctrl.Result, error) { - log := logf.FromContext(ctx) - stackitCluster := clusterScope.StackitCluster - - // Machines resolve their credentials and project context through this - // StackitCluster, so the finalizer has to stay while any of them remain. - machines, err := collections.GetFilteredMachinesForCluster(ctx, r.Client, clusterScope.Cluster) - if err != nil { - log.Error(err, "Failed to list Machines for cluster during deletion", "cluster", clusterScope.Cluster.Name) - record.Warnf(stackitCluster, "FailedListMachines", - "Failed to list Machines for cluster %s: %v", clusterScope.Cluster.Name, err) - return ctrl.Result{}, fmt.Errorf("list Machines for cluster %s: %w", clusterScope.Cluster.Name, err) - } - if machines.Len() > 0 { - log.Info( - "Waiting for Machines to be deleted before removing the StackitCluster finalizer", - "remainingMachines", machines.Len(), - ) - return ctrl.Result{RequeueAfter: deleteRequeueAfter}, nil - } - - // Cleanup runs unconditionally: neither spec nor status is a trustworthy - // record of what exists in the cloud. The load balancer, public IP, - // security group, server, and network services fall back to tag lookups and - // tolerate NotFound. - cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, stackitCluster) - if err != nil { - // A missing credentials Secret cannot be recovered from and commonly - // disappears first during namespace teardown, so finalize and make the - // possible leak loud rather than stranding the cluster in Terminating. - // Every other credentials problem is fixable and keeps retrying. - if apierrors.IsNotFound(err) { - log.Error(err, "Credentials secret is gone; finalizing without cloud cleanup", "cluster", stackitCluster.Name) - record.Warnf(stackitCluster, "CleanupSkipped", - "Credentials Secret is gone; finalizing without cloud cleanup. "+ - "Any remaining STACKIT resources for this cluster must be removed manually: %v", err) - controllerutil.RemoveFinalizer(stackitCluster, infrav1.ClusterFinalizer) - return ctrl.Result{}, nil - } - log.Error(err, "Failed to build cloud client for cluster deletion", "cluster", stackitCluster.Name) - record.Warnf(stackitCluster, "InvalidCredentials", - "Failed to build cloud client for deletion: %v", err) - return clusterScope.CredentialFailureResult( - err, - credentialsRetryRequeueAfter, - infrav1.ClusterCredentialsReadyCondition, - ) - } - clusterScope.SetCloudClient(cloudClient) - loadBalancerID := stackitCluster.Status.APIServerLoadBalancerID - if err := loadbalancer.New(clusterScope).Delete(ctx); err != nil { - log.Error(err, "Failed to delete API server load balancer", "cluster", stackitCluster.Name, "loadBalancerID", loadBalancerID) - record.Warnf(stackitCluster, "FailedDeleteLoadBalancer", - "Failed to delete API server load balancer %s: %v", loadBalancerID, err) - return ctrl.Result{}, err - } - if loadBalancerID != "" { - record.Eventf( - stackitCluster, "LoadBalancerDeleted", - "Deleted API server load balancer %s", loadBalancerID, - ) - } - if err := deleteBastion(ctx, clusterScope); err != nil { - log.Error(err, "Failed to delete bastion", "cluster", stackitCluster.Name) - record.Warnf(stackitCluster, "FailedDeleteBastion", - "Failed to delete bastion: %v", err) - return ctrl.Result{}, err - } - networkID := stackitCluster.Status.Network.ID - if err := network.New(clusterScope).Delete(ctx); err != nil { - if shared.IsConflict(err) { - log.Info("Managed network still has resources attached; requeueing deletion", - "cluster", stackitCluster.Name, - "networkID", networkID, - ) - clusterScope.SetConditions( - metav1.ConditionFalse, - "NetworkInUse", - err.Error(), - infrav1.ClusterNetworkReadyCondition, - ) - record.Warnf(stackitCluster, "NetworkInUse", - "Managed network %s still has active resources attached; retrying deletion: %v", networkID, err) - return ctrl.Result{RequeueAfter: deleteRequeueAfter}, nil - } - log.Error(err, "Failed to delete managed network", - "cluster", stackitCluster.Name, - "networkID", networkID, - ) - record.Warnf(stackitCluster, "FailedDeleteNetwork", - "Failed to delete managed network %s: %v", networkID, err) - return ctrl.Result{}, err - } - if networkID != "" && stackitCluster.Spec.Network.ID == "" { - record.Eventf(stackitCluster, "NetworkDeleted", "Deleted managed network %s", networkID) - } - controllerutil.RemoveFinalizer(stackitCluster, infrav1.ClusterFinalizer) - return ctrl.Result{}, nil -} diff --git a/controller/stackitmachine_controller.go b/controller/stackitmachine_controller.go index 29c60d8..b8b5de4 100644 --- a/controller/stackitmachine_controller.go +++ b/controller/stackitmachine_controller.go @@ -18,20 +18,31 @@ package controller import ( "context" + "errors" "fmt" + "time" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" clusterutil "sigs.k8s.io/cluster-api/util" + "sigs.k8s.io/cluster-api/util/collections" + "sigs.k8s.io/cluster-api/util/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/reconcile" infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" "github.com/stackitcloud/cluster-api-provider-stackit/cloud" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" + "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" "github.com/stackitcloud/cluster-api-provider-stackit/scope" "github.com/stackitcloud/cluster-api-provider-stackit/util" ) @@ -45,6 +56,17 @@ type StackitMachineReconciler struct { CloudClientFactory cloud.Factory } +// SetupWithManager registers the controller with the manager. +func (r *StackitMachineReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&infrav1.StackitMachine{}). + Watches(&clusterv1.Machine{}, handler.EnqueueRequestsFromMapFunc(r.stackitMachineRequestsForMachine)). + Watches(&infrav1.StackitCluster{}, handler.EnqueueRequestsFromMapFunc(r.stackitMachineRequestsForStackitCluster)). + Watches(&corev1.Secret{}, handler.EnqueueRequestsFromMapFunc(r.stackitMachineRequestsForBootstrapSecret)). + Named("stackitmachine"). + Complete(r) +} + // +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=stackitmachines,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=stackitmachines/status,verbs=get;update;patch // +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=stackitmachines/finalizers,verbs=update @@ -115,13 +137,329 @@ func (r *StackitMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque return r.reconcileNormal(ctx, machineScope) } -// SetupWithManager registers the controller with the manager. -func (r *StackitMachineReconciler) SetupWithManager(mgr ctrl.Manager) error { - return ctrl.NewControllerManagedBy(mgr). - For(&infrav1.StackitMachine{}). - Watches(&clusterv1.Machine{}, handler.EnqueueRequestsFromMapFunc(r.stackitMachineRequestsForMachine)). - Watches(&infrav1.StackitCluster{}, handler.EnqueueRequestsFromMapFunc(r.stackitMachineRequestsForStackitCluster)). - Watches(&corev1.Secret{}, handler.EnqueueRequestsFromMapFunc(r.stackitMachineRequestsForBootstrapSecret)). - Named("stackitmachine"). - Complete(r) +func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineScope *scope.MachineScope) (ctrl.Result, error) { + log := logf.FromContext(ctx) + stackitMachine := machineScope.StackitMachine + + if !controllerutil.ContainsFinalizer(stackitMachine, infrav1.MachineFinalizer) { + controllerutil.AddFinalizer(stackitMachine, infrav1.MachineFinalizer) + // Persisted immediately, before CreateServer can run, to ensure no servers + // are running behind an object that carries no finalizer to clean it up. + if err := machineScope.PatchObject(ctx); err != nil { + return ctrl.Result{}, fmt.Errorf("persist finalizer: %w", err) + } + } + + if !machineScope.StackitCluster.Status.Ready { + log.Info("Waiting for StackitCluster to be ready", "machine", stackitMachine.Name, "cluster", machineScope.StackitCluster.Name) + machineScope.SetNotReady("InfrastructureNotReady", "waiting for StackitCluster to be ready", infrav1.MachineReadyCondition) + return ctrl.Result{}, nil + } + if err := validateMachineAvailabilityZone(machineScope); err != nil { + log.Error(err, "Invalid availability zone for machine", "machine", stackitMachine.Name, "availabilityZone", stackitMachine.Spec.AvailabilityZone) + record.Warnf(stackitMachine, "InvalidFailureDomain", "%v", err) + machineScope.SetNotReady( + "InvalidFailureDomain", + err.Error(), + infrav1.MachineInstanceReadyCondition, + infrav1.MachineReadyCondition, + ) + return ctrl.Result{}, nil + } + + bootstrapData, conditionStatus, reason, message := r.fetchBootstrapData(ctx, machineScope.Machine) + machineScope.SetConditions(conditionStatus, reason, message, infrav1.MachineBootstrapReadyCondition) + if conditionStatus != metav1.ConditionTrue { + log.Error(errors.New(message), "Bootstrap data is not ready", "machine", stackitMachine.Name, "reason", reason) + record.Warnf(stackitMachine, reason, "Bootstrap data error: %s", message) + machineScope.SetConditions(metav1.ConditionFalse, reason, message, infrav1.MachineReadyCondition) + if reason == util.BootstrapReasonInvalid { + return ctrl.Result{}, nil + } + return ctrl.Result{RequeueAfter: retryableErrorRequeueAfter}, nil + } + + cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, machineScope.StackitCluster) + if err != nil { + log.Error(err, "Failed to build cloud client for machine", "machine", stackitMachine.Name) + record.Warnf(stackitMachine, "InvalidCredentials", "Failed to build cloud client: %v", err) + return machineScope.CredentialFailureResult( + err, + credentialsRetryRequeueAfter, + infrav1.MachineCredentialsReadyCondition, + infrav1.MachineReadyCondition, + ) + } + machineScope.SetCloudClient(cloudClient) + machineScope.SetConditions(metav1.ConditionTrue, "Available", "", infrav1.MachineCredentialsReadyCondition) + + machineScope.SetBootstrapData(bootstrapData) + err = server.New(machineScope).Reconcile(ctx) + if server.IsServerGone(err) { + machineScope.SetNotReady( + "InstanceNotFound", + err.Error(), + infrav1.MachineInstanceReadyCondition, + infrav1.MachineReadyCondition, + ) + record.Warnf( + stackitMachine, "InstanceNotFound", + "Server %s no longer exists; the Machine must be replaced", stackitMachine.Status.InstanceID, + ) + return ctrl.Result{}, nil + } + if err != nil { + log.Error(err, "Failed to create server", + "machine", stackitMachine.Name, + "imageID", stackitMachine.Spec.ImageID, + "networkID", machineScope.NetworkID(), + "region", machineScope.StackitCluster.Spec.Region, + ) + record.Warnf( + stackitMachine, "FailedCreateInstance", + "Failed to create server %s (imageID: %s, networkID: %s, region: %s): %v", + stackitMachine.Name, stackitMachine.Spec.ImageID, machineScope.NetworkID(), + machineScope.StackitCluster.Spec.Region, err, + ) + return machineScope.CloudFailureResult( + "InstanceError", + err, + retryableErrorRequeueAfter, + true, + infrav1.MachineInstanceReadyCondition, + infrav1.MachineReadyCondition, + ) + } + if machineScope.InstanceCreated() { + record.Eventf( + stackitMachine, "InstanceCreated", "Created instance %s", stackitMachine.Status.InstanceID, + ) + } + + if ready, reason, message, warn := server.StateCondition(stackitMachine.Status.InstanceState, machineScope.InstancePowerStatus()); !ready { + previousReason := "" + if previous := meta.FindStatusCondition(stackitMachine.Status.Conditions, infrav1.MachineInstanceReadyCondition); previous != nil { + previousReason = previous.Reason + } + machineScope.SetNotReady(reason, message, infrav1.MachineInstanceReadyCondition, infrav1.MachineReadyCondition) + // Only warn on entering the state, not on every requeue. + if warn && previousReason != reason { + record.Warnf(stackitMachine, reason, "Server %s: %s", stackitMachine.Status.InstanceID, message) + } + return ctrl.Result{RequeueAfter: 15 * time.Second}, nil + } + + if err := securitygroup.New(machineScope).Reconcile(ctx); err != nil { + log.Error(err, "Failed to configure bastion SSH access for node", "machine", stackitMachine.Name) + record.Warnf( + stackitMachine, "FailedNodeSSHAccess", + "Failed to configure bastion SSH access for server %s: %v", stackitMachine.Status.InstanceID, err, + ) + return machineScope.CloudFailureResult( + "BastionSSHAccessError", + err, + retryableErrorRequeueAfter, + true, + infrav1.MachineReadyCondition, + ) + } + + machineScope.SetReady() + log.V(1).Info("Reconciled StackitMachine", "providerID", stackitMachine.Status.ProviderID) + return ctrl.Result{}, nil +} + +func validateMachineAvailabilityZone(machineScope *scope.MachineScope) error { + clusterFailureDomains := machineScope.StackitCluster.Status.FailureDomains + if len(clusterFailureDomains) == 0 { + return nil + } + knownAZ := func(zone string) bool { + for _, failureDomain := range clusterFailureDomains { + if failureDomain.Name == zone { + return true + } + } + return false + } + if az := machineScope.StackitMachine.Spec.AvailabilityZone; az != "" && !knownAZ(az) { + return fmt.Errorf("availabilityZone %q is not published in StackitCluster status.failureDomains", az) + } + if machineScope.Machine != nil && machineScope.Machine.Spec.FailureDomain != "" { + fd := machineScope.Machine.Spec.FailureDomain + if !knownAZ(fd) { + return fmt.Errorf("machine failureDomain %q is not published in StackitCluster status.failureDomains", fd) + } + } + return nil +} + +func (r *StackitMachineReconciler) reconcileDelete(ctx context.Context, machineScope *scope.MachineScope) error { + log := logf.FromContext(ctx) + stackitMachine := machineScope.StackitMachine + + // An empty status.instanceID is not proof that no server exists, so every + // deletion asks the cloud before dropping the finalizer. + cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, machineScope.StackitCluster) + if err != nil { + // A missing credentials Secret cannot be recovered from and commonly + // disappears first during namespace teardown, so finalize and make the + // possible leak loud rather than stranding the Machine in Terminating. + // Every other credentials problem is fixable and keeps retrying. + if apierrors.IsNotFound(err) { + log.Error(err, "Credentials secret is gone; finalizing without cloud cleanup", "machine", stackitMachine.Name) + record.Warnf(stackitMachine, "CleanupSkipped", + "Credentials Secret is gone; finalizing without cloud cleanup. "+ + "Any remaining STACKIT server for this machine must be removed manually: %v", err) + controllerutil.RemoveFinalizer(stackitMachine, infrav1.MachineFinalizer) + return nil + } + log.Error(err, "Failed to build cloud client for machine deletion", "machine", stackitMachine.Name) + record.Warnf(stackitMachine, "InvalidCredentials", + "Failed to build cloud client for deletion: %v", err) + _, resultErr := machineScope.CredentialFailureResult( + err, + credentialsRetryRequeueAfter, + infrav1.MachineCredentialsReadyCondition, + ) + return resultErr + } + + machineScope.SetCloudClient(cloudClient) + instanceID := stackitMachine.Status.InstanceID + if err := server.New(machineScope).Delete(ctx); err != nil { + log.Error(err, "Failed to delete server", "machine", stackitMachine.Name, "instanceID", instanceID) + record.Warnf(stackitMachine, "FailedDeleteInstance", + "Failed to delete server %s: %v", instanceID, err) + return err + } + if instanceID != "" { + record.Eventf( + stackitMachine, "InstanceDeleted", "Deleted instance %s", instanceID, + ) + } + controllerutil.RemoveFinalizer(stackitMachine, infrav1.MachineFinalizer) + return nil +} + +func (r *StackitMachineReconciler) fetchBootstrapData(ctx context.Context, machine *clusterv1.Machine) ([]byte, metav1.ConditionStatus, string, string) { + if machine.Spec.Bootstrap.DataSecretName == nil || *machine.Spec.Bootstrap.DataSecretName == "" { + return nil, metav1.ConditionFalse, "BootstrapDataSecretMissing", "Machine.spec.bootstrap.dataSecretName is empty" + } + secret := &corev1.Secret{} + key := types.NamespacedName{Namespace: machine.Namespace, Name: *machine.Spec.Bootstrap.DataSecretName} + if err := r.Get(ctx, key, secret); err != nil { + if apierrors.IsNotFound(err) { + return nil, metav1.ConditionFalse, "BootstrapDataSecretNotFound", fmt.Sprintf("bootstrap secret %s not found", key) + } + return nil, metav1.ConditionFalse, "BootstrapDataSecretError", err.Error() + } + data, err := util.ExtractBootstrapData(secret) + if err != nil { + return nil, metav1.ConditionFalse, util.BootstrapReasonInvalid, err.Error() + } + return data, metav1.ConditionTrue, "Available", "" +} + +func (r *StackitMachineReconciler) getStackitCluster(ctx context.Context, cluster *clusterv1.Cluster) (*infrav1.StackitCluster, error) { + if cluster.Spec.InfrastructureRef.Name == "" { + return nil, nil + } + stackitCluster := &infrav1.StackitCluster{} + key := types.NamespacedName{Namespace: cluster.Namespace, Name: cluster.Spec.InfrastructureRef.Name} + if err := r.Get(ctx, key, stackitCluster); err != nil { + if apierrors.IsNotFound(err) { + return nil, nil + } + return nil, fmt.Errorf("get StackitCluster %s: %w", key, err) + } + return stackitCluster, nil +} + +func (r *StackitMachineReconciler) stackitMachineRequestsForMachine(_ context.Context, obj client.Object) []reconcile.Request { + machine, ok := obj.(*clusterv1.Machine) + if !ok { + return nil + } + return stackitMachineRequestForMachine(machine) +} + +func (r *StackitMachineReconciler) stackitMachineRequestsForStackitCluster(ctx context.Context, obj client.Object) []reconcile.Request { + stackitCluster, ok := obj.(*infrav1.StackitCluster) + if !ok { + return nil + } + + // Machine.spec.clusterName names the owning Cluster, not the StackitCluster, + // and a ClusterClass-generated infrastructureRef never shares its name. + cluster, err := clusterutil.GetOwnerCluster(ctx, r.Client, stackitCluster.ObjectMeta) + switch { + case apierrors.IsNotFound(err) || cluster == nil: + return nil + case err != nil: + logf.FromContext(ctx).Error(err, "Failed to get owning Cluster for StackitCluster watch", "stackitCluster", client.ObjectKeyFromObject(stackitCluster)) + return nil + } + + machines, err := collections.GetFilteredMachinesForCluster(ctx, r.Client, cluster) + if err != nil { + logf.FromContext(ctx).Error(err, "Failed to list Machines for StackitCluster watch", "stackitCluster", client.ObjectKeyFromObject(stackitCluster)) + return nil + } + + return stackitMachineRequestsForMachines(machines.UnsortedList(), func(machine *clusterv1.Machine) bool { + return machine.Spec.ClusterName == cluster.Name + }) +} + +func (r *StackitMachineReconciler) stackitMachineRequestsForBootstrapSecret(ctx context.Context, obj client.Object) []reconcile.Request { + secret, ok := obj.(*corev1.Secret) + if !ok { + return nil + } + + machines := &clusterv1.MachineList{} + if err := r.List(ctx, machines, client.InNamespace(secret.Namespace)); err != nil { + logf.FromContext(ctx).Error(err, "Failed to list Machines for bootstrap Secret watch", "secret", client.ObjectKeyFromObject(secret)) + return nil + } + + return stackitMachineRequestsForMachines(collections.FromMachineList(machines).UnsortedList(), func(machine *clusterv1.Machine) bool { + return machine.Spec.Bootstrap.DataSecretName != nil && + *machine.Spec.Bootstrap.DataSecretName == secret.Name + }) +} + +func stackitMachineRequestsForMachines(machines []*clusterv1.Machine, matches func(*clusterv1.Machine) bool) []reconcile.Request { + requests := make([]reconcile.Request, 0, len(machines)) + seen := map[types.NamespacedName]struct{}{} + for _, machine := range machines { + if !matches(machine) { + continue + } + for _, request := range stackitMachineRequestForMachine(machine) { + if _, ok := seen[request.NamespacedName]; ok { + continue + } + seen[request.NamespacedName] = struct{}{} + requests = append(requests, request) + } + } + return requests +} + +func stackitMachineRequestForMachine(machine *clusterv1.Machine) []reconcile.Request { + if machine == nil { + return nil + } + ref := machine.Spec.InfrastructureRef + if ref.APIGroup != infrav1.GroupVersion.Group || ref.Kind != "StackitMachine" || ref.Name == "" { + return nil + } + return []reconcile.Request{{ + NamespacedName: types.NamespacedName{ + Namespace: machine.Namespace, + Name: ref.Name, + }, + }} } diff --git a/controller/stackitmachine_infrastructure.go b/controller/stackitmachine_infrastructure.go deleted file mode 100644 index 19dab1f..0000000 --- a/controller/stackitmachine_infrastructure.go +++ /dev/null @@ -1,274 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -package controller - -import ( - "context" - "errors" - "fmt" - "time" - - corev1 "k8s.io/api/core/v1" - apierrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/meta" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" - clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" - "sigs.k8s.io/cluster-api/util/record" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - logf "sigs.k8s.io/controller-runtime/pkg/log" - - infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/securitygroup" - "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/server" - "github.com/stackitcloud/cluster-api-provider-stackit/scope" - "github.com/stackitcloud/cluster-api-provider-stackit/util" -) - -func (r *StackitMachineReconciler) reconcileNormal(ctx context.Context, machineScope *scope.MachineScope) (ctrl.Result, error) { - log := logf.FromContext(ctx) - stackitMachine := machineScope.StackitMachine - - if !controllerutil.ContainsFinalizer(stackitMachine, infrav1.MachineFinalizer) { - controllerutil.AddFinalizer(stackitMachine, infrav1.MachineFinalizer) - // Persisted immediately, before CreateServer can run, to ensure no servers - // are running behind an object that carries no finalizer to clean it up. - if err := machineScope.PatchObject(ctx); err != nil { - return ctrl.Result{}, fmt.Errorf("persist finalizer: %w", err) - } - } - - if !machineScope.StackitCluster.Status.Ready { - log.Info("Waiting for StackitCluster to be ready", "machine", stackitMachine.Name, "cluster", machineScope.StackitCluster.Name) - machineScope.SetNotReady("InfrastructureNotReady", "waiting for StackitCluster to be ready", infrav1.MachineReadyCondition) - return ctrl.Result{}, nil - } - if err := validateMachineAvailabilityZone(machineScope); err != nil { - log.Error(err, "Invalid availability zone for machine", "machine", stackitMachine.Name, "availabilityZone", stackitMachine.Spec.AvailabilityZone) - record.Warnf(stackitMachine, "InvalidFailureDomain", "%v", err) - machineScope.SetNotReady( - "InvalidFailureDomain", - err.Error(), - infrav1.MachineInstanceReadyCondition, - infrav1.MachineReadyCondition, - ) - return ctrl.Result{}, nil - } - - bootstrapData, conditionStatus, reason, message := r.fetchBootstrapData(ctx, machineScope.Machine) - machineScope.SetConditions(conditionStatus, reason, message, infrav1.MachineBootstrapReadyCondition) - if conditionStatus != metav1.ConditionTrue { - log.Error(errors.New(message), "Bootstrap data is not ready", "machine", stackitMachine.Name, "reason", reason) - record.Warnf(stackitMachine, reason, "Bootstrap data error: %s", message) - machineScope.SetConditions(metav1.ConditionFalse, reason, message, infrav1.MachineReadyCondition) - if reason == util.BootstrapReasonInvalid { - return ctrl.Result{}, nil - } - return ctrl.Result{RequeueAfter: retryableErrorRequeueAfter}, nil - } - - cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, machineScope.StackitCluster) - if err != nil { - log.Error(err, "Failed to build cloud client for machine", "machine", stackitMachine.Name) - record.Warnf(stackitMachine, "InvalidCredentials", "Failed to build cloud client: %v", err) - return machineScope.CredentialFailureResult( - err, - credentialsRetryRequeueAfter, - infrav1.MachineCredentialsReadyCondition, - infrav1.MachineReadyCondition, - ) - } - machineScope.SetCloudClient(cloudClient) - machineScope.SetConditions(metav1.ConditionTrue, "Available", "", infrav1.MachineCredentialsReadyCondition) - - machineScope.SetBootstrapData(bootstrapData) - err = server.New(machineScope).Reconcile(ctx) - if server.IsServerGone(err) { - machineScope.SetNotReady( - "InstanceNotFound", - err.Error(), - infrav1.MachineInstanceReadyCondition, - infrav1.MachineReadyCondition, - ) - record.Warnf( - stackitMachine, "InstanceNotFound", - "Server %s no longer exists; the Machine must be replaced", stackitMachine.Status.InstanceID, - ) - return ctrl.Result{}, nil - } - if err != nil { - log.Error(err, "Failed to create server", - "machine", stackitMachine.Name, - "imageID", stackitMachine.Spec.ImageID, - "networkID", machineScope.NetworkID(), - "region", machineScope.StackitCluster.Spec.Region, - ) - record.Warnf( - stackitMachine, "FailedCreateInstance", - "Failed to create server %s (imageID: %s, networkID: %s, region: %s): %v", - stackitMachine.Name, stackitMachine.Spec.ImageID, machineScope.NetworkID(), - machineScope.StackitCluster.Spec.Region, err, - ) - return machineScope.CloudFailureResult( - "InstanceError", - err, - retryableErrorRequeueAfter, - true, - infrav1.MachineInstanceReadyCondition, - infrav1.MachineReadyCondition, - ) - } - if machineScope.InstanceCreated() { - record.Eventf( - stackitMachine, "InstanceCreated", "Created instance %s", stackitMachine.Status.InstanceID, - ) - } - - if ready, reason, message, warn := serverStateCondition(stackitMachine.Status.InstanceState, machineScope.InstancePowerStatus()); !ready { - previousReason := "" - if previous := meta.FindStatusCondition(stackitMachine.Status.Conditions, infrav1.MachineInstanceReadyCondition); previous != nil { - previousReason = previous.Reason - } - machineScope.SetNotReady(reason, message, infrav1.MachineInstanceReadyCondition, infrav1.MachineReadyCondition) - // Only warn on entering the state, not on every requeue. - if warn && previousReason != reason { - record.Warnf(stackitMachine, reason, "Server %s: %s", stackitMachine.Status.InstanceID, message) - } - return ctrl.Result{RequeueAfter: 15 * time.Second}, nil - } - - if err := securitygroup.New(machineScope).Reconcile(ctx); err != nil { - log.Error(err, "Failed to configure bastion SSH access for node", "machine", stackitMachine.Name) - record.Warnf( - stackitMachine, "FailedNodeSSHAccess", - "Failed to configure bastion SSH access for server %s: %v", stackitMachine.Status.InstanceID, err, - ) - return machineScope.CloudFailureResult( - "BastionSSHAccessError", - err, - retryableErrorRequeueAfter, - true, - infrav1.MachineReadyCondition, - ) - } - - machineScope.SetReady() - log.V(1).Info("Reconciled StackitMachine", "providerID", stackitMachine.Status.ProviderID) - return ctrl.Result{}, nil -} - -func validateMachineAvailabilityZone(machineScope *scope.MachineScope) error { - clusterFailureDomains := machineScope.StackitCluster.Status.FailureDomains - if len(clusterFailureDomains) == 0 { - return nil - } - knownAZ := func(zone string) bool { - for _, failureDomain := range clusterFailureDomains { - if failureDomain.Name == zone { - return true - } - } - return false - } - if az := machineScope.StackitMachine.Spec.AvailabilityZone; az != "" && !knownAZ(az) { - return fmt.Errorf("availabilityZone %q is not published in StackitCluster status.failureDomains", az) - } - if machineScope.Machine != nil && machineScope.Machine.Spec.FailureDomain != "" { - fd := machineScope.Machine.Spec.FailureDomain - if !knownAZ(fd) { - return fmt.Errorf("machine failureDomain %q is not published in StackitCluster status.failureDomains", fd) - } - } - return nil -} - -func (r *StackitMachineReconciler) reconcileDelete(ctx context.Context, machineScope *scope.MachineScope) error { - log := logf.FromContext(ctx) - stackitMachine := machineScope.StackitMachine - - // An empty status.instanceID is not proof that no server exists, so every - // deletion asks the cloud before dropping the finalizer. - cloudClient, err := util.BuildCloudClient(ctx, r.Client, r.CloudClientFactory, machineScope.StackitCluster) - if err != nil { - // A missing credentials Secret cannot be recovered from and commonly - // disappears first during namespace teardown, so finalize and make the - // possible leak loud rather than stranding the Machine in Terminating. - // Every other credentials problem is fixable and keeps retrying. - if apierrors.IsNotFound(err) { - log.Error(err, "Credentials secret is gone; finalizing without cloud cleanup", "machine", stackitMachine.Name) - record.Warnf(stackitMachine, "CleanupSkipped", - "Credentials Secret is gone; finalizing without cloud cleanup. "+ - "Any remaining STACKIT server for this machine must be removed manually: %v", err) - controllerutil.RemoveFinalizer(stackitMachine, infrav1.MachineFinalizer) - return nil - } - log.Error(err, "Failed to build cloud client for machine deletion", "machine", stackitMachine.Name) - record.Warnf(stackitMachine, "InvalidCredentials", - "Failed to build cloud client for deletion: %v", err) - _, resultErr := machineScope.CredentialFailureResult( - err, - credentialsRetryRequeueAfter, - infrav1.MachineCredentialsReadyCondition, - ) - return resultErr - } - - machineScope.SetCloudClient(cloudClient) - instanceID := stackitMachine.Status.InstanceID - if err := server.New(machineScope).Delete(ctx); err != nil { - log.Error(err, "Failed to delete server", "machine", stackitMachine.Name, "instanceID", instanceID) - record.Warnf(stackitMachine, "FailedDeleteInstance", - "Failed to delete server %s: %v", instanceID, err) - return err - } - if instanceID != "" { - record.Eventf( - stackitMachine, "InstanceDeleted", "Deleted instance %s", instanceID, - ) - } - controllerutil.RemoveFinalizer(stackitMachine, infrav1.MachineFinalizer) - return nil -} - -func (r *StackitMachineReconciler) fetchBootstrapData(ctx context.Context, machine *clusterv1.Machine) ([]byte, metav1.ConditionStatus, string, string) { - if machine.Spec.Bootstrap.DataSecretName == nil || *machine.Spec.Bootstrap.DataSecretName == "" { - return nil, metav1.ConditionFalse, "BootstrapDataSecretMissing", "Machine.spec.bootstrap.dataSecretName is empty" - } - secret := &corev1.Secret{} - key := types.NamespacedName{Namespace: machine.Namespace, Name: *machine.Spec.Bootstrap.DataSecretName} - if err := r.Get(ctx, key, secret); err != nil { - if apierrors.IsNotFound(err) { - return nil, metav1.ConditionFalse, "BootstrapDataSecretNotFound", fmt.Sprintf("bootstrap secret %s not found", key) - } - return nil, metav1.ConditionFalse, "BootstrapDataSecretError", err.Error() - } - data, err := util.ExtractBootstrapData(secret) - if err != nil { - return nil, metav1.ConditionFalse, util.BootstrapReasonInvalid, err.Error() - } - return data, metav1.ConditionTrue, "Available", "" -} - -func (r *StackitMachineReconciler) getStackitCluster(ctx context.Context, cluster *clusterv1.Cluster) (*infrav1.StackitCluster, error) { - if cluster.Spec.InfrastructureRef.Name == "" { - return nil, nil - } - stackitCluster := &infrav1.StackitCluster{} - key := types.NamespacedName{Namespace: cluster.Namespace, Name: cluster.Spec.InfrastructureRef.Name} - if err := r.Get(ctx, key, stackitCluster); err != nil { - if apierrors.IsNotFound(err) { - return nil, nil - } - return nil, fmt.Errorf("get StackitCluster %s: %w", key, err) - } - return stackitCluster, nil -} diff --git a/controller/stackitmachine_watches.go b/controller/stackitmachine_watches.go deleted file mode 100644 index c8c9fbb..0000000 --- a/controller/stackitmachine_watches.go +++ /dev/null @@ -1,115 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -package controller - -import ( - "context" - - corev1 "k8s.io/api/core/v1" - apierrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/types" - clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" - clusterutil "sigs.k8s.io/cluster-api/util" - "sigs.k8s.io/cluster-api/util/collections" - "sigs.k8s.io/controller-runtime/pkg/client" - logf "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/reconcile" - - infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" -) - -func (r *StackitMachineReconciler) stackitMachineRequestsForMachine(_ context.Context, obj client.Object) []reconcile.Request { - machine, ok := obj.(*clusterv1.Machine) - if !ok { - return nil - } - return stackitMachineRequestForMachine(machine) -} - -func (r *StackitMachineReconciler) stackitMachineRequestsForStackitCluster(ctx context.Context, obj client.Object) []reconcile.Request { - stackitCluster, ok := obj.(*infrav1.StackitCluster) - if !ok { - return nil - } - - // Machine.spec.clusterName names the owning Cluster, not the StackitCluster, - // and a ClusterClass-generated infrastructureRef never shares its name. - cluster, err := clusterutil.GetOwnerCluster(ctx, r.Client, stackitCluster.ObjectMeta) - switch { - case apierrors.IsNotFound(err) || cluster == nil: - return nil - case err != nil: - logf.FromContext(ctx).Error(err, "Failed to get owning Cluster for StackitCluster watch", "stackitCluster", client.ObjectKeyFromObject(stackitCluster)) - return nil - } - - machines, err := collections.GetFilteredMachinesForCluster(ctx, r.Client, cluster) - if err != nil { - logf.FromContext(ctx).Error(err, "Failed to list Machines for StackitCluster watch", "stackitCluster", client.ObjectKeyFromObject(stackitCluster)) - return nil - } - - return stackitMachineRequestsForMachines(machines.UnsortedList(), func(machine *clusterv1.Machine) bool { - return machine.Spec.ClusterName == cluster.Name - }) -} - -func (r *StackitMachineReconciler) stackitMachineRequestsForBootstrapSecret(ctx context.Context, obj client.Object) []reconcile.Request { - secret, ok := obj.(*corev1.Secret) - if !ok { - return nil - } - - machines := &clusterv1.MachineList{} - if err := r.List(ctx, machines, client.InNamespace(secret.Namespace)); err != nil { - logf.FromContext(ctx).Error(err, "Failed to list Machines for bootstrap Secret watch", "secret", client.ObjectKeyFromObject(secret)) - return nil - } - - return stackitMachineRequestsForMachines(collections.FromMachineList(machines).UnsortedList(), func(machine *clusterv1.Machine) bool { - return machine.Spec.Bootstrap.DataSecretName != nil && - *machine.Spec.Bootstrap.DataSecretName == secret.Name - }) -} - -func stackitMachineRequestsForMachines(machines []*clusterv1.Machine, matches func(*clusterv1.Machine) bool) []reconcile.Request { - requests := make([]reconcile.Request, 0, len(machines)) - seen := map[types.NamespacedName]struct{}{} - for _, machine := range machines { - if !matches(machine) { - continue - } - for _, request := range stackitMachineRequestForMachine(machine) { - if _, ok := seen[request.NamespacedName]; ok { - continue - } - seen[request.NamespacedName] = struct{}{} - requests = append(requests, request) - } - } - return requests -} - -func stackitMachineRequestForMachine(machine *clusterv1.Machine) []reconcile.Request { - if machine == nil { - return nil - } - ref := machine.Spec.InfrastructureRef - if ref.APIGroup != infrav1.GroupVersion.Group || ref.Kind != "StackitMachine" || ref.Name == "" { - return nil - } - return []reconcile.Request{{ - NamespacedName: types.NamespacedName{ - Namespace: machine.Namespace, - Name: ref.Name, - }, - }} -} diff --git a/go.mod b/go.mod index dc54ea2..e140f31 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/stackitcloud/cluster-api-provider-stackit go 1.26 require ( + github.com/go-logr/logr v1.4.3 github.com/onsi/ginkgo/v2 v2.28.1 github.com/onsi/gomega v1.39.1 github.com/stackitcloud/stackit-sdk-go/core v0.26.0 @@ -30,7 +31,6 @@ require ( github.com/felixge/httpsnoop v1.0.4 // indirect github.com/fsnotify/fsnotify v1.9.0 // indirect github.com/fxamacker/cbor/v2 v2.9.0 // indirect - github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-logr/zapr v1.3.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect From f4e036eca0fafcea0899a9304576837a6d2d2be4 Mon Sep 17 00:00:00 2001 From: Jan Larwig Date: Wed, 18 Nov 2026 15:52:22 +0100 Subject: [PATCH 60/60] chore: cleanup unnecessary providerid file Signed-off-by: Jan Larwig --- cloud/cleanup.go | 66 +++++++++++----------- cloud/providerid.go | 32 ----------- cloud/providerid_test.go | 34 ------------ scope/machine_scope.go | 3 +- scope/scope_test.go | 117 --------------------------------------- 5 files changed, 35 insertions(+), 217 deletions(-) delete mode 100644 cloud/providerid.go delete mode 100644 cloud/providerid_test.go delete mode 100644 scope/scope_test.go diff --git a/cloud/cleanup.go b/cloud/cleanup.go index b547a28..67b391a 100644 --- a/cloud/cleanup.go +++ b/cloud/cleanup.go @@ -28,6 +28,39 @@ type cleanupScope struct { tags map[string]string } +// CleanupByTags deletes provider-managed cloud resources matching tags without +// relying on Kubernetes objects. Load balancers are deleted before servers so +// server deletion is not blocked by target attachments, servers are deleted +// before security groups so attached servers do not block security group +// deletion if server detachment fails or lags, and networks are deleted last. +func CleanupByTags(ctx context.Context, client Client, tags map[string]string) error { + if len(tags) == 0 { + return fmt.Errorf("%w: empty cleanup tag selector", shared.ErrInvalidInput) + } + + scope := &cleanupScope{ + Client: client, + tags: tags, + } + var errs []error + for _, deleteFn := range []func(context.Context) error{ + publicip.New(scope).Delete, + loadbalancer.New(scope).Delete, + server.New(scope).Delete, + securitygroup.New(scope).Delete, + network.New(scope).Delete, + } { + if err := deleteFn(ctx); err != nil { + errs = append(errs, err) + } + } + + if len(errs) > 0 { + return fmt.Errorf("%w: %w", shared.ErrTransient, errors.Join(errs...)) + } + return nil +} + func (s *cleanupScope) PublicIPSpec() publicip.PublicIPSpec { return publicip.PublicIPSpec{ Tags: s.tags, @@ -62,36 +95,3 @@ func (s *cleanupScope) NetworkSpec() network.NetworkSpec { Managed: true, } } - -// CleanupByTags deletes provider-managed cloud resources matching tags without -// relying on Kubernetes objects. Load balancers are deleted before servers so -// server deletion is not blocked by target attachments, servers are deleted -// before security groups so attached servers do not block security group -// deletion if server detachment fails or lags, and networks are deleted last. -func CleanupByTags(ctx context.Context, client Client, tags map[string]string) error { - if len(tags) == 0 { - return fmt.Errorf("%w: empty cleanup tag selector", shared.ErrInvalidInput) - } - - scope := &cleanupScope{ - Client: client, - tags: tags, - } - var errs []error - for _, deleteFn := range []func(context.Context) error{ - publicip.New(scope).Delete, - loadbalancer.New(scope).Delete, - server.New(scope).Delete, - securitygroup.New(scope).Delete, - network.New(scope).Delete, - } { - if err := deleteFn(ctx); err != nil { - errs = append(errs, err) - } - } - - if len(errs) > 0 { - return fmt.Errorf("%w: %w", shared.ErrTransient, errors.Join(errs...)) - } - return nil -} diff --git a/cloud/providerid.go b/cloud/providerid.go deleted file mode 100644 index e0d60e9..0000000 --- a/cloud/providerid.go +++ /dev/null @@ -1,32 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -package cloud - -import ( - "fmt" -) - -// providerIDScheme is the URL scheme used for STACKIT providerIDs. -const providerIDScheme = "stackit://" - -// NewProviderID returns a providerID string in the STACKIT format used by -// cloud-provider-stackit. -// -// Format verified against the local cloud-provider-stackit repository: -// - pkg/ccm/instances.go: Instances.makeInstanceID returns stackit:// -// -// Project ID and region are intentionally not encoded in the providerID. The -// cloud-provider-stackit controller gets project and region from its own -// configuration and then resolves Node.spec.providerID via GetServer(projectID, -// region, serverID). -func NewProviderID(serverID string) string { - return fmt.Sprintf("%s%s", providerIDScheme, serverID) -} diff --git a/cloud/providerid_test.go b/cloud/providerid_test.go deleted file mode 100644 index 5d25203..0000000 --- a/cloud/providerid_test.go +++ /dev/null @@ -1,34 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -package cloud - -import ( - "testing" -) - -func TestNewProviderID(t *testing.T) { - got := NewProviderID("server-id") - want := "stackit://server-id" - if got != want { - t.Fatalf("NewProviderID() = %q, want %q", got, want) - } -} - -func TestProviderIDMatchesCloudProviderStackitFormat(t *testing.T) { - serverID := "321a8b81-3660-43e5-bab8-6470b65ee4e8" - providerID := NewProviderID(serverID) - - // Verified against the local cloud-provider-stackit repository: - // pkg/ccm/instances.go uses fmt.Sprintf("%s://%s", ProviderName, server.GetId()). - if providerID != "stackit://"+serverID { - t.Fatalf("NewProviderID() = %q, want cloud-provider-stackit format %q", providerID, "stackit://"+serverID) - } -} diff --git a/scope/machine_scope.go b/scope/machine_scope.go index 195d0b8..652a6e9 100644 --- a/scope/machine_scope.go +++ b/scope/machine_scope.go @@ -12,6 +12,7 @@ package scope import ( "context" + "fmt" "time" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -139,7 +140,7 @@ func (s *MachineScope) setInstance(srv *server.Server, created bool) { s.instanceCreated = created s.instancePowerStatus = srv.PowerStatus s.StackitMachine.Status.Addresses = machineAddressesFromServer(srv.Addresses) - providerID := cloud.NewProviderID(srv.ID) + providerID := fmt.Sprintf("stackit://%s", srv.ID) s.StackitMachine.Status.InstanceID = srv.ID s.StackitMachine.Status.InstanceState = srv.State s.StackitMachine.Status.AvailabilityZone = srv.AvailabilityZone diff --git a/scope/scope_test.go b/scope/scope_test.go deleted file mode 100644 index 649ad7f..0000000 --- a/scope/scope_test.go +++ /dev/null @@ -1,117 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -package scope - -import ( - "testing" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - infrav1 "github.com/stackitcloud/cluster-api-provider-stackit/api/v1alpha1" -) - -func TestMachineScopeNetworkID(t *testing.T) { - tests := []struct { - name string - stackitMachine *infrav1.StackitMachine - stackitCluster *infrav1.StackitCluster - want string - }{ - { - name: "machine-level network override takes precedence", - stackitMachine: &infrav1.StackitMachine{ - Spec: infrav1.StackitMachineSpec{ - Network: infrav1.StackitMachineNetworkSpec{ID: "machine-network-id"}, - }, - }, - stackitCluster: &infrav1.StackitCluster{ - Spec: infrav1.StackitClusterSpec{ - Network: infrav1.StackitClusterNetworkSpec{ID: "cluster-spec-id"}, - }, - Status: infrav1.StackitClusterStatus{ - Network: infrav1.StackitNetworkStatus{ID: "cluster-status-id", Managed: true}, - }, - }, - want: "machine-network-id", - }, - { - name: "falls back to BYO cluster spec network ID when machine has none", - stackitMachine: &infrav1.StackitMachine{ - Spec: infrav1.StackitMachineSpec{}, - }, - stackitCluster: &infrav1.StackitCluster{ - Spec: infrav1.StackitClusterSpec{ - Network: infrav1.StackitClusterNetworkSpec{ID: "cluster-byo-id"}, - }, - }, - want: "cluster-byo-id", - }, - { - name: "falls back to managed cluster status network ID when spec has none", - stackitMachine: &infrav1.StackitMachine{ - Spec: infrav1.StackitMachineSpec{}, - }, - stackitCluster: &infrav1.StackitCluster{ - Spec: infrav1.StackitClusterSpec{}, - Status: infrav1.StackitClusterStatus{ - Network: infrav1.StackitNetworkStatus{ID: "cluster-managed-status-id", Managed: true}, - }, - }, - want: "cluster-managed-status-id", - }, - { - name: "returns empty when no network is configured anywhere", - stackitMachine: &infrav1.StackitMachine{ - Spec: infrav1.StackitMachineSpec{}, - }, - stackitCluster: &infrav1.StackitCluster{ - Spec: infrav1.StackitClusterSpec{}, - }, - want: "", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - scope := &MachineScope{ - StackitMachine: tt.stackitMachine, - StackitCluster: tt.stackitCluster, - } - if got := scope.NetworkID(); got != tt.want { - t.Errorf("NetworkID() = %q, want %q", got, tt.want) - } - }) - } -} - -func TestClusterScopeNetworkID(t *testing.T) { - clusterBYO := &infrav1.StackitCluster{ - ObjectMeta: metav1.ObjectMeta{Name: "byo-cluster"}, - Spec: infrav1.StackitClusterSpec{ - Network: infrav1.StackitClusterNetworkSpec{ID: "byo-id"}, - }, - } - scopeBYO := &ClusterScope{StackitCluster: clusterBYO} - if got := scopeBYO.NetworkID(); got != "byo-id" { - t.Errorf("ClusterScope.NetworkID() = %q, want byo-id", got) - } - - clusterManaged := &infrav1.StackitCluster{ - ObjectMeta: metav1.ObjectMeta{Name: "managed-cluster"}, - Status: infrav1.StackitClusterStatus{ - Network: infrav1.StackitNetworkStatus{ID: "managed-status-id", Managed: true}, - }, - } - scopeManaged := &ClusterScope{StackitCluster: clusterManaged} - if got := scopeManaged.NetworkID(); got != "managed-status-id" { - t.Errorf("ClusterScope.NetworkID() = %q, want managed-status-id", got) - } -}