Skip to content

Commit efcb7bf

Browse files
committed
Add k8s name formats
Organize k8s names formats into their own registry for ratcheted rollout, prefix with k8s-
1 parent b456828 commit efcb7bf

File tree

4 files changed

+385
-3
lines changed

4 files changed

+385
-3
lines changed

pkg/validation/strfmt/default.go

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,3 +1560,204 @@ func (r *Password) DeepCopy() *Password {
15601560
r.DeepCopyInto(out)
15611561
return out
15621562
}
1563+
1564+
// DNS1123Subdomain represents a DNS 1123 Subdomain.
1565+
//
1566+
// swagger:strfmt dns1123subdomain
1567+
type DNS1123Subdomain string
1568+
1569+
// MarshalText turns this instance into text
1570+
func (r DNS1123Subdomain) MarshalText() ([]byte, error) {
1571+
return []byte(string(r)), nil
1572+
}
1573+
1574+
// UnmarshalText hydrates this instance from text
1575+
func (r *DNS1123Subdomain) UnmarshalText(data []byte) error { // validation is performed later on
1576+
*r = DNS1123Subdomain(string(data))
1577+
return nil
1578+
}
1579+
1580+
// Scan read a value from a database driver
1581+
func (r *DNS1123Subdomain) Scan(raw interface{}) error {
1582+
switch v := raw.(type) {
1583+
case []byte:
1584+
*r = DNS1123Subdomain(string(v))
1585+
case string:
1586+
*r = DNS1123Subdomain(v)
1587+
default:
1588+
return fmt.Errorf("cannot sql.Scan() strfmt.DNS1123Subdomain from: %#v", v)
1589+
}
1590+
1591+
return nil
1592+
}
1593+
1594+
func (r DNS1123Subdomain) String() string {
1595+
return string(r)
1596+
}
1597+
1598+
// MarshalJSON returns the DNS1123Subdomain as JSON
1599+
func (r DNS1123Subdomain) MarshalJSON() ([]byte, error) {
1600+
return json.Marshal(string(r))
1601+
}
1602+
1603+
// UnmarshalJSON sets the DNS1123Subdomain from JSON
1604+
func (r *DNS1123Subdomain) UnmarshalJSON(data []byte) error {
1605+
if string(data) == jsonNull {
1606+
return nil
1607+
}
1608+
var ustr string
1609+
if err := json.Unmarshal(data, &ustr); err != nil {
1610+
return err
1611+
}
1612+
*r = DNS1123Subdomain(ustr)
1613+
return nil
1614+
}
1615+
1616+
// DeepCopyInto copies the receiver and writes its value into out.
1617+
func (r *DNS1123Subdomain) DeepCopyInto(out *DNS1123Subdomain) {
1618+
*out = *r
1619+
}
1620+
1621+
// DeepCopy copies the receiver into a new DNS1123Subdomain.
1622+
func (r *DNS1123Subdomain) DeepCopy() *DNS1123Subdomain {
1623+
if r == nil {
1624+
return nil
1625+
}
1626+
out := new(DNS1123Subdomain)
1627+
r.DeepCopyInto(out)
1628+
return out
1629+
}
1630+
1631+
// DNS1123Label represents a DNS 1123 Label.
1632+
//
1633+
// swagger:strfmt dns1123label
1634+
type DNS1123Label string
1635+
1636+
// MarshalText turns this instance into text
1637+
func (r DNS1123Label) MarshalText() ([]byte, error) {
1638+
return []byte(string(r)), nil
1639+
}
1640+
1641+
// UnmarshalText hydrates this instance from text
1642+
func (r *DNS1123Label) UnmarshalText(data []byte) error { // validation is performed later on
1643+
*r = DNS1123Label(string(data))
1644+
return nil
1645+
}
1646+
1647+
// Scan read a value from a database driver
1648+
func (r *DNS1123Label) Scan(raw interface{}) error {
1649+
switch v := raw.(type) {
1650+
case []byte:
1651+
*r = DNS1123Label(string(v))
1652+
case string:
1653+
*r = DNS1123Label(v)
1654+
default:
1655+
return fmt.Errorf("cannot sql.Scan() strfmt.DNS1123Label from: %#v", v)
1656+
}
1657+
1658+
return nil
1659+
}
1660+
1661+
func (r DNS1123Label) String() string {
1662+
return string(r)
1663+
}
1664+
1665+
// MarshalJSON returns the DNS1123Label as JSON
1666+
func (r DNS1123Label) MarshalJSON() ([]byte, error) {
1667+
return json.Marshal(string(r))
1668+
}
1669+
1670+
// UnmarshalJSON sets the DNS1123Label from JSON
1671+
func (r *DNS1123Label) UnmarshalJSON(data []byte) error {
1672+
if string(data) == jsonNull {
1673+
return nil
1674+
}
1675+
var ustr string
1676+
if err := json.Unmarshal(data, &ustr); err != nil {
1677+
return err
1678+
}
1679+
*r = DNS1123Label(ustr)
1680+
return nil
1681+
}
1682+
1683+
// DeepCopyInto copies the receiver and writes its value into out.
1684+
func (r *DNS1123Label) DeepCopyInto(out *DNS1123Label) {
1685+
*out = *r
1686+
}
1687+
1688+
// DeepCopy copies the receiver into a new DNS1123Label.
1689+
func (r *DNS1123Label) DeepCopy() *DNS1123Label {
1690+
if r == nil {
1691+
return nil
1692+
}
1693+
out := new(DNS1123Label)
1694+
r.DeepCopyInto(out)
1695+
return out
1696+
}
1697+
1698+
// DNS1035Label represents a DNS 1035 Label.
1699+
//
1700+
// swagger:strfmt dns1035label
1701+
type DNS1035Label string
1702+
1703+
// MarshalText turns this instance into text
1704+
func (r DNS1035Label) MarshalText() ([]byte, error) {
1705+
return []byte(string(r)), nil
1706+
}
1707+
1708+
// UnmarshalText hydrates this instance from text
1709+
func (r *DNS1035Label) UnmarshalText(data []byte) error { // validation is performed later on
1710+
*r = DNS1035Label(string(data))
1711+
return nil
1712+
}
1713+
1714+
// Scan read a value from a database driver
1715+
func (r *DNS1035Label) Scan(raw interface{}) error {
1716+
switch v := raw.(type) {
1717+
case []byte:
1718+
*r = DNS1035Label(string(v))
1719+
case string:
1720+
*r = DNS1035Label(v)
1721+
default:
1722+
return fmt.Errorf("cannot sql.Scan() strfmt.DNS1035Label from: %#v", v)
1723+
}
1724+
1725+
return nil
1726+
}
1727+
1728+
func (r DNS1035Label) String() string {
1729+
return string(r)
1730+
}
1731+
1732+
// MarshalJSON returns the DNS1035Label as JSON
1733+
func (r DNS1035Label) MarshalJSON() ([]byte, error) {
1734+
return json.Marshal(string(r))
1735+
}
1736+
1737+
// UnmarshalJSON sets the DNS1035Label from JSON
1738+
func (r *DNS1035Label) UnmarshalJSON(data []byte) error {
1739+
if string(data) == jsonNull {
1740+
return nil
1741+
}
1742+
var ustr string
1743+
if err := json.Unmarshal(data, &ustr); err != nil {
1744+
return err
1745+
}
1746+
*r = DNS1035Label(ustr)
1747+
return nil
1748+
}
1749+
1750+
// DeepCopyInto copies the receiver and writes its value into out.
1751+
func (r *DNS1035Label) DeepCopyInto(out *DNS1035Label) {
1752+
*out = *r
1753+
}
1754+
1755+
// DeepCopy copies the receiver into a new DNS1035Label.
1756+
func (r *DNS1035Label) DeepCopy() *DNS1035Label {
1757+
if r == nil {
1758+
return nil
1759+
}
1760+
out := new(DNS1035Label)
1761+
r.DeepCopyInto(out)
1762+
return out
1763+
}

pkg/validation/strfmt/default_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ type testableFormat interface {
317317
}
318318

319319
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) {
320324
// text encoding interface
321325
b := []byte(with)
322326
err := what.UnmarshalText(b)
@@ -348,22 +352,30 @@ func testStringFormat(t *testing.T, what testableFormat, format, with string, va
348352

349353
// validation with Registry
350354
for _, valid := range append(validSamples, with) {
351-
testValid(t, format, valid)
355+
testValidWithRegistry(t, registry, format, valid)
352356
}
353357

354358
for _, invalid := range invalidSamples {
355-
testInvalid(t, format, invalid)
359+
testInvalidWithRegistry(t, registry, format, invalid)
356360
}
357361
}
358362

359363
func testValid(t *testing.T, name, value string) {
360-
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)
361369
if !ok {
362370
t.Errorf("expected %q of type %s to be valid", value, name)
363371
}
364372
}
365373

366374
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) {
367379
ok := Default.Validates(name, value)
368380
if ok {
369381
t.Errorf("expected %q of type %s to be invalid", value, name)
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+
}

0 commit comments

Comments
 (0)