From 995b6e0b60c98608930ca4deb3f45230af61cec9 Mon Sep 17 00:00:00 2001 From: Spyros Synodinos <138458697+ssyno@users.noreply.github.com> Date: Wed, 19 Jun 2024 13:07:42 +0300 Subject: [PATCH] --prevent-deletion implementation (#1334) * init commit * Added prevent-deletion flag value to cluster template config * update for capz & capa-eks * CHANGELOG * tests update * revert test_4 * Update flag.go Co-authored-by: Marian Steinbach --------- Co-authored-by: vvondruska Co-authored-by: Marian Steinbach --- CHANGELOG.md | 4 ++++ cmd/template/cluster/flag.go | 3 +++ cmd/template/cluster/provider/capa.go | 7 ++++--- cmd/template/cluster/provider/capz.go | 7 ++++--- cmd/template/cluster/provider/common.go | 1 + cmd/template/cluster/provider/eks.go | 7 ++++--- cmd/template/cluster/provider/templates/capa/functions.go | 6 +++++- cmd/template/cluster/provider/templates/capa/types.go | 7 ++++--- cmd/template/cluster/provider/templates/capz/functions.go | 4 ++++ cmd/template/cluster/provider/templates/capz/types.go | 7 ++++--- cmd/template/cluster/provider/templates/eks/functions.go | 4 ++++ cmd/template/cluster/provider/templates/eks/types.go | 7 ++++--- cmd/template/cluster/runner.go | 1 + .../cluster/testdata/run_template_cluster_capa.golden | 1 + .../cluster/testdata/run_template_cluster_capa_2.golden | 1 + .../cluster/testdata/run_template_cluster_capa_3.golden | 1 + .../cluster/testdata/run_template_cluster_capa_6.golden | 1 + .../cluster/testdata/run_template_cluster_capa_7.golden | 1 + .../cluster/testdata/run_template_cluster_capa_8.golden | 1 + 19 files changed, 52 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db46da733..54531a9a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project's packages adheres to [Semantic Versioning](http://semver.org/s ## [Unreleased] +### Added + +- Added `--prevent-deletion` flag to cluster template command for capa, capa-eks, capz clusters + ## [2.56.0] - 2024-06-10 ### Added diff --git a/cmd/template/cluster/flag.go b/cmd/template/cluster/flag.go index 835b55ed7..c8fcb1dbd 100644 --- a/cmd/template/cluster/flag.go +++ b/cmd/template/cluster/flag.go @@ -20,6 +20,7 @@ const ( flagEnableLongNames = "enable-long-names" flagProvider = "provider" flagManagementCluster = "management-cluster" + flagPreventDeletion = "prevent-deletion" // AWS only. flagAWSExternalSNAT = "external-snat" @@ -143,6 +144,7 @@ type flag struct { EnableLongNames bool Provider string ManagementCluster string + PreventDeletion bool // Common. ControlPlaneAZ []string @@ -177,6 +179,7 @@ func (f *flag) Init(cmd *cobra.Command) { cmd.Flags().BoolVar(&f.EnableLongNames, flagEnableLongNames, true, "Allow long names.") cmd.Flags().StringVar(&f.Provider, flagProvider, "", "Installation infrastructure provider.") cmd.Flags().StringVar(&f.ManagementCluster, flagManagementCluster, "", "Name of the management cluster. Only required in combination with certain parameters.") + cmd.Flags().BoolVar(&f.PreventDeletion, flagPreventDeletion, false, "Prevent cluster from getting deleted") // AWS only. cmd.Flags().StringVar(&f.AWS.AWSClusterRoleIdentityName, flagAWSClusterRoleIdentityName, "", "Name of the AWSClusterRoleIdentity that will be used for cluster creation.") diff --git a/cmd/template/cluster/provider/capa.go b/cmd/template/cluster/provider/capa.go index d5bf70881..b20226eac 100644 --- a/cmd/template/cluster/provider/capa.go +++ b/cmd/template/cluster/provider/capa.go @@ -281,9 +281,10 @@ func BuildCapaClusterConfig(config ClusterConfig) capa.ClusterConfig { InstanceType: config.ControlPlaneInstanceType, }, Metadata: &capa.Metadata{ - Name: config.Name, - Description: config.Description, - Organization: config.Organization, + Name: config.Name, + Description: config.Description, + Organization: config.Organization, + PreventDeletion: config.PreventDeletion, }, NodePools: &map[string]capa.MachinePool{ config.AWS.MachinePool.Name: { diff --git a/cmd/template/cluster/provider/capz.go b/cmd/template/cluster/provider/capz.go index f979a2d70..d7c81436d 100644 --- a/cmd/template/cluster/provider/capz.go +++ b/cmd/template/cluster/provider/capz.go @@ -104,9 +104,10 @@ func BuildCapzClusterConfig(config ClusterConfig) capz.ClusterConfig { return capz.ClusterConfig{ Global: &capz.Global{ Metadata: &capz.Metadata{ - Name: config.Name, - Description: config.Description, - Organization: config.Organization, + Name: config.Name, + Description: config.Description, + Organization: config.Organization, + PreventDeletion: config.PreventDeletion, }, ProviderSpecific: &capz.ProviderSpecific{ Location: config.Region, diff --git a/cmd/template/cluster/provider/common.go b/cmd/template/cluster/provider/common.go index cade47579..9127292ad 100644 --- a/cmd/template/cluster/provider/common.go +++ b/cmd/template/cluster/provider/common.go @@ -154,6 +154,7 @@ type ClusterConfig struct { PodsCIDR string OIDC OIDC ServicePriority string + PreventDeletion bool Region string BastionInstanceType string diff --git a/cmd/template/cluster/provider/eks.go b/cmd/template/cluster/provider/eks.go index 3251dbc6d..fc5e945aa 100644 --- a/cmd/template/cluster/provider/eks.go +++ b/cmd/template/cluster/provider/eks.go @@ -108,9 +108,10 @@ func BuildEKSClusterConfig(config ClusterConfig) eks.ClusterConfig { return eks.ClusterConfig{ Global: &eks.Global{ Metadata: &eks.Metadata{ - Name: config.Name, - Description: config.Description, - Organization: config.Organization, + Name: config.Name, + Description: config.Description, + Organization: config.Organization, + PreventDeletion: config.PreventDeletion, }, }, } diff --git a/cmd/template/cluster/provider/templates/capa/functions.go b/cmd/template/cluster/provider/templates/capa/functions.go index 3bc831962..54a843994 100644 --- a/cmd/template/cluster/provider/templates/capa/functions.go +++ b/cmd/template/cluster/provider/templates/capa/functions.go @@ -34,7 +34,11 @@ func GenerateClusterValues(flagInputs ClusterConfig) (string, error) { } } - finalConfigString, err := yaml.Marshal(flagInputs) + if metadata, ok := flagConfigData["global"].(map[string]interface{})["metadata"].(map[string]interface{}); ok { + metadata["preventDeletion"] = flagInputs.Global.Metadata.PreventDeletion + } + + finalConfigString, err := yaml.Marshal(flagConfigData) if err != nil { return "", microerror.Mask(err) } diff --git a/cmd/template/cluster/provider/templates/capa/types.go b/cmd/template/cluster/provider/templates/capa/types.go index 290fd95fb..d293db0ae 100644 --- a/cmd/template/cluster/provider/templates/capa/types.go +++ b/cmd/template/cluster/provider/templates/capa/types.go @@ -13,9 +13,10 @@ type ClusterConfig struct { } type Metadata struct { - Description string `json:"description,omitempty"` - Name string `json:"name,omitempty"` - Organization string `json:"organization,omitempty"` + Description string `json:"description,omitempty"` + Name string `json:"name,omitempty"` + Organization string `json:"organization,omitempty"` + PreventDeletion bool `json:"preventDeletion,omitempty"` } type DefaultAppsConfig struct { diff --git a/cmd/template/cluster/provider/templates/capz/functions.go b/cmd/template/cluster/provider/templates/capz/functions.go index 88fff2b68..4722ecbe0 100644 --- a/cmd/template/cluster/provider/templates/capz/functions.go +++ b/cmd/template/cluster/provider/templates/capz/functions.go @@ -29,6 +29,10 @@ func GenerateClusterValues(flagInputs ClusterConfig) (string, error) { } } + if metadata, ok := flagConfigData["global"].(map[string]interface{})["metadata"].(map[string]interface{}); ok { + metadata["preventDeletion"] = flagInputs.Global.Metadata.PreventDeletion + } + finalConfigString, err := yaml.Marshal(flagInputs) if err != nil { return "", microerror.Mask(err) diff --git a/cmd/template/cluster/provider/templates/capz/types.go b/cmd/template/cluster/provider/templates/capz/types.go index 155cab44c..aa032702b 100644 --- a/cmd/template/cluster/provider/templates/capz/types.go +++ b/cmd/template/cluster/provider/templates/capz/types.go @@ -12,9 +12,10 @@ type Global struct { } type Metadata struct { - Name string `json:"name,omitempty"` - Description string `json:"description,omitempty"` - Organization string `json:"organization,omitempty"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + Organization string `json:"organization,omitempty"` + PreventDeletion bool `json:"preventDeletion,omitempty"` } type DefaultAppsConfig struct { diff --git a/cmd/template/cluster/provider/templates/eks/functions.go b/cmd/template/cluster/provider/templates/eks/functions.go index bfac71780..9030bf2d7 100644 --- a/cmd/template/cluster/provider/templates/eks/functions.go +++ b/cmd/template/cluster/provider/templates/eks/functions.go @@ -21,6 +21,10 @@ func GenerateClusterValues(flagInputs ClusterConfig) (string, error) { } } + if metadata, ok := flagConfigData["global"].(map[string]interface{})["metadata"].(map[string]interface{}); ok { + metadata["preventDeletion"] = flagInputs.Global.Metadata.PreventDeletion + } + finalConfigString, err := yaml.Marshal(flagInputs) if err != nil { return "", microerror.Mask(err) diff --git a/cmd/template/cluster/provider/templates/eks/types.go b/cmd/template/cluster/provider/templates/eks/types.go index bde9de2a6..ef57e9df3 100644 --- a/cmd/template/cluster/provider/templates/eks/types.go +++ b/cmd/template/cluster/provider/templates/eks/types.go @@ -14,7 +14,8 @@ type ClusterConfig struct { } type Metadata struct { - Name string `json:"name,omitempty"` - Description string `json:"description,omitempty"` - Organization string `json:"organization,omitempty"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + Organization string `json:"organization,omitempty"` + PreventDeletion bool `json:"preventDeletion,omitempty"` } diff --git a/cmd/template/cluster/runner.go b/cmd/template/cluster/runner.go index 8bd6542db..b63b5eb66 100644 --- a/cmd/template/cluster/runner.go +++ b/cmd/template/cluster/runner.go @@ -131,6 +131,7 @@ func (r *runner) getClusterConfig() (provider.ClusterConfig, error) { Namespace: metav1.NamespaceDefault, Region: r.flag.Region, ServicePriority: r.flag.ServicePriority, + PreventDeletion: r.flag.PreventDeletion, App: r.flag.App, AWS: r.flag.AWS, diff --git a/cmd/template/cluster/testdata/run_template_cluster_capa.golden b/cmd/template/cluster/testdata/run_template_cluster_capa.golden index 329b6200f..b1fae113a 100644 --- a/cmd/template/cluster/testdata/run_template_cluster_capa.golden +++ b/cmd/template/cluster/testdata/run_template_cluster_capa.golden @@ -31,6 +31,7 @@ data: description: just a test cluster name: test1 organization: test + preventDeletion: false nodePools: worker1: availabilityZones: diff --git a/cmd/template/cluster/testdata/run_template_cluster_capa_2.golden b/cmd/template/cluster/testdata/run_template_cluster_capa_2.golden index 1119eab06..33a825c88 100644 --- a/cmd/template/cluster/testdata/run_template_cluster_capa_2.golden +++ b/cmd/template/cluster/testdata/run_template_cluster_capa_2.golden @@ -33,6 +33,7 @@ data: description: just a test cluster name: test1 organization: test + preventDeletion: false nodePools: worker1: availabilityZones: diff --git a/cmd/template/cluster/testdata/run_template_cluster_capa_3.golden b/cmd/template/cluster/testdata/run_template_cluster_capa_3.golden index fd12be6a0..407401255 100644 --- a/cmd/template/cluster/testdata/run_template_cluster_capa_3.golden +++ b/cmd/template/cluster/testdata/run_template_cluster_capa_3.golden @@ -36,6 +36,7 @@ data: description: just a test cluster name: test1 organization: test + preventDeletion: false nodePools: worker1: availabilityZones: diff --git a/cmd/template/cluster/testdata/run_template_cluster_capa_6.golden b/cmd/template/cluster/testdata/run_template_cluster_capa_6.golden index 0d7d4b627..4df818afd 100644 --- a/cmd/template/cluster/testdata/run_template_cluster_capa_6.golden +++ b/cmd/template/cluster/testdata/run_template_cluster_capa_6.golden @@ -27,6 +27,7 @@ data: description: just a test cluster name: test6 organization: test + preventDeletion: false nodePools: worker1: availabilityZones: diff --git a/cmd/template/cluster/testdata/run_template_cluster_capa_7.golden b/cmd/template/cluster/testdata/run_template_cluster_capa_7.golden index 8a3474fa7..71700574f 100644 --- a/cmd/template/cluster/testdata/run_template_cluster_capa_7.golden +++ b/cmd/template/cluster/testdata/run_template_cluster_capa_7.golden @@ -31,6 +31,7 @@ data: description: just a test cluster name: test7 organization: test + preventDeletion: false nodePools: worker1: availabilityZones: diff --git a/cmd/template/cluster/testdata/run_template_cluster_capa_8.golden b/cmd/template/cluster/testdata/run_template_cluster_capa_8.golden index f63aba5e9..b37d51735 100644 --- a/cmd/template/cluster/testdata/run_template_cluster_capa_8.golden +++ b/cmd/template/cluster/testdata/run_template_cluster_capa_8.golden @@ -23,6 +23,7 @@ data: description: just a test cluster name: test8 organization: test + preventDeletion: false nodePools: worker1: availabilityZones: