-
Notifications
You must be signed in to change notification settings - Fork 34
/
k8s_role.go
58 lines (50 loc) · 1.42 KB
/
k8s_role.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
package k8s
import (
"context"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (c *Client) CreateRole(
ctx context.Context,
name string,
labels map[string]string,
policyRules []rbacv1.PolicyRule,
) error {
role := &rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: c.namespace,
Labels: labels,
},
Rules: policyRules,
}
_, err := c.clientset.RbacV1().Roles(c.namespace).Create(ctx, role, metav1.CreateOptions{})
return err
}
func (c *Client) DeleteRole(ctx context.Context, name string) error {
return c.clientset.RbacV1().Roles(c.namespace).Delete(ctx, name, metav1.DeleteOptions{})
}
func (c *Client) CreateClusterRole(
ctx context.Context,
name string,
labels map[string]string,
policyRules []rbacv1.PolicyRule,
) error {
_, err := c.clientset.RbacV1().ClusterRoles().Get(ctx, name, metav1.GetOptions{})
if err == nil || !errors.IsNotFound(err) {
return ErrClusterRoleAlreadyExists.WithParams(name).Wrap(err)
}
role := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: labels,
},
Rules: policyRules,
}
_, err = c.clientset.RbacV1().ClusterRoles().Create(ctx, role, metav1.CreateOptions{})
return err
}
func (c *Client) DeleteClusterRole(ctx context.Context, name string) error {
return c.clientset.RbacV1().ClusterRoles().Delete(ctx, name, metav1.DeleteOptions{})
}