Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(setting): do not delete the storage class set in the setting default-longhorn-static-storage-class (backport #3141) #3149

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 13 additions & 26 deletions controller/setting_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions datastore/longhorn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down