Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drain nodegroups during cluster deletion #4205

Merged
merged 4 commits into from
Sep 22, 2021
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
22 changes: 22 additions & 0 deletions pkg/actions/cluster/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/pkg/errors"

"github.com/weaveworks/eksctl/pkg/actions/nodegroup"
"github.com/weaveworks/eksctl/pkg/cfn/manager"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
"github.com/weaveworks/eksctl/pkg/fargate"
"github.com/weaveworks/eksctl/pkg/kubernetes"

Expand Down Expand Up @@ -151,3 +153,23 @@ func checkForUndeletedStacks(stackManager manager.StackManager) error {

return nil
}

func drainAllNodegroups(cfg *api.ClusterConfig, ctl *eks.ClusterProvider, stackManager manager.StackManager, clientSet kubernetes.Interface, allStacks []manager.NodeGroupStack) error {
if len(allStacks) == 0 {
return nil
}

cfg.NodeGroups = []*api.NodeGroup{}
for _, s := range allStacks {
if s.Type == api.NodeGroupTypeUnmanaged {
cmdutils.PopulateUnmanagedNodegroup(s.NodeGroupName, cfg)
}
}

logger.Info("will drain %d unmanaged nodegroup(s) in cluster %q", len(cfg.NodeGroups), cfg.Metadata.Name)
nodeGroupManager := nodegroup.New(cfg, ctl, clientSet)
if err := nodeGroupManager.Drain(cmdutils.ToKubeNodeGroups(cfg), false, ctl.Provider.WaitTimeout(), false); err != nil {
return err
}
return nil
}
7 changes: 7 additions & 0 deletions pkg/actions/cluster/owned.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ func (c *OwnedCluster) Delete(_ time.Duration, wait, force bool) error {
}
oidcSupported = false
}
allStacks, err := c.stackManager.ListNodeGroupStacks()
if err != nil {
return err
}
if err := drainAllNodegroups(c.cfg, c.ctl, c.stackManager, clientSet, allStacks); err != nil {
return err
}
aclevername marked this conversation as resolved.
Show resolved Hide resolved
}

if err := deleteSharedResources(c.cfg, c.ctl, c.stackManager, clusterOperable, clientSet); err != nil {
Expand Down
19 changes: 11 additions & 8 deletions pkg/actions/cluster/unowned.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,21 @@ func (c *UnownedCluster) Delete(waitInterval time.Duration, wait, force bool) er
logger.Debug("failed to check if cluster is operable: %v", err)
}

allStacks, err := c.stackManager.ListNodeGroupStacks()
if err != nil {
return err
}

var clientSet kubernetes.Interface
if clusterOperable {
clientSet, err = c.newClientSet()
if err != nil {
return err
}

if err := drainAllNodegroups(c.cfg, c.ctl, c.stackManager, clientSet, allStacks); err != nil {
return err
}
}

if err := deleteSharedResources(c.cfg, c.ctl, c.stackManager, clusterOperable, clientSet); err != nil {
Expand All @@ -93,7 +102,7 @@ func (c *UnownedCluster) Delete(waitInterval time.Duration, wait, force bool) er

// we have to wait for nodegroups to delete before deleting the cluster
// so the `wait` value is ignored here
if err := c.deleteAndWaitForNodegroupsDeletion(waitInterval); err != nil {
if err := c.deleteAndWaitForNodegroupsDeletion(waitInterval, allStacks); err != nil {
return err
}

Expand Down Expand Up @@ -237,7 +246,7 @@ func (c *UnownedCluster) deleteCluster(wait bool) error {
return waiters.Wait(clusterName, msg, acceptors, newRequest, c.ctl.Provider.WaitTimeout(), nil)
}

func (c *UnownedCluster) deleteAndWaitForNodegroupsDeletion(waitInterval time.Duration) error {
func (c *UnownedCluster) deleteAndWaitForNodegroupsDeletion(waitInterval time.Duration, allStacks []manager.NodeGroupStack) error {
clusterName := c.cfg.Metadata.Name
eksAPI := c.ctl.Provider.EKS()

Expand All @@ -249,12 +258,6 @@ func (c *UnownedCluster) deleteAndWaitForNodegroupsDeletion(waitInterval time.Du
return err
}

// get all nodegroup stacks for this cluster
allStacks, err := c.stackManager.ListNodeGroupStacks()
if err != nil {
return err
}

if len(allStacks) == 0 && len(nodeGroups.Nodegroups) == 0 {
logger.Warning("no nodegroups found for %s", clusterName)
return nil
Expand Down
5 changes: 0 additions & 5 deletions pkg/actions/nodegroup/drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@ import (

"github.com/weaveworks/eksctl/pkg/eks"

"github.com/kris-nova/logger"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
"github.com/weaveworks/eksctl/pkg/drain"
)

func (m *Manager) Drain(nodeGroups []eks.KubeNodeGroup, plan bool, maxGracePeriod time.Duration, disableEviction bool) error {
cmdutils.LogIntendedAction(plan, "drain %d nodegroup(s) in cluster %q", len(nodeGroups), m.cfg.Metadata.Name)

if !plan {
for _, n := range nodeGroups {
nodeGroupDrainer := drain.NewNodeGroupDrainer(m.clientSet, n, m.ctl.Provider.WaitTimeout(), maxGracePeriod, false, disableEviction)
if err := nodeGroupDrainer.Drain(); err != nil {
logger.Warning("error occurred during drain, to skip drain use '--drain=false' flag")
return err
}
}
Expand Down
49 changes: 28 additions & 21 deletions pkg/ctl/cmdutils/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,40 @@ func PopulateNodegroup(stackManager manager.StackManager, name string, cfg *api.
}
nodeGroupType = api.NodeGroupTypeUnowned
}
if err = PopulateNodegroupFromStack(nodeGroupType, name, cfg); err != nil {
return err
}

return nil
}

// PopulateNodegroupFromStack populates the nodegroup field of an api.ClusterConfig by type from its CF stack.
func PopulateNodegroupFromStack(nodeGroupType api.NodeGroupType, nodeGroupName string, cfg *api.ClusterConfig) error {
switch nodeGroupType {
case api.NodeGroupTypeUnmanaged:
cfg.NodeGroups = []*api.NodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: name,
},
},
}
PopulateUnmanagedNodegroup(nodeGroupName, cfg)
case api.NodeGroupTypeManaged:
cfg.ManagedNodeGroups = []*api.ManagedNodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: name,
},
cfg.ManagedNodeGroups = append(cfg.ManagedNodeGroups, &api.ManagedNodeGroup{
NodeGroupBase: &api.NodeGroupBase{
Name: nodeGroupName,
},
}
})
case api.NodeGroupTypeUnowned:
cfg.ManagedNodeGroups = []*api.ManagedNodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: name,
},
Unowned: true,
cfg.ManagedNodeGroups = append(cfg.ManagedNodeGroups, &api.ManagedNodeGroup{
NodeGroupBase: &api.NodeGroupBase{
Name: nodeGroupName,
},
}
Unowned: true,
})
}
aclevername marked this conversation as resolved.
Show resolved Hide resolved

return nil
}

// PopulateUnmanagedNodegroup populates the unmanaged nodegroup field of a ClucterConfig.
func PopulateUnmanagedNodegroup(nodeGroupName string, cfg *api.ClusterConfig) {
cfg.NodeGroups = append(cfg.NodeGroups, &api.NodeGroup{
NodeGroupBase: &api.NodeGroupBase{
Name: nodeGroupName,
},
})
}
63 changes: 63 additions & 0 deletions pkg/ctl/cmdutils/nodegroup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmdutils

import (
"github.com/aws/aws-sdk-go/aws"
awseks "github.com/aws/aws-sdk-go/service/eks"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/manager/fakes"
"github.com/weaveworks/eksctl/pkg/testutils/mockprovider"
)

var _ = Describe("PopulateNodegroup", func() {
var (
ngName string
cfg *api.ClusterConfig
fakeStackManager *fakes.FakeStackManager
mockProvider *mockprovider.MockProvider
err error
)

BeforeEach(func() {
fakeStackManager = new(fakes.FakeStackManager)
ngName = "ng"
cfg = api.NewClusterConfig()
mockProvider = mockprovider.NewMockProvider()
})

Context("unmanaged nodegroup", func() {
It("is added to the cfg", func() {
fakeStackManager.GetNodeGroupStackTypeReturns(api.NodeGroupTypeUnmanaged, nil)
err = PopulateNodegroup(fakeStackManager, ngName, cfg, mockProvider)
Expect(err).NotTo(HaveOccurred())
Expect(cfg.NodeGroups[0].Name).To(Equal(ngName))
})
})

Context("managed nodegroup", func() {
It("is added to the cfg", func() {
fakeStackManager.GetNodeGroupStackTypeReturns(api.NodeGroupTypeManaged, nil)
err = PopulateNodegroup(fakeStackManager, ngName, cfg, mockProvider)
Expect(err).NotTo(HaveOccurred())
Expect(cfg.ManagedNodeGroups[0].Name).To(Equal(ngName))
})
})

Context("unowned nodegroup", func() {
It("is added to the cfg", func() {
clusterName := "cluster-name"
cfg.Metadata.Name = clusterName
fakeStackManager.GetNodeGroupStackTypeReturns("", errors.New(""))
mockProvider.MockEKS().On("DescribeNodegroup", &awseks.DescribeNodegroupInput{
ClusterName: aws.String(clusterName),
NodegroupName: aws.String(ngName),
}).Return(&awseks.DescribeNodegroupOutput{Nodegroup: &awseks.Nodegroup{}}, nil)

err = PopulateNodegroup(fakeStackManager, ngName, cfg, mockProvider)
Expect(err).NotTo(HaveOccurred())
Expect(cfg.ManagedNodeGroups[0].Name).To(Equal(ngName))
})
})
})
2 changes: 2 additions & 0 deletions pkg/ctl/delete/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ func doDeleteNodeGroup(cmd *cmdutils.Cmd, ng *api.NodeGroup, updateAuthConfigMap

nodeGroupManager := nodegroup.New(cfg, ctl, clientSet)
if deleteNodeGroupDrain {
cmdutils.LogIntendedAction(cmd.Plan, "drain %d nodegroup(s) in cluster %q", len(allNodeGroups), cfg.Metadata.Name)
err := nodeGroupManager.Drain(allNodeGroups, cmd.Plan, maxGracePeriod, disableEviction)
if err != nil {
logger.Warning("error occurred during drain, to skip drain use '--drain=false' flag")
return err
}
}
Expand Down