Skip to content

Commit a4ea357

Browse files
committed
Changed the pvcToSnapshotsMap type and addressed some TODOs
1 parent d5507fa commit a4ea357

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,11 @@ func (n namespacedName) String() string {
249249
}
250250

251251
// pvcToSnapshotsMap maps a PVC to its snapshots.
252-
// Key is the namespaced name of the PVC and value is a map.
253-
// The key of the inner map is the namespaced name of the snapshot.
254-
// TODO: change the key of the inner map to string
252+
// The primary key is the namespaced name of the PVC and value is a map.
253+
// The key of the inner map is the name of the snapshot.
255254
type pvcToSnapshotsMap struct {
256255
*sync.RWMutex
257-
items map[namespacedName]map[namespacedName]struct{}
256+
items map[namespacedName]map[string]struct{}
258257
}
259258

260259
func (m *pvcToSnapshotsMap) add(pvc, snapshot, namespace string) {
@@ -266,13 +265,9 @@ func (m *pvcToSnapshotsMap) add(pvc, snapshot, namespace string) {
266265
name: pvc,
267266
}
268267
if _, ok := m.items[pvcKey]; !ok {
269-
m.items[pvcKey] = make(map[namespacedName]struct{})
268+
m.items[pvcKey] = make(map[string]struct{})
270269
}
271-
snapKey := namespacedName{
272-
namespace: namespace,
273-
name: snapshot,
274-
}
275-
m.items[pvcKey][snapKey] = struct{}{}
270+
m.items[pvcKey][snapshot] = struct{}{}
276271
}
277272

278273
func (m *pvcToSnapshotsMap) get(pvc, namespace string) []string {
@@ -289,8 +284,8 @@ func (m *pvcToSnapshotsMap) get(pvc, namespace string) []string {
289284
}
290285

291286
snaps := make([]string, 0, len(snapMap))
292-
for k := range snapMap {
293-
snaps = append(snaps, k.name)
287+
for snap := range snapMap {
288+
snaps = append(snaps, snap)
294289
}
295290
return snaps
296291
}
@@ -308,11 +303,7 @@ func (m *pvcToSnapshotsMap) delete(pvc, snapshot, namespace string) {
308303
return
309304
}
310305

311-
snapKey := namespacedName{
312-
namespace: namespace,
313-
name: snapshot,
314-
}
315-
delete(snapMap, snapKey)
306+
delete(snapMap, snapshot)
316307
if len(snapMap) != 0 {
317308
m.items[pvcKey] = snapMap
318309
return
@@ -2045,7 +2036,7 @@ func initPVCToSnapshotsMap(ctx context.Context, controllerClusterFlavor cnstypes
20452036

20462037
k8sOrchestratorInstance.pvcToSnapshotsMap = pvcToSnapshotsMap{
20472038
RWMutex: &sync.RWMutex{},
2048-
items: make(map[namespacedName]map[namespacedName]struct{}),
2039+
items: make(map[namespacedName]map[string]struct{}),
20492040
}
20502041
snapshotAdded := func(obj any) {
20512042
snap, ok := obj.(*snapshotv1.VolumeSnapshot)
@@ -2086,6 +2077,8 @@ func initPVCToSnapshotsMap(ctx context.Context, controllerClusterFlavor cnstypes
20862077
func(obj any) {
20872078
snapshotAdded(obj)
20882079
},
2080+
// Since the name of PVC associated with a snapshot is immutable,
2081+
// update events do not have any impact on the state of the cache.
20892082
nil,
20902083
func(obj any) {
20912084
snapshotDeleted(obj)

pkg/kubernetes/informers.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package kubernetes
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"sync"
2322
"time"
2423

@@ -322,8 +321,6 @@ func (im *InformerManager) Listen() (stopCh <-chan struct{}) {
322321

323322
go im.snapshotInformerFactory.Start(im.stopCh)
324323
cacheSync := im.snapshotInformerFactory.WaitForCacheSync(im.stopCh)
325-
// TODO: remove
326-
fmt.Print(cacheSync)
327324
for _, isSynced := range cacheSync {
328325
if !isSynced {
329326
return
@@ -332,3 +329,26 @@ func (im *InformerManager) Listen() (stopCh <-chan struct{}) {
332329

333330
return im.stopCh
334331
}
332+
333+
// NewConfigMapListener creates a new configmap listener in the given namespace.
334+
// NOTE: This creates a NewSharedIndexInformer everytime and does not use the informer factory.
335+
// Only use this function when you need a configmap listener in a different namespace than the
336+
// one already present in the informer factory.
337+
func NewConfigMapListener(ctx context.Context, client clientset.Interface, namespace string,
338+
add func(obj interface{}), update func(oldObj, newObj interface{}), remove func(obj interface{})) error {
339+
log := logger.GetLogger(ctx)
340+
configMapInformer := v1.NewFilteredConfigMapInformer(client, namespace, resyncPeriodConfigMapInformer,
341+
cache.Indexers{}, nil)
342+
343+
_, err := configMapInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
344+
AddFunc: add,
345+
UpdateFunc: update,
346+
DeleteFunc: remove,
347+
})
348+
if err != nil {
349+
return logger.LogNewErrorf(log, "failed to add event handler on configmap listener. Error: %v", err)
350+
}
351+
stopCh := make(chan struct{})
352+
go configMapInformer.Run(stopCh)
353+
return nil
354+
}

pkg/kubernetes/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type InformerManager struct {
5151
client clientset.Interface
5252
// main shared informer factory
5353
informerFactory informers.SharedInformerFactory
54-
54+
// snapshot informer factory
5555
snapshotInformerFactory externalversions.SharedInformerFactory
5656

5757
// main signal

0 commit comments

Comments
 (0)