Skip to content

Commit

Permalink
Add deletion prevention label also to templated cluster's App and `…
Browse files Browse the repository at this point in the history
…ConfigMap` if using the `kubectl gs template --prevent-deletion` parameter (#1508)
  • Loading branch information
AndiDog authored Dec 12, 2024
1 parent 92c398b commit 9132319
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project's packages adheres to [Semantic Versioning](http://semver.org/s

## [Unreleased]

### Changed

- Add deletion prevention label also to templated cluster's `App` and `ConfigMap` if using the `kubectl gs template --prevent-deletion` parameter

## [4.6.0] - 2024-11-28

### Changed
Expand Down
2 changes: 1 addition & 1 deletion cmd/template/app/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) err
if userConfigMap.Labels == nil {
userConfigMap.Labels = map[string]string{}
}
userConfigMap.Labels[label.PreventDeletion] = "true"
userConfigMap.Labels[label.PreventDeletion] = "true" //nolint:goconst
}

userConfigConfigMapYaml, err = yaml.Marshal(userConfigMap)
Expand Down
2 changes: 1 addition & 1 deletion cmd/template/cluster/flags/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ type Flag struct {
func (f *Flag) Init(cmd *cobra.Command) {
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")
cmd.Flags().BoolVar(&f.PreventDeletion, flagPreventDeletion, false, "Prevent cluster from getting deleted (see https://docs.giantswarm.io/tutorials/fleet-management/deletion-prevention/)")

// AWS only.
cmd.Flags().StringVar(&f.AWS.AWSClusterRoleIdentityName, flagAWSClusterRoleIdentityName, "", "Name of the AWSClusterRoleIdentity that will be used for cluster creation.")
Expand Down
8 changes: 8 additions & 0 deletions cmd/template/cluster/provider/capa.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"sigs.k8s.io/yaml"

gsannotation "github.com/giantswarm/k8smetadata/pkg/annotation"
"github.com/giantswarm/k8smetadata/pkg/label"
k8smetadata "github.com/giantswarm/k8smetadata/pkg/label"

"github.com/giantswarm/kubectl-gs/v5/cmd/template/cluster/common"
Expand Down Expand Up @@ -217,6 +218,9 @@ func templateClusterCAPA(ctx context.Context, k8sClient k8sclient.Interface, out

userConfigMap.Labels = map[string]string{}
userConfigMap.Labels[k8smetadata.Cluster] = config.Name
if config.PreventDeletion {
userConfigMap.Labels[label.PreventDeletion] = "true" //nolint:goconst
}

configMapYAML, err = yaml.Marshal(userConfigMap)
if err != nil {
Expand All @@ -233,6 +237,10 @@ func templateClusterCAPA(ctx context.Context, k8sClient k8sclient.Interface, out
Name: ClusterAWSRepoName,
Namespace: common.OrganizationNamespace(config.Organization),
UserConfigConfigMapName: configMapName,
ExtraLabels: map[string]string{},
}
if config.PreventDeletion {
clusterAppConfig.ExtraLabels[label.PreventDeletion] = "true"
}

var err error
Expand Down
8 changes: 8 additions & 0 deletions cmd/template/cluster/provider/capg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"text/template"

"github.com/giantswarm/k8sclient/v8/pkg/k8sclient"
"github.com/giantswarm/k8smetadata/pkg/label"
k8smetadata "github.com/giantswarm/k8smetadata/pkg/label"
"github.com/giantswarm/microerror"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -56,6 +57,9 @@ func templateClusterGCP(ctx context.Context, k8sClient k8sclient.Interface, outp

userConfigMap.Labels = map[string]string{}
userConfigMap.Labels[k8smetadata.Cluster] = config.Name
if config.PreventDeletion {
userConfigMap.Labels[label.PreventDeletion] = "true" //nolint:goconst
}

configMapYAML, err = yaml.Marshal(userConfigMap)
if err != nil {
Expand All @@ -82,6 +86,10 @@ func templateClusterGCP(ctx context.Context, k8sClient k8sclient.Interface, outp
Namespace: common.OrganizationNamespace(config.Organization),
Version: appVersion,
UserConfigConfigMapName: configMapName,
ExtraLabels: map[string]string{},
}
if config.PreventDeletion {
clusterAppConfig.ExtraLabels[label.PreventDeletion] = "true"
}

var err error
Expand Down
8 changes: 8 additions & 0 deletions cmd/template/cluster/provider/capv.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/giantswarm/microerror"
"sigs.k8s.io/yaml"

"github.com/giantswarm/k8smetadata/pkg/label"
k8smetadata "github.com/giantswarm/k8smetadata/pkg/label"

applicationv1alpha1 "github.com/giantswarm/apiextensions-application/api/v1alpha1"
Expand Down Expand Up @@ -81,6 +82,9 @@ func templateClusterVSphere(output io.Writer, config common.ClusterConfig, appVe

userConfigMap.Labels = map[string]string{}
userConfigMap.Labels[k8smetadata.Cluster] = config.Name
if config.PreventDeletion {
userConfigMap.Labels[label.PreventDeletion] = "true" //nolint:goconst
}

configMapYAML, err = yaml.Marshal(userConfigMap)
if err != nil {
Expand Down Expand Up @@ -109,6 +113,10 @@ func templateClusterVSphere(output io.Writer, config common.ClusterConfig, appVe
UserConfigConfigMapName: configMapName,
UserConfigSecretName: config.VSphere.CredentialsSecretName,
ExtraConfigs: extraConfigs,
ExtraLabels: map[string]string{},
}
if config.PreventDeletion {
clusterAppConfig.ExtraLabels[label.PreventDeletion] = "true"
}

var err error
Expand Down
8 changes: 8 additions & 0 deletions cmd/template/cluster/provider/capvcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/giantswarm/microerror"
"sigs.k8s.io/yaml"

"github.com/giantswarm/k8smetadata/pkg/label"
k8smetadata "github.com/giantswarm/k8smetadata/pkg/label"

applicationv1alpha1 "github.com/giantswarm/apiextensions-application/api/v1alpha1"
Expand Down Expand Up @@ -65,6 +66,9 @@ func templateClusterCloudDirector(output io.Writer, config common.ClusterConfig,

userConfigMap.Labels = map[string]string{}
userConfigMap.Labels[k8smetadata.Cluster] = config.Name
if config.PreventDeletion {
userConfigMap.Labels[label.PreventDeletion] = "true" //nolint:goconst
}

configMapYAML, err = yaml.Marshal(userConfigMap)
if err != nil {
Expand Down Expand Up @@ -92,6 +96,10 @@ func templateClusterCloudDirector(output io.Writer, config common.ClusterConfig,
Version: appVersion,
UserConfigConfigMapName: configMapName,
ExtraConfigs: extraConfigs,
ExtraLabels: map[string]string{},
}
if config.PreventDeletion {
clusterAppConfig.ExtraLabels[label.PreventDeletion] = "true"
}

var err error
Expand Down
8 changes: 8 additions & 0 deletions cmd/template/cluster/provider/capz.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/giantswarm/microerror"
"sigs.k8s.io/yaml"

"github.com/giantswarm/k8smetadata/pkg/label"
k8smetadata "github.com/giantswarm/k8smetadata/pkg/label"

"github.com/giantswarm/kubectl-gs/v5/cmd/template/cluster/common"
Expand Down Expand Up @@ -54,6 +55,9 @@ func templateClusterCAPZ(ctx context.Context, k8sClient k8sclient.Interface, out

userConfigMap.Labels = map[string]string{}
userConfigMap.Labels[k8smetadata.Cluster] = config.Name
if config.PreventDeletion {
userConfigMap.Labels[label.PreventDeletion] = "true" //nolint:goconst
}

configMapYAML, err = yaml.Marshal(userConfigMap)
if err != nil {
Expand All @@ -70,6 +74,10 @@ func templateClusterCAPZ(ctx context.Context, k8sClient k8sclient.Interface, out
Name: ClusterAzureRepoName,
Namespace: common.OrganizationNamespace(config.Organization),
UserConfigConfigMapName: configMapName,
ExtraLabels: map[string]string{},
}
if config.PreventDeletion {
clusterAppConfig.ExtraLabels[label.PreventDeletion] = "true"
}

var err error
Expand Down
8 changes: 8 additions & 0 deletions cmd/template/cluster/provider/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/giantswarm/microerror"
"sigs.k8s.io/yaml"

"github.com/giantswarm/k8smetadata/pkg/label"
k8smetadata "github.com/giantswarm/k8smetadata/pkg/label"

"github.com/giantswarm/kubectl-gs/v5/cmd/template/cluster/common"
Expand Down Expand Up @@ -61,6 +62,9 @@ func templateClusterEKS(ctx context.Context, k8sClient k8sclient.Interface, outp

userConfigMap.Labels = map[string]string{}
userConfigMap.Labels[k8smetadata.Cluster] = config.Name
if config.PreventDeletion {
userConfigMap.Labels[label.PreventDeletion] = "true" //nolint:goconst
}

configMapYAML, err = yaml.Marshal(userConfigMap)
if err != nil {
Expand All @@ -87,6 +91,10 @@ func templateClusterEKS(ctx context.Context, k8sClient k8sclient.Interface, outp
Namespace: common.OrganizationNamespace(config.Organization),
Version: appVersion,
UserConfigConfigMapName: configMapName,
ExtraLabels: map[string]string{},
}
if config.PreventDeletion {
clusterAppConfig.ExtraLabels[label.PreventDeletion] = "true"
}

var err error
Expand Down

0 comments on commit 9132319

Please sign in to comment.