/
k8s_role.go
53 lines (44 loc) · 1.06 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
package k8s
import (
"context"
"fmt"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// CreateRole creates a role
func CreateRole(
namespace,
name string,
labels map[string]string,
policyRules []rbacv1.PolicyRule,
) error {
role := &rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Rules: policyRules,
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if !IsInitialized() {
return fmt.Errorf("knuu is not initialized")
}
if _, err := Clientset().RbacV1().Roles(namespace).Create(ctx, role, metav1.CreateOptions{}); err != nil {
return err
}
return nil
}
// DeleteRole deletes a role
func DeleteRole(namespace, name string) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if !IsInitialized() {
return fmt.Errorf("knuu is not initialized")
}
if err := Clientset().RbacV1().Roles(namespace).Delete(ctx, name, metav1.DeleteOptions{}); err != nil {
return err
}
return nil
}