Skip to content

Commit

Permalink
fix: any layout should keep api/ as dir
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed May 8, 2020
1 parent 09fca78 commit 1f84ad2
Show file tree
Hide file tree
Showing 84 changed files with 162 additions and 68 deletions.
6 changes: 3 additions & 3 deletions docs/book/src/migration/multi-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Then, we'll rename `api` to `apis` to be more clear, and we'll move our
existing APIs into a new subdirectory, "batch":

```bash
mkdir apis/batch
mv api/* apis/batch
mkdir api/batch
mv api/* api/batch
# After ensuring that all was moved successfully remove the old directory `api/`
rm -rf api/
```
Expand Down Expand Up @@ -72,7 +72,7 @@ Note that this option indicates to KubeBuilder that this is a multi-group projec
In this way, if the project is not new and has previous APIs already implemented will be in the previous structure.
Notice that with the `multi-group` project the Kind API's files are
created under `apis/<group>/<version>` instead of `api/<version>`.
created under `api/<group>/<version>` instead of `api/<version>`.
Also, note that the controllers will be created under `controllers/<group>` instead of `controllers`.
That is the reason why we moved the previously generated APIs with the provided scripts in the previous steps.
Remember to update the references afterwards.
Expand Down
4 changes: 2 additions & 2 deletions docs/testing/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ package parent
import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
childapis "k8s.io/child/pkg/apis"
childv1alpha1 "k8s.io/childrepo/pkg/apis/child/v1alpha1"
childv1alpha1 "k8s.io/childrepo/pkg/api/child/v1alpha1"
parentapis "k8s.io/parent/pkg/apis"
parentv1alpha1 "k8s.io/parentrepo/pkg/apis/parent/v1alpha1"
parentv1alpha1 "k8s.io/parentrepo/pkg/api/parent/v1alpha1"
...<other import items>...
)
Expand Down
10 changes: 5 additions & 5 deletions docs/using_an_external_type.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ kubebuilder create api --group mygroup --version $APIVERSION --kind MyKind

Edit the following file to the pkg/apis directory to append their `AddToScheme` to your `AddToSchemes`:

file: pkg/apis/mytype_addtoscheme.go
file: pkg/api/mytype_addtoscheme.go
```
package apis
import (
mygroupv1alpha1 "github.com/myuser/myrepo/apis/mygroup/v1alpha1"
theirgroupv1alpha1 "github.com/theiruser/theirproject/apis/theirgroup/v1alpha1"
mygroupv1alpha1 "github.com/myuser/myrepo/api/mygroup/v1alpha1"
theirgroupv1alpha1 "github.com/theiruser/theirproject/api/theirgroup/v1alpha1"
)
func init() {
Expand All @@ -88,8 +88,8 @@ func init() {
file: pkg/controllers/mytype_controller.go
```
import (
mygroupv1alpha1 "github.com/myuser/myrepo/apis/mygroup/v1alpha1"
theirgroupv1alpha1 "github.com/theiruser/theirproject/apis/theirgroup/v1alpha1"
mygroupv1alpha1 "github.com/myuser/myrepo/api/mygroup/v1alpha1"
theirgroupv1alpha1 "github.com/theiruser/theirproject/api/theirgroup/v1alpha1"
)
```

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/resource/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (opts *Options) NewResource(c *config.Config, doResource bool) *Resource {

pkg := replacer.Replace(path.Join(c.Repo, "api", "%[version]"))
if c.MultiGroup {
pkg = replacer.Replace(path.Join(c.Repo, "apis", "%[group]", "%[version]"))
pkg = replacer.Replace(path.Join(c.Repo, "api", "%[group]", "%[version]"))
}
domain := c.Domain

Expand Down
6 changes: 3 additions & 3 deletions pkg/model/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var _ = Describe("Resource", func() {
Expect(resource.Kind).To(Equal(options.Kind))
Expect(resource.Plural).To(Equal("firstmates"))
Expect(resource.ImportAlias).To(Equal("crewv1"))
Expect(resource.Package).To(Equal(path.Join("test", "apis", "crew", "v1")))
Expect(resource.Package).To(Equal(path.Join("test", "api", "crew", "v1")))
Expect(resource.Domain).To(Equal("crew.test.io"))
})

Expand Down Expand Up @@ -156,7 +156,7 @@ var _ = Describe("Resource", func() {
Expect(resource.Group).To(Equal(options.Group))
Expect(resource.GroupPackageName).To(Equal("myproject"))
Expect(resource.ImportAlias).To(Equal("myprojectv1"))
Expect(resource.Package).To(Equal(path.Join("test", "apis", "my-project", "v1")))
Expect(resource.Package).To(Equal(path.Join("test", "api", "my-project", "v1")))
Expect(resource.Domain).To(Equal("my-project.test.io"))

options = &Options{Group: "my.project", Version: "v1", Kind: "FirstMate"}
Expand All @@ -173,7 +173,7 @@ var _ = Describe("Resource", func() {
Expect(resource.Group).To(Equal(options.Group))
Expect(resource.GroupPackageName).To(Equal("myproject"))
Expect(resource.ImportAlias).To(Equal("myprojectv1"))
Expect(resource.Package).To(Equal(path.Join("test", "apis", "my.project", "v1")))
Expect(resource.Package).To(Equal(path.Join("test", "api", "my.project", "v1")))
Expect(resource.Domain).To(Equal("my.project.test.io"))
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/internal/templates/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Group struct {
func (f *Group) SetTemplateDefaults() error {
if f.Path == "" {
if f.MultiGroup {
f.Path = filepath.Join("apis", "%[group]", "%[version]", "groupversion_info.go")
f.Path = filepath.Join("api", "%[group]", "%[version]", "groupversion_info.go")
} else {
f.Path = filepath.Join("api", "%[version]", "groupversion_info.go")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/internal/templates/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Types struct {
func (f *Types) SetTemplateDefaults() error {
if f.Path == "" {
if f.MultiGroup {
f.Path = filepath.Join("apis", "%[group]", "%[version]", "%[kind]_types.go")
f.Path = filepath.Join("api", "%[group]", "%[version]", "%[kind]_types.go")
} else {
f.Path = filepath.Join("api", "%[version]", "%[kind]_types.go")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/internal/templates/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Webhook struct { // nolint:maligned
func (f *Webhook) SetTemplateDefaults() error {
if f.Path == "" {
if f.MultiGroup {
f.Path = filepath.Join("apis", "%[group]", "%[version]", "%[kind]_webhook.go")
f.Path = filepath.Join("api", "%[group]", "%[version]", "%[kind]_webhook.go")
} else {
f.Path = filepath.Join("api", "%[version]", "%[kind]_webhook.go")
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/addon/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func ReplaceTypes(u *model.Universe) error {

var path string
if u.Config.MultiGroup {
path = filepath.Join("apis", u.Resource.Version, strings.ToLower(u.Resource.Kind)+"_types.go")
path = filepath.Join("api", u.Resource.Version, strings.ToLower(u.Resource.Kind)+"_types.go")
} else {
path = filepath.Join("api", u.Resource.Version, strings.ToLower(u.Resource.Kind)+"_types.go")
}
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/utils/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ func (kc *KBTestContext) CreateAPI(resourceOptions ...string) error {
return err
}

// Edit is for running `kubebuilder edit`
func (kc *KBTestContext) Edit(editOptions ...string) error {
editOptions = append([]string{"edit"}, editOptions...)
cmd := exec.Command("kubebuilder", editOptions...)
_, err := kc.Run(cmd)
return err
}

// CreateWebhook is for running `kubebuilder create webhook`
func (kc *KBTestContext) CreateWebhook(resourceOptions ...string) error {
resourceOptions = append([]string{"create", "webhook"}, resourceOptions...)
Expand Down
41 changes: 41 additions & 0 deletions test/e2e/v2/e2e_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"sigs.k8s.io/kubebuilder/test/e2e/utils"
)

//todo: (cleanup) try to centralize the impl without lost the flexibility of have tests for each project version
var _ = Describe("kubebuilder", func() {
Context("with v2 scaffolding", func() {
var kbc *utils.KBTestContext
Expand Down Expand Up @@ -328,6 +329,46 @@ var _ = Describe("kubebuilder", func() {
Expect(err).NotTo(HaveOccurred())
Expect(count).To(BeNumerically("==", 5))
})

It("should build successfully with multi-group layout", func() {
By("init v2 project")
err := kbc.Init(
"--project-version", "2",
"--domain", kbc.Domain,
"--dep=false")
Expect(err).Should(Succeed())

By("editing default layout")
err = kbc.Edit(
"--multigroup=true")
Expect(err).Should(Succeed())

By("creating api definition")
err = kbc.CreateAPI(
"--group", kbc.Group,
"--version", kbc.Version,
"--kind", kbc.Kind,
"--namespaced",
"--resource=true",
"--controller=true",
"--make=false")
Expect(err).Should(Succeed())

By("creating second api definition with same kind and version")
err = kbc.CreateAPI(
"--group", kbc.Group+"-example",
"--version", kbc.Version,
"--kind", kbc.Kind,
"--namespaced",
"--resource=true",
"--controller=true",
"--make=false")
Expect(err).Should(Succeed())

By("building image")
err = kbc.Make("docker-build", "IMG="+kbc.ImageName)
Expect(err).Should(Succeed())
})
})
})

Expand Down
41 changes: 41 additions & 0 deletions test/e2e/v3/e2e_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"sigs.k8s.io/kubebuilder/test/e2e/utils"
)

//todo: (cleanup) try to centralize the impl without lost the flexibility of have tests for each project version
var _ = Describe("kubebuilder", func() {
Context("with v3 scaffolding", func() {
var kbc *utils.KBTestContext
Expand Down Expand Up @@ -328,6 +329,46 @@ var _ = Describe("kubebuilder", func() {
Expect(err).NotTo(HaveOccurred())
Expect(count).To(BeNumerically("==", 5))
})

It("should build successfully with multi-group layout", func() {
By("init v3 project")
err := kbc.Init(
"--project-version", "3-alpha",
"--domain", kbc.Domain,
"--dep=false")
Expect(err).Should(Succeed())

By("editing default layout")
err = kbc.Edit(
"--multigroup=true")
Expect(err).Should(Succeed())

By("creating api definition")
err = kbc.CreateAPI(
"--group", kbc.Group,
"--version", kbc.Version,
"--kind", kbc.Kind,
"--namespaced",
"--resource=true",
"--controller=true",
"--make=true")
Expect(err).Should(Succeed())

By("creating second api definition with same kind and version")
err = kbc.CreateAPI(
"--group", kbc.Group+"-example",
"--version", kbc.Version,
"--kind", kbc.Kind,
"--namespaced",
"--resource=true",
"--controller=true",
"--make=false")
Expect(err).Should(Succeed())

By("building image")
err = kbc.Make("docker-build", "IMG="+kbc.ImageName)
Expect(err).Should(Succeed())
})
})
})

Expand Down
10 changes: 6 additions & 4 deletions testdata/project-v2-addon/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ require (
github.com/go-logr/logr v0.1.0
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.8.1
k8s.io/apimachinery v0.17.2
k8s.io/client-go v0.17.2
sigs.k8s.io/controller-runtime v0.5.0
sigs.k8s.io/kubebuilder-declarative-pattern v0.0.0-20200415210853-85eb326a6add
gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e // indirect
k8s.io/apimachinery v0.18.2
k8s.io/client-go v0.18.2
sigs.k8s.io/controller-runtime v0.6.0
sigs.k8s.io/kubebuilder-declarative-pattern v0.0.0-20200508060342-d7f9f832e44b
sigs.k8s.io/structured-merge-diff v1.0.1-0.20191108220359-b1b620dd3f06 // indirect
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

crewv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/crew/v1"
crewv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/crew/v1"
)

// CaptainReconciler reconciles a Captain object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

crewv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/crew/v1"
crewv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/crew/v1"
// +kubebuilder:scaffold:imports
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

foopolicyv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/foo.policy/v1"
foopolicyv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/foo.policy/v1"
)

// HealthCheckPolicyReconciler reconciles a HealthCheckPolicy object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

foopolicyv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/foo.policy/v1"
foopolicyv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/foo.policy/v1"
// +kubebuilder:scaffold:imports
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

seacreaturesv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/sea-creatures/v1beta1"
seacreaturesv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/sea-creatures/v1beta1"
)

// KrakenReconciler reconciles a Kraken object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

seacreaturesv1beta2 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/sea-creatures/v1beta2"
seacreaturesv1beta2 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/sea-creatures/v1beta2"
)

// LeviathanReconciler reconciles a Leviathan object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

seacreaturesv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/sea-creatures/v1beta1"
seacreaturesv1beta2 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/sea-creatures/v1beta2"
seacreaturesv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/sea-creatures/v1beta1"
seacreaturesv1beta2 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/sea-creatures/v1beta2"
// +kubebuilder:scaffold:imports
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

shipv2alpha1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/ship/v2alpha1"
shipv2alpha1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/ship/v2alpha1"
)

// CruiserReconciler reconciles a Cruiser object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

shipv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/ship/v1"
shipv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/ship/v1"
)

// DestroyerReconciler reconciles a Destroyer object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

shipv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/ship/v1beta1"
shipv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/ship/v1beta1"
)

// FrigateReconciler reconciles a Frigate object
Expand Down
6 changes: 3 additions & 3 deletions testdata/project-v2-multigroup/controllers/ship/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

shipv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/ship/v1"
shipv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/ship/v1beta1"
shipv2alpha1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/ship/v2alpha1"
shipv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/ship/v1"
shipv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/ship/v1beta1"
shipv2alpha1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/ship/v2alpha1"
// +kubebuilder:scaffold:imports
)

Expand Down
14 changes: 7 additions & 7 deletions testdata/project-v2-multigroup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

crewv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/crew/v1"
foopolicyv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/foo.policy/v1"
seacreaturesv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/sea-creatures/v1beta1"
seacreaturesv1beta2 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/sea-creatures/v1beta2"
shipv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/ship/v1"
shipv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/ship/v1beta1"
shipv2alpha1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/apis/ship/v2alpha1"
crewv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/crew/v1"
foopolicyv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/foo.policy/v1"
seacreaturesv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/sea-creatures/v1beta1"
seacreaturesv1beta2 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/sea-creatures/v1beta2"
shipv1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/ship/v1"
shipv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/ship/v1beta1"
shipv2alpha1 "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/api/ship/v2alpha1"
crewcontroller "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/controllers/crew"
foopolicycontroller "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/controllers/foo.policy"
seacreaturescontroller "sigs.k8s.io/kubebuilder/testdata/project-v2-multigroup/controllers/sea-creatures"
Expand Down
Loading

0 comments on commit 1f84ad2

Please sign in to comment.