-
Notifications
You must be signed in to change notification settings - Fork 90
/
kotsadm.go
309 lines (278 loc) · 10 KB
/
kotsadm.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
package k8sutil
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/kotsadm/types"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)
func FindKotsadm(clientset *kubernetes.Clientset, namespace string) (string, error) {
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: "app=kotsadm"})
if err != nil {
return "", errors.Wrap(err, "failed to list pods")
}
for _, pod := range pods.Items {
if pod.Status.Phase == corev1.PodRunning {
return pod.Name, nil
}
}
return "", errors.New("unable to find kotsadm pod")
}
func DeleteKotsadm(clientset *kubernetes.Clientset, namespace string) error {
selectorLabels := map[string]string{
types.KotsadmKey: types.KotsadmLabelValue,
}
listOptions := metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(selectorLabels).String(),
}
// services (does not have a DeleteCollection method)
services, err := clientset.CoreV1().Services(namespace).List(context.TODO(), listOptions)
if err != nil {
return errors.Wrap(err, "failed to list services")
}
for _, service := range services.Items {
err := clientset.CoreV1().Services(namespace).Delete(context.TODO(), service.ObjectMeta.Name, metav1.DeleteOptions{})
if err != nil {
return errors.Wrapf(err, "failed to delete service %s in namespace %s", service.ObjectMeta.Name, service.ObjectMeta.Namespace)
}
}
if err := waitForDeleteServices(clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete services")
}
// deployments
err = clientset.AppsV1().Deployments(namespace).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm deployments")
}
if err := waitForDeleteDeployments(clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete deployments")
}
// statefulsets
err = clientset.AppsV1().StatefulSets(namespace).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm statefulsets")
}
if err := waitForDeleteStatefulSets(clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete statefulsets")
}
// pods
err = clientset.CoreV1().Pods(namespace).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm pods")
}
if err := waitForDeletePods(clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete pods")
}
// PVCs
err = clientset.CoreV1().PersistentVolumeClaims(namespace).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm PVCs")
}
if err := waitForDeletePVCs(clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete PVCs")
}
// secrets
err = clientset.CoreV1().Secrets(namespace).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm secrets")
}
if err := waitForDeleteSecrets(clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete secrets")
}
// configmaps
err = clientset.CoreV1().ConfigMaps(namespace).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm configmaps")
}
if err := waitForDeleteConfigmaps(clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete configmaps")
}
// cluster role bindings
err = clientset.RbacV1().ClusterRoleBindings().DeleteCollection(context.TODO(), metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm clusterrolebindings")
}
if err := waitForDeleteClusterRoleBindings(clientset, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete clusterrolebindings")
}
// role bindings
err = clientset.RbacV1().RoleBindings(namespace).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm rolebindings")
}
if err := waitForDeleteRoleBindings(clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete rolebindings")
}
// cluster roles
err = clientset.RbacV1().ClusterRoles().DeleteCollection(context.TODO(), metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm clusterroles")
}
if err := waitForDeleteClusterRoles(clientset, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete clusterroles")
}
// roles
err = clientset.RbacV1().Roles(namespace).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm roles")
}
if err := waitForDeleteRoles(clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete roles")
}
// service accounts
err = clientset.CoreV1().ServiceAccounts(namespace).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm serviceaccounts")
}
if err := waitForDeleteServiceAccounts(clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete serviceaccounts")
}
return nil
}
func waitForDeleteServices(clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
for {
services, err := clientset.CoreV1().Services(namespace).List(context.TODO(), listOptions)
if err != nil {
return errors.Wrap(err, "failed to list services")
}
if len(services.Items) == 0 {
return nil
}
time.Sleep(time.Second)
}
}
func waitForDeleteDeployments(clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
for {
deployments, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), listOptions)
if err != nil {
return errors.Wrap(err, "failed to list deployments")
}
if len(deployments.Items) == 0 {
return nil
}
time.Sleep(time.Second)
}
}
func waitForDeleteStatefulSets(clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
for {
statefulsets, err := clientset.AppsV1().StatefulSets(namespace).List(context.TODO(), listOptions)
if err != nil {
return errors.Wrap(err, "failed to list statefulsets")
}
if len(statefulsets.Items) == 0 {
return nil
}
time.Sleep(time.Second)
}
}
func waitForDeletePods(clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
for {
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), listOptions)
if err != nil {
return errors.Wrap(err, "failed to list pods")
}
if len(pods.Items) == 0 {
return nil
}
time.Sleep(time.Second)
}
}
func waitForDeletePVCs(clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
for {
pvcs, err := clientset.CoreV1().PersistentVolumeClaims(namespace).List(context.TODO(), listOptions)
if err != nil {
return errors.Wrap(err, "failed to list pvcs")
}
if len(pvcs.Items) == 0 {
return nil
}
time.Sleep(time.Second)
}
}
func waitForDeleteSecrets(clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
for {
secrets, err := clientset.CoreV1().Secrets(namespace).List(context.TODO(), listOptions)
if err != nil {
return errors.Wrap(err, "failed to list secrets")
}
if len(secrets.Items) == 0 {
return nil
}
time.Sleep(time.Second)
}
}
func waitForDeleteConfigmaps(clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
for {
configmaps, err := clientset.CoreV1().ConfigMaps(namespace).List(context.TODO(), listOptions)
if err != nil {
return errors.Wrap(err, "failed to list configmaps")
}
if len(configmaps.Items) == 0 {
return nil
}
time.Sleep(time.Second)
}
}
func waitForDeleteClusterRoleBindings(clientset *kubernetes.Clientset, listOptions metav1.ListOptions) error {
for {
crbs, err := clientset.RbacV1().ClusterRoleBindings().List(context.TODO(), listOptions)
if err != nil {
return errors.Wrap(err, "failed to list clusterrolebindings")
}
if len(crbs.Items) == 0 {
return nil
}
time.Sleep(time.Second)
}
}
func waitForDeleteRoleBindings(clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
for {
rbs, err := clientset.RbacV1().RoleBindings(namespace).List(context.TODO(), listOptions)
if err != nil {
return errors.Wrap(err, "failed to list rolebindings")
}
if len(rbs.Items) == 0 {
return nil
}
time.Sleep(time.Second)
}
}
func waitForDeleteClusterRoles(clientset *kubernetes.Clientset, listOptions metav1.ListOptions) error {
for {
crs, err := clientset.RbacV1().ClusterRoles().List(context.TODO(), listOptions)
if err != nil {
return errors.Wrap(err, "failed to list clusterroles")
}
if len(crs.Items) == 0 {
return nil
}
time.Sleep(time.Second)
}
}
func waitForDeleteRoles(clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
for {
roles, err := clientset.RbacV1().Roles(namespace).List(context.TODO(), listOptions)
if err != nil {
return errors.Wrap(err, "failed to list roles")
}
if len(roles.Items) == 0 {
return nil
}
time.Sleep(time.Second)
}
}
func waitForDeleteServiceAccounts(clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
for {
serviceAccounts, err := clientset.CoreV1().ServiceAccounts(namespace).List(context.TODO(), listOptions)
if err != nil {
return errors.Wrap(err, "failed to list serviceaccounts")
}
if len(serviceAccounts.Items) == 0 {
return nil
}
time.Sleep(time.Second)
}
}