-
Notifications
You must be signed in to change notification settings - Fork 962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DevEx] controller/rbac: edge based events for applies #4307
[DevEx] controller/rbac: edge based events for applies #4307
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems reasonable to only create an event when we do something - do you have an example from your testing of what events in this style look like now?
@@ -210,9 +213,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco | |||
} | |||
|
|||
log.Debug("Applied RBAC Role") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be useful in this debug log statement to include the name of the RBAC role that was just applied?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it actually is, look the log.WithValues
above :) Had the same thought.
} | ||
|
||
r.record.Event(ns, event.Normal(reasonApplyRoles, "Applied RBAC Roles")) | ||
sort.Strings(applied) | ||
if len(applied) > 3 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this > 3 pattern pretty common in upstream k8s? i feel like i may have seen this before - not 💯 on it though, as I have hazy memories of thinking to myself "well why didn't you just tell me the rest of them too then?" 😂
more curious than anything - not blocking from me 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we use something like that in OpenShift. No strong opinions. I thought 3 is a good number :) 10 might be too.
7dce9bd
to
c72cfaf
Compare
c72cfaf
to
6ec954e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, thank you. FWIW there's a similar event WRT selecting a Composition in the XR controller that I've been meaning to fix if you're feeling motivated to do so.
Few style nits, but nothing blocking.
if len(applied) > 3 { | ||
r.record.Event(d, event.Normal(reasonApplyRoles, fmt.Sprintf("Applied RBAC ClusterRoles: %s, %s, %s, and %d more", applied[0], applied[1], applied[2], len(applied)-3))) | ||
} else if len(applied) > 0 { | ||
r.record.Event(d, event.Normal(reasonApplyRoles, fmt.Sprintf("Applied RBAC ClusterRoles: %s", strings.Join(applied, ", ")))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given this pattern is repeated a few times I wonder whether a little utility could help? I'm on the fence. Normally I prefer to avoid such little utilities for things like this, until they're almost everywhere, but I must admit I'm a little (unreasonably?) allergic to if then else
statements.
I'm thinking something like:
r.record.Event(d, event.Normal(reasonApplyRoles, maybeTruncate("Applied RBAC ClusterRoles", applied)))
// maybeTruncate needs a better name :)
func maybeTruncate(prefix string, names []string) string {
if len(names) > 3 {
return fmt.Sprintf("%s: %s, and %d more", prefix, strings.Join(names[:3], ", "), len(names)-3)
}
return fmt.Sprintf("%s: %s", prefix, strings.Join(names, ", "))
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added. Any good place to put it? Have 3 copies now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could imagine this being added as a utility in crossplane-runtime somewhere (not sure which package). I'm fine leaving a few copies scattered around for now though - we can figure out a better home if and when it becomes more widely used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack. Leaving it. We can deduplicate later.
PS the |
@@ -528,8 +529,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco | |||
return reconcile.Result{Requeue: true}, errors.Wrap(r.client.Status().Update(ctx, cm), errUpdateClaimStatus) | |||
} | |||
|
|||
log.Debug("Successfully applied composite resource") | |||
record.Event(cm, event.Normal(reasonCompositeConfigure, "Successfully applied composite resource")) | |||
if oldRV != cp.GetResourceVersion() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@negz not sure I am a fan of this construct. Am open for better patterns.
See my comments about the available applicators in Slack.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps something like:
err := r.client.Apply(ctx, cp, resource.AllowUpdateIf(func(current, desired) bool { !cmp.Equal(current, desired) }))
switch {
case resource.IsNotAllowed(err):
log.Debug("Skipped no-op composite resource apply")
case err != nil:
// Current error handling block
default:
log.Debug("Successfully applied composite resource")
record.Event(cm, event.Normal(reasonCompositeConfigure, "Successfully applied composite resource"))
}
I believe this would disallow the apply (i.e. patch) entirely if this reconcile didn't actually change cp
. Using cmp.Equal
on the entire *Unstructured
feels inelegant but this approach at least seems syntactically more pleasant to me.
https://pkg.go.dev/github.com/crossplane/[email protected]/pkg/resource#AllowUpdateIf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was wondering whether this can still lead to multiple events when the informers are lagging behind. Just because an update is attempted does not mean an actual update happened.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was wondering whether this can still lead to multiple events when the informers are lagging behind.
FWIW I think the controller-runtime machinery automatically dedupes multiple events in the queue to result in one reconcile call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and in any case this will eventually settle. If two or more, but few events are creating on rare races, that's fine. Just cosmetics (different to the RV topic in the applicator which is about consistency).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated.
f26326f
to
cb730d8
Compare
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
…nding apply Signed-off-by: Dr. Stefan Schimanski <[email protected]>
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
cb730d8
to
fd64e44
Compare
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
fd64e44
to
4a0bb29
Compare
Description of your changes
The
Applied RBAC roles
event is repeated again and again:Similar for
This PR supresses the events if nothing happens, and it prints up to the first 3 role names in case it's not an noop. With that the event is actually useful.
I have:
make reviewable
to ensure this PR is ready for review.Addedbackport release-x.y
labels to auto-backport this PR if necessary.