-
Notifications
You must be signed in to change notification settings - Fork 128
fix: label values sanitization and normalization #2724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+189
β15
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ff4697a
fix: label values when dot are present
filariow 447b6c7
add sanification of the input and improve logic
filariow c95a1d7
fix: move from RFC1123 to LabelValue
filariow a1da962
fix: linter complaints
filariow 3c0e429
fix: remove unneeded check
filariow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,105 @@ | ||
| package formatting | ||
|
|
||
| import ( | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "k8s.io/apimachinery/pkg/util/validation" | ||
| ) | ||
|
|
||
| // CleanValueKubernetes conform a string to kubernetes naming convention | ||
| var ( | ||
| managedSpecialCharsMap = map[rune]string{ | ||
| ':': "-", | ||
| '/': "-", | ||
| ' ': "_", | ||
| '[': "__", | ||
| ']': "__", | ||
| } | ||
| allowedSpecialCharsLabelValue = ".-_" | ||
| allowedSpecialCharsLabelValueRunes = []rune(allowedSpecialCharsLabelValue) | ||
|
|
||
| // Build the replacer starting from the managedSpecialCharsMap. | ||
| managedSpecialCharsInLabelValueReplacer = strings.NewReplacer(pairs(managedSpecialCharsMap)...) | ||
| ) | ||
|
|
||
| // CleanValueKubernetes conforms a string to kubernetes naming convention | ||
| // see https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names | ||
| // rules are: | ||
| // β’ contain at most 63 characters | ||
| // β’ contain only lowercase alphanumeric characters or '-' | ||
| // β’ contain only alphanumeric characters or '-', '.', and '_' | ||
| // β’ start with an alphanumeric character | ||
| // β’ end with an alphanumeric character. | ||
| func CleanValueKubernetes(s string) string { | ||
| if len(s) >= 63 { | ||
| // keep the last 62 characters | ||
| s = s[len(s)-62:] | ||
| // cut short if the string is already a valid label | ||
| if len(validation.IsValidLabelValue(s)) == 0 { | ||
| return s | ||
| } | ||
|
|
||
| // sanitize the input | ||
| sanitized := sanitizeLabelValue(s) | ||
|
|
||
| // replace the managed special symbols | ||
| replaced := managedSpecialCharsInLabelValueReplacer.Replace(sanitized) | ||
|
|
||
| // trim unwanted chars | ||
| trimmed := strings.Trim(replaced, allowedSpecialCharsLabelValue) | ||
|
|
||
| // cut to max length | ||
| cut := cutToLabelValueMaxLength(trimmed) | ||
|
|
||
| // trim left again to ensure no invalid values | ||
| // are at the edge after the cut | ||
| return strings.TrimLeft(cut, allowedSpecialCharsLabelValue) | ||
| } | ||
|
|
||
| // cutToLabelValueMaxLength cuts the provided string to the maximum length allowed | ||
| // for a kubernetes Label (63 chars). | ||
| func cutToLabelValueMaxLength(s string) string { | ||
| if len(s) > validation.LabelValueMaxLength { | ||
| return s[len(s)-(validation.LabelValueMaxLength):] | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| // sanitizeLabelValue removes all unmanaged characters from the input string. | ||
| func sanitizeLabelValue(s string) string { | ||
| b := strings.Builder{} | ||
| for _, r := range s { | ||
| if isSafe(r) { | ||
| b.WriteRune(r) | ||
| } | ||
| } | ||
| return b.String() | ||
| } | ||
|
|
||
| // pairs extracts all the key-value pairs from a rune-string map. | ||
| func pairs(m map[rune]string) []string { | ||
| ss := make([]string, 0, len(m)*2) | ||
| for k, v := range m { | ||
| ss = append(ss, string(k), v) | ||
| } | ||
| return ss | ||
| } | ||
|
|
||
| // isSafe a helper to identify if a rune is safe to process or should be dropped. | ||
| func isSafe(r rune) bool { | ||
| return isAllowedSpecialCharLabelValue(r) || isAlphanumeric(r) || isManagedSpecialChar(r) | ||
| } | ||
|
|
||
| // isAlphanumeric returns true if the rune is an alphanumeric value. | ||
| func isAlphanumeric(r rune) bool { | ||
| return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') | ||
| } | ||
|
|
||
| // isAllowedSpecialCharLabelValue returns true if the rune is an allowed char as per | ||
| // Kubernetes Label Values. | ||
| func isAllowedSpecialCharLabelValue(r rune) bool { | ||
| return slices.Contains(allowedSpecialCharsLabelValueRunes, r) | ||
| } | ||
|
|
||
| replasoeur := strings.NewReplacer(":", "-", "/", "-", " ", "_", "[", "__", "]", "__") | ||
| s = strings.TrimRight(s, " -_[]") | ||
| s = strings.TrimLeft(s, " -_[]") | ||
| replaced := replasoeur.Replace(s) | ||
| return strings.TrimSpace(replaced) | ||
| // isManagedSpecialChar returns true if the rune is a managed special char. | ||
| // The normalization of the value will replace it with its counterpart. | ||
| func isManagedSpecialChar(r rune) bool { | ||
| _, ok := managedSpecialCharsMap[r] | ||
| return ok | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.