Skip to content

Commit 8e07871

Browse files
committed
Restrict caches for Roles and Rolebindings by name
Signed-off-by: Angel Misevski <amisevsk@redhat.com>
1 parent 6c83e0a commit 8e07871

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

pkg/cache/cache.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ package cache
1616
import (
1717
"fmt"
1818

19+
"github.com/devfile/devworkspace-operator/pkg/common"
1920
"github.com/devfile/devworkspace-operator/pkg/constants"
2021
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
2122
routev1 "github.com/openshift/api/route/v1"
2223
appsv1 "k8s.io/api/apps/v1"
2324
batchv1 "k8s.io/api/batch/v1"
2425
corev1 "k8s.io/api/core/v1"
2526
networkingv1 "k8s.io/api/networking/v1"
27+
rbacv1 "k8s.io/api/rbac/v1"
28+
"k8s.io/apimachinery/pkg/fields"
2629
"k8s.io/apimachinery/pkg/labels"
2730
"sigs.k8s.io/controller-runtime/pkg/cache"
2831
)
@@ -71,6 +74,12 @@ func GetCacheFunc() (cache.NewCacheFunc, error) {
7174
&corev1.Secret{}: {
7275
Label: secretObjectSelector,
7376
},
77+
&rbacv1.Role{}: {
78+
Field: fields.SelectorFromSet(fields.Set{"metadata.name": common.WorkspaceRoleName()}),
79+
},
80+
&rbacv1.RoleBinding{}: {
81+
Field: fields.SelectorFromSet(fields.Set{"metadata.name": common.WorkspaceRolebindingName()}),
82+
},
7483
}
7584

7685
if infrastructure.IsOpenShift() {

pkg/common/naming.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"fmt"
2020
"regexp"
2121
"strings"
22+
23+
"github.com/devfile/devworkspace-operator/pkg/constants"
2224
)
2325

2426
var NonAlphaNumRegexp = regexp.MustCompile(`[^a-z0-9]+`)
@@ -95,3 +97,11 @@ func AutoMountSecretVolumeName(volumeName string) string {
9597
func AutoMountPVCVolumeName(pvcName string) string {
9698
return fmt.Sprintf("automount-pvc-%s", pvcName)
9799
}
100+
101+
func WorkspaceRoleName() string {
102+
return "workspace"
103+
}
104+
105+
func WorkspaceRolebindingName() string {
106+
return constants.ServiceAccount + "dw"
107+
}

pkg/provision/workspace/rbac.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ package workspace
1717

1818
import (
1919
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
20+
"github.com/devfile/devworkspace-operator/pkg/common"
2021
"github.com/devfile/devworkspace-operator/pkg/provision/sync"
2122

22-
"github.com/devfile/devworkspace-operator/pkg/constants"
23-
2423
rbacv1 "k8s.io/api/rbac/v1"
2524
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2625
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -52,7 +51,7 @@ func generateRBAC(namespace string) []client.Object {
5251
return []client.Object{
5352
&rbacv1.Role{
5453
ObjectMeta: metav1.ObjectMeta{
55-
Name: "workspace",
54+
Name: common.WorkspaceRoleName(),
5655
Namespace: namespace,
5756
},
5857
Rules: []rbacv1.PolicyRule{
@@ -96,7 +95,7 @@ func generateRBAC(namespace string) []client.Object {
9695
},
9796
&rbacv1.RoleBinding{
9897
ObjectMeta: metav1.ObjectMeta{
99-
Name: constants.ServiceAccount + "-dw",
98+
Name: common.WorkspaceRolebindingName(),
10099
Namespace: namespace,
101100
},
102101
RoleRef: rbacv1.RoleRef{

0 commit comments

Comments
 (0)