Skip to content

Commit 06400ac

Browse files
fix(kubernetes): probe Keycloak CRDs by output in oidc-bootstrap cleanup
`kubectl api-resources --api-group=<absent group>` exits 0 and prints an empty list, so guarding on the exit code always fell through. The delete then failed with `the server doesn't have a resource type "keycloakclient"` — `--ignore-not-found` covers a missing object, not an unknown type. Because the oidc-bootstrap Job is a post-install/post-upgrade Helm hook, that failure blocked the Helm upgrade of every `kubernetes-<cluster>` release on any cluster without the EDP Keycloak operator CRDs, which is the default configuration (`oidc.mode: None`). Capture the discovery output once and gate each delete on its own resource type, matching the probe idiom already used by the platform migrations and the ouroboros cleanup hook. Fixes cozystack#3516 Signed-off-by: Mattia Eleuteri <[email protected]> Assisted-by: LLM
1 parent 5e32ef3 commit 06400ac

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎packages/apps/kubernetes/templates/oidc-rbac-job.yaml‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,28 @@ spec:
214214
# 3. Chart-owned KeycloakClient + audience KeycloakClientScope in
215215
# the cozy realm. These are created by oidc-keycloak.yaml only
216216
# for mode=System; at mode=None they must not linger.
217-
if kubectl api-resources --api-group=v1.edp.epam.com >/dev/null 2>&1; then
218-
echo " deleting chart-owned Keycloak objects"
217+
#
218+
# The CRD probe must test the OUTPUT of `kubectl api-resources`,
219+
# not its exit code: for an absent API group the command exits 0
220+
# and merely prints an empty list. Guarding on the exit code let
221+
# the deletes run on clusters without the Keycloak operator,
222+
# where they fail with `the server doesn't have a resource type
223+
# "keycloakclient"` — `--ignore-not-found` covers a missing
224+
# object, not an unknown type. Since this Job is a
225+
# post-install/post-upgrade hook, that failure blocked every
226+
# `kubernetes-<cluster>` Helm upgrade at oidc.mode=None (the
227+
# chart default), a fully supported configuration.
228+
# api-resources hits the discovery endpoint, which
229+
# system:discovery grants to every authenticated principal, so
230+
# the probe needs no cluster-scoped CRD read permission.
231+
EDP_RESOURCES=$(kubectl api-resources --api-group=v1.edp.epam.com --output=name 2>/dev/null || true)
232+
if echo "$EDP_RESOURCES" | grep -q '^keycloakclients\.'; then
233+
echo " deleting chart-owned KeycloakClient"
219234
kubectl --namespace "$RELEASE_NAMESPACE" delete keycloakclient.v1.edp.epam.com \
220235
"$RELEASE_NAMESPACE-$RELEASE" --ignore-not-found
236+
fi
237+
if echo "$EDP_RESOURCES" | grep -q '^keycloakclientscopes\.'; then
238+
echo " deleting chart-owned KeycloakClientScope"
221239
kubectl --namespace "$RELEASE_NAMESPACE" delete keycloakclientscope.v1.edp.epam.com \
222240
"$RELEASE_NAMESPACE-$RELEASE-audience" --ignore-not-found
223241
fi

‎packages/apps/kubernetes/tests/oidc_test.yaml‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,39 @@ tests:
8282
pattern: 'if \[ "\$OIDC_MODE" = "None" \]'
8383
documentIndex: 3
8484

85+
- it: mode=None Keycloak cleanup probes the CRDs by output, not by exit code
86+
# Regression guard. `kubectl api-resources --api-group=<absent group>` exits 0
87+
# and prints an empty list, so an exit-code guard always falls through and the
88+
# delete fails with `the server doesn't have a resource type "keycloakclient"`
89+
# (--ignore-not-found covers a missing object, not an unknown type). As this
90+
# Job is a post-install/post-upgrade hook, that failure blocks the Helm
91+
# upgrade of every kubernetes-* release on clusters without the Keycloak
92+
# operator — the default configuration.
93+
release:
94+
name: test
95+
namespace: tenant-test
96+
template: templates/oidc-rbac-job.yaml
97+
asserts:
98+
# The probe captures the discovery output ...
99+
- matchRegex:
100+
path: spec.template.spec.containers[0].command[2]
101+
pattern: 'EDP_RESOURCES=\$\(kubectl api-resources --api-group=v1\.edp\.epam\.com --output=name'
102+
documentIndex: 3
103+
# ... and each delete is gated on its own resource type being present.
104+
- matchRegex:
105+
path: spec.template.spec.containers[0].command[2]
106+
pattern: 'if echo "\$EDP_RESOURCES" \| grep -q ..keycloakclients\\\.'
107+
documentIndex: 3
108+
- matchRegex:
109+
path: spec.template.spec.containers[0].command[2]
110+
pattern: 'if echo "\$EDP_RESOURCES" \| grep -q ..keycloakclientscopes\\\.'
111+
documentIndex: 3
112+
# The broken exit-code guard must not come back.
113+
- notMatchRegex:
114+
path: spec.template.spec.containers[0].command[2]
115+
pattern: 'if kubectl api-resources --api-group=v1\.edp\.epam\.com >/dev/null'
116+
documentIndex: 3
117+
85118
- it: emits no OIDC kubeconfig placeholder Secret when oidc.mode is None
86119
release:
87120
name: test

0 commit comments

Comments
 (0)