Skip to content

Commit fafa12f

Browse files
fix(chart): make the derived version label total over every OCI tag
Review finding on PR #7876: `image.tag` is an unconstrained string, but `buzz.imageVersionLabel` only stripped the digest prefix, truncated to 63, and trimmed at most one trailing `-`, `.`, and `_`. That is not a mapping onto the Kubernetes label grammar, and three chart-valid inputs produced a CronJob the API server rejects or mis-attributes. Reproduced at 5105892 by rendering the chart: - `image.tag=_foo` -> label `_foo`; a label must start alphanumeric. - a 67-byte tag ending `-_.` -> label `aaa…-`; the one-pass trim order (`-`, then `.`, then `_`) removes the `.` and the `_` and leaves the `-`. - two 67-byte tags sharing their first 63 bytes -> one identical label, so two builds report the same telemetry version. The image reference and `BUZZ_STORAGE_SNAPSHOT_CODE_SHA` were correct in all three; only the label was wrong. The helper is now total by construction, in three cases: a `sha256:<64 hex>` digest keeps 63 hex characters (unchanged, and two digests sharing a 252-bit prefix do not occur); a revision that is already a valid label value is emitted byte for byte, so `1.2.3-rc.4` stays readable; anything else becomes a sanitized prefix of at most 52 bytes plus 10 hex characters of the revision's SHA-256. The hash restores what truncation destroys and always ends the label on a hex digit, which removes the trailing-delimiter trimming entirely rather than adding another pass to it. No new value, flag, or configurable concept, and the existing image-tag contract is not narrowed: nothing is rejected that the chart used to accept. `BUZZ_STORAGE_SNAPSHOT_CODE_SHA` is never sanitized, so the snapshot row keeps the exact revision while the label stays a valid, distinct telemetry key. Co-Authored-By: Claude Opus 5 <[email protected]> Signed-off-by: tornquist <[email protected]>
1 parent 5105892 commit fafa12f

4 files changed

Lines changed: 190 additions & 17 deletions

File tree

‎deploy/charts/buzz/README.md‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,24 @@ describe the deployment rather than the image — passes through unchanged.
345345
Remove any wrapper-maintained `tags.datadoghq.com/version` pin when upgrading;
346346
leaving it in place is harmless but dead.
347347

348-
Because a label value may not contain `:` and is capped at 63 characters, a
349-
digest pin renders as the digest hex without its `sha256:` prefix, truncated to
350-
63 characters — a unique prefix of the full digest that the same Pod reports in
351-
`BUZZ_STORAGE_SNAPSHOT_CODE_SHA`. See `docs/deployment-identity.md`.
348+
A Kubernetes label value is capped at 63 bytes, must begin and end with an
349+
alphanumeric, and may otherwise contain only `[-._a-zA-Z0-9]` — a far narrower
350+
grammar than an OCI tag. `image.tag` is deliberately left unconstrained, so the
351+
label is derived through a total mapping rather than a check that could reject
352+
a tag the chart used to accept:
353+
354+
| Revision | Label |
355+
|---|---|
356+
| `sha256:<64 hex>` digest | the hex without `sha256:`, first 63 characters |
357+
| already a valid label value | emitted byte for byte (e.g. `1.2.3-rc.4`) |
358+
| anything else | up to 52 sanitized bytes + `-` + 10 hex of the revision's SHA-256 |
359+
360+
The third case is what makes the mapping total: `_foo` renders
361+
`foo-583da8894a`, and two overlong tags sharing their first 63 bytes still
362+
render distinct versions. `BUZZ_STORAGE_SNAPSHOT_CODE_SHA` is never sanitized —
363+
it always carries the exact revision, so the snapshot row remains the precise
364+
record and the label remains the joinable telemetry key. See
365+
`docs/deployment-identity.md`.
352366

353367
## Device pairing relay
354368

‎deploy/charts/buzz/templates/_helpers.tpl‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,39 @@ app.kubernetes.io/component: relay
7171

7272
{{/*
7373
Kubernetes-label-safe rendering of "buzz.imageRevision", used for Datadog
74-
unified-service-tagging's version tag on chart-managed Pods.
75-
76-
The revision is either an image tag or a "sha256:<64 hex>" digest. A label
77-
value may not contain ":" and is capped at 63 characters, so the digest
78-
algorithm prefix is stripped and the result bounded. The label is therefore a
79-
prefix-comparable form of the exact revision the same Pod reports in
80-
BUZZ_STORAGE_SNAPSHOT_CODE_SHA — one identity, derived twice, never a second
74+
unified-service-tagging's version tag on chart-managed Pods. The label is
75+
always derived from the same revision the Pod reports verbatim in
76+
BUZZ_STORAGE_SNAPSHOT_CODE_SHA — one identity, rendered twice, never a second
8177
value an operator has to keep in sync.
78+
79+
A label value is at most 63 bytes, must begin and end with an alphanumeric,
80+
and may otherwise contain only [-._a-zA-Z0-9]. An OCI tag is far more
81+
permissive (image.tag is an unconstrained string), so the mapping is total by
82+
construction, in three cases:
83+
84+
1. A "sha256:<64 hex>" digest drops its algorithm prefix and keeps 63 hex
85+
characters. Unchanged from the digest behaviour this chart already had:
86+
hex is label-safe, and two digests sharing a 252-bit prefix do not occur.
87+
2. A revision that is already a valid label value is emitted byte for byte,
88+
so ordinary tags like 1.2.3-rc.4 stay readable and comparable.
89+
3. Anything else — a leading "_", a byte outside the label alphabet, or more
90+
than 63 bytes — becomes a sanitized prefix of at most 52 bytes plus a
91+
10-hex-character SHA-256 of the *exact* revision. The hash restores what
92+
truncation destroys: two long tags sharing a 63-byte prefix still render
93+
distinct versions, and the result always ends on a hex digit, so no
94+
trailing-delimiter trimming is needed.
8295
*/}}
8396
{{- define "buzz.imageVersionLabel" -}}
84-
{{- include "buzz.imageRevision" . | trimPrefix "sha256:" | trunc 63 | trimSuffix "-" | trimSuffix "." | trimSuffix "_" -}}
97+
{{- $revision := include "buzz.imageRevision" . -}}
98+
{{- if regexMatch "^sha256:[0-9a-f]{64}$" $revision -}}
99+
{{- $revision | trimPrefix "sha256:" | trunc 63 -}}
100+
{{- else if and (le (len $revision) 63) (regexMatch "^[a-zA-Z0-9]([-._a-zA-Z0-9]*[a-zA-Z0-9])?$" $revision) -}}
101+
{{- $revision -}}
102+
{{- else -}}
103+
{{- $sanitized := regexReplaceAll "[^-._a-zA-Z0-9]" $revision "-" -}}
104+
{{- $prefix := regexReplaceAll "^[-._]+" $sanitized "" | trunc 52 -}}
105+
{{- printf "%s-%s" $prefix (sha256sum $revision | trunc 10) | trimPrefix "-" -}}
106+
{{- end -}}
85107
{{- end -}}
86108

87109
{{/*

‎deploy/charts/buzz/tests/storage_accounting_test.yaml‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,132 @@ tests:
245245
name: BUZZ_STORAGE_SNAPSHOT_CODE_SHA
246246
value: sha256:1111111111111111111111111111111111111111111111111111111111111111
247247
template: templates/storage-accounting-cronjob.yaml
248+
# ── Label-safety of arbitrary OCI tags (PR #7876 review, P0) ───────────────
249+
# image.tag is an unconstrained string in values.schema.json, so any tag the
250+
# OCI grammar allows must still yield a valid Kubernetes label value. The
251+
# chart renders a bounded sanitized prefix plus a stable hash of the exact
252+
# revision whenever the revision is not already label-safe.
253+
- it: sanitizes a leading underscore that OCI allows but Kubernetes labels do not
254+
set:
255+
relayUrl: wss://buzz.example.com
256+
ownerPubkey: "0000000000000000000000000000000000000000000000000000000000000000"
257+
externalPostgresql.url: postgres://u:p@h:5432/d
258+
externalRedis.url: redis://h:6379
259+
s3.endpoint: https://s3.example.com
260+
s3.accessKey: test
261+
s3.secretKey: test
262+
image.tag: "_foo"
263+
storageAccounting:
264+
enabled: true
265+
asserts:
266+
# Leading '_' is a valid OCI tag byte but an invalid first label byte.
267+
- equal:
268+
path: spec.jobTemplate.spec.template.metadata.labels["tags.datadoghq.com/version"]
269+
value: "foo-583da8894a"
270+
template: templates/storage-accounting-cronjob.yaml
271+
# The rendered label must satisfy the Kubernetes label-value grammar:
272+
# <=63 bytes, alphanumeric first and last byte, [-._a-zA-Z0-9] inside.
273+
- matchRegex:
274+
path: spec.jobTemplate.spec.template.metadata.labels["tags.datadoghq.com/version"]
275+
pattern: ^[a-zA-Z0-9]([-._a-zA-Z0-9]{0,61}[a-zA-Z0-9])?$
276+
template: templates/storage-accounting-cronjob.yaml
277+
# The exact image revision still reaches the snapshot row unmodified.
278+
- contains:
279+
path: spec.jobTemplate.spec.template.spec.containers[0].env
280+
content:
281+
name: BUZZ_STORAGE_SNAPSHOT_CODE_SHA
282+
value: "_foo"
283+
template: templates/storage-accounting-cronjob.yaml
284+
- it: keeps the label valid when truncation would land on mixed trailing delimiters
285+
set:
286+
relayUrl: wss://buzz.example.com
287+
ownerPubkey: "0000000000000000000000000000000000000000000000000000000000000000"
288+
externalPostgresql.url: postgres://u:p@h:5432/d
289+
externalRedis.url: redis://h:6379
290+
s3.endpoint: https://s3.example.com
291+
s3.accessKey: test
292+
s3.secretKey: test
293+
image.tag: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-_.tail"
294+
storageAccounting:
295+
enabled: true
296+
asserts:
297+
# A one-pass '-'/'.'/'_' trim leaves a trailing '-' for a tag ending '-_.'.
298+
- equal:
299+
path: spec.jobTemplate.spec.template.metadata.labels["tags.datadoghq.com/version"]
300+
value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-e4fb46fc85"
301+
template: templates/storage-accounting-cronjob.yaml
302+
# The rendered label must satisfy the Kubernetes label-value grammar:
303+
# <=63 bytes, alphanumeric first and last byte, [-._a-zA-Z0-9] inside.
304+
- matchRegex:
305+
path: spec.jobTemplate.spec.template.metadata.labels["tags.datadoghq.com/version"]
306+
pattern: ^[a-zA-Z0-9]([-._a-zA-Z0-9]{0,61}[a-zA-Z0-9])?$
307+
template: templates/storage-accounting-cronjob.yaml
308+
# The exact image revision still reaches the snapshot row unmodified.
309+
- contains:
310+
path: spec.jobTemplate.spec.template.spec.containers[0].env
311+
content:
312+
name: BUZZ_STORAGE_SNAPSHOT_CODE_SHA
313+
value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-_.tail"
314+
template: templates/storage-accounting-cronjob.yaml
315+
- it: distinguishes the first of two >63-character tags sharing a 63-byte prefix
316+
set:
317+
relayUrl: wss://buzz.example.com
318+
ownerPubkey: "0000000000000000000000000000000000000000000000000000000000000000"
319+
externalPostgresql.url: postgres://u:p@h:5432/d
320+
externalRedis.url: redis://h:6379
321+
s3.endpoint: https://s3.example.com
322+
s3.accessKey: test
323+
s3.secretKey: test
324+
image.tag: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-one"
325+
storageAccounting:
326+
enabled: true
327+
asserts:
328+
# Plain truncation collapses both tags to the same telemetry version.
329+
- equal:
330+
path: spec.jobTemplate.spec.template.metadata.labels["tags.datadoghq.com/version"]
331+
value: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-2aad1d5b65"
332+
template: templates/storage-accounting-cronjob.yaml
333+
# The rendered label must satisfy the Kubernetes label-value grammar:
334+
# <=63 bytes, alphanumeric first and last byte, [-._a-zA-Z0-9] inside.
335+
- matchRegex:
336+
path: spec.jobTemplate.spec.template.metadata.labels["tags.datadoghq.com/version"]
337+
pattern: ^[a-zA-Z0-9]([-._a-zA-Z0-9]{0,61}[a-zA-Z0-9])?$
338+
template: templates/storage-accounting-cronjob.yaml
339+
# The exact image revision still reaches the snapshot row unmodified.
340+
- contains:
341+
path: spec.jobTemplate.spec.template.spec.containers[0].env
342+
content:
343+
name: BUZZ_STORAGE_SNAPSHOT_CODE_SHA
344+
value: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-one"
345+
template: templates/storage-accounting-cronjob.yaml
346+
- it: distinguishes the second of two >63-character tags sharing a 63-byte prefix
347+
set:
348+
relayUrl: wss://buzz.example.com
349+
ownerPubkey: "0000000000000000000000000000000000000000000000000000000000000000"
350+
externalPostgresql.url: postgres://u:p@h:5432/d
351+
externalRedis.url: redis://h:6379
352+
s3.endpoint: https://s3.example.com
353+
s3.accessKey: test
354+
s3.secretKey: test
355+
image.tag: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-two"
356+
storageAccounting:
357+
enabled: true
358+
asserts:
359+
# Same 63-byte prefix as the previous case; the hash suffix keeps them apart.
360+
- equal:
361+
path: spec.jobTemplate.spec.template.metadata.labels["tags.datadoghq.com/version"]
362+
value: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-86dde532eb"
363+
template: templates/storage-accounting-cronjob.yaml
364+
# The rendered label must satisfy the Kubernetes label-value grammar:
365+
# <=63 bytes, alphanumeric first and last byte, [-._a-zA-Z0-9] inside.
366+
- matchRegex:
367+
path: spec.jobTemplate.spec.template.metadata.labels["tags.datadoghq.com/version"]
368+
pattern: ^[a-zA-Z0-9]([-._a-zA-Z0-9]{0,61}[a-zA-Z0-9])?$
369+
template: templates/storage-accounting-cronjob.yaml
370+
# The exact image revision still reaches the snapshot row unmodified.
371+
- contains:
372+
path: spec.jobTemplate.spec.template.spec.containers[0].env
373+
content:
374+
name: BUZZ_STORAGE_SNAPSHOT_CODE_SHA
375+
value: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-two"
376+
template: templates/storage-accounting-cronjob.yaml

‎docs/deployment-identity.md‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,16 @@ label are both derived from `image.digest` — or `image.tag`, or
7777
`Chart.AppVersion` — so a snapshot's telemetry version and its recorded version
7878
cannot drift apart.
7979

80-
Kubernetes label values may not contain `:` and are capped at 63 characters, so
81-
the label carries the digest hex without its `sha256:` prefix, truncated to 63
82-
characters. The environment variable keeps the exact revision. Any
83-
`tags.datadoghq.com/version` supplied through `storageAccounting.podLabels` is
84-
ignored; see the chart README's "Storage accounting worker" section.
80+
The environment variable always keeps the exact revision. The label cannot: a
81+
label value is capped at 63 bytes, must begin and end with an alphanumeric, and
82+
may otherwise contain only `[-._a-zA-Z0-9]`, while `image.tag` accepts any OCI
83+
tag. The chart therefore maps the revision onto that grammar deterministically —
84+
a digest keeps the first 63 characters of its hex, a revision that is already a
85+
valid label value is preserved byte for byte, and anything else (a leading `_`,
86+
a byte outside the label alphabet, more than 63 bytes) becomes a sanitized
87+
prefix of at most 52 bytes plus 10 hex characters of the revision's SHA-256.
88+
The hash suffix is what keeps two long tags sharing a 63-byte prefix from
89+
reporting the same version.
90+
91+
Any `tags.datadoghq.com/version` supplied through `storageAccounting.podLabels`
92+
is ignored; see the chart README's "Storage accounting worker" section.

0 commit comments

Comments
 (0)