--- title: "ADR 0038: Label values are sanitized in a fixed order, trimming twice" --- * Status: accepted * Code: [`pkg/formatting/k8labels.go`](https://github.com/tektoncd/pipelines-as-code/blob/main/pkg/formatting/k8labels.go) ## Context Kubernetes label values must be at most 63 characters, empty or beginning and ending with an alphanumeric character, and may contain `-`, `_`, `.` and alphanumerics in between (`validation.IsValidLabelValue`). PAC derives label values from arbitrary strings such as branch names and repository names. ## Decision `CleanValueKubernetes` returns the input untouched when `validation.IsValidLabelValue` reports no errors. Otherwise it applies, in order: 1. `sanitizeLabelValue` drops characters that are neither alphanumeric, allowed special, nor in the managed special character map. 2. A `strings.Replacer` built once from `managedSpecialCharsMap` (for example `[` and `]` become `__`) replaces the managed symbols with their counterparts. 3. `strings.Trim` removes `.`, `-` and `_` from both ends. 4. `cutToLabelValueMaxLength` keeps the last 63 characters (the tail, not the head). 5. `strings.TrimLeft` removes the same characters from the start again. ## Consequences * Truncation can expose a special character at the start of the kept tail, so the second left trim is required. Removing it would produce invalid values for long inputs. * Truncation keeps the end of the string, so the distinguishing suffix of long names survives.