Skip to content

Commit

Permalink
Extract Resource from v1 specific package
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Orive <[email protected]>

#1144
  • Loading branch information
Adirio committed Nov 11, 2019
1 parent 9eeec12 commit f4c81fe
Show file tree
Hide file tree
Showing 40 changed files with 276 additions and 250 deletions.
2 changes: 1 addition & 1 deletion cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

"sigs.k8s.io/kubebuilder/cmd/util"
"sigs.k8s.io/kubebuilder/pkg/scaffold"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
"sigs.k8s.io/kubebuilder/plugins/addon"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/webhook_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
"sigs.k8s.io/kubebuilder/pkg/scaffold"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/project"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/manager"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/webhook"
)

Expand Down
8 changes: 4 additions & 4 deletions cmd/webhook_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"sigs.k8s.io/kubebuilder/pkg/scaffold"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/project"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
resourcev2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
scaffoldv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/webhook"
)

Expand Down Expand Up @@ -92,8 +92,8 @@ You need to implement the conversion.Hub and conversion.Convertible interfaces f
os.Exit(1)
}

err = (&resourcev2.Main{}).Update(
&resourcev2.MainUpdateOptions{
err = (&scaffoldv2.Main{}).Update(
&scaffoldv2.MainUpdateOptions{
Project: &projectInfo,
WireResource: false,
WireController: false,
Expand Down
32 changes: 17 additions & 15 deletions pkg/scaffold/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import (
"strings"

"github.com/gobuffalo/flect"

"sigs.k8s.io/kubebuilder/pkg/model"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/project"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold/util"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/controller"
resourcev1 "sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
resourcev2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2"
scaffoldv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2"
crdv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/crd"
)

Expand All @@ -40,7 +42,7 @@ type API struct {
// Plugins is the list of plugins we should allow to transform our generated scaffolding
Plugins []Plugin

Resource *resourcev1.Resource
Resource *resource.Resource

project *input.ProjectFile

Expand Down Expand Up @@ -97,7 +99,7 @@ func (api *API) Scaffold() error {
}

func (api *API) buildUniverse() *model.Universe {
resource := &model.Resource{
resourceModel := &model.Resource{
Namespaced: api.Resource.Namespaced,
Group: api.Resource.Group,
Version: api.Resource.Version,
Expand All @@ -106,10 +108,10 @@ func (api *API) buildUniverse() *model.Universe {
Plural: flect.Pluralize(strings.ToLower(api.Resource.Kind)),
}

resource.GoPackage, resource.GroupDomain = util.GetResourceInfo(api.Resource, api.project.Repo, api.project.Domain)
resourceModel.GoPackage, resourceModel.GroupDomain = util.GetResourceInfo(api.Resource, api.project.Repo, api.project.Domain)

return &model.Universe{
Resource: resource,
Resource: resourceModel,
}
}

Expand Down Expand Up @@ -175,13 +177,13 @@ func (api *API) scaffoldV2() error {
fmt.Sprintf("%s_types.go", strings.ToLower(r.Kind))))

files := []input.File{
&resourcev2.Types{
&scaffoldv2.Types{
Input: input.Input{
Path: filepath.Join("api", r.Version, fmt.Sprintf("%s_types.go", strings.ToLower(r.Kind))),
},
Resource: r},
&resourcev2.Group{Resource: r},
&resourcev2.CRDSample{Resource: r},
&scaffoldv2.Group{Resource: r},
&scaffoldv2.CRDSample{Resource: r},
&crdv2.EnableWebhookPatch{Resource: r},
&crdv2.EnableCAInjectionPatch{Resource: r},
}
Expand Down Expand Up @@ -232,8 +234,8 @@ func (api *API) scaffoldV2() error {
Plugins: api.Plugins,
}

ctrlScaffolder := &resourcev2.Controller{Resource: r}
testsuiteScaffolder := &resourcev2.ControllerSuiteTest{Resource: r}
ctrlScaffolder := &scaffoldv2.Controller{Resource: r}
testsuiteScaffolder := &scaffoldv2.ControllerSuiteTest{Resource: r}
err := scaffold.Execute(
api.buildUniverse(),
input.Options{},
Expand All @@ -250,8 +252,8 @@ func (api *API) scaffoldV2() error {
}
}

err := (&resourcev2.Main{}).Update(
&resourcev2.MainUpdateOptions{
err := (&scaffoldv2.Main{}).Update(
&scaffoldv2.MainUpdateOptions{
Project: api.project,
WireResource: api.DoResource,
WireController: api.DoController,
Expand All @@ -266,10 +268,10 @@ func (api *API) scaffoldV2() error {

// Since we support single group only in v2 scaffolding, validate if resource
// being created belongs to existing group.
func (api *API) validateResourceGroup(resource *resourcev1.Resource) error {
func (api *API) validateResourceGroup(r *resource.Resource) error {
for _, existingGroup := range api.project.ResourceGroups() {
if strings.ToLower(resource.Group) != strings.ToLower(existingGroup) {
return fmt.Errorf("Group '%s' is not same as existing group '%s'. Multiple groups are not supported yet.", resource.Group, existingGroup)
if strings.ToLower(r.Group) != strings.ToLower(existingGroup) {
return fmt.Errorf("Group '%s' is not same as existing group '%s'. Multiple groups are not supported yet.", r.Group, existingGroup)
}
}
return nil
Expand Down
File renamed without changes.
126 changes: 126 additions & 0 deletions pkg/scaffold/resource/resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package resource_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

. "sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)

var _ = Describe("Resource", func() {
Describe("scaffolding an API", func() {
It("should succeed if the Resource is valid", func() {
instance := &Resource{Group: "crew", Version: "v1", Kind: "FirstMate"}
Expect(instance.Validate()).To(Succeed())
})

It("should fail if the Group is not specified", func() {
instance := &Resource{Version: "v1", Kind: "FirstMate"}
Expect(instance.Validate()).NotTo(Succeed())
Expect(instance.Validate().Error()).To(ContainSubstring("group cannot be empty"))
})

It("should fail if the Group is not all lowercase", func() {
instance := &Resource{Group: "Crew", Version: "v1", Kind: "FirstMate"}
Expect(instance.Validate()).NotTo(Succeed())
Expect(instance.Validate().Error()).To(ContainSubstring("group name is invalid: ([a DNS-1123 subdomain must consist of lower case alphanumeric characters"))
})

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

It("should fail if the Version is not specified", func() {
instance := &Resource{Group: "crew", Kind: "FirstMate"}
Expect(instance.Validate()).NotTo(Succeed())
Expect(instance.Validate().Error()).To(ContainSubstring("version cannot be empty"))
})

It("should fail if the Version does not match the version format", func() {
instance := &Resource{Group: "crew", Version: "1", Kind: "FirstMate"}
Expect(instance.Validate()).NotTo(Succeed())
Expect(instance.Validate().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was 1)`))

instance = &Resource{Group: "crew", Version: "1beta1", Kind: "FirstMate"}
Expect(instance.Validate()).NotTo(Succeed())
Expect(instance.Validate().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was 1beta1)`))

instance = &Resource{Group: "crew", Version: "a1beta1", Kind: "FirstMate"}
Expect(instance.Validate()).NotTo(Succeed())
Expect(instance.Validate().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was a1beta1)`))

instance = &Resource{Group: "crew", Version: "v1beta", Kind: "FirstMate"}
Expect(instance.Validate()).NotTo(Succeed())
Expect(instance.Validate().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was v1beta)`))

instance = &Resource{Group: "crew", Version: "v1beta1alpha1", Kind: "FirstMate"}
Expect(instance.Validate()).NotTo(Succeed())
Expect(instance.Validate().Error()).To(ContainSubstring(
`version must match ^v\d+(alpha\d+|beta\d+)?$ (was v1beta1alpha1)`))
})

It("should fail if the Kind is not specified", func() {
instance := &Resource{Group: "crew", Version: "v1"}
Expect(instance.Validate()).NotTo(Succeed())
Expect(instance.Validate().Error()).To(ContainSubstring("kind cannot be empty"))
})

It("should fail if the Kind is not pascal cased", func() {
// Base case
instance := &Resource{Group: "crew", Kind: "FirstMate", Version: "v1"}
Expect(instance.Validate()).To(Succeed())

// Can't detect this case :(
instance = &Resource{Group: "crew", Kind: "Firstmate", Version: "v1"}
Expect(instance.Validate()).To(Succeed())

instance = &Resource{Group: "crew", Kind: "firstMate", Version: "v1"}
Expect(instance.Validate()).NotTo(Succeed())
Expect(instance.Validate().Error()).To(ContainSubstring(
`kind must be PascalCase (expected FirstMate was firstMate)`))

instance = &Resource{Group: "crew", Kind: "firstmate", Version: "v1"}
Expect(instance.Validate()).NotTo(Succeed())
Expect(instance.Validate().Error()).To(ContainSubstring(
`kind must be PascalCase (expected Firstmate was firstmate)`))
})

It("should default the Resource by pluralizing the Kind", func() {
instance := &Resource{Group: "crew", Kind: "FirstMate", Version: "v1"}
Expect(instance.Validate()).To(Succeed())
Expect(instance.Resource).To(Equal("firstmates"))

instance = &Resource{Group: "crew", Kind: "Fish", Version: "v1"}
Expect(instance.Validate()).To(Succeed())
Expect(instance.Resource).To(Equal("fish"))

instance = &Resource{Group: "crew", Kind: "Helmswoman", Version: "v1"}
Expect(instance.Validate()).To(Succeed())
Expect(instance.Resource).To(Equal("helmswomen"))
})

It("should allow Cat as a Kind", func() {
instance := &Resource{Group: "crew", Kind: "Cat", Version: "v1"}
Expect(instance.Validate()).To(Succeed())
Expect(instance.Resource).To(Equal("cats"))
})

It("should allow hyphens in group names", func() {
instance := &Resource{Group: "my-project", Kind: "Cat", Version: "v1"}
Expect(instance.Validate()).To(Succeed())
Expect(instance.GroupImportSafe).To(Equal("myproject"))
})

It("should keep the Resource if specified", func() {
instance := &Resource{Group: "crew", Kind: "FirstMate", Version: "v1", Resource: "myresource"}
Expect(instance.Validate()).To(Succeed())
Expect(instance.Resource).To(Equal("myresource"))
})
})
})
2 changes: 1 addition & 1 deletion pkg/scaffold/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)

func GetResourceInfo(r *resource.Resource, repo, domain string) (resourcePackage, groupDomain string) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/v1/controller/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)

var _ input.File = &AddController{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/v1/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/gobuffalo/flect"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)

// Controller scaffolds a Controller for a Resource
Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/v1/controller/controllersuitetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"strings"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)

// SuiteTest scaffolds a SuiteTest
Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/v1/controller/controllertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"strings"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)

// Test scaffolds a Controller Test
Expand Down
3 changes: 2 additions & 1 deletion pkg/scaffold/v1/resource/addtoscheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)

var _ input.File = &AddToScheme{}
Expand All @@ -30,7 +31,7 @@ type AddToScheme struct {
input.Input

// Resource is a resource in the API group
Resource *Resource
Resource *resource.Resource
}

// GetInput implements input.File
Expand Down
3 changes: 2 additions & 1 deletion pkg/scaffold/v1/resource/crd_sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)

var _ input.File = &CRDSample{}
Expand All @@ -31,7 +32,7 @@ type CRDSample struct {
input.Input

// Resource is a resource in the API group
Resource *Resource
Resource *resource.Resource
}

// GetInput implements input.File
Expand Down
3 changes: 2 additions & 1 deletion pkg/scaffold/v1/resource/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)

var _ input.File = &Doc{}
Expand All @@ -29,7 +30,7 @@ type Doc struct {
input.Input

// Resource is a resource for the API version
Resource *Resource
Resource *resource.Resource

// Comments are additional lines to write to the doc.go file
Comments []string
Expand Down
3 changes: 2 additions & 1 deletion pkg/scaffold/v1/resource/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)

var _ input.File = &Group{}
Expand All @@ -29,7 +30,7 @@ type Group struct {
input.Input

// Resource is a resource in the API group
Resource *Resource
Resource *resource.Resource
}

// GetInput implements input.File
Expand Down
3 changes: 2 additions & 1 deletion pkg/scaffold/v1/resource/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)

var _ input.File = &Register{}
Expand All @@ -29,7 +30,7 @@ type Register struct {
input.Input

// Resource is the resource to scaffold the types_test.go file for
Resource *Resource
Resource *resource.Resource
}

// GetInput implements input.File
Expand Down
Loading

0 comments on commit f4c81fe

Please sign in to comment.