-
Notifications
You must be signed in to change notification settings - Fork 163
/
rbac.go
133 lines (125 loc) · 3.5 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package resourceapply
import (
"context"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/equality"
rbacv1client "k8s.io/client-go/kubernetes/typed/rbac/v1"
rbacv1listers "k8s.io/client-go/listers/rbac/v1"
"k8s.io/client-go/tools/record"
)
func ApplyClusterRoleWithControl(
ctx context.Context,
control ApplyControlInterface[*rbacv1.ClusterRole],
recorder record.EventRecorder,
required *rbacv1.ClusterRole,
options ApplyOptions,
) (*rbacv1.ClusterRole, bool, error) {
return ApplyGeneric[*rbacv1.ClusterRole](ctx, control, recorder, required, options)
}
func ApplyClusterRole(
ctx context.Context,
client rbacv1client.ClusterRolesGetter,
lister rbacv1listers.ClusterRoleLister,
recorder record.EventRecorder,
required *rbacv1.ClusterRole,
options ApplyOptions,
) (*rbacv1.ClusterRole, bool, error) {
return ApplyClusterRoleWithControl(
ctx,
ApplyControlFuncs[*rbacv1.ClusterRole]{
GetCachedFunc: lister.Get,
CreateFunc: client.ClusterRoles().Create,
UpdateFunc: client.ClusterRoles().Update,
DeleteFunc: client.ClusterRoles().Delete,
},
recorder,
required,
options,
)
}
func ApplyRoleBindingWithControl(
ctx context.Context,
control ApplyControlInterface[*rbacv1.RoleBinding],
recorder record.EventRecorder,
required *rbacv1.RoleBinding,
options ApplyOptions,
) (*rbacv1.RoleBinding, bool, error) {
return ApplyGenericWithHandlers[*rbacv1.RoleBinding](
ctx,
control,
recorder,
required,
options,
nil,
func(required *rbacv1.RoleBinding, existing *rbacv1.RoleBinding) string {
if !equality.Semantic.DeepEqual(existing.RoleRef, (*required).RoleRef) {
return "roleRef is immutable"
}
return ""
},
)
}
func ApplyRoleBinding(
ctx context.Context,
client rbacv1client.RoleBindingsGetter,
lister rbacv1listers.RoleBindingLister,
recorder record.EventRecorder,
required *rbacv1.RoleBinding,
options ApplyOptions,
) (*rbacv1.RoleBinding, bool, error) {
return ApplyRoleBindingWithControl(
ctx,
ApplyControlFuncs[*rbacv1.RoleBinding]{
GetCachedFunc: lister.RoleBindings(required.Namespace).Get,
CreateFunc: client.RoleBindings(required.Namespace).Create,
UpdateFunc: client.RoleBindings(required.Namespace).Update,
DeleteFunc: client.RoleBindings(required.Namespace).Delete,
},
recorder,
required,
options,
)
}
func ApplyClusterRoleBindingWithControl(
ctx context.Context,
control ApplyControlInterface[*rbacv1.ClusterRoleBinding],
recorder record.EventRecorder,
required *rbacv1.ClusterRoleBinding,
options ApplyOptions,
) (*rbacv1.ClusterRoleBinding, bool, error) {
return ApplyGenericWithHandlers[*rbacv1.ClusterRoleBinding](
ctx,
control,
recorder,
required,
options,
nil,
func(required *rbacv1.ClusterRoleBinding, existing *rbacv1.ClusterRoleBinding) string {
if !equality.Semantic.DeepEqual(existing.RoleRef, (*required).RoleRef) {
return "roleRef is immutable"
}
return ""
},
)
}
func ApplyClusterRoleBinding(
ctx context.Context,
client rbacv1client.ClusterRoleBindingsGetter,
lister rbacv1listers.ClusterRoleBindingLister,
recorder record.EventRecorder,
required *rbacv1.ClusterRoleBinding,
options ApplyOptions,
) (*rbacv1.ClusterRoleBinding, bool, error) {
return ApplyClusterRoleBindingWithControl(
ctx,
ApplyControlFuncs[*rbacv1.ClusterRoleBinding]{
GetCachedFunc: lister.Get,
CreateFunc: client.ClusterRoleBindings().Create,
UpdateFunc: client.ClusterRoleBindings().Update,
DeleteFunc: client.ClusterRoleBindings().Delete,
},
recorder,
required,
options,
)
}