Skip to content

Commit

Permalink
fix(setting): do not delete the storage class
Browse files Browse the repository at this point in the history
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 <james.lu@suse.com>
(cherry picked from commit f45adc0)
  • Loading branch information
mantissahz authored and derekbit committed Sep 9, 2024
1 parent b7b51fc commit c633c0f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
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 @@ -958,6 +958,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 @@ -1318,31 +1330,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 @@ -402,6 +402,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

0 comments on commit c633c0f

Please sign in to comment.