Skip to content

Commit fd29f1e

Browse files
Replace group name regex validation for isDNS1123Subdomain from k8s.io/apimachinery/pkg/util/validation
1 parent 2eebf56 commit fd29f1e

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

pkg/scaffold/v1/resource/resource.go

+53-5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"github.com/gobuffalo/flect"
2525
)
2626

27-
const GroupMatchRegex = "^[a-z-]+$"
28-
2927
// Resource contains the information required to scaffold files for a resource.
3028
type Resource struct {
3129
// Namespaced is true if the resource is namespaced
@@ -70,9 +68,8 @@ func (r *Resource) Validate() error {
7068
r.Resource = flect.Pluralize(strings.ToLower(r.Kind))
7169
}
7270

73-
groupMatch := regexp.MustCompile(GroupMatchRegex)
74-
if !groupMatch.MatchString(r.Group) {
75-
return fmt.Errorf("group must match %s (was %s)", GroupMatchRegex, r.Group)
71+
if err := IsDNS1123Subdomain(r.Group); err != nil {
72+
return fmt.Errorf("group name is invalid: (%v)", err)
7673
}
7774

7875
r.GroupImportSafe = strings.Replace(r.Group, "-", "", -1)
@@ -88,3 +85,54 @@ func (r *Resource) Validate() error {
8885

8986
return nil
9087
}
88+
89+
// The following code came from "k8s.io/apimachinery/pkg/util/validation"
90+
// If be required the usage of more funcs from this then please replace it for the import
91+
// ---------------------------------------
92+
const (
93+
dns1123LabelFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
94+
dns1123SubdomainFmt string = dns1123LabelFmt + "(\\." + dns1123LabelFmt + ")*"
95+
dns1123SubdomainErrorMsg string = "a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character"
96+
97+
// dns1123SubdomainMaxLength is a subdomain's max length in DNS (RFC 1123)
98+
dns1123SubdomainMaxLength int = 253
99+
)
100+
101+
var dns1123SubdomainRegexp = regexp.MustCompile("^" + dns1123SubdomainFmt + "$")
102+
103+
// IsDNS1123Subdomain tests for a string that conforms to the definition of a
104+
// subdomain in DNS (RFC 1123).
105+
func IsDNS1123Subdomain(value string) []string {
106+
var errs []string
107+
if len(value) > dns1123SubdomainMaxLength {
108+
errs = append(errs, maxLenError(dns1123SubdomainMaxLength))
109+
}
110+
if !dns1123SubdomainRegexp.MatchString(value) {
111+
errs = append(errs, regexError(dns1123SubdomainErrorMsg, dns1123SubdomainFmt, "example.com"))
112+
}
113+
return errs
114+
}
115+
116+
// MaxLenError returns a string explanation of a "string too long" validation
117+
// failure.
118+
func maxLenError(length int) string {
119+
return fmt.Sprintf("must be no more than %d characters", length)
120+
}
121+
122+
// RegexError returns a string explanation of a regex validation failure.
123+
func regexError(msg string, fmt string, examples ...string) string {
124+
if len(examples) == 0 {
125+
return msg + " (regex used for validation is '" + fmt + "')"
126+
}
127+
msg += " (e.g. "
128+
for i := range examples {
129+
if i > 0 {
130+
msg += " or "
131+
}
132+
msg += "'" + examples[i] + "', "
133+
}
134+
msg += "regex used for validation is '" + fmt + "')"
135+
return msg
136+
}
137+
138+
// ---------------------------------------

pkg/scaffold/v1/resource/resource_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ var _ = Describe("Resource", func() {
3131
It("should fail if the Group is not all lowercase", func() {
3232
instance := &resource.Resource{Group: "Crew", Version: "v1", Kind: "FirstMate"}
3333
Expect(instance.Validate()).NotTo(Succeed())
34-
Expect(instance.Validate().Error()).To(ContainSubstring("group must match %s (was Crew)", resource.GroupMatchRegex))
34+
Expect(instance.Validate().Error()).To(ContainSubstring("group name is invalid: ([a DNS-1123 subdomain must consist of lower case alphanumeric characters"))
3535
})
3636

3737
It("should fail if the Group contains non-alpha characters", func() {
38-
instance := &resource.Resource{Group: "crew1", Version: "v1", Kind: "FirstMate"}
38+
instance := &resource.Resource{Group: "crew1*?", Version: "v1", Kind: "FirstMate"}
3939
Expect(instance.Validate()).NotTo(Succeed())
40-
Expect(instance.Validate().Error()).To(ContainSubstring("group must match %s (was crew1)", resource.GroupMatchRegex))
40+
Expect(instance.Validate().Error()).To(ContainSubstring("group name is invalid: ([a DNS-1123 subdomain must consist of lower case alphanumeric characters"))
4141
})
4242

4343
It("should fail if the Version is not specified", func() {

0 commit comments

Comments
 (0)