Skip to content

Commit

Permalink
add --filter-* to delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
cppforlife committed Sep 12, 2019
1 parent 82cac6a commit 4d751de
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
29 changes: 25 additions & 4 deletions pkg/kapp/cmd/app/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ type DeleteOptions struct {
ui ui.UI
depsFactory cmdcore.DepsFactory

AppFlags AppFlags
DiffFlags cmdtools.DiffFlags
ApplyFlags ApplyFlags
AppFlags AppFlags
DiffFlags cmdtools.DiffFlags
ResourceFilterFlags cmdtools.ResourceFilterFlags
ApplyFlags ApplyFlags
}

func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory) *DeleteOptions {
Expand All @@ -31,6 +32,7 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co
}
o.AppFlags.Set(cmd, flagsFactory)
o.DiffFlags.SetWithPrefix("diff", cmd)
o.ResourceFilterFlags.Set(cmd)
o.ApplyFlags.SetWithDefaults("", ApplyFlagsDeleteDefaults, cmd)
return cmd
}
Expand All @@ -57,11 +59,26 @@ func (o *DeleteOptions) Run() error {
return err
}

resourceFilter, err := o.ResourceFilterFlags.ResourceFilter()
if err != nil {
return err
}

existingResources, err := identifiedResources.List(labelSelector)
if err != nil {
return err
}

fullyDeleteApp := true
applicableExistingResources := resourceFilter.Apply(existingResources)

if len(applicableExistingResources) != len(existingResources) {
fullyDeleteApp = false
o.ui.PrintLinef("App '%s' (namespace: %s) will not be fully deleted because some resources are excluded by filters",
app.Name(), o.AppFlags.NamespaceFlags.Name)
}

existingResources = applicableExistingResources
changeFactory := ctldiff.NewChangeFactory(nil)

changes, err := ctldiff.NewChangeSet(existingResources, nil, o.DiffFlags.ChangeSetOpts, changeFactory).Calculate()
Expand Down Expand Up @@ -97,6 +114,10 @@ func (o *DeleteOptions) Run() error {
return err
}

return app.Delete()
if fullyDeleteApp {
return app.Delete()
}

return nil
})
}
2 changes: 1 addition & 1 deletion pkg/kapp/cmd/tools/resource_filter_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (s *ResourceFilterFlags) Set(cmd *cobra.Command) {
cmd.Flags().StringSliceVar(&s.rf.KindNamespaces, "filter-kind-ns", nil, "Set kind-namespace filter (example: Pod/, Pod/knative-serving) (can repeat)")
cmd.Flags().StringSliceVar(&s.rf.KindNsNames, "filter-kind-ns-name", nil, "Set kind-namespace-name filter (example: Deployment/knative-serving/controller) (can repeat)")

cmd.Flags().StringVar(&s.bf, "filter", "", `Set filter (example: {"and":[{"not":{"match":{kind":"foo%"}}},{"kind":"!foo"}]})`)
cmd.Flags().StringVar(&s.bf, "filter", "", `Set filter (example: {"and":[{"not":{"resource":{"kind":"foo%"}}},{"resource":{"kind":"!foo"}}]})`)
}

func (s *ResourceFilterFlags) ResourceFilter() (ctlres.ResourceFilter, error) {
Expand Down

0 comments on commit 4d751de

Please sign in to comment.