Skip to content

Commit

Permalink
fix(setting): create a real SC for SettingNameDefaultLonghornStaticSt…
Browse files Browse the repository at this point in the history
…orageClass

Create a Storage Class by the value of the setting
SettingNameDefaultLonghornStaticStorageClass, and delete the old
one if users modified the setting for expanding a volume.

ref: longhorn/longhorn 6446

Signed-off-by: James Lu <james.lu@suse.com>
  • Loading branch information
mantissahz authored and innobead committed Jun 26, 2024
1 parent 0a1a108 commit 77ac26d
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion controller/setting_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand Down Expand Up @@ -138,7 +139,7 @@ func NewSettingController(
var err error
if _, err = ds.SettingInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
AddFunc: sc.enqueueSetting,
UpdateFunc: func(old, cur interface{}) { sc.enqueueSetting(cur) },
UpdateFunc: func(old, cur interface{}) { sc.enqueueUpdateSetting(old, cur) },
DeleteFunc: sc.enqueueSetting,
}, settingControllerResyncPeriod); err != nil {
return nil, err
Expand Down Expand Up @@ -276,6 +277,10 @@ func (sc *SettingController) syncNonDangerZoneSettingsForManagedComponents(setti
if err := sc.updateLogLevel(); err != nil {
return err
}
case types.SettingNameDefaultLonghornStaticStorageClass:
if err := sc.syncDefaultLonghornStaticStorageClass(); err != nil {
return err
}
}

return nil
Expand Down Expand Up @@ -965,6 +970,34 @@ func (sc *SettingController) updateLogLevel() error {
return nil
}

func (sc *SettingController) syncDefaultLonghornStaticStorageClass() error {
setting, err := sc.ds.GetSettingWithAutoFillingRO(types.SettingNameDefaultLonghornStaticStorageClass)
if err != nil {
return err
}

defaultStaticStorageClassName := setting.Value
_, err = sc.ds.GetStorageClassRO(defaultStaticStorageClassName)
if err != nil && apierrors.IsNotFound(err) {
allowVolumeExpansion := true
storageClass := &storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: defaultStaticStorageClassName,
},
Provisioner: types.LonghornDriverName,
AllowVolumeExpansion: &allowVolumeExpansion,
Parameters: map[string]string{"staleReplicaTimeout": "30"},
}

if _, err := sc.ds.CreateStorageClass(storageClass); err != nil {
return err
}
return nil
}

return err
}

// updateDataEngine deletes the corresponding instance manager pods immediately if the data engine setting is disabled.
func (sc *SettingController) updateDataEngine(setting types.SettingName) error {
enabled, err := sc.ds.GetSettingAsBool(setting)
Expand Down Expand Up @@ -1304,6 +1337,31 @@ 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

0 comments on commit 77ac26d

Please sign in to comment.