Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Revert ":bug: Align CRD version validation with apiextensions" #3787

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pkg/model/resource/gvk.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ 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 {
Expand All @@ -53,9 +60,8 @@ func (gvk GVK) Validate() error {
if gvk.Version == "" {
return fmt.Errorf(versionRequired)
}
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)
if !versionRegex.MatchString(gvk.Version) {
return fmt.Errorf("Version must match %s (was %s)", versionPattern, gvk.Version)
}

// Check if kind has a valid DNS1035 label value
Expand Down
10 changes: 7 additions & 3 deletions pkg/model/resource/gvk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ 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 (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("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("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"}),
Expand Down
Loading