This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
uninstall.go
384 lines (321 loc) · 10.6 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// percona-everest-cli
// Copyright (C) 2023 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package uninstall ...
package uninstall
import (
"context"
"fmt"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
everestv1alpha1 "github.com/percona/everest-operator/api/v1alpha1"
"go.uber.org/zap"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/percona/percona-everest-cli/pkg/install"
"github.com/percona/percona-everest-cli/pkg/kubernetes"
)
// Uninstall implements logic for the cluster command.
type Uninstall struct {
config Config
kubeClient *kubernetes.Kubernetes
l *zap.SugaredLogger
}
// Config stores configuration for the Uninstall command.
type Config struct {
// KubeconfigPath is a path to a kubeconfig
KubeconfigPath string `mapstructure:"kubeconfig"`
// AssumeYes is true when all questions can be skipped.
AssumeYes bool `mapstructure:"assume-yes"`
// Force is true when we shall not prompt for removal.
Force bool
}
// NewUninstall returns a new Uninstall struct.
func NewUninstall(c Config, l *zap.SugaredLogger) (*Uninstall, error) {
kubeClient, err := kubernetes.New(c.KubeconfigPath, l)
if err != nil {
return nil, err
}
cli := &Uninstall{
config: c,
kubeClient: kubeClient,
l: l,
}
return cli, nil
}
// Run runs the cluster command.
func (u *Uninstall) Run(ctx context.Context) error {
if !u.config.AssumeYes {
msg := `You are about to uninstall Everest from the Kubernetes cluster.
This will uninstall Everest and all its components from the cluster.`
fmt.Printf("\n%s\n\n", msg) //nolint:forbidigo
confirm := &survey.Confirm{
Message: "Are you sure you want to uninstall Everest?",
}
prompt := false
if err := survey.AskOne(confirm, &prompt); err != nil {
return err
}
if !prompt {
u.l.Info("Exiting")
return nil
}
}
dbsExist, err := u.dbsExist(ctx)
if err != nil {
return err
}
if dbsExist {
force, err := u.confirmForce()
if err != nil {
return err
}
if !force {
u.l.Info("Can't proceed without deleting database clusters")
return nil
}
if err := u.deleteDBs(ctx); err != nil {
return err
}
}
// BackupStorages have finalizers, so we need to delete them first
if err := u.deleteBackupStorages(ctx); err != nil {
return err
}
if err := u.deleteDBNamespaces(ctx); err != nil {
return err
}
// VMAgent has finalizers, so we need to delete the monitoring configs first
if err := u.deleteMonitoringConfigs(ctx); err != nil {
return err
}
// There are no resources with finalizers in the monitoring namespace, so
// we can delete it directly
if err := u.deleteNamespaces(ctx, []string{install.MonitoringNamespace}); err != nil {
return err
}
// All resources with finalizers in the system namespace (DBCs and
// BackupStorages) have already been deleted, so we can delete the
// namespace directly
if err := u.deleteNamespaces(ctx, []string{install.SystemNamespace}); err != nil {
return err
}
// There are no resources with finalizers in the monitoring namespace, so
// we can delete it directly
if err := u.deleteOLM(ctx); err != nil {
return err
}
u.l.Info("Everest has been uninstalled successfully")
return nil
}
func (u *Uninstall) confirmForce() (bool, error) {
if u.config.Force {
return true, nil
}
confirm := &survey.Confirm{
Message: "There are still database clusters managed by Everest. Do you want to delete them?",
}
prompt := false
if err := survey.AskOne(confirm, &prompt); err != nil {
return false, err
}
return prompt, nil
}
func (u *Uninstall) getDBs(ctx context.Context) (map[string]*everestv1alpha1.DatabaseClusterList, error) {
namespaces, err := u.kubeClient.GetDBNamespaces(ctx, install.SystemNamespace)
if err != nil {
return nil, err
}
allDBs := make(map[string]*everestv1alpha1.DatabaseClusterList)
for _, ns := range namespaces {
dbs, err := u.kubeClient.ListDatabaseClusters(ctx, ns)
if err != nil {
return nil, err
}
allDBs[ns] = dbs
}
return allDBs, nil
}
func (u *Uninstall) dbsExist(ctx context.Context) (bool, error) {
allDBs, err := u.getDBs(ctx)
if err != nil {
return false, err
}
exist := false
for ns, dbs := range allDBs {
if len(dbs.Items) == 0 {
continue
}
exist = true
u.l.Warnf("Database clusters in namespace '%s':", ns)
for _, db := range dbs.Items {
u.l.Warnf(" - %s", db.Name)
}
}
return exist, nil
}
func (u *Uninstall) deleteDBs(ctx context.Context) error {
allDBs, err := u.getDBs(ctx)
if err != nil {
return err
}
for ns, dbs := range allDBs {
for _, db := range dbs.Items {
u.l.Infof("Deleting database cluster '%s' in namespace '%s'", db.Name, ns)
if err := u.kubeClient.DeleteDatabaseCluster(ctx, ns, db.Name); err != nil {
return err
}
}
}
// Wait for all database clusters to be deleted, or timeout after 5 minutes.
u.l.Info("Waiting for database clusters to be deleted")
// XXX: When deleting a DBC CR, the everest operator doesn't wait for the
// DB operator's CRs to be deleted. Thus, as soon as we delete the DBC CRs,
// these cease to exist in the cluster and the polling below will return
// immediately. If we don't wait for the DB operators to process the
// deletion of the CRs, we may end up deleting the namespaces before the DB
// operators have a chance to delete the resources they manage, leaving the
// namespaces in an endless Terminating state waiting for finalizers to be
// removed.
// The everest operator should have a Deleting status that waits for the DB
// operators to delete their DB CRs before removing the corresponting DBC
// CR. Until this is implemented, we work around this by sleeping for two
// minutes to give the DB operators a chance to delete the resources they
// manage before we delete the namespaces.
time.Sleep(2 * time.Minute)
return wait.PollUntilContextTimeout(ctx, 5*time.Second, 5*time.Minute, false, func(ctx context.Context) (bool, error) {
allDBs, err := u.getDBs(ctx)
if err != nil {
return false, err
}
for _, dbs := range allDBs {
if len(dbs.Items) != 0 {
return false, nil
}
}
u.l.Info("All database clusters have been deleted")
return true, nil
})
}
func (u *Uninstall) deleteNamespaces(ctx context.Context, namespaces []string) error {
for _, ns := range namespaces {
u.l.Infof("Deleting namespace '%s'", ns)
if err := u.kubeClient.DeleteNamespace(ctx, ns); err != nil {
return err
}
}
// Wait for all namespaces to be deleted, or timeout after 5 minutes.
u.l.Infof("Waiting for namespace(s) '%s' to be deleted", strings.Join(namespaces, "', '"))
return wait.PollUntilContextTimeout(ctx, 5*time.Second, 5*time.Minute, false, func(ctx context.Context) (bool, error) {
for _, ns := range namespaces {
_, err := u.kubeClient.GetNamespace(ctx, ns)
if err != nil && !k8serrors.IsNotFound(err) {
return false, err
}
if err == nil {
return false, nil
}
}
u.l.Infof("Namespace(s) '%s' have been deleted", strings.Join(namespaces, "', '"))
return true, nil
})
}
func (u *Uninstall) deleteDBNamespaces(ctx context.Context) error {
namespaces, err := u.kubeClient.GetDBNamespaces(ctx, install.SystemNamespace)
if err != nil {
return err
}
return u.deleteNamespaces(ctx, namespaces)
}
func (u *Uninstall) deleteBackupStorages(ctx context.Context) error { //nolint:dupl
storages, err := u.kubeClient.ListBackupStorages(ctx, install.SystemNamespace)
if err != nil {
return err
}
if len(storages.Items) == 0 {
return nil
}
for _, storage := range storages.Items {
u.l.Infof("Deleting backup storage '%s'", storage.Name)
if err := u.kubeClient.DeleteBackupStorage(ctx, install.SystemNamespace, storage.Name); err != nil {
return err
}
}
// Wait for all backup storages to be deleted, or timeout after 5 minutes.
u.l.Infof("Waiting for backup storages to be deleted")
return wait.PollUntilContextTimeout(ctx, 5*time.Second, 5*time.Minute, false, func(ctx context.Context) (bool, error) {
storages, err := u.kubeClient.ListBackupStorages(ctx, install.SystemNamespace)
if err != nil {
return false, err
}
if len(storages.Items) != 0 {
return false, nil
}
u.l.Info("All backup storages have been deleted")
return true, nil
})
}
func (u *Uninstall) deleteMonitoringConfigs(ctx context.Context) error { //nolint:dupl
monitoringConfigs, err := u.kubeClient.ListMonitoringConfigs(ctx, install.MonitoringNamespace)
if err != nil {
return err
}
if len(monitoringConfigs.Items) == 0 {
return nil
}
for _, config := range monitoringConfigs.Items {
u.l.Infof("Deleting monitoring config '%s'", config.Name)
if err := u.kubeClient.DeleteMonitoringConfig(ctx, install.MonitoringNamespace, config.Name); err != nil {
return err
}
}
// Wait for all monitoring configs to be deleted, or timeout after 5 minutes.
u.l.Infof("Waiting for monitoring configs to be deleted")
return wait.PollUntilContextTimeout(ctx, 5*time.Second, 5*time.Minute, false, func(ctx context.Context) (bool, error) {
monitoringConfigs, err := u.kubeClient.ListMonitoringConfigs(ctx, install.MonitoringNamespace)
if err != nil {
return false, err
}
if len(monitoringConfigs.Items) != 0 {
return false, nil
}
u.l.Info("All monitoring configs have been deleted")
return true, nil
})
}
func (u *Uninstall) deleteOLM(ctx context.Context) error {
packageServerName := types.NamespacedName{Name: "packageserver", Namespace: kubernetes.OLMNamespace}
if err := u.kubeClient.DeleteClusterServiceVersion(ctx, packageServerName); err != nil {
return err
}
// Wait for the packageserver CSV to be deleted, or timeout after 5 minutes.
u.l.Infof("Waiting for packageserver CSV to be deleted")
err := wait.PollUntilContextTimeout(ctx, 5*time.Second, 5*time.Minute, false, func(ctx context.Context) (bool, error) {
_, err := u.kubeClient.GetClusterServiceVersion(ctx, packageServerName)
if err != nil && !k8serrors.IsNotFound(err) {
return false, err
}
if err == nil {
return false, nil
}
u.l.Info("Packageserver CSV has been deleted")
return true, nil
})
if err != nil {
return err
}
return u.deleteNamespaces(ctx, []string{kubernetes.OLMNamespace})
}