Skip to content

Commit 45c2589

Browse files
committed
Manage resources by tags only
1 parent 8cb403d commit 45c2589

File tree

16 files changed

+131
-97
lines changed

16 files changed

+131
-97
lines changed

internal/scope/cluster.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,7 @@ func (c *Cluster) Close(ctx context.Context) error {
8383
// ResourceNameName returns the name/prefix that resources created for the cluster should have.
8484
// It is possible to provide additional suffixes that will be appended to the name with a leading "-".
8585
func (c *Cluster) ResourceName(suffixes ...string) string {
86-
name := strings.Builder{}
87-
name.WriteString("caps")
88-
89-
for _, suffix := range append([]string{c.ScalewayCluster.Name}, suffixes...) {
90-
name.WriteString("-")
91-
name.WriteString(suffix)
92-
}
93-
94-
return truncateString(name.String())
86+
return truncateString(strings.Join(append([]string{c.ScalewayCluster.Name}, suffixes...), "-"), 128)
9587
}
9688

9789
// ResourceTags returns the tags that resources created for the cluster should have.

internal/scope/cluster_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestCluster_ResourceName(t *testing.T) {
3434
},
3535
},
3636
args: args{},
37-
want: "caps-cluster-name",
37+
want: "cluster-name",
3838
},
3939
{
4040
name: "suffix provided",
@@ -46,7 +46,7 @@ func TestCluster_ResourceName(t *testing.T) {
4646
},
4747
},
4848
args: args{suffixes: []string{"0"}},
49-
want: "caps-cluster-name-0",
49+
want: "cluster-name-0",
5050
},
5151
}
5252
for _, tt := range tests {

internal/scope/machine.go

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"slices"
8-
"strings"
97

108
infrav1 "github.com/scaleway/cluster-api-provider-scaleway/api/v1alpha1"
119
"github.com/scaleway/cluster-api-provider-scaleway/internal/service/scaleway"
@@ -73,28 +71,14 @@ func (m *Machine) Close(ctx context.Context) error {
7371
return m.PatchObject(ctx)
7472
}
7573

76-
// ResourceNameName returns the name/prefix that resources created for the machine should have.
77-
// It is possible to provide additional suffixes that will be appended to the name with a leading "-".
78-
func (m *Machine) ResourceName(suffixes ...string) string {
79-
name := strings.Builder{}
80-
name.WriteString("caps")
81-
82-
for _, suffix := range append([]string{m.ScalewayMachine.Name}, suffixes...) {
83-
name.WriteString("-")
84-
name.WriteString(suffix)
85-
}
86-
87-
return truncateString(name.String())
74+
// ResourceNameName returns the name that resources created for the machine should have.
75+
func (m *Machine) ResourceName() string {
76+
return truncateString(m.ScalewayMachine.Name, 63)
8877
}
8978

9079
// ResourceTags returns the tags that resources created for the machine should have.
91-
// It is possible to provide additional tags that will be added to the default tags.
92-
func (m *Machine) ResourceTags(additional ...string) []string {
93-
return slices.Concat(
94-
m.Cluster.ResourceTags(),
95-
[]string{fmt.Sprintf("caps-scalewaymachine=%s", m.ScalewayMachine.Name)},
96-
additional,
97-
)
80+
func (m *Machine) ResourceTags() []string {
81+
return append(m.Cluster.ResourceTags(), fmt.Sprintf("caps-scalewaymachine=%s", m.ScalewayMachine.Name))
9882
}
9983

10084
// Zone returns the zone of the machine.

internal/scope/scope.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package scope
22

3-
// maxResourceNameLen is the maximum length a Scaleway resource name should have.
4-
const maxResourceNameLen = 128
5-
6-
// truncateString shortens a string to a maximum of 128 characters.
3+
// truncateString shortens a string to a maximum of maxLen characters.
74
// If the string exceeds this length, it replaces the middle portion
85
// with a single dash, preserving characters from the start and end.
9-
func truncateString(s string) string {
10-
if len(s) <= maxResourceNameLen {
6+
func truncateString(s string, maxLen int) string {
7+
if len(s) <= maxLen {
118
return s
129
}
13-
n := (maxResourceNameLen - 1) / 2
14-
return s[:n] + "-" + s[len(s)-(maxResourceNameLen-1-n):]
10+
n := (maxLen - 1) / 2
11+
return s[:n] + "-" + s[len(s)-(maxLen-1-n):]
1512
}

internal/scope/scope_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,42 @@ import "testing"
44

55
func Test_truncateString(t *testing.T) {
66
type args struct {
7-
s string
7+
s string
8+
maxLen int
89
}
910
tests := []struct {
1011
name string
1112
args args
1213
want string
1314
}{
1415
{
15-
name: "short string",
16-
args: args{s: "short"},
16+
name: "string below max length",
17+
args: args{
18+
s: "short",
19+
maxLen: 128,
20+
},
1721
want: "short",
1822
},
1923
{
2024
name: "exact length",
21-
args: args{s: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
25+
args: args{
26+
s: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
27+
maxLen: 128,
28+
},
2229
want: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
2330
},
2431
{
2532
name: "long string",
26-
args: args{s: "this is a very long string that exceeds the maximum length of 128 characters, so it should be truncated in the middle to fit within the limit imposed by Scaleway resource naming conventions."},
33+
args: args{
34+
s: "this is a very long string that exceeds the maximum length of 128 characters, so it should be truncated in the middle to fit within the limit imposed by Scaleway resource naming conventions.",
35+
maxLen: 128,
36+
},
2737
want: "this is a very long string that exceeds the maximum length of 1-ithin the limit imposed by Scaleway resource naming conventions.",
2838
},
2939
}
3040
for _, tt := range tests {
3141
t.Run(tt.name, func(t *testing.T) {
32-
if got := truncateString(tt.args.s); got != tt.want {
42+
if got := truncateString(tt.args.s, tt.args.maxLen); got != tt.want {
3343
t.Errorf("truncateString() = %v, want %v", got, tt.want)
3444
}
3545
})

internal/service/scaleway/client/block.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ func (c *Client) FindVolume(ctx context.Context, zone scw.Zone, tags []string) (
4646
return nil, err
4747
}
4848

49+
if err := validateTags(tags); err != nil {
50+
return nil, err
51+
}
52+
4953
resp, err := c.block.ListVolumes(&block.ListVolumesRequest{
5054
Zone: zone,
5155
Tags: tags,

internal/service/scaleway/client/client.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import (
1515
"github.com/scaleway/scaleway-sdk-go/scw"
1616
)
1717

18+
const (
19+
createdByTag = "created-by=cluster-api-provider-scaleway"
20+
createdByDescription = "Created by cluster-api-provider-scaleway"
21+
)
22+
1823
// Client is a wrapper over scaleway-sdk-go to access Scaleway Product APIs in
1924
// a specific region and project.
2025
type Client struct {
@@ -85,3 +90,11 @@ func matchTags(tags []string, wantedTags []string) bool {
8590

8691
return true
8792
}
93+
94+
func validateTags(tags []string) error {
95+
if len(tags) == 0 {
96+
return fmt.Errorf("tags cannot be empty")
97+
}
98+
99+
return nil
100+
}

internal/service/scaleway/client/domain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (c *Client) SetDNSZoneRecords(ctx context.Context, zone, name string, ips [
5151
Priority: 0,
5252
TTL: 60,
5353
Type: domain.RecordTypeA,
54-
Comment: scw.StringPtr("Created by cluster-api-provider-scaleway"),
54+
Comment: scw.StringPtr(createdByDescription),
5555
})
5656
}
5757

internal/service/scaleway/client/instance.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,29 @@ import (
1111
"github.com/scaleway/scaleway-sdk-go/scw"
1212
)
1313

14-
// FindServer finds an existing Instance server by name.
14+
// FindServer finds an existing Instance server by tags.
1515
// It returns ErrNoItemFound if no matching server is found.
16-
func (c *Client) FindServer(ctx context.Context, zone scw.Zone, name string) (*instance.Server, error) {
16+
func (c *Client) FindServer(ctx context.Context, zone scw.Zone, tags []string) (*instance.Server, error) {
1717
if err := c.validateZone(c.instance, zone); err != nil {
1818
return nil, err
1919
}
2020

21+
if err := validateTags(tags); err != nil {
22+
return nil, err
23+
}
24+
2125
resp, err := c.instance.ListServers(&instance.ListServersRequest{
22-
Name: scw.StringPtr(name),
26+
Tags: tags,
2327
Project: &c.projectID,
2428
Zone: zone,
2529
}, scw.WithContext(ctx), scw.WithAllPages())
2630
if err != nil {
2731
return nil, newCallError("ListServers", err)
2832
}
2933

30-
// Filter out all servers that have the wrong name.
34+
// Filter out all servers that have the wrong tags.
3135
servers := slices.DeleteFunc(resp.Servers, func(server *instance.Server) bool {
32-
return server.Name != name
36+
return !matchTags(server.Tags, tags)
3337
})
3438

3539
switch len(servers) {
@@ -38,7 +42,7 @@ func (c *Client) FindServer(ctx context.Context, zone scw.Zone, name string) (*i
3842
case 1:
3943
return servers[0], nil
4044
default:
41-
return nil, fmt.Errorf("%w: found %d servers with name %s", ErrTooManyItemsFound, len(servers), name)
45+
return nil, fmt.Errorf("%w: found %d servers with tags %s", ErrTooManyItemsFound, len(servers), tags)
4246
}
4347
}
4448

@@ -75,7 +79,7 @@ func (c *Client) CreateServer(
7579
Boot: scw.BoolPtr(true),
7680
},
7781
},
78-
Tags: tags,
82+
Tags: append(tags, createdByTag),
7983
}
8084

8185
if len(publicIPs) > 0 {
@@ -136,6 +140,10 @@ func (c *Client) FindIPs(ctx context.Context, zone scw.Zone, tags []string) ([]*
136140
return nil, err
137141
}
138142

143+
if err := validateTags(tags); err != nil {
144+
return nil, err
145+
}
146+
139147
resp, err := c.instance.ListIPs(&instance.ListIPsRequest{
140148
Zone: zone,
141149
Project: &c.projectID,
@@ -160,7 +168,7 @@ func (c *Client) CreateIP(ctx context.Context, zone scw.Zone, ipType instance.IP
160168

161169
ip, err := c.instance.CreateIP(&instance.CreateIPRequest{
162170
Zone: zone,
163-
Tags: tags,
171+
Tags: append(tags, createdByTag),
164172
Type: ipType,
165173
}, scw.WithContext(ctx))
166174
if err != nil {
@@ -194,6 +202,7 @@ func (c *Client) CreatePrivateNIC(ctx context.Context, zone scw.Zone, serverID,
194202
Zone: zone,
195203
ServerID: serverID,
196204
PrivateNetworkID: privateNetworkID,
205+
Tags: []string{createdByTag},
197206
}, scw.WithContext(ctx))
198207
if err != nil {
199208
return nil, newCallError("CreatePrivateNIC", err)
@@ -303,6 +312,10 @@ func (c *Client) FindInstanceVolume(ctx context.Context, zone scw.Zone, tags []s
303312
return nil, err
304313
}
305314

315+
if err := validateTags(tags); err != nil {
316+
return nil, err
317+
}
318+
306319
resp, err := c.instance.ListVolumes(&instance.ListVolumesRequest{
307320
Zone: zone,
308321
Tags: tags,

internal/service/scaleway/client/lb.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,27 @@ import (
1010
"github.com/scaleway/scaleway-sdk-go/scw"
1111
)
1212

13-
func (c *Client) FindLB(ctx context.Context, zone scw.Zone, name string) (*lb.LB, error) {
13+
func (c *Client) FindLB(ctx context.Context, zone scw.Zone, tags []string) (*lb.LB, error) {
1414
if err := c.validateZone(c.lb, zone); err != nil {
1515
return nil, err
1616
}
1717

18+
if err := validateTags(tags); err != nil {
19+
return nil, err
20+
}
21+
1822
resp, err := c.lb.ListLBs(&lb.ZonedAPIListLBsRequest{
1923
Zone: zone,
20-
Name: &name,
24+
Tags: tags,
2125
ProjectID: &c.projectID,
2226
}, scw.WithContext(ctx), scw.WithAllPages())
2327
if err != nil {
2428
return nil, newCallError("ListLBs", err)
2529
}
2630

27-
// Filter out all LBs that have the wrong name.
31+
// Filter out all LBs that have the wrong tags.
2832
lbs := slices.DeleteFunc(resp.LBs, func(lb *lb.LB) bool {
29-
return lb.Name != name
33+
return !matchTags(lb.Tags, tags)
3034
})
3135

3236
switch len(lbs) {
@@ -35,7 +39,7 @@ func (c *Client) FindLB(ctx context.Context, zone scw.Zone, name string) (*lb.LB
3539
case 1:
3640
return lbs[0], nil
3741
default:
38-
return nil, fmt.Errorf("%w: found %d LBs with name %s", ErrTooManyItemsFound, len(lbs), name)
42+
return nil, fmt.Errorf("%w: found %d LBs with tags %s", ErrTooManyItemsFound, len(lbs), tags)
3943
}
4044
}
4145

@@ -95,7 +99,8 @@ func (c *Client) CreateLB(
9599
Name: name,
96100
Type: lbType,
97101
IPID: ipID,
98-
Tags: tags,
102+
Tags: append(tags, createdByTag),
103+
Description: createdByDescription,
99104
AssignFlexibleIP: scw.BoolPtr(ipID == nil),
100105
AssignFlexibleIPv6: scw.BoolPtr(false),
101106
}, scw.WithContext(ctx))
@@ -122,11 +127,14 @@ func (c *Client) DeleteLB(ctx context.Context, zone scw.Zone, id string, release
122127
return nil
123128
}
124129

125-
func (c *Client) FindLBs(ctx context.Context, prefix string, tags []string) ([]*lb.LB, error) {
130+
func (c *Client) FindLBs(ctx context.Context, tags []string) ([]*lb.LB, error) {
131+
if err := validateTags(tags); err != nil {
132+
return nil, err
133+
}
134+
126135
resp, err := c.lb.ListLBs(&lb.ZonedAPIListLBsRequest{
127136
Zone: scw.ZoneFrPar1, // Dummy value, refer to the scw.WithZones option.
128137
ProjectID: &c.projectID,
129-
Name: scw.StringPtr(prefix),
130138
Tags: tags,
131139
}, scw.WithContext(ctx), scw.WithAllPages(), scw.WithZones(c.productZones(c.lb)...))
132140
if err != nil {
@@ -135,7 +143,7 @@ func (c *Client) FindLBs(ctx context.Context, prefix string, tags []string) ([]*
135143

136144
// Filter out LBs that don't have the right prefix or tags.
137145
lbs := slices.DeleteFunc(resp.LBs, func(lb *lb.LB) bool {
138-
return !strings.HasPrefix(lb.Name, prefix) || !matchTags(lb.Tags, tags)
146+
return !matchTags(lb.Tags, tags)
139147
})
140148

141149
return lbs, nil

0 commit comments

Comments
 (0)