Skip to content

Commit 2d595d2

Browse files
authored
refactor: public ip service (#35)
1 parent f1e6d9d commit 2d595d2

28 files changed

Lines changed: 1546 additions & 440 deletions

‎cloud/cleanup.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"fmt"
1616
"maps"
1717
"strings"
18+
19+
"github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/publicip"
1820
)
1921

2022
// 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
2931
bastionTags := make(map[string]string, len(tags)+1)
3032
maps.Copy(bastionTags, tags)
3133
bastionTags["cluster-api-provider-stackit/resource-role"] = "bastion"
34+
bastionServers, err := client.ListServersByTags(ctx, bastionTags)
35+
if err != nil {
36+
errs = append(errs, err.Error())
37+
}
38+
publicIPs, err := publicip.ListByTags(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), tags)
39+
if err != nil {
40+
errs = append(errs, err.Error())
41+
} else {
42+
for _, pubIP := range publicIPs {
43+
for _, server := range bastionServers {
44+
if err := publicip.DetachFromServer(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), server.ID, pubIP.ID); err != nil {
45+
errs = append(errs, fmt.Sprintf("detach public IP %q from server %q: %v", pubIP.ID, server.ID, err))
46+
}
47+
}
48+
if err := publicip.DeleteByID(ctx, client.IaaSClient(), client.ProjectID(), client.Region(), pubIP.ID); err != nil {
49+
errs = append(errs, fmt.Sprintf("delete public IP %q: %v", pubIP.ID, err))
50+
}
51+
}
52+
}
3253
if err := client.DeleteBastion(ctx, BastionInput{Tags: bastionTags}, Bastion{}); err != nil && !IsNotFound(err) {
3354
errs = append(errs, fmt.Sprintf("delete bastion resources: %v", err))
3455
}

‎cloud/cleanup_test.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) {
3636
if _, err := client.CreateServer(ctx, cloud.CreateServerInput{Name: "other", Tags: otherTags}); err != nil {
3737
t.Fatalf("CreateServer() error = %v", err)
3838
}
39+
if _, err := client.CreatePublicIP(ctx, tags); err != nil {
40+
t.Fatalf("CreatePublicIP() error = %v", err)
41+
}
42+
if _, err := client.CreatePublicIP(ctx, otherTags); err != nil {
43+
t.Fatalf("CreatePublicIP() error = %v", err)
44+
}
3945
if _, err := client.EnsureAPIServerLoadBalancer(ctx, cloud.LoadBalancerInput{
4046
Name: "matched",
4147
Tags: tags,
@@ -86,4 +92,18 @@ func TestCleanupByTagsDeletesMatchingCloudResources(t *testing.T) {
8692
if len(matchedLoadBalancers) != 0 {
8793
t.Fatalf("matched load balancer count = %d, want 0", len(matchedLoadBalancers))
8894
}
95+
matchedPublicIPs, err := client.ListPublicIPsByTags(ctx, tags)
96+
if err != nil {
97+
t.Fatalf("ListPublicIPsByTags() error = %v", err)
98+
}
99+
if len(matchedPublicIPs) != 0 {
100+
t.Fatalf("matched public IP count = %d, want 0", len(matchedPublicIPs))
101+
}
102+
otherPublicIPs, err := client.ListPublicIPsByTags(ctx, otherTags)
103+
if err != nil {
104+
t.Fatalf("ListPublicIPsByTags(other) error = %v", err)
105+
}
106+
if len(otherPublicIPs) != 1 {
107+
t.Fatalf("other public IP count = %d, want 1", len(otherPublicIPs))
108+
}
89109
}

‎cloud/client.go‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@ You may obtain a copy of the License at
1010

1111
package cloud
1212

13-
import "context"
13+
import (
14+
"context"
15+
16+
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api"
17+
)
18+
19+
// Reconciler is a generic interface used by components offering a type of service.
20+
type Reconciler interface {
21+
Reconcile(ctx context.Context) error
22+
Delete(ctx context.Context) error
23+
}
1424

1525
// Client is the abstraction over STACKIT cloud APIs used by the controllers.
1626
//
@@ -23,6 +33,12 @@ import "context"
2333
// - be idempotent (CreateServer/EnsureAPIServerLoadBalancer must not produce
2434
// duplicates when a resource with matching tags already exists)
2535
type Client interface {
36+
ProjectID() string
37+
38+
Region() string
39+
40+
IaaSClient() *iaas.APIClient
41+
2642
GetServer(ctx context.Context, id string) (*Server, error)
2743

2844
FindServerByTags(ctx context.Context, tags map[string]string) (*Server, error)
@@ -43,8 +59,6 @@ type Client interface {
4359

4460
DeleteNodeSSHAccess(ctx context.Context, tags map[string]string) error
4561

46-
ListPublicIPsByTags(ctx context.Context, tags map[string]string) ([]*PublicIP, error)
47-
4862
ListSecurityGroupsByTags(ctx context.Context, tags map[string]string) ([]*SecurityGroup, error)
4963

5064
EnsureAPIServerLoadBalancer(ctx context.Context, input LoadBalancerInput) (*LoadBalancer, error)

‎cloud/errors.go‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,32 @@ You may obtain a copy of the License at
1010

1111
package cloud
1212

13-
import "errors"
13+
import "github.com/stackitcloud/cluster-api-provider-stackit/cloud/services/shared"
1414

15-
// Sentinel errors returned by Client implementations. Controllers use
16-
// errors.Is to classify them into requeue / condition-update behavior.
15+
// Sentinel errors returned by Client implementations, re-exported from shared.
16+
// Controllers use errors.Is to classify them into requeue / condition-update behavior.
1717
var (
18-
ErrNotFound = errors.New("not found")
19-
ErrUnauthorized = errors.New("unauthorized")
20-
ErrInvalidInput = errors.New("invalid input")
21-
ErrConflict = errors.New("conflict")
22-
ErrTransient = errors.New("transient")
18+
ErrNotFound = shared.ErrNotFound
19+
ErrUnauthorized = shared.ErrUnauthorized
20+
ErrInvalidInput = shared.ErrInvalidInput
21+
ErrConflict = shared.ErrConflict
22+
ErrTransient = shared.ErrTransient
2323
)
2424

2525
// IsNotFound reports whether err wraps ErrNotFound.
26-
func IsNotFound(err error) bool { return errors.Is(err, ErrNotFound) }
26+
func IsNotFound(err error) bool { return shared.IsNotFound(err) }
2727

2828
// IsUnauthorized reports whether err wraps ErrUnauthorized.
29-
func IsUnauthorized(err error) bool { return errors.Is(err, ErrUnauthorized) }
29+
func IsUnauthorized(err error) bool { return shared.IsUnauthorized(err) }
3030

3131
// IsInvalidInput reports whether err wraps ErrInvalidInput.
32-
func IsInvalidInput(err error) bool { return errors.Is(err, ErrInvalidInput) }
32+
func IsInvalidInput(err error) bool { return shared.IsInvalidInput(err) }
3333

3434
// IsConflict reports whether err wraps ErrConflict.
35-
func IsConflict(err error) bool { return errors.Is(err, ErrConflict) }
35+
func IsConflict(err error) bool { return shared.IsConflict(err) }
3636

3737
// IsTransient reports whether err wraps ErrTransient.
38-
func IsTransient(err error) bool { return errors.Is(err, ErrTransient) }
38+
func IsTransient(err error) bool { return shared.IsTransient(err) }
3939

40-
// IsRetryable reports whether err should trigger a requeue (conflict or
41-
// transient).
42-
func IsRetryable(err error) bool {
43-
return IsConflict(err) || IsTransient(err)
44-
}
40+
// IsRetryable reports whether err should trigger a requeue (conflict or transient).
41+
func IsRetryable(err error) bool { return shared.IsRetryable(err) }

0 commit comments

Comments
 (0)