/
volumes.go
151 lines (122 loc) · 5.28 KB
/
volumes.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 clusterdeletion
import (
"context"
"fmt"
"go.uber.org/zap"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
"k8c.io/reconciler/pkg/reconciling"
corev1 "k8s.io/api/core/v1"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
annotationKeyDescription = "description"
// AnnDynamicallyProvisioned is added to a PV that is dynamically provisioned by kubernetes
// Because the annotation is defined only at k8s.io/kubernetes, copying the content instead of vendoring
// https://github.com/kubernetes/kubernetes/blob/v1.21.0/pkg/controller/volume/persistentvolume/util/util.go#L65
AnnDynamicallyProvisioned = "pv.kubernetes.io/provisioned-by"
)
func (d *Deletion) cleanupVolumes(ctx context.Context, log *zap.SugaredLogger, cluster *kubermaticv1.Cluster) (deletedSomeResource bool, err error) {
userClusterClient, err := d.userClusterClientGetter()
if err != nil {
return false, err
}
// We disable the PV & PVC creation so nothing creates new PV's while we delete them
if err := d.disablePVCreation(ctx, userClusterClient); err != nil {
return false, fmt.Errorf("failed to disable future PV & PVC creation: %w", err)
}
pvcList := &corev1.PersistentVolumeClaimList{}
if err := userClusterClient.List(ctx, pvcList); err != nil {
return false, fmt.Errorf("failed to list PVCs from user cluster: %w", err)
}
allPVList := &corev1.PersistentVolumeList{}
if err := userClusterClient.List(ctx, allPVList); err != nil {
return false, fmt.Errorf("failed to list PVs from user cluster: %w", err)
}
pvList := &corev1.PersistentVolumeList{}
for _, pv := range allPVList.Items {
// Check only dynamically provisioned PVs with delete reclaim policy to verify provisioner has done the cleanup
// this filters out everything else because we leave those be
if pv.Annotations[AnnDynamicallyProvisioned] != "" && pv.Spec.PersistentVolumeReclaimPolicy == corev1.PersistentVolumeReclaimDelete {
pvList.Items = append(pvList.Items, pv)
}
}
// Do not attempt to delete any pods when there are no PVs and PVCs
if len(pvcList.Items) == 0 && len(pvList.Items) == 0 {
return deletedSomeResource, nil
}
// Delete all Pods that use PVs. We must keep the remaining pods, otherwise
// we end up in a deadlock when CSI is used
if err := d.cleanupPVCUsingPods(ctx, log, userClusterClient); err != nil {
return false, fmt.Errorf("failed to clean up PV using pod from user cluster: %w", err)
}
// Delete PVC's
for _, pvc := range pvcList.Items {
if pvc.DeletionTimestamp == nil {
identifier := fmt.Sprintf("%s/%s", pvc.Namespace, pvc.Name)
log.Infow("Deleting PVC...", "pvc", identifier)
if err := userClusterClient.Delete(ctx, &pvc); ctrlruntimeclient.IgnoreNotFound(err) != nil {
return deletedSomeResource, fmt.Errorf("failed to delete PVC from user cluster: %w", err)
}
deletedSomeResource = true
}
}
if len(pvList.Items) > 0 {
// We don't delete PVs but we want to wait for provisioners to cleanup dynamically provisioned PVs
// pretend we need to requeue to avoid removing finalizer prematurely
deletedSomeResource = true
}
return deletedSomeResource, nil
}
func (d *Deletion) disablePVCreation(ctx context.Context, userClusterClient ctrlruntimeclient.Client) error {
// Prevent re-creation of PVs and PVCs by using an intentionally defunct admissionWebhook
creatorGetters := []reconciling.NamedValidatingWebhookConfigurationReconcilerFactory{
creationPreventingWebhook("", []string{"persistentvolumes", "persistentvolumeclaims"}),
}
if err := reconciling.ReconcileValidatingWebhookConfigurations(ctx, creatorGetters, "", userClusterClient); err != nil {
return fmt.Errorf("failed to create ValidatingWebhookConfiguration to prevent creation of PVs/PVCs: %w", err)
}
return nil
}
func (d *Deletion) cleanupPVCUsingPods(ctx context.Context, log *zap.SugaredLogger, userClusterClient ctrlruntimeclient.Client) error {
podList := &corev1.PodList{}
if err := userClusterClient.List(ctx, podList); err != nil {
return fmt.Errorf("failed to list Pods from user cluster: %w", err)
}
pvUsingPods := []*corev1.Pod{}
for idx := range podList.Items {
pod := &podList.Items[idx]
if podUsesPV(pod) {
pvUsingPods = append(pvUsingPods, pod)
}
}
for _, pod := range pvUsingPods {
if pod.DeletionTimestamp == nil {
identifier := fmt.Sprintf("%s/%s", pod.Namespace, pod.Name)
log.Infow("Deleting Pod...", "pod", identifier)
if err := userClusterClient.Delete(ctx, pod); ctrlruntimeclient.IgnoreNotFound(err) != nil {
return fmt.Errorf("failed to delete Pod: %w", err)
}
}
}
return nil
}
func podUsesPV(p *corev1.Pod) bool {
for _, volume := range p.Spec.Volumes {
if volume.VolumeSource.PersistentVolumeClaim != nil {
return true
}
}
return false
}