diff --git a/controller/setting_controller.go b/controller/setting_controller.go index 613c5b2b3c..21231bce84 100644 --- a/controller/setting_controller.go +++ b/controller/setting_controller.go @@ -139,7 +139,7 @@ func NewSettingController( var err error if _, err = ds.SettingInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{ AddFunc: sc.enqueueSetting, - UpdateFunc: func(old, cur interface{}) { sc.enqueueUpdateSetting(old, cur) }, + UpdateFunc: func(old, cur interface{}) { sc.enqueueSetting(cur) }, DeleteFunc: sc.enqueueSetting, }, settingControllerResyncPeriod); err != nil { return nil, err @@ -1084,6 +1084,18 @@ func (sc *SettingController) syncDefaultLonghornStaticStorageClass() error { } defaultStaticStorageClassName := setting.Value + + definition, ok := types.GetSettingDefinition(types.SettingNameDefaultLonghornStaticStorageClass) + if !ok { + return fmt.Errorf("setting %v is not found", types.SettingNameDefaultLonghornStaticStorageClass) + } + + // Only create the default Longhorn static storage class named 'longhorn-static' if it does not exist + // And validator will check if the storage class exists when the setting value is not 'longhorn-static'. + if defaultStaticStorageClassName != definition.Default { + return nil + } + _, err = sc.ds.GetStorageClassRO(defaultStaticStorageClassName) if err != nil && apierrors.IsNotFound(err) { allowVolumeExpansion := true @@ -1444,31 +1456,6 @@ func (sc *SettingController) enqueueSetting(obj interface{}) { sc.queue.Add(key) } -func (sc *SettingController) enqueueUpdateSetting(oldObj, newObj interface{}) { - key, err := controller.KeyFunc(newObj) - if err != nil { - utilruntime.HandleError(fmt.Errorf("failed to get key for object %#v: %v", newObj, err)) - return - } - - oldSetting := oldObj.(*longhorn.Setting) - if oldSetting.Name == string(types.SettingNameDefaultLonghornStaticStorageClass) { - _, err := sc.ds.GetStorageClassRO(oldSetting.Value) - if err == nil { - if err := sc.ds.DeleteStorageClass(oldSetting.Value); err != nil { - utilruntime.HandleError(fmt.Errorf("failed to delete old %v for object %#v: %v", types.SettingNameDefaultLonghornStaticStorageClass, oldObj, err)) - return - } - } - if err != nil && !apierrors.IsNotFound(err) { - utilruntime.HandleError(fmt.Errorf("failed to get old %v for object %#v: %v", types.SettingNameDefaultLonghornStaticStorageClass, oldObj, err)) - return - } - } - - sc.queue.Add(key) -} - func (sc *SettingController) enqueueSettingForNode(obj interface{}) { if _, ok := obj.(*longhorn.Node); !ok { // Ignore deleted node diff --git a/datastore/longhorn.go b/datastore/longhorn.go index 1d9fe3dd52..5a9479ca99 100644 --- a/datastore/longhorn.go +++ b/datastore/longhorn.go @@ -424,6 +424,23 @@ func (s *DataStore) ValidateSetting(name, value string) (err error) { if v < 2 || v > 250 { return fmt.Errorf("%s should be between 2 and 250", name) } + case types.SettingNameDefaultLonghornStaticStorageClass: + definition, ok := types.GetSettingDefinition(types.SettingNameDefaultLonghornStaticStorageClass) + if !ok { + return fmt.Errorf("setting %v is not found", types.SettingNameDefaultLonghornStaticStorageClass) + } + + if value == definition.Default { + return nil + } + + _, err := s.GetStorageClassRO(value) + if err != nil { + if apierrors.IsNotFound(err) { + return errors.Wrapf(err, "cannot use a storage class %v that does not exist to set the setting %v", value, types.SettingNameDefaultLonghornStaticStorageClass) + } + return errors.Wrapf(err, "failed to get the storage class %v for setting %v", value, types.SettingNameDefaultLonghornStaticStorageClass) + } } return nil }