Skip to content

Commit 73727bd

Browse files
committed
Organize k8s names formats into their own registry for ratcheted rollout, prefix with k8s-
1 parent b8eeb00 commit 73727bd

File tree

5 files changed

+185
-119
lines changed

5 files changed

+185
-119
lines changed

pkg/validation/strfmt/default.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -124,48 +124,6 @@ func IsEmail(str string) bool {
124124
return e == nil && addr.Address != ""
125125
}
126126

127-
const dns1123LabelFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
128-
129-
// DNS1123LabelMaxLength is a label's max length in DNS (RFC 1123)
130-
const DNS1123LabelMaxLength int = 63
131-
132-
var dns1123LabelRegexp = regexp.MustCompile("^" + dns1123LabelFmt + "$")
133-
134-
// IsDNS1123Label tests for a string that conforms to the definition of a label in
135-
// DNS (RFC 1123).
136-
func IsDNS1123Label(value string) bool {
137-
return len(value) <= DNS1123LabelMaxLength &&
138-
dns1123LabelRegexp.MatchString(value)
139-
}
140-
141-
const dns1123SubdomainFmt string = dns1123LabelFmt + "(\\." + dns1123LabelFmt + ")*"
142-
143-
// DNS1123SubdomainMaxLength is a subdomain's max length in DNS (RFC 1123)
144-
const DNS1123SubdomainMaxLength int = 253
145-
146-
var dns1123SubdomainRegexp = regexp.MustCompile("^" + dns1123SubdomainFmt + "$")
147-
148-
// IsDNS1123Subdomain tests for a string that conforms to the definition of a
149-
// subdomain in DNS (RFC 1123).
150-
func IsDNS1123Subdomain(value string) bool {
151-
return len(value) <= DNS1123SubdomainMaxLength &&
152-
dns1123SubdomainRegexp.MatchString(value)
153-
}
154-
155-
const dns1035LabelFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?"
156-
157-
// DNS1035LabelMaxLength is a label's max length in DNS (RFC 1035)
158-
const DNS1035LabelMaxLength int = 63
159-
160-
var dns1035LabelRegexp = regexp.MustCompile("^" + dns1035LabelFmt + "$")
161-
162-
// IsDNS1035Label tests for a string that conforms to the definition of a label in
163-
// DNS (RFC 1035).
164-
func IsDNS1035Label(value string) bool {
165-
return len(value) <= DNS1035LabelMaxLength &&
166-
dns1035LabelRegexp.MatchString(value)
167-
}
168-
169127
func init() {
170128
// register formats in the default registry:
171129
// - byte
@@ -247,15 +205,6 @@ func init() {
247205

248206
pw := Password("")
249207
Default.Add("password", &pw, func(_ string) bool { return true })
250-
251-
dns1123subdomain := DNS1123Subdomain("")
252-
Default.Add("dns1123subdomain", &dns1123subdomain, IsDNS1123Subdomain)
253-
254-
dns1123label := DNS1123Label("")
255-
Default.Add("dns1123label", &dns1123label, IsDNS1123Label)
256-
257-
dns1035label := DNS1035Label("")
258-
Default.Add("dns1035label", &dns1035label, IsDNS1035Label)
259208
}
260209

261210
// isIPv4 checks if the string is an IPv4 address, tolerating leading 0's for compatibility with go < 1.17.

pkg/validation/strfmt/default_test.go

Lines changed: 15 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -308,71 +308,6 @@ func TestFormatBase64(t *testing.T) {
308308
testInvalid(t, "byte", "ZWxpemFiZXRocG9zZXk") // missing pad char
309309
}
310310

311-
func TestFormatDNS1123Subdomain(t *testing.T) {
312-
goodValues := []string{
313-
"a", "ab", "abc", "a1", "a-1", "a--1--2--b",
314-
"0", "01", "012", "1a", "1-a", "1--a--b--2",
315-
"a.a", "ab.a", "abc.a", "a1.a", "a-1.a", "a--1--2--b.a",
316-
"a.1", "ab.1", "abc.1", "a1.1", "a-1.1", "a--1--2--b.1",
317-
"0.a", "01.a", "012.a", "1a.a", "1-a.a", "1--a--b--2",
318-
"0.1", "01.1", "012.1", "1a.1", "1-a.1", "1--a--b--2.1",
319-
"a.b.c.d.e", "aa.bb.cc.dd.ee", "1.2.3.4.5", "11.22.33.44.55",
320-
strings.Repeat("a", 253),
321-
}
322-
badValues := []string{
323-
"", "A", "ABC", "aBc", "A1", "A-1", "1-A",
324-
"-", "a-", "-a", "1-", "-1",
325-
"_", "a_", "_a", "a_b", "1_", "_1", "1_2",
326-
".", "a.", ".a", "a..b", "1.", ".1", "1..2",
327-
" ", "a ", " a", "a b", "1 ", " 1", "1 2",
328-
"A.a", "aB.a", "ab.A", "A1.a", "a1.A",
329-
"A.1", "aB.1", "A1.1", "1A.1",
330-
"0.A", "01.A", "012.A", "1A.a", "1a.A",
331-
"A.B.C.D.E", "AA.BB.CC.DD.EE", "a.B.c.d.e", "aa.bB.cc.dd.ee",
332-
"a@b", "a,b", "a_b", "a;b",
333-
"a:b", "a%b", "a?b", "a$b",
334-
strings.Repeat("a", 254),
335-
}
336-
v := DNS1123Subdomain("a")
337-
testStringFormat(t, &v, "dns1123subdomain", "a", goodValues, badValues)
338-
}
339-
340-
func TestIsDNS1123Label(t *testing.T) {
341-
goodValues := []string{
342-
"a", "ab", "abc", "a1", "a-1", "a--1--2--b",
343-
"0", "01", "012", "1a", "1-a", "1--a--b--2",
344-
strings.Repeat("a", 63),
345-
}
346-
badValues := []string{
347-
"", "A", "ABC", "aBc", "A1", "A-1", "1-A",
348-
"-", "a-", "-a", "1-", "-1",
349-
"_", "a_", "_a", "a_b", "1_", "_1", "1_2",
350-
".", "a.", ".a", "a.b", "1.", ".1", "1.2",
351-
" ", "a ", " a", "a b", "1 ", " 1", "1 2",
352-
strings.Repeat("a", 64),
353-
}
354-
v := DNS1123Label("a")
355-
testStringFormat(t, &v, "dns1123label", "a", goodValues, badValues)
356-
}
357-
358-
func TestIsDNS1035Label(t *testing.T) {
359-
goodValues := []string{
360-
"a", "ab", "abc", "a1", "a-1", "a--1--2--b",
361-
strings.Repeat("a", 63),
362-
}
363-
badValues := []string{
364-
"0", "01", "012", "1a", "1-a", "1--a--b--2",
365-
"", "A", "ABC", "aBc", "A1", "A-1", "1-A",
366-
"-", "a-", "-a", "1-", "-1",
367-
"_", "a_", "_a", "a_b", "1_", "_1", "1_2",
368-
".", "a.", ".a", "a.b", "1.", ".1", "1.2",
369-
" ", "a ", " a", "a b", "1 ", " 1", "1 2",
370-
strings.Repeat("a", 64),
371-
}
372-
v := DNS1035Label("a")
373-
testStringFormat(t, &v, "dns1035label", "a", goodValues, badValues)
374-
}
375-
376311
type testableFormat interface {
377312
encoding.TextMarshaler
378313
encoding.TextUnmarshaler
@@ -382,6 +317,10 @@ type testableFormat interface {
382317
}
383318

384319
func testStringFormat(t *testing.T, what testableFormat, format, with string, validSamples, invalidSamples []string) {
320+
testStringFormatWithRegistry(t, Default, what, format, with, validSamples, invalidSamples)
321+
}
322+
323+
func testStringFormatWithRegistry(t *testing.T, registry Registry, what testableFormat, format, with string, validSamples, invalidSamples []string) {
385324
// text encoding interface
386325
b := []byte(with)
387326
err := what.UnmarshalText(b)
@@ -413,22 +352,30 @@ func testStringFormat(t *testing.T, what testableFormat, format, with string, va
413352

414353
// validation with Registry
415354
for _, valid := range append(validSamples, with) {
416-
testValid(t, format, valid)
355+
testValidWithRegistry(t, registry, format, valid)
417356
}
418357

419358
for _, invalid := range invalidSamples {
420-
testInvalid(t, format, invalid)
359+
testInvalidWithRegistry(t, registry, format, invalid)
421360
}
422361
}
423362

424363
func testValid(t *testing.T, name, value string) {
425-
ok := Default.Validates(name, value)
364+
testValidWithRegistry(t, Default, name, value)
365+
}
366+
367+
func testValidWithRegistry(t *testing.T, registry Registry, name, value string) {
368+
ok := registry.Validates(name, value)
426369
if !ok {
427370
t.Errorf("expected %q of type %s to be valid", value, name)
428371
}
429372
}
430373

431374
func testInvalid(t *testing.T, name, value string) {
375+
testInvalidWithRegistry(t, Default, name, value)
376+
}
377+
378+
func testInvalidWithRegistry(t *testing.T, registry Registry, name, value string) {
432379
ok := Default.Validates(name, value)
433380
if ok {
434381
t.Errorf("expected %q of type %s to be invalid", value, name)

pkg/validation/strfmt/format.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"time"
2424

2525
"github.com/mitchellh/mapstructure"
26+
2627
"k8s.io/kube-openapi/pkg/validation/errors"
2728
)
2829

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2024 go-swagger maintainers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package strfmt
16+
17+
import (
18+
"regexp"
19+
)
20+
21+
// KubeStyleNames is the formats registry for Kubernetes style names.
22+
var KubeStyleNames = NewSeededFormats(nil, nil)
23+
24+
func init() {
25+
// register formats in the KubeStyleNames registry:
26+
// - k8s-dns1123subdomain
27+
// - k8s-dns1123label
28+
// - k8s-dns1035label
29+
dns1123subdomain := DNS1123Subdomain("")
30+
KubeStyleNames.Add("k8s-dns1123subdomain", &dns1123subdomain, IsDNS1123Subdomain)
31+
32+
dns1123label := DNS1123Label("")
33+
KubeStyleNames.Add("k8s-dns1123label", &dns1123label, IsDNS1123Label)
34+
35+
dns1035label := DNS1035Label("")
36+
KubeStyleNames.Add("k8s-dns1035label", &dns1035label, IsDNS1035Label)
37+
}
38+
39+
const dns1123LabelFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
40+
41+
// DNS1123LabelMaxLength is a label's max length in DNS (RFC 1123)
42+
const DNS1123LabelMaxLength int = 63
43+
44+
var dns1123LabelRegexp = regexp.MustCompile("^" + dns1123LabelFmt + "$")
45+
46+
// IsDNS1123Label tests for a string that almost conforms to the definition of a label in
47+
// DNS (RFC 1123), except that uppercase letters are not allowed.
48+
// xref: https://github.com/kubernetes/kubernetes/issues/71140
49+
func IsDNS1123Label(value string) bool {
50+
return len(value) <= DNS1123LabelMaxLength &&
51+
dns1123LabelRegexp.MatchString(value)
52+
}
53+
54+
const dns1123SubdomainFmt string = dns1123LabelFmt + "(\\." + dns1123LabelFmt + ")*"
55+
56+
// DNS1123SubdomainMaxLength is a subdomain's max length in DNS (RFC 1123)
57+
const DNS1123SubdomainMaxLength int = 253
58+
59+
var dns1123SubdomainRegexp = regexp.MustCompile("^" + dns1123SubdomainFmt + "$")
60+
61+
// IsDNS1123Subdomain tests for a string that almost conforms to the definition of a
62+
// subdomain in DNS (RFC 1123), except that uppercase letters are not allowed.
63+
// and there is no max length of limit of 63 for each of the dot separated DNS Labels
64+
// that make up the subdomain.
65+
// xref: https://github.com/kubernetes/kubernetes/issues/71140
66+
// xref: https://github.com/kubernetes/kubernetes/issues/79351
67+
func IsDNS1123Subdomain(value string) bool {
68+
return len(value) <= DNS1123SubdomainMaxLength &&
69+
dns1123SubdomainRegexp.MatchString(value)
70+
}
71+
72+
const dns1035LabelFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?"
73+
74+
// DNS1035LabelMaxLength is a label's max length in DNS (RFC 1035)
75+
const DNS1035LabelMaxLength int = 63
76+
77+
var dns1035LabelRegexp = regexp.MustCompile("^" + dns1035LabelFmt + "$")
78+
79+
// IsDNS1035Label tests for a string that almost conforms to the definition of a label in
80+
// DNS (RFC 1035), except that uppercase letters are not allowed.
81+
func IsDNS1035Label(value string) bool {
82+
return len(value) <= DNS1035LabelMaxLength &&
83+
dns1035LabelRegexp.MatchString(value)
84+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2024 go-swagger maintainers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package strfmt
16+
17+
import (
18+
"strings"
19+
"testing"
20+
)
21+
22+
func TestFormatDNS1123Subdomain(t *testing.T) {
23+
goodValues := []string{
24+
"a", "ab", "abc", "a1", "a-1", "a--1--2--b",
25+
"0", "01", "012", "1a", "1-a", "1--a--b--2",
26+
"a.a", "ab.a", "abc.a", "a1.a", "a-1.a", "a--1--2--b.a",
27+
"a.1", "ab.1", "abc.1", "a1.1", "a-1.1", "a--1--2--b.1",
28+
"0.a", "01.a", "012.a", "1a.a", "1-a.a", "1--a--b--2",
29+
"0.1", "01.1", "012.1", "1a.1", "1-a.1", "1--a--b--2.1",
30+
"a.b.c.d.e", "aa.bb.cc.dd.ee", "1.2.3.4.5", "11.22.33.44.55",
31+
strings.Repeat("a", 253),
32+
}
33+
badValues := []string{
34+
"", "A", "ABC", "aBc", "A1", "A-1", "1-A",
35+
"-", "a-", "-a", "1-", "-1",
36+
"_", "a_", "_a", "a_b", "1_", "_1", "1_2",
37+
".", "a.", ".a", "a..b", "1.", ".1", "1..2",
38+
" ", "a ", " a", "a b", "1 ", " 1", "1 2",
39+
"A.a", "aB.a", "ab.A", "A1.a", "a1.A",
40+
"A.1", "aB.1", "A1.1", "1A.1",
41+
"0.A", "01.A", "012.A", "1A.a", "1a.A",
42+
"A.B.C.D.E", "AA.BB.CC.DD.EE", "a.B.c.d.e", "aa.bB.cc.dd.ee",
43+
"a@b", "a,b", "a_b", "a;b",
44+
"a:b", "a%b", "a?b", "a$b",
45+
strings.Repeat("a", 254),
46+
}
47+
v := DNS1123Subdomain("a")
48+
testStringFormatWithRegistry(t, KubeStyleNames, &v, "k8s-dns1123subdomain", "a", goodValues, badValues)
49+
}
50+
51+
func TestIsDNS1123Label(t *testing.T) {
52+
goodValues := []string{
53+
"a", "ab", "abc", "a1", "a-1", "a--1--2--b",
54+
"0", "01", "012", "1a", "1-a", "1--a--b--2",
55+
strings.Repeat("a", 63),
56+
}
57+
badValues := []string{
58+
"", "A", "ABC", "aBc", "A1", "A-1", "1-A",
59+
"-", "a-", "-a", "1-", "-1",
60+
"_", "a_", "_a", "a_b", "1_", "_1", "1_2",
61+
".", "a.", ".a", "a.b", "1.", ".1", "1.2",
62+
" ", "a ", " a", "a b", "1 ", " 1", "1 2",
63+
strings.Repeat("a", 64),
64+
}
65+
v := DNS1123Label("a")
66+
testStringFormatWithRegistry(t, KubeStyleNames, &v, "k8s-dns1123label", "a", goodValues, badValues)
67+
}
68+
69+
func TestIsDNS1035Label(t *testing.T) {
70+
goodValues := []string{
71+
"a", "ab", "abc", "a1", "a-1", "a--1--2--b",
72+
strings.Repeat("a", 63),
73+
}
74+
badValues := []string{
75+
"0", "01", "012", "1a", "1-a", "1--a--b--2",
76+
"", "A", "ABC", "aBc", "A1", "A-1", "1-A",
77+
"-", "a-", "-a", "1-", "-1",
78+
"_", "a_", "_a", "a_b", "1_", "_1", "1_2",
79+
".", "a.", ".a", "a.b", "1.", ".1", "1.2",
80+
" ", "a ", " a", "a b", "1 ", " 1", "1 2",
81+
strings.Repeat("a", 64),
82+
}
83+
v := DNS1035Label("a")
84+
testStringFormatWithRegistry(t, KubeStyleNames, &v, "k8s-dns1035label", "a", goodValues, badValues)
85+
}

0 commit comments

Comments
 (0)