Skip to content

Commit

Permalink
Support deletion prevention for kubectl gs template app command (#1124
Browse files Browse the repository at this point in the history
)
  • Loading branch information
AndiDog authored Oct 10, 2023
1 parent 1ff0220 commit 2e3b293
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 5 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]

### Added

- Support deletion prevention for `kubectl gs template app` command

## [2.42.0] - 2023-10-06

### Fixed
Expand Down
1 change: 1 addition & 0 deletions cmd/login/runner_config_modify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func TestKubeConfigModification(t *testing.T) {
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
configDir, err := os.MkdirTemp("", "loginTest")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/login/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ func TestLogin(t *testing.T) {
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
configDir, err := os.MkdirTemp("", "loginTest")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/template/app/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func New(config Config) (*cobra.Command, error) {
Use: name,
Short: description,
Long: description,
Args: cobra.NoArgs,
RunE: r.Run,
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/template/app/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

"github.com/giantswarm/k8smetadata/pkg/label"
"github.com/giantswarm/microerror"
"github.com/spf13/cobra"

Expand All @@ -26,6 +27,7 @@ const (
flagNamespaceConfigAnnotations = "namespace-annotations"
flagNamespaceConfigLabels = "namespace-labels"
flagOrganization = "organization"
flagPreventDeletion = "prevent-deletion"
flagRollbackTimeout = "rollback-timeout"
flagUninstallTimeout = "uninstall-timeout"
flagUpgradeTimeout = "upgrade-timeout"
Expand Down Expand Up @@ -55,6 +57,7 @@ type flag struct {
UpgradeTimeout time.Duration
flagUserConfigMap string
flagUserSecret string
PreventDeletion bool
}

func (f *flag) Init(cmd *cobra.Command) {
Expand All @@ -78,6 +81,7 @@ func (f *flag) Init(cmd *cobra.Command) {
cmd.Flags().StringVar(&f.Version, flagVersion, "", "App version to be installed.")
cmd.Flags().StringSliceVar(&f.flagNamespaceConfigAnnotations, flagNamespaceConfigAnnotations, nil, "Namespace configuration annotations in form key=value.")
cmd.Flags().StringSliceVar(&f.flagNamespaceConfigLabels, flagNamespaceConfigLabels, nil, "Namespace configuration labels in form key=value.")
cmd.Flags().BoolVar(&f.PreventDeletion, flagPreventDeletion, false, fmt.Sprintf("Label the App and other templated resources with '%s' to prevent deletion (see https://docs.giantswarm.io/advanced/deletion-prevention/).", label.PreventDeletion))

_ = cmd.Flags().MarkDeprecated(flagNamespace, fmt.Sprintf("use --%s instead", flagTargetNamespace))
_ = cmd.Flags().MarkDeprecated(flagCluster, fmt.Sprintf("use --%s instead", flagClusterName))
Expand Down
20 changes: 20 additions & 0 deletions cmd/template/app/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"text/template"

"github.com/giantswarm/k8smetadata/pkg/label"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -73,6 +74,7 @@ func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) err
CatalogNamespace: r.flag.CatalogNamespace,
Cluster: clusterName,
DefaultingEnabled: r.flag.DefaultingEnabled,
ExtraLabels: map[string]string{},
InCluster: r.flag.InCluster,
Name: r.flag.Name,
Namespace: targetNamespace,
Expand Down Expand Up @@ -110,6 +112,13 @@ func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) err
}
appConfig.UserConfigSecretName = userSecret.GetName()

if r.flag.PreventDeletion {
if userSecret.Labels == nil {
userSecret.Labels = map[string]string{}
}
userSecret.Labels[label.PreventDeletion] = "true" //nolint:goconst
}

userConfigSecretYaml, err = yaml.Marshal(userSecret)
if err != nil {
return microerror.Mask(err)
Expand All @@ -129,6 +138,13 @@ func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) err
}
appConfig.UserConfigConfigMapName = userConfigMap.GetName()

if r.flag.PreventDeletion {
if userConfigMap.Labels == nil {
userConfigMap.Labels = map[string]string{}
}
userConfigMap.Labels[label.PreventDeletion] = "true"
}

userConfigConfigMapYaml, err = yaml.Marshal(userConfigMap)
if err != nil {
return microerror.Mask(err)
Expand All @@ -147,6 +163,10 @@ func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) err
}
appConfig.NamespaceConfigLabels = namespaceLabels

if r.flag.PreventDeletion {
appConfig.ExtraLabels[label.PreventDeletion] = "true"
}

r.setTimeouts(&appConfig)

appCRYaml, err := templateapp.NewAppCR(appConfig)
Expand Down
2 changes: 1 addition & 1 deletion cmd/template/cluster/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const (
flagOpenStackDNSNameservers = "dns-nameservers"
flagOpenStackExternalNetworkID = "external-network-id"
flagOpenStackNodeCIDR = "node-cidr"
flagOpenStackBastionBootFromVolume = "bastion-boot-from-volume"
flagOpenStackBastionBootFromVolume = "bastion-boot-from-volume" //nolint:gosec
flagOpenStackNetworkName = "network-name"
flagOpenStackSubnetName = "subnet-name"
flagOpenStackBastionDiskSize = "bastion-disk-size"
Expand Down
2 changes: 1 addition & 1 deletion cmd/template/nodepool/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
// Azure only.
flagAzureVMSize = "azure-vm-size"
flagAzureUseSpotVMs = "azure-spot-vms"
flagAzureSpotVMsMaxPrice = "azure-spot-vms-max-price"
flagAzureSpotVMsMaxPrice = "azure-spot-vms-max-price" //nolint:gosec

// Common.
flagAvailabilityZones = "availability-zones"
Expand Down
1 change: 1 addition & 0 deletions cmd/update/app/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func Test_run(t *testing.T) {
}

for i, tc := range testCases {
tc := tc
t.Run(fmt.Sprintf("case %d: %s", i, tc.name), func(t *testing.T) {
var err error

Expand Down
1 change: 1 addition & 0 deletions cmd/update/cluster/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func Test_run(t *testing.T) {
}

for i, tc := range testCases {
tc := tc
t.Run(fmt.Sprintf("case %d: %s", i, tc.name), func(t *testing.T) {
var err error

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/giantswarm/appcatalog v0.10.1
github.com/giantswarm/backoff v1.0.0
github.com/giantswarm/k8sclient/v7 v7.0.1
github.com/giantswarm/k8smetadata v0.22.0
github.com/giantswarm/k8smetadata v0.23.0
github.com/giantswarm/microerror v0.4.0
github.com/giantswarm/micrologger v1.0.0
github.com/giantswarm/organization-operator v1.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ github.com/giantswarm/backoff v1.0.0 h1:1oeTvyPsm1tJrHlSmfxbIWuoCNWPOkWJCb8kfLvE
github.com/giantswarm/backoff v1.0.0/go.mod h1:l/WqbggvG5Ndxxws0LUgVEvP5E82Qj5/PF8SMip/1QM=
github.com/giantswarm/k8sclient/v7 v7.0.1 h1:UmRwgsw5Uda27tpIblPo7nWjp/nq5qwqxEPHWcvzsHk=
github.com/giantswarm/k8sclient/v7 v7.0.1/go.mod h1:zJTXammjLHSiukMIO4+a6eUDgzj/lJxEXFZ22mC0WXc=
github.com/giantswarm/k8smetadata v0.22.0 h1:hTDM61G/vbyCPTo16bz3tTb+/Jg77kkEcUWKj6qZP4o=
github.com/giantswarm/k8smetadata v0.22.0/go.mod h1:QiQAyaZnwco1U0lENLF0Kp4bSN4dIPwIlHWEvUo3ES8=
github.com/giantswarm/k8smetadata v0.23.0 h1:iGwa1Nb45Sfcd5wqJEKBvxY1u5yXFg3Sq5Fw62nyRGA=
github.com/giantswarm/k8smetadata v0.23.0/go.mod h1:QiQAyaZnwco1U0lENLF0Kp4bSN4dIPwIlHWEvUo3ES8=
github.com/giantswarm/microerror v0.4.0 h1:QeU+UZL0rRlVXKqYOHMxS0L7g8UD+dn84NT7myWVh4U=
github.com/giantswarm/microerror v0.4.0/go.mod h1:Ju1YdC6TX/8witv7fIlkgiRr5FQUNyq3f4TX2QYnO7c=
github.com/giantswarm/micrologger v1.0.0 h1:5W4h3K414CXhoXQ3rkGbrMl8yUGdbCLKMdg/EezccM4=
Expand Down
17 changes: 17 additions & 0 deletions pkg/template/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ func Test_NewAppCR(t *testing.T) {
},
expectedGoldenFile: "app_config_cluster_values_yaml_output.golden",
},
{
name: "case 9: user values + extra labels",
config: Config{
AppName: "ingress-nginx",
Catalog: "giantswarm",
Cluster: "eggs2",
DefaultingEnabled: true,
ExtraLabels: map[string]string{
"giantswarm.io/prevent-deletion": "true",
},
Name: "ingress-nginx",
Namespace: "kube-system",
UserConfigConfigMapName: "ingress-nginx-user-values",
Version: "3.0.0",
},
expectedGoldenFile: "app_user_values_and_extra_labels_yaml_output.golden",
},
}

for i, tc := range testCases {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: application.giantswarm.io/v1alpha1
kind: App
metadata:
labels:
giantswarm.io/prevent-deletion: "true"
name: ingress-nginx
namespace: eggs2
spec:
catalog: giantswarm
kubeConfig:
inCluster: false
name: ingress-nginx
namespace: kube-system
userConfig:
configMap:
name: ingress-nginx-user-values
namespace: eggs2
version: 3.0.0

0 comments on commit 2e3b293

Please sign in to comment.