Skip to content

Commit 0199538

Browse files
committed
fix (storage) : use operator podSecurityContext for PVC cleanup job on OpenShift
Apply workspace.Config.Workspace.PodSecurityContext to the cleanup Job pod spec, matching the workspace deployment behavior. Signed-off-by: Rohan Kumar <rohaan@redhat.com>
1 parent 113b402 commit 0199538

2 files changed

Lines changed: 82 additions & 9 deletions

File tree

pkg/provision/storage/cleanup.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"time"
2222

2323
"github.com/devfile/devworkspace-operator/pkg/dwerrors"
24-
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
2524
"github.com/devfile/devworkspace-operator/pkg/library/status"
2625
nsconfig "github.com/devfile/devworkspace-operator/pkg/provision/config"
2726
"github.com/devfile/devworkspace-operator/pkg/provision/sync"
@@ -138,13 +137,6 @@ func getSpecCommonPVCCleanupJob(workspace *common.DevWorkspaceWithConfig, cluste
138137
jobLabels[constants.DevWorkspaceRestrictedAccessAnnotation] = restrictedAccess
139138
}
140139

141-
var securityContext *corev1.PodSecurityContext
142-
if infrastructure.IsOpenShift() {
143-
securityContext = &corev1.PodSecurityContext{}
144-
} else {
145-
securityContext = workspace.Config.Workspace.PodSecurityContext
146-
}
147-
148140
job := &batchv1.Job{
149141
ObjectMeta: metav1.ObjectMeta{
150142
Name: common.PVCCleanupJobName(workspaceId),
@@ -160,7 +152,7 @@ func getSpecCommonPVCCleanupJob(workspace *common.DevWorkspaceWithConfig, cluste
160152
},
161153
Spec: corev1.PodSpec{
162154
RestartPolicy: "Never",
163-
SecurityContext: securityContext,
155+
SecurityContext: workspace.Config.Workspace.PodSecurityContext,
164156
Volumes: []corev1.Volume{
165157
{
166158
Name: pvcName,
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
"testing"
21+
22+
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
23+
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
24+
"github.com/devfile/devworkspace-operator/pkg/common"
25+
"github.com/devfile/devworkspace-operator/pkg/constants"
26+
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
27+
"github.com/devfile/devworkspace-operator/pkg/provision/sync"
28+
"github.com/stretchr/testify/assert"
29+
corev1 "k8s.io/api/core/v1"
30+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
32+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
33+
)
34+
35+
func TestGetSpecCommonPVCCleanupJobUsesConfigPodSecurityContext(t *testing.T) {
36+
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
37+
38+
fsGroupChangeOnRootMismatch := corev1.FSGroupChangeOnRootMismatch
39+
customPodSecurityContext := &corev1.PodSecurityContext{
40+
FSGroupChangePolicy: &fsGroupChangeOnRootMismatch,
41+
SELinuxOptions: &corev1.SELinuxOptions{Type: "spc_t"},
42+
}
43+
44+
namespace := "test-ns"
45+
pvcName := "claim-devworkspace"
46+
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(
47+
&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}},
48+
).Build()
49+
50+
workspace := &common.DevWorkspaceWithConfig{
51+
DevWorkspace: &dw.DevWorkspace{
52+
ObjectMeta: metav1.ObjectMeta{
53+
Name: "test-workspace",
54+
Namespace: namespace,
55+
Labels: map[string]string{
56+
constants.DevWorkspaceCreatorLabel: "test-creator",
57+
},
58+
},
59+
Status: dw.DevWorkspaceStatus{
60+
DevWorkspaceId: "test-workspace-id",
61+
},
62+
},
63+
Config: &v1alpha1.OperatorConfiguration{
64+
Workspace: &v1alpha1.WorkspaceConfig{
65+
PVCName: pvcName,
66+
PodSecurityContext: customPodSecurityContext,
67+
},
68+
},
69+
}
70+
71+
clusterAPI := sync.ClusterAPI{
72+
Client: fakeClient,
73+
Scheme: scheme,
74+
Logger: zap.New(zap.UseDevMode(true)),
75+
Ctx: context.Background(),
76+
}
77+
78+
job, err := getSpecCommonPVCCleanupJob(workspace, clusterAPI)
79+
assert.NoError(t, err)
80+
assert.Equal(t, customPodSecurityContext, job.Spec.Template.Spec.SecurityContext)
81+
}

0 commit comments

Comments
 (0)