forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_context.go
148 lines (128 loc) · 3.47 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
package cmd
import (
"fmt"
"io"
"sort"
"strings"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"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
}
// NewCmdDeleteContext creates a command object for the "delete repo" command
func NewCmdDeleteContext(f Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &DeleteContextOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
},
}
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")
return cmd
}
// Run implements the command
func (o *DeleteContextOptions) Run() error {
config, po, err := 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)
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)
if err != nil {
return err
}
if !flag {
return nil
}
newConfig := *config
for _, name := range selected {
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
}