Skip to content

Commit cdec8d5

Browse files
committed
Update reconciler to use library to decide if storage is needed
- Only set storage finalizer on workspace if workspace needs storage as defined by storage library - Only generate and sync PVC if workspace needs storage Signed-off-by: Angel Misevski <[email protected]>
1 parent 680e04b commit cdec8d5

File tree

4 files changed

+17
-59
lines changed

4 files changed

+17
-59
lines changed

controllers/workspace/devworkspace_controller.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,21 @@ func (r *DevWorkspaceReconciler) Reconcile(req ctrl.Request) (reconcileResult ct
179179
reqLogger.Info("DevWorkspace start failed")
180180
reconcileStatus.Phase = devworkspace.WorkspaceStatusFailed
181181
reconcileStatus.Conditions[devworkspace.WorkspaceFailedStart] = fmt.Sprintf("Error processing devfile: %s", err)
182-
return reconcile.Result{}, err
182+
return reconcile.Result{}, nil
183183
}
184184
err = storagelib.RewriteContainerVolumeMounts(workspace.Status.WorkspaceId, devfilePodAdditions, workspace.Spec.Template)
185185
if err != nil {
186186
reqLogger.Info("DevWorkspace start failed")
187187
reconcileStatus.Phase = devworkspace.WorkspaceStatusFailed
188-
reconcileStatus.Conditions[devworkspace.WorkspaceFailedStart] = fmt.Sprintf("Error processing devfile persistent storage: %s", err)
189-
return reconcile.Result{}, err
188+
reconcileStatus.Conditions[devworkspace.WorkspaceFailedStart] = fmt.Sprintf("Error processing devfile volumes: %s", err)
189+
return reconcile.Result{}, nil
190190
}
191191
componentDescriptions, err := shimlib.GetComponentDescriptionsFromPodAdditions(devfilePodAdditions, workspace.Spec.Template)
192192
if err != nil {
193193
reqLogger.Info("DevWorkspace start failed")
194194
reconcileStatus.Phase = devworkspace.WorkspaceStatusFailed
195195
reconcileStatus.Conditions[devworkspace.WorkspaceFailedStart] = fmt.Sprintf("Error processing devfile for Theia: %s", err)
196-
return reconcile.Result{}, err
196+
return reconcile.Result{}, nil
197197
}
198198
reconcileStatus.Conditions[devworkspace.WorkspaceReady] = ""
199199
timing.SetTime(workspace, timing.ComponentsReady)
@@ -211,9 +211,11 @@ func (r *DevWorkspaceReconciler) Reconcile(req ctrl.Request) (reconcileResult ct
211211
componentDescriptions = append([]controllerv1alpha1.ComponentDescription{cheRestApisComponent}, componentDescriptions...)
212212
}
213213

214-
pvcStatus := provision.SyncPVC(workspace, componentDescriptions, r.Client, reqLogger)
215-
if pvcStatus.Err != nil || !pvcStatus.Continue {
216-
return reconcile.Result{Requeue: true}, pvcStatus.Err
214+
if storagelib.NeedsStorage(workspace.Spec.Template) {
215+
pvcStatus := provision.SyncPVC(workspace, r.Client, reqLogger)
216+
if pvcStatus.Err != nil || !pvcStatus.Continue {
217+
return reconcile.Result{Requeue: true}, pvcStatus.Err
218+
}
217219
}
218220

219221
rbacStatus := provision.SyncRBAC(workspace, r.Client, reqLogger)
@@ -283,7 +285,7 @@ func (r *DevWorkspaceReconciler) Reconcile(req ctrl.Request) (reconcileResult ct
283285

284286
// Step six: Create deployment and wait for it to be ready
285287
timing.SetTime(workspace, timing.DeploymentCreated)
286-
deploymentStatus := provision.SyncDeploymentToCluster(workspace, podAdditions, componentDescriptions, serviceAcctName, clusterAPI)
288+
deploymentStatus := provision.SyncDeploymentToCluster(workspace, podAdditions, serviceAcctName, clusterAPI)
287289
if !deploymentStatus.Continue {
288290
if deploymentStatus.FailStartup {
289291
reqLogger.Info("Workspace start failed")

controllers/workspace/finalize.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"path"
1919
"time"
2020

21+
storagelib "github.com/devfile/devworkspace-operator/pkg/library/storage"
22+
2123
"github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
2224
"github.com/devfile/devworkspace-operator/controllers/workspace/provision"
2325
"github.com/devfile/devworkspace-operator/internal/images"
@@ -223,10 +225,8 @@ func (r *DevWorkspaceReconciler) getClusterCleanupJob(ctx context.Context, works
223225
return clusterJob, nil
224226
}
225227

226-
func isFinalizerNecessary(_ *v1alpha2.DevWorkspace) bool {
227-
// TODO: Implement checking whether persistent storage is used (once other choices are possible)
228-
// Note this could interfere with cloud-shell until this TODO is resolved.
229-
return true
228+
func isFinalizerNecessary(workspace *v1alpha2.DevWorkspace) bool {
229+
return storagelib.NeedsStorage(workspace.Spec.Template)
230230
}
231231

232232
func hasFinalizer(workspace *v1alpha2.DevWorkspace) bool {

controllers/workspace/provision/deployment.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,12 @@ var deploymentDiffOpts = cmp.Options{
5959
func SyncDeploymentToCluster(
6060
workspace *devworkspace.DevWorkspace,
6161
podAdditions []v1alpha1.PodAdditions,
62-
components []v1alpha1.ComponentDescription,
6362
saName string,
6463
clusterAPI ClusterAPI) DeploymentProvisioningStatus {
6564

6665
// [design] we have to pass components and routing pod additions separately because we need mountsources from each
6766
// component.
68-
specDeployment, err := getSpecDeployment(workspace, podAdditions, components, saName, clusterAPI.Scheme)
67+
specDeployment, err := getSpecDeployment(workspace, podAdditions, saName, clusterAPI.Scheme)
6968
if err != nil {
7069
return DeploymentProvisioningStatus{
7170
ProvisioningStatus: ProvisioningStatus{
@@ -160,7 +159,6 @@ func checkDeploymentStatus(deployment *appsv1.Deployment) (ready bool) {
160159
func getSpecDeployment(
161160
workspace *devworkspace.DevWorkspace,
162161
podAdditionsList []v1alpha1.PodAdditions,
163-
components []v1alpha1.ComponentDescription,
164162
saName string,
165163
scheme *runtime.Scheme) (*appsv1.Deployment, error) {
166164
replicas := int32(1)
@@ -225,8 +223,7 @@ func getSpecDeployment(
225223
},
226224
}
227225

228-
if IsPVCRequired(components) {
229-
deployment.Spec.Template.Spec.Volumes = append(deployment.Spec.Template.Spec.Volumes, getPersistentVolumeClaim())
226+
if len(podAdditions.Volumes) > 0 {
230227
// Kubernetes creates directories in a PVC to support subpaths such that only the leaf directory has g+rwx permissions.
231228
// This means that mounting the subpath e.g. <workspace-id>/plugins will result in the <workspace-id> directory being
232229
// created with 755 permissions, requiring the root UID to remove it.
@@ -345,19 +342,6 @@ func mergePodAdditions(toMerge []v1alpha1.PodAdditions) (*v1alpha1.PodAdditions,
345342
return podAdditions, nil
346343
}
347344

348-
func getPersistentVolumeClaim() corev1.Volume {
349-
var workspaceClaim = corev1.PersistentVolumeClaimVolumeSource{
350-
ClaimName: config.ControllerCfg.GetWorkspacePVCName(),
351-
}
352-
pvcVolume := corev1.Volume{
353-
Name: config.ControllerCfg.GetWorkspacePVCName(),
354-
VolumeSource: corev1.VolumeSource{
355-
PersistentVolumeClaim: &workspaceClaim,
356-
},
357-
}
358-
return pvcVolume
359-
}
360-
361345
func getWorkspaceSubpathVolumeMount(workspaceId string) corev1.VolumeMount {
362346
volumeName := config.ControllerCfg.GetWorkspacePVCName()
363347

controllers/workspace/provision/pvc.go

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ package provision
1414

1515
import (
1616
devworkspace "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
17-
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
1817
"github.com/devfile/devworkspace-operator/pkg/config"
1918
"github.com/go-logr/logr"
2019
corev1 "k8s.io/api/core/v1"
@@ -23,11 +22,7 @@ import (
2322
"sigs.k8s.io/controller-runtime/pkg/client"
2423
)
2524

26-
func SyncPVC(workspace *devworkspace.DevWorkspace, components []v1alpha1.ComponentDescription, client client.Client, reqLogger logr.Logger) ProvisioningStatus {
27-
if !IsPVCRequired(components) {
28-
return ProvisioningStatus{Continue: true}
29-
}
30-
25+
func SyncPVC(workspace *devworkspace.DevWorkspace, client client.Client, reqLogger logr.Logger) ProvisioningStatus {
3126
pvc, err := generatePVC(workspace)
3227
if err != nil {
3328
return ProvisioningStatus{Err: err}
@@ -61,26 +56,3 @@ func generatePVC(workspace *devworkspace.DevWorkspace) (*corev1.PersistentVolume
6156
},
6257
}, nil
6358
}
64-
65-
// IsPVCRequired checks to see if we need a PVC for the given devfile.
66-
// If there is any Containers with VolumeMounts that have the same name as the workspace PVC name then we need a PVC
67-
func IsPVCRequired(components []v1alpha1.ComponentDescription) bool {
68-
volumeName := config.ControllerCfg.GetWorkspacePVCName()
69-
for _, comp := range components {
70-
for _, cont := range comp.PodAdditions.Containers {
71-
for _, vm := range cont.VolumeMounts {
72-
if vm.Name == volumeName {
73-
return true
74-
}
75-
}
76-
}
77-
for _, cont := range comp.PodAdditions.InitContainers {
78-
for _, vm := range cont.VolumeMounts {
79-
if vm.Name == volumeName {
80-
return true
81-
}
82-
}
83-
}
84-
}
85-
return false
86-
}

0 commit comments

Comments
 (0)