From 49023a382b4d0a50d91b10985cd05f81907a8556 Mon Sep 17 00:00:00 2001 From: James Lu Date: Mon, 9 Sep 2024 09:03:00 +0800 Subject: [PATCH] fix(setting): do not delete the storage class Delete the old storage class set in reconciling the `default-longhorn-static-storage-class` setting. Create the storage with a label if the storage class named as the value of `default-longhorn-static-storage-class` setting does not exist. ref: longhorn/longhorn 9391 Signed-off-by: James Lu (cherry picked from commit f45adc036b5db73f890670fe86cb2e0eb849a5ff) --- controller/setting_controller.go | 39 +++++++++++--------------------- datastore/longhorn.go | 17 ++++++++++++++ 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/controller/setting_controller.go b/controller/setting_controller.go index 9dc236e7b4..a51a3a61b4 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 b76faa4334..d7ce3a709e 100644 --- a/datastore/longhorn.go +++ b/datastore/longhorn.go @@ -414,6 +414,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 }