Skip to content

Commit

Permalink
feat: add plugin v3 to fix controller typo alias and improve its log…
Browse files Browse the repository at this point in the history
…s (only V3)
  • Loading branch information
Camila Macedo committed Jun 19, 2020
1 parent 36aa113 commit 9f2f08a
Show file tree
Hide file tree
Showing 53 changed files with 48 additions and 87 deletions.
2 changes: 1 addition & 1 deletion docs/book/src/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ kubebuilder create api --group webapp --version v1 --kind Guestbook
<h1>Press Options</h1>

If you press `y` for Create Resource [y/n] and for Create Controller [y/n] then this will create the files `api/v1/guestbook_types.go` where the API is defined
and the `controller/guestbook_controller.go` where the reconciliation business logic is implemented for this Kind(CRD).
and the `controllers/guestbook_controller.go` where the reconciliation business logic is implemented for this Kind(CRD).

</aside>

Expand Down
4 changes: 2 additions & 2 deletions docs/testing/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

This article explores steps to write and run integration tests for controllers created using Kubebuilder. Kubebuilder provides a template for writing integration tests. You can simply run all integration (and unit) tests within the project by running: `make test`

For example, there is a controller watches *Parent* objects. The *Parent* objects create *Child* objects. Note that the *Child* objects must have their `.ownerReferences` field setting to the `Parent` objects. You can find the template under `pkg/controller/parent/parent_controller_test.go`:
For example, there is a controller watches *Parent* objects. The *Parent* objects create *Child* objects. Note that the *Child* objects must have their `.ownerReferences` field setting to the `Parent` objects. You can find the template under `controllers/parent/parent_controller_test.go`:
```
package parent
Expand Down Expand Up @@ -79,4 +79,4 @@ func TestReconcile(t *testing.T) {

The manager is started as part of the test itself (`StartTestManager` function).

Both functions are located in `pkg/controller/parent/parent_controller_suite_test.go` file. The file also contains a `TestMain` function that allows you to specify CRD directory paths for the testing environment.
Both functions are located in `controllers/parent/parent_controller_suite_test.go` file. The file also contains a `TestMain` function that allows you to specify CRD directory paths for the testing environment.
2 changes: 1 addition & 1 deletion docs/using_an_external_type.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ dep ensure --add

Edit the `CRDDirectoryPaths` in your test suite by appending the path to their CRDs:

file pkg/controller/my_kind_controller_suite_test.go
file pkg/controllers/my_kind_controller_suite_test.go
```
var cfg *rest.Config
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module sigs.k8s.io/kubebuilder
go 1.13

require (
github.com/blang/semver v3.5.1+incompatible
github.com/gobuffalo/flect v0.2.1
github.com/onsi/ginkgo v1.12.0
github.com/onsi/gomega v1.9.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
Expand Down
29 changes: 0 additions & 29 deletions internal/config/config_suite_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/plugin/v2/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
const pluginName = "go" + plugin.DefaultNameQualifier

var (
supportedProjectVersions = []string{config.Version2, config.Version3Alpha}
supportedProjectVersions = []string{config.Version2}
pluginVersion = plugin.Version{Number: 2}
)

Expand Down
8 changes: 4 additions & 4 deletions pkg/plugin/v2/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"sigs.k8s.io/kubebuilder/pkg/model/resource"
"sigs.k8s.io/kubebuilder/pkg/plugin/internal/machinery"
"sigs.k8s.io/kubebuilder/pkg/plugin/scaffold"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/internal/templates"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/internal/templates/controller"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/internal/templates/crd"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/templates"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/templates/controller"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/templates/crd"
)

// (used only to gen api with --pattern=addon)
Expand Down Expand Up @@ -72,7 +72,7 @@ func (s *apiScaffolder) Scaffold() error {
fmt.Println("Writing scaffold for you to edit...")

switch {
case s.config.IsV2(), s.config.IsV3():
case s.config.IsV2():
return s.scaffold()
default:
return fmt.Errorf("unknown project version %v", s.config.Version)
Expand Down
12 changes: 6 additions & 6 deletions pkg/plugin/v2/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
"sigs.k8s.io/kubebuilder/pkg/model/config"
"sigs.k8s.io/kubebuilder/pkg/plugin/internal/machinery"
"sigs.k8s.io/kubebuilder/pkg/plugin/scaffold"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/internal/templates"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/internal/templates/certmanager"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/internal/templates/manager"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/internal/templates/metricsauth"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/internal/templates/prometheus"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/internal/templates/webhook"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/templates"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/templates/certmanager"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/templates/manager"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/templates/metricsauth"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/templates/prometheus"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/templates/webhook"
)

const (
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ const (
`
controllerImportCodeFragment = `"%s/controllers"
`
// TODO(v3): `&%scontrollers` should be used instead of `&%scontroller` as there may be multiple
// controller for different Kinds in the same group. However, this is a backwards incompatible
// change, and thus should be done for next project version.
multiGroupControllerImportCodeFragment = `%scontroller "%s/controllers/%s"
`
addschemeCodeFragment = `utilruntime.Must(%s.AddToScheme(scheme))
Expand All @@ -124,9 +121,6 @@ const (
os.Exit(1)
}
`
// TODO(v3): loggers for the same Kind controllers from different groups use the same logger.
// `.WithName("controllers").WithName(GROUP).WithName(KIND)` should be used instead. However,
// this is a backwards incompatible change, and thus should be done for next project version.
multiGroupReconcilerSetupCodeFragment = `if err = (&%scontroller.%sReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("%s"),
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions pkg/plugin/v2/scaffolds/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"sigs.k8s.io/kubebuilder/pkg/model/resource"
"sigs.k8s.io/kubebuilder/pkg/plugin/internal/machinery"
"sigs.k8s.io/kubebuilder/pkg/plugin/scaffold"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/internal/templates"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/internal/templates/webhook"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/templates"
"sigs.k8s.io/kubebuilder/pkg/plugin/v2/scaffolds/templates/webhook"
)

var _ scaffold.Scaffolder = &webhookScaffolder{}
Expand Down Expand Up @@ -63,7 +63,7 @@ func (s *webhookScaffolder) Scaffold() error {
fmt.Println("Writing scaffold for you to edit...")

switch {
case s.config.IsV2(), s.config.IsV3():
case s.config.IsV2():
return s.scaffold()
default:
return fmt.Errorf("unknown project version %v", s.config.Version)
Expand Down
11 changes: 8 additions & 3 deletions pkg/plugin/v3/scaffolds/api.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020 The Kubernetes Authors.
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -66,7 +66,13 @@ func NewAPIScaffolder(
// Scaffold implements Scaffolder
func (s *apiScaffolder) Scaffold() error {
fmt.Println("Writing scaffold for you to edit...")
return s.scaffold()

switch {
case s.config.IsV3():
return s.scaffold()
default:
return fmt.Errorf("unknown project version %v", s.config.Version)
}
}

func (s *apiScaffolder) newUniverse() *model.Universe {
Expand All @@ -77,7 +83,6 @@ func (s *apiScaffolder) newUniverse() *model.Universe {
)
}

// TODO: re-use universe created by s.newUniverse() if possible.
func (s *apiScaffolder) scaffold() error {
if s.doResource {
s.config.AddResource(s.resource.GVK())
Expand Down
14 changes: 4 additions & 10 deletions pkg/plugin/v3/scaffolds/internal/templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ const (
`
controllerImportCodeFragment = `"%s/controllers"
`
// TODO(v3): `&%scontrollers` should be used instead of `&%scontroller` as there may be multiple
// controller for different Kinds in the same group. However, this is a backwards incompatible
// change, and thus should be done for next project version.
multiGroupControllerImportCodeFragment = `%scontroller "%s/controllers/%s"
multiGroupControllerImportCodeFragment = `%scontrollers "%s/controllers/%s"
`
addschemeCodeFragment = `utilruntime.Must(%s.AddToScheme(scheme))
`
Expand All @@ -124,12 +121,9 @@ const (
os.Exit(1)
}
`
// TODO(v3): loggers for the same Kind controllers from different groups use the same logger.
// `.WithName("controllers").WithName(GROUP).WithName(KIND)` should be used instead. However,
// this is a backwards incompatible change, and thus should be done for next project version.
multiGroupReconcilerSetupCodeFragment = `if err = (&%scontroller.%sReconciler{
multiGroupReconcilerSetupCodeFragment = `if err = (&%scontrollers.%sReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("%s"),
Log: ctrl.Log.WithName("controllers").WithName("%s").WithName("%s"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "%s")
Expand Down Expand Up @@ -176,7 +170,7 @@ func (f *MainUpdater) GetCodeFragments() file.CodeFragmentsMap {
f.Resource.Kind, f.Resource.Kind, f.Resource.Kind))
} else {
setup = append(setup, fmt.Sprintf(multiGroupReconcilerSetupCodeFragment,
f.Resource.GroupPackageName, f.Resource.Kind, f.Resource.Kind, f.Resource.Kind))
f.Resource.GroupPackageName, f.Resource.Kind, f.Resource.Group, f.Resource.Kind, f.Resource.Kind))
}
}
if f.WireWebhook {
Expand Down
36 changes: 18 additions & 18 deletions testdata/project-v3-multigroup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ import (
shipv1 "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/apis/ship/v1"
shipv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/apis/ship/v1beta1"
shipv2alpha1 "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/apis/ship/v2alpha1"
crewcontroller "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/controllers/crew"
foopolicycontroller "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/controllers/foo.policy"
seacreaturescontroller "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/controllers/sea-creatures"
shipcontroller "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/controllers/ship"
crewcontrollers "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/controllers/crew"
foopolicycontrollers "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/controllers/foo.policy"
seacreaturescontrollers "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/controllers/sea-creatures"
shipcontrollers "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/controllers/ship"
// +kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -82,9 +82,9 @@ func main() {
os.Exit(1)
}

if err = (&crewcontroller.CaptainReconciler{
if err = (&crewcontrollers.CaptainReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Captain"),
Log: ctrl.Log.WithName("controllers").WithName("crew").WithName("Captain"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Captain")
Expand All @@ -94,9 +94,9 @@ func main() {
setupLog.Error(err, "unable to create webhook", "webhook", "Captain")
os.Exit(1)
}
if err = (&shipcontroller.FrigateReconciler{
if err = (&shipcontrollers.FrigateReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Frigate"),
Log: ctrl.Log.WithName("controllers").WithName("ship").WithName("Frigate"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Frigate")
Expand All @@ -106,41 +106,41 @@ func main() {
setupLog.Error(err, "unable to create webhook", "webhook", "Frigate")
os.Exit(1)
}
if err = (&shipcontroller.DestroyerReconciler{
if err = (&shipcontrollers.DestroyerReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Destroyer"),
Log: ctrl.Log.WithName("controllers").WithName("ship").WithName("Destroyer"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Destroyer")
os.Exit(1)
}
if err = (&shipcontroller.CruiserReconciler{
if err = (&shipcontrollers.CruiserReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Cruiser"),
Log: ctrl.Log.WithName("controllers").WithName("ship").WithName("Cruiser"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Cruiser")
os.Exit(1)
}
if err = (&seacreaturescontroller.KrakenReconciler{
if err = (&seacreaturescontrollers.KrakenReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Kraken"),
Log: ctrl.Log.WithName("controllers").WithName("sea-creatures").WithName("Kraken"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Kraken")
os.Exit(1)
}
if err = (&seacreaturescontroller.LeviathanReconciler{
if err = (&seacreaturescontrollers.LeviathanReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Leviathan"),
Log: ctrl.Log.WithName("controllers").WithName("sea-creatures").WithName("Leviathan"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Leviathan")
os.Exit(1)
}
if err = (&foopolicycontroller.HealthCheckPolicyReconciler{
if err = (&foopolicycontrollers.HealthCheckPolicyReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("HealthCheckPolicy"),
Log: ctrl.Log.WithName("controllers").WithName("foo.policy").WithName("HealthCheckPolicy"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "HealthCheckPolicy")
Expand Down

0 comments on commit 9f2f08a

Please sign in to comment.