1
0
mirror of https://github.com/rancher/rke.git synced 2025-04-29 12:03:35 +00:00
rke/k8s/role.go

55 lines
1.8 KiB
Go
Raw Permalink Normal View History

2017-12-20 01:51:07 +00:00
package k8s
import (
"context"
2017-12-20 01:51:07 +00:00
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2017-12-20 01:51:07 +00:00
"k8s.io/client-go/kubernetes"
)
func UpdateRoleBindingFromYaml(k8sClient *kubernetes.Clientset, roleBindingYaml, namespace string) error {
2017-12-20 01:51:07 +00:00
roleBinding := rbacv1.RoleBinding{}
2019-10-03 01:56:39 +00:00
if err := DecodeYamlResource(&roleBinding, roleBindingYaml); err != nil {
2017-12-20 01:51:07 +00:00
return err
}
roleBinding.Namespace = namespace
return retryTo(updateRoleBinding, k8sClient, roleBinding, DefaultRetries, DefaultSleepSeconds)
2017-12-20 01:51:07 +00:00
}
func updateRoleBinding(k8sClient *kubernetes.Clientset, rb interface{}) error {
roleBinding := rb.(rbacv1.RoleBinding)
if _, err := k8sClient.RbacV1().RoleBindings(roleBinding.Namespace).Create(context.TODO(), &roleBinding, metav1.CreateOptions{}); err != nil {
2017-12-20 01:51:07 +00:00
if !apierrors.IsAlreadyExists(err) {
return err
}
if _, err := k8sClient.RbacV1().RoleBindings(roleBinding.Namespace).Update(context.TODO(), &roleBinding, metav1.UpdateOptions{}); err != nil {
2017-12-20 01:51:07 +00:00
return err
}
}
return nil
}
func UpdateRoleFromYaml(k8sClient *kubernetes.Clientset, roleYaml, namespace string) error {
2017-12-20 01:51:07 +00:00
role := rbacv1.Role{}
2019-10-03 01:56:39 +00:00
if err := DecodeYamlResource(&role, roleYaml); err != nil {
2017-12-20 01:51:07 +00:00
return err
}
role.Namespace = namespace
return retryTo(updateRole, k8sClient, role, DefaultRetries, DefaultSleepSeconds)
2017-12-20 01:51:07 +00:00
}
func updateRole(k8sClient *kubernetes.Clientset, r interface{}) error {
role := r.(rbacv1.Role)
if _, err := k8sClient.RbacV1().Roles(role.Namespace).Create(context.TODO(), &role, metav1.CreateOptions{}); err != nil {
2017-12-20 01:51:07 +00:00
if !apierrors.IsAlreadyExists(err) {
return err
}
if _, err := k8sClient.RbacV1().Roles(role.Namespace).Update(context.TODO(), &role, metav1.UpdateOptions{}); err != nil {
2017-12-20 01:51:07 +00:00
return err
}
}
return nil
}