Skip to content

Commit

Permalink
Add support to scaffold controllers for External Types
Browse files Browse the repository at this point in the history
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
  • Loading branch information
camilamacedo86 committed Sep 23, 2024
1 parent 87a4801 commit 62bbaa6
Show file tree
Hide file tree
Showing 38 changed files with 890 additions and 350 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea/
.vscode/
WORKSPACE
.DS_Store
# don't check in the build output of the book
docs/book/book/

Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -206,3 +208,5 @@ func main() {
}
// +kubebuilder:docs-gen:collapse=old stuff
}

// +kubebuilder:scaffold:add-method-check-external-api
4 changes: 4 additions & 0 deletions docs/book/src/getting-started/testdata/project/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -168,3 +170,5 @@ func main() {
os.Exit(1)
}
}

// +kubebuilder:scaffold:add-method-check-external-api
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -208,3 +210,5 @@ func main() {
}
// +kubebuilder:docs-gen:collapse=existing setup
}

// +kubebuilder:scaffold:add-method-check-external-api
24 changes: 13 additions & 11 deletions docs/book/src/reference/markers/scaffold.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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. |
| `+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. |

<aside class="note">
<h1>Creating Your Own Markers</h1>
Expand Down
48 changes: 25 additions & 23 deletions docs/book/src/reference/project-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,28 +130,29 @@ version: "3"

Now let's check its layout fields definition:

| Field | Description |
|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `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. |
| `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. |
| `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`. |
| `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. |
| `repo` | The project repository which is the Golang module, e.g `github.com/example/myproject-operator`. |
| `resources` | An array of all resources which were scaffolded in the project. |
| `resources.api` | The API scaffolded in the project via the sub-command `create api`. |
| `resources.api.crdVersion` | The Kubernetes API version (`apiVersion`) used to do the scaffolding for the CRD resource. |
| `resources.api.namespaced` | The API RBAC permissions which can be namespaced or cluster scoped. |
| `resources.controller` | Indicates whether a controller was scaffolded for the API. |
| `resources.domain` | The domain of the resource which is provided by the `--domain` flag when the sub-command `create api` is used. |
| `resources.group` | The GKV group of the resource which is provided by the `--group` flag when the sub-command `create api` is used. |
| `resources.version` | The GKV version of the resource which is provided by the `--version` flag when the sub-command `create api` is used. |
| `resources.kind` | Store GKV Kind of the resource which is provided by the `--kind` flag when the sub-command `create api` is used. |
| `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]. |
| `resources.webhooks`| Store the webhooks data when the sub-command `create webhook` is used. |
| `resources.webhooks.webhookVersion` | The Kubernetes API version (`apiVersion`) used to scaffold the webhook resource. |
| `resources.webhooks.conversion` | It is `true` when the webhook was scaffold with the `--conversion` flag which means that is a conversion webhook. |
| `resources.webhooks.defaulting` | It is `true` when the webhook was scaffold with the `--defaulting` flag which means that is a defaulting webhook. |
| `resources.webhooks.validation` | It is `true` when the webhook was scaffold with the `--programmatic-validation` flag which means that is a validation webhook. |
| Field | Description |
|-------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `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. |
| `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. |
| `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`. |
| `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. |
| `repo` | The project repository which is the Golang module, e.g `github.com/example/myproject-operator`. |
| `resources` | An array of all resources which were scaffolded in the project. |
| `resources.api` | The API scaffolded in the project via the sub-command `create api`. |
| `resources.api.crdVersion` | The Kubernetes API version (`apiVersion`) used to do the scaffolding for the CRD resource. |
| `resources.api.namespaced` | The API RBAC permissions which can be namespaced or cluster scoped. |
| `resources.controller` | Indicates whether a controller was scaffolded for the API. |
| `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]. |
| `resources.group` | The GKV group of the resource which is provided by the `--group` flag when the sub-command `create api` is used. |
| `resources.version` | The GKV version of the resource which is provided by the `--version` flag when the sub-command `create api` is used. |
| `resources.kind` | Store GKV Kind of the resource which is provided by the `--kind` flag when the sub-command `create api` is used. |
| `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` |
| `resources.external` | It is `true` when the flag `--external-api-path` was used to generated the scaffold for an [External Type][external-type]. |
| `resources.webhooks` | Store the webhooks data when the sub-command `create webhook` is used. |
| `resources.webhooks.webhookVersion` | The Kubernetes API version (`apiVersion`) used to scaffold the webhook resource. |
| `resources.webhooks.conversion` | It is `true` when the webhook was scaffold with the `--conversion` flag which means that is a conversion webhook. |
| `resources.webhooks.defaulting` | It is `true` when the webhook was scaffold with the `--defaulting` flag which means that is a defaulting webhook. |
| `resources.webhooks.validation` | It is `true` when the webhook was scaffold with the `--programmatic-validation` flag which means that is a validation webhook. |

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

0 comments on commit 62bbaa6

Please sign in to comment.