-
Notifications
You must be signed in to change notification settings - Fork 787
/
delete_context.go
159 lines (138 loc) · 4.2 KB
/
delete_context.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
package cmd
import (
"fmt"
"sort"
"strings"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
"k8s.io/client-go/tools/clientcmd"
)
var (
delete_context_long = templates.LongDesc(`
Deletes one or more Kubernetes contexts.
`)
delete_context_example = templates.Examples(`
# Deletes a context for a cluster that no longer exists
jx delete context something
# Deletes all contexts containing the word cheese
# selecting them all by default
jx delete ctx -a cheese
`)
)
// DeleteContextOptions the options for the create spring command
type DeleteContextOptions struct {
CreateOptions
SelectAll bool
SelectFilter string
DeleteAuthInfo bool
DeleteCluster bool
}
// NewCmdDeleteContext creates a command object for the "delete repo" command
func NewCmdDeleteContext(commonOpts *CommonOptions) *cobra.Command {
options := &DeleteContextOptions{
CreateOptions: CreateOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "contexts",
Short: "Deletes one or more Kubernetes contexts",
Aliases: []string{"context", "ctx"},
Long: delete_context_long,
Example: delete_context_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
//addDeleteFlags(cmd, &options.CreateOptions)
cmd.Flags().BoolVarP(&options.SelectAll, "all", "a", false, "Selects all the matched contexts")
cmd.Flags().StringVarP(&options.SelectFilter, "filter", "f", "", "Filter the list of contexts to those containing this text")
cmd.Flags().BoolVarP(&options.DeleteAuthInfo, "delete-user", "", false, "Also delete the user config associated to the context")
cmd.Flags().BoolVarP(&options.DeleteCluster, "delete-cluster", "", false, "Also delete the cluster config associated to the context")
return cmd
}
// Run implements the command
func (o *DeleteContextOptions) Run() error {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
config, po, err := o.Kube().LoadConfig()
if err != nil {
return err
}
if config == nil || config.Contexts == nil || len(config.Contexts) == 0 {
return fmt.Errorf("No Kubernetes contexts available! Try create or connect to cluster?")
}
names := []string{}
allNames := []string{}
args := o.Args
for k := range config.Contexts {
allNames = append(allNames, k)
if matchesFilter(k, args) {
names = append(names, k)
}
}
sort.Strings(allNames)
if len(names) == 0 {
if len(args) == 0 {
return fmt.Errorf("Failed to find a context!")
}
return util.InvalidArg(args[1], allNames)
}
selected, err := util.SelectNamesWithFilter(names, "Select the Kubernetes Contexts to delete: ", o.SelectAll, o.SelectFilter, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
if len(selected) == 0 {
return nil
}
flag := false
prompt := &survey.Confirm{
Message: "Are you sure you want to delete these these Kubernetes Contexts?",
Default: false,
}
err = survey.AskOne(prompt, &flag, nil, surveyOpts)
if err != nil {
return err
}
if !flag {
return nil
}
newConfig := *config
for _, name := range selected {
a := newConfig.Contexts[name].AuthInfo
if o.DeleteAuthInfo && a != "" {
o.Debugf("Deleting user %s for context %s\n", util.ColorInfo(a), util.ColorInfo(name))
delete(newConfig.AuthInfos, a)
}
c := newConfig.Contexts[name].Cluster
if o.DeleteCluster && c != "" {
o.Debugf("Deleting cluster %s for context %s\n", util.ColorInfo(c), util.ColorInfo(name))
delete(newConfig.Clusters, c)
}
o.Debugf("Deleting context %s\n", util.ColorInfo(name))
delete(newConfig.Contexts, name)
}
err = clientcmd.ModifyConfig(po, newConfig, false)
if err != nil {
return fmt.Errorf("Failed to update the kube config %s", err)
}
log.Infof("Deleted Kubernetes contexts: %s\n", util.ColorInfo(strings.Join(selected, ", ")))
return nil
}
// matchesFilter returns true if there are no filters or the text contains one of the filters
func matchesFilter(text string, filters []string) bool {
if len(filters) == 0 {
return true
}
for _, f := range filters {
if strings.Contains(text, f) {
return true
}
}
return false
}