Skip to content

Commit 33c4fc3

Browse files
simo5enj
authored andcommitted
Handle reconciliation annotation during conversion
Signed-off-by: Simo Sorce <[email protected]>
1 parent be05ce5 commit 33c4fc3

File tree

3 files changed

+127
-31
lines changed

3 files changed

+127
-31
lines changed

pkg/authorization/apis/authorization/conversion.go

+44
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
"github.com/openshift/origin/pkg/user/apis/user/validation"
1313
)
1414

15+
// reconcileProtectAnnotation is the name of an annotation which prevents reconciliation if set to "true"
16+
// can't use this const in pkg/oc/admin/policy because of import cycle
17+
const reconcileProtectAnnotation = "openshift.io/reconcile-protect"
18+
1519
func addConversionFuncs(scheme *runtime.Scheme) error {
1620
if err := scheme.AddConversionFuncs(
1721
Convert_authorization_ClusterRole_To_rbac_ClusterRole,
@@ -30,6 +34,7 @@ func addConversionFuncs(scheme *runtime.Scheme) error {
3034

3135
func Convert_authorization_ClusterRole_To_rbac_ClusterRole(in *ClusterRole, out *rbac.ClusterRole, _ conversion.Scope) error {
3236
out.ObjectMeta = in.ObjectMeta
37+
out.Annotations = convert_authorization_Annotations_To_rbac_Annotations(in.Annotations)
3338
out.Rules = convert_api_PolicyRules_To_rbac_PolicyRules(in.Rules)
3439
return nil
3540
}
@@ -154,6 +159,7 @@ func getRBACRoleRefKind(namespace string) string {
154159

155160
func Convert_rbac_ClusterRole_To_authorization_ClusterRole(in *rbac.ClusterRole, out *ClusterRole, _ conversion.Scope) error {
156161
out.ObjectMeta = in.ObjectMeta
162+
out.Annotations = convert_rbac_Annotations_To_authorization_Annotations(in.Annotations)
157163
out.Rules = Convert_rbac_PolicyRules_To_authorization_PolicyRules(in.Rules)
158164
return nil
159165
}
@@ -241,3 +247,41 @@ func Convert_rbac_PolicyRules_To_authorization_PolicyRules(in []rbac.PolicyRule)
241247
}
242248
return rules
243249
}
250+
251+
func copyMapExcept(in map[string]string, except string) map[string]string {
252+
out := map[string]string{}
253+
for k, v := range in {
254+
if k != except {
255+
out[k] = v
256+
}
257+
}
258+
return out
259+
}
260+
261+
var stringBool = sets.NewString("true", "false")
262+
263+
func convert_authorization_Annotations_To_rbac_Annotations(in map[string]string) map[string]string {
264+
if value, ok := in[reconcileProtectAnnotation]; ok && stringBool.Has(value) {
265+
out := copyMapExcept(in, reconcileProtectAnnotation)
266+
if value == "true" {
267+
out[rbac.AutoUpdateAnnotationKey] = "false"
268+
} else {
269+
out[rbac.AutoUpdateAnnotationKey] = "true"
270+
}
271+
return out
272+
}
273+
return in
274+
}
275+
276+
func convert_rbac_Annotations_To_authorization_Annotations(in map[string]string) map[string]string {
277+
if value, ok := in[rbac.AutoUpdateAnnotationKey]; ok && stringBool.Has(value) {
278+
out := copyMapExcept(in, rbac.AutoUpdateAnnotationKey)
279+
if value == "true" {
280+
out[reconcileProtectAnnotation] = "false"
281+
} else {
282+
out[reconcileProtectAnnotation] = "true"
283+
}
284+
return out
285+
}
286+
return in
287+
}

pkg/authorization/apis/authorization/conversion_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,58 @@ func TestResourceAndNonResourceRuleSplit(t *testing.T) {
295295
}
296296
}
297297

298+
func TestAnnotationsConversion(t *testing.T) {
299+
for _, boolval := range []string{"true", "false"} {
300+
ocr := &authorizationapi.ClusterRole{
301+
Rules: []authorizationapi.PolicyRule{},
302+
}
303+
ocr.Annotations = map[string]string{"openshift.io/reconcile-protect": boolval}
304+
ocr2 := &authorizationapi.ClusterRole{}
305+
crcr := &rbac.ClusterRole{}
306+
if err := authorizationapi.Convert_authorization_ClusterRole_To_rbac_ClusterRole(ocr, crcr, nil); err != nil {
307+
t.Fatal(err)
308+
}
309+
value, ok := crcr.Annotations[rbac.AutoUpdateAnnotationKey]
310+
if ok {
311+
if (boolval == "true" && value != "false") || (boolval == "false" && value != "true") {
312+
t.Fatal(fmt.Errorf("Wrong conversion value, 'true' instead of 'false'"))
313+
}
314+
} else {
315+
t.Fatal(fmt.Errorf("Missing converted Annotation"))
316+
}
317+
if err := authorizationapi.Convert_rbac_ClusterRole_To_authorization_ClusterRole(crcr, ocr2, nil); err != nil {
318+
t.Fatal(err)
319+
}
320+
if !reflect.DeepEqual(ocr, ocr2) {
321+
t.Errorf("origin cluster data not preserved; the diff is %s", diff.ObjectDiff(ocr, ocr2))
322+
}
323+
324+
rcr := &rbac.ClusterRole{
325+
Rules: []rbac.PolicyRule{},
326+
}
327+
rcr.Annotations = map[string]string{rbac.AutoUpdateAnnotationKey: boolval}
328+
rcr2 := &rbac.ClusterRole{}
329+
cocr := &authorizationapi.ClusterRole{}
330+
if err := authorizationapi.Convert_rbac_ClusterRole_To_authorization_ClusterRole(rcr, cocr, nil); err != nil {
331+
t.Fatal(err)
332+
}
333+
value, ok = cocr.Annotations["openshift.io/reconcile-protect"]
334+
if ok {
335+
if (boolval == "true" && value != "false") || (boolval == "false" && value != "true") {
336+
t.Fatal(fmt.Errorf("Wrong conversion value, 'true' instead of 'false'"))
337+
}
338+
} else {
339+
t.Fatal(fmt.Errorf("Missing converted Annotation"))
340+
}
341+
if err := authorizationapi.Convert_authorization_ClusterRole_To_rbac_ClusterRole(cocr, rcr2, nil); err != nil {
342+
t.Fatal(err)
343+
}
344+
if !reflect.DeepEqual(rcr, rcr2) {
345+
t.Errorf("rbac cluster data not preserved; the diff is %s", diff.ObjectDiff(rcr, rcr2))
346+
}
347+
}
348+
}
349+
298350
var fuzzer = fuzz.New().NilChance(0).Funcs(
299351
func(*metav1.TypeMeta, fuzz.Continue) {}, // Ignore TypeMeta
300352
func(*runtime.Object, fuzz.Continue) {}, // Ignore AttributeRestrictions since they are deprecated

0 commit comments

Comments
 (0)