Skip to content

Commit

Permalink
fix:failed get correct asgs when use multiple --node-group-auto-disco…
Browse files Browse the repository at this point in the history
…very
  • Loading branch information
maoqide committed Jul 5, 2024
1 parent 02521ed commit 677d897
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
18 changes: 10 additions & 8 deletions cluster-autoscaler/cloudprovider/aws/auto_scaling_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...)
Expand Down
48 changes: 48 additions & 0 deletions cluster-autoscaler/cloudprovider/aws/auto_scaling_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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{
map[string]string{"tag": "", "anothertag": ""},
map[string]string{"cooltag": "", "anothertag": ""},
map[string]string{"label": "value", "anothertag": ""},
map[string]string{"my:label": "value", "my:otherlabel": "othervalue"},
},
},
{
name: "SingleSpec",
specs: []string{
"asg:tag=mylabel=value,myotherlabel=othervalue",
},
want: []map[string]string{
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)
Expand Down

0 comments on commit 677d897

Please sign in to comment.