-
Notifications
You must be signed in to change notification settings - Fork 49
/
rbac.go
93 lines (86 loc) · 2.59 KB
/
rbac.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// Copyright (c) 2019-2021 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package provision
import (
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/devworkspace-operator/pkg/constants"
"github.com/go-logr/logr"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// SyncRBAC generates RBAC and synchronizes the runtime objects
func SyncRBAC(workspace *dw.DevWorkspace, client client.Client, reqLogger logr.Logger) ProvisioningStatus {
rbac := generateRBAC(workspace.Namespace)
requeue, err := SyncMutableObjects(rbac, client, reqLogger)
return ProvisioningStatus{Continue: !requeue, Err: err}
}
func generateRBAC(namespace string) []runtime.Object {
// TODO: The rolebindings here are created namespace-wide; find a way to limit this, given that each workspace
return []runtime.Object{
&rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: "workspace",
Namespace: namespace,
},
Rules: []rbacv1.PolicyRule{
{
Resources: []string{"pods/exec"},
APIGroups: []string{""},
Verbs: []string{"create"},
},
{
Resources: []string{"pods"},
APIGroups: []string{""},
Verbs: []string{"get", "list", "watch"},
},
{
Resources: []string{"deployments", "replicasets"},
APIGroups: []string{"apps", "extensions"},
Verbs: []string{"get", "list", "watch"},
},
{
Resources: []string{"devworkspaces"},
APIGroups: []string{"workspace.devfile.io"},
Verbs: []string{"get", "watch", "list", "patch", "update"},
},
{
Resources: []string{"devworkspaceroutings"},
APIGroups: []string{"controller.devfile.io"},
Verbs: []string{"get", "list", "watch"},
},
{
Resources: []string{"devworkspacetemplates"},
APIGroups: []string{"workspace.devfile.io"},
Verbs: []string{"get", "create", "patch", "update", "delete", "list", "watch"},
},
},
},
&rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: constants.ServiceAccount + "-dw",
Namespace: namespace,
},
RoleRef: rbacv1.RoleRef{
Kind: "Role",
Name: "workspace",
},
Subjects: []rbacv1.Subject{
{
Kind: "Group",
Name: "system:serviceaccounts:" + namespace,
},
},
},
}
}