Skip to content

Commit

Permalink
Get nodegroup desired capacity from autoscaling instead of cloudforma…
Browse files Browse the repository at this point in the history
…tion (#3515)
  • Loading branch information
omnibrian committed Apr 8, 2021
1 parent b1221cd commit cf6d0ed
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/cfn/manager/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"time"

"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/cloudtrail/cloudtrailiface"
"github.com/weaveworks/eksctl/pkg/cfn/waiter"

Expand Down Expand Up @@ -74,6 +75,7 @@ type StackCollection struct {
eksAPI eksiface.EKSAPI
iamAPI iamiface.IAMAPI
cloudTrailAPI cloudtrailiface.CloudTrailAPI
asgAPI autoscalingiface.AutoScalingAPI
spec *api.ClusterConfig
disableRollback bool
roleARN string
Expand Down Expand Up @@ -104,6 +106,7 @@ func NewStackCollection(provider api.ClusterProvider, spec *api.ClusterConfig) *
eksAPI: provider.EKS(),
iamAPI: provider.IAM(),
cloudTrailAPI: provider.CloudTrail(),
asgAPI: provider.ASG(),
disableRollback: provider.CloudFormationDisableRollback(),
roleARN: provider.CloudFormationRoleARN(),
region: provider.Region(),
Expand Down
26 changes: 26 additions & 0 deletions pkg/cfn/manager/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
cfn "github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/blang/semver"
Expand Down Expand Up @@ -298,6 +299,14 @@ func (c *StackCollection) GetNodeGroupSummaries(name string) ([]*NodeGroupSummar

summary.AutoScalingGroupName = asgName

asgDesiredCapacity, err := c.GetAutoScalingGroupDesiredCapacity(asgName)
if err != nil {
return nil, errors.Wrap(err, "getting autoscalinggroup desired capacity")
}
if asgDesiredCapacity != nil {
summary.DesiredCapacity = int(*asgDesiredCapacity)
}

if name == "" {
summaries = append(summaries, summary)
} else if summary.Name == name {
Expand Down Expand Up @@ -370,7 +379,24 @@ func (c *StackCollection) GetManagedNodeGroupAutoScalingGroupName(s *Stack) (str
}
}
return strings.Join(asgs, ","), nil
}

// GetAutoScalingGroupDesiredCapacity returns the AutoScalingGroup's desired capacity
func (c *StackCollection) GetAutoScalingGroupDesiredCapacity(name string) (*int64, error) {
asg, err := c.asgAPI.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{
&name,
},
})
if err != nil {
return nil, fmt.Errorf("couldn't describe ASG: %s", name)
}
if len(asg.AutoScalingGroups) != 1 {
logger.Warning("couldn't find ASG %s", name)
return nil, nil
}

return asg.AutoScalingGroups[0].DesiredCapacity, nil
}

// DescribeNodeGroupStack gets the specified nodegroup stack
Expand Down
13 changes: 13 additions & 0 deletions pkg/cfn/manager/nodegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
cfn "github.com/aws/aws-sdk-go/service/cloudformation"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
Expand Down Expand Up @@ -269,6 +270,17 @@ var _ = Describe("StackCollection NodeGroup", func() {

p.MockCloudFormation().On("DescribeStackResource", mock.Anything).Return(nil, fmt.Errorf("DescribeStackResource failed"))

p.MockASG().On("DescribeAutoScalingGroups", mock.MatchedBy(func(input *autoscaling.DescribeAutoScalingGroupsInput) bool {
return len(input.AutoScalingGroupNames) == 1 && *input.AutoScalingGroupNames[0] == "eksctl-test-cluster-nodegroup-123451-NodeGroup-1N68LL8H1EH27"
})).Return(&autoscaling.DescribeAutoScalingGroupsOutput{
AutoScalingGroups: []*autoscaling.Group{
{
DesiredCapacity: aws.Int64(7),
},
},
}, nil)

p.MockASG().On("DescribeAutoScalingGroups", mock.Anything).Return(nil, fmt.Errorf("DescribeAutoScalingGroups failed"))
})

Context("With no matching stacks", func() {
Expand Down Expand Up @@ -318,6 +330,7 @@ var _ = Describe("StackCollection NodeGroup", func() {
Expect(out).To(HaveLen(1))
Expect(out[0].StackName).To(Equal("eksctl-test-cluster-nodegroup-12345"))
Expect(out[0].NodeInstanceRoleARN).To(Equal("arn:aws:iam::1111:role/eks-nodes-base-role"))
Expect(out[0].DesiredCapacity).To(Equal(7))
})
})
})
Expand Down
3 changes: 3 additions & 0 deletions pkg/testutils/mockprovider/mock_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func (m MockProvider) MockCloudFormation() *mocks.CloudFormationAPI {
// ASG returns a representation of the ASG API
func (m MockProvider) ASG() autoscalingiface.AutoScalingAPI { return m.asg }

// MockASG returns a mocked ASG API
func (m MockProvider) MockASG() *mocks.AutoScalingAPI { return m.ASG().(*mocks.AutoScalingAPI) }

// EKS returns a representation of the EKS API
func (m MockProvider) EKS() eksiface.EKSAPI { return m.eks }

Expand Down

0 comments on commit cf6d0ed

Please sign in to comment.