diff --git a/cluster-autoscaler/cloudprovider/aws/auto_scaling_groups.go b/cluster-autoscaler/cloudprovider/aws/auto_scaling_groups.go index 6c34b7c726dc..18083c8fb205 100644 --- a/cluster-autoscaler/cloudprovider/aws/auto_scaling_groups.go +++ b/cluster-autoscaler/cloudprovider/aws/auto_scaling_groups.go @@ -391,12 +391,10 @@ func (m *asgCache) isPlaceholderInstance(instance *AwsInstanceRef) bool { // Fetch automatically discovered ASGs. These ASGs should be unregistered if // they no longer exist in AWS. -func (m *asgCache) buildAsgTags() map[string]string { - groupTags := map[string]string{} +func (m *asgCache) buildAsgTags() []map[string]string { + groupTags := []map[string]string{} for _, spec := range m.asgAutoDiscoverySpecs { - for k, v := range spec.Tags { - groupTags[k] = v - } + groupTags = append(groupTags, spec.Tags) } return groupTags @@ -433,9 +431,13 @@ func (m *asgCache) regenerate() error { refreshTags := m.buildAsgTags() klog.V(4).Infof("Regenerating instance to ASG map for ASG tags: %v", refreshTags) - taggedGroups, err := m.awsService.getAutoscalingGroupsByTags(refreshTags) - if err != nil { - return err + taggedGroups := []*autoscaling.Group{} + for _, tags := range refreshTags { + groups, err := m.awsService.getAutoscalingGroupsByTags(tags) + if err != nil { + return err + } + taggedGroups = append(taggedGroups, groups...) } groups := append(namedGroups, taggedGroups...) diff --git a/cluster-autoscaler/cloudprovider/aws/auto_scaling_groups_test.go b/cluster-autoscaler/cloudprovider/aws/auto_scaling_groups_test.go index b536194fdf64..b8a3efd70674 100644 --- a/cluster-autoscaler/cloudprovider/aws/auto_scaling_groups_test.go +++ b/cluster-autoscaler/cloudprovider/aws/auto_scaling_groups_test.go @@ -22,6 +22,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "k8s.io/autoscaler/cluster-autoscaler/cloudprovider" "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/aws/aws-sdk-go/aws" "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/aws/aws-sdk-go/service/autoscaling" ) @@ -45,6 +46,53 @@ func TestBuildAsg(t *testing.T) { assert.Error(t, err) } +func TestBuildAsgTags(t *testing.T) { + + cases := []struct { + name string + specs []string + want []map[string]string + wantErr bool + }{ + { + name: "MultiSpecs", + specs: []string{ + "asg:tag=tag,anothertag", + "asg:tag=cooltag,anothertag", + "asg:tag=label=value,anothertag", + "asg:tag=my:label=value,my:otherlabel=othervalue", + }, + want: []map[string]string{ + {"tag": "", "anothertag": ""}, + {"cooltag": "", "anothertag": ""}, + {"label": "value", "anothertag": ""}, + {"my:label": "value", "my:otherlabel": "othervalue"}, + }, + }, + { + name: "SingleSpec", + specs: []string{ + "asg:tag=mylabel=value,myotherlabel=othervalue", + }, + want: []map[string]string{ + {"mylabel": "value", "myotherlabel": "othervalue"}, + }, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + do := cloudprovider.NodeGroupDiscoveryOptions{NodeGroupAutoDiscoverySpecs: tc.specs} + specs, _ := parseASGAutoDiscoverySpecs(do) + asgCache := &asgCache{ + asgAutoDiscoverySpecs: specs, + } + got := asgCache.buildAsgTags() + assert.True(t, assert.ObjectsAreEqualValues(tc.want, got), "\ngot: %#v\nwant: %#v", got, tc.want) + }) + } +} + func validateAsg(t *testing.T, asg *asg, name string, minSize int, maxSize int) { assert.Equal(t, name, asg.Name) assert.Equal(t, minSize, asg.minSize)