Skip to content

Commit

Permalink
Do not create kapp app configmap while using --diff-run (#454)
Browse files Browse the repository at this point in the history
* not to create configmap while diff-run

* taking care of configmap with suffix

* added e2e test case

* added newline at end of file

* rebased and refactored after prev-app changes

* rebase after final app-suffix changes

* refactored code and renamed test method

* don't update configmap while diff-run

* Revert "don't update configmap while diff-run"

This reverts commit 9ecf7ac.

* added missing arguments
  • Loading branch information
sethiyash authored May 16, 2022
1 parent 4a6a3a6 commit 77ce86d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pkg/kapp/app/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ type App interface {
UsedGKs() (*[]schema.GroupKind, error)
UpdateUsedGVsAndGKs([]schema.GroupVersion, []schema.GroupKind) error

CreateOrUpdate(map[string]string) error
CreateOrUpdate(map[string]string, bool) error
Exists() (bool, string, error)
Delete() error
Rename(string, string) error
RenamePrevApp(string, map[string]string) error
RenamePrevApp(string, map[string]string, bool) error

// Sorted as first is oldest
Changes() ([]Change, error)
Expand Down
6 changes: 3 additions & 3 deletions pkg/kapp/app/labeled_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (a *LabeledApp) UsedGVs() ([]schema.GroupVersion, error)
func (a *LabeledApp) UsedGKs() (*[]schema.GroupKind, error) { return nil, nil }
func (a *LabeledApp) UpdateUsedGVsAndGKs([]schema.GroupVersion, []schema.GroupKind) error { return nil }

func (a *LabeledApp) CreateOrUpdate(labels map[string]string) error { return nil }
func (a *LabeledApp) Exists() (bool, string, error) { return true, "", nil }
func (a *LabeledApp) CreateOrUpdate(labels map[string]string, isDiffRun bool) error { return nil }
func (a *LabeledApp) Exists() (bool, string, error) { return true, "", nil }

func (a *LabeledApp) Delete() error {
labelSelector, err := a.LabelSelector()
Expand All @@ -70,7 +70,7 @@ func (a *LabeledApp) Delete() error {
}

func (a *LabeledApp) Rename(_ string, _ string) error { return fmt.Errorf("Not supported") }
func (a *LabeledApp) RenamePrevApp(_ string, _ map[string]string) error {
func (a *LabeledApp) RenamePrevApp(_ string, _ map[string]string, _ bool) error {
return fmt.Errorf("Not supported")
}

Expand Down
18 changes: 12 additions & 6 deletions pkg/kapp/app/recorded_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (a *RecordedApp) UpdateUsedGVsAndGKs(gvs []schema.GroupVersion, gks []schem
})
}

func (a *RecordedApp) CreateOrUpdate(labels map[string]string) error {
func (a *RecordedApp) CreateOrUpdate(labels map[string]string, isDiffRun bool) error {
defer a.logger.DebugFunc("CreateOrUpdate").Finish()

app, foundMigratedApp, err := a.find(a.fqName())
Expand All @@ -152,7 +152,7 @@ func (a *RecordedApp) CreateOrUpdate(labels map[string]string) error {
return a.updateApp(app, labels)
}

return a.create(labels)
return a.create(labels, isDiffRun)
}

func (a *RecordedApp) find(name string) (*corev1.ConfigMap, bool, error) {
Expand All @@ -166,7 +166,7 @@ func (a *RecordedApp) find(name string) (*corev1.ConfigMap, bool, error) {
return cm, true, nil
}

func (a *RecordedApp) create(labels map[string]string) error {
func (a *RecordedApp) create(labels map[string]string, isDiffRun bool) error {
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: a.name,
Expand Down Expand Up @@ -197,7 +197,13 @@ func (a *RecordedApp) create(labels map[string]string) error {
return err
}

_, err = a.coreClient.CoreV1().ConfigMaps(a.nsName).Create(context.TODO(), configMap, metav1.CreateOptions{})
createOpts := metav1.CreateOptions{}
if isDiffRun {
createOpts.DryRun = []string{metav1.DryRunAll}
}
app, err := a.coreClient.CoreV1().ConfigMaps(a.nsName).Create(context.TODO(), configMap, createOpts)

a.setMeta(*app)

return err
}
Expand All @@ -216,7 +222,7 @@ func (a *RecordedApp) updateApp(existingConfigMap *corev1.ConfigMap, labels map[
return nil
}

func (a *RecordedApp) RenamePrevApp(prevAppName string, labels map[string]string) error {
func (a *RecordedApp) RenamePrevApp(prevAppName string, labels map[string]string, isDiffRun bool) error {
defer a.logger.DebugFunc("RenamePrevApp").Finish()

app, foundMigratedApp, err := a.find(a.fqName())
Expand Down Expand Up @@ -263,7 +269,7 @@ func (a *RecordedApp) RenamePrevApp(prevAppName string, labels map[string]string
return a.renameConfigMap(app, a.name, a.nsName)
}

return a.create(labels)
return a.create(labels, isDiffRun)
}

func (a *RecordedApp) migrate(c *corev1.ConfigMap, labels map[string]string, newName string) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kapp/cmd/app/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ func (o *DeployOptions) Run() error {
}

if o.PrevAppFlags.PrevAppName != "" {
err = app.RenamePrevApp(o.PrevAppFlags.PrevAppName, appLabels)
err = app.RenamePrevApp(o.PrevAppFlags.PrevAppName, appLabels, o.DiffFlags.Run)
} else {
err = app.CreateOrUpdate(appLabels)
err = app.CreateOrUpdate(appLabels, o.DiffFlags.Run)
}

if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions test/e2e/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,30 @@ data:

require.Containsf(t, out, expectedOutput, "Did not find expected diff output")
}

func TestDiffRun(t *testing.T) {
env := BuildEnv(t)
kapp := Kapp{t, env.Namespace, env.KappBinaryPath, Logger{}}
kubectl := Kubectl{t, env.Namespace, Logger{}}

name := "not-create-configmap-diff-run"
cleanUp := func() {
kapp.Run([]string{"delete", "-a", name})
}

cleanUp()
defer cleanUp()

yaml1 := `
apiVersion: v1
kind: ConfigMap
metadata:
name: config
`

_, _ = kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name,
"--diff-run"},
RunOpts{IntoNs: true, AllowError: true, StdinReader: strings.NewReader(yaml1)})

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

0 comments on commit 77ce86d

Please sign in to comment.