Skip to content

Commit 4b7f77c

Browse files
authored
chore: Mount DWO_NAMESPACE into dashboard pod (#2102)
* chore: Mount DWO_NAMESPACE into dashboard pod Signed-off-by: Anatolii Bazko <abazko@redhat.com>
1 parent 9dcdd29 commit 4b7f77c

10 files changed

Lines changed: 94 additions & 50 deletions

File tree

controllers/che/checluster_controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/eclipse-che/che-operator/pkg/common/constants"
2020
k8sclient "github.com/eclipse-che/che-operator/pkg/common/k8s-client"
2121
"github.com/eclipse-che/che-operator/pkg/common/reconciler"
22+
"github.com/eclipse-che/che-operator/pkg/deploy/devworkspace"
2223
"k8s.io/utils/pointer"
2324
"sigs.k8s.io/controller-runtime/pkg/controller"
2425

@@ -28,16 +29,14 @@ import (
2829

2930
editorsdefinitions "github.com/eclipse-che/che-operator/pkg/deploy/editors-definitions"
3031

31-
"github.com/eclipse-che/che-operator/pkg/common/test"
32-
containerbuild "github.com/eclipse-che/che-operator/pkg/deploy/container-capabilities"
33-
3432
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
3533
"github.com/eclipse-che/che-operator/pkg/common/chetypes"
34+
"github.com/eclipse-che/che-operator/pkg/common/test"
3635
"github.com/eclipse-che/che-operator/pkg/common/utils"
3736
"github.com/eclipse-che/che-operator/pkg/deploy"
3837
"github.com/eclipse-che/che-operator/pkg/deploy/consolelink"
38+
containerbuild "github.com/eclipse-che/che-operator/pkg/deploy/container-capabilities"
3939
"github.com/eclipse-che/che-operator/pkg/deploy/dashboard"
40-
devworkspaceconfig "github.com/eclipse-che/che-operator/pkg/deploy/dev-workspace-config"
4140
"github.com/eclipse-che/che-operator/pkg/deploy/devfileregistry"
4241
"github.com/eclipse-che/che-operator/pkg/deploy/gateway"
4342
identityprovider "github.com/eclipse-che/che-operator/pkg/deploy/identity-provider"
@@ -103,7 +102,7 @@ func NewReconciler(
103102

104103
reconcilerManager.AddReconciler(tls.NewCertificatesReconciler())
105104
reconcilerManager.AddReconciler(tls.NewTlsSecretReconciler())
106-
reconcilerManager.AddReconciler(devworkspaceconfig.NewDevWorkspaceConfigReconciler())
105+
reconcilerManager.AddReconciler(devworkspace.NewDevWorkspaceConfigReconciler())
107106
reconcilerManager.AddReconciler(rbac.NewGatewayPermissionsReconciler())
108107

109108
// we have to expose che endpoint independently of syncing other server
@@ -116,6 +115,7 @@ func NewReconciler(
116115
reconcilerManager.AddReconciler(devfileregistry.NewDevfileRegistryReconciler())
117116
reconcilerManager.AddReconciler(pluginregistry.NewPluginRegistryReconciler())
118117
reconcilerManager.AddReconciler(editorsdefinitions.NewEditorsDefinitionsReconciler())
118+
reconcilerManager.AddReconciler(devworkspace.NewDwoNamespaceReconciler())
119119
reconcilerManager.AddReconciler(dashboard.NewDashboardReconciler())
120120
reconcilerManager.AddReconciler(gateway.NewGatewayReconciler())
121121
reconcilerManager.AddReconciler(server.NewCheServerReconciler())

pkg/common/chetypes/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type DeployContext struct {
3232
Proxy *Proxy
3333
IsSelfSignedCertificate bool
3434
CheHost string
35+
DwoNamespace string
3536
}
3637

3738
type ClusterAPI struct {

pkg/common/test/deploy_context.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ func (f *DeployContextBuild) Build() *chetypes.DeployContext {
6868
ClientWrapper: k8s_client.NewK8sClient(fakeClient, scheme),
6969
NonCachingClientWrapper: k8s_client.NewK8sClient(fakeClient, scheme),
7070
},
71-
Proxy: &chetypes.Proxy{},
71+
Proxy: &chetypes.Proxy{},
72+
DwoNamespace: "devworkspace-controller",
7273
}
7374

7475
if f.cheCluster != nil {

pkg/deploy/container-capabilities/container_capabilities.go

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

1515
import (
1616
"context"
17-
"fmt"
1817
"time"
1918

2019
chev2 "github.com/eclipse-che/che-operator/api/v2"
@@ -24,18 +23,13 @@ import (
2423
"github.com/eclipse-che/che-operator/pkg/common/utils"
2524
ctrl "sigs.k8s.io/controller-runtime"
2625

27-
"k8s.io/apimachinery/pkg/labels"
28-
29-
"sigs.k8s.io/controller-runtime/pkg/client"
30-
3126
"k8s.io/apimachinery/pkg/types"
3227

3328
"github.com/eclipse-che/che-operator/pkg/common/chetypes"
3429
"github.com/eclipse-che/che-operator/pkg/common/constants"
3530
defaults "github.com/eclipse-che/che-operator/pkg/common/operator-defaults"
3631
"github.com/eclipse-che/che-operator/pkg/deploy"
3732
securityv1 "github.com/openshift/api/security/v1"
38-
corev1 "k8s.io/api/core/v1"
3933
rbacv1 "k8s.io/api/rbac/v1"
4034
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4135
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -116,11 +110,6 @@ func (r *ContainerCapabilitiesReconciler) sync(ctx *chetypes.DeployContext, cc C
116110
return nil
117111
}
118112

119-
devWorkspaceServiceAccountNamespace, err := r.getDevWorkspaceServiceAccountNamespace(ctx)
120-
if err != nil {
121-
return err
122-
}
123-
124113
if err := ctx.ClusterAPI.ClientWrapper.Sync(
125114
context.TODO(),
126115
r.getDWOClusterRole(
@@ -135,7 +124,7 @@ func (r *ContainerCapabilitiesReconciler) sync(ctx *chetypes.DeployContext, cc C
135124
if err := ctx.ClusterAPI.ClientWrapper.Sync(
136125
context.TODO(),
137126
r.getDWClusterRoleBinding(
138-
devWorkspaceServiceAccountNamespace,
127+
ctx.DwoNamespace,
139128
cc.getDWOClusterRoleName(),
140129
cc.getDWOClusterRoleBindingName(),
141130
),
@@ -239,35 +228,6 @@ func (r *ContainerCapabilitiesReconciler) delete(ctx *chetypes.DeployContext, cc
239228
return nil
240229
}
241230

242-
// getDevWorkspaceServiceAccountNamespace returns the namespace of the DevWorkspace ServiceAccount.
243-
// It searches for the DevWorkspace Operator Pods by its labels.
244-
func (r *ContainerCapabilitiesReconciler) getDevWorkspaceServiceAccountNamespace(ctx *chetypes.DeployContext) (string, error) {
245-
selector := labels.SelectorFromSet(
246-
labels.Set{
247-
constants.KubernetesNameLabelKey: constants.DevWorkspaceControllerName,
248-
constants.KubernetesPartOfLabelKey: constants.DevWorkspaceOperatorName,
249-
},
250-
)
251-
252-
items, err := ctx.ClusterAPI.NonCachingClientWrapper.List(
253-
context.TODO(),
254-
&corev1.PodList{},
255-
&client.ListOptions{LabelSelector: selector},
256-
)
257-
if err != nil {
258-
return "", err
259-
}
260-
261-
for _, item := range items {
262-
pod := item.(*corev1.Pod)
263-
if pod.Spec.ServiceAccountName == constants.DevWorkspaceServiceAccountName {
264-
return pod.Namespace, nil
265-
}
266-
}
267-
268-
return "", fmt.Errorf("ServiceAccount %s not found", constants.DevWorkspaceServiceAccountName)
269-
}
270-
271231
func (r *ContainerCapabilitiesReconciler) getUserClusterRole(sccName string, clusterRoleName string) *rbacv1.ClusterRole {
272232
return &rbacv1.ClusterRole{
273233
TypeMeta: metav1.TypeMeta{

pkg/deploy/container-capabilities/container_capabilities_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ func TestShouldNotSyncSCCIfAlreadyExists(t *testing.T) {
149149
}
150150

151151
ctx := test.NewCtxBuilder().WithObjects(dwPod, sccBuild, sccRun).Build()
152+
ctx.DwoNamespace = "devworkspace-controller"
152153

153154
ctx.CheCluster.Spec.DevEnvironments.DisableContainerBuildCapabilities = pointer.Bool(false)
154155
ctx.CheCluster.Spec.DevEnvironments.ContainerBuildConfiguration = &chev2.ContainerBuildConfiguration{OpenShiftSecurityContextConstraint: "scc-build"}

pkg/deploy/dashboard/deployment_dashboard.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ func (d *DashboardReconciler) getDashboardDeploymentSpec(ctx *chetypes.DeployCon
9292
corev1.EnvVar{
9393
Name: "CHECLUSTER_CR_NAME",
9494
Value: ctx.CheCluster.Name},
95+
corev1.EnvVar{
96+
Name: "DWO_NAMESPACE",
97+
Value: ctx.DwoNamespace,
98+
},
9599
)
96100

97101
envVars = append(envVars,

pkg/deploy/dev-workspace-config/dev_workspace_config.go renamed to pkg/deploy/devworkspace/dev_workspace_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Red Hat, Inc. - initial API and implementation
1111
//
1212

13-
package devworkspaceconfig
13+
package devworkspace
1414

1515
import (
1616
"encoding/json"

pkg/deploy/dev-workspace-config/dev_workspace_config_test.go renamed to pkg/deploy/devworkspace/dev_workspace_config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Red Hat, Inc. - initial API and implementation
1111
//
1212

13-
package devworkspaceconfig
13+
package devworkspace
1414

1515
import (
1616
"context"
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// Copyright (c) 2019-2025 Red Hat, Inc.
3+
// This program and the accompanying materials are made
4+
// available under the terms of the Eclipse Public License 2.0
5+
// which is available at https://www.eclipse.org/legal/epl-2.0/
6+
//
7+
// SPDX-License-Identifier: EPL-2.0
8+
//
9+
// Contributors:
10+
// Red Hat, Inc. - initial API and implementation
11+
//
12+
13+
package devworkspace
14+
15+
import (
16+
"context"
17+
"fmt"
18+
19+
"github.com/eclipse-che/che-operator/pkg/common/chetypes"
20+
"github.com/eclipse-che/che-operator/pkg/common/constants"
21+
"github.com/eclipse-che/che-operator/pkg/common/reconciler"
22+
corev1 "k8s.io/api/core/v1"
23+
"k8s.io/apimachinery/pkg/labels"
24+
"sigs.k8s.io/controller-runtime/pkg/client"
25+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
26+
)
27+
28+
type DwoNamespaceReconciler struct {
29+
reconciler.Reconcilable
30+
}
31+
32+
func NewDwoNamespaceReconciler() *DwoNamespaceReconciler {
33+
return &DwoNamespaceReconciler{}
34+
}
35+
36+
func (r *DwoNamespaceReconciler) Reconcile(ctx *chetypes.DeployContext) (reconcile.Result, bool, error) {
37+
dwoNamespace, err := r.getDevWorkspaceNamespace(ctx)
38+
if err != nil {
39+
return reconcile.Result{}, false, err
40+
}
41+
42+
ctx.DwoNamespace = dwoNamespace
43+
return reconcile.Result{}, true, nil
44+
}
45+
46+
func (r *DwoNamespaceReconciler) Finalize(ctx *chetypes.DeployContext) bool {
47+
return true
48+
}
49+
50+
// getDevWorkspaceNamespace returns the namespace of the DevWorkspace operator.
51+
// It searches for the DevWorkspace Operator Pods by its labels.
52+
func (r *DwoNamespaceReconciler) getDevWorkspaceNamespace(ctx *chetypes.DeployContext) (string, error) {
53+
selector := labels.SelectorFromSet(
54+
labels.Set{
55+
constants.KubernetesNameLabelKey: constants.DevWorkspaceControllerName,
56+
constants.KubernetesPartOfLabelKey: constants.DevWorkspaceOperatorName,
57+
},
58+
)
59+
60+
items, err := ctx.ClusterAPI.NonCachingClientWrapper.List(
61+
context.TODO(),
62+
&corev1.PodList{},
63+
&client.ListOptions{LabelSelector: selector},
64+
)
65+
if err != nil {
66+
return "", err
67+
}
68+
69+
for _, item := range items {
70+
pod := item.(*corev1.Pod)
71+
if pod.Spec.ServiceAccountName == constants.DevWorkspaceServiceAccountName {
72+
return pod.Namespace, nil
73+
}
74+
}
75+
76+
return "", fmt.Errorf("DevWorkspace namespace not found")
77+
}

pkg/deploy/dev-workspace-config/init_test.go renamed to pkg/deploy/devworkspace/init_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Red Hat, Inc. - initial API and implementation
1111
//
1212

13-
package devworkspaceconfig
13+
package devworkspace
1414

1515
import (
1616
"github.com/devfile/devworkspace-operator/pkg/infrastructure"

0 commit comments

Comments
 (0)