Skip to content

Commit

Permalink
Merge pull request #14 from appuio/fix-server-list
Browse files Browse the repository at this point in the history
Fix filtering servers by cluster id; cloudscale only supports single tag filter
  • Loading branch information
bastjan authored Nov 13, 2024
2 parents 3ec052c + f6bcfa4 commit 91eb7fd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
13 changes: 10 additions & 3 deletions pkg/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,21 @@ func (a *Actuator) Delete(ctx context.Context, machine *machinev1beta1.Machine)

func (a *Actuator) getServer(ctx context.Context, sc cloudscale.ServerService, machineCtx machineContext) (*cloudscale.Server, error) {
lookupKey := cloudscale.TagMap{
machineNameTag: machineCtx.machine.Name,
machineClusterIDTag: machineCtx.clusterId,
machineNameTag: machineCtx.machine.Name,
}

ss, err := sc.List(ctx, cloudscale.WithTagFilter(lookupKey))
ssa, err := sc.List(ctx, cloudscale.WithTagFilter(lookupKey))
if err != nil {
return nil, fmt.Errorf("failed to list servers: %w", err)
}
// The cloudscale API does not support filtering by multiple tags, so we have to filter manually
ss := make([]cloudscale.Server, 0, len(ssa))
for _, s := range ssa {
if tk := s.TaggedResource.Tags[machineClusterIDTag]; tk != "" && tk == machineCtx.clusterId {
ss = append(ss, s)
}
}

if len(ss) == 0 {
return nil, nil
}
Expand Down
55 changes: 44 additions & 11 deletions pkg/machine/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func Test_Actuator_Create_ComplexMachineE2E(t *testing.T) {
ZonalResourceRequest: cloudscale.ZonalResourceRequest{
Zone: providerSpec.Zone,
},

Flavor: providerSpec.Flavor,
Image: providerSpec.Image,
VolumeSizeGB: providerSpec.RootVolumeSizeGB,
Expand All @@ -150,6 +149,12 @@ func Test_Actuator_Create_ComplexMachineE2E(t *testing.T) {
}),
).DoAndReturn(cloudscaleServerFromServerRequest(func(s *cloudscale.Server) {
s.UUID = "created-server-uuid"
s.TaggedResource = cloudscale.TaggedResource{
Tags: cloudscale.TagMap{
machineNameTag: machine.Name,
machineClusterIDTag: clusterID,
},
}
}))

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

func Test_Actuator_Exists(t *testing.T) {
t.Parallel()
const clusterID = "cluster-id"

tcs := []struct {
name string
Expand All @@ -455,6 +461,12 @@ func Test_Actuator_Exists(t *testing.T) {
servers: []cloudscale.Server{
{
Name: "app-test",
TaggedResource: cloudscale.TaggedResource{
Tags: cloudscale.TagMap{
machineNameTag: "app-test",
machineClusterIDTag: clusterID,
},
},
},
},
exists: true,
Expand All @@ -464,14 +476,27 @@ func Test_Actuator_Exists(t *testing.T) {
servers: []cloudscale.Server{},
exists: false,
},
{
name: "machine has wrong cluster ID",
servers: []cloudscale.Server{
{
Name: "app-test",
TaggedResource: cloudscale.TaggedResource{
Tags: cloudscale.TagMap{
machineNameTag: "app-test",
machineClusterIDTag: "wrong-cluster-id",
},
},
},
},
exists: false,
},
}

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

const clusterID = "cluster-id"

ctx := context.Background()

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

ss.EXPECT().List(ctx, csTagMatcher{t: t, tags: map[string]string{
machineNameTag: machine.Name,
machineClusterIDTag: clusterID,
machineNameTag: machine.Name,
}}).Return(tc.servers, nil)

exists, err := actuator.Exists(ctx, machine)
Expand Down Expand Up @@ -554,11 +578,16 @@ func Test_Actuator_Update(t *testing.T) {
ss.EXPECT().List(ctx, csTagMatcher{
t: t,
tags: map[string]string{
machineNameTag: machine.Name,
machineClusterIDTag: clusterID,
machineNameTag: machine.Name,
},
}).Return([]cloudscale.Server{{
UUID: "machine-uuid",
TaggedResource: cloudscale.TaggedResource{
Tags: cloudscale.TagMap{
machineNameTag: machine.Name,
machineClusterIDTag: clusterID,
},
},
}}, nil)

require.NoError(t, actuator.Update(ctx, machine))
Expand Down Expand Up @@ -586,12 +615,17 @@ func Test_Actuator_Delete(t *testing.T) {
ss.EXPECT().List(
gomock.Any(),
csTagMatcher{t: t, tags: map[string]string{
machineNameTag: machine.Name,
machineClusterIDTag: clusterID,
machineNameTag: machine.Name,
}},
).Return([]cloudscale.Server{
{
UUID: "machine-uuid",
TaggedResource: cloudscale.TaggedResource{
Tags: cloudscale.TagMap{
machineNameTag: machine.Name,
machineClusterIDTag: clusterID,
},
},
},
}, nil)
ss.EXPECT().Delete(
Expand All @@ -605,8 +639,7 @@ func Test_Actuator_Delete(t *testing.T) {
ss.EXPECT().List(
gomock.Any(),
csTagMatcher{t: t, tags: map[string]string{
machineNameTag: machine.Name,
machineClusterIDTag: clusterID,
machineNameTag: machine.Name,
}},
).Return([]cloudscale.Server{}, nil)
},
Expand Down

0 comments on commit 91eb7fd

Please sign in to comment.