Skip to content

Commit f6bcfa4

Browse files
committed
Fix filtering servers by cluster id; cloudscale only supports single tag filter
Follow up of #13
1 parent 3ec052c commit f6bcfa4

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

pkg/machine/actuator.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,21 @@ func (a *Actuator) Delete(ctx context.Context, machine *machinev1beta1.Machine)
209209

210210
func (a *Actuator) getServer(ctx context.Context, sc cloudscale.ServerService, machineCtx machineContext) (*cloudscale.Server, error) {
211211
lookupKey := cloudscale.TagMap{
212-
machineNameTag: machineCtx.machine.Name,
213-
machineClusterIDTag: machineCtx.clusterId,
212+
machineNameTag: machineCtx.machine.Name,
214213
}
215214

216-
ss, err := sc.List(ctx, cloudscale.WithTagFilter(lookupKey))
215+
ssa, err := sc.List(ctx, cloudscale.WithTagFilter(lookupKey))
217216
if err != nil {
218217
return nil, fmt.Errorf("failed to list servers: %w", err)
219218
}
219+
// The cloudscale API does not support filtering by multiple tags, so we have to filter manually
220+
ss := make([]cloudscale.Server, 0, len(ssa))
221+
for _, s := range ssa {
222+
if tk := s.TaggedResource.Tags[machineClusterIDTag]; tk != "" && tk == machineCtx.clusterId {
223+
ss = append(ss, s)
224+
}
225+
}
226+
220227
if len(ss) == 0 {
221228
return nil, nil
222229
}

pkg/machine/actuator_test.go

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ func Test_Actuator_Create_ComplexMachineE2E(t *testing.T) {
128128
ZonalResourceRequest: cloudscale.ZonalResourceRequest{
129129
Zone: providerSpec.Zone,
130130
},
131-
132131
Flavor: providerSpec.Flavor,
133132
Image: providerSpec.Image,
134133
VolumeSizeGB: providerSpec.RootVolumeSizeGB,
@@ -150,6 +149,12 @@ func Test_Actuator_Create_ComplexMachineE2E(t *testing.T) {
150149
}),
151150
).DoAndReturn(cloudscaleServerFromServerRequest(func(s *cloudscale.Server) {
152151
s.UUID = "created-server-uuid"
152+
s.TaggedResource = cloudscale.TaggedResource{
153+
Tags: cloudscale.TagMap{
154+
machineNameTag: machine.Name,
155+
machineClusterIDTag: clusterID,
156+
},
157+
}
153158
}))
154159

155160
require.NoError(t, actuator.Create(ctx, machine))
@@ -444,6 +449,7 @@ func Test_Actuator_Create_AntiAffinityPools(t *testing.T) {
444449

445450
func Test_Actuator_Exists(t *testing.T) {
446451
t.Parallel()
452+
const clusterID = "cluster-id"
447453

448454
tcs := []struct {
449455
name string
@@ -455,6 +461,12 @@ func Test_Actuator_Exists(t *testing.T) {
455461
servers: []cloudscale.Server{
456462
{
457463
Name: "app-test",
464+
TaggedResource: cloudscale.TaggedResource{
465+
Tags: cloudscale.TagMap{
466+
machineNameTag: "app-test",
467+
machineClusterIDTag: clusterID,
468+
},
469+
},
458470
},
459471
},
460472
exists: true,
@@ -464,14 +476,27 @@ func Test_Actuator_Exists(t *testing.T) {
464476
servers: []cloudscale.Server{},
465477
exists: false,
466478
},
479+
{
480+
name: "machine has wrong cluster ID",
481+
servers: []cloudscale.Server{
482+
{
483+
Name: "app-test",
484+
TaggedResource: cloudscale.TaggedResource{
485+
Tags: cloudscale.TagMap{
486+
machineNameTag: "app-test",
487+
machineClusterIDTag: "wrong-cluster-id",
488+
},
489+
},
490+
},
491+
},
492+
exists: false,
493+
},
467494
}
468495

469496
for _, tc := range tcs {
470497
t.Run(tc.name, func(t *testing.T) {
471498
t.Parallel()
472499

473-
const clusterID = "cluster-id"
474-
475500
ctx := context.Background()
476501

477502
ctrl := gomock.NewController(t)
@@ -504,8 +529,7 @@ func Test_Actuator_Exists(t *testing.T) {
504529
actuator := newActuator(c, ss, sgs)
505530

506531
ss.EXPECT().List(ctx, csTagMatcher{t: t, tags: map[string]string{
507-
machineNameTag: machine.Name,
508-
machineClusterIDTag: clusterID,
532+
machineNameTag: machine.Name,
509533
}}).Return(tc.servers, nil)
510534

511535
exists, err := actuator.Exists(ctx, machine)
@@ -554,11 +578,16 @@ func Test_Actuator_Update(t *testing.T) {
554578
ss.EXPECT().List(ctx, csTagMatcher{
555579
t: t,
556580
tags: map[string]string{
557-
machineNameTag: machine.Name,
558-
machineClusterIDTag: clusterID,
581+
machineNameTag: machine.Name,
559582
},
560583
}).Return([]cloudscale.Server{{
561584
UUID: "machine-uuid",
585+
TaggedResource: cloudscale.TaggedResource{
586+
Tags: cloudscale.TagMap{
587+
machineNameTag: machine.Name,
588+
machineClusterIDTag: clusterID,
589+
},
590+
},
562591
}}, nil)
563592

564593
require.NoError(t, actuator.Update(ctx, machine))
@@ -586,12 +615,17 @@ func Test_Actuator_Delete(t *testing.T) {
586615
ss.EXPECT().List(
587616
gomock.Any(),
588617
csTagMatcher{t: t, tags: map[string]string{
589-
machineNameTag: machine.Name,
590-
machineClusterIDTag: clusterID,
618+
machineNameTag: machine.Name,
591619
}},
592620
).Return([]cloudscale.Server{
593621
{
594622
UUID: "machine-uuid",
623+
TaggedResource: cloudscale.TaggedResource{
624+
Tags: cloudscale.TagMap{
625+
machineNameTag: machine.Name,
626+
machineClusterIDTag: clusterID,
627+
},
628+
},
595629
},
596630
}, nil)
597631
ss.EXPECT().Delete(
@@ -605,8 +639,7 @@ func Test_Actuator_Delete(t *testing.T) {
605639
ss.EXPECT().List(
606640
gomock.Any(),
607641
csTagMatcher{t: t, tags: map[string]string{
608-
machineNameTag: machine.Name,
609-
machineClusterIDTag: clusterID,
642+
machineNameTag: machine.Name,
610643
}},
611644
).Return([]cloudscale.Server{}, nil)
612645
},

0 commit comments

Comments
 (0)