Skip to content

Commit a2c9c07

Browse files
committed
fix(apps): keep Job and CronJob names within the Kubernetes limits
The apps API admits release names of up to 53 characters, and several charts name a Job <release>-<suffix>. Kubernetes copies a Job name into the job-name label of its pods, and a label value takes at most 63 characters, so the apiserver refused these Jobs for the longest names. A Kafka whose name has 42 to 44 characters could not be installed, a VMDisk of 37 or more could not grow, a VMInstance of 40 or 41 could not take an instance type, profile or external change, and a Bucket of 44 or more or a Qdrant of 42 or more failed its post-delete hook. Those Job names, and the pod label vm-instance copies from one, are now cut to 63 characters. A release name is never longer than 53, so only the fixed suffix is cut and two releases still get two Jobs. A CronJob name is refused over 52 characters, because the controller names each Job it starts <cronjob>-<timestamp>. With the backup CronJob on, a MariaDB whose name has 38 characters or more and a ClickHouse of 35 or more failed the whole release. A cut there reaches into the release name and could give two releases one CronJob, so a name that does not fit becomes the release name cut to 38 characters, a hash of the full release name and -backup, the way the ClickHouse Keeper name is already shortened. The tenant cleanup Job is named after the tenant namespace, which a nested tenant can fill to 63 characters by itself. The Job runs in cozy-system next to that of every other tenant, where a plain cut could give a parent and its child one name and let the child's before-hook-creation delete the parent's Job while it runs, so it is shortened with a hash of the namespace the same way. Names that fit render exactly as before, and a name that did not fit never produced an object, so nothing is renamed on upgrade. The hook ServiceAccounts, Roles and RoleBindings have no such limit and keep their names. Assisted-by: LLM Signed-off-by: Yan Bondarenko <[email protected]>
1 parent aed7fa8 commit a2c9c07

16 files changed

Lines changed: 795 additions & 10 deletions

File tree

‎packages/apps/bucket/templates/hooks/cleanup-acme.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
apiVersion: batch/v1
33
kind: Job
44
metadata:
5-
name: {{ .Release.Name }}-cleanup-acme
5+
name: {{ printf "%s-cleanup-acme" .Release.Name | trunc 63 | trimSuffix "-" }}
66
labels:
77
app.kubernetes.io/instance: {{ .Release.Name }}
88
annotations:
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
suite: bucket cleanup Job name budget
2+
3+
# The API server copies a Job's name into the job-name label of its pods, and a
4+
# label value takes 63 characters at most, while a release name may take 53.
5+
# The ServiceAccount, Role and RoleBinding have no such limit and keep the full
6+
# name, so a hook that ran before keeps the same objects.
7+
8+
templates:
9+
- templates/hooks/cleanup-acme.yaml
10+
11+
set:
12+
_namespace:
13+
host: example.org
14+
15+
tests:
16+
- it: keeps <release>-cleanup-acme for a short release name
17+
release:
18+
name: my-bucket
19+
namespace: tenant-root
20+
documentSelector:
21+
path: kind
22+
value: Job
23+
asserts:
24+
- equal:
25+
path: metadata.name
26+
value: my-bucket-cleanup-acme
27+
28+
- it: keeps the name when it lands exactly on 63 characters
29+
release:
30+
name: bucket-abcdefghijklmnopqrstuvwxyz0123456789abcdefg
31+
namespace: tenant-root
32+
documentSelector:
33+
path: kind
34+
value: Job
35+
asserts:
36+
- equal:
37+
path: metadata.name
38+
value: bucket-abcdefghijklmnopqrstuvwxyz0123456789abcdefg-cleanup-acme
39+
40+
- it: cuts the name to 63 characters for a 53-character release name
41+
release:
42+
name: bucket-abcdefghijklmnopqrstuvwxyz0123456789abcdefghij
43+
namespace: tenant-root
44+
documentSelector:
45+
path: kind
46+
value: Job
47+
asserts:
48+
- equal:
49+
path: metadata.name
50+
value: bucket-abcdefghijklmnopqrstuvwxyz0123456789abcdefghij-cleanup-a
51+
- matchRegex:
52+
path: metadata.name
53+
pattern: ^[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?$
54+
- equal:
55+
path: spec.template.spec.serviceAccountName
56+
value: bucket-abcdefghijklmnopqrstuvwxyz0123456789abcdefghij-cleanup-acme
57+
58+
- it: leaves the RBAC objects on the full name for a 53-character release name
59+
release:
60+
name: bucket-abcdefghijklmnopqrstuvwxyz0123456789abcdefghij
61+
namespace: tenant-root
62+
asserts:
63+
- equal:
64+
path: metadata.name
65+
value: bucket-abcdefghijklmnopqrstuvwxyz0123456789abcdefghij-cleanup-acme
66+
documentSelector:
67+
path: kind
68+
value: ServiceAccount
69+
- equal:
70+
path: roleRef.name
71+
value: bucket-abcdefghijklmnopqrstuvwxyz0123456789abcdefghij-cleanup-acme
72+
documentSelector:
73+
path: kind
74+
value: RoleBinding
75+
- equal:
76+
path: subjects[0].name
77+
value: bucket-abcdefghijklmnopqrstuvwxyz0123456789abcdefghij-cleanup-acme
78+
documentSelector:
79+
path: kind
80+
value: RoleBinding

‎packages/apps/clickhouse/templates/backup-cronjob.yaml‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@
1818
*/}}
1919
{{- if and .Values.backup.enabled .Values.backup.schedule (not .Values.backup.useSystemBucket) }}
2020
{{ $image := .Files.Get "images/backup.json" | fromJson }}
21+
{{- /* The CronJob controller starts each Job as <name>-<timestamp>, so the API
22+
refuses a CronJob name over 52 characters. Cutting the release name to
23+
fit could give two releases one CronJob, so a name that does not fit
24+
keeps a hash of the full release name instead. */}}
25+
{{- $cronJobName := printf "%s-backup" .Release.Name }}
26+
{{- if gt (len $cronJobName) 52 }}
27+
{{- $cronJobName = printf "%s-%s-backup" (.Release.Name | trunc 38 | trimSuffix "-") (.Release.Name | sha256sum | trunc 6) }}
28+
{{- end }}
2129

2230
apiVersion: batch/v1
2331
kind: CronJob
2432
metadata:
25-
name: {{ .Release.Name }}-backup
33+
name: {{ $cronJobName }}
2634
spec:
2735
schedule: "{{ .Values.backup.schedule }}"
2836
concurrencyPolicy: Forbid
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
suite: clickhouse legacy backup CronJob name budget
2+
3+
# The API refuses a CronJob name over 52 characters, because the controller
4+
# names each Job it starts <name>-<timestamp>, while a release name may take 53.
5+
# Cutting the release name alone would give two long names one CronJob, so a
6+
# name that does not fit is the release name cut to 38 characters, a hash of
7+
# the full release name and -backup.
8+
9+
templates:
10+
- templates/backup-cronjob.yaml
11+
- templates/backup-script.yaml
12+
- templates/backup-secret.yaml
13+
14+
set:
15+
_cluster: {}
16+
backup:
17+
enabled: true
18+
schedule: "0 2 * * *"
19+
20+
tests:
21+
- it: keeps <release>-backup for a short release name
22+
release:
23+
name: ch-test
24+
namespace: tenant-test
25+
documentSelector:
26+
path: kind
27+
value: CronJob
28+
asserts:
29+
- equal:
30+
path: metadata.name
31+
value: ch-test-backup
32+
33+
- it: keeps the name when it lands exactly on 52 characters
34+
release:
35+
name: clickhouse-abcdefghijklmnopqrstuvwxyz01234567
36+
namespace: tenant-test
37+
documentSelector:
38+
path: kind
39+
value: CronJob
40+
asserts:
41+
- equal:
42+
path: metadata.name
43+
value: clickhouse-abcdefghijklmnopqrstuvwxyz01234567-backup
44+
45+
- it: shortens the name one character past the budget
46+
release:
47+
name: clickhouse-abcdefghijklmnopqrstuvwxyz012345678
48+
namespace: tenant-test
49+
documentSelector:
50+
path: kind
51+
value: CronJob
52+
asserts:
53+
- equal:
54+
path: metadata.name
55+
value: clickhouse-abcdefghijklmnopqrstuvwxyz0-bec95e-backup
56+
57+
- it: fits a 53-character release name into 52 characters
58+
release:
59+
name: clickhouse-abcdefghijklmnopqrstuvwxyz0123456789abcdef
60+
namespace: tenant-test
61+
documentSelector:
62+
path: kind
63+
value: CronJob
64+
asserts:
65+
- equal:
66+
path: metadata.name
67+
value: clickhouse-abcdefghijklmnopqrstuvwxyz0-3e8cf9-backup
68+
- matchRegex:
69+
path: metadata.name
70+
pattern: ^[a-z0-9]([-a-z0-9]{0,50}[a-z0-9])?$
71+
72+
- it: gives a release name differing only in its last character another CronJob
73+
release:
74+
name: clickhouse-abcdefghijklmnopqrstuvwxyz0123456789abcdex
75+
namespace: tenant-test
76+
documentSelector:
77+
path: kind
78+
value: CronJob
79+
asserts:
80+
- equal:
81+
path: metadata.name
82+
value: clickhouse-abcdefghijklmnopqrstuvwxyz0-bd3283-backup
83+
84+
- it: does not end the cut release name with a dash
85+
release:
86+
name: clickhouse-abcdefghijklmnopqrstuvwxyz-0123456789
87+
namespace: tenant-test
88+
documentSelector:
89+
path: kind
90+
value: CronJob
91+
asserts:
92+
- equal:
93+
path: metadata.name
94+
value: clickhouse-abcdefghijklmnopqrstuvwxyz-07ba9e-backup

‎packages/apps/kafka/templates/migration-hook.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ subjects:
7979
apiVersion: batch/v1
8080
kind: Job
8181
metadata:
82-
name: {{ .Release.Name }}-kafka-migration
82+
name: {{ printf "%s-kafka-migration" .Release.Name | trunc 63 | trimSuffix "-" }}
8383
annotations:
8484
helm.sh/hook: pre-install,pre-upgrade
8585
helm.sh/hook-weight: "2"
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
suite: kafka migration Job name budget
2+
3+
# The API server copies a Job's name into the job-name label of its pods, and a
4+
# label value takes 63 characters at most. The Kafka name cap leaves a release
5+
# name of up to 50 characters, and the migration hook runs on install, so an
6+
# overlong Job name would fail every install of the longest names. The
7+
# ServiceAccount, Role and RoleBinding have no such limit and keep the full
8+
# name, so the ones an earlier attempt left behind are replaced, not orphaned.
9+
10+
templates:
11+
- templates/migration-hook.yaml
12+
13+
tests:
14+
- it: keeps <release>-kafka-migration for a short release name
15+
release:
16+
name: test-kafka
17+
namespace: tenant-test
18+
documentSelector:
19+
path: kind
20+
value: Job
21+
asserts:
22+
- equal:
23+
path: metadata.name
24+
value: test-kafka-kafka-migration
25+
26+
- it: keeps the name when it lands exactly on 63 characters
27+
release:
28+
name: kafka-abcdefghijklmnopqrstuvwxyz0123456789abcde
29+
namespace: tenant-test
30+
documentSelector:
31+
path: kind
32+
value: Job
33+
asserts:
34+
- equal:
35+
path: metadata.name
36+
value: kafka-abcdefghijklmnopqrstuvwxyz0123456789abcde-kafka-migration
37+
38+
- it: cuts the name to 63 characters for the longest release name the cap admits
39+
release:
40+
name: kafka-abcdefghijklmnopqrstuvwxyz0123456789abcdefgh
41+
namespace: tenant-test
42+
documentSelector:
43+
path: kind
44+
value: Job
45+
asserts:
46+
- equal:
47+
path: metadata.name
48+
value: kafka-abcdefghijklmnopqrstuvwxyz0123456789abcdefgh-kafka-migrat
49+
- matchRegex:
50+
path: metadata.name
51+
pattern: ^[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?$
52+
- equal:
53+
path: spec.template.spec.serviceAccountName
54+
value: kafka-abcdefghijklmnopqrstuvwxyz0123456789abcdefgh-kafka-migration
55+
56+
- it: leaves the ServiceAccount on the full name for the longest release name
57+
release:
58+
name: kafka-abcdefghijklmnopqrstuvwxyz0123456789abcdefgh
59+
namespace: tenant-test
60+
documentSelector:
61+
path: kind
62+
value: ServiceAccount
63+
asserts:
64+
- equal:
65+
path: metadata.name
66+
value: kafka-abcdefghijklmnopqrstuvwxyz0123456789abcdefgh-kafka-migration

‎packages/apps/mariadb/templates/backup-cronjob.yaml‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
{{- if .Values.backup.enabled }}
22
{{ $image := .Files.Get "images/backup.json" | fromJson }}
3+
{{- /* The CronJob controller starts each Job as <name>-<timestamp>, so the API
4+
refuses a CronJob name over 52 characters. Cutting the release name to
5+
fit could give two releases one CronJob, so a name that does not fit
6+
keeps a hash of the full release name instead. */}}
7+
{{- $cronJobName := printf "%s-backup" .Release.Name }}
8+
{{- if gt (len $cronJobName) 52 }}
9+
{{- $cronJobName = printf "%s-%s-backup" (.Release.Name | trunc 38 | trimSuffix "-") (.Release.Name | sha256sum | trunc 6) }}
10+
{{- end }}
311

412
apiVersion: batch/v1
513
kind: CronJob
614
metadata:
7-
name: {{ .Release.Name }}-backup
15+
name: {{ $cronJobName }}
816
spec:
917
schedule: "{{ .Values.backup.schedule }}"
1018
concurrencyPolicy: Forbid
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
suite: mariadb backup CronJob name budget
2+
3+
# The API refuses a CronJob name over 52 characters, because the controller
4+
# names each Job it starts <name>-<timestamp>, while a release name may take 53.
5+
# Cutting the release name alone would give two long names one CronJob, so a
6+
# name that does not fit is the release name cut to 38 characters, a hash of
7+
# the full release name and -backup.
8+
9+
templates:
10+
- templates/backup-cronjob.yaml
11+
- templates/backup-script.yaml
12+
- templates/backup-secret.yaml
13+
14+
set:
15+
backup:
16+
enabled: true
17+
18+
tests:
19+
- it: keeps <release>-backup for a short release name
20+
release:
21+
name: mariadb-test
22+
namespace: tenant-test
23+
documentSelector:
24+
path: kind
25+
value: CronJob
26+
asserts:
27+
- equal:
28+
path: metadata.name
29+
value: mariadb-test-backup
30+
31+
- it: keeps the name when it lands exactly on 52 characters
32+
release:
33+
name: mariadb-abcdefghijklmnopqrstuvwxyz0123456789a
34+
namespace: tenant-test
35+
documentSelector:
36+
path: kind
37+
value: CronJob
38+
asserts:
39+
- equal:
40+
path: metadata.name
41+
value: mariadb-abcdefghijklmnopqrstuvwxyz0123456789a-backup
42+
43+
- it: shortens the name one character past the budget
44+
release:
45+
name: mariadb-abcdefghijklmnopqrstuvwxyz0123456789ab
46+
namespace: tenant-test
47+
documentSelector:
48+
path: kind
49+
value: CronJob
50+
asserts:
51+
- equal:
52+
path: metadata.name
53+
value: mariadb-abcdefghijklmnopqrstuvwxyz0123-57a159-backup
54+
55+
- it: fits a 53-character release name into 52 characters
56+
release:
57+
name: mariadb-abcdefghijklmnopqrstuvwxyz0123456789abcdefghi
58+
namespace: tenant-test
59+
documentSelector:
60+
path: kind
61+
value: CronJob
62+
asserts:
63+
- equal:
64+
path: metadata.name
65+
value: mariadb-abcdefghijklmnopqrstuvwxyz0123-b5132c-backup
66+
- matchRegex:
67+
path: metadata.name
68+
pattern: ^[a-z0-9]([-a-z0-9]{0,50}[a-z0-9])?$
69+
70+
- it: gives a release name differing only in its last character another CronJob
71+
release:
72+
name: mariadb-abcdefghijklmnopqrstuvwxyz0123456789abcdefghx
73+
namespace: tenant-test
74+
documentSelector:
75+
path: kind
76+
value: CronJob
77+
asserts:
78+
- equal:
79+
path: metadata.name
80+
value: mariadb-abcdefghijklmnopqrstuvwxyz0123-a49389-backup
81+
82+
- it: does not end the cut release name with a dash
83+
release:
84+
name: mariadb-abcdefghijklmnopqrstuvwxyz012-3456789abc
85+
namespace: tenant-test
86+
documentSelector:
87+
path: kind
88+
value: CronJob
89+
asserts:
90+
- equal:
91+
path: metadata.name
92+
value: mariadb-abcdefghijklmnopqrstuvwxyz012-2985e6-backup

0 commit comments

Comments
 (0)