Skip to content

Commit d787aeb

Browse files
Alldadkwon17
authored andcommitted
Extract GetWorkspacePVCInfo to separate module
The function will be used across both backup and restore features and should be accessed multiple places. Signed-off-by: Ales Raszka <araszka@redhat.com>
1 parent 0bcd7a6 commit d787aeb

2 files changed

Lines changed: 67 additions & 30 deletions

File tree

controllers/backupcronjob/backupcronjob_controller.go

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ import (
2626
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2727
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
2828
"github.com/devfile/devworkspace-operator/internal/images"
29-
"github.com/devfile/devworkspace-operator/pkg/common"
3029
"github.com/devfile/devworkspace-operator/pkg/conditions"
3130
"github.com/devfile/devworkspace-operator/pkg/config"
32-
wkspConfig "github.com/devfile/devworkspace-operator/pkg/config"
3331
"github.com/devfile/devworkspace-operator/pkg/constants"
3432
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
35-
"github.com/devfile/devworkspace-operator/pkg/provision/storage"
33+
"github.com/devfile/devworkspace-operator/pkg/library/storage"
3634
"github.com/go-logr/logr"
3735
"github.com/robfig/cron/v3"
3836
batchv1 "k8s.io/api/batch/v1"
@@ -353,7 +351,7 @@ func (r *BackupCronJobReconciler) createBackupJob(
353351
}
354352

355353
// Find a PVC with used by the workspace
356-
pvcName, workspacePath, err := r.getWorkspacePVCName(ctx, workspace, dwOperatorConfig, log)
354+
pvcName, workspacePath, err := storage.GetWorkspacePVCInfo(ctx, workspace, dwOperatorConfig.Config, r.Client, log)
357355
if err != nil {
358356
log.Error(err, "Failed to get workspace PVC name", "devworkspace", workspace.Name)
359357
return err
@@ -482,32 +480,6 @@ func (r *BackupCronJobReconciler) createBackupJob(
482480
return nil
483481
}
484482

485-
// getWorkspacePVCName determines the PVC name and workspace path based on the storage provisioner used.
486-
func (r *BackupCronJobReconciler) getWorkspacePVCName(ctx context.Context, workspace *dw.DevWorkspace, dwOperatorConfig *controllerv1alpha1.DevWorkspaceOperatorConfig, log logr.Logger) (string, string, error) {
487-
config, err := wkspConfig.ResolveConfigForWorkspace(workspace, r.Client)
488-
489-
workspaceWithConfig := &common.DevWorkspaceWithConfig{}
490-
workspaceWithConfig.DevWorkspace = workspace
491-
workspaceWithConfig.Config = config
492-
493-
storageProvisioner, err := storage.GetProvisioner(workspaceWithConfig)
494-
if err != nil {
495-
return "", "", err
496-
}
497-
if _, ok := storageProvisioner.(*storage.PerWorkspaceStorageProvisioner); ok {
498-
pvcName := common.PerWorkspacePVCName(workspace.Status.DevWorkspaceId)
499-
return pvcName, constants.DefaultProjectsSourcesRoot, nil
500-
501-
} else if _, ok := storageProvisioner.(*storage.CommonStorageProvisioner); ok {
502-
pvcName := constants.DefaultWorkspacePVCName
503-
if dwOperatorConfig.Config.Workspace.PVCName != "" {
504-
pvcName = dwOperatorConfig.Config.Workspace.PVCName
505-
}
506-
return pvcName, workspace.Status.DevWorkspaceId + constants.DefaultProjectsSourcesRoot, nil
507-
}
508-
return "", "", nil
509-
}
510-
511483
func (r *BackupCronJobReconciler) handleRegistryAuthSecret(ctx context.Context, workspace *dw.DevWorkspace,
512484
dwOperatorConfig *controllerv1alpha1.DevWorkspaceOperatorConfig, log logr.Logger,
513485
) (*corev1.Secret, error) {

pkg/library/storage/storage.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// Copyright (c) 2019-2025 Red Hat, Inc.
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
package storage
17+
18+
import (
19+
"context"
20+
21+
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
22+
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
23+
"github.com/devfile/devworkspace-operator/pkg/common"
24+
"github.com/devfile/devworkspace-operator/pkg/constants"
25+
"github.com/devfile/devworkspace-operator/pkg/provision/storage"
26+
"github.com/go-logr/logr"
27+
"sigs.k8s.io/controller-runtime/pkg/client"
28+
)
29+
30+
// GetWorkspacePVCInfo determines the PVC name and workspace path based on the storage provisioner used.
31+
// This function can be used by both backup and restore operations to ensure consistent PVC resolution logic.
32+
//
33+
// Returns:
34+
// - pvcName: The name of the PVC that stores workspace data
35+
// - workspacePath: The path within the PVC where workspace data is stored
36+
// - error: Any error that occurred during PVC resolution
37+
func GetWorkspacePVCInfo(
38+
ctx context.Context,
39+
workspace *dw.DevWorkspace,
40+
config *controllerv1alpha1.OperatorConfiguration,
41+
k8sClient client.Client,
42+
log logr.Logger,
43+
) (pvcName string, workspacePath string, err error) {
44+
workspaceWithConfig := &common.DevWorkspaceWithConfig{}
45+
workspaceWithConfig.DevWorkspace = workspace
46+
workspaceWithConfig.Config = config
47+
48+
storageProvisioner, err := storage.GetProvisioner(workspaceWithConfig)
49+
if err != nil {
50+
return "", "", err
51+
}
52+
53+
if _, ok := storageProvisioner.(*storage.PerWorkspaceStorageProvisioner); ok {
54+
pvcName := common.PerWorkspacePVCName(workspace.Status.DevWorkspaceId)
55+
return pvcName, constants.DefaultProjectsSourcesRoot, nil
56+
57+
} else if _, ok := storageProvisioner.(*storage.CommonStorageProvisioner); ok {
58+
pvcName := constants.DefaultWorkspacePVCName
59+
if config.Workspace != nil && config.Workspace.PVCName != "" {
60+
pvcName = config.Workspace.PVCName
61+
}
62+
return pvcName, workspace.Status.DevWorkspaceId + constants.DefaultProjectsSourcesRoot, nil
63+
}
64+
return "", "", nil
65+
}

0 commit comments

Comments
 (0)