Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions cloud/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
"context"
"errors"
"fmt"
"time"

ctrl "sigs.k8s.io/controller-runtime"

"github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/loadbalancer"
"github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/network"
Expand All @@ -25,6 +28,7 @@ import (

type cleanupScope struct {
Client
shared.NoOpReconcileScope
tags map[string]string
}

Expand All @@ -43,14 +47,14 @@ func CleanupByTags(ctx context.Context, client Client, tags map[string]string) e
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,
for _, reconciler := range []Reconciler{
publicip.New(scope),
loadbalancer.New(scope),
server.New(scope),
securitygroup.New(scope),
network.New(scope),
} {
if err := deleteFn(ctx); err != nil {
if _, err := reconciler.Delete(ctx); err != nil {
errs = append(errs, err)
}
}
Expand All @@ -68,18 +72,46 @@ func (s *cleanupScope) PublicIPSpec() publicip.PublicIPSpec {
}
}

func (s *cleanupScope) SetPublicIP(_ *publicip.PublicIP) {}

func (s *cleanupScope) APIServerLoadBalancerEnabled() bool {
return true
}

func (s *cleanupScope) ControlPlaneEndpointHost() string {
return ""
}

func (s *cleanupScope) UseExternalControlPlaneEndpoint() {}

func (s *cleanupScope) APIServerEndpointHost() string {
return ""
}

func (s *cleanupScope) ResolveLoadBalancerSpec(_ context.Context) (loadbalancer.LoadBalancerSpec, error) {
return s.LoadBalancerSpec(), nil
}

func (s *cleanupScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec {
return loadbalancer.LoadBalancerSpec{
Tags: s.tags,
}
}

func (s *cleanupScope) SetLoadBalancer(_ *loadbalancer.LoadBalancer) {}

func (s *cleanupScope) TargetPoolFailureResult(err error, _ time.Duration) (ctrl.Result, error) {
return ctrl.Result{}, err
}

func (s *cleanupScope) ServerSpec() server.ServerSpec {
return server.ServerSpec{
Tags: s.tags,
}
}

func (s *cleanupScope) SetServer(_ *server.Server, _ bool) {}

func (s *cleanupScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec {
return []securitygroup.SecurityGroupSpec{
{
Expand All @@ -89,9 +121,13 @@ func (s *cleanupScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec {
}
}

func (s *cleanupScope) SetSecurityGroup(_ string, _ *securitygroup.SecurityGroup) {}

func (s *cleanupScope) NetworkSpec() network.NetworkSpec {
return network.NetworkSpec{
Tags: s.tags,
Managed: true,
}
}

func (s *cleanupScope) SetNetwork(_ *network.Network) {}
77 changes: 62 additions & 15 deletions cloud/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
"fmt"
"maps"
"testing"
"time"

ctrl "sigs.k8s.io/controller-runtime"

"github.com/stackitcloud/cluster-api-provider-stackit/cloud"
cloudfake "github.com/stackitcloud/cluster-api-provider-stackit/cloud/fake"
Expand All @@ -23,13 +26,17 @@ 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"
)

type testResourceScope struct {
cloud.Client
shared.NoOpReconcileScope
networkSpec network.NetworkSpec
setNetwork func(*network.Network)
serverSpec server.ServerSpec
setServer func(*server.Server, bool)
publicIPSpec publicip.PublicIPSpec
loadBalancerSpec loadbalancer.LoadBalancerSpec
securityGroupSpec []securitygroup.SecurityGroupSpec
Expand All @@ -39,54 +46,94 @@ func (s *testResourceScope) NetworkSpec() network.NetworkSpec {
return s.networkSpec
}

func (s *testResourceScope) SetNetwork(net *network.Network) {
if s.setNetwork != nil {
s.setNetwork(net)
}
}

func (s *testResourceScope) ServerSpec() server.ServerSpec {
return s.serverSpec
}

func (s *testResourceScope) SetServer(srv *server.Server, created bool) {
if s.setServer != nil {
s.setServer(srv, created)
}
}

func (s *testResourceScope) PublicIPSpec() publicip.PublicIPSpec {
return s.publicIPSpec
}

func (s *testResourceScope) SetPublicIP(_ *publicip.PublicIP) {}

func (s *testResourceScope) APIServerLoadBalancerEnabled() bool {
return true
}

func (s *testResourceScope) ControlPlaneEndpointHost() string {
return ""
}

func (s *testResourceScope) UseExternalControlPlaneEndpoint() {}

func (s *testResourceScope) APIServerEndpointHost() string {
return ""
}

func (s *testResourceScope) ResolveLoadBalancerSpec(_ context.Context) (loadbalancer.LoadBalancerSpec, error) {
return s.loadBalancerSpec, nil
}

func (s *testResourceScope) LoadBalancerSpec() loadbalancer.LoadBalancerSpec {
return s.loadBalancerSpec
}

func (s *testResourceScope) SetLoadBalancer(_ *loadbalancer.LoadBalancer) {}

func (s *testResourceScope) TargetPoolFailureResult(err error, _ time.Duration) (ctrl.Result, error) {
return ctrl.Result{}, err
}

func (s *testResourceScope) SecurityGroupSpecs() []securitygroup.SecurityGroupSpec {
return s.securityGroupSpec
}

func (s *testResourceScope) SetSecurityGroup(_ string, _ *securitygroup.SecurityGroup) {}

func createTestResources(t *testing.T, client cloud.Client, prefix string, tags map[string]string) {
t.Helper()
ctx := context.Background()

var networkID string
if err := network.New(&testResourceScope{
if _, err := network.New(&testResourceScope{
Client: client,
networkSpec: network.NetworkSpec{
Name: prefix + "-net",
IPv4Prefix: network.DefaultNetworkPrefix,
Tags: tags,
Managed: true,
SetNetwork: func(net *network.Network, _ bool) {
networkID = net.ID
},
},
setNetwork: func(net *network.Network) {
networkID = net.ID
},
}).Reconcile(ctx); err != nil {
t.Fatalf("network.Reconcile() error = %v", err)
}

var workerServerID, bastionServerID string
if err := server.New(&testResourceScope{
if _, err := server.New(&testResourceScope{
Client: client,
serverSpec: server.ServerSpec{
Name: prefix + "-worker",
ImageID: "22222222-2222-4222-8222-222222222222",
MachineType: "c2i.2",
NetworkID: networkID,
Tags: tags,
SetServer: func(srv *server.Server, _ bool) {
workerServerID = srv.ID
},
},
setServer: func(srv *server.Server, _ bool) {
workerServerID = srv.ID
},
}).Reconcile(ctx); err != nil {
t.Fatalf("server.Reconcile(worker) error = %v", err)
Expand All @@ -95,23 +142,23 @@ func createTestResources(t *testing.T, client cloud.Client, prefix string, tags
bastionTags := make(map[string]string, len(tags)+1)
maps.Copy(bastionTags, tags)
bastionTags[util.LabelResourceRole] = util.ResourceRoleBastion
if err := server.New(&testResourceScope{
if _, err := server.New(&testResourceScope{
Client: client,
serverSpec: server.ServerSpec{
Name: prefix + "-bastion",
ImageID: "22222222-2222-4222-8222-222222222222",
MachineType: "c2i.2",
NetworkID: networkID,
Tags: bastionTags,
SetServer: func(srv *server.Server, _ bool) {
bastionServerID = srv.ID
},
},
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{
if _, err := publicip.New(&testResourceScope{
Client: client,
publicIPSpec: publicip.PublicIPSpec{
Tags: bastionTags,
Expand All @@ -124,7 +171,7 @@ func createTestResources(t *testing.T, client cloud.Client, prefix string, tags
nodeSSHTags := make(map[string]string, len(tags)+1)
maps.Copy(nodeSSHTags, tags)
nodeSSHTags[util.LabelResourceRole] = util.ResourceRoleNodeSSH
if err := securitygroup.New(&testResourceScope{
if _, err := securitygroup.New(&testResourceScope{
Client: client,
securityGroupSpec: []securitygroup.SecurityGroupSpec{
{
Expand All @@ -151,7 +198,7 @@ func createTestResources(t *testing.T, client cloud.Client, prefix string, tags
t.Fatalf("securitygroup.Reconcile() error = %v", err)
}

if err := loadbalancer.New(&testResourceScope{
if _, err := loadbalancer.New(&testResourceScope{
Client: client,
loadBalancerSpec: loadbalancer.LoadBalancerSpec{
Name: prefix + "-apiserver",
Expand Down
9 changes: 8 additions & 1 deletion cloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ import (

iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api"
lb "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api"
ctrl "sigs.k8s.io/controller-runtime"
)

// Reconciler is a common interface for reconciling and deleting STACKIT cloud resources.
type Reconciler interface {
Reconcile(ctx context.Context) (ctrl.Result, error)
Delete(ctx context.Context) (ctrl.Result, error)
}

// Client provides the target STACKIT project, region, and SDK API clients.
type Client interface {
ProjectID() string
Expand All @@ -33,7 +40,7 @@ type Client interface {
//
// Controllers receive a Factory rather than a Client so that the real
// implementation can be swapped for the fake in tests.
type Factory func(ctx context.Context, creds Credentials) (Client, error)
type Factory func(creds Credentials) (Client, error)

// Credentials carries the materials needed to construct a STACKIT API client.
// For MVP we only need the service-account JSON and project ID.
Expand Down
3 changes: 1 addition & 2 deletions cloud/sdk_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ You may obtain a copy of the License at
package cloud

import (
"context"
"fmt"
"os"

Expand Down Expand Up @@ -42,7 +41,7 @@ type SDKClient struct {
// The credential secret format mirrors machine-controller-manager-provider-stackit:
// "project-id" plus "serviceaccount.json". Authentication uses the STACKIT
// service-account key flow.
func NewClient(_ context.Context, creds Credentials) (Client, error) {
func NewClient(creds Credentials) (Client, error) {
if creds.ProjectID == "" {
return nil, fmt.Errorf("%w: project ID is empty", shared.ErrInvalidInput)
}
Expand Down
9 changes: 4 additions & 5 deletions cloud/sdk_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ You may obtain a copy of the License at
package cloud

import (
"context"
"errors"
"testing"

Expand All @@ -26,7 +25,7 @@ const (

func TestNewClientValidation(t *testing.T) {
t.Run("rejects empty project ID", func(t *testing.T) {
_, err := NewClient(context.Background(), Credentials{
_, err := NewClient(Credentials{
Region: testSDKRegion,
ServiceAccountJSON: []byte(`{}`),
})
Expand All @@ -36,7 +35,7 @@ func TestNewClientValidation(t *testing.T) {
})

t.Run("rejects empty region", func(t *testing.T) {
_, err := NewClient(context.Background(), Credentials{
_, err := NewClient(Credentials{
ProjectID: testSDKProjectID,
ServiceAccountJSON: []byte(`{}`),
})
Expand All @@ -47,7 +46,7 @@ func TestNewClientValidation(t *testing.T) {

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{
_, err := NewClient(Credentials{
ProjectID: testSDKProjectID,
Region: testSDKRegion,
})
Expand All @@ -58,7 +57,7 @@ func TestNewClientValidation(t *testing.T) {

t.Run("creates SDKClient when STACKIT_NO_AUTH is true", func(t *testing.T) {
t.Setenv(stackitNoAuthEnv, testBoolTrue)
client, err := NewClient(context.Background(), Credentials{
client, err := NewClient(Credentials{
ProjectID: testSDKProjectID,
Region: testSDKRegion,
})
Expand Down
Loading