forked from rancher/rke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.go
49 lines (44 loc) · 1.47 KB
/
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
package k8s
import (
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/kubernetes"
)
func UpdateRoleBindingFromYaml(k8sClient *kubernetes.Clientset, roleBindingYaml string) error {
roleBinding := rbacv1.RoleBinding{}
if err := decodeYamlResource(&roleBinding, roleBindingYaml); err != nil {
return err
}
return retryTo(updateRoleBinding, k8sClient, roleBinding, DefaultRetries, DefaultSleepSeconds)
}
func updateRoleBinding(k8sClient *kubernetes.Clientset, rb interface{}) error {
roleBinding := rb.(rbacv1.RoleBinding)
if _, err := k8sClient.RbacV1().RoleBindings(roleBinding.Namespace).Create(&roleBinding); err != nil {
if !apierrors.IsAlreadyExists(err) {
return err
}
if _, err := k8sClient.RbacV1().RoleBindings(roleBinding.Namespace).Update(&roleBinding); err != nil {
return err
}
}
return nil
}
func UpdateRoleFromYaml(k8sClient *kubernetes.Clientset, roleYaml string) error {
role := rbacv1.Role{}
if err := decodeYamlResource(&role, roleYaml); err != nil {
return err
}
return retryTo(updateRole, k8sClient, role, DefaultRetries, DefaultSleepSeconds)
}
func updateRole(k8sClient *kubernetes.Clientset, r interface{}) error {
role := r.(rbacv1.Role)
if _, err := k8sClient.RbacV1().Roles(role.Namespace).Create(&role); err != nil {
if !apierrors.IsAlreadyExists(err) {
return err
}
if _, err := k8sClient.RbacV1().Roles(role.Namespace).Update(&role); err != nil {
return err
}
}
return nil
}