Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
0a5c604
:sparkles: Use ErrorIfCRDPathMissing in EnvTest
jamielennox Nov 2, 2020
887d714
:fix: remove unecessary checks adds to the main.go (go/v3-alpha)
camilamacedo86 Dec 16, 2020
b1670d8
Remove misleading help that mentioned dep
justinsb Jan 13, 2021
3ad354c
Merge pull request #1945 from justinsb/no_more_dep
k8s-ci-robot Jan 13, 2021
84b6f53
Build the command and use it to report user errors so that the comman…
Adirio Jan 12, 2021
9fd31f1
pkg/plugins/golang/v3: upgrade kubebuilder-declarative-pattern to k8s…
estroz Jan 13, 2021
1cd45e2
Merge pull request #1946 from estroz/chore/update-dep
k8s-ci-robot Jan 13, 2021
c988208
Add help dialog to makefile template
droctothorpe Jan 14, 2021
a7fc753
Add updated testdata
droctothorpe Jan 14, 2021
20df76b
Make directory-not-empty error more self-explanatory
justinsb Jan 13, 2021
f7a5201
Merge pull request #1947 from droctothorpe/makefile-updates
k8s-ci-robot Jan 14, 2021
1001d36
Merge pull request #1943 from justinsb/directory_not_empty_message
k8s-ci-robot Jan 15, 2021
d1105e2
*: upgrade kube-rbac-proxy to v0.8.0
estroz Jan 16, 2021
c577244
Merge pull request #1937 from Adirio/enhance-root-cmd
k8s-ci-robot Jan 18, 2021
032f0c4
Merge pull request #1910 from camilamacedo86/fix-checks
k8s-ci-robot Jan 18, 2021
0d938e4
upgrade kubebuilder module from v2 to v3
camilamacedo86 Jan 18, 2021
e3e3658
Merge pull request #1958 from camilamacedo86/v3-dep
k8s-ci-robot Jan 19, 2021
8d108aa
test commit
estroz Jan 20, 2021
3af7d23
Merge pull request #1955 from estroz/deps/krp-v0.8.0
k8s-ci-robot Jan 20, 2021
79ecd51
Merge pull request #1765 from jamielennox/error-if-missing
k8s-ci-robot Jan 20, 2021
ea75c59
Update Options and Resource models and implement Config as an interfa…
Adirio Dec 17, 2020
1e78a6f
Merge pull request #1911 from Adirio/update-models
k8s-ci-robot Jan 22, 2021
1a6bb7a
Add --plural flag (go/v3)
Adirio Jan 22, 2021
b290147
Merge pull request #1967 from Adirio/plurals
k8s-ci-robot Jan 22, 2021
45f69ce
Validate the plugin flag
Adirio Jan 23, 2021
7b84f60
Merge pull request #1970 from Adirio/validate-plural
k8s-ci-robot Jan 25, 2021
db966c6
Split resource validation between Options and Resource objects so tha…
Adirio Jan 26, 2021
2aef3ad
Add the rest of the missing fields and stabilize config v3
Adirio Jan 22, 2021
03431ed
(go/v3) change 'runAsUser: 65532', which is redundant, to 'runAsNonRo…
estroz Jan 27, 2021
14b27b4
Merge pull request #1978 from estroz/feature/pod-nonroot
k8s-ci-robot Jan 28, 2021
12de4fb
Remove make calls from post-scaffold hooks
Adirio Jan 29, 2021
4fd2cd7
Tolerate "dot" directories when checking if dir is empty
justinsb Jan 13, 2021
e222f25
Merge pull request #1944 from justinsb/tolerate_git_dir
k8s-ci-robot Jan 29, 2021
71b8578
Merge pull request #1968 from Adirio/config/v3
k8s-ci-robot Jan 29, 2021
386207f
:seedling: sort plugins key for the help
camilamacedo86 Jan 29, 2021
63286ce
Merge pull request #1984 from camilamacedo86/sort-plugins-key
k8s-ci-robot Jan 29, 2021
479367b
Merge pull request #1983 from Adirio/remove-post-scaffold-make
k8s-ci-robot Jan 30, 2021
fa40846
Return a typed error in case DecodePluginConfig was unable to find th…
Adirio Feb 1, 2021
564a6e9
Merge pull request #1985 from Adirio/plugin-config-missing-error
k8s-ci-robot Feb 1, 2021
90344af
Merge pull request #1974 from Adirio/split-resource-validation
k8s-ci-robot Feb 2, 2021
95466c5
Add documentation to v2 controllers
Adirio Feb 3, 2021
fed6174
Go fmt templates
Adirio Feb 3, 2021
63904d2
Merge pull request #1989 from Adirio/v2-controller-doc
k8s-ci-robot Feb 3, 2021
6a676b1
Merge pull request #1987 from Adirio/go-fmt-templates
k8s-ci-robot Feb 3, 2021
6e1be3d
Bump addon *_types.go and controller.go templates with changes that w…
Adirio Feb 3, 2021
4ddc71c
Improve Makefile help
Adirio Jan 29, 2021
3c8e370
Merge pull request #1988 from Adirio/bump-addon
k8s-ci-robot Feb 3, 2021
f1685b8
Fix sorting issue nwith plugin versions and their supported project v…
Adirio Feb 8, 2021
89c42c4
Merge pull request #1995 from Adirio/fix-sort
k8s-ci-robot Feb 8, 2021
fe481ef
Merge pull request #1981 from Adirio/makefile-help
k8s-ci-robot Feb 8, 2021
a9dd9dc
Fix go/v2 with config/v3 resources in config file to store webhook in…
Adirio Feb 9, 2021
72f01e5
Merge pull request #1998 from Adirio/fix-go-v2-config-v3-webhooks
k8s-ci-robot Feb 9, 2021
42091e9
Track resources without an API in the config file (config/v3)
Adirio Feb 10, 2021
05abe25
Merge pull request #2003 from Adirio/track-resources-without-api
k8s-ci-robot Feb 11, 2021
bd00512
:seeding: ignore maligned lint issue for WebhookSuite struct
camilamacedo86 Feb 11, 2021
c82530b
Merge pull request #2009 from camilamacedo86/fix-lint-issue
k8s-ci-robot Feb 11, 2021
3948289
Plugin phase 1.5 EP
Adirio Feb 3, 2021
757cc8a
Merge pull request #1990 from Adirio/plugin-phase-1-5
k8s-ci-robot Feb 12, 2021
8c08161
Tests cleanup
Adirio Jan 8, 2021
ab3bb7f
:book: uprade migration guide for v3
camilamacedo86 Dec 17, 2020
685558d
Merge pull request #1916 from camilamacedo86/migration-doc
k8s-ci-robot Feb 12, 2021
9fafbf9
Accept auto-generated files from GitHub into the allowed set of files
Adirio Feb 13, 2021
46c65ba
Merge pull request #2014 from Adirio/accept-github-fefault-files
k8s-ci-robot Feb 13, 2021
a9c3e50
Add Adirio to aprovers
Adirio Feb 13, 2021
5044d6e
Merge pull request #2013 from Adirio/adirio-aprover
k8s-ci-robot Feb 13, 2021
6224a06
Merge pull request #1933 from Adirio/cleanup-tests
k8s-ci-robot Feb 14, 2021
53b2cf6
:book: proposal for new plugin to generate code
camilamacedo86 Nov 9, 2020
5afa97e
Update book examples to Kubebuilder v3.
HoustonPutman Feb 16, 2021
7adcffd
Merge pull request #1803 from camilamacedo86/code-generate-plugin
k8s-ci-robot Feb 17, 2021
e23cbe4
Fix the bug where an error was being hidden by a potentially valid fl…
Adirio Feb 17, 2021
b46beea
Merge pull request #2023 from Adirio/fix-hidden-error
k8s-ci-robot Feb 17, 2021
2398445
Merge pull request #2021 from HoustonPutman/book-empty-examples-v3
k8s-ci-robot Feb 17, 2021
d71b1a9
Allow cross-package coverage inside the "pkg" directory
Adirio Feb 17, 2021
2d9367e
Merge pull request #2024 from Adirio/increase-coverage
k8s-ci-robot Feb 17, 2021
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
10 changes: 10 additions & 0 deletions pkg/model/resource/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ type API struct {
Namespaced bool `json:"namespaced,omitempty"`
}

// Validate checks that the API is valid.
func (api API) Validate() error {
// Validate the CRD version
if err := validateAPIVersion(api.CRDVersion); err != nil {
return fmt.Errorf("invalid CRD version: %w", err)
}

return nil
}

// Copy returns a deep copy of the API that can be safely modified without affecting the original.
func (api API) Copy() API {
// As this function doesn't use a pointer receiver, api is already a shallow copy.
Expand Down
13 changes: 13 additions & 0 deletions pkg/model/resource/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ import (

//nolint:dupl
var _ = Describe("API", func() {
Context("Validate", func() {
It("should succeed for a valid API", func() {
Expect(API{CRDVersion: v1}.Validate()).To(Succeed())
})

DescribeTable("should fail for invalid APIs",
func(api API) { Expect(api.Validate()).NotTo(Succeed()) },
// Ensure that the rest of the fields are valid to check each part
Entry("empty CRD version", API{}),
Entry("invalid CRD version", API{CRDVersion: "1"}),
)
})

Context("Update", func() {
var api, other API

Expand Down
40 changes: 40 additions & 0 deletions pkg/model/resource/gvk.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ package resource

import (
"fmt"
"regexp"
"strings"

"sigs.k8s.io/kubebuilder/v3/pkg/internal/validation"
)

const (
versionPattern = "^v\\d+(?:alpha\\d+|beta\\d+)?$"
)

var (
versionRegex = regexp.MustCompile(versionPattern)
)

// GVK stores the Group - Version - Kind triplet that uniquely identifies a resource.
Expand All @@ -29,6 +41,34 @@ type GVK struct {
Kind string `json:"kind"`
}

// Validate checks that the GVK is valid.
func (gvk GVK) Validate() error {
// Check if the qualified group has a valid DNS1123 subdomain value
if err := validation.IsDNS1123Subdomain(gvk.QualifiedGroup()); err != nil {
// NOTE: IsDNS1123Subdomain returns a slice of strings instead of an error, so no wrapping
return fmt.Errorf("either Group or Domain is invalid: %s", err)
}

// Check if the version follows the valid pattern
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
if errors := validation.IsDNS1035Label(strings.ToLower(gvk.Kind)); len(errors) != 0 {
// NOTE: IsDNS1035Label returns a slice of strings instead of an error, so no wrapping
return fmt.Errorf("invalid Kind: %#v", errors)
}

// Require kind to start with an uppercase character
// NOTE: previous validation already fails for empty strings, gvk.Kind[0] will not panic
if string(gvk.Kind[0]) == strings.ToLower(string(gvk.Kind[0])) {
return fmt.Errorf("invalid Kind: must start with an uppercase character")
}

return nil
}

// QualifiedGroup returns the fully qualified group name with the available information.
func (gvk GVK) QualifiedGroup() string {
switch "" {
Expand Down
41 changes: 37 additions & 4 deletions pkg/model/resource/gvk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package resource

import (
"strings"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
Expand All @@ -30,19 +32,50 @@ var _ = Describe("GVK", func() {
kind = "Kind"
)

var gvk = GVK{Group: group, Domain: domain, Version: version, Kind: kind}

Context("Validate", func() {
It("should succeed for a valid GVK", func() {
Expect(gvk.Validate()).To(Succeed())
})

DescribeTable("should fail for invalid GVKs",
func(gvk GVK) { Expect(gvk.Validate()).NotTo(Succeed()) },
// Ensure that the rest of the fields are valid to check each part
Entry("Group (uppercase)", GVK{Group: "Group", Domain: domain, Version: version, Kind: kind}),
Entry("Group (non-alpha characters)", GVK{Group: "_*?", Domain: domain, Version: version, Kind: kind}),
Entry("Domain (uppercase)", GVK{Group: group, Domain: "Domain", Version: version, Kind: kind}),
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("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"}),
Entry("Kind (starts with number)", GVK{Group: group, Domain: domain, Version: version, Kind: "1Kind"}),
Entry("Kind (ends with `-`)", GVK{Group: group, Domain: domain, Version: version, Kind: "Kind-"}),
Entry("Kind (non-alpha characters)", GVK{Group: group, Domain: domain, Version: version, Kind: "_*?"}),
Entry("Kind (too long)",
GVK{Group: group, Domain: domain, Version: version, Kind: strings.Repeat("a", 64)}),
)
})

Context("QualifiedGroup", func() {
DescribeTable("should return the correct string",
func(gvk GVK, qualifiedGroup string) { Expect(gvk.QualifiedGroup()).To(Equal(qualifiedGroup)) },
Entry("fully qualified resource", GVK{Group: group, Domain: domain, Version: version, Kind: kind},
group+"."+domain),
Entry("fully qualified resource", gvk, group+"."+domain),
Entry("empty group name", GVK{Domain: domain, Version: version, Kind: kind}, domain),
Entry("empty domain", GVK{Group: group, Version: version, Kind: kind}, group),
)
})

Context("IsEqualTo", func() {
var gvk = GVK{Group: group, Domain: domain, Version: version, Kind: kind}

It("should return true for the same resource", func() {
Expect(gvk.IsEqualTo(GVK{Group: group, Domain: domain, Version: version, Kind: kind})).To(BeTrue())
})
Expand Down
34 changes: 34 additions & 0 deletions pkg/model/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package resource
import (
"fmt"
"strings"

"sigs.k8s.io/kubebuilder/v3/pkg/internal/validation"
)

// Resource contains the information required to scaffold files for a resource.
Expand All @@ -42,6 +44,38 @@ type Resource struct {
Webhooks *Webhooks `json:"webhooks,omitempty"`
}

// Validate checks that the Resource is valid.
func (r Resource) Validate() error {
// Validate the GVK
if err := r.GVK.Validate(); err != nil {
return err
}

// Validate the Plural
// NOTE: IsDNS1035Label returns a slice of strings instead of an error, so no wrapping
if errors := validation.IsDNS1035Label(r.Plural); len(errors) != 0 {
return fmt.Errorf("invalid Plural: %#v", errors)
}

// TODO: validate the path

// Validate the API
if r.API != nil && !r.API.IsEmpty() {
if err := r.API.Validate(); err != nil {
return fmt.Errorf("invalid API: %w", err)
}
}

// Validate the Webhooks
if r.Webhooks != nil && !r.Webhooks.IsEmpty() {
if err := r.Webhooks.Validate(); err != nil {
return fmt.Errorf("invalid Webhooks: %w", err)
}
}

return nil
}

// PackageName returns a name valid to be used por go packages.
func (r Resource) PackageName() string {
if r.Group == "" {
Expand Down
Loading