From 040ae319673f7b710f46ee57d5b86a7f709e3f1e Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Sun, 15 Sep 2024 15:09:40 +0100 Subject: [PATCH] Add support to scaffold controllers for External Types Introduces the option to allow users scaffold controllers for external types by running: kubebuilder create api --group certmanager --version v1 --kind Certificate --controller=true --resource=false --make=false --external-api-path=github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1 --- .gitignore | 1 + docs/book/src/SUMMARY.md | 2 +- .../testdata/project/cmd/main.go | 4 + .../testdata/project/cmd/main.go | 4 + .../testdata/project/cmd/main.go | 4 + docs/book/src/reference/markers/scaffold.md | 24 +- docs/book/src/reference/reference.md | 2 +- docs/book/src/reference/submodule-layouts.md | 6 +- .../reference/using_an_external_resource.md | 88 ++++++ .../src/reference/using_an_external_type.md | 275 ------------------ .../deploy-image/v1alpha1/scaffolds/api.go | 3 +- pkg/plugins/golang/options.go | 15 +- pkg/plugins/golang/v4/api.go | 14 +- pkg/plugins/golang/v4/scaffolds/api.go | 12 +- .../controllers/controller_suitetest.go | 1 + .../v4/scaffolds/internal/templates/main.go | 88 +++++- test/testdata/generate.sh | 5 + testdata/project-v4-multigroup/PROJECT | 6 + testdata/project-v4-multigroup/cmd/main.go | 65 ++++- .../config/rbac/role.yaml | 26 ++ .../project-v4-multigroup/dist/install.yaml | 26 ++ testdata/project-v4-multigroup/go.mod | 24 +- .../certmanager/certificate_controller.go | 62 ++++ .../certificate_controller_test.go | 32 ++ .../controller/certmanager/suite_test.go | 95 ++++++ testdata/project-v4-with-plugins/cmd/main.go | 4 + testdata/project-v4/PROJECT | 6 + testdata/project-v4/cmd/main.go | 59 ++++ testdata/project-v4/config/rbac/role.yaml | 26 ++ testdata/project-v4/dist/install.yaml | 26 ++ testdata/project-v4/go.mod | 24 +- .../controller/certificate_controller.go | 62 ++++ .../controller/certificate_controller_test.go | 32 ++ .../internal/controller/suite_test.go | 5 + 34 files changed, 798 insertions(+), 330 deletions(-) create mode 100644 docs/book/src/reference/using_an_external_resource.md delete mode 100644 docs/book/src/reference/using_an_external_type.md create mode 100644 testdata/project-v4-multigroup/internal/controller/certmanager/certificate_controller.go create mode 100644 testdata/project-v4-multigroup/internal/controller/certmanager/certificate_controller_test.go create mode 100644 testdata/project-v4-multigroup/internal/controller/certmanager/suite_test.go create mode 100644 testdata/project-v4/internal/controller/certificate_controller.go create mode 100644 testdata/project-v4/internal/controller/certificate_controller_test.go diff --git a/.gitignore b/.gitignore index 3cd3d73e21f..9c5965921cc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .idea/ .vscode/ WORKSPACE +.DS_Store # don't check in the build output of the book docs/book/book/ diff --git a/docs/book/src/SUMMARY.md b/docs/book/src/SUMMARY.md index 67590e84a28..f2b717265b1 100644 --- a/docs/book/src/SUMMARY.md +++ b/docs/book/src/SUMMARY.md @@ -102,7 +102,7 @@ - [Manager and CRDs Scope](./reference/scopes.md) - [Sub-Module Layouts](./reference/submodule-layouts.md) - - [Using an external Type / API](./reference/using_an_external_type.md) + - [Using an external Resource / API](./reference/using_an_external_resource.md) - [Configuring EnvTest](./reference/envtest.md) diff --git a/docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go b/docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go index ab7399bf0c4..d3e90520d65 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go @@ -190,6 +190,8 @@ func main() { } // +kubebuilder:scaffold:builder + // +kubebuilder:scaffold:check-external-api + if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { setupLog.Error(err, "unable to set up health check") os.Exit(1) @@ -206,3 +208,5 @@ func main() { } // +kubebuilder:docs-gen:collapse=old stuff } + +// +kubebuilder:scaffold:add-method-check-external-api diff --git a/docs/book/src/getting-started/testdata/project/cmd/main.go b/docs/book/src/getting-started/testdata/project/cmd/main.go index b40205924db..1e6951c90e2 100644 --- a/docs/book/src/getting-started/testdata/project/cmd/main.go +++ b/docs/book/src/getting-started/testdata/project/cmd/main.go @@ -153,6 +153,8 @@ func main() { } // +kubebuilder:scaffold:builder + // +kubebuilder:scaffold:check-external-api + if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { setupLog.Error(err, "unable to set up health check") os.Exit(1) @@ -168,3 +170,5 @@ func main() { os.Exit(1) } } + +// +kubebuilder:scaffold:add-method-check-external-api diff --git a/docs/book/src/multiversion-tutorial/testdata/project/cmd/main.go b/docs/book/src/multiversion-tutorial/testdata/project/cmd/main.go index 65f0a6405bc..77567fa46b1 100644 --- a/docs/book/src/multiversion-tutorial/testdata/project/cmd/main.go +++ b/docs/book/src/multiversion-tutorial/testdata/project/cmd/main.go @@ -192,6 +192,8 @@ func main() { /* */ + // +kubebuilder:scaffold:check-external-api + if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { setupLog.Error(err, "unable to set up health check") os.Exit(1) @@ -208,3 +210,5 @@ func main() { } // +kubebuilder:docs-gen:collapse=existing setup } + +// +kubebuilder:scaffold:add-method-check-external-api diff --git a/docs/book/src/reference/markers/scaffold.md b/docs/book/src/reference/markers/scaffold.md index 48d18fa88bf..ec18accc5a2 100644 --- a/docs/book/src/reference/markers/scaffold.md +++ b/docs/book/src/reference/markers/scaffold.md @@ -95,17 +95,19 @@ properly registered with the manager, so that the controller can reconcile the r ## List of `+kubebuilder:scaffold` Markers -| Marker | Usual Location | Function | -|--------------------------------------------|------------------------------|---------------------------------------------------------------------------------| -| `+kubebuilder:scaffold:imports` | `main.go` | Marks where imports for new controllers, webhooks, or APIs should be injected. | -| `+kubebuilder:scaffold:scheme` | `init()` in `main.go` | Used to add API versions to the scheme for runtime. | -| `+kubebuilder:scaffold:builder` | `main.go` | Marks where new controllers should be registered with the manager. | -| `+kubebuilder:scaffold:webhook` | `webhooks suite tests` files | Marks where webhook setup functions are added. | -| `+kubebuilder:scaffold:crdkustomizeresource`| `config/crd` | Marks where CRD custom resource patches are added. | -| `+kubebuilder:scaffold:crdkustomizewebhookpatch` | `config/crd` | Marks where CRD webhook patches are added. | -| `+kubebuilder:scaffold:crdkustomizecainjectionpatch` | `config/crd` | Marks where CA injection patches are added for the webhook. | -| `+kubebuilder:scaffold:manifestskustomizesamples` | `config/samples` | Marks where Kustomize sample manifests are injected. | -| `+kubebuilder:scaffold:e2e-webhooks-checks` | `test/e2e` | Adds e2e checks for webhooks depending on the types of webhooks scaffolded. | +| Marker | Usual Location | Function | +|--------------------------------------------|------------------------------|-----------------------------------------------------------------------------------------------------------------------------------| +| `+kubebuilder:scaffold:imports` | `main.go` | Marks where imports for new controllers, webhooks, or APIs should be injected. | +| `+kubebuilder:scaffold:scheme` | `init()` in `main.go` | Used to add API versions to the scheme for runtime. | +| `+kubebuilder:scaffold:builder` | `main.go` | Marks where new controllers should be registered with the manager. | +| `+kubebuilder:scaffold:webhook` | `webhooks suite tests` files | Marks where webhook setup functions are added. | +| `+kubebuilder:scaffold:crdkustomizeresource`| `config/crd` | Marks where CRD custom resource patches are added. | +| `+kubebuilder:scaffold:crdkustomizewebhookpatch` | `config/crd` | Marks where CRD webhook patches are added. | +| `+kubebuilder:scaffold:crdkustomizecainjectionpatch` | `config/crd` | Marks where CA injection patches are added for the webhook. | +| `+kubebuilder:scaffold:manifestskustomizesamples` | `config/samples` | Marks where Kustomize sample manifests are injected. | +| `+kubebuilder:scaffold:e2e-webhooks-checks` | `test/e2e` | Adds e2e checks for webhooks depending on the types of webhooks scaffolded. | +| `+kubebuilder:scaffold:check-external-api` | `main.go` | If an controller be scaffolded for an External Type then, adds a new check for the API to ensure that the api and version exists. | +| `+kubebuilder:scaffold:add-method-check-external-api` | `main.go` | If an controller be scaffolded for an External Type then, adds the method to do verify if the API/version exist on the cluster. |