From 58916cbd76e1697a32b6d980be11577dcca7a5b8 Mon Sep 17 00:00:00 2001 From: Jake Date: Thu, 8 Apr 2021 16:20:35 +0100 Subject: [PATCH] use autoscaling group/EKS nodegroup summary as source of truth for nodegroups scaling config --- pkg/actions/nodegroup/export_test.go | 6 + pkg/actions/nodegroup/get.go | 24 +- pkg/actions/nodegroup/scale.go | 62 +++- pkg/actions/nodegroup/scale_test.go | 146 ++++++-- pkg/cfn/manager/fakes/fake_stack_manager.go | 390 ++++---------------- pkg/cfn/manager/interface.go | 5 +- pkg/cfn/manager/nodegroup.go | 180 ++------- pkg/cfn/manager/nodegroup_test.go | 140 +------ 8 files changed, 308 insertions(+), 645 deletions(-) diff --git a/pkg/actions/nodegroup/export_test.go b/pkg/actions/nodegroup/export_test.go index d6b1f51123d..2e7a5c89825 100644 --- a/pkg/actions/nodegroup/export_test.go +++ b/pkg/actions/nodegroup/export_test.go @@ -1,5 +1,11 @@ package nodegroup +import "github.com/weaveworks/eksctl/pkg/cfn/manager" + func (m *Manager) SetWaiter(wait WaitFunc) { m.wait = wait } + +func (m *Manager) SetStackManager(stackManager manager.StackManager) { + m.stackManager = stackManager +} diff --git a/pkg/actions/nodegroup/get.go b/pkg/actions/nodegroup/get.go index 7bf83fd5e4b..3a5d823592c 100644 --- a/pkg/actions/nodegroup/get.go +++ b/pkg/actions/nodegroup/get.go @@ -10,36 +10,22 @@ import ( ) func (m *Manager) GetAll() ([]*manager.NodeGroupSummary, error) { - summaries, err := m.stackManager.GetNodeGroupSummaries("") + summaries, err := m.stackManager.GetUnmanagedNodeGroupSummaries("") if err != nil { return nil, errors.Wrap(err, "getting nodegroup stack summaries") } - nodeGroups, err := m.ctl.Provider.EKS().ListNodegroups(&eks.ListNodegroupsInput{ + managedNodeGroups, err := m.ctl.Provider.EKS().ListNodegroups(&eks.ListNodegroupsInput{ ClusterName: &m.cfg.Metadata.Name, }) if err != nil { return nil, err } - var nodeGroupsWithoutStacks []string - for _, ng := range nodeGroups.Nodegroups { - found := false - for _, summary := range summaries { - if summary.Name == *ng { - found = true - } - } - - if !found { - nodeGroupsWithoutStacks = append(nodeGroupsWithoutStacks, *ng) - } - } - - for _, nodeGroupWithoutStack := range nodeGroupsWithoutStacks { + for _, managedNodeGroup := range managedNodeGroups.Nodegroups { describeOutput, err := m.ctl.Provider.EKS().DescribeNodegroup(&eks.DescribeNodegroupInput{ ClusterName: &m.cfg.Metadata.Name, - NodegroupName: &nodeGroupWithoutStack, + NodegroupName: managedNodeGroup, }) if err != nil { return nil, err @@ -72,7 +58,7 @@ func (m *Manager) GetAll() ([]*manager.NodeGroupSummary, error) { } func (m *Manager) Get(name string) (*manager.NodeGroupSummary, error) { - summaries, err := m.stackManager.GetNodeGroupSummaries(name) + summaries, err := m.stackManager.GetUnmanagedNodeGroupSummaries(name) if err != nil { return nil, errors.Wrap(err, "getting nodegroup stack summaries") } diff --git a/pkg/actions/nodegroup/scale.go b/pkg/actions/nodegroup/scale.go index 307dca47cc3..5d8d405b034 100644 --- a/pkg/actions/nodegroup/scale.go +++ b/pkg/actions/nodegroup/scale.go @@ -4,7 +4,9 @@ import ( "fmt" "github.com/aws/aws-sdk-go/aws/request" + "github.com/aws/aws-sdk-go/service/autoscaling" "github.com/kris-nova/logger" + "github.com/weaveworks/eksctl/pkg/cfn/manager" "github.com/weaveworks/eksctl/pkg/utils/waiters" "github.com/aws/aws-sdk-go/aws" @@ -16,17 +18,27 @@ import ( func (m *Manager) Scale(ng *api.NodeGroup) error { logger.Info("scaling nodegroup %q in cluster %s", ng.Name, m.cfg.Metadata.Name) - stackManager := m.ctl.NewStackManager(m.cfg) - hasStacks, err := m.hasStacks(ng.Name) + nodegroupStackInfos, err := m.stackManager.DescribeNodeGroupStacksAndResources() if err != nil { return err } - if hasStacks { - err = stackManager.ScaleNodeGroup(ng) + var stackInfo manager.StackInfo + var ok, isUnmanagedNodegroup bool + stackInfo, ok = nodegroupStackInfos[ng.Name] + if ok { + nodegroupType, err := manager.GetNodeGroupType(stackInfo.Stack.Tags) + if err != nil { + return err + } + isUnmanagedNodegroup = nodegroupType == api.NodeGroupTypeUnmanaged + } + + if isUnmanagedNodegroup { + err = m.scaleUnmanagedNodeGroup(ng, stackInfo) } else { - err = m.scale(ng) + err = m.scaleManagedNodeGroup(ng) } if err != nil { @@ -36,7 +48,45 @@ func (m *Manager) Scale(ng *api.NodeGroup) error { return nil } -func (m *Manager) scale(ng *api.NodeGroup) error { +func (m *Manager) scaleUnmanagedNodeGroup(ng *api.NodeGroup, stackInfo manager.StackInfo) error { + asgName := "" + for _, resource := range stackInfo.Resources { + if *resource.LogicalResourceId == "NodeGroup" { + asgName = *resource.PhysicalResourceId + break + } + } + + if asgName == "" { + return fmt.Errorf("failed to find NodeGroup auto scaling group") + } + + input := &autoscaling.UpdateAutoScalingGroupInput{ + AutoScalingGroupName: &asgName, + } + + if ng.MaxSize != nil { + input.MaxSize = aws.Int64(int64(*ng.MaxSize)) + } + + if ng.MinSize != nil { + input.MinSize = aws.Int64(int64(*ng.MinSize)) + } + + if ng.DesiredCapacity != nil { + input.DesiredCapacity = aws.Int64(int64(*ng.DesiredCapacity)) + } + out, err := m.ctl.Provider.ASG().UpdateAutoScalingGroup(input) + if err != nil { + logger.Debug("ASG update output: %s", out.String()) + return err + } + logger.Info("nodegroup successfully scaled") + + return nil +} + +func (m *Manager) scaleManagedNodeGroup(ng *api.NodeGroup) error { scalingConfig := &eks.NodegroupScalingConfig{} if ng.MaxSize != nil { diff --git a/pkg/actions/nodegroup/scale_test.go b/pkg/actions/nodegroup/scale_test.go index 3dce5557510..03f2f1db822 100644 --- a/pkg/actions/nodegroup/scale_test.go +++ b/pkg/actions/nodegroup/scale_test.go @@ -8,42 +8,68 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/request" + "github.com/aws/aws-sdk-go/service/autoscaling" + "github.com/aws/aws-sdk-go/service/cloudformation" awseks "github.com/aws/aws-sdk-go/service/eks" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/weaveworks/eksctl/pkg/actions/nodegroup" api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5" + "github.com/weaveworks/eksctl/pkg/cfn/manager" + "github.com/weaveworks/eksctl/pkg/cfn/manager/fakes" "github.com/weaveworks/eksctl/pkg/eks" "github.com/weaveworks/eksctl/pkg/testutils/mockprovider" ) var _ = Describe("Scale", func() { - When("the nodegroup was not created by eksctl", func() { - var ( - clusterName, ngName string - p *mockprovider.MockProvider - cfg *api.ClusterConfig - ng *api.NodeGroup - manager *nodegroup.Manager - ) + var ( + clusterName, ngName string + p *mockprovider.MockProvider + cfg *api.ClusterConfig + ng *api.NodeGroup + m *nodegroup.Manager + fakeStackManager *fakes.FakeStackManager + ) + BeforeEach(func() { + clusterName = "my-cluster" + ngName = "my-ng" + p = mockprovider.NewMockProvider() + cfg = api.NewClusterConfig() + cfg.Metadata.Name = clusterName + + ng = &api.NodeGroup{ + NodeGroupBase: &api.NodeGroupBase{ + Name: ngName, + ScalingConfig: &api.ScalingConfig{ + MinSize: aws.Int(1), + DesiredCapacity: aws.Int(3), + }, + }, + } + m = nodegroup.New(cfg, &eks.ClusterProvider{Provider: p}, nil) + fakeStackManager = new(fakes.FakeStackManager) + m.SetStackManager(fakeStackManager) + p.MockCloudFormation().On("ListStacksPages", mock.Anything, mock.Anything).Return(nil, nil) + }) + + Describe("Managed NodeGroup", func() { BeforeEach(func() { - clusterName = "my-cluster" - ngName = "my-ng" - p = mockprovider.NewMockProvider() - cfg = api.NewClusterConfig() - cfg.Metadata.Name = clusterName - - ng = &api.NodeGroup{ - NodeGroupBase: &api.NodeGroupBase{ - Name: ngName, - ScalingConfig: &api.ScalingConfig{ - MinSize: aws.Int(1), - DesiredCapacity: aws.Int(3), + nodegroups := make(map[string]manager.StackInfo) + nodegroups["my-ng"] = manager.StackInfo{ + Stack: &manager.Stack{ + Tags: []*cloudformation.Tag{ + { + Key: aws.String(api.NodeGroupNameTag), + Value: aws.String("my-ng"), + }, + { + Key: aws.String(api.NodeGroupTypeTag), + Value: aws.String(string(api.NodeGroupTypeManaged)), + }, }, }, } - manager = nodegroup.New(cfg, &eks.ClusterProvider{Provider: p}, nil) - p.MockCloudFormation().On("ListStacksPages", mock.Anything, mock.Anything).Return(nil, nil) + fakeStackManager.DescribeNodeGroupStacksAndResourcesReturns(nodegroups, nil) }) It("scales the nodegroup using the values provided", func() { @@ -62,18 +88,18 @@ var _ = Describe("Scale", func() { }).Return(&request.Request{}, nil) waitCallCount := 0 - manager.SetWaiter(func(name, msg string, acceptors []request.WaiterAcceptor, newRequest func() *request.Request, waitTimeout time.Duration, troubleshoot func(string) error) error { + m.SetWaiter(func(name, msg string, acceptors []request.WaiterAcceptor, newRequest func() *request.Request, waitTimeout time.Duration, troubleshoot func(string) error) error { waitCallCount++ return nil }) - err := manager.Scale(ng) + err := m.Scale(ng) Expect(err).NotTo(HaveOccurred()) Expect(waitCallCount).To(Equal(1)) }) - When("upgrade fails", func() { + When("update fails", func() { It("returns an error", func() { p.MockEKS().On("UpdateNodegroupConfig", &awseks.UpdateNodegroupConfigInput{ ScalingConfig: &awseks.NodegroupScalingConfig{ @@ -84,11 +110,79 @@ var _ = Describe("Scale", func() { NodegroupName: &ngName, }).Return(nil, fmt.Errorf("foo")) - err := manager.Scale(ng) + err := m.Scale(ng) Expect(err).To(MatchError(fmt.Sprintf("failed to scale nodegroup for cluster %q, error: foo", clusterName))) }) }) }) + Describe("Unmanaged Nodegroup", func() { + When("the ASG exists", func() { + BeforeEach(func() { + nodegroups := make(map[string]manager.StackInfo) + nodegroups["my-ng"] = manager.StackInfo{ + Stack: &manager.Stack{ + Tags: []*cloudformation.Tag{ + { + Key: aws.String(api.NodeGroupNameTag), + Value: aws.String("my-ng"), + }, + { + Key: aws.String(api.NodeGroupTypeTag), + Value: aws.String(string(api.NodeGroupTypeUnmanaged)), + }, + }, + }, + Resources: []*cloudformation.StackResource{ + { + PhysicalResourceId: aws.String("asg-name"), + LogicalResourceId: aws.String("NodeGroup"), + }, + }, + } + fakeStackManager.DescribeNodeGroupStacksAndResourcesReturns(nodegroups, nil) + + p.MockASG().On("UpdateAutoScalingGroup", &autoscaling.UpdateAutoScalingGroupInput{ + AutoScalingGroupName: aws.String("asg-name"), + MinSize: aws.Int64(1), + DesiredCapacity: aws.Int64(3), + }).Return(nil, nil) + + }) + + It("scales the nodegroup", func() { + err := m.Scale(ng) + Expect(err).NotTo(HaveOccurred()) + }) + }) + + When("the asg resource doesn't exist", func() { + BeforeEach(func() { + nodegroups := make(map[string]manager.StackInfo) + nodegroups["my-ng"] = manager.StackInfo{ + Stack: &manager.Stack{ + Tags: []*cloudformation.Tag{ + { + Key: aws.String(api.NodeGroupNameTag), + Value: aws.String("my-ng"), + }, + { + Key: aws.String(api.NodeGroupTypeTag), + Value: aws.String(string(api.NodeGroupTypeUnmanaged)), + }, + }, + }, + Resources: []*cloudformation.StackResource{}, + } + fakeStackManager.DescribeNodeGroupStacksAndResourcesReturns(nodegroups, nil) + + }) + + It("returns an error", func() { + err := m.Scale(ng) + Expect(err).To(MatchError(ContainSubstring("failed to find NodeGroup auto scaling group"))) + }) + }) + }) }) diff --git a/pkg/cfn/manager/fakes/fake_stack_manager.go b/pkg/cfn/manager/fakes/fake_stack_manager.go index 6bbd7abebe6..f69fabd9afe 100644 --- a/pkg/cfn/manager/fakes/fake_stack_manager.go +++ b/pkg/cfn/manager/fakes/fake_stack_manager.go @@ -327,19 +327,6 @@ type FakeStackManager struct { result1 []*v1alpha5.ClusterIAMServiceAccount result2 error } - GetManagedNodeGroupAutoScalingGroupNameStub func(*cloudformation.Stack) (string, error) - getManagedNodeGroupAutoScalingGroupNameMutex sync.RWMutex - getManagedNodeGroupAutoScalingGroupNameArgsForCall []struct { - arg1 *cloudformation.Stack - } - getManagedNodeGroupAutoScalingGroupNameReturns struct { - result1 string - result2 error - } - getManagedNodeGroupAutoScalingGroupNameReturnsOnCall map[int]struct { - result1 string - result2 error - } GetManagedNodeGroupTemplateStub func(string) (string, error) getManagedNodeGroupTemplateMutex sync.RWMutex getManagedNodeGroupTemplateArgsForCall []struct { @@ -353,19 +340,6 @@ type FakeStackManager struct { result1 string result2 error } - GetNodeGroupAutoScalingGroupNameStub func(*cloudformation.Stack) (string, error) - getNodeGroupAutoScalingGroupNameMutex sync.RWMutex - getNodeGroupAutoScalingGroupNameArgsForCall []struct { - arg1 *cloudformation.Stack - } - getNodeGroupAutoScalingGroupNameReturns struct { - result1 string - result2 error - } - getNodeGroupAutoScalingGroupNameReturnsOnCall map[int]struct { - result1 string - result2 error - } GetNodeGroupNameStub func(*cloudformation.Stack) string getNodeGroupNameMutex sync.RWMutex getNodeGroupNameArgsForCall []struct { @@ -390,19 +364,6 @@ type FakeStackManager struct { result1 v1alpha5.NodeGroupType result2 error } - GetNodeGroupSummariesStub func(string) ([]*manager.NodeGroupSummary, error) - getNodeGroupSummariesMutex sync.RWMutex - getNodeGroupSummariesArgsForCall []struct { - arg1 string - } - getNodeGroupSummariesReturns struct { - result1 []*manager.NodeGroupSummary - result2 error - } - getNodeGroupSummariesReturnsOnCall map[int]struct { - result1 []*manager.NodeGroupSummary - result2 error - } GetStackTemplateStub func(string) (string, error) getStackTemplateMutex sync.RWMutex getStackTemplateArgsForCall []struct { @@ -416,6 +377,19 @@ type FakeStackManager struct { result1 string result2 error } + GetUnmanagedNodeGroupSummariesStub func(string) ([]*manager.NodeGroupSummary, error) + getUnmanagedNodeGroupSummariesMutex sync.RWMutex + getUnmanagedNodeGroupSummariesArgsForCall []struct { + arg1 string + } + getUnmanagedNodeGroupSummariesReturns struct { + result1 []*manager.NodeGroupSummary + result2 error + } + getUnmanagedNodeGroupSummariesReturnsOnCall map[int]struct { + result1 []*manager.NodeGroupSummary + result2 error + } HasClusterStackStub func() (bool, error) hasClusterStackMutex sync.RWMutex hasClusterStackArgsForCall []struct { @@ -700,17 +674,6 @@ type FakeStackManager struct { refreshFargatePodExecutionRoleARNReturnsOnCall map[int]struct { result1 error } - ScaleNodeGroupStub func(*v1alpha5.NodeGroup) error - scaleNodeGroupMutex sync.RWMutex - scaleNodeGroupArgsForCall []struct { - arg1 *v1alpha5.NodeGroup - } - scaleNodeGroupReturns struct { - result1 error - } - scaleNodeGroupReturnsOnCall map[int]struct { - result1 error - } StackStatusIsNotReadyStub func(*cloudformation.Stack) bool stackStatusIsNotReadyMutex sync.RWMutex stackStatusIsNotReadyArgsForCall []struct { @@ -2264,70 +2227,6 @@ func (fake *FakeStackManager) GetIAMServiceAccountsReturnsOnCall(i int, result1 }{result1, result2} } -func (fake *FakeStackManager) GetManagedNodeGroupAutoScalingGroupName(arg1 *cloudformation.Stack) (string, error) { - fake.getManagedNodeGroupAutoScalingGroupNameMutex.Lock() - ret, specificReturn := fake.getManagedNodeGroupAutoScalingGroupNameReturnsOnCall[len(fake.getManagedNodeGroupAutoScalingGroupNameArgsForCall)] - fake.getManagedNodeGroupAutoScalingGroupNameArgsForCall = append(fake.getManagedNodeGroupAutoScalingGroupNameArgsForCall, struct { - arg1 *cloudformation.Stack - }{arg1}) - stub := fake.GetManagedNodeGroupAutoScalingGroupNameStub - fakeReturns := fake.getManagedNodeGroupAutoScalingGroupNameReturns - fake.recordInvocation("GetManagedNodeGroupAutoScalingGroupName", []interface{}{arg1}) - fake.getManagedNodeGroupAutoScalingGroupNameMutex.Unlock() - if stub != nil { - return stub(arg1) - } - if specificReturn { - return ret.result1, ret.result2 - } - return fakeReturns.result1, fakeReturns.result2 -} - -func (fake *FakeStackManager) GetManagedNodeGroupAutoScalingGroupNameCallCount() int { - fake.getManagedNodeGroupAutoScalingGroupNameMutex.RLock() - defer fake.getManagedNodeGroupAutoScalingGroupNameMutex.RUnlock() - return len(fake.getManagedNodeGroupAutoScalingGroupNameArgsForCall) -} - -func (fake *FakeStackManager) GetManagedNodeGroupAutoScalingGroupNameCalls(stub func(*cloudformation.Stack) (string, error)) { - fake.getManagedNodeGroupAutoScalingGroupNameMutex.Lock() - defer fake.getManagedNodeGroupAutoScalingGroupNameMutex.Unlock() - fake.GetManagedNodeGroupAutoScalingGroupNameStub = stub -} - -func (fake *FakeStackManager) GetManagedNodeGroupAutoScalingGroupNameArgsForCall(i int) *cloudformation.Stack { - fake.getManagedNodeGroupAutoScalingGroupNameMutex.RLock() - defer fake.getManagedNodeGroupAutoScalingGroupNameMutex.RUnlock() - argsForCall := fake.getManagedNodeGroupAutoScalingGroupNameArgsForCall[i] - return argsForCall.arg1 -} - -func (fake *FakeStackManager) GetManagedNodeGroupAutoScalingGroupNameReturns(result1 string, result2 error) { - fake.getManagedNodeGroupAutoScalingGroupNameMutex.Lock() - defer fake.getManagedNodeGroupAutoScalingGroupNameMutex.Unlock() - fake.GetManagedNodeGroupAutoScalingGroupNameStub = nil - fake.getManagedNodeGroupAutoScalingGroupNameReturns = struct { - result1 string - result2 error - }{result1, result2} -} - -func (fake *FakeStackManager) GetManagedNodeGroupAutoScalingGroupNameReturnsOnCall(i int, result1 string, result2 error) { - fake.getManagedNodeGroupAutoScalingGroupNameMutex.Lock() - defer fake.getManagedNodeGroupAutoScalingGroupNameMutex.Unlock() - fake.GetManagedNodeGroupAutoScalingGroupNameStub = nil - if fake.getManagedNodeGroupAutoScalingGroupNameReturnsOnCall == nil { - fake.getManagedNodeGroupAutoScalingGroupNameReturnsOnCall = make(map[int]struct { - result1 string - result2 error - }) - } - fake.getManagedNodeGroupAutoScalingGroupNameReturnsOnCall[i] = struct { - result1 string - result2 error - }{result1, result2} -} - func (fake *FakeStackManager) GetManagedNodeGroupTemplate(arg1 string) (string, error) { fake.getManagedNodeGroupTemplateMutex.Lock() ret, specificReturn := fake.getManagedNodeGroupTemplateReturnsOnCall[len(fake.getManagedNodeGroupTemplateArgsForCall)] @@ -2392,70 +2291,6 @@ func (fake *FakeStackManager) GetManagedNodeGroupTemplateReturnsOnCall(i int, re }{result1, result2} } -func (fake *FakeStackManager) GetNodeGroupAutoScalingGroupName(arg1 *cloudformation.Stack) (string, error) { - fake.getNodeGroupAutoScalingGroupNameMutex.Lock() - ret, specificReturn := fake.getNodeGroupAutoScalingGroupNameReturnsOnCall[len(fake.getNodeGroupAutoScalingGroupNameArgsForCall)] - fake.getNodeGroupAutoScalingGroupNameArgsForCall = append(fake.getNodeGroupAutoScalingGroupNameArgsForCall, struct { - arg1 *cloudformation.Stack - }{arg1}) - stub := fake.GetNodeGroupAutoScalingGroupNameStub - fakeReturns := fake.getNodeGroupAutoScalingGroupNameReturns - fake.recordInvocation("GetNodeGroupAutoScalingGroupName", []interface{}{arg1}) - fake.getNodeGroupAutoScalingGroupNameMutex.Unlock() - if stub != nil { - return stub(arg1) - } - if specificReturn { - return ret.result1, ret.result2 - } - return fakeReturns.result1, fakeReturns.result2 -} - -func (fake *FakeStackManager) GetNodeGroupAutoScalingGroupNameCallCount() int { - fake.getNodeGroupAutoScalingGroupNameMutex.RLock() - defer fake.getNodeGroupAutoScalingGroupNameMutex.RUnlock() - return len(fake.getNodeGroupAutoScalingGroupNameArgsForCall) -} - -func (fake *FakeStackManager) GetNodeGroupAutoScalingGroupNameCalls(stub func(*cloudformation.Stack) (string, error)) { - fake.getNodeGroupAutoScalingGroupNameMutex.Lock() - defer fake.getNodeGroupAutoScalingGroupNameMutex.Unlock() - fake.GetNodeGroupAutoScalingGroupNameStub = stub -} - -func (fake *FakeStackManager) GetNodeGroupAutoScalingGroupNameArgsForCall(i int) *cloudformation.Stack { - fake.getNodeGroupAutoScalingGroupNameMutex.RLock() - defer fake.getNodeGroupAutoScalingGroupNameMutex.RUnlock() - argsForCall := fake.getNodeGroupAutoScalingGroupNameArgsForCall[i] - return argsForCall.arg1 -} - -func (fake *FakeStackManager) GetNodeGroupAutoScalingGroupNameReturns(result1 string, result2 error) { - fake.getNodeGroupAutoScalingGroupNameMutex.Lock() - defer fake.getNodeGroupAutoScalingGroupNameMutex.Unlock() - fake.GetNodeGroupAutoScalingGroupNameStub = nil - fake.getNodeGroupAutoScalingGroupNameReturns = struct { - result1 string - result2 error - }{result1, result2} -} - -func (fake *FakeStackManager) GetNodeGroupAutoScalingGroupNameReturnsOnCall(i int, result1 string, result2 error) { - fake.getNodeGroupAutoScalingGroupNameMutex.Lock() - defer fake.getNodeGroupAutoScalingGroupNameMutex.Unlock() - fake.GetNodeGroupAutoScalingGroupNameStub = nil - if fake.getNodeGroupAutoScalingGroupNameReturnsOnCall == nil { - fake.getNodeGroupAutoScalingGroupNameReturnsOnCall = make(map[int]struct { - result1 string - result2 error - }) - } - fake.getNodeGroupAutoScalingGroupNameReturnsOnCall[i] = struct { - result1 string - result2 error - }{result1, result2} -} - func (fake *FakeStackManager) GetNodeGroupName(arg1 *cloudformation.Stack) string { fake.getNodeGroupNameMutex.Lock() ret, specificReturn := fake.getNodeGroupNameReturnsOnCall[len(fake.getNodeGroupNameArgsForCall)] @@ -2581,70 +2416,6 @@ func (fake *FakeStackManager) GetNodeGroupStackTypeReturnsOnCall(i int, result1 }{result1, result2} } -func (fake *FakeStackManager) GetNodeGroupSummaries(arg1 string) ([]*manager.NodeGroupSummary, error) { - fake.getNodeGroupSummariesMutex.Lock() - ret, specificReturn := fake.getNodeGroupSummariesReturnsOnCall[len(fake.getNodeGroupSummariesArgsForCall)] - fake.getNodeGroupSummariesArgsForCall = append(fake.getNodeGroupSummariesArgsForCall, struct { - arg1 string - }{arg1}) - stub := fake.GetNodeGroupSummariesStub - fakeReturns := fake.getNodeGroupSummariesReturns - fake.recordInvocation("GetNodeGroupSummaries", []interface{}{arg1}) - fake.getNodeGroupSummariesMutex.Unlock() - if stub != nil { - return stub(arg1) - } - if specificReturn { - return ret.result1, ret.result2 - } - return fakeReturns.result1, fakeReturns.result2 -} - -func (fake *FakeStackManager) GetNodeGroupSummariesCallCount() int { - fake.getNodeGroupSummariesMutex.RLock() - defer fake.getNodeGroupSummariesMutex.RUnlock() - return len(fake.getNodeGroupSummariesArgsForCall) -} - -func (fake *FakeStackManager) GetNodeGroupSummariesCalls(stub func(string) ([]*manager.NodeGroupSummary, error)) { - fake.getNodeGroupSummariesMutex.Lock() - defer fake.getNodeGroupSummariesMutex.Unlock() - fake.GetNodeGroupSummariesStub = stub -} - -func (fake *FakeStackManager) GetNodeGroupSummariesArgsForCall(i int) string { - fake.getNodeGroupSummariesMutex.RLock() - defer fake.getNodeGroupSummariesMutex.RUnlock() - argsForCall := fake.getNodeGroupSummariesArgsForCall[i] - return argsForCall.arg1 -} - -func (fake *FakeStackManager) GetNodeGroupSummariesReturns(result1 []*manager.NodeGroupSummary, result2 error) { - fake.getNodeGroupSummariesMutex.Lock() - defer fake.getNodeGroupSummariesMutex.Unlock() - fake.GetNodeGroupSummariesStub = nil - fake.getNodeGroupSummariesReturns = struct { - result1 []*manager.NodeGroupSummary - result2 error - }{result1, result2} -} - -func (fake *FakeStackManager) GetNodeGroupSummariesReturnsOnCall(i int, result1 []*manager.NodeGroupSummary, result2 error) { - fake.getNodeGroupSummariesMutex.Lock() - defer fake.getNodeGroupSummariesMutex.Unlock() - fake.GetNodeGroupSummariesStub = nil - if fake.getNodeGroupSummariesReturnsOnCall == nil { - fake.getNodeGroupSummariesReturnsOnCall = make(map[int]struct { - result1 []*manager.NodeGroupSummary - result2 error - }) - } - fake.getNodeGroupSummariesReturnsOnCall[i] = struct { - result1 []*manager.NodeGroupSummary - result2 error - }{result1, result2} -} - func (fake *FakeStackManager) GetStackTemplate(arg1 string) (string, error) { fake.getStackTemplateMutex.Lock() ret, specificReturn := fake.getStackTemplateReturnsOnCall[len(fake.getStackTemplateArgsForCall)] @@ -2709,6 +2480,70 @@ func (fake *FakeStackManager) GetStackTemplateReturnsOnCall(i int, result1 strin }{result1, result2} } +func (fake *FakeStackManager) GetUnmanagedNodeGroupSummaries(arg1 string) ([]*manager.NodeGroupSummary, error) { + fake.getUnmanagedNodeGroupSummariesMutex.Lock() + ret, specificReturn := fake.getUnmanagedNodeGroupSummariesReturnsOnCall[len(fake.getUnmanagedNodeGroupSummariesArgsForCall)] + fake.getUnmanagedNodeGroupSummariesArgsForCall = append(fake.getUnmanagedNodeGroupSummariesArgsForCall, struct { + arg1 string + }{arg1}) + stub := fake.GetUnmanagedNodeGroupSummariesStub + fakeReturns := fake.getUnmanagedNodeGroupSummariesReturns + fake.recordInvocation("GetUnmanagedNodeGroupSummaries", []interface{}{arg1}) + fake.getUnmanagedNodeGroupSummariesMutex.Unlock() + if stub != nil { + return stub(arg1) + } + if specificReturn { + return ret.result1, ret.result2 + } + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *FakeStackManager) GetUnmanagedNodeGroupSummariesCallCount() int { + fake.getUnmanagedNodeGroupSummariesMutex.RLock() + defer fake.getUnmanagedNodeGroupSummariesMutex.RUnlock() + return len(fake.getUnmanagedNodeGroupSummariesArgsForCall) +} + +func (fake *FakeStackManager) GetUnmanagedNodeGroupSummariesCalls(stub func(string) ([]*manager.NodeGroupSummary, error)) { + fake.getUnmanagedNodeGroupSummariesMutex.Lock() + defer fake.getUnmanagedNodeGroupSummariesMutex.Unlock() + fake.GetUnmanagedNodeGroupSummariesStub = stub +} + +func (fake *FakeStackManager) GetUnmanagedNodeGroupSummariesArgsForCall(i int) string { + fake.getUnmanagedNodeGroupSummariesMutex.RLock() + defer fake.getUnmanagedNodeGroupSummariesMutex.RUnlock() + argsForCall := fake.getUnmanagedNodeGroupSummariesArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *FakeStackManager) GetUnmanagedNodeGroupSummariesReturns(result1 []*manager.NodeGroupSummary, result2 error) { + fake.getUnmanagedNodeGroupSummariesMutex.Lock() + defer fake.getUnmanagedNodeGroupSummariesMutex.Unlock() + fake.GetUnmanagedNodeGroupSummariesStub = nil + fake.getUnmanagedNodeGroupSummariesReturns = struct { + result1 []*manager.NodeGroupSummary + result2 error + }{result1, result2} +} + +func (fake *FakeStackManager) GetUnmanagedNodeGroupSummariesReturnsOnCall(i int, result1 []*manager.NodeGroupSummary, result2 error) { + fake.getUnmanagedNodeGroupSummariesMutex.Lock() + defer fake.getUnmanagedNodeGroupSummariesMutex.Unlock() + fake.GetUnmanagedNodeGroupSummariesStub = nil + if fake.getUnmanagedNodeGroupSummariesReturnsOnCall == nil { + fake.getUnmanagedNodeGroupSummariesReturnsOnCall = make(map[int]struct { + result1 []*manager.NodeGroupSummary + result2 error + }) + } + fake.getUnmanagedNodeGroupSummariesReturnsOnCall[i] = struct { + result1 []*manager.NodeGroupSummary + result2 error + }{result1, result2} +} + func (fake *FakeStackManager) HasClusterStack() (bool, error) { fake.hasClusterStackMutex.Lock() ret, specificReturn := fake.hasClusterStackReturnsOnCall[len(fake.hasClusterStackArgsForCall)] @@ -4092,67 +3927,6 @@ func (fake *FakeStackManager) RefreshFargatePodExecutionRoleARNReturnsOnCall(i i }{result1} } -func (fake *FakeStackManager) ScaleNodeGroup(arg1 *v1alpha5.NodeGroup) error { - fake.scaleNodeGroupMutex.Lock() - ret, specificReturn := fake.scaleNodeGroupReturnsOnCall[len(fake.scaleNodeGroupArgsForCall)] - fake.scaleNodeGroupArgsForCall = append(fake.scaleNodeGroupArgsForCall, struct { - arg1 *v1alpha5.NodeGroup - }{arg1}) - stub := fake.ScaleNodeGroupStub - fakeReturns := fake.scaleNodeGroupReturns - fake.recordInvocation("ScaleNodeGroup", []interface{}{arg1}) - fake.scaleNodeGroupMutex.Unlock() - if stub != nil { - return stub(arg1) - } - if specificReturn { - return ret.result1 - } - return fakeReturns.result1 -} - -func (fake *FakeStackManager) ScaleNodeGroupCallCount() int { - fake.scaleNodeGroupMutex.RLock() - defer fake.scaleNodeGroupMutex.RUnlock() - return len(fake.scaleNodeGroupArgsForCall) -} - -func (fake *FakeStackManager) ScaleNodeGroupCalls(stub func(*v1alpha5.NodeGroup) error) { - fake.scaleNodeGroupMutex.Lock() - defer fake.scaleNodeGroupMutex.Unlock() - fake.ScaleNodeGroupStub = stub -} - -func (fake *FakeStackManager) ScaleNodeGroupArgsForCall(i int) *v1alpha5.NodeGroup { - fake.scaleNodeGroupMutex.RLock() - defer fake.scaleNodeGroupMutex.RUnlock() - argsForCall := fake.scaleNodeGroupArgsForCall[i] - return argsForCall.arg1 -} - -func (fake *FakeStackManager) ScaleNodeGroupReturns(result1 error) { - fake.scaleNodeGroupMutex.Lock() - defer fake.scaleNodeGroupMutex.Unlock() - fake.ScaleNodeGroupStub = nil - fake.scaleNodeGroupReturns = struct { - result1 error - }{result1} -} - -func (fake *FakeStackManager) ScaleNodeGroupReturnsOnCall(i int, result1 error) { - fake.scaleNodeGroupMutex.Lock() - defer fake.scaleNodeGroupMutex.Unlock() - fake.ScaleNodeGroupStub = nil - if fake.scaleNodeGroupReturnsOnCall == nil { - fake.scaleNodeGroupReturnsOnCall = make(map[int]struct { - result1 error - }) - } - fake.scaleNodeGroupReturnsOnCall[i] = struct { - result1 error - }{result1} -} - func (fake *FakeStackManager) StackStatusIsNotReady(arg1 *cloudformation.Stack) bool { fake.stackStatusIsNotReadyMutex.Lock() ret, specificReturn := fake.stackStatusIsNotReadyReturnsOnCall[len(fake.stackStatusIsNotReadyArgsForCall)] @@ -4455,20 +4229,16 @@ func (fake *FakeStackManager) Invocations() map[string][][]interface{} { defer fake.getIAMAddonsStacksMutex.RUnlock() fake.getIAMServiceAccountsMutex.RLock() defer fake.getIAMServiceAccountsMutex.RUnlock() - fake.getManagedNodeGroupAutoScalingGroupNameMutex.RLock() - defer fake.getManagedNodeGroupAutoScalingGroupNameMutex.RUnlock() fake.getManagedNodeGroupTemplateMutex.RLock() defer fake.getManagedNodeGroupTemplateMutex.RUnlock() - fake.getNodeGroupAutoScalingGroupNameMutex.RLock() - defer fake.getNodeGroupAutoScalingGroupNameMutex.RUnlock() fake.getNodeGroupNameMutex.RLock() defer fake.getNodeGroupNameMutex.RUnlock() fake.getNodeGroupStackTypeMutex.RLock() defer fake.getNodeGroupStackTypeMutex.RUnlock() - fake.getNodeGroupSummariesMutex.RLock() - defer fake.getNodeGroupSummariesMutex.RUnlock() fake.getStackTemplateMutex.RLock() defer fake.getStackTemplateMutex.RUnlock() + fake.getUnmanagedNodeGroupSummariesMutex.RLock() + defer fake.getUnmanagedNodeGroupSummariesMutex.RUnlock() fake.hasClusterStackMutex.RLock() defer fake.hasClusterStackMutex.RUnlock() fake.hasClusterStackUsingCachedListMutex.RLock() @@ -4513,8 +4283,6 @@ func (fake *FakeStackManager) Invocations() map[string][][]interface{} { defer fake.newUnmanagedNodeGroupTaskMutex.RUnlock() fake.refreshFargatePodExecutionRoleARNMutex.RLock() defer fake.refreshFargatePodExecutionRoleARNMutex.RUnlock() - fake.scaleNodeGroupMutex.RLock() - defer fake.scaleNodeGroupMutex.RUnlock() fake.stackStatusIsNotReadyMutex.RLock() defer fake.stackStatusIsNotReadyMutex.RUnlock() fake.stackStatusIsNotTransitionalMutex.RLock() diff --git a/pkg/cfn/manager/interface.go b/pkg/cfn/manager/interface.go index 4d6524b1d75..efdc93f6fcf 100644 --- a/pkg/cfn/manager/interface.go +++ b/pkg/cfn/manager/interface.go @@ -16,10 +16,7 @@ import ( type StackManager interface { ListNodeGroupStacks() ([]NodeGroupStack, error) DescribeNodeGroupStacksAndResources() (map[string]StackInfo, error) - ScaleNodeGroup(ng *v1alpha5.NodeGroup) error - GetNodeGroupSummaries(name string) ([]*NodeGroupSummary, error) - GetNodeGroupAutoScalingGroupName(s *Stack) (string, error) - GetManagedNodeGroupAutoScalingGroupName(s *Stack) (string, error) + GetUnmanagedNodeGroupSummaries(name string) ([]*NodeGroupSummary, error) DescribeNodeGroupStack(nodeGroupName string) (*Stack, error) DescribeNodeGroupStacks() ([]*Stack, error) GetNodeGroupStackType(name string) (v1alpha5.NodeGroupType, error) diff --git a/pkg/cfn/manager/nodegroup.go b/pkg/cfn/manager/nodegroup.go index dccbc79ab9d..7fc07cd0517 100644 --- a/pkg/cfn/manager/nodegroup.go +++ b/pkg/cfn/manager/nodegroup.go @@ -1,7 +1,6 @@ package manager import ( - "bytes" "fmt" "strings" "time" @@ -14,7 +13,6 @@ import ( "github.com/kris-nova/logger" "github.com/pkg/errors" "github.com/tidwall/gjson" - "github.com/tidwall/sjson" api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5" "github.com/weaveworks/eksctl/pkg/cfn/builder" @@ -162,117 +160,8 @@ func (c *StackCollection) DescribeNodeGroupStacksAndResources() (map[string]Stac return allResources, nil } -// ScaleNodeGroup will scale an existing nodegroup -func (c *StackCollection) ScaleNodeGroup(ng *api.NodeGroup) error { - template, description, err := c.ScaleNodeGroupTemplate(ng) - if err != nil { - return nil - } - - if template == "" { - return nil - } - - return c.UpdateStack(c.makeNodeGroupStackName(ng.Name), c.MakeChangeSetName("scale-nodegroup"), description, TemplateBody(template), nil) -} - -func (c *StackCollection) ScaleNodeGroupTemplate(ng *api.NodeGroup) (string, string, error) { - clusterName := c.MakeClusterStackName() - c.spec.Status = &api.ClusterStatus{StackName: clusterName} - name := c.makeNodeGroupStackName(ng.Name) - - stack, err := c.DescribeStack(&Stack{StackName: &name}) - if err != nil { - return "", "", errors.Wrapf(err, "error describing nodegroup stack %s", name) - } - - // Get current stack - template, err := c.GetStackTemplate(name) - if err != nil { - return "", "", errors.Wrapf(err, "error getting stack template %s", name) - } - logger.Debug("stack template (pre-scale change): %s", template) - - var descriptionBuffer bytes.Buffer - descriptionBuffer.WriteString("scaling nodegroup") - - ngPaths, err := getNodeGroupPaths(stack.Tags) - if err != nil { - return "", "", err - } - var ( - desiredCapacityPath = ngPaths.DesiredCapacity - maxSizePath = ngPaths.MaxSize - minSizePath = ngPaths.MinSize - ) - - // TODO rewrite this using types - // Get the current values - currentCapacity := gjson.Get(template, desiredCapacityPath).Int() - currentMaxSize := gjson.Get(template, maxSizePath).Int() - currentMinSize := gjson.Get(template, minSizePath).Int() - - desiredCapacity := currentCapacity - if ng.DesiredCapacity != nil { - desiredCapacity = int64(*ng.DesiredCapacity) - } - - desiredMinSize := currentMinSize - if ng.MinSize != nil { - desiredMinSize = int64(*ng.MinSize) - } - - desiredMaxSize := currentMaxSize - if ng.MaxSize != nil { - desiredMaxSize = int64(*ng.MaxSize) - } - - if desiredCapacity == currentCapacity && desiredMinSize == currentMinSize && desiredMaxSize == currentMaxSize { - logger.Info("no change for nodegroup %q in cluster %q: nodes-min %d, desired %d, nodes-max %d", ng.Name, - clusterName, currentMinSize, desiredCapacity, currentMaxSize) - return "", "", nil - } - - if desiredCapacity < desiredMinSize { - logger.Warning("the desired nodes %d is less than the nodes-min/minSize %d", desiredCapacity, desiredMinSize) - return "", "", errors.Errorf("the desired nodes %d is less than the nodes-min/minSize %d", desiredCapacity, desiredMinSize) - } - - if desiredCapacity > desiredMaxSize { - logger.Warning("the desired nodes %d is greater than the nodes-max/maxSize %d", desiredCapacity, desiredMaxSize) - return "", "", errors.Errorf("the desired nodes %d is greater than the nodes-max/maxSize %d", desiredCapacity, desiredMaxSize) - } - - // Set the new values - updateField := func(path, fieldName string, newVal, oldVal int64) error { - if newVal == oldVal { - return nil - } - template, err = sjson.Set(template, path, fmt.Sprintf("%d", newVal)) - if err != nil { - return errors.Wrapf(err, "error setting %s", fieldName) - } - descriptionBuffer.WriteString(fmt.Sprintf(", %s from %d to %d", fieldName, oldVal, newVal)) - return nil - } - - if err := updateField(desiredCapacityPath, "desired capacity", desiredCapacity, currentCapacity); err != nil { - return "", "", err - } - - if err := updateField(minSizePath, "min size", desiredMinSize, currentMinSize); err != nil { - return "", "", err - } - - if err := updateField(maxSizePath, "max size", desiredMaxSize, currentMaxSize); err != nil { - return "", "", err - } - logger.Debug("stack template (post-scale change): %s", template) - return template, descriptionBuffer.String(), nil -} - -// GetNodeGroupSummaries returns a list of summaries for the nodegroups of a cluster -func (c *StackCollection) GetNodeGroupSummaries(name string) ([]*NodeGroupSummary, error) { +// GetUnmanagedNodeGroupSummaries returns a list of summaries for the unmanaged nodegroups of a cluster +func (c *StackCollection) GetUnmanagedNodeGroupSummaries(name string) ([]*NodeGroupSummary, error) { stacks, err := c.DescribeNodeGroupStacks() if err != nil { return nil, errors.Wrap(err, "getting nodegroup stacks") @@ -281,36 +170,43 @@ func (c *StackCollection) GetNodeGroupSummaries(name string) ([]*NodeGroupSummar // Create an empty array here so that an object is returned rather than null summaries := []*NodeGroupSummary{} for _, s := range stacks { - ngPaths, err := getNodeGroupPaths(s.Tags) + nodeGroupType, err := GetNodeGroupType(s.Tags) if err != nil { return nil, err } - summary, err := c.mapStackToNodeGroupSummary(s, ngPaths) + if nodeGroupType == api.NodeGroupTypeUnmanaged { + ngPaths, err := getNodeGroupPaths(s.Tags) + if err != nil { + return nil, err + } - if err != nil { - return nil, errors.Wrap(err, "mapping stack to nodegroup summary") - } + summary, err := c.mapStackToNodeGroupSummary(s, ngPaths) - asgName, err := c.GetAutoScalingGroupName(s) - if err != nil { - return nil, errors.Wrap(err, "getting autoscalinggroupname") - } + if err != nil { + return nil, errors.Wrap(err, "mapping stack to nodegroup summary") + } - summary.AutoScalingGroupName = asgName + asgName, err := c.getUnmanagedNodeGroupAutoScalingGroupName(s) + if err != nil { + return nil, errors.Wrap(err, "getting autoscalinggroupname") + } - asgDesiredCapacity, err := c.GetAutoScalingGroupDesiredCapacity(asgName) - if err != nil { - return nil, errors.Wrap(err, "getting autoscalinggroup desired capacity") - } - if asgDesiredCapacity != nil { - summary.DesiredCapacity = int(*asgDesiredCapacity) - } + summary.AutoScalingGroupName = asgName - if name == "" { - summaries = append(summaries, summary) - } else if summary.Name == name { - summaries = append(summaries, summary) + scalingGroup, err := c.GetAutoScalingGroupDesiredCapacity(asgName) + if err != nil { + return nil, errors.Wrap(err, "getting autoscalinggroup desired capacity") + } + summary.DesiredCapacity = int(*scalingGroup.DesiredCapacity) + summary.MinSize = int(*scalingGroup.MinSize) + summary.MaxSize = int(*scalingGroup.MaxSize) + + if name == "" { + summaries = append(summaries, summary) + } else if summary.Name == name { + summaries = append(summaries, summary) + } } } @@ -326,13 +222,13 @@ func (c *StackCollection) GetAutoScalingGroupName(s *Stack) (string, error) { switch nodeGroupType { case api.NodeGroupTypeManaged: - res, err := c.GetManagedNodeGroupAutoScalingGroupName(s) + res, err := c.getManagedNodeGroupAutoScalingGroupName(s) if err != nil { return "", err } return res, nil case api.NodeGroupTypeUnmanaged, "": - res, err := c.GetNodeGroupAutoScalingGroupName(s) + res, err := c.getUnmanagedNodeGroupAutoScalingGroupName(s) if err != nil { return "", err } @@ -344,7 +240,7 @@ func (c *StackCollection) GetAutoScalingGroupName(s *Stack) (string, error) { } // GetNodeGroupAutoScalingGroupName return the unmanaged nodegroup's AutoScalingGroupName -func (c *StackCollection) GetNodeGroupAutoScalingGroupName(s *Stack) (string, error) { +func (c *StackCollection) getUnmanagedNodeGroupAutoScalingGroupName(s *Stack) (string, error) { input := &cfn.DescribeStackResourceInput{ StackName: s.StackName, LogicalResourceId: aws.String("NodeGroup"), @@ -359,7 +255,7 @@ func (c *StackCollection) GetNodeGroupAutoScalingGroupName(s *Stack) (string, er } // GetManagedNodeGroupAutoScalingGroupName returns the managed nodegroup's AutoScalingGroupName -func (c *StackCollection) GetManagedNodeGroupAutoScalingGroupName(s *Stack) (string, error) { +func (c *StackCollection) getManagedNodeGroupAutoScalingGroupName(s *Stack) (string, error) { input := &eks.DescribeNodegroupInput{ ClusterName: aws.String(getClusterNameTag(s)), NodegroupName: aws.String(c.GetNodeGroupName(s)), @@ -382,21 +278,21 @@ func (c *StackCollection) GetManagedNodeGroupAutoScalingGroupName(s *Stack) (str } // GetAutoScalingGroupDesiredCapacity returns the AutoScalingGroup's desired capacity -func (c *StackCollection) GetAutoScalingGroupDesiredCapacity(name string) (*int64, error) { +func (c *StackCollection) GetAutoScalingGroupDesiredCapacity(name string) (autoscaling.Group, error) { asg, err := c.asgAPI.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{ AutoScalingGroupNames: []*string{ &name, }, }) if err != nil { - return nil, fmt.Errorf("couldn't describe ASG: %s", name) + return autoscaling.Group{}, 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 autoscaling.Group{}, fmt.Errorf("couldn't find ASG: %s", name) } - return asg.AutoScalingGroups[0].DesiredCapacity, nil + return *asg.AutoScalingGroups[0], nil } // DescribeNodeGroupStack gets the specified nodegroup stack diff --git a/pkg/cfn/manager/nodegroup_test.go b/pkg/cfn/manager/nodegroup_test.go index db7e8385e70..5cd2cc82870 100644 --- a/pkg/cfn/manager/nodegroup_test.go +++ b/pkg/cfn/manager/nodegroup_test.go @@ -38,8 +38,6 @@ var _ = Describe("StackCollection NodeGroup", func() { } ` - const nodegroupTemplate = "{\n \"Resources\": {\n \"NodeGroup\": {\n \"Type\": \"AWS::AutoScaling::AutoScalingGroup\",\n \"Properties\": {\n \"DesiredCapacity\": \"%d\",\n \"MaxSize\": \"%d\",\n \"MinSize\": \"%d\"\n }\n }\n }\n}" - testAZs := []string{"us-west-2b", "us-west-2a", "us-west-2c"} newClusterConfig := func(clusterName string) *api.ClusterConfig { @@ -62,139 +60,7 @@ var _ = Describe("StackCollection NodeGroup", func() { return ng } - Describe("ScaleNodeGroup", func() { - var ( - ng *api.NodeGroup - ) - - JustBeforeEach(func() { - p = mockprovider.NewMockProvider() - }) - - Context("With an existing NodeGroup", func() { - JustBeforeEach(func() { - cc = newClusterConfig("test-cluster") - ng = newNodeGroup(cc) - ng.Name = "12345" - sc = NewStackCollection(p, cc) - - p.MockCloudFormation(). - On("DescribeStacks", mock.MatchedBy(func(input *cfn.DescribeStacksInput) bool { - return input.StackName != nil && *input.StackName == "eksctl-test-cluster-nodegroup-12345" - })).Return(&cfn.DescribeStacksOutput{ - Stacks: []*Stack{ - { - Tags: []*cfn.Tag{ - { - Key: aws.String(api.NodeGroupNameTag), - Value: aws.String("12345"), - }, - }, - }, - }, - }, nil). - On("GetTemplate", mock.MatchedBy(func(input *cfn.GetTemplateInput) bool { - return input.StackName != nil && *input.StackName == "eksctl-test-cluster-nodegroup-12345" - })).Return(&cfn.GetTemplateOutput{ - TemplateBody: aws.String(nodegroupResource), - }, nil) - }) - - It("update the nodegroup if the desired capacity has changed", func() { - capacity := 4 - ng.DesiredCapacity = &capacity - template, _, err := sc.ScaleNodeGroupTemplate(ng) - Expect(err).NotTo(HaveOccurred()) - Expect(template).To(Equal(fmt.Sprintf(nodegroupTemplate, 4, 6, 1))) - }) - - It("update the nodegroup if the min capacity has changed", func() { - capacity := 2 - ng.MinSize = &capacity - template, _, err := sc.ScaleNodeGroupTemplate(ng) - Expect(err).NotTo(HaveOccurred()) - Expect(template).To(Equal(fmt.Sprintf(nodegroupTemplate, 3, 6, 2))) - }) - - It("update the nodegroup if the max capacity has changed", func() { - capacity := 10 - ng.MaxSize = &capacity - template, _, err := sc.ScaleNodeGroupTemplate(ng) - Expect(err).NotTo(HaveOccurred()) - Expect(template).To(Equal(fmt.Sprintf(nodegroupTemplate, 3, 10, 1))) - }) - - It("update the nodegroup if all the configuration has changed", func() { - minCapacity := 2 - ng.MinSize = &minCapacity - desiredCapacity := 4 - ng.DesiredCapacity = &desiredCapacity - maxCapacity := 10 - ng.MaxSize = &maxCapacity - template, _, err := sc.ScaleNodeGroupTemplate(ng) - Expect(err).NotTo(HaveOccurred()) - Expect(template).To(Equal(fmt.Sprintf(nodegroupTemplate, 4, 10, 2))) - }) - - It("should be a no-op if attempting to scale to the existing desired capacity", func() { - capacity := 3 - ng.DesiredCapacity = &capacity - template, _, err := sc.ScaleNodeGroupTemplate(ng) - Expect(err).NotTo(HaveOccurred()) - Expect(template).To(Equal("")) - }) - - It("should be a no-op if attempting to scale to the existing desired capacity, min size", func() { - minSize := 1 - capacity := 3 - ng.MinSize = &minSize - ng.DesiredCapacity = &capacity - template, _, err := sc.ScaleNodeGroupTemplate(ng) - Expect(err).NotTo(HaveOccurred()) - Expect(template).To(Equal("")) - }) - - It("should be a no-op if attempting to scale to the existing desired capacity, max size", func() { - capacity := 3 - maxSize := 6 - ng.DesiredCapacity = &capacity - ng.MaxSize = &maxSize - template, _, err := sc.ScaleNodeGroupTemplate(ng) - Expect(err).NotTo(HaveOccurred()) - Expect(template).To(Equal("")) - }) - - It("should be a no-op if attempting to scale to the existing desired capacity, min size and max size", func() { - minSize := 1 - capacity := 3 - maxSize := 6 - ng.MinSize = &minSize - ng.DesiredCapacity = &capacity - ng.MaxSize = &maxSize - template, _, err := sc.ScaleNodeGroupTemplate(ng) - Expect(err).NotTo(HaveOccurred()) - Expect(template).To(Equal("")) - }) - - It("should be a error if the desired capacity is greater than the CF maxSize", func() { - capacity := 10 - ng.DesiredCapacity = &capacity - _, _, err := sc.ScaleNodeGroupTemplate(ng) - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(Equal("the desired nodes 10 is greater than the nodes-max/maxSize 6")) - }) - - It("should be a error if the desired capacity is less than the CF minSize", func() { - capacity := 0 - ng.DesiredCapacity = &capacity - _, _, err := sc.ScaleNodeGroupTemplate(ng) - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(Equal("the desired nodes 0 is less than the nodes-min/minSize 1")) - }) - }) - }) - - Describe("GetNodeGroupSummaries", func() { + Describe("GetUnmanagedNodeGroupSummaries", func() { Context("With a cluster name", func() { var ( clusterName string @@ -289,7 +155,7 @@ var _ = Describe("StackCollection NodeGroup", func() { }) JustBeforeEach(func() { - out, err = sc.GetNodeGroupSummaries("") + out, err = sc.GetUnmanagedNodeGroupSummaries("") }) It("should not error", func() { @@ -311,7 +177,7 @@ var _ = Describe("StackCollection NodeGroup", func() { }) JustBeforeEach(func() { - out, err = sc.GetNodeGroupSummaries("") + out, err = sc.GetUnmanagedNodeGroupSummaries("") }) It("should not error", func() {