Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --prev-app to delete command #511

Merged
merged 6 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions pkg/kapp/cmd/app/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type DeleteOptions struct {
ResourceFilterFlags cmdtools.ResourceFilterFlags
ApplyFlags ApplyFlags
ResourceTypesFlags ResourceTypesFlags
PrevAppFlags PrevAppFlags
}

type changesSummary struct {
Expand All @@ -53,6 +54,7 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co
o.ResourceFilterFlags.Set(cmd)
o.ApplyFlags.SetWithDefaults("", ApplyFlagsDeleteDefaults, cmd)
o.ResourceTypesFlags.Set(cmd)
o.PrevAppFlags.Set(cmd)
return cmd
}

Expand All @@ -70,8 +72,28 @@ func (o *DeleteOptions) Run() error {
}

if !exists {
o.ui.PrintLinef("%s", notExistsMsg)
return nil
if o.PrevAppFlags.PrevAppName != "" {
o.AppFlags.Name = o.PrevAppFlags.PrevAppName

app, supportObjs, err = Factory(o.depsFactory, o.AppFlags, o.ResourceTypesFlags, o.logger)
if err != nil {
return err
}

prevAppExists, prevAppNotExistsMsg, err := app.Exists()
if err != nil {
return err
}

if !prevAppExists {
o.ui.PrintLinef("%s", prevAppNotExistsMsg)
o.ui.PrintLinef("%s", notExistsMsg)
return nil
}
} else {
o.ui.PrintLinef("%s", notExistsMsg)
return nil
}
}

usedGVs, err := app.UsedGVs()
Expand Down
6 changes: 4 additions & 2 deletions pkg/kapp/cmd/app/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type DeployOptions struct {
logger logger.Logger

AppFlags Flags
PrevAppFlags PrevAppFlags
FileFlags cmdtools.FileFlags
DiffFlags cmdtools.DiffFlags
ResourceFilterFlags cmdtools.ResourceFilterFlags
Expand Down Expand Up @@ -79,6 +80,7 @@ func NewDeployCmd(o *DeployOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co
o.DeployFlags.Set(cmd)
o.ResourceTypesFlags.Set(cmd)
o.LabelFlags.Set(cmd)
o.PrevAppFlags.Set(cmd)

return cmd
}
Expand All @@ -96,8 +98,8 @@ func (o *DeployOptions) Run() error {
return err
}

if o.DeployFlags.PrevApp != "" {
err = app.RenamePrevApp(o.DeployFlags.PrevApp, appLabels)
if o.PrevAppFlags.PrevAppName != "" {
err = app.RenamePrevApp(o.PrevAppFlags.PrevAppName, appLabels)
} else {
err = app.CreateOrUpdate(appLabels)
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/kapp/cmd/app/deploy_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type DeployFlags struct {
ExistingNonLabeledResourcesCheck bool
ExistingNonLabeledResourcesCheckConcurrency int
OverrideOwnershipOfExistingResources bool
PrevApp string

AppChangesMaxToKeep int

Expand All @@ -44,7 +43,6 @@ func (s *DeployFlags) Set(cmd *cobra.Command) {
100, "Concurrency to check for existing non-labeled resources")
cmd.Flags().BoolVar(&s.OverrideOwnershipOfExistingResources, "dangerous-override-ownership-of-existing-resources",
false, "Steal existing resources from another app")
cmd.Flags().StringVar(&s.PrevApp, "prev-app", "", "Rename existing app")

cmd.Flags().IntVar(&s.AppChangesMaxToKeep, "app-changes-max-to-keep", ctlapp.AppChangesMaxToKeepDefault, "Maximum number of app changes to keep")

Expand Down
16 changes: 16 additions & 0 deletions pkg/kapp/cmd/app/prev_app_flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2022 VMware, Inc.
neil-hickey marked this conversation as resolved.
Show resolved Hide resolved
neil-hickey marked this conversation as resolved.
Show resolved Hide resolved
// SPDX-License-Identifier: Apache-2.0

package app

import (
"github.com/spf13/cobra"
)

type PrevAppFlags struct {
PrevAppName string
}

func (s *PrevAppFlags) Set(cmd *cobra.Command) {
cmd.Flags().StringVar(&s.PrevAppName, "prev-app", "", "Set previous app name")
}
47 changes: 47 additions & 0 deletions test/e2e/create_update_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ data:
key: value
`

yaml3 := `
---
apiVersion: v1
kind: Service
metadata:
name: example-service
spec:
ports:
- port: 6381
targetPort: 6381
selector:
app: example-app
tier: backend
---
`

appName := "test-create-update-delete-prev-app"
prevAppName := "test-create-update-delete-prev-app-old"
cleanUp := func() {
Expand Down Expand Up @@ -244,6 +260,37 @@ data:
os.Setenv("KAPP_FQ_CONFIGMAP_NAMES", "True")
cleanUp()
})

logger.Section("delete migrated prevApp", func() {
os.Setenv("KAPP_FQ_CONFIGMAP_NAMES", "True")
kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", appName}, RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml1)})

kapp.Run([]string{"delete", "-a", "empty-app", "--prev-app", appName})

NewMissingClusterResource(t, "configmap", appName, env.Namespace, kubectl)
cleanUp()
})

logger.Section("delete unmigrated prevApp", func() {
kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", appName}, RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml1)})

kapp.Run([]string{"delete", "-a", "empty-app", "--prev-app", appName})

NewMissingClusterResource(t, "configmap", appName, env.Namespace, kubectl)
cleanUp()
})

// delete with --app and --prev-app existing should only delete --app and not --prev-app
logger.Section("delete ignores prevApp", func() {
kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", appName}, RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml1)})
kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", prevAppName}, RunOpts{IntoNs: true, StdinReader: strings.NewReader(yaml3)})

kapp.Run([]string{"delete", "-a", appName, "--prev-app", prevAppName})

NewMissingClusterResource(t, "configmap", appName, env.Namespace, kubectl)
NewPresentClusterResource("configmap", prevAppName, env.Namespace, kubectl)
cleanUp()
})
}

func TestCreateUpdateDelete_PrevApp_FQConfigmap_Enabled(t *testing.T) {
Expand Down