-
Notifications
You must be signed in to change notification settings - Fork 90
/
rbac.go
78 lines (60 loc) · 2.47 KB
/
rbac.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package resources
import (
"context"
"github.com/pkg/errors"
kotsadmobjects "github.com/replicatedhq/kots/pkg/kotsadm/objects"
rbacv1 "k8s.io/api/rbac/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func EnsureKotsadmRole(namespace string, clientset kubernetes.Interface) error {
role := kotsadmobjects.KotsadmRole(namespace)
currentRole, err := clientset.RbacV1().Roles(namespace).Get(context.TODO(), "kotsadm-role", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get role")
}
_, err := clientset.RbacV1().Roles(namespace).Create(context.TODO(), role, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "failed to create role")
}
return nil
}
currentRole = updateKotsadmRole(currentRole, role)
// we have now changed the role, so an upgrade is required
_, err = clientset.RbacV1().Roles(namespace).Update(context.TODO(), currentRole, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "failed to update role")
}
return nil
}
func updateKotsadmRole(existing, desiredRole *rbacv1.Role) *rbacv1.Role {
existing.Rules = desiredRole.Rules
return existing
}
func EnsureKotsadmRoleBinding(roleBindingNamespace string, kotsadmNamespace string, clientset kubernetes.Interface) error {
roleBinding := kotsadmobjects.KotsadmRoleBinding(roleBindingNamespace, kotsadmNamespace)
currentRoleBinding, err := clientset.RbacV1().RoleBindings(roleBindingNamespace).Get(context.TODO(), "kotsadm-rolebinding", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get rolebinding")
}
_, err := clientset.RbacV1().RoleBindings(roleBindingNamespace).Create(context.TODO(), roleBinding, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "failed to create rolebinding")
}
return nil
}
currentRoleBinding = updateKotsadmRoleBinding(currentRoleBinding, roleBinding)
// we have now changed the rolebinding, so an upgrade is required
_, err = clientset.RbacV1().RoleBindings(roleBindingNamespace).Update(context.TODO(), currentRoleBinding, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "failed to update rolebinding")
}
return nil
}
func updateKotsadmRoleBinding(existing, desiredRoleBinding *rbacv1.RoleBinding) *rbacv1.RoleBinding {
existing.Subjects = desiredRoleBinding.Subjects
return existing
}