forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbac.go
190 lines (154 loc) · 6.59 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package resourceapply
import (
"fmt"
"k8s.io/klog"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
rbacclientv1 "k8s.io/client-go/kubernetes/typed/rbac/v1"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/resource/resourcemerge"
)
// ApplyClusterRole merges objectmeta, requires rules, aggregation rules are not allowed for now.
func ApplyClusterRole(client rbacclientv1.ClusterRolesGetter, recorder events.Recorder, required *rbacv1.ClusterRole) (*rbacv1.ClusterRole, bool, error) {
if required.AggregationRule != nil && len(required.AggregationRule.ClusterRoleSelectors) != 0 {
return nil, false, fmt.Errorf("cannot create an aggregated cluster role")
}
existing, err := client.ClusterRoles().Get(required.Name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
actual, err := client.ClusterRoles().Create(required)
reportCreateEvent(recorder, required, err)
return actual, true, err
}
if err != nil {
return nil, false, err
}
modified := resourcemerge.BoolPtr(false)
existingCopy := existing.DeepCopy()
resourcemerge.EnsureObjectMeta(modified, &existingCopy.ObjectMeta, required.ObjectMeta)
contentSame := equality.Semantic.DeepEqual(existingCopy.Rules, required.Rules)
if contentSame && !*modified {
return existingCopy, false, nil
}
existingCopy.Rules = required.Rules
existingCopy.AggregationRule = nil
if klog.V(4) {
klog.Infof("ClusterRole %q changes: %v", required.Name, JSONPatchNoError(existing, existingCopy))
}
actual, err := client.ClusterRoles().Update(existingCopy)
reportUpdateEvent(recorder, required, err)
return actual, true, err
}
// ApplyClusterRoleBinding merges objectmeta, requires subjects and role refs
// TODO on non-matching roleref, delete and recreate
func ApplyClusterRoleBinding(client rbacclientv1.ClusterRoleBindingsGetter, recorder events.Recorder, required *rbacv1.ClusterRoleBinding) (*rbacv1.ClusterRoleBinding, bool, error) {
existing, err := client.ClusterRoleBindings().Get(required.Name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
actual, err := client.ClusterRoleBindings().Create(required)
reportCreateEvent(recorder, required, err)
return actual, true, err
}
if err != nil {
return nil, false, err
}
modified := resourcemerge.BoolPtr(false)
existingCopy := existing.DeepCopy()
requiredCopy := required.DeepCopy()
// Enforce apiGroup fields in roleRefs
existingCopy.RoleRef.APIGroup = rbacv1.GroupName
for i := range existingCopy.Subjects {
if existingCopy.Subjects[i].Kind == "User" {
existingCopy.Subjects[i].APIGroup = rbacv1.GroupName
}
}
requiredCopy.RoleRef.APIGroup = rbacv1.GroupName
for i := range requiredCopy.Subjects {
if existingCopy.Subjects[i].Kind == "User" {
requiredCopy.Subjects[i].APIGroup = rbacv1.GroupName
}
}
resourcemerge.EnsureObjectMeta(modified, &existingCopy.ObjectMeta, requiredCopy.ObjectMeta)
subjectsAreSame := equality.Semantic.DeepEqual(existingCopy.Subjects, requiredCopy.Subjects)
roleRefIsSame := equality.Semantic.DeepEqual(existingCopy.RoleRef, requiredCopy.RoleRef)
if subjectsAreSame && roleRefIsSame && !*modified {
return existingCopy, false, nil
}
existingCopy.Subjects = requiredCopy.Subjects
existingCopy.RoleRef = requiredCopy.RoleRef
if klog.V(4) {
klog.Infof("ClusterRoleBinding %q changes: %v", requiredCopy.Name, JSONPatchNoError(existing, existingCopy))
}
actual, err := client.ClusterRoleBindings().Update(existingCopy)
reportUpdateEvent(recorder, requiredCopy, err)
return actual, true, err
}
// ApplyRole merges objectmeta, requires rules
func ApplyRole(client rbacclientv1.RolesGetter, recorder events.Recorder, required *rbacv1.Role) (*rbacv1.Role, bool, error) {
existing, err := client.Roles(required.Namespace).Get(required.Name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
actual, err := client.Roles(required.Namespace).Create(required)
reportCreateEvent(recorder, required, err)
return actual, true, err
}
if err != nil {
return nil, false, err
}
modified := resourcemerge.BoolPtr(false)
existingCopy := existing.DeepCopy()
resourcemerge.EnsureObjectMeta(modified, &existingCopy.ObjectMeta, required.ObjectMeta)
contentSame := equality.Semantic.DeepEqual(existingCopy.Rules, required.Rules)
if contentSame && !*modified {
return existingCopy, false, nil
}
existingCopy.Rules = required.Rules
if klog.V(4) {
klog.Infof("Role %q changes: %v", required.Namespace+"/"+required.Name, JSONPatchNoError(existing, existingCopy))
}
actual, err := client.Roles(required.Namespace).Update(existingCopy)
reportUpdateEvent(recorder, required, err)
return actual, true, err
}
// ApplyRoleBinding merges objectmeta, requires subjects and role refs
// TODO on non-matching roleref, delete and recreate
func ApplyRoleBinding(client rbacclientv1.RoleBindingsGetter, recorder events.Recorder, required *rbacv1.RoleBinding) (*rbacv1.RoleBinding, bool, error) {
existing, err := client.RoleBindings(required.Namespace).Get(required.Name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
actual, err := client.RoleBindings(required.Namespace).Create(required)
reportCreateEvent(recorder, required, err)
return actual, true, err
}
if err != nil {
return nil, false, err
}
modified := resourcemerge.BoolPtr(false)
existingCopy := existing.DeepCopy()
requiredCopy := required.DeepCopy()
// Enforce apiGroup fields in roleRefs and subjects
existingCopy.RoleRef.APIGroup = rbacv1.GroupName
for i := range existingCopy.Subjects {
if existingCopy.Subjects[i].Kind == "User" {
existingCopy.Subjects[i].APIGroup = rbacv1.GroupName
}
}
requiredCopy.RoleRef.APIGroup = rbacv1.GroupName
for i := range requiredCopy.Subjects {
if existingCopy.Subjects[i].Kind == "User" {
requiredCopy.Subjects[i].APIGroup = rbacv1.GroupName
}
}
resourcemerge.EnsureObjectMeta(modified, &existingCopy.ObjectMeta, requiredCopy.ObjectMeta)
subjectsAreSame := equality.Semantic.DeepEqual(existingCopy.Subjects, requiredCopy.Subjects)
roleRefIsSame := equality.Semantic.DeepEqual(existingCopy.RoleRef, requiredCopy.RoleRef)
if subjectsAreSame && roleRefIsSame && !*modified {
return existingCopy, false, nil
}
existingCopy.Subjects = requiredCopy.Subjects
existingCopy.RoleRef = requiredCopy.RoleRef
if klog.V(4) {
klog.Infof("RoleBinding %q changes: %v", requiredCopy.Namespace+"/"+requiredCopy.Name, JSONPatchNoError(existing, existingCopy))
}
actual, err := client.RoleBindings(requiredCopy.Namespace).Update(existingCopy)
reportUpdateEvent(recorder, requiredCopy, err)
return actual, true, err
}