Skip to content

Commit 679e0d5

Browse files
yt2985k8s-publishing-bot
authored andcommitted
Promote PodCertificateRequests to v1beta1
Kubernetes-commit: 59e075e8d35dbc16c5db7c302f3836f3f9a144f8
1 parent 05faff5 commit 679e0d5

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

pkg/util/validation/validation.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,31 @@ func IsDomainPrefixedPath(fldPath *field.Path, dpPath string) field.ErrorList {
117117
return allErrs
118118
}
119119

120+
// IsDomainPrefixedKey checks if the given key string is a domain-prefixed key
121+
// (e.g. acme.io/foo). All characters before the first "/" must be a valid
122+
// subdomain as defined by RFC 1123. All characters trailing the first "/" must
123+
// be non-empty and match the regex ^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$.
124+
func IsDomainPrefixedKey(fldPath *field.Path, key string) field.ErrorList {
125+
var allErrs field.ErrorList
126+
if len(key) == 0 {
127+
return append(allErrs, field.Required(fldPath, ""))
128+
}
129+
for _, errMessages := range content.IsLabelKey(key) {
130+
allErrs = append(allErrs, field.Invalid(fldPath, key, errMessages))
131+
}
132+
133+
if len(allErrs) > 0 {
134+
return allErrs
135+
}
136+
137+
segments := strings.Split(key, "/")
138+
if len(segments) != 2 {
139+
return append(allErrs, field.Invalid(fldPath, key, "must be a domain-prefixed key (such as \"acme.io/foo\")"))
140+
}
141+
142+
return allErrs
143+
}
144+
120145
// LabelValueMaxLength is a label's max length
121146
// Deprecated: Use k8s.io/apimachinery/pkg/api/validate/content.LabelValueMaxLength instead.
122147
const LabelValueMaxLength int = content.LabelValueMaxLength

pkg/util/validation/validation_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,3 +694,71 @@ func TestIsDNS1123SubdomainWithUnderscore(t *testing.T) {
694694
})
695695
}
696696
}
697+
698+
func TestIsDomainPrefixedKey(t *testing.T) {
699+
tests := []struct {
700+
name string
701+
value string
702+
expected field.ErrorList
703+
}{
704+
{
705+
name: "valid domain-prefixed key 1",
706+
value: "example.com/foo",
707+
expected: nil,
708+
},
709+
{
710+
name: "valid domain-prefixed key 2",
711+
value: "example/com",
712+
expected: nil,
713+
},
714+
{
715+
name: "invalid key 1",
716+
value: "example",
717+
expected: field.ErrorList{field.Invalid(field.NewPath("test"), "example", "must be a domain-prefixed key (such as \"acme.io/foo\")")},
718+
},
719+
{
720+
name: "invalid key 2",
721+
value: "example/foo/bar",
722+
expected: field.ErrorList{field.Invalid(field.NewPath("test"), "example/foo/bar", "a valid label key must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]') with an optional DNS subdomain prefix and '/' (e.g. 'example.com/MyName')")},
723+
},
724+
{
725+
name: "invalid key 3",
726+
value: "/example/foo",
727+
expected: field.ErrorList{field.Invalid(field.NewPath("test"), "/example/foo", "a valid label key must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]') with an optional DNS subdomain prefix and '/' (e.g. 'example.com/MyName')")},
728+
},
729+
{
730+
name: "invalid key 4",
731+
value: "",
732+
expected: field.ErrorList{field.Required(field.NewPath("test"), "")},
733+
},
734+
{
735+
name: "invalid key 5",
736+
value: "example.com/_e",
737+
expected: field.ErrorList{field.Invalid(field.NewPath("test"), "example.com/_e", "name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')")},
738+
},
739+
{
740+
name: "invalid key 6",
741+
value: "example.com/e_",
742+
expected: field.ErrorList{field.Invalid(field.NewPath("test"), "example.com/e_", "name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')")},
743+
},
744+
{
745+
name: "invalid key 7",
746+
value: "example.com/e?",
747+
expected: field.ErrorList{field.Invalid(field.NewPath("test"), "example.com/e?", "name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')")},
748+
},
749+
{
750+
name: "invalid key 8",
751+
value: "example.com/e_$",
752+
expected: field.ErrorList{field.Invalid(field.NewPath("test"), "example.com/e_$", "name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')")},
753+
},
754+
}
755+
756+
for _, tt := range tests {
757+
t.Run(tt.name, func(t *testing.T) {
758+
result := IsDomainPrefixedKey(field.NewPath("test"), tt.value)
759+
if !reflect.DeepEqual(result, tt.expected) {
760+
t.Errorf("IsDomainPrefixedKey(%q) = %v; want %v", tt.value, result, tt.expected)
761+
}
762+
})
763+
}
764+
}

0 commit comments

Comments
 (0)