Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controllers/awsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ func (r *AWSMachineReconciler) createInstance(ctx context.Context, ec2svc servic
return nil, errors.Wrapf(userDataErr, "failed to resolve userdata")
}

instance, err := ec2svc.CreateInstance(machineScope, userData, userDataFormat)
instance, err := ec2svc.CreateInstance(ctx, machineScope, userData, userDataFormat)
if err != nil {
return nil, errors.Wrapf(err, "failed to create AWSMachine instance")
}
Expand Down
24 changes: 12 additions & 12 deletions controllers/awsmachine_controller_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func TestAWSMachineReconciler(t *testing.T) {
expectedErr := errors.New("Invalid instance")
ec2Svc.EXPECT().InstanceIfExists(gomock.Any()).Return(nil, nil)
secretSvc.EXPECT().Create(gomock.Any(), gomock.Any()).Return("test", int32(1), nil).Times(1)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, expectedErr)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, expectedErr)
secretSvc.EXPECT().UserData(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).Times(1)

_, err := reconciler.reconcileNormal(context.Background(), ms, cs, cs, cs, cs)
Expand Down Expand Up @@ -349,7 +349,7 @@ func TestAWSMachineReconciler(t *testing.T) {
instance.State = infrav1.InstanceStatePending

ec2Svc.EXPECT().GetRunningInstanceByTags(gomock.Any()).Return(nil, nil)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil)
}

t.Run("instance security group errors", func(t *testing.T) {
Expand Down Expand Up @@ -785,7 +785,7 @@ func TestAWSMachineReconciler(t *testing.T) {
return elbSvc
}

ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil)
ec2Svc.EXPECT().GetRunningInstanceByTags(gomock.Any()).Return(nil, nil)
elbSvc.EXPECT().IsInstanceRegisteredWithAPIServerELB(gomock.Any()).Return(false, errors.New("error describing ELB"))
secretSvc.EXPECT().UserData(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).Times(1)
Expand All @@ -811,7 +811,7 @@ func TestAWSMachineReconciler(t *testing.T) {
return elbSvc
}

ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil)
ec2Svc.EXPECT().GetRunningInstanceByTags(gomock.Any()).Return(nil, nil)
elbSvc.EXPECT().IsInstanceRegisteredWithAPIServerELB(gomock.Any()).Return(false, nil)
elbSvc.EXPECT().RegisterInstanceWithAPIServerELB(gomock.Any()).Return(errors.New("failed to attach ELB"))
Expand Down Expand Up @@ -842,7 +842,7 @@ func TestAWSMachineReconciler(t *testing.T) {
ensureTag := func(t *testing.T, g *WithT) {
t.Helper()
ec2Svc.EXPECT().InstanceIfExists(gomock.Any()).Return(nil, nil)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil)
secretSvc.EXPECT().Create(gomock.Any(), gomock.Any()).Return("test", int32(1), nil)
secretSvc.EXPECT().UserData(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil)
}
Expand Down Expand Up @@ -888,7 +888,7 @@ func TestAWSMachineReconciler(t *testing.T) {
ensureSecurityGroups := func(t *testing.T, g *WithT) {
t.Helper()
ec2Svc.EXPECT().InstanceIfExists(gomock.Any()).Return(nil, nil)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil)
secretSvc.EXPECT().Create(gomock.Any(), gomock.Any()).Return("test", int32(1), nil)
secretSvc.EXPECT().UserData(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil)
ec2Svc.EXPECT().GetInstanceSecurityGroups(gomock.Any()).Return(map[string][]string{"eid": {}}, nil)
Expand Down Expand Up @@ -1011,7 +1011,7 @@ func TestAWSMachineReconciler(t *testing.T) {

ec2Svc.EXPECT().GetRunningInstanceByTags(gomock.Any()).Return(nil, nil).AnyTimes()
secretSvc.EXPECT().Create(gomock.Any(), gomock.Any()).Return(secretPrefix, int32(1), nil).Times(1)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil).AnyTimes()
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil).AnyTimes()
secretSvc.EXPECT().UserData(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).Times(1)
ec2Svc.EXPECT().GetInstanceSecurityGroups(gomock.Any()).Return(map[string][]string{"eid": {}}, nil).Times(1)
ec2Svc.EXPECT().GetCoreSecurityGroups(gomock.Any()).Return([]string{}, nil).Times(1)
Expand Down Expand Up @@ -1048,7 +1048,7 @@ func TestAWSMachineReconciler(t *testing.T) {

ec2Svc.EXPECT().GetRunningInstanceByTags(gomock.Any()).Return(nil, nil).AnyTimes()
secretSvc.EXPECT().Create(gomock.Any(), gomock.Any()).Return(secretPrefix, int32(1), nil).Times(1)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil).AnyTimes()
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil).AnyTimes()
secretSvc.EXPECT().UserData(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).Times(1)
ec2Svc.EXPECT().GetInstanceSecurityGroups(gomock.Any()).Return(map[string][]string{"eid": {}}, nil).Times(1)
ec2Svc.EXPECT().GetCoreSecurityGroups(gomock.Any()).Return([]string{}, nil).Times(1)
Expand Down Expand Up @@ -1304,7 +1304,7 @@ func TestAWSMachineReconciler(t *testing.T) {
}
instance.State = infrav1.InstanceStatePending
secretSvc.EXPECT().Create(gomock.Any(), gomock.Any()).Return(secretPrefix, int32(1), nil).Times(1)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil).AnyTimes()
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil).AnyTimes()
secretSvc.EXPECT().UserData(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).Times(1)
ec2Svc.EXPECT().GetInstanceSecurityGroups(gomock.Any()).Return(map[string][]string{"eid": {}}, nil).Times(1)
ec2Svc.EXPECT().GetCoreSecurityGroups(gomock.Any()).Return([]string{}, nil).Times(1)
Expand Down Expand Up @@ -1357,7 +1357,7 @@ func TestAWSMachineReconciler(t *testing.T) {
fakeS3URL := "s3://foo"

objectStoreSvc.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any()).Return(fakeS3URL, nil).Times(1)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil).AnyTimes()
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil).AnyTimes()
ec2Svc.EXPECT().GetInstanceSecurityGroups(gomock.Any()).Return(map[string][]string{"eid": {}}, nil).Times(1)
ec2Svc.EXPECT().GetCoreSecurityGroups(gomock.Any()).Return([]string{}, nil).Times(1)
ec2Svc.EXPECT().GetAdditionalSecurityGroupsIDs(gomock.Any()).Return(nil, nil)
Expand Down Expand Up @@ -1392,7 +1392,7 @@ func TestAWSMachineReconciler(t *testing.T) {
presigned := "https://cluster-api-aws.s3.us-west-2.amazonaws.com/bootstrap-data.yaml?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA3SGQVQG7FGA6KKA6%2F20221104%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20221104T140227Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=b228dbec8c1008c80c162e1210e4503dceead1e4d4751b4d9787314fd6da4d55"

objectStoreSvc.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any()).Return(presigned, nil).Times(1)
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil).AnyTimes()
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil).AnyTimes()
ec2Svc.EXPECT().GetInstanceSecurityGroups(gomock.Any()).Return(map[string][]string{"eid": {}}, nil).Times(1)
ec2Svc.EXPECT().GetCoreSecurityGroups(gomock.Any()).Return([]string{}, nil).Times(1)
ec2Svc.EXPECT().GetAdditionalSecurityGroupsIDs(gomock.Any()).Return(nil, nil)
Expand Down Expand Up @@ -1611,7 +1611,7 @@ func TestAWSMachineReconciler(t *testing.T) {
// Expect no Cluster Object Store to be created.
objectStoreSvc.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any()).Return(fakeS3URL, nil).Times(0)

ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil).AnyTimes()
ec2Svc.EXPECT().CreateInstance(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(instance, nil).AnyTimes()
ec2Svc.EXPECT().GetInstanceSecurityGroups(gomock.Any()).Return(map[string][]string{"eid": {}}, nil).Times(1)
ec2Svc.EXPECT().GetCoreSecurityGroups(gomock.Any()).Return([]string{}, nil).Times(1)
ec2Svc.EXPECT().GetAdditionalSecurityGroupsIDs(gomock.Any()).Return(nil, nil)
Expand Down
16 changes: 8 additions & 8 deletions exp/controllers/awsmachinepool_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
defer teardown(t, g)

ec2Svc.EXPECT().GetLaunchTemplate(gomock.Eq("test")).Return(nil, "", nil, nil, nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any()).Return(ptr.To[string]("ami-abcdef123"), nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any(), gomock.Any()).Return(ptr.To[string]("ami-abcdef123"), nil)
ec2Svc.EXPECT().CreateLaunchTemplate(gomock.Any(), gomock.Eq(ptr.To[string]("ami-abcdef123")), gomock.Eq(userDataSecretKey), gomock.Eq([]byte("shell-script")), gomock.Eq(userdata.ComputeHash([]byte("shell-script")))).Return("lt-ghijkl456", nil)
asgSvc.EXPECT().GetASGByName(gomock.Any()).Return(nil, nil)
asgSvc.EXPECT().CreateASG(gomock.Any()).DoAndReturn(func(scope *scope.MachinePoolScope) (*expinfrav1.AutoScalingGroup, error) {
Expand Down Expand Up @@ -740,7 +740,7 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
&userDataSecretKey,
nil,
nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any()).Return(ptr.To[string]("ami-existing"), nil) // no change
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any(), gomock.Any()).Return(ptr.To[string]("ami-existing"), nil) // no change
ec2Svc.EXPECT().LaunchTemplateNeedsUpdate(gomock.Any(), gomock.Any(), gomock.Any()).Return(false, nil)

asgSvc.EXPECT().GetASGByName(gomock.Any()).DoAndReturn(func(scope *scope.MachinePoolScope) (*expinfrav1.AutoScalingGroup, error) {
Expand Down Expand Up @@ -789,7 +789,7 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
&userDataSecretKey,
nil,
nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any()).Return(ptr.To[string]("ami-different"), nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any(), gomock.Any()).Return(ptr.To[string]("ami-different"), nil)
ec2Svc.EXPECT().LaunchTemplateNeedsUpdate(gomock.Any(), gomock.Any(), gomock.Any()).Return(false, nil)
asgSvc.EXPECT().CanStartASGInstanceRefresh(gomock.Any()).Return(true, nil)
ec2Svc.EXPECT().PruneLaunchTemplateVersions(gomock.Any()).Return(nil, nil)
Expand Down Expand Up @@ -845,7 +845,7 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
&apimachinerytypes.NamespacedName{Namespace: "default", Name: "previous-secret-name"},
nil,
nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any()).Return(ptr.To[string]("ami-existing"), nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any(), gomock.Any()).Return(ptr.To[string]("ami-existing"), nil)
ec2Svc.EXPECT().LaunchTemplateNeedsUpdate(gomock.Any(), gomock.Any(), gomock.Any()).Return(false, nil)
asgSvc.EXPECT().CanStartASGInstanceRefresh(gomock.Any()).Return(true, nil)
ec2Svc.EXPECT().PruneLaunchTemplateVersions(gomock.Any()).Return(nil, nil)
Expand Down Expand Up @@ -887,7 +887,7 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
defer teardown(t, g)

ec2Svc.EXPECT().GetLaunchTemplate(gomock.Eq("test")).Return(nil, "", nil, nil, nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any()).Return(ptr.To[string]("ami-abcdef123"), nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any(), gomock.Any()).Return(ptr.To[string]("ami-abcdef123"), nil)
ec2Svc.EXPECT().CreateLaunchTemplate(gomock.Any(), gomock.Eq(ptr.To[string]("ami-abcdef123")), gomock.Eq(userDataSecretKey), gomock.Eq([]byte("shell-script")), gomock.Eq(userdata.ComputeHash([]byte("shell-script")))).Return("lt-ghijkl456", nil)
asgSvc.EXPECT().GetASGByName(gomock.Any()).Return(nil, nil)
asgSvc.EXPECT().CreateASG(gomock.Any()).DoAndReturn(func(scope *scope.MachinePoolScope) (*expinfrav1.AutoScalingGroup, error) {
Expand Down Expand Up @@ -931,7 +931,7 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
&apimachinerytypes.NamespacedName{Namespace: "default", Name: "bootstrap-data"},
nil,
nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any()).Return(ptr.To[string]("ami-existing"), nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any(), gomock.Any()).Return(ptr.To[string]("ami-existing"), nil)
ec2Svc.EXPECT().LaunchTemplateNeedsUpdate(gomock.Any(), gomock.Any(), gomock.Any()).Return(false, nil)
asgSvc.EXPECT().CanStartASGInstanceRefresh(gomock.Any()).Return(true, nil)
ec2Svc.EXPECT().PruneLaunchTemplateVersions(gomock.Any()).Return(nil, nil)
Expand Down Expand Up @@ -1017,7 +1017,7 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
&apimachinerytypes.NamespacedName{Namespace: "default", Name: "previous-secret-name"},
nil,
nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any()).Return(ptr.To[string]("ami-existing"), nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any(), gomock.Any()).Return(ptr.To[string]("ami-existing"), nil)
ec2Svc.EXPECT().LaunchTemplateNeedsUpdate(gomock.Any(), gomock.Any(), gomock.Any()).Return(false, nil)

s3Mock.EXPECT().PutObject(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, input *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
Expand Down Expand Up @@ -1063,7 +1063,7 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
&apimachinerytypes.NamespacedName{Namespace: "default", Name: "previous-secret-name"},
nil,
nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any()).Return(ptr.To[string]("ami-existing"), nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any(), gomock.Any()).Return(ptr.To[string]("ami-existing"), nil)
ec2Svc.EXPECT().LaunchTemplateNeedsUpdate(gomock.Any(), gomock.Any(), gomock.Any()).Return(false, nil)

s3Mock.EXPECT().PutObject(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, input *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/eks v1.64.0
github.com/aws/aws-sdk-go-v2/service/iam v1.32.0
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1
github.com/aws/aws-sdk-go-v2/service/ssm v1.59.1
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6
github.com/aws/smithy-go v1.22.4
github.com/awslabs/goformation/v4 v4.19.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ github.com/aws/aws-sdk-go-v2/service/servicequotas v1.21.4 h1:SSDkZRAO8Ok5SoQ4BJ
github.com/aws/aws-sdk-go-v2/service/servicequotas v1.21.4/go.mod h1:plXue/Zg49kU3uU6WwfCWgRR5SRINNiJf03Y/UhYOhU=
github.com/aws/aws-sdk-go-v2/service/sqs v1.38.8 h1:80dpSqWMwx2dAm30Ib7J6ucz1ZHfiv5OCRwN/EnCOXQ=
github.com/aws/aws-sdk-go-v2/service/sqs v1.38.8/go.mod h1:IzNt/udsXlETCdvBOL0nmyMe2t9cGmXmZgsdoZGYYhI=
github.com/aws/aws-sdk-go-v2/service/ssm v1.59.1 h1:Z4cmgV3hKuUIkhJsdn47hf/ABYHUtILfMrV+L8+kRwE=
github.com/aws/aws-sdk-go-v2/service/ssm v1.59.1/go.mod h1:PUWUl5MDiYNQkUHN9Pyd9kgtA/YhbxnSnHP+yQqzrM8=
github.com/aws/aws-sdk-go-v2/service/sso v1.20.5 h1:vN8hEbpRnL7+Hopy9dzmRle1xmDc7o8tmY0klsr175w=
github.com/aws/aws-sdk-go-v2/service/sso v1.20.5/go.mod h1:qGzynb/msuZIE8I75DVRCUXw3o3ZyBmUvMwQ2t/BrGM=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 h1:Jux+gDDyi1Lruk+KHF91tK2KCuY61kzoCpvtvJJBtOE=
Expand Down
91 changes: 91 additions & 0 deletions pkg/cloud/converters/map_to_ssm_tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2021 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package converters

import (
"reflect"
"testing"

ssmtypes "github.com/aws/aws-sdk-go-v2/service/ssm/types"

infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
)

func TestMapToSSMTags(t *testing.T) {
tests := []struct {
name string
input infrav1.Tags
expected []ssmtypes.Tag
}{
{
name: "nil map",
input: nil,
expected: []ssmtypes.Tag{},
},
{
name: "empty map",
input: infrav1.Tags{},
expected: []ssmtypes.Tag{},
},
{
name: "single key-value",
input: infrav1.Tags{"k1": "v1"},
expected: []ssmtypes.Tag{
{Key: strPtr("k1"), Value: strPtr("v1")},
},
},
{
name: "multiple keys",
input: infrav1.Tags{"k1": "v1", "k2": "v2"},
expected: []ssmtypes.Tag{
{Key: strPtr("k1"), Value: strPtr("v1")},
{Key: strPtr("k2"), Value: strPtr("v2")},
},
},
{
name: "empty value string",
input: infrav1.Tags{"k1": ""},
expected: []ssmtypes.Tag{
{Key: strPtr("k1"), Value: strPtr("")},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := MapToSSMTags(tt.input)
if len(got) != len(tt.expected) {
t.Errorf("got %d tags, want %d", len(got), len(tt.expected))
return
}
gotMap := make(map[string]string)
for _, tag := range got {
gotMap[*tag.Key] = *tag.Value
}
expectedMap := make(map[string]string)
for _, tag := range tt.expected {
expectedMap[*tag.Key] = *tag.Value
}
if !reflect.DeepEqual(gotMap, expectedMap) {
t.Errorf("MapToSSMTags() = %v, want %v", gotMap, expectedMap)
}
})
}
}

func strPtr(s string) *string {
return &s
}
35 changes: 15 additions & 20 deletions pkg/cloud/converters/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (

autoscalingtypes "github.com/aws/aws-sdk-go-v2/service/autoscaling/types"
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
ssmtypes "github.com/aws/aws-sdk-go-v2/service/ssm/types"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/aws/aws-sdk-go/service/ssm"

infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
)
Expand Down Expand Up @@ -75,6 +75,20 @@ func MapToTags(src infrav1.Tags) []*ec2.Tag {
return tags
}

// MapToSSMTags converts infrav1.Tags (a map of string key-value pairs) to a slice of SSM Tag objects.
func MapToSSMTags(tags infrav1.Tags) []ssmtypes.Tag {
result := make([]ssmtypes.Tag, 0, len(tags))
for k, v := range tags {
key := k
value := v
result = append(result, ssmtypes.Tag{
Key: &key,
Value: &value,
})
}
return result
}

// ELBTagsToMap converts a []*elb.Tag into a infrav1.Tags.
func ELBTagsToMap(src []*elb.Tag) infrav1.Tags {
tags := make(infrav1.Tags, len(src))
Expand Down Expand Up @@ -154,25 +168,6 @@ func MapToSecretsManagerTags(src infrav1.Tags) []*secretsmanager.Tag {
return tags
}

// MapToSSMTags converts a infrav1.Tags to a []*ssm.Tag.
func MapToSSMTags(src infrav1.Tags) []*ssm.Tag {
tags := make([]*ssm.Tag, 0, len(src))

for k, v := range src {
tag := &ssm.Tag{
Key: aws.String(k),
Value: aws.String(v),
}

tags = append(tags, tag)
}

// Sort so that unit tests can expect a stable order
sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key })

return tags
}

// MapToIAMTags converts a infrav1.Tags to a []*iam.Tag.
func MapToIAMTags(src infrav1.Tags) []iamtypes.Tag {
tags := make([]iamtypes.Tag, 0, len(src))
Expand Down
Loading