Skip to content

Commit 040ae31

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 c00db6e commit 040ae31

File tree

34 files changed

+798
-330
lines changed

34 files changed

+798
-330
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 an controller be scaffolded for an External Type then, adds a new check for the API to ensure that the api and version exists. |
110+
| `+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. |
109111

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

docs/book/src/reference/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
- [Platform Support](platform.md)
3636

3737
- [Sub-Module Layouts](submodule-layouts.md)
38-
- [Using an external Type / API](using_an_external_type.md)
38+
- [Using an external Resource / API](using_an_external_resource.md)
3939

4040
- [Metrics](metrics.md)
4141
- [Reference](metrics-reference.md)

docs/book/src/reference/submodule-layouts.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ This part describes how to modify a scaffolded project for use with multiple `go
55
Sub-Module Layouts (in a way you could call them a special form of [Monorepo's][monorepo]) are a special use case and can help in scenarios that involve reuse of APIs without introducing indirect dependencies that should not be available in the project consuming the API externally.
66

77
<aside class="note">
8-
<h1>Using external Types</h1>
8+
<h1>Using External Resources/APIs</h1>
99

10-
If you are looking to do operations and reconcile via a controller a Type(CRD) which are owned by another project then, please see [Using an external Type](/reference/using_an_external_type.md) for more info.
10+
If you are looking to do operations and reconcile via a controller a Type(CRD) which are owned by another project
11+
or By Kubernetes API then, please see [Using an external Resources/API](/reference/using_an_external_type.md)
12+
for more info.
1113

1214
</aside>
1315

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Using External Resources
2+
3+
In some cases, your project may need to work with resources that aren't defined by your own APIs.
4+
These external resources fall into two main categories:
5+
6+
- **Core Types**: API types defined by Kubernetes itself, such as `Pods`, `Services`, and `Deployments`.
7+
- **External Types**: API types defined in other projects, such as CRDs managed by another operator.
8+
9+
## Managing External Types
10+
11+
### Creating a Controller for External Types
12+
13+
To create a controller for an external type without scaffolding a resource, you can use the `create api` command with
14+
the `--resource=false` option and specify the path to the external API type using the `--external-api-path` option.
15+
This allows you to generate a controller that operates on types defined outside of your project, such as CRDs managed
16+
by other operators.
17+
18+
The command looks like this:
19+
20+
```shell
21+
kubebuilder create api --group <theirgroup> --version v1alpha1 --kind <ExternalTypeKind> --controller --resource=false --external-api-path=<Golang Import Path>
22+
```
23+
24+
- `--external-api-path`: This is the import path for the external API type in your Go modules.
25+
You should provide the Go import path where the external types are defined.
26+
27+
For example, if you're managing Certificates from Cert Manager:
28+
29+
```shell
30+
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
31+
```
32+
33+
This scaffolds a controller for the external type but skips creating new resource definitions since the type is defined in an external project.
34+
35+
36+
### Creating a Webhooks to manage an external type
37+
38+
> Webhook support for external types is not currently available. (TODO)
39+
40+
## Managing Core Types
41+
42+
Core Kubernetes API types, such as `Pods`, `Services`, and `Deployments`, are predefined by Kubernetes.
43+
To create a controller for these core types without scaffolding the resource, use the appropriate group for the type.
44+
45+
The following table lists the core groups and their corresponding API group paths.
46+
Use this information when defining controllers for core Kubernetes resources.
47+
48+
| Group | API Group |
49+
|--------------------------|-------------------------|
50+
| admission | `k8s.io` |
51+
| admissionregistration | `k8s.io` |
52+
| apps | *(none)* |
53+
| auditregistration | `k8s.io` |
54+
| apiextensions | `k8s.io` |
55+
| authentication | `k8s.io` |
56+
| authorization | `k8s.io` |
57+
| autoscaling | *(none)* |
58+
| batch | *(none)* |
59+
| certificates | `k8s.io` |
60+
| coordination | `k8s.io` |
61+
| core | *(none)* |
62+
| events | `k8s.io` |
63+
| extensions | *(none)* |
64+
| imagepolicy | `k8s.io` |
65+
| networking | `k8s.io` |
66+
| node | `k8s.io` |
67+
| metrics | `k8s.io` |
68+
| policy | *(none)* |
69+
| rbac.authorization | `k8s.io` |
70+
| scheduling | `k8s.io` |
71+
| setting | `k8s.io` |
72+
| storage | `k8s.io` |
73+
74+
Use the group and API version specific to the type you're
75+
managing when creating a controller for a core type.
76+
77+
The command for managing a core type looks like this:
78+
79+
```shell
80+
kubebuilder create api --group core --version v1 --kind Pod --controller=true --resource=false
81+
```
82+
83+
This scaffolds a controller for the Core type `corev1.Pod` but skips creating new resource
84+
definitions since the type is defined in the Kubernetes API.
85+
86+
### Creating a Webhooks to manage a Core Type
87+
88+
> Webhook support for Core Types is not currently available. (TODO)

0 commit comments

Comments
 (0)