From 728af3b7421ec75e9750d3c965dcb2b8605a933a Mon Sep 17 00:00:00 2001 From: Rob Zienert Date: Thu, 25 Jan 2024 09:48:06 -0800 Subject: [PATCH] Align CRD version validation with apiextensions --- pkg/model/resource/gvk.go | 12 +++--------- pkg/model/resource/gvk_test.go | 10 +++------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/pkg/model/resource/gvk.go b/pkg/model/resource/gvk.go index a17e7fd287f..811edf19727 100644 --- a/pkg/model/resource/gvk.go +++ b/pkg/model/resource/gvk.go @@ -18,24 +18,17 @@ package resource import ( "fmt" - "regexp" "strings" "sigs.k8s.io/kubebuilder/v3/pkg/internal/validation" ) const ( - versionPattern = "^v\\d+(?:alpha\\d+|beta\\d+)?$" - groupRequired = "group cannot be empty if the domain is empty" versionRequired = "version cannot be empty" kindRequired = "kind cannot be empty" ) -var ( - versionRegex = regexp.MustCompile(versionPattern) -) - // GVK stores the Group - Version - Kind triplet that uniquely identifies a resource. // In kubebuilder, the k8s fully qualified group is stored as Group and Domain to improve UX. type GVK struct { @@ -60,8 +53,9 @@ func (gvk GVK) Validate() error { if gvk.Version == "" { return fmt.Errorf(versionRequired) } - if !versionRegex.MatchString(gvk.Version) { - return fmt.Errorf("Version must match %s (was %s)", versionPattern, gvk.Version) + if err := validation.IsDNS1035Label(gvk.Version); err != nil { + // NOTE: IsDNS1035Label returns a slice of strings instead of an error, so no wrapping + return fmt.Errorf("invalid Version: %#v", err) } // Check if kind has a valid DNS1035 label value diff --git a/pkg/model/resource/gvk_test.go b/pkg/model/resource/gvk_test.go index 8b93bef4dd6..e04ff1c18a6 100644 --- a/pkg/model/resource/gvk_test.go +++ b/pkg/model/resource/gvk_test.go @@ -47,13 +47,9 @@ var _ = Describe("GVK", func() { Entry("Domain (non-alpha characters)", GVK{Group: group, Domain: "_*?", Version: version, Kind: kind}), Entry("Group and Domain (empty)", GVK{Group: "", Domain: "", Version: version, Kind: kind}), Entry("Version (empty)", GVK{Group: group, Domain: domain, Version: "", Kind: kind}), - Entry("Version (no v prefix)", GVK{Group: group, Domain: domain, Version: "1", Kind: kind}), - Entry("Version (wrong prefix)", GVK{Group: group, Domain: domain, Version: "a1", Kind: kind}), - Entry("Version (unstable no v prefix)", GVK{Group: group, Domain: domain, Version: "1beta1", Kind: kind}), - Entry("Version (unstable no alpha/beta number)", - GVK{Group: group, Domain: domain, Version: "v1beta", Kind: kind}), - Entry("Version (multiple unstable)", - GVK{Group: group, Domain: domain, Version: "v1beta1alpha1", Kind: kind}), + Entry("Version (starts with number)", GVK{Group: group, Domain: domain, Version: "1", Kind: kind}), + Entry("Version (uppercase)", GVK{Group: group, Domain: domain, Version: "V1", Kind: kind}), + Entry("Version (non-alpha characters)", GVK{Group: group, Domain: domain, Version: "_*?", Kind: kind}), Entry("Kind (empty)", GVK{Group: group, Domain: domain, Version: version, Kind: ""}), Entry("Kind (whitespaces)", GVK{Group: group, Domain: domain, Version: version, Kind: "Ki nd"}), Entry("Kind (lowercase)", GVK{Group: group, Domain: domain, Version: version, Kind: "kind"}),