Skip to content

Commit 9cbe52f

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

File tree

11 files changed

+182
-0
lines changed

11 files changed

+182
-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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
8+
"github.com/kris-nova/logger"
9+
"github.com/spf13/cobra"
10+
"github.com/spf13/pflag"
11+
12+
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
13+
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
14+
)
15+
16+
func updateClusterDeletionProtectionCmd(cmd *cmdutils.Cmd) {
17+
cfg := api.NewClusterConfig()
18+
cmd.ClusterConfig = cfg
19+
20+
var enabled string
21+
22+
cmd.SetDescription("deletion-protection", "Update cluster deletion protection", "")
23+
24+
cmdutils.AddCommonFlagsForAWS(cmd, &cmd.ProviderConfig, false)
25+
26+
cmd.FlagSetGroup.InFlagSet("General", func(fs *pflag.FlagSet) {
27+
fs.StringVarP(&cfg.Metadata.Name, "name", "n", "", "EKS cluster name")
28+
cmdutils.AddRegionFlag(fs, &cmd.ProviderConfig)
29+
cmdutils.AddConfigFileFlag(fs, &cmd.ClusterConfigFile)
30+
cmdutils.AddApproveFlag(fs, cmd)
31+
fs.StringVar(&enabled, "enabled", "", "Enable or disable deletion protection (true|false)")
32+
})
33+
34+
cmd.CobraCommand.RunE = func(_ *cobra.Command, args []string) error {
35+
cmd.NameArg = cmdutils.GetNameArg(args)
36+
37+
if enabled == "" {
38+
return fmt.Errorf("--enabled flag is required (true|false)")
39+
}
40+
41+
val, err := strconv.ParseBool(enabled)
42+
if err != nil {
43+
return fmt.Errorf("--enabled must be 'true' or 'false', got: %s", enabled)
44+
}
45+
46+
cfg.DeletionProtection = &val
47+
48+
return doUpdateClusterDeletionProtection(cmd)
49+
}
50+
}
51+
52+
func doUpdateClusterDeletionProtection(cmd *cmdutils.Cmd) error {
53+
ctx := context.Background()
54+
if err := cmdutils.NewMetadataLoader(cmd).Load(); err != nil {
55+
return err
56+
}
57+
58+
cfg := cmd.ClusterConfig
59+
if cfg.Metadata.Name == "" {
60+
return fmt.Errorf("cluster name is required")
61+
}
62+
63+
ctl, err := cmd.NewProviderForExistingCluster(ctx)
64+
if err != nil {
65+
return err
66+
}
67+
68+
if cmd.Plan {
69+
logger.Critical("--dry-run is not supported for this command")
70+
return nil
71+
}
72+
73+
action := "disabling"
74+
if cfg.DeletionProtection != nil && *cfg.DeletionProtection {
75+
action = "enabling"
76+
}
77+
78+
logger.Info("%s deletion protection for cluster %q", action, cfg.Metadata.Name)
79+
return ctl.UpdateClusterConfigForDeletionProtection(ctx, cfg)
80+
}

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)