-
Notifications
You must be signed in to change notification settings - Fork 201
Enhancement - Cleanup Orphaned PVCs #3789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,10 +21,15 @@ import ( | |
| "time" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes" | ||
| "k8s.io/client-go/rest" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
| cnsoperatorv1alpha1 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/cnsoperator" | ||
| batchattachv1a1 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/cnsoperator/cnsnodevmbatchattachment/v1alpha1" | ||
| cnsregistervolumev1alpha1 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/cnsoperator/cnsregistervolume/v1alpha1" | ||
| cnsunregistervolumev1alpha1 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/cnsoperator/cnsunregistervolume/v1alpha1" | ||
| "sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common/commonco" | ||
| cnsoperatortypes "sigs.k8s.io/vsphere-csi-driver/v3/pkg/syncer/cnsoperator/types" | ||
|
|
||
| "sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/logger" | ||
| k8s "sigs.k8s.io/vsphere-csi-driver/v3/pkg/kubernetes" | ||
|
|
@@ -109,3 +114,76 @@ func cleanUpCnsUnregisterVolumeInstances(ctx context.Context, restClientConfig * | |
| } | ||
| } | ||
| } | ||
|
|
||
| var ( | ||
| newClientForGroup = k8s.NewClientForGroup | ||
| newForConfig = func(config *rest.Config) (kubernetes.Interface, error) { | ||
| return kubernetes.NewForConfig(config) | ||
| } | ||
| ) | ||
|
|
||
| // cleanupPVCs removes the `CNSPvcFinalizer` from the PVCs in cases | ||
| // where the CnsNodeVmBatchAttachment CR gets deleted before removing the finalizer. | ||
| // This is EXTREMELY UNLIKELY to happen but still a possibility that has to be addressed. | ||
| func cleanupPVCs(ctx context.Context, config rest.Config) { | ||
| log := logger.GetLogger(ctx) | ||
|
|
||
| pvcList := commonco.ContainerOrchestratorUtility.ListPVCs(ctx, "") | ||
| if len(pvcList) == 0 { | ||
| log.Info("no PVCs found. Exiting...") | ||
| return | ||
| } | ||
|
|
||
| // map to hold all the PVCs that have the finalizer added by the batch attach reconciler | ||
| pvcMap := make(map[string]map[string]struct{}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A description of what this map holds will be helpful |
||
| for _, pvc := range pvcList { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider only those PVCs which have deletion timestamp
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be a more general purpose finalizer remover. We do not have to wait for the pvc to get deleted to cleanup - as long as a PVC has the finalizer but no batch attach CR, the finalizer can be removed.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, there can be PVCs being used by pod VMs or attached via the old CnsNodeVMAttachment CR. We should not be removing finalizer from them.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. I see that older node vm attach and file access configs are also using this CR - but PodVMs won't have this finalizer on them. Let me think more on how to address this. |
||
| // Check if PVC has the CNS finalizer in metadata.finalizers | ||
| if !controllerutil.ContainsFinalizer(pvc, cnsoperatortypes.CNSPvcFinalizer) { | ||
| // not a PVC that is attached to or being attached to a VM. Can be ignored. | ||
| continue | ||
| } | ||
|
|
||
| if _, ok := pvcMap[pvc.Namespace]; !ok { | ||
| pvcMap[pvc.Namespace] = map[string]struct{}{} | ||
| } | ||
| pvcMap[pvc.Namespace][pvc.Name] = struct{}{} | ||
| } | ||
|
|
||
| cnsClient, err := newClientForGroup(ctx, &config, cnsoperatorv1alpha1.GroupName) | ||
| if err != nil { | ||
| log.Error("failed to create cns operator client") | ||
| return | ||
| } | ||
|
|
||
| batchAttachList := batchattachv1a1.CnsNodeVMBatchAttachmentList{} | ||
| err = cnsClient.List(ctx, &batchAttachList) | ||
| if err != nil { | ||
| log.With("kind", batchAttachList.Kind).Error("listing failed") | ||
| return | ||
| } | ||
|
|
||
| for _, cr := range batchAttachList.Items { | ||
| for _, vol := range cr.Spec.Volumes { | ||
| // Any PVCs that are still in the spec can be safely ignored to be processed by the reconciler. | ||
| delete(pvcMap[cr.Namespace], vol.PersistentVolumeClaim.ClaimName) | ||
| } | ||
| } | ||
|
|
||
| c, err := newForConfig(&config) | ||
| if err != nil { | ||
| log.Error("failed to create core API client") | ||
| return | ||
| } | ||
|
|
||
| // Remove the finalizer for the remaining PVCs | ||
| for namespace, pvcs := range pvcMap { | ||
| for name := range pvcs { | ||
| err := k8s.RemoveFinalizerFromPVC(ctx, c, name, namespace, cnsoperatortypes.CNSPvcFinalizer) | ||
| if err != nil { | ||
| log.With("name", name).With("namespace", namespace). | ||
| Error("failed to remove the finalizer") | ||
| continue | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.