-
Notifications
You must be signed in to change notification settings - Fork 65
/
mco_clusterrolebinding.go
57 lines (50 loc) · 1.86 KB
/
mco_clusterrolebinding.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
// Copyright (c) 2021 Red Hat, Inc.
// Copyright Contributors to the Open Cluster Management project
package utils
import (
"context"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
)
func GetCRB(opt TestOptions, isHub bool, name string) (error, *rbacv1.ClusterRoleBinding) {
clientKube := getKubeClient(opt, isHub)
crb, err := clientKube.RbacV1().ClusterRoleBindings().Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
klog.Errorf("Failed to get cluster rolebinding %s due to %v", name, err)
}
return err, crb
}
func DeleteCRB(opt TestOptions, isHub bool, name string) error {
clientKube := getKubeClient(opt, isHub)
err := clientKube.RbacV1().ClusterRoleBindings().Delete(context.TODO(), name, metav1.DeleteOptions{})
if err != nil {
klog.Errorf("Failed to delete cluster rolebinding %s due to %v", name, err)
}
return err
}
func UpdateCRB(opt TestOptions, isHub bool, name string,
crb *rbacv1.ClusterRoleBinding) (error, *rbacv1.ClusterRoleBinding) {
clientKube := getKubeClient(opt, isHub)
updateCRB, err := clientKube.RbacV1().ClusterRoleBindings().Update(context.TODO(), crb, metav1.UpdateOptions{})
if err != nil {
klog.Errorf("Failed to update cluster rolebinding %s due to %v", name, err)
}
return err, updateCRB
}
func CreateCRB(opt TestOptions, isHub bool,
crb *rbacv1.ClusterRoleBinding) error {
clientKube := getKubeClient(opt, isHub)
_, err := clientKube.RbacV1().ClusterRoleBindings().Create(context.TODO(), crb, metav1.CreateOptions{})
if err != nil {
if errors.IsAlreadyExists(err) {
klog.V(1).Infof("clusterrolebinding %s already exists, updating...", crb.GetName())
err, _ := UpdateCRB(opt, isHub, crb.GetName(), crb)
return err
}
klog.Errorf("Failed to create cluster rolebinding %s due to %v", crb.GetName(), err)
return err
}
return nil
}