-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add finalizer to VolumeSnapshot when create PVC with it
This patch implement a PVC watcher. When the watcher receive a PVC ADDED event and PVC with VolumeSnapshot kind dataSource, the VolumeSnapshot will be added a finalizer. And the finalizer will be removed when the watcher receive the PVC's DELETED event. NOTE: Asynchronously add finalizer by watcher seems like unsafety, might have some potential issues. After the PR [1] merged and released, we need to refactor it and implement a synchronous method [1] kubernetes-csi/external-provisioner#1070
- Loading branch information
Showing
4 changed files
with
133 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package cinder | ||
|
||
import ( | ||
"fmt" | ||
|
||
snap "github.com/kubernetes-csi/external-snapshotter/client/v6/clientset/versioned" | ||
"golang.org/x/net/context" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
) | ||
|
||
const ( | ||
annStorageProvisioner = "volume.kubernetes.io/storage-provisioner" | ||
snapshotKind = "VolumeSnapshot" | ||
) | ||
|
||
type controllerWatcher struct { | ||
snapClient snap.Clientset | ||
watcher watch.Interface | ||
} | ||
|
||
func (cw *controllerWatcher) run() { | ||
klog.V(2).Info("Controller watcher started") | ||
for event := range cw.watcher.ResultChan() { | ||
obj := event.Object.DeepCopyObject() | ||
pvc, ok := obj.(*corev1.PersistentVolumeClaim) | ||
if !ok { | ||
continue | ||
} | ||
if pvc.ObjectMeta.Annotations[annStorageProvisioner] != driverName { | ||
continue | ||
} | ||
if pvc.Spec.DataSource == nil || pvc.Spec.DataSource.Kind != snapshotKind { | ||
continue | ||
} | ||
snapshotObj, err := cw.snapClient.SnapshotV1().VolumeSnapshots(pvc.Namespace).Get(context.Background(), pvc.Spec.DataSource.Name, metav1.GetOptions{}) | ||
if err != nil { | ||
klog.ErrorS(err, "Error get VolumeSnapshot", "namespace", pvc.Namespace, "name", pvc.Spec.DataSource.Name) | ||
continue | ||
} | ||
update := false | ||
finalizer := fmt.Sprintf("pvc-%s", pvc.UID) | ||
// TODO(JeffYang): Asynchronously add finalizer seems like unsafety, might have some potential issues. | ||
// Move it into controllerServer CreateVolume after the PR https://github.com/kubernetes-csi/external-provisioner/pull/1070 merged and released. | ||
// We can synchronously add finalizer there | ||
if event.Type == watch.Added { | ||
klog.V(5).InfoS("Add finalizer to VolumeSnapshot", "PersistentVolumeClaim", pvc.Name, "VolumeSnapshot", snapshotObj.Name) | ||
update = controllerutil.AddFinalizer(snapshotObj, finalizer) | ||
} | ||
if event.Type == watch.Deleted { | ||
klog.V(5).InfoS("Remove finalizer to VolumeSnapshot", "PersistentVolumeClaim", pvc.Name, "VolumeSnapshot", snapshotObj.Name) | ||
update = controllerutil.RemoveFinalizer(snapshotObj, finalizer) | ||
} | ||
if update { | ||
_, err := cw.snapClient.SnapshotV1().VolumeSnapshots(pvc.Namespace).Update(context.Background(), snapshotObj, metav1.UpdateOptions{}) | ||
if err != nil { | ||
klog.ErrorS(err, "Error update VolumeSnapshot", "name", snapshotObj.Name, "finalizer", finalizer) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func StartContollerWatcher(restConfig *rest.Config) error { | ||
snapClient, err := snap.NewForConfig(restConfig) | ||
if err != nil { | ||
klog.ErrorS(err, "Error building snapshot clientset") | ||
return err | ||
} | ||
watcher, err := client.NewWithWatch(restConfig, client.Options{}) | ||
if err != nil { | ||
klog.ErrorS(err, "Error building watcher") | ||
return err | ||
} | ||
watchInterface, err := watcher.Watch(context.TODO(), &corev1.PersistentVolumeClaimList{}) | ||
if err != nil { | ||
klog.ErrorS(err, "Error watching PersistentVolumeClaim") | ||
return err | ||
} | ||
cw := controllerWatcher{snapClient: *snapClient, watcher: watchInterface} | ||
go cw.run() | ||
return nil | ||
} |