Skip to content

Commit aadaa5d

Browse files
authored
Merge pull request kubernetes#90172 from nak3/add-IsDNS1123Label
Add DNS1123Label validation to IsFullyQualifiedDomainName() func
2 parents 9238fb1 + 18856da commit aadaa5d

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ func IsFullyQualifiedDomainName(fldPath *field.Path, name string) field.ErrorLis
106106
if len(strings.Split(name, ".")) < 2 {
107107
return append(allErrors, field.Invalid(fldPath, name, "should be a domain with at least two segments separated by dots"))
108108
}
109+
for _, label := range strings.Split(name, ".") {
110+
if errs := IsDNS1123Label(label); len(errs) > 0 {
111+
return append(allErrors, field.Invalid(fldPath, label, strings.Join(errs, ",")))
112+
}
113+
}
109114
return allErrors
110115
}
111116

staging/src/k8s.io/apimachinery/pkg/util/validation/validation_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,8 @@ func TestIsFullyQualifiedDomainName(t *testing.T) {
557557
"bbc.co.uk",
558558
"10.0.0.1", // DNS labels can start with numbers and there is no requirement for letters.
559559
"hyphens-are-good.k8s.io",
560-
strings.Repeat("a", 246) + ".k8s.io",
560+
strings.Repeat("a", 63) + ".k8s.io",
561+
strings.Repeat("a", 63) + "." + strings.Repeat("b", 63) + "." + strings.Repeat("c", 63) + "." + strings.Repeat("d", 54) + ".k8s.io",
561562
}
562563
for _, val := range goodValues {
563564
if err := IsFullyQualifiedDomainName(field.NewPath(""), val).ToAggregate(); err != nil {
@@ -579,7 +580,8 @@ func TestIsFullyQualifiedDomainName(t *testing.T) {
579580
"underscores_are_bad.k8s.io",
580581
581582
"http://foo.example.com",
582-
strings.Repeat("a", 247) + ".k8s.io",
583+
strings.Repeat("a", 64) + ".k8s.io",
584+
strings.Repeat("a", 63) + "." + strings.Repeat("b", 63) + "." + strings.Repeat("c", 63) + "." + strings.Repeat("d", 55) + ".k8s.io",
583585
}
584586
for _, val := range badValues {
585587
if err := IsFullyQualifiedDomainName(field.NewPath(""), val).ToAggregate(); err == nil {

0 commit comments

Comments
 (0)