Skip to content

Commit 637f85a

Browse files
committed
docs: supplement plugin creation
1 parent 5f51f1d commit 637f85a

File tree

1 file changed

+127
-1
lines changed

1 file changed

+127
-1
lines changed

docs/book/src/plugins/creating-plugins.md

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,40 @@
1212

1313
You can extend the Kubebuilder API to create your own plugins. If [extending the CLI][extending-cli], your plugin will be implemented in your project and registered to the CLI as has been done by the [SDK][sdk] project. See its [CLI code][sdk-cli-pkg] as an example.
1414

15+
## When it is useful?
16+
17+
- If you are looking to create plugins which support and work with another language.
18+
- If you would like to create helpers and integrations on top of the scaffolds done by the plugins provided by Kubebuiler.
19+
- If you would like to have customized layouts according to your needs.
20+
21+
## How the plugins can be used?
22+
23+
Kubebuilder provides a set of plugins to scaffold the projects, to help you extend and re-use its implementation to provide additional features.
24+
For further information see [Available Plugins][available-plugins].
25+
26+
Therefore, if you have a need you might want to propose a solution by adding a new plugin
27+
which would be shipped with Kubebuilder by default.
28+
29+
However, you might also want to have your own tool to address your specific scenarios and by taking advantage of what is provided by Kubebuilder as a library.
30+
That way, you can focus on addressing your needs and keep your solutions easier to maintain.
31+
32+
Note that by using Kubebuilder as a library, you can import its plugins and then create your own plugins that do customizations on top.
33+
For instance, `Operator-SDK` does with the plugins [manifest][operator-sdk-manifest] and [scorecard][operator-sdk-scorecard] to add its features.
34+
Also see [here][operator-sdk-plugin-ref].
35+
36+
Another option implemented with the [Extensible CLI and Scaffolding Plugins - Phase 2][plugins-phase2-design-doc] is
37+
to extend Kibebuilder as a LIB to create only a specific plugin that can be called and used with
38+
Kubebuilder as well.
39+
40+
<aside class="note">
41+
<H1> Plugins proposal docs</H1>
42+
43+
You can check the proposal documentation for better understanding its motivations. See the [Extensible CLI and Scaffolding Plugins: phase 1][plugins-phase1-design-doc],
44+
the [Extensible CLI and Scaffolding Plugins: phase 1.5][plugins-phase1-design-doc-1.5] and the [Extensible CLI and Scaffolding Plugins - Phase 2][plugins-phase2-design-doc]
45+
design docs. Also, you can check the [Plugins section][plugins-section].
46+
47+
</aside>
48+
1549
## Language-based Plugins
1650

1751
Kubebuilder offers the Golang-based operator plugins, which will help its CLI tool users create projects following the [Operator Pattern][operator-pattern].
@@ -68,5 +102,97 @@ kubebuilder edit --plugins="grafana.kubebuilder.io/v1-alpha"
68102

69103
In this way, by [Extending the Kubebuilder CLI][extending-cli], you can also create custom plugins such this one.
70104

71-
[grafana]: https://github.com/kubernetes-sigs/kubebuilder/tree/v3.7.0/pkg/plugins/optional/grafana/v1alpha
105+
Feel free to check the implementation under:
106+
107+
- deploy-image: <https://github.com/kubernetes-sigs/kubebuilder/tree/v3.7.0/pkg/plugins/golang/deploy-image/v1alpha1>
108+
- grafana: <https://github.com/kubernetes-sigs/kubebuilder/tree/v3.7.0/pkg/plugins/optional/grafana/v1alpha>
109+
110+
## Plugin Scaffolding
111+
112+
Your plugin may add code on top of what is scaffolded by default with Kubebuilder sub-commands(`init`, `create`, ...).
113+
This is common as you may expect your plugin to:
114+
115+
- Create API
116+
- Update controller manager logic
117+
- Generate corresponding manifests
118+
119+
### Boilerplates
120+
121+
The Kubebuilder internal plugins use boilerplates to generate the files of code.
122+
123+
For instance, the go/v3 scaffolds the `main.go` file by defining an object that [implements the machinery interface][kubebuilder-machinery].
124+
In the [implementation][go-v3-settemplatedefault] of `Template.SetTemplateDefaults`, the [raw template][go-v3-main-template] is set to the body.
125+
Such object that implements the machinery interface will later pass to the [execution of scaffold][go-v3-scaffold-execute].
126+
127+
Similar, you may also design your code of plugin implementation by such reference.
128+
You can also view the other parts of the code file given by the links above.
129+
130+
If your plugin is expected to modify part of the existing files with its scaffold, you may use functions provided by [sigs.k8s.io/kubebuilder/v3/pkg/plugin/util][kb-util].
131+
See [example of deploy-image][example-of-deploy-image-2].
132+
In brief, the util package helps you customize your scaffold in a lower level.
133+
134+
### Use Kubebuilder Machinery Lib
135+
136+
Notice that Kubebuilder also provides [machinery pkg][kubebuilder-machinery-pkg] where you can:
137+
138+
- Define file I/O behavior.
139+
- Add markers to the scaffolded file.
140+
- Define the template for scaffolding.
141+
142+
#### Overwrite A File
143+
144+
You might want for example to overwrite a scaffold done by using the option:
145+
146+
```go
147+
f.IfExistsAction = machinery.OverwriteFile
148+
```
149+
150+
Let's imagine that you would like to have a helper plugin that would be called in a chain with `go/v4-alpha` to add customizations on top.
151+
Therefore after we generate the code calling the subcommand to `init` from `go/v4-alpha` we would like to overwrite the Makefile to change this scaffold via our plugin.
152+
In this way, we would implement the Bollerplate for our Makefile and then use this option to ensure that it would be overwritten.
153+
154+
See [example of deploy-image][example-of-deploy-image-1].
155+
156+
### A Combination of Multiple Plugins
157+
158+
Since your plugin may work frequently with other plugins, the executing command for scaffolding may become cumbersome, e.g:
159+
160+
```shell
161+
kubebuilder create api --plugins=go/v3,kustomize/v1,yourplugin/v1
162+
```
163+
164+
You can probably define a method to your scaffolder that calls the plugin scaffolding method in order.
165+
See [example of deploy-image][example-of-deploy-image-3].
166+
167+
#### Define Plugin Bundles
168+
169+
Alternatively, you can create a plugin bundle to include the target plugins. For instance:
170+
171+
```go
172+
mylanguagev1Bundle, _ := plugin.NewBundle(language.DefaultNameQualifier, plugin.Version{Number: 1},
173+
kustomizecommonv1.Plugin{}, // extend the common base from Kuebebuilder
174+
mylanguagev1.Plugin{}, // your plugin language which will do the scaffolds for the specific language on top of the common base
175+
)
176+
```
177+
178+
[controller-runtime]: https://github.com/kubernetes-sigs/controller-runtime
72179
[deploy-image]: https://github.com/kubernetes-sigs/kubebuilder/tree/v3.7.0/pkg/plugins/golang/deploy-image/v1alpha1
180+
[grafana]: https://github.com/kubernetes-sigs/kubebuilder/tree/v3.7.0/pkg/plugins/optional/grafana/v1alpha
181+
[extending-cli]: ./extending-cli.md
182+
[kb-util]: https://pkg.go.dev/sigs.k8s.io/kubebuilder/v3/pkg/plugin/util
183+
[example-of-deploy-image-1]: https://github.com/kubernetes-sigs/kubebuilder/blob/df1ed6ccf19df40bd929157a91eaae6a9215bfc6/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/api/types.go#L58
184+
[example-of-deploy-image-2]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/api.go#L170-L266
185+
[example-of-deploy-image-3]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/api.go#L77-L98
186+
[available-plugins]: https://github.com/kubernetes-sigs/kubebuilder/pull/available-plugins.md
187+
[operator-sdk-manifest]: https://github.com/operator-framework/operator-sdk/tree/v1.23.0/internal/plugins/manifests/v2
188+
[operator-sdk-scorecard]: https://github.com/operator-framework/operator-sdk/tree/v1.23.0/internal/plugins/scorecard/v2
189+
[operator-sdk-plugin-ref]: https://github.com/operator-framework/operator-sdk/blob/v1.23.0/internal/cmd/operator-sdk/cli/cli.go#L78-L160
190+
[plugins-section]: ???
191+
[plugins-phase1-design-doc]: https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/designs/extensible-cli-and-scaffolding-plugins-phase-1.md#extensible-cli-and-scaffolding-plugins
192+
[plugins-phase1-design-doc-1.5]: https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/designs/extensible-cli-and-scaffolding-plugins-phase-1-5.md#extensible-cli-and-scaffolding-plugins---phase-15
193+
[plugins-phase2-design-doc]: https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/designs/extensible-cli-and-scaffolding-plugins-phase-2.md#extensible-cli-and-scaffolding-plugins---phase-2
194+
[go-v3-main-template]: https://github.com/kubernetes-sigs/kubebuilder/blob/3bfc84ec8767fa760d1771ce7a0cb05a9a8f6286/pkg/plugins/golang/v3/scaffolds/internal/templates/main.go#L183
195+
[kubebuilder-machinery]: https://github.com/kubernetes-sigs/kubebuilder/blob/3bfc84ec8767fa760d1771ce7a0cb05a9a8f6286/pkg/plugins/golang/v3/scaffolds/internal/templates/main.go#L28
196+
[go-v3-settemplatedefault]: https://github.com/kubernetes-sigs/kubebuilder/blob/3bfc84ec8767fa760d1771ce7a0cb05a9a8f6286/pkg/plugins/golang/v3/scaffolds/internal/templates/main.go#L40
197+
[go-v3-scaffold-execute]: https://github.com/kubernetes-sigs/kubebuilder/blob/3bfc84ec8767fa760d1771ce7a0cb05a9a8f6286/pkg/plugins/golang/v3/scaffolds/init.go#L120
198+
[kubebuilder-machinery-pkg]: https://pkg.go.dev/sigs.k8s.io/kubebuilder/v3/pkg/machinery#section-documentation

0 commit comments

Comments
 (0)