Skip to content

Commit c493dd6

Browse files
authored
fix repo name (#2420)
* fix repo name * use general sanitzation func for all labels
1 parent 2eb4264 commit c493dd6

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

backend/service_clients/k8s.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"k8s.io/client-go/rest"
1717
)
1818

19+
20+
1921
func newInClusterClient() (*kubernetes.Clientset, error) {
2022
config, err := rest.InClusterConfig()
2123
if err != nil {
@@ -121,7 +123,7 @@ func (k K8sJobClient) triggerJob(ctx context.Context, opt JobOptions) (*Backgrou
121123
"app.kubernetes.io/managed-by": "digger-jobs",
122124
}
123125
for k, v := range opt.Labels {
124-
labels[k] = v
126+
labels[k] = sanitizeLabel(v)
125127
}
126128

127129
job := &batchv1.Job{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package service_clients
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
)
7+
8+
// Allowed: (([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?
9+
// This means: optional string that starts and ends with alphanumeric,
10+
// and may contain -, _, . in between.
11+
// this is because k8s does not allow labels that do not match this patter
12+
var allowedPattern = regexp.MustCompile(`^[A-Za-z0-9]([-A-Za-z0-9_.]*[A-Za-z0-9])?$`)
13+
var cleanupPattern = regexp.MustCompile(`[^A-Za-z0-9_.-]+`)
14+
15+
16+
func sanitizeLabel(input string) string {
17+
// Remove all disallowed characters
18+
cleaned := cleanupPattern.ReplaceAllString(input, "")
19+
// Trim leading and trailing non-alphanumerics
20+
cleaned = strings.Trim(cleaned, "-_.")
21+
return cleaned
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package service_clients
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSanitizeLabel(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
input string
11+
expected string
12+
}{
13+
// Basic alphanumeric
14+
{"SimpleAlphaNum", "abc123", "abc123"},
15+
// Remove illegal characters
16+
{"RemoveIllegalChars", "abc!@#123", "abc123"},
17+
// Trim leading/trailing dots/dashes/underscores
18+
{"TrimLeadingTrailing", "-._abc123-_.", "abc123"},
19+
// Keep valid internal characters
20+
{"KeepInternalSpecials", "ab-c_d.e", "ab-c_d.e"},
21+
// Collapse only removes invalid chars, not valid ones
22+
{"MixedValidInvalid", "a$bc.._123", "abc.._123"},
23+
// Entirely invalid string
24+
{"AllInvalid", "!@#$%^&*", ""},
25+
// Leading dot only
26+
{"LeadingDot", ".abc", "abc"},
27+
// Trailing underscore only
28+
{"TrailingUnderscore", "abc_", "abc"},
29+
// Consecutive invalids
30+
{"ConsecutiveInvalids", "a@@b##c", "abc"},
31+
// Empty input
32+
{"Empty", "", ""},
33+
}
34+
35+
for _, tt := range tests {
36+
t.Run(tt.name, func(t *testing.T) {
37+
got := sanitizeLabel(tt.input)
38+
if got != tt.expected {
39+
t.Errorf("sanitizeLabel(%q) = %q; want %q", tt.input, got, tt.expected)
40+
}
41+
})
42+
}
43+
}

0 commit comments

Comments
 (0)