Skip to content

Commit 790646f

Browse files
Remove sorting from instance keys when creating input tag specifications
Co-authored-by: Kirk Bater <[email protected]>
1 parent 2a4d434 commit 790646f

File tree

3 files changed

+81
-19
lines changed

3 files changed

+81
-19
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
github.com/sirupsen/logrus v1.9.3
4141
github.com/spf13/cobra v1.8.0
4242
github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace
43+
github.com/stretchr/testify v1.9.0
4344
github.com/zgalor/weberr v0.6.0
4445
golang.org/x/crypto v0.22.0
4546
golang.org/x/text v0.14.0
@@ -169,6 +170,7 @@ require (
169170
github.com/pelletier/go-toml v1.9.5 // indirect
170171
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
171172
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
173+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
172174
github.com/prometheus/client_model v0.6.1 // indirect
173175
github.com/prometheus/common v0.52.2 // indirect
174176
github.com/prometheus/procfs v0.13.0 // indirect
@@ -185,7 +187,6 @@ require (
185187
github.com/spf13/cast v1.6.0 // indirect
186188
github.com/spf13/viper v1.18.2 // indirect
187189
github.com/stoewer/go-strcase v1.2.0 // indirect
188-
github.com/stretchr/testify v1.9.0 // indirect
189190
github.com/subosito/gotenv v1.6.0 // indirect
190191
github.com/valyala/fastjson v1.6.4 // indirect
191192
github.com/vincent-petithory/dataurl v1.0.0 // indirect

pkg/cloud/services/ec2/instances.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -619,17 +619,10 @@ func (s *Service) runInstance(role string, i *infrav1.Instance) (*infrav1.Instan
619619
resources := []string{ec2.ResourceTypeInstance, ec2.ResourceTypeVolume, ec2.ResourceTypeNetworkInterface}
620620
for _, r := range resources {
621621
spec := &ec2.TagSpecification{ResourceType: aws.String(r)}
622-
623-
// We need to sort keys for tests to work
624-
keys := make([]string, 0, len(i.Tags))
625-
for k := range i.Tags {
626-
keys = append(keys, k)
627-
}
628-
sort.Strings(keys)
629-
for _, key := range keys {
622+
for tagKey, tagValue := range i.Tags {
630623
spec.Tags = append(spec.Tags, &ec2.Tag{
631-
Key: aws.String(key),
632-
Value: aws.String(i.Tags[key]),
624+
Key: aws.String(tagKey),
625+
Value: aws.String(tagValue),
633626
})
634627
}
635628

pkg/cloud/services/ec2/instances_test.go

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package ec2
1919
import (
2020
"context"
2121
"encoding/base64"
22+
"fmt"
2223
"strings"
2324
"testing"
2425

@@ -30,6 +31,7 @@ import (
3031
"github.com/google/go-cmp/cmp"
3132
. "github.com/onsi/gomega"
3233
"github.com/pkg/errors"
34+
"github.com/stretchr/testify/assert"
3335
corev1 "k8s.io/api/core/v1"
3436
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3537
"k8s.io/apimachinery/pkg/runtime"
@@ -45,6 +47,72 @@ import (
4547
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
4648
)
4749

50+
type runInstancesInputMatcher struct {
51+
runInstancesInput *ec2.RunInstancesInput
52+
}
53+
54+
func (m runInstancesInputMatcher) Matches(arg interface{}) bool {
55+
riiArg, ok := arg.(*ec2.RunInstancesInput)
56+
if !ok {
57+
return false
58+
}
59+
60+
if *m.runInstancesInput.ImageId != *riiArg.ImageId {
61+
return false
62+
}
63+
if *m.runInstancesInput.InstanceType != *riiArg.InstanceType {
64+
return false
65+
}
66+
if *m.runInstancesInput.KeyName != *riiArg.KeyName {
67+
return false
68+
}
69+
if *m.runInstancesInput.SubnetId != *riiArg.SubnetId {
70+
return false
71+
}
72+
if *m.runInstancesInput.UserData != *riiArg.UserData {
73+
return false
74+
}
75+
if *m.runInstancesInput.MaxCount != *riiArg.MaxCount {
76+
return false
77+
}
78+
if *m.runInstancesInput.MinCount != *riiArg.MinCount {
79+
return false
80+
}
81+
82+
if !assert.ElementsMatch(nil, m.runInstancesInput.SecurityGroupIds, riiArg.SecurityGroupIds) {
83+
return false
84+
}
85+
86+
if len(m.runInstancesInput.TagSpecifications) != len(riiArg.TagSpecifications) {
87+
return false
88+
}
89+
90+
for _, instanceTagSpec := range m.runInstancesInput.TagSpecifications {
91+
found := false
92+
for _, argTagSpec := range riiArg.TagSpecifications {
93+
if *instanceTagSpec.ResourceType == *argTagSpec.ResourceType {
94+
found = true
95+
if !assert.ElementsMatch(nil, instanceTagSpec.Tags, argTagSpec.Tags) {
96+
return false
97+
}
98+
}
99+
}
100+
if !found {
101+
return false
102+
}
103+
}
104+
105+
return true
106+
}
107+
108+
func (m runInstancesInputMatcher) String() string {
109+
return fmt.Sprintf("has the same elements as %v", m.runInstancesInput)
110+
}
111+
112+
func RunInstancesInputEq(runInstancesInput *ec2.RunInstancesInput) gomock.Matcher {
113+
return runInstancesInputMatcher{runInstancesInput}
114+
}
115+
48116
func TestInstanceIfExists(t *testing.T) {
49117
mockCtrl := gomock.NewController(t)
50118
defer mockCtrl.Finish()
@@ -671,7 +739,7 @@ func TestCreateInstance(t *testing.T) {
671739
},
672740
}, nil)
673741
m.
674-
RunInstancesWithContext(context.TODO(), &ec2.RunInstancesInput{
742+
RunInstancesWithContext(context.TODO(), RunInstancesInputEq(&ec2.RunInstancesInput{
675743
ImageId: aws.String("abc"),
676744
InstanceType: aws.String("m5.2xlarge"),
677745
KeyName: aws.String("default"),
@@ -757,7 +825,7 @@ func TestCreateInstance(t *testing.T) {
757825
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
758826
MaxCount: aws.Int64(1),
759827
MinCount: aws.Int64(1),
760-
}).Return(&ec2.Reservation{
828+
})).Return(&ec2.Reservation{
761829
Instances: []*ec2.Instance{
762830
{
763831
State: &ec2.InstanceState{
@@ -919,7 +987,7 @@ func TestCreateInstance(t *testing.T) {
919987
},
920988
}, nil)
921989
m.
922-
RunInstancesWithContext(context.TODO(), &ec2.RunInstancesInput{
990+
RunInstancesWithContext(context.TODO(), RunInstancesInputEq(&ec2.RunInstancesInput{
923991
ImageId: aws.String("abc"),
924992
InstanceType: aws.String("m5.2xlarge"),
925993
KeyName: aws.String("default"),
@@ -1005,7 +1073,7 @@ func TestCreateInstance(t *testing.T) {
10051073
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
10061074
MaxCount: aws.Int64(1),
10071075
MinCount: aws.Int64(1),
1008-
}).Return(&ec2.Reservation{
1076+
})).Return(&ec2.Reservation{
10091077
Instances: []*ec2.Instance{
10101078
{
10111079
State: &ec2.InstanceState{
@@ -3337,7 +3405,7 @@ func TestCreateInstance(t *testing.T) {
33373405
},
33383406
expect: func(m *mocks.MockEC2APIMockRecorder) {
33393407
m. // TODO: Restore these parameters, but with the tags as well
3340-
RunInstancesWithContext(context.TODO(), gomock.Eq(&ec2.RunInstancesInput{
3408+
RunInstancesWithContext(context.TODO(), RunInstancesInputEq(&ec2.RunInstancesInput{
33413409
ImageId: aws.String("abc"),
33423410
InstanceType: aws.String("m5.large"),
33433411
KeyName: aws.String("default"),
@@ -3546,7 +3614,7 @@ func TestCreateInstance(t *testing.T) {
35463614
},
35473615
expect: func(m *mocks.MockEC2APIMockRecorder) {
35483616
m. // TODO: Restore these parameters, but with the tags as well
3549-
RunInstancesWithContext(context.TODO(), gomock.Eq(&ec2.RunInstancesInput{
3617+
RunInstancesWithContext(context.TODO(), RunInstancesInputEq(&ec2.RunInstancesInput{
35503618
ImageId: aws.String("abc"),
35513619
InstanceType: aws.String("m5.large"),
35523620
KeyName: aws.String("default"),
@@ -3775,7 +3843,7 @@ func TestCreateInstance(t *testing.T) {
37753843
},
37763844
}, nil)
37773845
m. // TODO: Restore these parameters, but with the tags as well
3778-
RunInstancesWithContext(context.TODO(), gomock.Eq(&ec2.RunInstancesInput{
3846+
RunInstancesWithContext(context.TODO(), RunInstancesInputEq(&ec2.RunInstancesInput{
37793847
ImageId: aws.String("abc"),
37803848
InstanceType: aws.String("m5.large"),
37813849
KeyName: aws.String("default"),
@@ -3966,7 +4034,7 @@ func TestCreateInstance(t *testing.T) {
39664034
},
39674035
expect: func(m *mocks.MockEC2APIMockRecorder) {
39684036
m. // TODO: Restore these parameters, but with the tags as well
3969-
RunInstancesWithContext(context.TODO(), gomock.Eq(&ec2.RunInstancesInput{
4037+
RunInstancesWithContext(context.TODO(), RunInstancesInputEq(&ec2.RunInstancesInput{
39704038
ImageId: aws.String("abc"),
39714039
InstanceType: aws.String("m5.large"),
39724040
KeyName: aws.String("default"),

0 commit comments

Comments
 (0)