-
Notifications
You must be signed in to change notification settings - Fork 90
/
kotsadm.go
521 lines (466 loc) · 17 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package k8sutil
import (
"context"
"os"
"time"
"github.com/pkg/errors"
kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types"
"github.com/replicatedhq/kots/pkg/util"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)
const (
KotsadmIDConfigMapName = "kotsadm-id"
)
func FindKotsadmImage(namespace string) (string, error) {
clientset, err := GetClientset()
if err != nil {
return "", errors.Wrap(err, "failed to get k8s client set")
}
var containers []corev1.Container
if os.Getenv("POD_OWNER_KIND") == "deployment" {
kotsadmDeployment, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), "kotsadm", metav1.GetOptions{})
if err != nil {
return "", errors.Wrap(err, "failed to get kotsadm deployment")
}
containers = kotsadmDeployment.Spec.Template.Spec.Containers
} else {
kotsadmStatefulSet, err := clientset.AppsV1().StatefulSets(namespace).Get(context.TODO(), "kotsadm", metav1.GetOptions{})
if err != nil {
return "", errors.Wrap(err, "failed to get kotsadm statefulset")
}
containers = kotsadmStatefulSet.Spec.Template.Spec.Containers
}
apiContainerIndex := -1
for i, container := range containers {
if container.Name == "kotsadm" {
apiContainerIndex = i
break
}
}
if apiContainerIndex == -1 {
return "", errors.New("kotsadm container not found")
}
kotsadmImage := containers[apiContainerIndex].Image
return kotsadmImage, nil
}
// IsKotsadmClusterScoped will check if kotsadm has cluster scope access or not
func IsKotsadmClusterScoped(ctx context.Context, clientset kubernetes.Interface, namespace string) bool {
rb, err := clientset.RbacV1().ClusterRoleBindings().Get(ctx, "kotsadm-rolebinding", metav1.GetOptions{})
if err != nil {
return false
}
for _, s := range rb.Subjects {
if s.Kind != "ServiceAccount" {
continue
}
if s.Name != "kotsadm" {
continue
}
if s.Namespace != "" && s.Namespace == namespace {
return true
}
if s.Namespace == "" && namespace == metav1.NamespaceDefault {
return true
}
}
return false
}
func GetKotsadmIDConfigMap() (*corev1.ConfigMap, error) {
clientset, err := GetClientset()
if err != nil {
return nil, errors.Wrap(err, "failed to get clientset")
}
namespace := util.PodNamespace
existingConfigmap, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), KotsadmIDConfigMapName, metav1.GetOptions{})
if err != nil && !kuberneteserrors.IsNotFound(err) {
return nil, errors.Wrap(err, "failed to get configmap")
} else if kuberneteserrors.IsNotFound(err) {
return nil, nil
}
return existingConfigmap, nil
}
func CreateKotsadmIDConfigMap(kotsadmID string) error {
var err error = nil
clientset, err := GetClientset()
if err != nil {
return err
}
configmap := corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: KotsadmIDConfigMapName,
Namespace: util.PodNamespace,
Labels: map[string]string{
kotsadmtypes.KotsadmKey: kotsadmtypes.KotsadmLabelValue,
kotsadmtypes.ExcludeKey: kotsadmtypes.ExcludeValue,
},
},
Data: map[string]string{"id": kotsadmID},
}
_, err = clientset.CoreV1().ConfigMaps(util.PodNamespace).Create(context.TODO(), &configmap, metav1.CreateOptions{})
return err
}
func IsKotsadmIDConfigMapPresent() (bool, error) {
clientset, err := GetClientset()
if err != nil {
return false, errors.Wrap(err, "failed to get clientset")
}
namespace := util.PodNamespace
_, err = clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), KotsadmIDConfigMapName, metav1.GetOptions{})
if err != nil && !kuberneteserrors.IsNotFound(err) {
return false, errors.Wrap(err, "failed to get configmap")
} else if kuberneteserrors.IsNotFound(err) {
return false, nil
}
return true, nil
}
func UpdateKotsadmIDConfigMap(kotsadmID string) error {
clientset, err := GetClientset()
if err != nil {
return errors.Wrap(err, "failed to get clientset")
}
namespace := util.PodNamespace
existingConfigMap, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), KotsadmIDConfigMapName, metav1.GetOptions{})
if err != nil && !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get configmap")
} else if kuberneteserrors.IsNotFound(err) {
return nil
}
if existingConfigMap.Data == nil {
existingConfigMap.Data = map[string]string{}
}
existingConfigMap.Data["id"] = kotsadmID
_, err = clientset.CoreV1().ConfigMaps(util.PodNamespace).Update(context.Background(), existingConfigMap, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "failed to update config map")
}
return nil
}
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 WaitForKotsadm(clientset *kubernetes.Clientset, namespace string, timeoutWaitingForWeb time.Duration) (string, error) {
start := time.Now()
for {
// todo, find service, not pod
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")
}
readyPods := []corev1.Pod{}
for _, pod := range pods.Items {
if pod.Status.Phase == corev1.PodRunning {
if pod.Status.ContainerStatuses[0].Ready {
readyPods = append(readyPods, pod)
}
}
}
// kotsadm pods from different owners (deployment, statefulset) may co-exist for a brief period of time
// during the upgrade process from versions pre 1.47.0 to 1.47+. we can't just check that the owner is a statefulset
// because full snapshots taken before 1.47.0 will have kotsadm as a deployment and the restore will hang waiting for a statefulset.
if len(readyPods) > 0 && PodsHaveTheSameOwner(readyPods) {
return readyPods[0].Name, nil
}
time.Sleep(time.Second)
if time.Since(start) > timeoutWaitingForWeb {
return "", &kotsadmtypes.ErrorTimeout{Message: "timeout waiting for kotsadm pod"}
}
}
}
func DeleteKotsadm(ctx context.Context, clientset *kubernetes.Clientset, namespace string, isKurl bool) error {
selectorLabels := map[string]string{
kotsadmtypes.KotsadmKey: kotsadmtypes.KotsadmLabelValue,
}
listOptions := metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(selectorLabels).String(),
}
// services (does not have a DeleteCollection method)
services, err := clientset.CoreV1().Services(namespace).List(ctx, listOptions)
if err != nil {
return errors.Wrap(err, "failed to list services")
}
for _, service := range services.Items {
err := clientset.CoreV1().Services(namespace).Delete(ctx, 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(ctx, clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete services")
}
// deployments
err = clientset.AppsV1().Deployments(namespace).DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm deployments")
}
if err := waitForDeleteDeployments(ctx, clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete deployments")
}
// statefulsets
err = clientset.AppsV1().StatefulSets(namespace).DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm statefulsets")
}
if err := waitForDeleteStatefulSets(ctx, clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete statefulsets")
}
// pods
err = clientset.CoreV1().Pods(namespace).DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm pods")
}
if err := waitForDeletePods(ctx, clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete pods")
}
// PVCs
err = clientset.CoreV1().PersistentVolumeClaims(namespace).DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm PVCs")
}
if err := waitForDeletePVCs(ctx, clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete PVCs")
}
// secrets
err = clientset.CoreV1().Secrets(namespace).DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm secrets")
}
if err := waitForDeleteSecrets(ctx, clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete secrets")
}
// configmaps
err = clientset.CoreV1().ConfigMaps(namespace).DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm configmaps")
}
if err := waitForDeleteConfigmaps(ctx, clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete configmaps")
}
// cluster role bindings
err = clientset.RbacV1().ClusterRoleBindings().DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm clusterrolebindings")
}
if err := waitForDeleteClusterRoleBindings(ctx, clientset, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete clusterrolebindings")
}
// role bindings
err = clientset.RbacV1().RoleBindings(namespace).DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm rolebindings")
}
if err := waitForDeleteRoleBindings(ctx, clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete rolebindings")
}
// cluster roles
err = clientset.RbacV1().ClusterRoles().DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm clusterroles")
}
if err := waitForDeleteClusterRoles(ctx, clientset, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete clusterroles")
}
// roles
err = clientset.RbacV1().Roles(namespace).DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm roles")
}
if err := waitForDeleteRoles(ctx, clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete roles")
}
// service accounts
err = clientset.CoreV1().ServiceAccounts(namespace).DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm serviceaccounts")
}
if err := waitForDeleteServiceAccounts(ctx, clientset, namespace, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete serviceaccounts")
}
if !isKurl {
return nil
}
// kURL registry
registryNS := "kurl"
err = clientset.AppsV1().Deployments(registryNS).DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete registry deployments")
}
if err := waitForDeleteDeployments(ctx, clientset, registryNS, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete registry deployments")
}
err = clientset.CoreV1().Pods(registryNS).DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete registry pods")
}
if err := waitForDeletePods(ctx, clientset, registryNS, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete registry pods")
}
err = clientset.CoreV1().Secrets(registryNS).DeleteCollection(ctx, metav1.DeleteOptions{}, listOptions)
if err != nil {
return errors.Wrap(err, "failed to delete kotsadm secrets")
}
if err := waitForDeleteSecrets(ctx, clientset, registryNS, listOptions); err != nil {
return errors.Wrap(err, "failed to wait for delete secrets")
}
return nil
}
func waitForDeleteServices(ctx context.Context, clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
for {
services, err := clientset.CoreV1().Services(namespace).List(ctx, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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)
}
}