Skip to content
Snippets Groups Projects

Add Kubernetes pod label sanitization

Merged Theodor van Nahl requested to merge (removed):Adding_kubernetes_label_sanitation into main
1 unresolved thread
Compare and Show latest version
2 files
+ 45
10
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -349,13 +349,27 @@ func buildCapabilities(enabled map[string]bool) *api.Capabilities {
// Sanitize labels to match Kubernetes restrictions from https://kubernetes.io/
// /docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set
func sanitizeLabel(value string) string {
mapFn := func(r rune) rune {
if r >= 'a' && r <= 'z' ||
r >= 'A' && r <= 'Z' ||
r >= '0' && r <= '9' ||
r == '-' || r == '_' || r == '.' {
return r
}
return '_'
}
// only alphanumerics, dashes (-), underscores (_), dots (.) are valid
value = strings.Map(mapFn, value)
// must start/end with alphanumerics only
value = strings.Trim(value, "-_.")
// length must be <= 63 characters
if len(value) > 63 {
value = value[:63]
}
re := regexp.MustCompile(`[^A-Za-z0-9-._ ]`)
standardized := re.ReplaceAllLiteralString(value, "")
final := strings.ReplaceAll(standardized, " ", "-")
return final
// trim again if required after shortening
return strings.Trim(value, "-_.")
}
Loading