Skip to content

Commit

Permalink
pb-3338: Added support for handling default storage class request in the
Browse files Browse the repository at this point in the history
storageclass mapping
  • Loading branch information
sivakumar subraani authored and siva-portworx committed Feb 8, 2023
1 parent 5d15bda commit 332790c
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions pkg/applicationmanager/controllers/applicationrestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/libopenstorage/stork/pkg/version"
"github.com/portworx/sched-ops/k8s/apiextensions"
"github.com/portworx/sched-ops/k8s/core"
"github.com/portworx/sched-ops/k8s/storage"
storkops "github.com/portworx/sched-ops/k8s/stork"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
Expand All @@ -42,6 +43,21 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const (
defaultStorageClass = "use-default-storage-class"
)

// isStorageClassMappingContainsDefault - will check whether any storageclass has use-default-storage-class
// as destination storage class.
func isStorageClassMappingContainsDefault(restore *storkapi.ApplicationRestore) bool {
for _, value := range restore.Spec.StorageClassMapping {
if value == defaultStorageClass {
return true
}
}
return false
}

// NewApplicationRestore creates a new instance of ApplicationRestoreController.
func NewApplicationRestore(mgr manager.Manager, r record.EventRecorder, rc resourcecollector.ResourceCollector) *ApplicationRestoreController {
return &ApplicationRestoreController{
Expand Down Expand Up @@ -286,6 +302,49 @@ func (a *ApplicationRestoreController) handle(ctx context.Context, restore *stor
return nil
}

if len(restore.Spec.StorageClassMapping) >= 1 && isStorageClassMappingContainsDefault(restore) {
// Update the default storageclass name in storageclassmapping.
// storageclassMapping will have "use-default-storage-class" as destination storage class,
// If default storageclass need to be selected.
scList, err := storage.Instance().GetDefaultStorageClasses()
if err != nil {
log.ApplicationRestoreLog(restore).Errorf(err.Error())
a.recorder.Event(restore,
v1.EventTypeWarning,
string(storkapi.ApplicationRestoreStatusFailed),
err.Error())
return nil
}
// If more than one storageclass is set as default storageclass, update error event
if len(scList.Items) > 1 {
errMsg := "more than one storageclass is set as default on destination cluster"
log.ApplicationRestoreLog(restore).Errorf(errMsg)
a.recorder.Event(restore,
v1.EventTypeWarning,
string(storkapi.ApplicationRestoreStatusFailed),
errMsg)
return nil
}
// If no storageclass is set as default storageclass, update error event
if len(scList.Items) == 0 {
err := fmt.Errorf("no storageclass is set as default on destination cluster")
log.ApplicationRestoreLog(restore).Errorf(err.Error())
a.recorder.Event(restore,
v1.EventTypeWarning,
string(storkapi.ApplicationRestoreStatusFailed),
err.Error())
return nil
}
for key, value := range restore.Spec.StorageClassMapping {
if value == defaultStorageClass {
restore.Spec.StorageClassMapping[key] = scList.Items[0].Name
}
}
err = a.client.Update(context.TODO(), restore)
if err != nil {
return err
}
}
switch restore.Status.Stage {
case storkapi.ApplicationRestoreStageInitial:
// Make sure the namespaces exist
Expand Down

0 comments on commit 332790c

Please sign in to comment.