Skip to content

Commit

Permalink
chore: refactor check for enabling home persistence
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Obuchowicz <aobuchow@redhat.com>
  • Loading branch information
AObuchow committed Mar 15, 2024
1 parent 7e53503 commit 60d584c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion controllers/workspace/devworkspace_controller.go
Expand Up @@ -291,7 +291,7 @@ func (r *DevWorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request
return r.failWorkspace(workspace, fmt.Sprintf("Error provisioning storage: %s", err), metrics.ReasonBadRequest, reqLogger, &reconcileStatus), nil
}

if home.StorageStrategySupportsPersistentHome(workspace) && home.NeedsPersistentHomeDirectory(workspace) {
if home.NeedsPersistentHomeDirectory(workspace) {
workspaceWithHomeVolume, err := home.AddPersistentHomeVolume(workspace)
if err != nil {
reconcileStatus.addWarning(fmt.Sprintf("Info: default persistentHome volume is not being used: %s", err.Error()))
Expand Down
20 changes: 12 additions & 8 deletions pkg/library/home/persistentHome.go
Expand Up @@ -60,23 +60,19 @@ func AddPersistentHomeVolume(workspace *common.DevWorkspaceWithConfig) (*v1alpha
return dwTemplateSpecCopy, nil
}

// Returns true if the workspace's storage strategy supports persisting the user home directory.
// The storage strategies which support home persistence are: per-user/common, per-workspace & async.
// The ephemeral storage strategy does not support home persistence.
func StorageStrategySupportsPersistentHome(workspace *common.DevWorkspaceWithConfig) bool {
storageClass := workspace.Spec.Template.Attributes.GetString(constants.DevWorkspaceStorageTypeAttribute, nil)
return storageClass != constants.EphemeralStorageClassType
}

// Returns true if the following criteria is met:
// - `persistUserHome` is enabled in the DevWorkspaceOperatorConfig
// - The storage strategy used by the DevWorkspace supports home persistence
// - None of the container components in the DevWorkspace mount a volume to `/home/user/`.
// - Persistent storage is required for the DevWorkspace
// Returns false otherwise.
func NeedsPersistentHomeDirectory(workspace *common.DevWorkspaceWithConfig) bool {
if !pointer.BoolDeref(workspace.Config.Workspace.PersistUserHome.Enabled, false) {
return false
}
if !storageStrategySupportsPersistentHome(workspace) {
return false
}
for _, component := range workspace.Spec.Template.Components {
if component.Container == nil {
continue
Expand All @@ -91,3 +87,11 @@ func NeedsPersistentHomeDirectory(workspace *common.DevWorkspaceWithConfig) bool
}
return storage.WorkspaceNeedsStorage(&workspace.Spec.Template)
}

// Returns true if the workspace's storage strategy supports persisting the user home directory.
// The storage strategies which support home persistence are: per-user/common, per-workspace & async.
// The ephemeral storage strategy does not support home persistence.
func storageStrategySupportsPersistentHome(workspace *common.DevWorkspaceWithConfig) bool {
storageClass := workspace.Spec.Template.Attributes.GetString(constants.DevWorkspaceStorageTypeAttribute, nil)
return storageClass != constants.EphemeralStorageClassType
}

0 comments on commit 60d584c

Please sign in to comment.