|
| 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 restore defines library functions for restoring workspace data from backup images |
| 17 | +package restore |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" |
| 24 | + "github.com/devfile/devworkspace-operator/pkg/common" |
| 25 | + devfileConstants "github.com/devfile/devworkspace-operator/pkg/library/constants" |
| 26 | + "github.com/devfile/devworkspace-operator/pkg/library/storage" |
| 27 | + "github.com/go-logr/logr" |
| 28 | + corev1 "k8s.io/api/core/v1" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + |
| 31 | + "github.com/devfile/devworkspace-operator/internal/images" |
| 32 | + "github.com/devfile/devworkspace-operator/pkg/constants" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + workspaceRestoreContainerName = "workspace-restore" |
| 37 | +) |
| 38 | + |
| 39 | +type Options struct { |
| 40 | + Image string |
| 41 | + PullPolicy corev1.PullPolicy |
| 42 | + Resources *corev1.ResourceRequirements |
| 43 | + Env []corev1.EnvVar |
| 44 | +} |
| 45 | + |
| 46 | +// GetWorkspaceRestoreInitContainer creates an init container that restores workspace data from a backup image. |
| 47 | +// The restore container uses the existing workspace-recovery.sh script to extract backup content. |
| 48 | +func GetWorkspaceRestoreInitContainer( |
| 49 | + ctx context.Context, |
| 50 | + workspace *common.DevWorkspaceWithConfig, |
| 51 | + k8sClient client.Client, |
| 52 | + options Options, |
| 53 | + log logr.Logger, |
| 54 | +) (*corev1.Container, error) { |
| 55 | + wokrspaceTempplate := &workspace.Spec.Template |
| 56 | + // Check if restore is requested via workspace attribute |
| 57 | + if !wokrspaceTempplate.Attributes.Exists(constants.WorkspaceRestoreAttribute) { |
| 58 | + return nil, nil |
| 59 | + } |
| 60 | + |
| 61 | + // Get workspace PVC information for mounting into the restore container |
| 62 | + pvcName, _, err := storage.GetWorkspacePVCInfo(ctx, workspace.DevWorkspace, workspace.Config, k8sClient, log) |
| 63 | + if err != nil { |
| 64 | + return nil, fmt.Errorf("failed to resolve workspace PVC info for restore: %w", err) |
| 65 | + } |
| 66 | + if pvcName == "" { |
| 67 | + return nil, fmt.Errorf("no PVC found for workspace %s during restore", workspace.Name) |
| 68 | + } |
| 69 | + |
| 70 | + // Determine the source image for restore |
| 71 | + var restoreSourceImage string |
| 72 | + if wokrspaceTempplate.Attributes.Exists(constants.WorkspaceRestoreSourceImageAttribute) { |
| 73 | + // User choose custom image specified in the attribute |
| 74 | + restoreSourceImage = wokrspaceTempplate.Attributes.GetString(constants.WorkspaceRestoreSourceImageAttribute, &err) |
| 75 | + if err != nil { |
| 76 | + return nil, fmt.Errorf("failed to read %s attribute on workspace: %w", constants.WorkspaceRestoreSourceImageAttribute, err) |
| 77 | + } |
| 78 | + } else { |
| 79 | + if workspace.Config.Workspace.BackupCronJob == nil { |
| 80 | + return nil, fmt.Errorf("workspace restore requested but backup cron job configuration is missing") |
| 81 | + } |
| 82 | + if workspace.Config.Workspace.BackupCronJob.Registry == nil || workspace.Config.Workspace.BackupCronJob.Registry.Path == "" { |
| 83 | + return nil, fmt.Errorf("workspace restore requested but backup cron job registry is not configured") |
| 84 | + } |
| 85 | + // Use default backup image location based on workspace info |
| 86 | + restoreSourceImage = workspace.Config.Workspace.BackupCronJob.Registry.Path + "/" + workspace.Namespace + "/" + workspace.Name + ":latest" |
| 87 | + } |
| 88 | + if restoreSourceImage == "" { |
| 89 | + return nil, fmt.Errorf("empty value for attribute %s is invalid", constants.WorkspaceRestoreSourceImageAttribute) |
| 90 | + } |
| 91 | + |
| 92 | + if !hasContainerComponents(wokrspaceTempplate) { |
| 93 | + // Avoid adding restore init container when DevWorkspace does not define any containers |
| 94 | + return nil, nil |
| 95 | + } |
| 96 | + |
| 97 | + // Use the project backup image which contains the workspace-recovery.sh script |
| 98 | + restoreImage := images.GetProjectBackupImage() |
| 99 | + |
| 100 | + // Prepare environment variables for the restore script |
| 101 | + env := append(options.Env, []corev1.EnvVar{ |
| 102 | + {Name: "BACKUP_IMAGE", Value: restoreSourceImage}, |
| 103 | + }...) |
| 104 | + |
| 105 | + return &corev1.Container{ |
| 106 | + Name: workspaceRestoreContainerName, |
| 107 | + Image: restoreImage, |
| 108 | + Command: []string{"/workspace-recovery.sh"}, |
| 109 | + Args: []string{"--restore"}, |
| 110 | + Env: env, |
| 111 | + VolumeMounts: []corev1.VolumeMount{ |
| 112 | + { |
| 113 | + Name: devfileConstants.ProjectsVolumeName, |
| 114 | + MountPath: constants.DefaultProjectsSourcesRoot, |
| 115 | + }, |
| 116 | + }, |
| 117 | + ImagePullPolicy: options.PullPolicy, |
| 118 | + // }, |
| 119 | + }, nil |
| 120 | +} |
| 121 | + |
| 122 | +func hasContainerComponents(workspace *dw.DevWorkspaceTemplateSpec) bool { |
| 123 | + for _, component := range workspace.Components { |
| 124 | + if component.Container != nil { |
| 125 | + return true |
| 126 | + } |
| 127 | + } |
| 128 | + return false |
| 129 | +} |
0 commit comments