-
Notifications
You must be signed in to change notification settings - Fork 786
/
delete_team.go
176 lines (156 loc) · 4.33 KB
/
delete_team.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
package cmd
import (
"fmt"
"strings"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// DeleteTeamOptions are the flags for delete commands
type DeleteTeamOptions struct {
*CommonOptions
SelectAll bool
SelectFilter string
Confirm bool
}
var (
deleteTeamLong = templates.LongDesc(`
Deletes one or more teams and their associated resources (Environments, Jenkins etc)
`)
deleteTeamExample = templates.Examples(`
# Delete the named team
jx delete team cheese
# Delete the teams matching the given filter
jx delete team -f foo
`)
)
// NewCmdDeleteTeam creates a command object
// retrieves one or more resources from a server.
func NewCmdDeleteTeam(commonOpts *CommonOptions) *cobra.Command {
options := &DeleteTeamOptions{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "team",
Short: "Deletes one or more teams and their associated resources (Environments, Jenkins etc)",
Long: deleteTeamLong,
Example: deleteTeamExample,
Aliases: []string{"teams"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.SelectAll, "all", "a", false, "Should we default to selecting all the matched teams for deletion")
cmd.Flags().StringVarP(&options.SelectFilter, "filter", "f", "", "Filters the list of teams you can pick from")
cmd.Flags().BoolVarP(&options.Confirm, "yes", "y", false, "Confirms we should uninstall this installation")
return cmd
}
// Run implements this command
func (o *DeleteTeamOptions) Run() error {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
kubeClient, err := o.KubeClient()
if err != nil {
return err
}
apisClient, err := o.ApiExtensionsClient()
if err != nil {
return err
}
kube.RegisterEnvironmentCRD(apisClient)
_, teamNames, err := kube.GetTeams(kubeClient)
if err != nil {
return err
}
names := o.Args
if len(names) == 0 {
if o.BatchMode {
return fmt.Errorf("Missing team name argument")
}
names, err = util.SelectNamesWithFilter(teamNames, "Which teams do you want to delete: ", o.SelectAll, o.SelectFilter, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
}
if o.BatchMode {
if !o.Confirm {
return fmt.Errorf("In batch mode you must specify the '-y' flag to confirm")
}
} else {
log.Warnf("You are about to delete the following teams '%s'. This operation CANNOT be undone!",
strings.Join(names, ","))
flag := false
prompt := &survey.Confirm{
Message: "Are you sure you want to delete all these teams?",
Default: false,
}
err = survey.AskOne(prompt, &flag, nil, surveyOpts)
if err != nil {
return err
}
if !flag {
return nil
}
}
for _, name := range names {
err = o.deleteTeam(name)
if err != nil {
log.Warnf("Failed to delete team %s: %s\n", name, err)
}
}
return nil
}
func (o *DeleteTeamOptions) deleteTeam(name string) error {
err := o.registerTeamCRD()
if err != nil {
return err
}
jxClient, ns, err := o.JXClientAndAdminNamespace()
if err != nil {
return err
}
kubeClient, err := o.KubeClient()
if err != nil {
return err
}
_, err = kubeClient.CoreV1().Namespaces().Get(name, metav1.GetOptions{})
if err != nil {
// we don't have the namespace so the team cannot have been provisioned yet
return kube.DeleteTeam(jxClient, ns, name)
}
origNamespace := o.currentNamespace
o.ChangeNamespace(name)
uninstall := &UninstallOptions{
CommonOptions: o.CommonOptions,
Namespace: name,
Force: true,
}
uninstall.BatchMode = true
err = o.ModifyTeam(ns, name, func(team *v1.Team) error {
team.Status.ProvisionStatus = v1.TeamProvisionStatusDeleting
team.Status.Message = "Deleting resources"
return nil
})
if err != nil {
return err
}
err = uninstall.Run()
if err != nil {
o.ModifyTeam(ns, name, func(team *v1.Team) error {
team.Status.ProvisionStatus = v1.TeamProvisionStatusError
team.Status.Message = fmt.Sprintf("Failed to delete team resources: %s", err)
return nil
})
} else {
err = kube.DeleteTeam(jxClient, ns, name)
}
o.ChangeNamespace(origNamespace)
return err
}