Skip to content

Commit fbd5e3d

Browse files
committed
feat: add support for DeletionProtection attribute passing
1 parent 05ec10a commit fbd5e3d

File tree

11 files changed

+175
-0
lines changed

11 files changed

+175
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# A sample ClusterConfig file that creates a cluster with deletion protection enabled.
2+
3+
# DeletionProtection prevents accidental cluster deletion
4+
# Valid values are true or false (default)
5+
# - https://docs.aws.amazon.com/eks/latest/APIReference/API_CreateCluster.html
6+
# - https://docs.aws.amazon.com/eks/latest/APIReference/API_CreateCluster.html#AmazonEKS-CreateCluster-request-deletionProtection
7+
8+
apiVersion: eksctl.io/v1alpha5
9+
kind: ClusterConfig
10+
11+
metadata:
12+
name: deletion-protection-cluster
13+
region: us-west-2
14+
15+
deletionProtection: true
16+
17+
managedNodeGroups:
18+
- name: mng-1
19+
desiredCapacity: 1

pkg/apis/eksctl.io/v1alpha5/assets/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,11 @@
467467
"description": "See [CloudWatch support](/usage/cloudwatch-cluster-logging/)",
468468
"x-intellij-html-description": "See <a href=\"/usage/cloudwatch-cluster-logging/\">CloudWatch support</a>"
469469
},
470+
"deletionProtection": {
471+
"type": "boolean",
472+
"description": "specifies whether deletion protection is enabled for the cluster",
473+
"x-intellij-html-description": "specifies whether deletion protection is enabled for the cluster"
474+
},
470475
"fargateProfiles": {
471476
"items": {
472477
"$ref": "#/definitions/FargateProfile"
@@ -569,6 +574,7 @@
569574
"apiVersion",
570575
"metadata",
571576
"upgradePolicy",
577+
"deletionProtection",
572578
"kubernetesNetworkConfig",
573579
"autoModeConfig",
574580
"remoteNetworkConfig",

pkg/apis/eksctl.io/v1alpha5/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,10 @@ type ClusterConfig struct {
966966
// +optional
967967
UpgradePolicy *UpgradePolicy `json:"upgradePolicy,omitempty"`
968968

969+
// DeletionProtection specifies whether deletion protection is enabled for the cluster
970+
// +optional
971+
DeletionProtection *bool `json:"deletionProtection,omitempty"`
972+
969973
// +optional
970974
KubernetesNetworkConfig *KubernetesNetworkConfig `json:"kubernetesNetworkConfig,omitempty"`
971975

pkg/apis/eksctl.io/v1alpha5/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cfn/builder/cluster.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ func (c *ClusterResourceSet) addResourcesForControlPlane(subnetDetails *SubnetDe
364364
}
365365
}
366366

367+
var deletionProtection *gfnt.Value
368+
if c.spec.DeletionProtection != nil {
369+
deletionProtection = gfnt.NewBoolean(*c.spec.DeletionProtection)
370+
}
371+
367372
cluster := gfneks.Cluster{
368373
EncryptionConfig: encryptionConfigs,
369374
Logging: makeClusterLogging(c.spec),
@@ -372,6 +377,7 @@ func (c *ClusterResourceSet) addResourcesForControlPlane(subnetDetails *SubnetDe
372377
RoleArn: serviceRoleARN,
373378
BootstrapSelfManagedAddons: gfnt.NewBoolean(false),
374379
UpgradePolicy: upgradePolicy,
380+
DeletionProtection: deletionProtection,
375381
AccessConfig: &gfneks.Cluster_AccessConfig{
376382
AuthenticationMode: gfnt.NewString(string(c.spec.AccessConfig.AuthenticationMode)),
377383
BootstrapClusterCreatorAdminPermissions: gfnt.NewBoolean(!api.IsDisabled(c.spec.AccessConfig.BootstrapClusterCreatorAdminPermissions)),
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/kris-nova/logger"
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/pflag"
10+
11+
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
12+
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
13+
)
14+
15+
func updateClusterDeletionProtectionCmd(cmd *cmdutils.Cmd) {
16+
cfg := api.NewClusterConfig()
17+
cmd.ClusterConfig = cfg
18+
19+
var enable, disable bool
20+
21+
cmd.SetDescription("deletion-protection", "Update cluster deletion protection", "")
22+
23+
cmdutils.AddCommonFlagsForAWS(cmd, &cmd.ProviderConfig, false)
24+
25+
cmd.FlagSetGroup.InFlagSet("General", func(fs *pflag.FlagSet) {
26+
fs.StringVarP(&cfg.Metadata.Name, "name", "n", "", "EKS cluster name")
27+
cmdutils.AddRegionFlag(fs, &cmd.ProviderConfig)
28+
cmdutils.AddConfigFileFlag(fs, &cmd.ClusterConfigFile)
29+
cmdutils.AddApproveFlag(fs, cmd)
30+
fs.BoolVar(&enable, "enable", false, "Enable deletion protection")
31+
fs.BoolVar(&disable, "disable", false, "Disable deletion protection")
32+
})
33+
34+
cmd.CobraCommand.RunE = func(_ *cobra.Command, args []string) error {
35+
cmd.NameArg = cmdutils.GetNameArg(args)
36+
37+
if enable && disable {
38+
return fmt.Errorf("--enable and --disable cannot be used together")
39+
}
40+
if !enable && !disable {
41+
return fmt.Errorf("either --enable or --disable must be specified")
42+
}
43+
44+
if enable {
45+
cfg.DeletionProtection = &enable
46+
} else {
47+
cfg.DeletionProtection = &disable
48+
}
49+
50+
return doUpdateClusterDeletionProtection(cmd)
51+
}
52+
}
53+
54+
func doUpdateClusterDeletionProtection(cmd *cmdutils.Cmd) error {
55+
ctx := context.Background()
56+
if err := cmdutils.NewMetadataLoader(cmd).Load(); err != nil {
57+
return err
58+
}
59+
60+
cfg := cmd.ClusterConfig
61+
ctl, err := cmd.NewProviderForExistingCluster(ctx)
62+
if err != nil {
63+
return err
64+
}
65+
66+
if cmd.Plan {
67+
logger.Critical("--dry-run is not supported for this command")
68+
return nil
69+
}
70+
71+
logger.Info("updating deletion protection for cluster %q", cfg.Metadata.Name)
72+
return ctl.UpdateClusterConfigForDeletionProtection(ctx, cfg)
73+
}

pkg/ctl/utils/utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command {
2222
cmdutils.AddResourceCmd(flagGrouping, verbCmd, updateClusterEndpointsCmd)
2323
cmdutils.AddResourceCmd(flagGrouping, verbCmd, publicAccessCIDRsCmd)
2424
cmdutils.AddResourceCmd(flagGrouping, verbCmd, updateClusterVPCConfigCmd)
25+
cmdutils.AddResourceCmd(flagGrouping, verbCmd, updateClusterDeletionProtectionCmd)
2526
cmdutils.AddResourceCmd(flagGrouping, verbCmd, enableSecretsEncryptionCmd)
2627
cmdutils.AddResourceCmd(flagGrouping, verbCmd, schemaCmd)
2728
cmdutils.AddResourceCmd(flagGrouping, verbCmd, nodeGroupHealthCmd)

pkg/eks/update.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ func (c *ClusterProvider) UpdatePublicAccessCIDRs(ctx context.Context, clusterCo
156156
return c.UpdateClusterConfig(ctx, input)
157157
}
158158

159+
// UpdateClusterConfigForDeletionProtection calls eks.UpdateClusterConfig and updates deletion protection
160+
func (c *ClusterProvider) UpdateClusterConfigForDeletionProtection(ctx context.Context, cfg *api.ClusterConfig) error {
161+
input := &eks.UpdateClusterConfigInput{
162+
Name: &cfg.Metadata.Name,
163+
DeletionProtection: cfg.DeletionProtection,
164+
}
165+
return c.UpdateClusterConfig(ctx, input)
166+
}
167+
159168
// UpdateClusterConfig calls EKS.UpdateClusterConfig and waits for the update to complete.
160169
func (c *ClusterProvider) UpdateClusterConfig(ctx context.Context, input *eks.UpdateClusterConfigInput) error {
161170
output, err := c.AWSProvider.EKS().UpdateClusterConfig(ctx, input)

pkg/goformation/cloudformation/eks/aws-eks-cluster.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ type Cluster struct {
3030
// See: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-eks-cluster.html#cfn-eks-cluster-computeconfig
3131
ComputeConfig *Cluster_ComputeConfig `json:"ComputeConfig,omitempty"`
3232

33+
// DeletionProtection AWS CloudFormation Property
34+
// Required: false
35+
// See: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-eks-cluster.html#cfn-eks-cluster-deletionprotection
36+
DeletionProtection *types.Value `json:"DeletionProtection,omitempty"`
37+
3338
// EncryptionConfig AWS CloudFormation Property
3439
// Required: false
3540
// See: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-eks-cluster.html#cfn-eks-cluster-encryptionconfig

userdocs/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ nav:
158158
- usage/cluster-upgrade.md
159159
- usage/addon-upgrade.md
160160
- usage/upgrade-policy.md
161+
- usage/deletion-protection.md
161162
- usage/zonal-shift.md
162163
- Nodegroups:
163164
- usage/nodegroups.md

0 commit comments

Comments
 (0)