Skip to content

Commit c1a4a58

Browse files
authored
[oidc] make keycloak deletable (#1178)
<!-- Thank you for making a contribution! Here are some tips for you: - Start the PR title with the [label] of Cozystack component: - For system components: [platform], [system], [linstor], [cilium], [kube-ovn], [dashboard], [cluster-api], etc. - For managed apps: [apps], [tenant], [kubernetes], [postgres], [virtual-machine] etc. - For development and maintenance: [tests], [ci], [docs], [maintenance]. - If it's a work in progress, consider creating this PR as a draft. - Don't hesistate to ask for opinion and review in the community chats, even if it's still a draft. - Add the label `backport` if it's a bugfix that needs to be backported to a previous version. --> ## What this PR does ### Release note <!-- Write a release note: - Explain what has changed internally and for users. - Start with the same [label] as in the PR title - Follow the guidelines at https://github.com/kubernetes/community/blob/master/contributors/guide/release-notes.md. --> ```release-note [oidc] make keycloak deletable ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit * **New Features** * Added automated cleanup of Keycloak-related resources during uninstallation to ensure smooth deletion. * **Bug Fixes** * Improved conditional logic for enabling OIDC and Keycloak-related resources, ensuring they are only activated when explicitly set to "true". * **Chores** * Updated version numbers and references for the tenant application. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 1b7a597 + 1faf40c commit c1a4a58

6 files changed

Lines changed: 153 additions & 5 deletions

File tree

‎packages/apps/tenant/Chart.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ description: Separated tenant namespace
44
icon: /logos/tenant.svg
55

66
type: application
7-
version: 1.11.0
7+
version: 1.11.1

‎packages/apps/tenant/templates/info.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{- $cozyConfig := lookup "v1" "ConfigMap" "cozy-system" "cozystack" }}
22
{{- $oidcEnabled := index $cozyConfig.data "oidc-enabled" }}
3-
{{- if $oidcEnabled }}
3+
{{- if eq $oidcEnabled "true" }}
44
apiVersion: helm.toolkit.fluxcd.io/v2
55
kind: HelmRelease
66
metadata:

‎packages/apps/tenant/templates/keycloakgroups.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{- $cozyConfig := lookup "v1" "ConfigMap" "cozy-system" "cozystack" }}
22
{{- $oidcEnabled := index $cozyConfig.data "oidc-enabled" }}
3-
{{- if $oidcEnabled }}
3+
{{- if eq $oidcEnabled "true" }}
44
apiVersion: v1.edp.epam.com/v1
55
kind: KeycloakRealmGroup
66
metadata:

‎packages/apps/versions_map‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ tcp-balancer 0.4.1 62cb694d
139139
tcp-balancer 0.4.2 4369b031
140140
tcp-balancer 0.5.0 HEAD
141141
tenant 1.10.0 4369b031
142-
tenant 1.11.0 HEAD
142+
tenant 1.11.0 70f82667
143+
tenant 1.11.1 HEAD
143144
virtual-machine 0.1.4 f2015d65
144145
virtual-machine 0.1.5 263e47be
145146
virtual-machine 0.2.0 c0685f43

‎packages/core/platform/templates/helmreleases.yaml‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{{- $dependencyNamespaces := dict }}
55
{{- $disabledComponents := splitList "," ((index $cozyConfig.data "bundle-disable") | default "") }}
66
{{- $enabledComponents := splitList "," ((index $cozyConfig.data "bundle-enable") | default "") }}
7+
{{- $oidcEnabled := (index (default dict $cozyConfig.data) "oidc-enabled") | default "false" | eq "true" }}
78

89
{{/* collect dependency namespaces from releases */}}
910
{{- range $x := $bundle.releases }}
@@ -14,7 +15,18 @@
1415

1516
{{- $shouldInstall := true }}
1617
{{- $shouldDelete := false }}
17-
{{- if or (has $x.name $disabledComponents) (and ($x.optional) (not (has $x.name $enabledComponents))) }}
18+
{{- $notEnabledOptionalComponent := and ($x.optional) (not (has $x.name $enabledComponents)) }}
19+
{{- $disabledComponent := has $x.name $disabledComponents }}
20+
{{- $isKeycloakComponent := or (eq $x.name "keycloak") (eq $x.name "keycloak-operator") (eq $x.name "keycloak-configure") }}
21+
22+
{{- if and $isKeycloakComponent (not $oidcEnabled) }}
23+
{{- $shouldInstall = false }}
24+
{{- if $.Capabilities.APIVersions.Has "helm.toolkit.fluxcd.io/v2" }}
25+
{{- if lookup "helm.toolkit.fluxcd.io/v2" "HelmRelease" $x.namespace $x.name }}
26+
{{- $shouldDelete = true }}
27+
{{- end }}
28+
{{- end }}
29+
{{- else if or $disabledComponent $notEnabledOptionalComponent }}
1830
{{- $shouldInstall = false }}
1931
{{- if $.Capabilities.APIVersions.Has "helm.toolkit.fluxcd.io/v2" }}
2032
{{- if lookup "helm.toolkit.fluxcd.io/v2" "HelmRelease" $x.namespace $x.name }}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
apiVersion: batch/v1
3+
kind: Job
4+
metadata:
5+
annotations:
6+
"helm.sh/hook": pre-delete
7+
"helm.sh/hook-weight": "10"
8+
"helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation,hook-failed
9+
name: {{ .Release.Name }}-flux-teardown
10+
spec:
11+
template:
12+
spec:
13+
serviceAccountName: {{ .Release.Name }}-flux-teardown
14+
restartPolicy: Never
15+
tolerations:
16+
- key: CriticalAddonsOnly
17+
operator: Exists
18+
- key: node-role.kubernetes.io/control-plane
19+
operator: Exists
20+
effect: "NoSchedule"
21+
containers:
22+
- name: kubectl
23+
image: docker.io/clastix/kubectl:v1.32
24+
command:
25+
- /bin/sh
26+
- -c
27+
- |
28+
for resource in KeycloakRealmGroup KeycloakClientScope KeycloakClient; do
29+
kubectl get "$resource" -A --no-headers -o custom-columns="NAMESPACE:.metadata.namespace,NAME:.metadata.name" | \
30+
while read -r namespace name; do
31+
kubectl patch "$resource" "$name" -n "$namespace" --type=merge -p '{"metadata":{"finalizers":[]}}'
32+
done
33+
done
34+
35+
for resource in ClusterKeycloakRealm ClusterKeycloak; do
36+
kubectl get "$resource" --no-headers -o custom-columns="NAME:.metadata.name" | \
37+
while read -r name; do
38+
kubectl patch "$resource" "$name" --type=merge -p '{"metadata":{"finalizers":[]}}'
39+
done
40+
done
41+
42+
kubectl patch hr keycloak-configure -n cozy-system --type=merge -p '{"metadata":{"finalizers":[]}}'
43+
44+
45+
---
46+
apiVersion: v1
47+
kind: ServiceAccount
48+
metadata:
49+
name: {{ .Release.Name }}-flux-teardown
50+
annotations:
51+
helm.sh/hook: pre-delete
52+
helm.sh/hook-delete-policy: before-hook-creation,hook-failed
53+
helm.sh/hook-weight: "0"
54+
---
55+
apiVersion: rbac.authorization.k8s.io/v1
56+
kind: ClusterRole
57+
metadata:
58+
annotations:
59+
"helm.sh/hook": pre-install,post-install,pre-delete
60+
"helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation,hook-failed
61+
"helm.sh/hook-weight": "5"
62+
name: {{ .Release.Name }}-flux-teardown
63+
rules:
64+
- apiGroups:
65+
- "v1.edp.epam.com"
66+
resources:
67+
- keycloakrealmgroups
68+
- keycloakclientscopes
69+
- keycloakclients
70+
- clusterkeycloakrealms
71+
- clusterkeycloaks
72+
- keycloakrealms
73+
- keycloakrealmusers
74+
- keycloakrealmroles
75+
- keycloakrealmidentityproviders
76+
- keycloakrealmcomponents
77+
- keycloakauthflows
78+
- keycloaks
79+
verbs:
80+
- get
81+
- list
82+
- delete
83+
- patch
84+
---
85+
apiVersion: rbac.authorization.k8s.io/v1
86+
kind: ClusterRoleBinding
87+
metadata:
88+
name: {{ .Release.Name }}-flux-teardown
89+
roleRef:
90+
apiGroup: rbac.authorization.k8s.io
91+
kind: ClusterRole
92+
name: {{ .Release.Name }}-flux-teardown
93+
subjects:
94+
- kind: ServiceAccount
95+
name: {{ .Release.Name }}-flux-teardown
96+
namespace: {{ .Release.Namespace }}
97+
---
98+
apiVersion: rbac.authorization.k8s.io/v1
99+
kind: Role
100+
metadata:
101+
annotations:
102+
"helm.sh/hook": pre-install,post-install,pre-delete
103+
"helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation,hook-failed
104+
"helm.sh/hook-weight": "5"
105+
name: {{ .Release.Name }}-flux-teardown
106+
rules:
107+
- apiGroups:
108+
- "helm.toolkit.fluxcd.io"
109+
resources:
110+
- helmreleases
111+
verbs:
112+
- get
113+
- list
114+
- delete
115+
- watch
116+
- patch
117+
resourceNames:
118+
- {{ .Release.Name }}
119+
---
120+
apiVersion: rbac.authorization.k8s.io/v1
121+
kind: RoleBinding
122+
metadata:
123+
annotations:
124+
helm.sh/hook: pre-delete
125+
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation,hook-failed
126+
helm.sh/hook-weight: "5"
127+
name: {{ .Release.Name }}-flux-teardown
128+
roleRef:
129+
apiGroup: rbac.authorization.k8s.io
130+
kind: Role
131+
name: {{ .Release.Name }}-flux-teardown
132+
subjects:
133+
- kind: ServiceAccount
134+
name: {{ .Release.Name }}-flux-teardown
135+
namespace: {{ .Release.Namespace }}

0 commit comments

Comments
 (0)