diff --git a/pkg/controller/storage/capability/capability_controller.go b/pkg/controller/storage/capability/capability_controller.go index 96935c2983c7e9ed29dbfc81a8ed4137c38c25a1..d9ac286f0c94d740fdc158947c962d5133ae655e 100644 --- a/pkg/controller/storage/capability/capability_controller.go +++ b/pkg/controller/storage/capability/capability_controller.go @@ -101,6 +101,7 @@ func NewController( ) *StorageCapabilityController { utilruntime.Must(crdscheme.AddToScheme(scheme.Scheme)) + controller := &StorageCapabilityController{ storageClassCapabilityClient: capabilityClient, storageCapabilityLister: capabilityInformer.StorageClassCapabilities().Lister(), @@ -111,15 +112,18 @@ func NewController( storageClassLister: storageClassInformer.Lister(), storageClassSynced: storageClassInformer.Informer().HasSynced, snapshotSupported: snapshotSupported, - snapshotClassClient: snapshotClassClient, - snapshotClassLister: snapshotClassInformer.Lister(), - snapshotClassSynced: snapshotClassInformer.Informer().HasSynced, csiDriverLister: csiDriverInformer.Lister(), csiDriverSynced: csiDriverInformer.Informer().HasSynced, csiAddressGetter: csiAddress, workQueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "StorageClasses"), } + if snapshotSupported { + controller.snapshotClassClient = snapshotClassClient + controller.snapshotClassLister = snapshotClassInformer.Lister() + controller.snapshotClassSynced = snapshotClassInformer.Informer().HasSynced + } + storageClassInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ AddFunc: controller.enqueueStorageClass, UpdateFunc: func(old, new interface{}) { @@ -161,6 +165,10 @@ func (c *StorageCapabilityController) Run(threadCnt int, stopCh <-chan struct{}) c.csiDriverSynced, } + if c.snapshotAllowed() { + cacheSyncs = append(cacheSyncs, c.snapshotClassSynced) + } + if ok := cache.WaitForCacheSync(stopCh, cacheSyncs...); !ok { return fmt.Errorf("failed to wait for caches to sync") } @@ -250,7 +258,7 @@ func (c *StorageCapabilityController) syncHandler(key string) error { if err != nil { // StorageClass has been deleted, delete StorageClassCapability and VolumeSnapshotClass if errors.IsNotFound(err) { - if c.snapshotSupported { + if c.snapshotAllowed() { err = c.deleteSnapshotClass(name) if err != nil { return err @@ -280,7 +288,7 @@ func (c *StorageCapabilityController) syncHandler(key string) error { // Handle VolumeSnapshotClass with same name of StorageClass // annotate "support-snapshot" of StorageClass withSnapshotCapability := false - if c.snapshotSupported && capabilitySpec.Features.Snapshot.Create { + if c.snapshotAllowed() && capabilitySpec.Features.Snapshot.Create { _, err = c.snapshotClassLister.Get(name) if err != nil { // If VolumeSnapshotClass not exist, create it @@ -356,7 +364,7 @@ func (c *StorageCapabilityController) deleteStorageCapability(name string) error } func (c *StorageCapabilityController) deleteSnapshotClass(name string) error { - if !c.snapshotSupported { + if !c.snapshotAllowed() { return nil } _, err := c.snapshotClassLister.Get(name) @@ -429,6 +437,10 @@ func (c *StorageCapabilityController) setCSIAddressGetter(getter csiAddressGette c.csiAddressGetter = getter } +func (c *StorageCapabilityController) snapshotAllowed() bool { + return c.snapshotSupported && c.snapshotClassClient != nil && c.snapshotClassLister != nil && c.snapshotClassSynced != nil +} + func SnapshotSupported(discoveryInterface discovery.DiscoveryInterface) bool { minVer := version.MustParseGeneric(minSnapshotSupportedVersion) rawVer, err := discoveryInterface.ServerVersion()