diff --git a/controller/setting_controller.go b/controller/setting_controller.go index 5e93ccbbcd..8ffb7bbf7e 100644 --- a/controller/setting_controller.go +++ b/controller/setting_controller.go @@ -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" @@ -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 @@ -257,6 +258,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 @@ -946,6 +951,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) @@ -1285,6 +1318,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