-
Notifications
You must be signed in to change notification settings - Fork 788
/
uninstall.go
229 lines (208 loc) · 6.9 KB
/
uninstall.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
package cmd
import (
"fmt"
"io"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"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"
"gopkg.in/AlecAivazis/survey.v1/terminal"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type UninstallOptions struct {
CommonOptions
Namespace string
Context string
Force bool // Force uninstallation - programmatic use only - do not expose to the user
KeepEnvironments bool
}
var (
uninstall_long = templates.LongDesc(`
Uninstalls the Jenkins X platform from a Kubernetes cluster`)
uninstall_example = templates.Examples(`
# Uninstall the Jenkins X platform
jx uninstall`)
)
func NewCmdUninstall(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &UninstallOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "uninstall",
Short: "Uninstall the Jenkins X platform",
Long: uninstall_long,
Example: uninstall_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCommonFlags(cmd)
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "The team namespace to uninstall. Defaults to the current namespace.")
cmd.Flags().StringVarP(&options.Context, "context", "", "", "The kube context to uninstall JX from. This will be compared with the current context to prevent accidental uninstallation from the wrong cluster")
cmd.Flags().BoolVarP(&options.KeepEnvironments, "keep-environments", "", false, "Don't delete environments. Uninstall Jenkins X only.")
return cmd
}
func (o *UninstallOptions) Run() error {
config, _, err := o.Kube().LoadConfig()
if err != nil {
return err
}
jxClient, _, err := o.JXClient()
if err != nil {
return err
}
currentContext := kube.CurrentContextName(config)
namespace := o.Namespace
if namespace == "" {
namespace = kube.CurrentNamespace(config)
}
var targetContext string
if !o.Force {
if o.BatchMode || o.Context != "" {
targetContext = o.Context
} else {
targetContext, err = util.PickValue(fmt.Sprintf("Enter the current context name to confirm "+
"uninstallation of the Jenkins X platform from the %s namespace:", util.ColorInfo(namespace)),
"", true,
"To prevent accidental uninstallation from the wrong cluster, you must enter the current "+
"kubernetes context. This can be found with `kubectl config current-context`",
o.In, o.Out, o.Err)
if err != nil {
return err
}
}
if targetContext != currentContext {
return fmt.Errorf("The context '%s' must match the current context to uninstall", targetContext)
}
}
log.Infof("Removing installation of Jenkins X in team namespace %s\n", util.ColorInfo(namespace))
err = o.cleanupConfig()
if err != nil {
return err
}
envMap, envNames, err := kube.GetEnvironments(jxClient, namespace)
if err != nil {
log.Warnf("Failed to find Environments. Probably not installed yet?. Error: %s\n", err)
}
if !o.KeepEnvironments {
for _, env := range envNames {
release := namespace + "-" + env
err := o.Helm().StatusRelease(namespace, release)
if err != nil {
continue
}
err = o.Helm().DeleteRelease(namespace, release, true)
if err != nil {
log.Warnf("Failed to uninstall environment chart %s: %s\n", release, err)
}
}
}
errs := []error{}
err = o.Helm().StatusRelease(namespace, "jx-prow")
if err == nil {
err := o.Helm().DeleteRelease(namespace, "jx-prow", true)
if err != nil {
errs = append(errs, fmt.Errorf("failed to uninstall the prow helm chart in namespace %s: %s", namespace, err))
}
}
err = o.Helm().StatusRelease(namespace, "jenkins-x")
if err == nil {
err = o.Helm().DeleteRelease(namespace, "jenkins-x", true)
if err != nil {
errs = append(errs, fmt.Errorf("failed to uninstall the jenkins-x helm chart in namespace %s: %s", namespace, err))
}
}
err = jxClient.JenkinsV1().Environments(namespace).DeleteCollection(&meta_v1.DeleteOptions{}, meta_v1.ListOptions{})
if err != nil {
errs = append(errs, fmt.Errorf("failed to delete the environments in namespace %s: %s", namespace, err))
}
err = o.cleanupNamespaces(namespace, envNames, envMap)
if err != nil {
errs = append(errs, fmt.Errorf("failed to cleanup namespaces in namespace %s: %s", namespace, err))
}
if len(errs) > 0 {
return util.CombineErrors(errs...)
}
log.Successf("Jenkins X has been successfully uninstalled from team namespace %s", namespace)
return nil
}
func (o *UninstallOptions) cleanupNamespaces(namespace string, envNames []string, envMap map[string]*v1.Environment) error {
client, err := o.KubeClient()
if err != nil {
return errors.Wrap(err, "getting the kube client")
}
errs := []error{}
err = o.deleteNamespace(namespace)
if err != nil {
errs = append(errs, fmt.Errorf("deleting namespace %s: %s", namespace, err))
}
if !o.KeepEnvironments {
for _, env := range envNames {
envNamespaces := []string{namespace + "-" + env}
envResource := envMap[env]
envNamespace := ""
if envResource != nil {
envNamespace = envResource.Spec.Namespace
}
if envNamespace != "" && envNamespaces[0] != envNamespace && envNamespace != namespace {
envNamespaces = append(envNamespaces, envNamespace)
}
for _, envNamespace := range envNamespaces {
_, err := client.CoreV1().Namespaces().Get(envNamespace, meta_v1.GetOptions{})
if err != nil {
continue
}
err = o.deleteNamespace(envNamespace)
if err != nil {
errs = append(errs, fmt.Errorf("deleting environment namespace %s: %s", envNamespace, err))
}
}
}
}
return util.CombineErrors(errs...)
}
func (o *UninstallOptions) deleteNamespace(namespace string) error {
client, err := o.KubeClient()
if err != nil {
return errors.Wrap(err, "getting the kube client")
}
_, err = client.CoreV1().Namespaces().Get(namespace, meta_v1.GetOptions{})
if err != nil {
// There is nothing to delete if the namespace cannot be retrieved
return nil
}
log.Infof("deleting namespace %s\n", util.ColorInfo(namespace))
err = client.CoreV1().Namespaces().Delete(namespace, &meta_v1.DeleteOptions{})
if err != nil {
return errors.Wrapf(err, "deleting the namespace '%s' from Kubernetes cluster", namespace)
}
return nil
}
func (o *UninstallOptions) cleanupConfig() error {
authConfigSvc, err := o.CreateAuthConfigService(JenkinsAuthConfigFile)
if err != nil || authConfigSvc == nil {
return nil
}
server := authConfigSvc.Config().CurrentServer
err = authConfigSvc.DeleteServer(server)
if err != nil {
return err
}
chartConfigSvc, err := o.CreateChartmuseumAuthConfigService()
if err != nil {
return err
}
server = chartConfigSvc.Config().CurrentServer
return chartConfigSvc.DeleteServer(server)
}