-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbac.go
160 lines (135 loc) · 3.97 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package simulator
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
serviceAccountName = "batch-simulator"
clusterRoleName = "batch-simulator-role"
clusterRoleBindingName = "batch-simulator-binding"
)
func CreateRBAC(ctx context.Context, clientset kubernetes.Interface, namespace string) error {
err := createServiceAccount(ctx, clientset, namespace)
if err != nil {
return fmt.Errorf("error creating service account: %w", err)
}
err = createClusterRole(ctx, clientset)
if err != nil {
return fmt.Errorf("error creating role: %w", err)
}
err = createClusterRoleBinding(ctx, clientset, namespace)
if err != nil {
return fmt.Errorf("error creating role binding: %w", err)
}
return nil
}
func createServiceAccount(ctx context.Context, clientset kubernetes.Interface, namespace string) error {
serviceAccount := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: serviceAccountName,
},
}
_, err := clientset.CoreV1().ServiceAccounts(namespace).Create(ctx, serviceAccount, metav1.CreateOptions{})
if err != nil {
return err
}
return nil
}
func createClusterRole(ctx context.Context, clientset kubernetes.Interface) error {
role := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: clusterRoleName,
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"nodes"},
Verbs: []string{"create", "delete", "get", "list", "watch"},
},
{
APIGroups: []string{""},
Resources: []string{"pods", "pods/log"},
Verbs: []string{"create", "delete", "get", "list", "watch"},
},
{
APIGroups: []string{""},
Resources: []string{"namespaces"},
Verbs: []string{"create", "delete", "get", "list", "watch"},
},
{
APIGroups: []string{"batch"},
Resources: []string{"jobs"},
Verbs: []string{"create", "delete", "get", "list", "watch"},
},
},
}
_, err := clientset.RbacV1().ClusterRoles().Create(ctx, role, metav1.CreateOptions{})
if err != nil {
return err
}
return nil
}
func createClusterRoleBinding(ctx context.Context, clientset kubernetes.Interface, namespace string) error {
clusterRoleBinding := &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: clusterRoleBindingName,
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: serviceAccountName,
Namespace: namespace,
},
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: clusterRoleName,
APIGroup: "rbac.authorization.k8s.io",
},
}
_, err := clientset.RbacV1().ClusterRoleBindings().Create(ctx, clusterRoleBinding, metav1.CreateOptions{})
if err != nil {
return err
}
return nil
}
func DeleteRBAC(ctx context.Context, clientset kubernetes.Interface, namespace string) error {
err := deleteClusterRole(ctx, clientset)
if err != nil {
return fmt.Errorf("error deleting role: %w", err)
}
err = deleteClusterRoleBinding(ctx, clientset)
if err != nil {
return fmt.Errorf("error deleting role binding: %w", err)
}
err = deleteServiceAccount(ctx, clientset, namespace)
if err != nil {
return fmt.Errorf("error deleting service account: %w", err)
}
return nil
}
func deleteServiceAccount(ctx context.Context, clientset kubernetes.Interface, namespace string) error {
err := clientset.CoreV1().ServiceAccounts(namespace).Delete(ctx, serviceAccountName, metav1.DeleteOptions{})
if err != nil {
return err
}
return nil
}
func deleteClusterRole(ctx context.Context, clientset kubernetes.Interface) error {
err := clientset.RbacV1().ClusterRoles().Delete(ctx, clusterRoleName, metav1.DeleteOptions{})
if err != nil {
return err
}
return nil
}
func deleteClusterRoleBinding(ctx context.Context, clientset kubernetes.Interface) error {
err := clientset.RbacV1().ClusterRoleBindings().Delete(ctx, clusterRoleBindingName, metav1.DeleteOptions{})
if err != nil {
return err
}
return nil
}