Skip to content

Commit 12a030c

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 12a030c

File tree

34 files changed

+798
-332
lines changed

34 files changed

+798
-332
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: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
# Using External Resources
3+
4+
In some cases, your project may need to work with resources that aren't defined by your own APIs. 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 defined by another solution.
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, use the `create api` command with the `--resource=false` option and specify the path to the external API type using the `--external-api-path` option. This generates a controller for types defined outside your project, such as CRDs managed by other operators.
14+
15+
The command looks like this:
16+
17+
```shell
18+
kubebuilder create api --group <theirgroup> --version v1alpha1 --kind <ExternalTypeKind> --controller --resource=false --external-api-path=<Golang Import Path>
19+
```
20+
21+
- `--external-api-path`: Provide the Go import path where the external types are defined.
22+
23+
For example, if you're managing Certificates from Cert Manager:
24+
25+
```shell
26+
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
27+
```
28+
29+
This scaffolds a controller for the external type but skips creating new resource definitions since the type is defined in an external project.
30+
31+
### Creating a Webhook to Manage an External Type
32+
33+
<aside>
34+
<H1> Support </H1>
35+
36+
Webhook support for external types is not currently automated by the tool. However, you can still use the tool to scaffold the webhook setup and make manual adjustments as needed. For guidance, you can follow a similar approach as described in the section [Webhooks for Core Types][webhook-for-core-types].
37+
38+
</aside>
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,
44+
use the Kubernetes group name described in the following
45+
table and specify the version and kind.
46+
47+
| Group | K8s API Group Full Path |
48+
|---------------------------|------------------------------------|
49+
| admission | k8s.io/admission |
50+
| admissionregistration | k8s.io/admissionregistration |
51+
| apps | apps |
52+
| auditregistration | k8s.io/auditregistration |
53+
| apiextensions | k8s.io/apiextensions |
54+
| authentication | k8s.io/authentication |
55+
| authorization | k8s.io/authorization |
56+
| autoscaling | autoscaling |
57+
| batch | batch |
58+
| certificates | k8s.io/certificates |
59+
| coordination | k8s.io/coordination |
60+
| core | core |
61+
| events | k8s.io/events |
62+
| extensions | extensions |
63+
| imagepolicy | k8s.io/imagepolicy |
64+
| networking | k8s.io/networking |
65+
| node | k8s.io/node |
66+
| metrics | k8s.io/metrics |
67+
| policy | policy |
68+
| rbac.authorization | k8s.io/rbac.authorization |
69+
| scheduling | k8s.io/scheduling |
70+
| setting | k8s.io/setting |
71+
| storage | k8s.io/storage |
72+
73+
The command to create a controller to manage `Pods` looks like this:
74+
75+
```shell
76+
kubebuilder create api --group core --version v1 --kind Pod --controller=true --resource=false
77+
```
78+
79+
This scaffolds a controller for the Core type `corev1.Pod` but skips creating new resource
80+
definitions since the type is already defined in the Kubernetes API.
81+
82+
83+
<aside class="note">
84+
<h1>Scheme Registry</h1>
85+
86+
The CLI will not automatically register or add schemes for Core Types because these types are already included by default in Kubernetes. Therefore, you don't need to manually register the scheme for core types, as they are inherently supported.
87+
88+
For External Types, the schemas will be added during scaffolding when you create a controller for an external type. The import path provided during scaffolding will be used to register the external type's schema in your project’s `main.go` file and in the `suite_test.go` file.
89+
90+
</aside>
91+
92+
### Creating a Webhook to Manage a Core Type
93+
94+
<aside>
95+
<H1> Support </H1>
96+
97+
Webhook support for Core Types is not currently automated by the tool. However, you can still use the tool to scaffold the webhook setup and make manual adjustments as needed. For guidance, you can follow [Webhooks for Core Types][webhook-for-core-types].
98+
99+
</aside>
100+
101+
[webhook-for-core-types]: ./webhook-for-core-types.md

0 commit comments

Comments
 (0)