-
Notifications
You must be signed in to change notification settings - Fork 110
/
delete.go
287 lines (234 loc) · 7.9 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package app
import (
"github.com/cppforlife/go-cli-ui/ui"
ctlapp "github.com/k14s/kapp/pkg/kapp/app"
ctlcap "github.com/k14s/kapp/pkg/kapp/clusterapply"
cmdcore "github.com/k14s/kapp/pkg/kapp/cmd/core"
cmdtools "github.com/k14s/kapp/pkg/kapp/cmd/tools"
ctlconf "github.com/k14s/kapp/pkg/kapp/config"
ctldiff "github.com/k14s/kapp/pkg/kapp/diff"
ctldgraph "github.com/k14s/kapp/pkg/kapp/diffgraph"
ctldiffui "github.com/k14s/kapp/pkg/kapp/diffui"
"github.com/k14s/kapp/pkg/kapp/logger"
ctlres "github.com/k14s/kapp/pkg/kapp/resources"
"github.com/spf13/cobra"
)
type DeleteOptions struct {
ui ui.UI
depsFactory cmdcore.DepsFactory
logger logger.Logger
AppFlags Flags
DiffFlags cmdtools.DiffFlags
ResourceFilterFlags cmdtools.ResourceFilterFlags
ApplyFlags ApplyFlags
ResourceTypesFlags ResourceTypesFlags
PrevAppFlags PrevAppFlags
}
type changesSummary struct {
HasNoChanges bool
SkippedChanges bool
}
func NewDeleteOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger) *DeleteOptions {
return &DeleteOptions{ui: ui, depsFactory: depsFactory, logger: logger}
}
func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete app",
RunE: func(_ *cobra.Command, _ []string) error { return o.Run() },
Annotations: map[string]string{
cmdcore.AppHelpGroup.Key: cmdcore.AppHelpGroup.Value,
TTYByDefaultKey: "",
},
}
o.AppFlags.Set(cmd, flagsFactory)
o.DiffFlags.SetWithPrefix("diff", cmd)
o.ResourceFilterFlags.Set(cmd)
o.ApplyFlags.SetWithDefaults("", ApplyFlagsDeleteDefaults, cmd)
o.ResourceTypesFlags.Set(cmd)
o.PrevAppFlags.Set(cmd)
return cmd
}
func (o *DeleteOptions) Run() error {
failingAPIServicesPolicy := o.ResourceTypesFlags.FailingAPIServicePolicy()
app, supportObjs, err := Factory(o.depsFactory, o.AppFlags, o.ResourceTypesFlags, o.logger)
if err != nil {
return err
}
exists, notExistsMsg, err := app.Exists()
if err != nil {
return err
}
if !exists {
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()
if err != nil {
return err
}
failingAPIServicesPolicy.MarkRequiredGVs(usedGVs)
existingResources, shouldFullyDeleteApp, err := o.existingResources(app, supportObjs)
if err != nil {
return err
}
_, conf, err := ctlconf.NewConfFromResourcesWithDefaults(nil)
if err != nil {
return err
}
clusterChangeSet, clusterChangesGraph, changesSummary, err :=
o.calculateAndPresentChanges(existingResources, conf, supportObjs)
if err != nil {
if o.DiffFlags.UI && clusterChangesGraph != nil {
return o.presentDiffUI(clusterChangesGraph)
}
return err
}
if changesSummary.SkippedChanges {
shouldFullyDeleteApp = false
}
if !shouldFullyDeleteApp {
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)
}
if o.DiffFlags.UI {
return o.presentDiffUI(clusterChangesGraph)
}
if o.DiffFlags.Run {
if o.DiffFlags.ExitStatus {
return DeployDiffExitStatus{changesSummary.HasNoChanges}
}
return nil
}
err = o.ui.AskForConfirmation()
if err != nil {
return err
}
touch := ctlapp.Touch{App: app, Description: "delete", IgnoreSuccessErr: true}
err = touch.Do(func() error {
err := clusterChangeSet.Apply(clusterChangesGraph)
if err != nil {
return err
}
if shouldFullyDeleteApp {
return app.Delete()
}
return nil
})
if err != nil {
return err
}
if o.ApplyFlags.ExitStatus {
return DeployApplyExitStatus{changesSummary.HasNoChanges}
}
return nil
}
func (o *DeleteOptions) existingResources(app ctlapp.App,
supportObjs FactorySupportObjs) ([]ctlres.Resource, bool, error) {
labelSelector, err := app.LabelSelector()
if err != nil {
return nil, false, err
}
existingResources, err := supportObjs.IdentifiedResources.List(labelSelector, nil, ctlres.IdentifiedResourcesListOpts{})
if err != nil {
return nil, false, err
}
resourceFilter, err := o.ResourceFilterFlags.ResourceFilter()
if err != nil {
return nil, false, err
}
fullyDeleteApp := true
applicableExistingResources := resourceFilter.Apply(existingResources)
if len(applicableExistingResources) != len(existingResources) {
fullyDeleteApp = false
}
existingResources = applicableExistingResources
o.changeIgnored(existingResources)
return existingResources, fullyDeleteApp, nil
}
func (o *DeleteOptions) calculateAndPresentChanges(existingResources []ctlres.Resource, conf ctlconf.Conf,
supportObjs FactorySupportObjs) (ctlcap.ClusterChangeSet, *ctldgraph.ChangeGraph, changesSummary, error) {
var (
clusterChangeSet ctlcap.ClusterChangeSet
skippedChanges bool
)
{ // Figure out changes for X existing resources -> 0 new resources
changeFactory := ctldiff.NewChangeFactory(nil, nil)
changeSetFactory := ctldiff.NewChangeSetFactory(o.DiffFlags.ChangeSetOpts, changeFactory)
changes, err := changeSetFactory.New(existingResources, nil).Calculate()
if err != nil {
return ctlcap.ClusterChangeSet{}, nil, changesSummary{}, err
}
diffFilter, err := o.DiffFlags.DiffFilter()
if err != nil {
return ctlcap.ClusterChangeSet{}, nil, changesSummary{}, err
}
appliedChanges := diffFilter.Apply(changes)
if len(changes) != len(appliedChanges) {
// setting it to true when changes are filtered by diffFilter
skippedChanges = true
}
{ // Build cluster changes based on diff changes
msgsUI := cmdcore.NewDedupingMessagesUI(cmdcore.NewPlainMessagesUI(o.ui))
convergedResFactory := ctlcap.NewConvergedResourceFactory(conf.WaitRules(), ctlcap.ConvergedResourceFactoryOpts{
IgnoreFailingAPIServices: o.ResourceTypesFlags.IgnoreFailingAPIServices,
})
clusterChangeFactory := ctlcap.NewClusterChangeFactory(
o.ApplyFlags.ClusterChangeOpts, supportObjs.IdentifiedResources,
changeFactory, changeSetFactory, convergedResFactory, msgsUI, conf.DiffMaskRules())
clusterChangeSet = ctlcap.NewClusterChangeSet(
appliedChanges, o.ApplyFlags.ClusterChangeSetOpts, clusterChangeFactory,
conf.ChangeGroupBindings(), conf.ChangeRuleBindings(), msgsUI, o.logger)
}
}
clusterChanges, clusterChangesGraph, err := clusterChangeSet.Calculate()
if err != nil {
return ctlcap.ClusterChangeSet{}, nil, changesSummary{}, err
}
{ // Present cluster changes in UI
changeViews := ctlcap.ClusterChangesAsChangeViews(clusterChanges)
changeSetView := ctlcap.NewChangeSetView(
changeViews, conf.DiffMaskRules(), o.DiffFlags.ChangeSetViewOpts)
changeSetView.Print(o.ui)
}
return clusterChangeSet, clusterChangesGraph, changesSummary{HasNoChanges: len(clusterChanges) == 0, SkippedChanges: skippedChanges}, nil
}
const (
ownedForDeletionAnnKey = "kapp.k14s.io/owned-for-deletion" // valid values: ''
)
func (o *DeleteOptions) changeIgnored(resources []ctlres.Resource) {
// Good example for use of this annotation is PVCs created by StatefulSet
// (PVCs do not get deleted when StatefulSet is deleted:
// https://github.com/vmware-tanzu/carvel-kapp/issues/36)
for _, res := range resources {
if _, found := res.Annotations()[ownedForDeletionAnnKey]; found {
res.MarkTransient(false)
}
}
}
func (o *DeleteOptions) presentDiffUI(graph *ctldgraph.ChangeGraph) error {
opts := ctldiffui.ServerOpts{
DiffDataFunc: func() *ctldgraph.ChangeGraph { return graph },
}
return ctldiffui.NewServer(opts, o.ui).Run()
}