Skip to content

Commit 62bbaa6

Browse files
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
1 parent 87a4801 commit 62bbaa6

File tree

38 files changed

+890
-350
lines changed

38 files changed

+890
-350
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.idea/
22
.vscode/
33
WORKSPACE
4+
.DS_Store
45
# don't check in the build output of the book
56
docs/book/book/
67

docs/book/src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
- [Manager and CRDs Scope](./reference/scopes.md)
103103

104104
- [Sub-Module Layouts](./reference/submodule-layouts.md)
105-
- [Using an external Type / API](./reference/using_an_external_type.md)
105+
- [Using an external Resource / API](./reference/using_an_external_resource.md)
106106

107107
- [Configuring EnvTest](./reference/envtest.md)
108108

docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ func main() {
190190
}
191191
// +kubebuilder:scaffold:builder
192192

193+
// +kubebuilder:scaffold:check-external-api
194+
193195
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
194196
setupLog.Error(err, "unable to set up health check")
195197
os.Exit(1)
@@ -206,3 +208,5 @@ func main() {
206208
}
207209
// +kubebuilder:docs-gen:collapse=old stuff
208210
}
211+
212+
// +kubebuilder:scaffold:add-method-check-external-api

docs/book/src/getting-started/testdata/project/cmd/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ func main() {
153153
}
154154
// +kubebuilder:scaffold:builder
155155

156+
// +kubebuilder:scaffold:check-external-api
157+
156158
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
157159
setupLog.Error(err, "unable to set up health check")
158160
os.Exit(1)
@@ -168,3 +170,5 @@ func main() {
168170
os.Exit(1)
169171
}
170172
}
173+
174+
// +kubebuilder:scaffold:add-method-check-external-api

docs/book/src/multiversion-tutorial/testdata/project/cmd/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ func main() {
192192
/*
193193
*/
194194

195+
// +kubebuilder:scaffold:check-external-api
196+
195197
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
196198
setupLog.Error(err, "unable to set up health check")
197199
os.Exit(1)
@@ -208,3 +210,5 @@ func main() {
208210
}
209211
// +kubebuilder:docs-gen:collapse=existing setup
210212
}
213+
214+
// +kubebuilder:scaffold:add-method-check-external-api

docs/book/src/reference/markers/scaffold.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,19 @@ properly registered with the manager, so that the controller can reconcile the r
9595

9696
## List of `+kubebuilder:scaffold` Markers
9797

98-
| Marker | Usual Location | Function |
99-
|--------------------------------------------|------------------------------|---------------------------------------------------------------------------------|
100-
| `+kubebuilder:scaffold:imports` | `main.go` | Marks where imports for new controllers, webhooks, or APIs should be injected. |
101-
| `+kubebuilder:scaffold:scheme` | `init()` in `main.go` | Used to add API versions to the scheme for runtime. |
102-
| `+kubebuilder:scaffold:builder` | `main.go` | Marks where new controllers should be registered with the manager. |
103-
| `+kubebuilder:scaffold:webhook` | `webhooks suite tests` files | Marks where webhook setup functions are added. |
104-
| `+kubebuilder:scaffold:crdkustomizeresource`| `config/crd` | Marks where CRD custom resource patches are added. |
105-
| `+kubebuilder:scaffold:crdkustomizewebhookpatch` | `config/crd` | Marks where CRD webhook patches are added. |
106-
| `+kubebuilder:scaffold:crdkustomizecainjectionpatch` | `config/crd` | Marks where CA injection patches are added for the webhook. |
107-
| `+kubebuilder:scaffold:manifestskustomizesamples` | `config/samples` | Marks where Kustomize sample manifests are injected. |
108-
| `+kubebuilder:scaffold:e2e-webhooks-checks` | `test/e2e` | Adds e2e checks for webhooks depending on the types of webhooks scaffolded. |
98+
| Marker | Usual Location | Function |
99+
|--------------------------------------------|------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
100+
| `+kubebuilder:scaffold:imports` | `main.go` | Marks where imports for new controllers, webhooks, or APIs should be injected. |
101+
| `+kubebuilder:scaffold:scheme` | `init()` in `main.go` | Used to add API versions to the scheme for runtime. |
102+
| `+kubebuilder:scaffold:builder` | `main.go` | Marks where new controllers should be registered with the manager. |
103+
| `+kubebuilder:scaffold:webhook` | `webhooks suite tests` files | Marks where webhook setup functions are added. |
104+
| `+kubebuilder:scaffold:crdkustomizeresource`| `config/crd` | Marks where CRD custom resource patches are added. |
105+
| `+kubebuilder:scaffold:crdkustomizewebhookpatch` | `config/crd` | Marks where CRD webhook patches are added. |
106+
| `+kubebuilder:scaffold:crdkustomizecainjectionpatch` | `config/crd` | Marks where CA injection patches are added for the webhook. |
107+
| `+kubebuilder:scaffold:manifestskustomizesamples` | `config/samples` | Marks where Kustomize sample manifests are injected. |
108+
| `+kubebuilder:scaffold:e2e-webhooks-checks` | `test/e2e` | Adds e2e checks for webhooks depending on the types of webhooks scaffolded. |
109+
| `+kubebuilder:scaffold:check-external-api` | `main.go` | If a controller is scaffolded for an External Type then, adds a new check for the API to ensure that the API/CRD exist on cluster since it become a prerequisite. |
110+
| `+kubebuilder:scaffold:add-method-check-external-api` | `main.go` | If a controller is scaffolded for an External Type then, adds the method to do verify if the API/version exist on the cluster since it become a prerequisite. |
109111

110112
<aside class="note">
111113
<h1>Creating Your Own Markers</h1>

docs/book/src/reference/project-config.md

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,29 @@ version: "3"
130130

131131
Now let's check its layout fields definition:
132132

133-
| Field | Description |
134-
|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
135-
| `layout` | Defines the global plugins, e.g. a project `init` with `--plugins="go/v4,deploy-image/v1-alpha"` means that any sub-command used will always call its implementation for both plugins in a chain. |
136-
| `domain` | Store the domain of the project. This information can be provided by the user when the project is generate with the `init` sub-command and the `domain` flag. |
137-
| `plugins` | Defines the plugins used to do custom scaffolding, e.g. to use the optional `deploy-image/v1-alpha` plugin to do scaffolding for just a specific api via the command `kubebuider create api [options] --plugins=deploy-image/v1-alpha`. |
138-
| `projectName` | The name of the project. This will be used to scaffold the manager data. By default it is the name of the project directory, however, it can be provided by the user in the `init` sub-command via the `--project-name` flag. |
139-
| `repo` | The project repository which is the Golang module, e.g `github.com/example/myproject-operator`. |
140-
| `resources` | An array of all resources which were scaffolded in the project. |
141-
| `resources.api` | The API scaffolded in the project via the sub-command `create api`. |
142-
| `resources.api.crdVersion` | The Kubernetes API version (`apiVersion`) used to do the scaffolding for the CRD resource. |
143-
| `resources.api.namespaced` | The API RBAC permissions which can be namespaced or cluster scoped. |
144-
| `resources.controller` | Indicates whether a controller was scaffolded for the API. |
145-
| `resources.domain` | The domain of the resource which is provided by the `--domain` flag when the sub-command `create api` is used. |
146-
| `resources.group` | The GKV group of the resource which is provided by the `--group` flag when the sub-command `create api` is used. |
147-
| `resources.version` | The GKV version of the resource which is provided by the `--version` flag when the sub-command `create api` is used. |
148-
| `resources.kind` | Store GKV Kind of the resource which is provided by the `--kind` flag when the sub-command `create api` is used. |
149-
| `resources.path` | The import path for the API resource. It will be `<repo>/api/<kind>` unless the API added to the project is an external or core-type. For the core-types scenarios, the paths used are mapped [here][core-types]. |
150-
| `resources.webhooks`| Store the webhooks data when the sub-command `create webhook` is used. |
151-
| `resources.webhooks.webhookVersion` | The Kubernetes API version (`apiVersion`) used to scaffold the webhook resource. |
152-
| `resources.webhooks.conversion` | It is `true` when the webhook was scaffold with the `--conversion` flag which means that is a conversion webhook. |
153-
| `resources.webhooks.defaulting` | It is `true` when the webhook was scaffold with the `--defaulting` flag which means that is a defaulting webhook. |
154-
| `resources.webhooks.validation` | It is `true` when the webhook was scaffold with the `--programmatic-validation` flag which means that is a validation webhook. |
133+
| Field | Description |
134+
|-------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
135+
| `layout` | Defines the global plugins, e.g. a project `init` with `--plugins="go/v4,deploy-image/v1-alpha"` means that any sub-command used will always call its implementation for both plugins in a chain. |
136+
| `domain` | Store the domain of the project. This information can be provided by the user when the project is generate with the `init` sub-command and the `domain` flag. |
137+
| `plugins` | Defines the plugins used to do custom scaffolding, e.g. to use the optional `deploy-image/v1-alpha` plugin to do scaffolding for just a specific api via the command `kubebuider create api [options] --plugins=deploy-image/v1-alpha`. |
138+
| `projectName` | The name of the project. This will be used to scaffold the manager data. By default it is the name of the project directory, however, it can be provided by the user in the `init` sub-command via the `--project-name` flag. |
139+
| `repo` | The project repository which is the Golang module, e.g `github.com/example/myproject-operator`. |
140+
| `resources` | An array of all resources which were scaffolded in the project. |
141+
| `resources.api` | The API scaffolded in the project via the sub-command `create api`. |
142+
| `resources.api.crdVersion` | The Kubernetes API version (`apiVersion`) used to do the scaffolding for the CRD resource. |
143+
| `resources.api.namespaced` | The API RBAC permissions which can be namespaced or cluster scoped. |
144+
| `resources.controller` | Indicates whether a controller was scaffolded for the API. |
145+
| `resources.domain` | The domain of the resource which was provided by the `--domain` flag when the project was initialized or via the flag `--external-api-domain` when it was used to scaffold controllers for an [External Type][external-type]. |
146+
| `resources.group` | The GKV group of the resource which is provided by the `--group` flag when the sub-command `create api` is used. |
147+
| `resources.version` | The GKV version of the resource which is provided by the `--version` flag when the sub-command `create api` is used. |
148+
| `resources.kind` | Store GKV Kind of the resource which is provided by the `--kind` flag when the sub-command `create api` is used. |
149+
| `resources.path` | The import path for the API resource. It will be `<repo>/api/<kind>` unless the API added to the project is an external or core-type. For the core-types scenarios, the paths used are mapped [here][core-types]. Or either the path informed by the flag `--external-api-path` |
150+
| `resources.external` | It is `true` when the flag `--external-api-path` was used to generated the scaffold for an [External Type][external-type]. |
151+
| `resources.webhooks` | Store the webhooks data when the sub-command `create webhook` is used. |
152+
| `resources.webhooks.webhookVersion` | The Kubernetes API version (`apiVersion`) used to scaffold the webhook resource. |
153+
| `resources.webhooks.conversion` | It is `true` when the webhook was scaffold with the `--conversion` flag which means that is a conversion webhook. |
154+
| `resources.webhooks.defaulting` | It is `true` when the webhook was scaffold with the `--defaulting` flag which means that is a defaulting webhook. |
155+
| `resources.webhooks.validation` | It is `true` when the webhook was scaffold with the `--programmatic-validation` flag which means that is a validation webhook. |
155156

156157
[project]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v3/PROJECT
157158
[versioning]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/VERSIONING.md#Versioning
@@ -160,4 +161,5 @@ Now let's check its layout fields definition:
160161
[olm]: https://olm.operatorframework.io/
161162
[plugins-doc]: ../plugins/creating-plugins.html#why-use-the-kubebuilder-style
162163
[doc-design-helper]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/designs/helper_to_upgrade_projects_by_rescaffolding.md
163-
[operator-sdk]: https://sdk.operatorframework.io/
164+
[operator-sdk]: https://sdk.operatorframework.io/
165+
[external-type]: ./using_an_external_resource.md

0 commit comments

Comments
 (0)