-
Notifications
You must be signed in to change notification settings - Fork 9
/
delete.go
116 lines (96 loc) · 3.02 KB
/
delete.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package kubernetes
import (
"context"
"fmt"
"io"
"golang.org/x/exp/slices"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/framework/rpcerrors/multierr"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/std/tasks"
)
func (r *Cluster) DeleteAllRecursively(ctx context.Context, wait bool, progress io.Writer) (bool, error) {
namespaces, err := r.cli.CoreV1().Namespaces().List(ctx, metav1.ListOptions{
LabelSelector: kubedef.SerializeSelector(kubedef.ManagedByUs()),
})
if err != nil {
return false, fnerrors.Wrapf(nil, err, "unable to list namespaces")
}
var filtered []string
for _, ns := range namespaces.Items {
// Only delete namespaces that were used to deploy an environment.
if _, ok := ns.Labels[kubedef.K8sEnvName]; !ok {
continue
}
filtered = append(filtered, ns.Name)
}
return DeleteAllRecursively(ctx, r.cli, wait, progress, filtered...)
}
func DeleteAllRecursively(ctx context.Context, cli *kubernetes.Clientset, wait bool, progress io.Writer, namespaces ...string) (bool, error) {
if len(namespaces) == 0 {
return false, nil
}
return tasks.Return(ctx, tasks.Action("kubernetes.namespace.delete").Arg("namespaces", namespaces), func(ctx context.Context) (bool, error) {
var w watch.Interface
if wait {
// Start watching before emitting any Delete, to make sure we observe all events.
watcher, err := cli.CoreV1().Namespaces().Watch(ctx, metav1.ListOptions{})
if err != nil {
return false, err
}
w = watcher
defer watcher.Stop()
}
var removed []string
var errs []error
for _, ns := range namespaces {
var grace int64 = 0
if err := cli.CoreV1().Namespaces().Delete(ctx, ns, metav1.DeleteOptions{
GracePeriodSeconds: &grace,
}); err != nil {
// Namespace already deleted?
if !k8serrors.IsNotFound(err) {
errs = append(errs, err)
}
} else {
removed = append(removed, ns)
}
}
if len(errs) > 0 {
return false, multierr.New(errs...)
}
if !wait || len(removed) == 0 {
return len(removed) > 0, nil
}
return tasks.Return(ctx, tasks.Action("kubernetes.namespace.delete-wait").Arg("namespaces", removed), func(context.Context) (bool, error) {
for ev := range w.ResultChan() {
if ev.Type != watch.Deleted {
continue
}
ns, ok := ev.Object.(*v1.Namespace)
if !ok {
continue
}
idx := slices.Index(removed, ns.Name)
if idx >= 0 {
removed = slices.Delete(removed, idx, idx+1)
if progress != nil {
fmt.Fprintf(progress, "Namespace %q removed.\n", ns.Name)
}
}
if len(removed) == 0 {
break
}
}
return true, nil
})
})
}