forked from splunk/qbec
/
delete.go
141 lines (122 loc) · 3.56 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
/*
Copyright 2019 Splunk Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
import (
"fmt"
"github.com/spf13/cobra"
"github.com/splunk/qbec/internal/model"
"github.com/splunk/qbec/internal/objsort"
"github.com/splunk/qbec/internal/remote"
"github.com/splunk/qbec/internal/sio"
)
type deleteCommandConfig struct {
*Config
dryRun bool
useLocal bool
filterFunc func() (filterParams, error)
}
func doDelete(args []string, config deleteCommandConfig) error {
if len(args) != 1 {
return newUsageError("exactly one environment required")
}
env := args[0]
if env == model.Baseline { // cannot apply for the baseline environment
return newUsageError("cannot delete baseline environment, use a real environment")
}
fp, err := config.filterFunc()
if err != nil {
return err
}
client, err := config.Client(env)
if err != nil {
return err
}
var deletions []model.K8sQbecMeta
if config.useLocal {
objects, err := filteredObjects(config.Config, env, client.ObjectKey, fp)
if err != nil {
return err
}
for _, o := range objects {
if o.GetName() != "" {
deletions = append(deletions, o)
}
}
} else {
all, err := allObjects(config.Config, env)
if err != nil {
return err
}
lister, scope, err := newRemoteLister(client, all, config.app.DefaultNamespace(env))
if err != nil {
return err
}
lister.start(remote.ListQueryConfig{
Application: config.App().Name(),
Tag: config.App().Tag(),
Environment: env,
KindFilter: fp.kindFilter,
ListQueryScope: scope,
})
deletions, err = lister.deletions(nil, fp.Includes)
if err != nil {
return err
}
}
dryRun := ""
if config.dryRun {
dryRun = "[dry-run] "
}
// process deletions
deletions = objsort.SortMeta(deletions, sortConfig(client.IsNamespaced))
if !config.dryRun && len(deletions) > 0 {
msg := fmt.Sprintf("will delete %d objects", len(deletions))
if err := config.Confirm(msg); err != nil {
return err
}
}
var stats applyStats
for i := len(deletions) - 1; i >= 0; i-- {
ob := deletions[i]
name := client.DisplayName(ob)
res, err := client.Delete(ob, config.dryRun)
if err != nil {
return err
}
stats.update(name, res)
sio.Noticeln(dryRun+"delete", name)
sio.Println(res.Details)
}
printStats(config.Stdout(), &stats)
if config.dryRun {
sio.Noticeln("** dry-run mode, nothing was actually changed **")
}
return nil
}
func newDeleteCommand(cp ConfigProvider) *cobra.Command {
cmd := &cobra.Command{
Use: "delete [-n] <environment>",
Short: "delete one or more components from a Kubernetes cluster",
Example: deleteExamples(),
}
config := deleteCommandConfig{
filterFunc: addFilterParams(cmd, true),
}
cmd.Flags().BoolVarP(&config.dryRun, "dry-run", "n", false, "dry-run, do not delete resources but show what would happen")
cmd.Flags().BoolVar(&config.useLocal, "local", false, "use local object names to delete, do not derive list from server")
cmd.RunE = func(c *cobra.Command, args []string) error {
config.Config = cp()
return wrapError(doDelete(args, config))
}
return cmd
}