Skip to content

Commit 7549927

Browse files
committed
fix permission claim label not updated when selector changes
Signed-off-by: olalekan odukoya <[email protected]>
1 parent ed46a47 commit 7549927

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

pkg/reconciler/apis/permissionclaimlabel/permissionclaimlabel_reconcile.go

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@ func (c *controller) reconcile(ctx context.Context, apiBinding *apisv1alpha2.API
8484
}
8585

8686
appliedClaims := sets.New[string]()
87+
appliedClaimsMap := make(map[string]apisv1alpha2.ScopedPermissionClaim)
8788
for _, claim := range apiBinding.Status.AppliedPermissionClaims {
88-
appliedClaims.Insert(setKeyForClaim(claim.PermissionClaim))
89+
key := setKeyForClaim(claim.PermissionClaim)
90+
appliedClaims.Insert(key)
91+
appliedClaimsMap[key] = claim
8992
}
9093

9194
expectedClaims := exportedClaims.Intersection(acceptedClaims)
@@ -94,6 +97,20 @@ func (c *controller) reconcile(ctx context.Context, apiBinding *apisv1alpha2.API
9497
needToRemove := appliedClaims.Difference(acceptedClaims)
9598
allChanges := needToApply.Union(needToRemove)
9699

100+
for key := range expectedClaims {
101+
if acceptedClaims.Has(key) && appliedClaims.Has(key) {
102+
acceptedClaim := acceptedClaimsMap[key]
103+
appliedClaim := appliedClaimsMap[key]
104+
if !selectorsEqual(acceptedClaim.Selector, appliedClaim.Selector) {
105+
allChanges.Insert(key)
106+
107+
logger.V(4).Info("detected selector change for claim", "claim", key,
108+
"oldSelector", appliedClaim.Selector,
109+
"newSelector", acceptedClaim.Selector)
110+
}
111+
}
112+
}
113+
97114
logger.V(4).Info("claim set details",
98115
"expected", expectedClaims,
99116
"unexpected", unexpectedClaims,
@@ -289,3 +306,65 @@ func (c *controller) patchGenericObject(ctx context.Context, obj metav1.Object,
289306
}
290307
return nil
291308
}
309+
310+
// selectorsEqual compares two PermissionClaimSelector objects to determine if they are equal.
311+
// This is needed to detect when only the selector changes (not the claim key).
312+
func selectorsEqual(a, b apisv1alpha2.PermissionClaimSelector) bool {
313+
// Compare MatchAll first
314+
if a.MatchAll != b.MatchAll {
315+
return false
316+
}
317+
318+
// If both are MatchAll, they're equal
319+
if a.MatchAll && b.MatchAll {
320+
return true
321+
}
322+
323+
// Compare MatchLabels
324+
if len(a.MatchLabels) != len(b.MatchLabels) {
325+
return false
326+
}
327+
for k, v := range a.MatchLabels {
328+
if b.MatchLabels[k] != v {
329+
return false
330+
}
331+
}
332+
333+
// Compare MatchExpressions
334+
if len(a.MatchExpressions) != len(b.MatchExpressions) {
335+
return false
336+
}
337+
// Compare each expression individually
338+
aExprs := make(map[string]metav1.LabelSelectorRequirement)
339+
for _, expr := range a.MatchExpressions {
340+
key := fmt.Sprintf("%s:%s", expr.Key, string(expr.Operator))
341+
aExprs[key] = expr
342+
}
343+
for _, expr := range b.MatchExpressions {
344+
key := fmt.Sprintf("%s:%s", expr.Key, string(expr.Operator))
345+
if aExpr, ok := aExprs[key]; !ok {
346+
return false
347+
} else if !matchExpressionEqual(aExpr, expr) {
348+
return false
349+
}
350+
}
351+
352+
return true
353+
}
354+
355+
// matchExpressionEqual compares two LabelSelectorRequirement objects.
356+
func matchExpressionEqual(a, b metav1.LabelSelectorRequirement) bool {
357+
if a.Key != b.Key {
358+
return false
359+
}
360+
if a.Operator != b.Operator {
361+
return false
362+
}
363+
if len(a.Values) != len(b.Values) {
364+
return false
365+
}
366+
// Compare values as sets (order doesn't matter)
367+
aValues := sets.New(a.Values...)
368+
bValues := sets.New(b.Values...)
369+
return aValues.Equal(bValues)
370+
}

0 commit comments

Comments
 (0)