-
Notifications
You must be signed in to change notification settings - Fork 184
feat(platform-auth): add functions to manage platform auth pod restar… #3491
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
Open
CasLubbers
wants to merge
5
commits into
main
Choose a base branch
from
APL-1985
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4b48ef1
feat(platform-auth): add functions to manage platform auth pod restar…
CasLubbers ccd39ff
Potential fix for pull request finding
CasLubbers 9cb8007
feat: add constant for configmap name
CasLubbers 62a0fdf
Merge branch 'main' into APL-1985
CasLubbers 5eb1030
Merge branch 'main' into APL-1985
svcAPLBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
adr/2026-08-04-restart-platform-auth-pods-after-oauth2-proxy-healthy.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # Restart platform-auth pods once oauth2-proxy is healthy | ||
|
|
||
| - Status: accepted | ||
|
|
||
| ## Context and Problem Statement | ||
|
|
||
| On install, Keycloak is not yet available when Istio first resolves the JWKS used by `RequestAuthentication`. Istio's sidecar proxy caches whatever it fetched at that point (a dummy/invalid key) and does not reliably re-fetch once Keycloak comes up, so any pod whose sidecar started before Keycloak was ready keeps failing JWT verification ("JWT verification failed") until it happens to self-heal (several minutes) or is restarted. This affects every pod labelled `otomi.io/auth: platform` (see [ADR-2026-06-12](2026-06-12-auth-policy-pod-label.md)), not just Prometheus where it was first observed. | ||
|
|
||
| The sidecar's fetch races Keycloak independently of the app container's own readiness gating (an app's initContainer waiting on Keycloak does not delay its Istio sidecar). Two previously attempted mitigations — tuning `PILOT_JWT_PUB_KEY_REFRESH_INTERVAL` and `PILOT_DEBOUNCE_MAX` — did not solve it and were reverted. | ||
|
|
||
| `oauth2-proxy` already gates its own startup on Keycloak's OIDC issuer being reachable (`wait-for-keycloak` initContainer in `values/oauth2-proxy/oauth2-proxy.gotmpl`), making "oauth2-proxy's ArgoCD Application is Healthy" the most reliable available signal that Keycloak is actually serving real keys. | ||
|
|
||
| ## Decision Drivers | ||
|
|
||
| - Must not add a new long-running process or watcher to `apl-operator` — it already runs two concurrent loops and additional operational complexity there is undesirable. | ||
| - Cannot hook into the Phase 1 install sequence (`src/cmd/install.ts`): oauth2-proxy's ArgoCD Application is only created by `applyAsApps()`, which is only ever invoked from Phase 2's poll/reconcile loop (`apl-operator.ts`). Phase 1 has nothing to wait for. | ||
| - Must fire exactly once per cluster lifetime — restarting the same pods on every apply cycle would cause needless churn. | ||
| - Should reuse existing, tested primitives rather than introduce new infrastructure (chart changes, RBAC, shell scripts). | ||
|
|
||
| ## Considered Options | ||
|
|
||
| - Add a blocking wait-then-restart step to `src/cmd/install.ts` (Phase 1) — impossible, see decision drivers. | ||
| - Start a separate watcher process/goroutine inside `apl-operator` dedicated to this — rejected, adds a third concurrent loop to an already complex operator. | ||
| - An ArgoCD `PostSync` hook Job shipped inside the `oauth2-proxy` chart (via its existing `extraObjects` support), restarting labelled pods once the oauth2-proxy Application syncs healthy — viable, keeps the operator untouched, but adds new chart/RBAC/script surface and duplicates the "wait for Keycloak" signal in bash instead of reusing the operator's existing ArgoCD health-check helper. | ||
| - Piggyback on the existing Phase 2 poll/reconcile loop in `apl-operator.ts` (chosen) — see Decision Outcome. | ||
|
|
||
| ## Decision Outcome | ||
|
|
||
| Chosen option: piggyback on the existing poll/reconcile loop. | ||
|
|
||
| At the end of `runApplyIfNotBusy`'s success path (`apl-operator.ts`), after every successful apply — both `Poll`- and `Reconcile`-triggered — do a single non-blocking check of the oauth2-proxy ArgoCD Application's health (`checkArgoCDAppStatus`, no retry wrapper; the loop's own cadence is the retry). If healthy and a dedicated marker ConfigMap (e.g. `apl-platform-auth-restart-state`, separate from `apl-operator-state` since it tracks an unrelated, one-shot concern) is not yet set: restart every pod labelled `otomi.io/auth: platform` across all namespaces, reusing `restartPodOwner`/`getWorkloadKeyFromPod` from `src/common/runtime-upgrades/restart-istio-sidecars.ts`, then write the marker. | ||
|
|
||
| This adds no new interval, loop, or process — the check rides on cadence that already exists (sub-second while there are git changes to apply during install, at minimum every 5 minutes via the reconcile timer regardless). It also runs unconditionally on upgrades, since there is no cheap way to distinguish "fresh install" from "upgrade" at this point, and a single unnecessary pod-restart round on upgrade is harmless. | ||
|
|
||
| Restarting all `otomi.io/auth: platform` pods unconditionally (rather than only the ones observed failing) is deliberate: the bug is systemic, not app-specific, and scoping it to specific apps would just leave the bug in place for whichever app wasn't listed. | ||
|
|
||
| Fixing sidecar restarts after an Istio version upgrade is a related but separate problem, already solved by `detectAndRestartOutdatedIstioSidecars` (wired as a versioned runtime-upgrade hook in `src/common/runtime-upgrades/runtime-upgrades.ts`); it is out of scope here. | ||
|
|
||
| ### Negative Consequences | ||
|
|
||
| - A pod whose sidecar races Keycloak independently of oauth2-proxy's own timing (i.e. starts before Keycloak is ready even though oauth2-proxy is already healthy) is not covered by this one-shot fix. Judged acceptable: Keycloak is a lighter, earlier-starting component than most affected apps, and the ticket already accepts a multi-minute self-heal as tolerable for stragglers. |
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
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
49 changes: 49 additions & 0 deletions
49
src/common/runtime-upgrades/restart-platform-auth-pods.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { CoreV1Api } from '@kubernetes/client-node' | ||
| import { PLATFORM_AUTH_LABEL_SELECTOR, restartPlatformAuthPods } from './restart-platform-auth-pods' | ||
|
|
||
| describe('restartPlatformAuthPods', () => { | ||
| const mockCoreApi = { | ||
| listPodForAllNamespaces: jest.fn(), | ||
| } as unknown as jest.Mocked<CoreV1Api> | ||
|
|
||
| const mockDeps = { | ||
| getWorkloadKeyFromPod: jest.fn(), | ||
| restartPodOwner: jest.fn(), | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| it('lists pods labelled otomi.io/auth=platform', async () => { | ||
| mockCoreApi.listPodForAllNamespaces.mockResolvedValue({ items: [] }) | ||
|
|
||
| await restartPlatformAuthPods(mockCoreApi, mockDeps) | ||
|
|
||
| expect(mockCoreApi.listPodForAllNamespaces).toHaveBeenCalledWith({ | ||
| labelSelector: PLATFORM_AUTH_LABEL_SELECTOR, | ||
| }) | ||
| }) | ||
|
|
||
| it('restarts the owner of each matching pod', async () => { | ||
| mockDeps.getWorkloadKeyFromPod.mockReturnValueOnce('ns/deploy-a').mockReturnValueOnce('ns/deploy-b') | ||
| mockCoreApi.listPodForAllNamespaces.mockResolvedValue({ | ||
| items: [{ metadata: { namespace: 'ns', name: 'pod-a' } }, { metadata: { namespace: 'ns', name: 'pod-b' } }], | ||
| }) | ||
|
|
||
| await restartPlatformAuthPods(mockCoreApi, mockDeps) | ||
|
|
||
| expect(mockDeps.restartPodOwner).toHaveBeenCalledTimes(2) | ||
| }) | ||
|
|
||
| it('restarts each distinct workload only once', async () => { | ||
| mockDeps.getWorkloadKeyFromPod.mockReturnValue('ns/deploy-a') | ||
| mockCoreApi.listPodForAllNamespaces.mockResolvedValue({ | ||
| items: [{ metadata: { namespace: 'ns', name: 'pod-a-1' } }, { metadata: { namespace: 'ns', name: 'pod-a-2' } }], | ||
| }) | ||
|
|
||
| await restartPlatformAuthPods(mockCoreApi, mockDeps) | ||
|
|
||
| expect(mockDeps.restartPodOwner).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { CoreV1Api } from '@kubernetes/client-node' | ||
| import { getParsedArgs } from '../yargs' | ||
| import { terminal } from '../debug' | ||
| import { getWorkloadKeyFromPod, restartPodOwner } from './restart-istio-sidecars' | ||
|
|
||
| export const PLATFORM_AUTH_LABEL_SELECTOR = 'otomi.io/auth=platform' | ||
| export const OAUTH2_PROXY_ARGOCD_APP_NAME = 'istio-system-oauth2-proxy' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Declared here, but it seems to be exclusively used in |
||
|
|
||
| export async function restartPlatformAuthPods( | ||
| coreV1Api: CoreV1Api, | ||
| deps = { getWorkloadKeyFromPod, restartPodOwner }, | ||
| ): Promise<void> { | ||
| const d = terminal('restartPlatformAuthPods') | ||
| const parsedArgs = getParsedArgs() | ||
|
|
||
| const podsResponse = await coreV1Api.listPodForAllNamespaces({ labelSelector: PLATFORM_AUTH_LABEL_SELECTOR }) | ||
| const pods = podsResponse.items | ||
|
|
||
| d.info(`Found ${pods.length} pods labelled ${PLATFORM_AUTH_LABEL_SELECTOR}`) | ||
|
|
||
| const restartedWorkloads = new Set<string>() | ||
|
|
||
| for (const pod of pods) { | ||
| const workloadKey = deps.getWorkloadKeyFromPod(pod) | ||
| if (workloadKey && restartedWorkloads.has(workloadKey)) continue | ||
|
|
||
| await deps.restartPodOwner(pod, d, parsedArgs) | ||
| if (workloadKey) restartedWorkloads.add(workloadKey) | ||
| } | ||
|
|
||
| d.info(`Restarted ${restartedWorkloads.size} workloads with platform-auth pods`) | ||
| } | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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 prefer skipping the APL_ prefix. The id is quite long as it is.