-
Notifications
You must be signed in to change notification settings - Fork 9
/
destroy.go
408 lines (350 loc) · 12.4 KB
/
destroy.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package playground
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
apitypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/templates"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
cp "github.com/apecloud/kbcli/pkg/cloudprovider"
"github.com/apecloud/kbcli/pkg/cmd/kubeblocks"
"github.com/apecloud/kbcli/pkg/printer"
"github.com/apecloud/kbcli/pkg/spinner"
"github.com/apecloud/kbcli/pkg/types"
"github.com/apecloud/kbcli/pkg/util"
"github.com/apecloud/kbcli/pkg/util/helm"
"github.com/apecloud/kbcli/pkg/util/prompt"
)
var (
destroyExample = templates.Examples(`
# destroy playground cluster
kbcli playground destroy`)
)
type destroyOptions struct {
genericiooptions.IOStreams
baseOptions
// purge resources, before destroying kubernetes cluster we should delete cluster and
// uninstall KubeBlocks
autoApprove bool
purge bool
// timeout represents the timeout for the destruction process.
timeout time.Duration
}
func newDestroyCmd(streams genericiooptions.IOStreams) *cobra.Command {
o := &destroyOptions{
IOStreams: streams,
}
cmd := &cobra.Command{
Use: "destroy",
Short: "Destroy the playground KubeBlocks and kubernetes cluster.",
Example: destroyExample,
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(o.complete(cmd))
util.CheckErr(o.validate())
util.CheckErr(o.destroy())
},
}
cmd.Flags().BoolVar(&o.purge, "purge", true, "Purge all resources before destroying kubernetes cluster, delete all clusters created by KubeBlocks and uninstall KubeBlocks.")
cmd.Flags().DurationVar(&o.timeout, "timeout", 300*time.Second, "Time to wait for destroying KubeBlocks, such as --timeout=10m")
cmd.Flags().BoolVar(&o.autoApprove, "auto-approve", false, "Skip interactive approval before destroying the playground")
return cmd
}
func (o *destroyOptions) destroy() error {
if o.prevCluster == nil {
return fmt.Errorf("no playground cluster found")
}
if o.prevCluster.CloudProvider == cp.Local {
return o.destroyLocal()
}
return o.destroyCloud()
}
// destroyLocal destroy local k3d cluster that will destroy all resources
func (o *destroyOptions) destroyLocal() error {
provider, _ := cp.New(cp.Local, "", o.Out, o.ErrOut)
s := spinner.New(o.Out, spinnerMsg("Delete playground k3d cluster "+o.prevCluster.ClusterName))
defer s.Fail()
if err := provider.DeleteK8sCluster(o.prevCluster); err != nil {
if !strings.Contains(err.Error(), "no cluster found") &&
!strings.Contains(err.Error(), "does not exist") {
return err
}
}
s.Success()
if err := o.removeKubeConfig(); err != nil {
return err
}
return o.removeStateFile()
}
// destroyCloud destroys cloud kubernetes cluster, before destroying, we should delete
// all clusters created by KubeBlocks, uninstall KubeBlocks and remove the KubeBlocks
// namespace that will destroy all resources created by KubeBlocks, avoid to leave resources behind
func (o *destroyOptions) destroyCloud() error {
var err error
printer.Warning(o.Out, `This action will destroy the kubernetes cluster, there may be residual resources,
please confirm and manually clean up related resources after this action.
`)
fmt.Fprintf(o.Out, "Do you really want to destroy the kubernetes cluster %s?\n%s\n\n The operation cannot be rollbacked. Only 'yes' will be accepted to confirm.\n\n",
o.prevCluster.ClusterName, o.prevCluster.String())
// confirm to destroy
if !o.autoApprove {
entered, _ := prompt.NewPrompt("Enter a value:", nil, o.In).Run()
if entered != yesStr {
fmt.Fprintf(o.Out, "\nPlayground destroy cancelled.\n")
return cmdutil.ErrExit
}
}
o.startTime = time.Now()
// for cloud provider, we should delete all clusters created by KubeBlocks first,
// uninstall KubeBlocks and remove the KubeBlocks namespace, then destroy the
// playground cluster, avoid to leave resources behind.
// delete all clusters created by KubeBlocks, MUST BE VERY CAUTIOUS, use the right
// kubeconfig and context, otherwise, it will delete the wrong cluster.
if err = o.deleteClustersAndUninstallKB(); err != nil {
if strings.Contains(err.Error(), kubeClusterUnreachableErr.Error()) {
printer.Warning(o.Out, err.Error())
} else {
return err
}
}
// destroy playground kubernetes cluster
cpPath, err := cloudProviderRepoDir(o.prevCluster.KbcliVersion)
if err != nil {
return err
}
provider, err := cp.New(o.prevCluster.CloudProvider, cpPath, o.Out, o.ErrOut)
if err != nil {
return err
}
fmt.Fprintf(o.Out, "Destroy %s %s cluster %s...\n",
o.prevCluster.CloudProvider, cp.K8sService(o.prevCluster.CloudProvider), o.prevCluster.ClusterName)
if err = provider.DeleteK8sCluster(o.prevCluster); err != nil {
return err
}
// remove the cluster kubeconfig from the use default kubeconfig
if err = o.removeKubeConfig(); err != nil {
return err
}
// at last, remove the state file
if err = o.removeStateFile(); err != nil {
return err
}
fmt.Fprintf(o.Out, "Playground destroy completed in %s.\n", time.Since(o.startTime).Truncate(time.Second))
return nil
}
func (o *destroyOptions) deleteClustersAndUninstallKB() error {
var err error
if !o.purge {
klog.V(1).Infof("Skip to delete all clusters created by KubeBlocks and uninstall KubeBlocks")
return nil
}
if o.prevCluster.KubeConfig == "" {
fmt.Fprintf(o.Out, "No kubeconfig found for kubernetes cluster %s in %s \n",
o.prevCluster.ClusterName, o.stateFilePath)
return nil
}
// write kubeconfig content to a temporary file and use it
if err = writeAndUseKubeConfig(o.prevCluster.KubeConfig, o.kubeConfigPath, o.Out); err != nil {
return err
}
client, dynamic, err := getKubeClient()
if err != nil {
return err
}
// delete all clusters created by KubeBlocks
if err = o.deleteClusters(dynamic); err != nil {
return err
}
// uninstall KubeBlocks and remove namespace created by KubeBlocks
return o.uninstallKubeBlocks(client, dynamic)
}
// delete all clusters created by KubeBlocks
func (o *destroyOptions) deleteClusters(dynamic dynamic.Interface) error {
var err error
ctx := context.Background()
// get all clusters in all namespaces
getClusters := func() (*unstructured.UnstructuredList, error) {
return dynamic.Resource(types.ClusterGVR()).Namespace(metav1.NamespaceAll).
List(context.Background(), metav1.ListOptions{})
}
// get all clusters and check if satisfy the checkFn
checkClusters := func(checkFn func(cluster *appsv1alpha1.Cluster) bool) (bool, error) {
res := true
clusters, err := getClusters()
if err != nil {
return false, err
}
for _, item := range clusters.Items {
cluster := &appsv1alpha1.Cluster{}
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(item.Object, cluster); err != nil {
return false, err
}
if !checkFn(cluster) {
res = false
break
}
}
return res, nil
}
// delete all clusters
deleteClusters := func(clusters *unstructured.UnstructuredList) error {
for _, cluster := range clusters.Items {
if err = dynamic.Resource(types.ClusterGVR()).Namespace(cluster.GetNamespace()).
Delete(ctx, cluster.GetName(), *metav1.NewDeleteOptions(0)); err != nil {
return err
}
}
return nil
}
s := spinner.New(o.Out, spinnerMsg("Delete clusters created by KubeBlocks"))
defer s.Fail()
// get all clusters
clusters, err := getClusters()
if clusters == nil || len(clusters.Items) == 0 {
s.Success()
return nil
}
checkWipeOut := false
// set all cluster termination policy to WipeOut to delete all resources, otherwise
// the cluster will be deleted but the resources will be left
for _, item := range clusters.Items {
cluster := &appsv1alpha1.Cluster{}
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(item.Object, cluster); err != nil {
return err
}
if cluster.Spec.TerminationPolicy == appsv1alpha1.WipeOut {
continue
}
// terminate policy is not WipeOut, set it to WipeOut
klog.V(1).Infof("Set cluster %s termination policy to WipeOut", cluster.Name)
if _, err = dynamic.Resource(types.ClusterGVR()).Namespace(cluster.Namespace).Patch(ctx, cluster.Name, apitypes.JSONPatchType,
[]byte(fmt.Sprintf("[{\"op\": \"replace\", \"path\": \"/spec/terminationPolicy\", \"value\": \"%s\" }]",
appsv1alpha1.WipeOut)), metav1.PatchOptions{}); err != nil {
return err
}
// set some cluster termination policy to WipeOut, need to check again
checkWipeOut = true
}
// check all clusters termination policy is WipeOut
if checkWipeOut {
if err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second,
o.timeout, true, func(_ context.Context) (bool, error) {
return checkClusters(func(cluster *appsv1alpha1.Cluster) bool {
if cluster.Spec.TerminationPolicy != appsv1alpha1.WipeOut {
klog.V(1).Infof("Cluster %s termination policy is %s", cluster.Name, cluster.Spec.TerminationPolicy)
}
return cluster.Spec.TerminationPolicy == appsv1alpha1.WipeOut
})
}); err != nil {
return err
}
}
// delete all clusters
if err = deleteClusters(clusters); err != nil {
return err
}
// check and wait all clusters are deleted
if err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second,
o.timeout, true, func(_ context.Context) (bool, error) {
return checkClusters(func(cluster *appsv1alpha1.Cluster) bool {
// always return false if any cluster is not deleted
klog.V(1).Infof("Cluster %s is not deleted", cluster.Name)
return false
})
}); err != nil {
return err
}
s.Success()
return nil
}
func (o *destroyOptions) uninstallKubeBlocks(client kubernetes.Interface, dynamic dynamic.Interface) error {
var err error
uninstall := kubeblocks.UninstallOptions{
Options: kubeblocks.Options{
IOStreams: o.IOStreams,
Client: client,
Dynamic: dynamic,
Wait: true,
},
AutoApprove: true,
RemoveNamespace: true,
Quiet: true,
}
uninstall.HelmCfg = helm.NewConfig("", o.kubeConfigPath, "", klog.V(1).Enabled())
if err = uninstall.PreCheck(); err != nil {
return err
}
if err = uninstall.Uninstall(); err != nil {
return err
}
return nil
}
func (o *destroyOptions) removeKubeConfig() error {
s := spinner.New(o.Out, spinnerMsg("Remove kubeconfig from "+defaultKubeConfigPath))
defer s.Fail()
if err := kubeConfigRemove(o.prevCluster.KubeConfig, defaultKubeConfigPath); err != nil {
if os.IsNotExist(err) {
s.Success()
return nil
} else {
return err
}
}
s.Success()
clusterContext, err := kubeConfigCurrentContext(o.prevCluster.KubeConfig)
if err != nil {
return err
}
// check if current context in kubeconfig is deleted, if yes, notify user to set current context
currentContext, err := kubeConfigCurrentContextFromFile(defaultKubeConfigPath)
if err != nil {
return err
}
// current context is deleted, notify user to set current context with kubectl
if currentContext == clusterContext {
printer.Warning(o.Out, "this removed your active context, use \"kubectl config use-context\" to select a different one\n")
}
return nil
}
// remove state file
func (o *destroyOptions) removeStateFile() error {
s := spinner.New(o.Out, spinnerMsg("Remove state file %s", o.stateFilePath))
defer s.Fail()
if err := removeStateFile(o.stateFilePath); err != nil {
return err
}
s.Success()
return nil
}
func (o *destroyOptions) complete(cmd *cobra.Command) error {
// enable log
if err := util.EnableLogToFile(cmd.Flags()); err != nil {
return fmt.Errorf("failed to enable the log file %s", err.Error())
}
return nil
}