fix: add drift detection to ProtocolMapper, Role and ClientScope - #2
Merged
Merged
Conversation
These controllers PUT their definition to Keycloak on every sync (5m)
even when nothing changed. Each protocol mapper PUT invalidates the
owning client in Keycloak's realm cache on all nodes, so the next
token/userinfo requests reload from the DB. On prod (40 mapper CRs)
this meant ~11.6k no-op writes per day and 2-13s token latencies,
pushing the pace-id SLA (v1) from ~99.98% to 99.74%.
Compare the desired definition against the current representation with
the existing definitionsMatch (subset, unordered string arrays) and skip
the PUT when in sync, as the client controller already does:
- ProtocolMapper: list mappers raw and keep the matched representation
- Role: compare via Get{Realm,Client}RoleRaw; composites are stripped
beforehand and still synced through the composites endpoint
- ClientScope: compare via GetClientScopeRaw
A failed fetch falls through to the update, so behaviour on errors is
unchanged. Verified against live prod state: all 40 mappers compare as
in sync.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Since the rollout of v0.12.1 on prod (2026-09-20), the ProtocolMapper, Role and ClientScope controllers PUT their definition to Keycloak on every sync (5m), even when nothing changed.
Operator writes per day on prod (Loki):
Every protocol mapper PUT invalidates the owning client in Keycloak's realm cache on all nodes, so the following
token/userinforequests reload from the DB. Result: 2–13s token latencies (~95 requests >2s per day, previously 0–3). This pushed the pace-id SLA v1 from ~99.98% down to 99.82% / 99.77% / 99.74% (21.–23.09.). Of the slow requests, 61% start within 5s of an operator write, versus 11% for random timestamps (small sample, n=28).Fix
Compare the desired definition against the current Keycloak representation before updating, using the existing
definitionsMatch(subset compare, string arrays unordered). This is the same pattern the client controller already uses:Get{Client,ClientScope}ProtocolMappersRawand keep the representation matched by name (findMapperByName)Get{Realm,Client}RoleRaw.compositesis stripped beforehand as before and synced separately through the composites endpointGetClientScopeRawIf the fetch fails, the controller falls through to the update, so error behaviour is unchanged. Create paths are untouched.
Verification
go test ./internal/...andgo vetpass. New tests inkeycloakprotocolmapper_drift_test.go, using the mapper shape returned by a live Keycloak 26:KeycloakProtocolMapperCRs and both client scopes compare as in sync, so zero PUTs after rollout.Rollout / check
After deploying,
protocol mapper updated successfullyshould drop from ~480/h to ~0. Slow token requests (>2s) should disappear from SLA v1 within a day.Related (separate PR in
keycloak-realm):man-idpusesconfig.hideOnLoginPage, which Keycloak 26 drops in favour of the top-levelhideOnLogin. That causes a perpetual IdP diff.🤖 Generated with Claude Code