Skip to content

Commit 57d2e75

Browse files
committed
fix(cozy-lib): keep the Secret cleanup Job name within 63 characters
The apps API admits release names of up to 53 characters, so <release>-secrets-cleanup could reach 69. Kubernetes copies a Job name into the job-name label of its pods, and a label value takes at most 63 characters. The apiserver refused such a Job, so the uninstall failed and left the Secrets in place. Assisted-by: LLM Signed-off-by: Yan Bondarenko <[email protected]>
1 parent 3e55f98 commit 57d2e75

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎packages/library/cozy-lib/templates/_hooks.tpl‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ a failure there leaves the Secrets in place.
6565
{{- fail (printf "cozy-lib.hooks.deleteSecrets: every entry of certificates must be a Kubernetes resource name, got %q" .) -}}
6666
{{- end -}}
6767
{{- end -}}
68-
{{- $name := printf "%s-secrets-cleanup" $root.Release.Name -}}
68+
{{- /* A Job name goes into the job-name label of its pods, which takes at
69+
most 63 characters, while the apps API admits release names of up to
70+
53. Cutting the suffix keeps the whole release name, so the name stays
71+
unique. */ -}}
72+
{{- $name := printf "%s-secrets-cleanup" $root.Release.Name | trunc 63 | trimSuffix "-" -}}
6973
---
7074
apiVersion: v1
7175
kind: ServiceAccount

‎packages/tests/cozy-lib-tests/tests/hooks_deletesecrets_test.yaml‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,60 @@ tests:
8080
path: spec.template.spec.containers[0].command[2]
8181
pattern: 'if ! kubectl delete certificates\.cert-manager\.io --namespace tenant-acme --ignore-not-found "myapp-tls"; then\s+echo "ERROR: [^"]*" >&2\s+exit 1\s+fi\s+echo "Deleting Secrets myapp-tls left by myapp"\s+if kubectl delete secret'
8282

83+
# The apps API admits release names of up to 53 characters, and a Job name
84+
# goes into the job-name label of its pods, which takes at most 63.
85+
- it: keeps the Job name within 63 characters for a 53-character release name
86+
release:
87+
name: redis-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
88+
set:
89+
deleteSecrets:
90+
secrets: [myapp-tls]
91+
documentSelector:
92+
path: kind
93+
value: Job
94+
asserts:
95+
- matchRegex:
96+
path: metadata.name
97+
pattern: ^.{1,63}$
98+
- equal:
99+
path: metadata.name
100+
value: redis-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-secrets-c
101+
- equal:
102+
path: spec.template.spec.serviceAccountName
103+
value: redis-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-secrets-c
104+
105+
- it: gives the ServiceAccount, Role and RoleBinding the same cut name as the Job
106+
release:
107+
name: redis-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
108+
set:
109+
deleteSecrets:
110+
secrets: [myapp-tls]
111+
documentSelector:
112+
path: metadata.name
113+
value: redis-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-secrets-c
114+
matchMany: true
115+
asserts:
116+
- hasDocuments:
117+
count: 4
118+
filterAware: true
119+
120+
- it: binds the cut Role name to the cut ServiceAccount name
121+
release:
122+
name: redis-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
123+
set:
124+
deleteSecrets:
125+
secrets: [myapp-tls]
126+
documentSelector:
127+
path: kind
128+
value: RoleBinding
129+
asserts:
130+
- equal:
131+
path: roleRef.name
132+
value: redis-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-secrets-c
133+
- equal:
134+
path: subjects[0].name
135+
value: redis-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-secrets-c
136+
83137
- it: refuses an empty Secret list, whose RBAC rule would have no resourceNames
84138
set:
85139
deleteSecrets:

0 commit comments

Comments
 (0)