-
Notifications
You must be signed in to change notification settings - Fork 785
/
uninstall.go
277 lines (245 loc) · 9.08 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package uninstall
import (
"fmt"
"github.com/jenkins-x/jx/v2/pkg/errorutil"
v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx-api/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/util"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type UninstallOptions struct {
*opts.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. This will remove all Jenkins X components, secrets, config and namespaces including any environment related namespaces`)
uninstall_example = templates.Examples(`
# Uninstall the Jenkins X platform
jx uninstall`)
)
const (
kubeSystemNs = "kube-system"
jxIngressRelease = "jxing"
)
func NewCmdUninstall(commonOpts *opts.CommonOptions) *cobra.Command {
options := &UninstallOptions{
CommonOptions: commonOpts,
}
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()
helper.CheckErr(err)
},
}
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 {
envNamespaces := o.getAllNamespaces(namespace, jxClient)
help := "All Jenkins X components will be removed and the following namespaces will be deleted:"
for _, envNamespace := range envNamespaces {
help += "\n " + envNamespace
}
if answer, err := util.Confirm("Uninstall JX - this command will remove all JX components and delete all namespaces created by Jenkins X. Do you wish to continue?", false,
help, o.GetIOFileHandles()); !answer {
return err
}
}
if o.BatchMode || o.Context != "" {
targetContext = o.Context
} else {
msg := fmt.Sprintf("This action will permanently delete Jenkins X from the Kubernetes context %s. "+
"Please type in the name of the context to confirm:", util.ColorInfo(currentContext))
helpMsg := "To prevent accidental uninstallation from the wrong cluster, you must enter the current Kubernetes context."
targetContext, err = util.PickValue(msg, "", true, helpMsg, o.GetIOFileHandles())
if err != nil {
return err
}
}
if targetContext != currentContext {
return fmt.Errorf("the context '%s' must match the current context '%s' to uninstall", targetContext, currentContext)
}
}
log.Logger().Infof("Removing installation of Jenkins X in team namespace %s", util.ColorInfo(namespace))
err = o.setPVCFinalizerToNull(namespace)
if err != nil {
return errors.Wrapf(err, "unable to nil PVC finalizers")
}
envMap, envNames, err := kube.GetEnvironments(jxClient, namespace)
if err != nil {
log.Logger().Warnf("Failed to find Environments. Probably not installed yet?. Error: %s", 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.Logger().Warnf("Failed to uninstall environment chart %s: %s", release, err)
}
}
}
var errs []error
errs = o.DeleteReleaseIfPresent(namespace, "jx-prow", errs, false)
errs = o.DeleteReleaseIfPresent(namespace, "jenkins-x", errs, false)
errs = o.DeleteReleaseIfPresent(kubeSystemNs, jxIngressRelease, errs, true)
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 errorutil.CombineErrors(errs...)
}
log.Logger().Infof("Jenkins X has been successfully uninstalled from team namespace %s", namespace)
return nil
}
func (o *UninstallOptions) getAllNamespaces(namespace string, jxClient versioned.Interface) []string {
envNamespaces := []string{namespace}
if jxClient != nil {
envMap, envNames, err := kube.GetEnvironments(jxClient, namespace)
if err != nil {
log.Logger().Warnf("Failed to find Environments. Error: %s", err)
return envNamespaces
}
for _, env := range envNames {
envResource := envMap[env]
envNamespace := namespace + "-" + env
if envResource != nil {
envNamespace = envResource.Spec.Namespace
}
if envNamespace != "" && envNamespace != namespace {
envNamespaces = append(envNamespaces, envNamespace)
}
}
}
return envNamespaces
}
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 errorutil.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.Logger().Infof("deleting namespace %s", 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
}
// DeleteReleaseIfPresent deletes the given chart in the given namespace and adds any erro to the passed errors slice
// as it checks if the release is present before deleting, it can be forced to delete in case it doesn't find it because
// of an unrelated error
func (o *UninstallOptions) DeleteReleaseIfPresent(ns string, releaseName string, errors []error, force bool) []error {
log.Logger().Debugf("Deleting release '%s'", releaseName)
if err := o.Helm().StatusRelease(ns, releaseName); err == nil || force {
err := o.Helm().DeleteRelease(ns, releaseName, true)
if err != nil {
errors = append(errors, fmt.Errorf("failed to uninstall the %s helm chart in namespace %s: %s", releaseName, ns, err))
}
} else {
log.Logger().Warnf("Not deleting %s because the release is not installed", releaseName)
}
return errors
}
func (o *UninstallOptions) setPVCFinalizerToNull(ns string) error {
client, err := o.KubeClient()
if err != nil {
return errors.Wrap(err, "getting the kube client")
}
list, err := client.CoreV1().PersistentVolumeClaims(ns).List(meta_v1.ListOptions{})
if err != nil {
return errors.Wrap(err, "getting PVC list")
}
for _, pvc := range list.Items {
currentPVC := pvc
currentPVC.Finalizers = nil
log.Logger().Debugf("Setting finalizer for PVC '%s' to nil", currentPVC.Name)
_, err = client.CoreV1().PersistentVolumeClaims(ns).Update(¤tPVC)
if err != nil {
return errors.Wrapf(err, "failed to update pvc '%s'", pvc.GetName())
}
}
return nil
}