@@ -244,7 +244,7 @@ func (m *volumeIDToNameMap) get(volumeID string) (string, bool) {
244244// The primary key is the namespaced name of the PVC and value is a map.
245245// The key of the inner map is the name of the snapshot.
246246type pvcToSnapshotsMap struct {
247- * sync.RWMutex
247+ sync.RWMutex
248248 items map [k8stypes.NamespacedName ]map [string ]struct {}
249249}
250250
@@ -2017,6 +2017,44 @@ func nodeRemove(obj interface{}) {
20172017 k8sOrchestratorInstance .nodeIDToNameMap .remove (nodeMoID )
20182018}
20192019
2020+ // handleSnapshotAdded handles the snapshot add event by adding it to the pvcToSnapshotsMap cache.
2021+ func handleSnapshotAdded (ctx context.Context , obj any , pvcMap * pvcToSnapshotsMap ) {
2022+ log := logger .GetLogger (ctx )
2023+ snap , ok := obj .(* snapshotv1.VolumeSnapshot )
2024+ if ! ok || snap == nil {
2025+ log .Warnf ("unrecognized object %+v" , obj )
2026+ return
2027+ }
2028+
2029+ if snap .Spec .Source .PersistentVolumeClaimName == nil {
2030+ log .Warnf ("snapshot is not associated with any PVC. Ignoring it..." )
2031+ return
2032+ }
2033+
2034+ pvcMap .add (* snap .Spec .Source .PersistentVolumeClaimName , snap .Name , snap .Namespace )
2035+ log .With ("pvc" , * snap .Spec .Source .PersistentVolumeClaimName ).With ("snapshot" , snap .Name ).
2036+ With ("namespace" , snap .Namespace ).Debug ("successfully added the snapshot to the cache" )
2037+ }
2038+
2039+ // handleSnapshotDeleted handles the snapshot delete event by removing it from the pvcToSnapshotsMap cache.
2040+ func handleSnapshotDeleted (ctx context.Context , obj any , pvcMap * pvcToSnapshotsMap ) {
2041+ log := logger .GetLogger (ctx )
2042+ snap , ok := obj .(* snapshotv1.VolumeSnapshot )
2043+ if ! ok || snap == nil {
2044+ log .Warnf ("unrecognized object %+v" , obj )
2045+ return
2046+ }
2047+
2048+ if snap .Spec .Source .PersistentVolumeClaimName == nil {
2049+ log .Warnf ("snapshot is not associated with any PVC. Ignoring it..." )
2050+ return
2051+ }
2052+
2053+ pvcMap .delete (* snap .Spec .Source .PersistentVolumeClaimName , snap .Name , snap .Namespace )
2054+ log .With ("pvc" , * snap .Spec .Source .PersistentVolumeClaimName ).With ("snapshot" , snap .Name ).
2055+ With ("namespace" , snap .Namespace ).Debug ("successfully removed the snapshot from the cache" )
2056+ }
2057+
20202058func initPVCToSnapshotsMap (ctx context.Context , controllerClusterFlavor cnstypes.CnsClusterFlavor ) error {
20212059 log := logger .GetLogger (ctx )
20222060 // TODO: check if we need to check the FSS as well
@@ -2027,53 +2065,19 @@ func initPVCToSnapshotsMap(ctx context.Context, controllerClusterFlavor cnstypes
20272065 }
20282066
20292067 k8sOrchestratorInstance .pvcToSnapshotsMap = pvcToSnapshotsMap {
2030- RWMutex : & sync.RWMutex {},
2068+ RWMutex : sync.RWMutex {},
20312069 items : make (map [k8stypes.NamespacedName ]map [string ]struct {}),
20322070 }
2033- snapshotAdded := func (obj any ) {
2034- snap , ok := obj .(* snapshotv1.VolumeSnapshot )
2035- if ! ok || snap == nil {
2036- log .Warnf ("unrecognized object %+v" , obj )
2037- return
2038- }
2039-
2040- if snap .Spec .Source .PersistentVolumeClaimName == nil {
2041- log .Warnf ("snapshot is not associated with any PVC. Ignoring it..." )
2042- return
2043- }
2044-
2045- k8sOrchestratorInstance .pvcToSnapshotsMap .add (
2046- * snap .Spec .Source .PersistentVolumeClaimName , snap .Name , snap .Namespace )
2047- log .With ("pvc" , * snap .Spec .Source .PersistentVolumeClaimName ).With ("snapshot" , snap .Name ).
2048- With ("namespace" , snap .Namespace ).Debug ("successfully added the snapshot to the cache" )
2049- }
2050- snapshotDeleted := func (obj any ) {
2051- snap , ok := obj .(* snapshotv1.VolumeSnapshot )
2052- if ! ok || snap == nil {
2053- log .Warnf ("unrecognized object %+v" , obj )
2054- return
2055- }
2056-
2057- if snap .Spec .Source .PersistentVolumeClaimName == nil {
2058- log .Warnf ("snapshot is not associated with any PVC. Ignoring it..." )
2059- return
2060- }
2061-
2062- k8sOrchestratorInstance .pvcToSnapshotsMap .delete (
2063- * snap .Spec .Source .PersistentVolumeClaimName , snap .Name , snap .Namespace )
2064- log .With ("pvc" , * snap .Spec .Source .PersistentVolumeClaimName ).With ("snapshot" , snap .Name ).
2065- With ("namespace" , snap .Namespace ).Debug ("successfully removed the snapshot from the cache" )
2066- }
20672071
20682072 err := k8sOrchestratorInstance .informerManager .AddSnapshotListener (ctx ,
20692073 func (obj any ) {
2070- snapshotAdded ( obj )
2074+ handleSnapshotAdded ( ctx , obj , & k8sOrchestratorInstance . pvcToSnapshotsMap )
20712075 },
20722076 // Since the name of PVC associated with a snapshot is immutable,
20732077 // update events do not have any impact on the state of the cache.
20742078 nil ,
20752079 func (obj any ) {
2076- snapshotDeleted ( obj )
2080+ handleSnapshotDeleted ( ctx , obj , & k8sOrchestratorInstance . pvcToSnapshotsMap )
20772081 })
20782082 if err != nil {
20792083 return logger .LogNewErrorf (log , "failed to listen on volumesnapshots. Error: %v" , err )
0 commit comments