Skip to content

Commit a9b8c0b

Browse files
committed
Implemented handlers for snapshot create and delete events
1 parent 0571c11 commit a9b8c0b

File tree

1 file changed

+63
-21
lines changed

1 file changed

+63
-21
lines changed

pkg/csi/service/common/commonco/k8sorchestrator/k8sorchestrator.go

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -248,21 +248,12 @@ func (n namespacedName) String() string {
248248
return fmt.Sprintf("%s/%s", n.namespace, n.name)
249249
}
250250

251-
type snapshotInfo struct {
252-
snapshotName string
253-
snapshotContentName string
254-
}
255-
256-
func (s snapshotInfo) String() string {
257-
return fmt.Sprintf("name: %s, content name: %s", s.snapshotName, s.snapshotContentName)
258-
}
259-
260251
// pvcToSnapshotsMap maps a PVC to its snapshots.
261252
// Key is the namespaced name of the PVC and value is a map.
262253
// The key of the inner map is the namespaced name of the snapshot.
263254
type pvcToSnapshotsMap struct {
264255
*sync.RWMutex
265-
items map[namespacedName]map[namespacedName]snapshotInfo
256+
items map[namespacedName]map[namespacedName]struct{}
266257
}
267258

268259
// K8sOrchestrator defines set of properties specific to K8s.
@@ -277,7 +268,7 @@ type K8sOrchestrator struct {
277268
nodeIDToNameMap *nodeIDToNameMap
278269
volumeNameToNodesMap *volumeNameToNodesMap // used when ListVolume FSS is enabled
279270
volumeIDToNameMap *volumeIDToNameMap // used when ListVolume FSS is enabled
280-
pvcToSnapshotsMap *pvcToSnapshotsMap
271+
pvcToSnapshotsMap pvcToSnapshotsMap
281272
k8sClient clientset.Interface
282273
snapshotterClient snapshotterClientSet.Interface
283274
// pvcUIDCache maps PVC UID to its namespaced name (namespace/name).
@@ -2327,34 +2318,85 @@ func initPVCToSnapshotsMap(ctx context.Context, controllerClusterFlavor cnstypes
23272318
return nil
23282319
}
23292320

2330-
log.Debugf("Initializing pvc namespaced name to volumesnapshot names map")
2331-
k8sOrchestratorInstance.pvcToSnapshotsMap = &pvcToSnapshotsMap{
2321+
k8sOrchestratorInstance.pvcToSnapshotsMap = pvcToSnapshotsMap{
23322322
RWMutex: &sync.RWMutex{},
2333-
items: make(map[namespacedName]map[namespacedName]snapshotInfo),
2323+
items: make(map[namespacedName]map[namespacedName]struct{}),
23342324
}
23352325

2336-
snapshotAdded := func(obj interface{}) {
2326+
snapshotAdded := func(obj any) {
23372327
snap, ok := obj.(*snapshotv1.VolumeSnapshot)
23382328
if !ok || snap == nil {
23392329
log.Warnf("snapshotAdded: unrecognized object %+v", obj)
23402330
return
23412331
}
23422332

2343-
// TODO: implement
23442333
log.Infof("snapshotAdded: snapshot=%v", snap)
2334+
if snap.Spec.Source.PersistentVolumeClaimName == nil {
2335+
log.Warnf("snapshotAdded: snapshot is not associated with any PVC. Ignoring it...")
2336+
return
2337+
}
2338+
2339+
k8sOrchestratorInstance.pvcToSnapshotsMap.Lock()
2340+
defer k8sOrchestratorInstance.pvcToSnapshotsMap.Unlock()
2341+
2342+
pvcName := namespacedName{
2343+
namespace: snap.Namespace,
2344+
name: *snap.Spec.Source.PersistentVolumeClaimName,
2345+
}
2346+
if _, ok := k8sOrchestratorInstance.pvcToSnapshotsMap.items[pvcName]; !ok {
2347+
k8sOrchestratorInstance.pvcToSnapshotsMap.items[pvcName] = make(map[namespacedName]struct{})
2348+
}
2349+
snapName := namespacedName{
2350+
namespace: snap.Namespace,
2351+
name: snap.Name,
2352+
}
2353+
k8sOrchestratorInstance.pvcToSnapshotsMap.items[pvcName][snapName] = struct{}{}
2354+
log.With("pvc", pvcName).With("snapshot", snapName).Debug("successfully added the snapshot to the cache")
2355+
}
2356+
2357+
snapshotDeleted := func(obj any) {
2358+
snap, ok := obj.(*snapshotv1.VolumeSnapshot)
2359+
if !ok || snap == nil {
2360+
log.Warnf("snapshotDeleted: unrecognized object %+v", obj)
2361+
return
2362+
}
2363+
2364+
log.Infof("snapshotDeleted: snapshot=%v", snap)
2365+
if snap.Spec.Source.PersistentVolumeClaimName == nil {
2366+
log.Warnf("snapshotDeleted: snapshot is not associated with any PVC. Ignoring it...")
2367+
return
2368+
}
2369+
2370+
k8sOrchestratorInstance.pvcToSnapshotsMap.Lock()
2371+
defer k8sOrchestratorInstance.pvcToSnapshotsMap.Unlock()
2372+
2373+
pvcName := namespacedName{
2374+
namespace: snap.Namespace,
2375+
name: *snap.Spec.Source.PersistentVolumeClaimName,
2376+
}
2377+
if _, ok := k8sOrchestratorInstance.pvcToSnapshotsMap.items[pvcName]; !ok {
2378+
log.Infof("snapshotDeleted: associated PVC %s not found in the map. Ignoring the delete event...", pvcName)
2379+
return
2380+
}
2381+
2382+
snapName := namespacedName{
2383+
namespace: snap.Namespace,
2384+
name: snap.Name,
2385+
}
2386+
delete(k8sOrchestratorInstance.pvcToSnapshotsMap.items[pvcName], snapName)
2387+
log.With("pvc", pvcName).With("snapshot", snapName).Debug("successfully removed the snapshot from the cache")
23452388
}
23462389

23472390
err := k8sOrchestratorInstance.informerManager.AddSnapshotListener(ctx,
2348-
func(obj interface{}) {
2391+
func(obj any) {
23492392
snapshotAdded(obj)
23502393
},
2351-
func(oldObj, newObj interface{}) {
2394+
func(oldObj, newObj any) {
23522395
// TODO: implement
23532396
log.Info("snapshotUpdated")
23542397
},
2355-
func(obj interface{}) {
2356-
// TODO: implement
2357-
log.Info("snapshotDeleted")
2398+
func(obj any) {
2399+
snapshotDeleted(obj)
23582400
})
23592401
if err != nil {
23602402
return logger.LogNewErrorf(log, "failed to listen on volumesnapshots. Error: %v", err)

0 commit comments

Comments
 (0)