-
Notifications
You must be signed in to change notification settings - Fork 0
/
rolebinding.go
48 lines (41 loc) · 1.08 KB
/
rolebinding.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
package resource
import (
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type RoleBindingOption func(*rbacv1.RoleBinding)
func NewRoleBinding(namespace, name, roleName, subjectName string, opts ...RoleBindingOption) *rbacv1.RoleBinding {
crGVK := rbacv1.SchemeGroupVersion.WithKind("ClusterRole")
saGVK := corev1.SchemeGroupVersion.WithKind("ServiceAccount")
meta := NewMeta(namespace, name)
rb := &rbacv1.RoleBinding{
TypeMeta: metav1.TypeMeta{
Kind: "RoleBinding",
APIVersion: rbacv1.SchemeGroupVersion.String(),
},
ObjectMeta: *meta,
RoleRef: rbacv1.RoleRef{
APIGroup: crGVK.Group,
Kind: crGVK.Kind,
Name: roleName,
},
Subjects: []rbacv1.Subject{{
APIGroup: saGVK.Group,
Kind: saGVK.Kind,
Namespace: namespace,
Name: subjectName,
}},
}
for _, opt := range opts {
opt(rb)
}
return rb
}
func RoleBindingWithMetaOptions(opts ...MetaOption) RoleBindingOption {
return func(s *rbacv1.RoleBinding) {
for _, opt := range opts {
opt(&s.ObjectMeta)
}
}
}