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

Avoid listing labeled resources for newly created app #659

Merged
merged 3 commits into from
Jan 19, 2023
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
2 changes: 1 addition & 1 deletion pkg/kapp/app/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type App interface {
UsedGKs() (*[]schema.GroupKind, error)
UpdateUsedGVsAndGKs([]schema.GroupVersion, []schema.GroupKind) error

CreateOrUpdate(map[string]string, bool) error
CreateOrUpdate(map[string]string, bool) (bool, error)
Exists() (bool, string, error)
Delete() error
Rename(string, string) error
Expand Down
6 changes: 4 additions & 2 deletions pkg/kapp/app/labeled_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ 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, isDiffRun bool) error { return nil }
func (a *LabeledApp) Exists() (bool, string, error) { return true, "", nil }
func (a *LabeledApp) CreateOrUpdate(labels map[string]string, isDiffRun bool) (bool, error) {
return false, nil
}
func (a *LabeledApp) Exists() (bool, string, error) { return true, "", nil }

func (a *LabeledApp) Delete() error {
labelSelector, err := a.LabelSelector()
Expand Down
14 changes: 7 additions & 7 deletions pkg/kapp/app/recorded_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,32 +127,32 @@ func (a *RecordedApp) UpdateUsedGVsAndGKs(gvs []schema.GroupVersion, gks []schem
})
}

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

app, foundMigratedApp, err := a.find(a.fqName())
if err != nil {
return err
return false, err
}

if foundMigratedApp {
a.isMigrated = true
return a.updateApp(app, labels)
return false, a.updateApp(app, labels)
}

app, foundNonMigratedApp, err := a.find(a.name)
if err != nil {
return err
return false, err
}

if foundNonMigratedApp {
if a.isMigrationEnabled() {
return a.migrate(app, labels, a.fqName())
return false, a.migrate(app, labels, a.fqName())
}
return a.updateApp(app, labels)
return false, a.updateApp(app, labels)
}

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

func (a *RecordedApp) find(name string) (*corev1.ConfigMap, bool, error) {
Expand Down
8 changes: 5 additions & 3 deletions pkg/kapp/cmd/app/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func NewDeployCmd(o *DeployOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co
}

func (o *DeployOptions) Run() error {
var isNewApp bool
failingAPIServicesPolicy := o.ResourceTypesFlags.FailingAPIServicePolicy()

app, supportObjs, err := Factory(o.depsFactory, o.AppFlags, o.ResourceTypesFlags, o.logger)
Expand All @@ -107,7 +108,7 @@ func (o *DeployOptions) Run() error {
if o.PrevAppFlags.PrevAppName != "" {
err = app.RenamePrevApp(o.PrevAppFlags.PrevAppName, appLabels, o.DiffFlags.Run)
} else {
err = app.CreateOrUpdate(appLabels, o.DiffFlags.Run)
isNewApp, err = app.CreateOrUpdate(appLabels, o.DiffFlags.Run)
}

if err != nil {
Expand Down Expand Up @@ -158,7 +159,7 @@ func (o *DeployOptions) Run() error {
}

existingResources, existingPodRs, err := o.existingResources(
newResources, labeledResources, resourceFilter, supportObjs.Apps, usedGKs, append(meta.LastChange.Namespaces, nsNames...))
newResources, labeledResources, resourceFilter, supportObjs.Apps, usedGKs, append(meta.LastChange.Namespaces, nsNames...), isNewApp)
if err != nil {
return err
}
Expand Down Expand Up @@ -352,7 +353,7 @@ func (o *DeployOptions) newResourcesFromFiles() ([]ctlres.Resource, error) {

func (o *DeployOptions) existingResources(newResources []ctlres.Resource,
labeledResources *ctlres.LabeledResources, resourceFilter ctlres.ResourceFilter,
apps ctlapp.Apps, usedGKs []schema.GroupKind, resourceNamespaces []string) ([]ctlres.Resource, []ctlres.Resource, error) {
apps ctlapp.Apps, usedGKs []schema.GroupKind, resourceNamespaces []string, isNewApp bool) ([]ctlres.Resource, []ctlres.Resource, error) {

labelErrorResolutionFunc := func(key string, val string) string {
items, _ := apps.List(nil)
Expand All @@ -369,6 +370,7 @@ func (o *DeployOptions) existingResources(newResources []ctlres.Resource,
ExistingNonLabeledResourcesCheck: o.DeployFlags.ExistingNonLabeledResourcesCheck,
ExistingNonLabeledResourcesCheckConcurrency: o.DeployFlags.ExistingNonLabeledResourcesCheckConcurrency,
SkipResourceOwnershipCheck: o.DeployFlags.OverrideOwnershipOfExistingResources,
IsNewApp: isNewApp,

// Prevent accidently overriding kapp state records
DisallowedResourcesByLabelKeys: []string{ctlapp.KappIsAppLabelKey},
Expand Down
16 changes: 12 additions & 4 deletions pkg/kapp/resources/labeled_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type AllAndMatchingOpts struct {
ExistingNonLabeledResourcesCheck bool
ExistingNonLabeledResourcesCheckConcurrency int
SkipResourceOwnershipCheck bool
IsNewApp bool

DisallowedResourcesByLabelKeys []string
LabelErrorResolutionFunc func(string, string) string
Expand All @@ -107,15 +108,22 @@ type AllAndMatchingOpts struct {
func (a *LabeledResources) AllAndMatching(newResources []Resource, opts AllAndMatchingOpts) ([]Resource, error) {
defer a.logger.DebugFunc("AllAndMatching").Finish()

resources, err := a.All(opts.IdentifiedResourcesListOpts)
if err != nil {
return nil, err
var (
resources []Resource
err error
)

// avoid listing labeled resources for newly created app
if !opts.IsNewApp {
resources, err = a.All(opts.IdentifiedResourcesListOpts)
if err != nil {
return nil, err
}
}

var nonLabeledResources []Resource

if opts.ExistingNonLabeledResourcesCheck {
var err error
nonLabeledResources, err = a.findNonLabeledResources(
resources, newResources, opts.ExistingNonLabeledResourcesCheckConcurrency)
if err != nil {
Expand Down
92 changes: 92 additions & 0 deletions test/e2e/create_update_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,95 @@ data:
cleanUp()
})
}

func TestAppDeploy_With_Existing_OR_New_Res(t *testing.T) {
100mik marked this conversation as resolved.
Show resolved Hide resolved
env := BuildEnv(t)
logger := Logger{}
kapp := Kapp{t, env.Namespace, env.KappBinaryPath, logger}
kubectl := Kubectl{t, env.Namespace, logger}

yaml1 := `
---
apiVersion: v1
kind: ConfigMap
metadata:
name: cm1
data:
key: value
`
yaml2 := `
---
apiVersion: v1
kind: ConfigMap
metadata:
name: cm2
data:
key: value
`
yaml3 := `
---
apiVersion: v1
kind: ConfigMap
metadata:
name: cm2
data:
key: value2
`

name := "test-app-deploy-with-existing-res"
name2 := "test-app-deploy-with-new-res"
cleanUp := func() {
kapp.Run([]string{"delete", "-a", name})
kapp.Run([]string{"delete", "-a", name2})
}

cleanUp()
defer cleanUp()

kubectl.RunWithOpts([]string{"apply", "-f", "-"},
RunOpts{StdinReader: strings.NewReader(yaml1)})
NewPresentClusterResource("configmap", "cm1", env.Namespace, kubectl)

logger.Section("deploy app with existing resource", func() {
out, _ := kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name, "--diff-changes"},
RunOpts{StdinReader: strings.NewReader(yaml1)})

expectedOutput := `
@@ update configmap/cm1 (v1) namespace: kapp-test @@
...
4, 4 metadata:
5 - annotations:
6 - kubectl.kubernetes.io/last-applied-configuration: |
7 - {"apiVersion":"v1","data":{"key":"value"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm1","namespace":"kapp-test"}}
8, 5 creationTimestamp: "2006-01-02T15:04:05Z07:00"
6 + labels:
7 + kapp.k14s.io/app: "-replaced-"
8 + kapp.k14s.io/association: -replaced-
`
out = replaceTimestampWithDfaultValue(out)
out = replaceLabelValues(out)
require.Contains(t, out, expectedOutput, "output does not match")
})

logger.Section("deploy app with all new resources", func() {
kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name2},
RunOpts{StdinReader: strings.NewReader(yaml2)})
NewPresentClusterResource("configmap", "cm2", env.Namespace, kubectl)
})

logger.Section("deploy again by updating resource", func() {
out, _ := kapp.RunWithOpts([]string{"deploy", "-f", "-", "-a", name2, "--diff-changes"},
RunOpts{StdinReader: strings.NewReader(yaml3)})

expectedOutput := `
@@ update configmap/cm2 (v1) namespace: kapp-test @@
...
1, 1 data:
2 - key: value
2 + key: value2
3, 3 kind: ConfigMap
4, 4 metadata:
`
require.Contains(t, out, expectedOutput, "output does not match")
100mik marked this conversation as resolved.
Show resolved Hide resolved
})
}