/
rbac.go
100 lines (89 loc) · 2.03 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
94
95
96
97
98
99
100
package operator
import (
"fmt"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func MakeRBACObjects(name, namespace, component string, additionalRules []rbacv1.PolicyRule) (*rbacv1.ClusterRole, *corev1.ServiceAccount, *rbacv1.ClusterRoleBinding) {
rbacName := fmt.Sprintf("kubesphere-%s", component)
cr := rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: rbacName,
},
Rules: []rbacv1.PolicyRule{
{
Verbs: []string{"get"},
APIGroups: []string{""},
Resources: []string{"pods"},
},
},
}
if additionalRules != nil {
cr.Rules = append(cr.Rules, additionalRules...)
}
sa := corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
crb := rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: rbacName,
},
Subjects: []rbacv1.Subject{
{
Kind: rbacv1.ServiceAccountKind,
Name: name,
Namespace: namespace,
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Kind: "ClusterRole",
Name: rbacName,
},
}
return &cr, &sa, &crb
}
func MakeScopedRBACObjects(fbName, fbNamespace string) (*rbacv1.Role, *corev1.ServiceAccount, *rbacv1.RoleBinding) {
r := rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: "kubesphere:fluent",
Namespace: fbNamespace,
},
Rules: []rbacv1.PolicyRule{
{
Verbs: []string{"get"},
APIGroups: []string{""},
Resources: []string{"pods"},
},
},
}
sa := corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: fbName,
Namespace: fbNamespace,
},
}
rb := rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "kubesphere:fluent",
Namespace: fbNamespace,
},
Subjects: []rbacv1.Subject{
{
Kind: rbacv1.ServiceAccountKind,
Name: fbName,
Namespace: fbNamespace,
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Kind: "Role",
Name: "kubesphere:fluent",
},
}
return &r, &sa, &rb
}