Skip to content

Commit 808c663

Browse files
authored
Include cluster ID tag on cloudscale servers (#13)
1 parent a201479 commit 808c663

File tree

2 files changed

+80
-24
lines changed

2 files changed

+80
-24
lines changed

pkg/machine/actuator.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ import (
2121
)
2222

2323
const (
24-
antiAffinityTag = "machine-api-provider-cloudscale_appuio_io_antiAffinityKey"
25-
machineNameTag = "machine-api-provider-cloudscale_appuio_io_name"
24+
antiAffinityTag = "machine-api-provider-cloudscale_appuio_io_antiAffinityKey"
25+
machineNameTag = "machine-api-provider-cloudscale_appuio_io_name"
26+
machineClusterIDTag = "machine-api-provider-cloudscale_appuio_io_cluster_id"
27+
28+
machineClusterIDLabelName = "machine.openshift.io/cluster-api-cluster"
2629
)
2730

2831
// Actuator is responsible for performing machine reconciliation.
@@ -82,6 +85,7 @@ func (a *Actuator) Create(ctx context.Context, machine *machinev1beta1.Machine)
8285
spec.Tags = make(map[string]string)
8386
}
8487
spec.Tags[machineNameTag] = machine.Name
88+
spec.Tags[machineClusterIDTag] = mctx.clusterId
8589

8690
// Null is not allowed for SSH keys in the cloudscale API
8791
if spec.SSHKeys == nil {
@@ -149,7 +153,7 @@ func (a *Actuator) Exists(ctx context.Context, machine *machinev1beta1.Machine)
149153
}
150154
sc := a.serverClientFactory(mctx.token)
151155

152-
s, err := a.getServer(ctx, sc, machine)
156+
s, err := a.getServer(ctx, sc, *mctx)
153157

154158
return s != nil, err
155159
}
@@ -161,7 +165,7 @@ func (a *Actuator) Update(ctx context.Context, machine *machinev1beta1.Machine)
161165
}
162166
sc := a.serverClientFactory(mctx.token)
163167

164-
s, err := a.getServer(ctx, sc, machine)
168+
s, err := a.getServer(ctx, sc, *mctx)
165169
if err != nil {
166170
return fmt.Errorf("failed to get server %q: %w", machine.Name, err)
167171
}
@@ -186,7 +190,7 @@ func (a *Actuator) Delete(ctx context.Context, machine *machinev1beta1.Machine)
186190
}
187191
sc := a.serverClientFactory(mctx.token)
188192

189-
s, err := a.getServer(ctx, sc, machine)
193+
s, err := a.getServer(ctx, sc, *mctx)
190194
if err != nil {
191195
return fmt.Errorf("failed to get server %q: %w", machine.Name, err)
192196
}
@@ -203,8 +207,11 @@ func (a *Actuator) Delete(ctx context.Context, machine *machinev1beta1.Machine)
203207
return nil
204208
}
205209

206-
func (a *Actuator) getServer(ctx context.Context, sc cloudscale.ServerService, machine *machinev1beta1.Machine) (*cloudscale.Server, error) {
207-
lookupKey := cloudscale.TagMap{machineNameTag: machine.Name}
210+
func (a *Actuator) getServer(ctx context.Context, sc cloudscale.ServerService, machineCtx machineContext) (*cloudscale.Server, error) {
211+
lookupKey := cloudscale.TagMap{
212+
machineNameTag: machineCtx.machine.Name,
213+
machineClusterIDTag: machineCtx.clusterId,
214+
}
208215

209216
ss, err := sc.List(ctx, cloudscale.WithTagFilter(lookupKey))
210217
if err != nil {
@@ -214,7 +221,7 @@ func (a *Actuator) getServer(ctx context.Context, sc cloudscale.ServerService, m
214221
return nil, nil
215222
}
216223
if len(ss) > 1 {
217-
return nil, fmt.Errorf("found multiple servers with name %q", machine.Name)
224+
return nil, fmt.Errorf("found multiple servers with name %q", machineCtx.machine.Name)
218225
}
219226

220227
return &ss[0], nil
@@ -392,16 +399,22 @@ func cloudscaleServerInterfacesFromProviderSpecInterfaces(interfaces []csv1beta1
392399
}
393400

394401
type machineContext struct {
395-
machine *machinev1beta1.Machine
396-
spec csv1beta1.CloudscaleMachineProviderSpec
397-
token string
402+
machine *machinev1beta1.Machine
403+
clusterId string
404+
spec csv1beta1.CloudscaleMachineProviderSpec
405+
token string
398406
}
399407

400408
func (a *Actuator) getMachineContext(ctx context.Context, machine *machinev1beta1.Machine) (*machineContext, error) {
401409
const tokenKey = "token"
402410

403411
origMachine := machine.DeepCopy()
404412

413+
clusterId, ok := machine.Labels[machineClusterIDLabelName]
414+
if !ok {
415+
return nil, fmt.Errorf("cluster ID label %q not found on machine %q", machineClusterIDLabelName, machine.Name)
416+
}
417+
405418
spec, err := csv1beta1.ProviderSpecFromRawExtension(machine.Spec.ProviderSpec.Value)
406419
if err != nil {
407420
return nil, fmt.Errorf("failed to get provider spec from machine %q: %w", machine.Name, err)
@@ -423,9 +436,10 @@ func (a *Actuator) getMachineContext(ctx context.Context, machine *machinev1beta
423436
}
424437

425438
return &machineContext{
426-
machine: origMachine,
427-
spec: *spec,
428-
token: token,
439+
machine: origMachine,
440+
clusterId: clusterId,
441+
spec: *spec,
442+
token: token,
429443
}, nil
430444
}
431445

pkg/machine/actuator_test.go

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ func Test_Actuator_Create_ComplexMachineE2E(t *testing.T) {
2929

3030
ctx := context.Background()
3131

32+
const clusterID = "cluster-id"
33+
3234
ctrl := gomock.NewController(t)
3335
defer ctrl.Finish()
3436

3537
machine := &machinev1beta1.Machine{
3638
ObjectMeta: metav1.ObjectMeta{
3739
Name: "app-test",
40+
Labels: map[string]string{
41+
machineClusterIDLabelName: clusterID,
42+
},
3843
},
3944
}
4045
providerSpec := csv1beta1.CloudscaleMachineProviderSpec{
@@ -115,7 +120,8 @@ func Test_Actuator_Create_ComplexMachineE2E(t *testing.T) {
115120

116121
TaggedResourceRequest: cloudscale.TaggedResourceRequest{
117122
Tags: ptr.To(cloudscale.TagMap{
118-
machineNameTag: machine.Name,
123+
machineNameTag: machine.Name,
124+
machineClusterIDTag: clusterID,
119125
}),
120126
},
121127
Zone: providerSpec.Zone,
@@ -190,6 +196,8 @@ func Test_Actuator_Create_ComplexMachineE2E(t *testing.T) {
190196
func Test_Actuator_Create_AntiAffinityPools(t *testing.T) {
191197
const zone = "rma1"
192198

199+
const clusterID = "cluster-id"
200+
193201
tcs := []struct {
194202
name string
195203
apiMock func(*testing.T, *machinev1beta1.Machine, csv1beta1.CloudscaleMachineProviderSpec, *csmock.MockServerService, *csmock.MockServerGroupService)
@@ -229,7 +237,8 @@ func Test_Actuator_Create_AntiAffinityPools(t *testing.T) {
229237
},
230238
TaggedResourceRequest: cloudscale.TaggedResourceRequest{
231239
Tags: ptr.To(cloudscale.TagMap{
232-
machineNameTag: machine.Name,
240+
machineNameTag: machine.Name,
241+
machineClusterIDTag: clusterID,
233242
}),
234243
},
235244
ServerGroups: []string{newSGUUID},
@@ -266,7 +275,8 @@ func Test_Actuator_Create_AntiAffinityPools(t *testing.T) {
266275
},
267276
TaggedResourceRequest: cloudscale.TaggedResourceRequest{
268277
Tags: ptr.To(cloudscale.TagMap{
269-
machineNameTag: machine.Name,
278+
machineNameTag: machine.Name,
279+
machineClusterIDTag: clusterID,
270280
}),
271281
},
272282
ServerGroups: []string{existingUUID},
@@ -320,7 +330,8 @@ func Test_Actuator_Create_AntiAffinityPools(t *testing.T) {
320330
},
321331
TaggedResourceRequest: cloudscale.TaggedResourceRequest{
322332
Tags: ptr.To(cloudscale.TagMap{
323-
machineNameTag: machine.Name,
333+
machineNameTag: machine.Name,
334+
machineClusterIDTag: clusterID,
324335
}),
325336
},
326337
ServerGroups: []string{newSGUUID},
@@ -374,7 +385,8 @@ func Test_Actuator_Create_AntiAffinityPools(t *testing.T) {
374385
},
375386
TaggedResourceRequest: cloudscale.TaggedResourceRequest{
376387
Tags: ptr.To(cloudscale.TagMap{
377-
machineNameTag: machine.Name,
388+
machineNameTag: machine.Name,
389+
machineClusterIDTag: clusterID,
378390
}),
379391
},
380392
ServerGroups: []string{newSGUUID},
@@ -398,6 +410,9 @@ func Test_Actuator_Create_AntiAffinityPools(t *testing.T) {
398410
machine := &machinev1beta1.Machine{
399411
ObjectMeta: metav1.ObjectMeta{
400412
Name: "app-test",
413+
Labels: map[string]string{
414+
machineClusterIDLabelName: "cluster-id",
415+
},
401416
},
402417
}
403418
providerSpec := csv1beta1.CloudscaleMachineProviderSpec{
@@ -455,6 +470,8 @@ func Test_Actuator_Exists(t *testing.T) {
455470
t.Run(tc.name, func(t *testing.T) {
456471
t.Parallel()
457472

473+
const clusterID = "cluster-id"
474+
458475
ctx := context.Background()
459476

460477
ctrl := gomock.NewController(t)
@@ -463,6 +480,9 @@ func Test_Actuator_Exists(t *testing.T) {
463480
machine := &machinev1beta1.Machine{
464481
ObjectMeta: metav1.ObjectMeta{
465482
Name: "app-test",
483+
Labels: map[string]string{
484+
machineClusterIDLabelName: clusterID,
485+
},
466486
},
467487
}
468488
providerSpec := csv1beta1.CloudscaleMachineProviderSpec{
@@ -483,7 +503,10 @@ func Test_Actuator_Exists(t *testing.T) {
483503
sgs := csmock.NewMockServerGroupService(ctrl)
484504
actuator := newActuator(c, ss, sgs)
485505

486-
ss.EXPECT().List(ctx, csTagMatcher{t: t, tags: map[string]string{machineNameTag: machine.Name}}).Return(tc.servers, nil)
506+
ss.EXPECT().List(ctx, csTagMatcher{t: t, tags: map[string]string{
507+
machineNameTag: machine.Name,
508+
machineClusterIDTag: clusterID,
509+
}}).Return(tc.servers, nil)
487510

488511
exists, err := actuator.Exists(ctx, machine)
489512
require.NoError(t, err)
@@ -497,12 +520,17 @@ func Test_Actuator_Update(t *testing.T) {
497520

498521
ctx := context.Background()
499522

523+
const clusterID = "cluster-id"
524+
500525
ctrl := gomock.NewController(t)
501526
defer ctrl.Finish()
502527

503528
machine := &machinev1beta1.Machine{
504529
ObjectMeta: metav1.ObjectMeta{
505530
Name: "app-test",
531+
Labels: map[string]string{
532+
machineClusterIDLabelName: clusterID,
533+
},
506534
},
507535
}
508536
providerSpec := csv1beta1.CloudscaleMachineProviderSpec{
@@ -524,8 +552,11 @@ func Test_Actuator_Update(t *testing.T) {
524552
actuator := newActuator(c, ss, sgs)
525553

526554
ss.EXPECT().List(ctx, csTagMatcher{
527-
t: t,
528-
tags: map[string]string{machineNameTag: machine.Name},
555+
t: t,
556+
tags: map[string]string{
557+
machineNameTag: machine.Name,
558+
machineClusterIDTag: clusterID,
559+
},
529560
}).Return([]cloudscale.Server{{
530561
UUID: "machine-uuid",
531562
}}, nil)
@@ -542,6 +573,8 @@ func Test_Actuator_Update(t *testing.T) {
542573
func Test_Actuator_Delete(t *testing.T) {
543574
t.Parallel()
544575

576+
const clusterID = "cluster-id"
577+
545578
tcs := []struct {
546579
name string
547580
apiMock func(*testing.T, *machinev1beta1.Machine, *csmock.MockServerService, *csmock.MockServerGroupService)
@@ -552,7 +585,10 @@ func Test_Actuator_Delete(t *testing.T) {
552585
apiMock: func(t *testing.T, machine *machinev1beta1.Machine, ss *csmock.MockServerService, sgs *csmock.MockServerGroupService) {
553586
ss.EXPECT().List(
554587
gomock.Any(),
555-
csTagMatcher{t: t, tags: map[string]string{machineNameTag: machine.Name}},
588+
csTagMatcher{t: t, tags: map[string]string{
589+
machineNameTag: machine.Name,
590+
machineClusterIDTag: clusterID,
591+
}},
556592
).Return([]cloudscale.Server{
557593
{
558594
UUID: "machine-uuid",
@@ -568,7 +604,10 @@ func Test_Actuator_Delete(t *testing.T) {
568604
apiMock: func(t *testing.T, machine *machinev1beta1.Machine, ss *csmock.MockServerService, sgs *csmock.MockServerGroupService) {
569605
ss.EXPECT().List(
570606
gomock.Any(),
571-
csTagMatcher{t: t, tags: map[string]string{machineNameTag: machine.Name}},
607+
csTagMatcher{t: t, tags: map[string]string{
608+
machineNameTag: machine.Name,
609+
machineClusterIDTag: clusterID,
610+
}},
572611
).Return([]cloudscale.Server{}, nil)
573612
},
574613
},
@@ -586,6 +625,9 @@ func Test_Actuator_Delete(t *testing.T) {
586625
machine := &machinev1beta1.Machine{
587626
ObjectMeta: metav1.ObjectMeta{
588627
Name: "app-test",
628+
Labels: map[string]string{
629+
machineClusterIDLabelName: clusterID,
630+
},
589631
},
590632
}
591633
providerSpec := csv1beta1.CloudscaleMachineProviderSpec{

0 commit comments

Comments
 (0)