Skip to content

Stop the external auth mirror from clearing conditions it did not write - #7162

Merged
ChrisJBurns merged 3 commits into
stacklok:mainfrom
im-jari-niskala:fix/external-auth-mirror-keeps-healthy-condition
Nov 25, 2026
Merged

ChrisJBurns merged 3 commits into
stacklok:mainfrom
im-jari-niskala:fix/external-auth-mirror-keeps-healthy-condition

Conversation

@im-jari-niskala

@im-jari-niskala im-jari-niskala commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When a referenced MCPExternalAuthConfig is healthy, mirrorInvalidOnMCPServer and mirrorInvalidOnRemoteProxy removed the consumer's ExternalAuthConfigValidated condition unless ownedByLocalValidation claimed it. isLocalValidationFailure claims only False conditions, so that included the healthy condition (True, ExternalAuthConfigValid), which handleExternalAuthConfig re-adds a few lines later with a fresh LastTransitionTime. Every reconcile of a healthy MCPServer or MCPRemoteProxy with an externalAuthConfigRef therefore wrote status, and the write triggered the next reconcile through the consumer's own watch, and a reconcile of every controller that watches MCPServers, such as MCPGroup.

  • New clearStaleMirror removes the condition only when it is False and ownedByLocalValidation does not attribute it to another owner. The mirror only ever writes False, so a True condition is never a stale mirror.
  • isLocalValidationFailure also recognises MultiUpstreamNotSupported (ConditionReasonExternalAuthConfigMultiUpstream, the same string as the MCPRemoteProxy constant), unless its message carries mirroredMessagePrefix. The single-upstream check re-derives that condition after the mirror runs, like the two existing owners, so it flapped the same way. The same-hash guards in setMCPServerExternalAuthConfigValidCondition and setMCPRemoteProxyExternalAuthConfigValidCondition also require Reason == InvalidConfig, so they behave as before.
  • Both mirror functions call clearStaleMirror. The heal path is unchanged: a stale mirrored False is still removed once the source is valid, which the existing "stale mirror is cleared when source has healed" case covers.

VirtualMCPServer needs no change. It uses mirroredExternalAuthConfigInvalid only to build errors, and its status collector keeps one entry per condition type, so a remove followed by a set collapses into a set.

Fixes #7161

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

New table tests TestMCPServerReconciler_handleExternalAuthConfig_DoesNotFlapLastTransitionTime and TestMCPRemoteProxyReconciler_handleExternalAuthConfig_DoesNotFlapLastTransitionTime seed the condition with a LastTransitionTime an hour in the past, the same technique as TestMCPServerReconciler_InvalidCABundleDoesNotFlapLastTransitionTime, and check that handleExternalAuthConfig leaves it unchanged, for the healthy True condition and for the MultiUpstreamNotSupported failure. TestIsLocalValidationFailure gains a local and a mirrored MultiUpstreamNotSupported case. After rebasing onto main at e7570b0: all three tests fail on main's external_auth_mirror.go and pass with this change, and go test passes for every unit-test package under cmd/thv-operator (the envtest suites under test-integration were not run locally). golangci-lint v2.15.0 reported 0 issues on cmd/thv-operator/controllers before the rebase and was not re-run after it.

Manual: a build of v0.63.0 with this change was deployed to the cluster described in #7161. Afterwards all 14 MCPServers kept the same resourceVersion over 30 s, and operator CPU went from about 311m to about 8m. That deploy also restarted the operator, which on a second cluster appeared to quiet the churn on its own for a while, so the unit tests above are the direct evidence for the change.

API Compatibility

  • This PR does not break the v1beta1 API, OR the api-break-allowed label is applied and the migration guidance is described above.

Does this introduce a user-facing change?

Yes. An MCPServer or MCPRemoteProxy that references a healthy MCPExternalAuthConfig no longer has its status rewritten on every reconcile, so its ExternalAuthConfigValidated condition keeps its lastTransitionTime, and the operator's reconcile rate, CPU use and log volume drop to what the cluster's real changes cause.

🤖 Generated with Claude Code

@ChrisJBurns

Copy link
Copy Markdown
Collaborator

Thanks for the PR @im-jari-niskala , you'll have to rebase with main though and resolve some conflicts

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.15%. Comparing base (504241a) to head (bb38c47).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7162      +/-   ##
==========================================
+ Coverage   82.10%   82.15%   +0.04%     
==========================================
  Files         931      931              
  Lines       94306    94309       +3     
==========================================
+ Hits        77434    77479      +45     
+ Misses      16867    16825      -42     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

mirrorInvalidOnMCPServer and mirrorInvalidOnRemoteProxy run on every
reconcile. When the referenced MCPExternalAuthConfig is healthy they
removed the consumer's ExternalAuthConfigValidated condition unless
ownedByLocalValidation claimed it. isLocalValidationFailure only claims
False conditions, so that included the healthy condition (True,
ExternalAuthConfigValid), which handleExternalAuthConfig re-adds a few
lines later with a fresh LastTransitionTime. Every reconcile of a healthy
MCPServer or MCPRemoteProxy with an externalAuthConfigRef therefore wrote
status, and the write triggered the next reconcile through the consumer's
own watch.

The mirror only ever writes Status=False, so clear the condition only
when it is False and no local validation owns it (clearStaleMirror). The
single-upstream check (MultiUpstreamNotSupported) re-derives its condition
after the mirror runs in the same way, so isLocalValidationFailure now
claims it too. The same-hash guards in the set*ExternalAuthConfigValid
writers still hold only InvalidConfig, so they are unchanged.

Signed-off-by: Jari Niskala <[email protected]>
@im-jari-niskala
im-jari-niskala force-pushed the fix/external-auth-mirror-keeps-healthy-condition branch from 3e439b6 to 29225ae Compare November 25, 2026 20:41
@ChrisJBurns
ChrisJBurns merged commit a1cdea0 into stacklok:main Nov 25, 2026
42 checks passed
ChrisJBurns added a commit to 3nesdeniz/toolhive that referenced this pull request Nov 26, 2026
Resolve conflicts with the OSS Entra OBO port (stacklok#7201) and the external
auth mirror rework (stacklok#7162):

- Keep the UnsupportedAuthType check alongside main's OBO incoming-auth
  check in MCPServer, and treat UnsupportedAuthType as a local-validation
  reason in isLocalValidationFailure.
- Route the MCPServer terminal-error path through main's
  markExternalAuthConfigFailed, keeping the PR's skip of unchanged status
  writes.
- Drop "obo when an enterprise handler is registered" wording and the
  deprecated EnterpriseRequired reason from the PR's godoc and tests; obo
  is now built in.
- Move the support-matrix dispatch test into a fresh tokenexchange_test.go
  (main deleted the old file) and pass the consumer in main's OBO tests.
- Regenerate CRD manifests and API docs.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: Chris Burns <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Operator rewrites MCPServer status on every reconcile when externalAuthConfigRef is healthy (ExternalAuthConfigValidated lastTransitionTime flaps)

2 participants