Skip to content

Commit e4a0ca9

Browse files
docs: describe e2e test towards plugin
Co-authored-by: Camila Macedo <[email protected]>
1 parent 58a88d8 commit e4a0ca9

File tree

5 files changed

+194
-63
lines changed

5 files changed

+194
-63
lines changed

CONTRIBUTING.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,63 @@ Following the targets that can be used to test your changes locally.
6262

6363
**NOTE** To use the `make lint` is required to install `golangci-lint` locally. More info: https://github.com/golangci/golangci-lint#install
6464

65+
### Test Plugin
66+
67+
If your intended PR creates a new plugin, make sure the PR also provides test cases. Testing should include:
68+
69+
1. `e2e tests` to validate the behavior of the proposed plugin.
70+
2. `sample projects` to verify the scaffolded output from the plugin.
71+
72+
#### 1. Plugin E2E Tests
73+
74+
All the plugins provided by Kubebuilder should be validated through `e2e-tests` across multiple platforms.
75+
76+
Current Kubebuilder provides the testing framework that includes testing code based on [ginkgo](https://github.com/onsi/ginkgo), [Github Actions](https://github.com/Kavinjsir/kubebuilder/blob/docs%2Ftest-plugin/.github/workflows/testdata.yml) for unit tests, and multiple env tests driven by [test-infra](https://github.com/kubernetes/test-infra/blob/master/config/jobs/kubernetes-sigs/kubebuilder/kubebuilder-presubmits.yaml).
77+
78+
To fully test the proposed plugin:
79+
80+
1. Create a new package(folder) under `test/e2e/<your-plugin>`.
81+
2. Create [e2e_suite_test.go](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/test/e2e/v4/e2e_suite_test.go), which imports the necessary testing framework.
82+
3. Create `generate_test.go` ([ref](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/test/e2e/v4/generate_test.go)). That should:
83+
- Introduce/Receive a `TextContext` instance
84+
- Trigger the plugin's bound subcommands. See [Init](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/test/e2e/utils/test_context.go#L213), [CreateAPI](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.6.0/test/e2e/utils/test_context.go#L222)
85+
- Use [PluginUtil](https://pkg.go.dev/sigs.k8s.io/kubebuilder/v3/pkg/plugin/util) to verify the scaffolded outputs. See [InsertCode](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/pkg/plugin/util/util.go#L67), [ReplaceInFile](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.6.0/pkg/plugin/util/util.go#L196), [UncommendCode](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.6.0/pkg/plugin/util/util.go#L86)
86+
4. Create `plugin_cluster_test.go` ([ref](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/test/e2e/v4/plugin_cluster_test.go)). That should:
87+
88+
- 4.1. Setup testing environment, e.g:
89+
90+
- Cleanup environment, create temp dir. See [Prepare](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/test/e2e/utils/test_context.go#L97)
91+
- If your test will cover the provided features then, ensure that you install prerequisites CRDs: See [InstallCertManager](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/test/e2e/utils/test_context.go#L138), [InstallPrometheusManager](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.6.0/test/e2e/utils/test_context.go#L171)
92+
93+
- 4.2. Run the function from `generate_test.go`.
94+
95+
- 4.3. Further make sure the scaffolded output works, e.g:
96+
97+
- Execute commands in your `Makefile`. See [Make](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/test/e2e/utils/test_context.go#L240)
98+
- Temporary load image of the testing controller. See [LoadImageToKindCluster](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/test/e2e/utils/test_context.go#L283)
99+
- Call Kubectl to validate running resources. See [utils.Kubectl](https://pkg.go.dev/sigs.k8s.io/kubebuilder/v3/test/e2e/utils#Kubectl)
100+
101+
- 4.4. Delete temporary resources after testing exited, e.g:
102+
- Uninstall prerequisites CRDs: See [UninstallPrometheusOperManager](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/test/e2e/utils/test_context.go#L183)
103+
- Delete temp dir. See [Destroy](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/test/e2e/utils/test_context.go#L255)
104+
105+
5. Add the command in [test/e2e/plugin](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/test/e2e/setup.sh#L65) to run your testing code:
106+
107+
```shell
108+
go test $(dirname "$0")/<your-plugin-test-folder> $flags -timeout 30m
109+
```
110+
111+
#### 2. Sample Projects from the Plugin
112+
113+
It is also necessary to test consistency of the proposed plugin across different env and the integration with other plugins.
114+
115+
This is performed by generating sample projects based on the plugins. The CI workflow defined in Github Action would validate the availability and the consistency.
116+
117+
See:
118+
119+
- [test/testdata/generated.sh](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/test/testdata/generate.sh#L144)
120+
- [make generate](https://github.com/kubernetes-sigs/kubebuilder/blob/v3.7.0/Makefile#L70)
121+
65122
## PR Process
66123

67124
See [VERSIONING.md](VERSIONING.md) for a full description. TL;DR:

docs/book/src/SUMMARY.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- [Adding a new API](./cronjob-tutorial/new-api.md)
1717
- [Designing an API](./cronjob-tutorial/api-design.md)
1818

19-
- [A Brief Aside: What's the rest of this stuff?](./cronjob-tutorial/other-api-files.md)
19+
- [A Brief Aside: What's the rest of this stuff?](./cronjob-tutorial/other-api-files.md)
2020

2121
- [What's in a controller?](./cronjob-tutorial/controller-overview.md)
2222
- [Implementing a controller](./cronjob-tutorial/controller-implementation.md)
@@ -39,7 +39,7 @@
3939
- [Hubs, spokes, and other wheel metaphors](./multiversion-tutorial/conversion-concepts.md)
4040
- [Implementing conversion](./multiversion-tutorial/conversion.md)
4141

42-
- [and setting up the webhooks](./multiversion-tutorial/webhooks.md)
42+
- [and setting up the webhooks](./multiversion-tutorial/webhooks.md)
4343

4444
- [Deployment and Testing](./multiversion-tutorial/deployment.md)
4545

@@ -50,20 +50,22 @@
5050

5151
- [Using a custom type](./component-config-tutorial/custom-type.md)
5252

53-
- [Adding a new Config Type](./component-config-tutorial/config-type.md)
54-
- [Updating main](./component-config-tutorial/updating-main.md)
55-
- [Defining your Custom Config](./component-config-tutorial/define-custom-config.md)
53+
- [Adding a new Config Type](./component-config-tutorial/config-type.md)
54+
- [Updating main](./component-config-tutorial/updating-main.md)
55+
- [Defining your Custom Config](./component-config-tutorial/define-custom-config.md)
56+
5657
---
5758

5859
- [Migrations](./migrations.md)
5960

6061
- [Kubebuilder v1 vs v2](./migration/v1vsv2.md)
6162

62-
- [Migration Guide](./migration/legacy/migration_guide_v1tov2.md)
63+
- [Migration Guide](./migration/legacy/migration_guide_v1tov2.md)
6364

6465
- [Kubebuilder v2 vs v3](./migration/v2vsv3.md)
65-
- [Migration Guide](./migration/migration_guide_v2tov3.md)
66-
- [Migration by updating the files](./migration/manually_migration_guide_v2_v3.md)
66+
67+
- [Migration Guide](./migration/migration_guide_v2tov3.md)
68+
- [Migration by updating the files](./migration/manually_migration_guide_v2_v3.md)
6769

6870
- [Single Group to Multi-Group](./migration/multi-group.md)
6971

@@ -82,12 +84,12 @@
8284
- [Webhooks for Core Types](reference/webhook-for-core-types.md)
8385
- [Markers for Config/Code Generation](./reference/markers.md)
8486

85-
- [CRD Generation](./reference/markers/crd.md)
86-
- [CRD Validation](./reference/markers/crd-validation.md)
87-
- [CRD Processing](./reference/markers/crd-processing.md)
88-
- [Webhook](./reference/markers/webhook.md)
89-
- [Object/DeepCopy](./reference/markers/object.md)
90-
- [RBAC](./reference/markers/rbac.md)
87+
- [CRD Generation](./reference/markers/crd.md)
88+
- [CRD Validation](./reference/markers/crd-validation.md)
89+
- [CRD Processing](./reference/markers/crd-processing.md)
90+
- [Webhook](./reference/markers/webhook.md)
91+
- [Object/DeepCopy](./reference/markers/object.md)
92+
- [RBAC](./reference/markers/rbac.md)
9193

9294
- [controller-gen CLI](./reference/controller-gen.md)
9395
- [completion](./reference/completion.md)
@@ -97,7 +99,7 @@
9799

98100
- [Metrics](./reference/metrics.md)
99101

100-
- [Reference](./reference/metrics-reference.md)
102+
- [Reference](./reference/metrics-reference.md)
101103

102104
- [Makefile Helpers](./reference/makefile-helpers.md)
103105
- [Project config](./reference/project-config.md)
@@ -107,20 +109,21 @@
107109
- [Plugins][plugins]
108110

109111
- [Available Plugins](./plugins/available-plugins.md)
110-
- [go/v2 (Deprecated)](./plugins/go-v2-plugin.md)
111-
- [go/v3 (Default init scaffold)](./plugins/go-v3-plugin.md)
112-
- [go/v4-alpha](./plugins/go-v4-plugin.md)
113-
- [kustomize/v1](./plugins/kustomize-v1.md)
114-
- [kustomize/v2-alpha](./plugins/kustomize-v2-alpha.md)
115-
- [declarative/v1](./plugins/declarative-v1.md)
116-
- [grafana/v1-alpha](./plugins/grafana-v1-alpha.md)
117-
- [deploy-image/v1-alpha](./plugins/deploy-image-plugin-v1-alpha.md)
112+
- [go/v2 (Deprecated)](./plugins/go-v2-plugin.md)
113+
- [go/v3 (Default init scaffold)](./plugins/go-v3-plugin.md)
114+
- [go/v4-alpha](./plugins/go-v4-plugin.md)
115+
- [kustomize/v1](./plugins/kustomize-v1.md)
116+
- [kustomize/v2-alpha](./plugins/kustomize-v2-alpha.md)
117+
- [declarative/v1](./plugins/declarative-v1.md)
118+
- [grafana/v1-alpha](./plugins/grafana-v1-alpha.md)
119+
- [deploy-image/v1-alpha](./plugins/deploy-image-plugin-v1-alpha.md)
118120
- [Extending the CLI](./plugins/extending-cli.md)
119121
- [Creating your own plugins](./plugins/creating-plugins.md)
122+
- [Testing your own plugins](./plugins/testing-plugins.md)
120123
- [Plugins Versioning](./plugins/plugins-versioning.md)
121124

122-
---
123-
[Appendix: The TODO Landing Page](./TODO.md)
125+
---
124126

127+
[Appendix: The TODO Landing Page](./TODO.md)
125128

126129
[plugins]: ./plugins/plugins.md

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

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,54 +25,42 @@ If you do not want to develop your plugin using Golang, you can follow its stand
2525

2626
```sh
2727
kubebuilder init --plugins=kustomize
28-
```
28+
```
2929

3030
Then you can, for example, create your implementations for the sub-commands `create api` and `create webhook` using your language of preference.
3131

3232
<aside class="note">
3333
<h1>Why use the Kubebuilder style?</h1>
3434

35-
Kubebuilder and SDK are both broadly adopted projects which leverage the [controller-runtime][controller-runtime] project. They both allow users to build solutions using the [Operator Pattern][operator-pattern] and follow common standards.
35+
Kubebuilder and SDK are both broadly adopted projects which leverage the [controller-runtime][controller-runtime] project. They both allow users to build solutions using the [Operator Pattern][operator-pattern] and follow common standards.
3636

3737
Adopting these standards can bring significant benefits, such as joining forces on maintaining the common standards as the features provided by Kubebuilder and take advantage of the contributions made by the community. This allows you to focus on the specific needs and requirements for your plugin and use-case.
3838

3939
And then, you will also be able to use custom plugins and options currently or in the future which might to be provided by these projects as any other which decides to persuade the same standards.
4040

4141
</aside>
4242

43-
## Custom Plugins
43+
## Custom Plugins
4444

45-
Note that users are also able to use plugins to customize their scaffold and address specific needs. See that Kubebuilder provides the [declarative][declarative-code] plugin which can be used when for example an API is scaffold:
45+
Note that users are also able to use plugins to customize their scaffolds and address specific needs.
4646

47-
```sh
48-
kubebuider create api [options] --plugins=go/v3,declarative/v1
49-
```
47+
See that Kubebuilder provides the [deploy-image][deploy-image] plugin that allows the user to create the controller & CRs which will deploy and manage an image on the cluster:
5048

51-
This plugin will perform a custom scaffold using the [kubebuilder declarative pattern][kubebuilder-declarative-pattern].
49+
```sh
50+
kubebuilder create api --group example.com --version v1alpha1 --kind Memcached --image=memcached:1.6.15-alpine --image-container-command="memcached,-m=64,modern,-v" --image-container-port="11211" --run-as-user="1001" --plugins="deploy-image/v1-alpha"
51+
```
5252

53-
In this way, by [Extending the Kubebuilder CLI][extending-cli], you can also create custom plugins such this one. Feel free to check its implementation in [`pkg/plugins/golang/declarative`][declarative-code].
53+
This plugin will perform a custom scaffold following the [Operator Pattern](https://kubernetes.io/docs/concepts/extend-kubernetes/operator).
5454

55-
## Future vision for Kubebuilder Plugins
55+
Another example is the [grafana][grafana] plugin that scaffolds a new folder container manifests to visualize operator status on Grafana Web UI:
5656

57-
As the next steps for the plugins, its possible to highlight three initiatives so far, which are:
57+
```sh
58+
kubebuilder edit --plugins="grafana.kubebuilder.io/v1-alpha"
59+
```
5860

59-
- [Plugin phase 2.0][plugin-2.0]: allow the Kubebuilder CLI or any other CLI, which is [Extending the Kubebuilder CLI][extending-cli], to discover external plugins, in this way, allow the users to use these external options as helpers to perform the scaffolds with the tool.
60-
- [Config-gen][config-gen]: the config-gen option has been provided as an alpha option in the Kubebuilder CLI(`kubebuilder alpha config-gen`) to encourage its contributions. The idea of this option would simplify the config scaffold. For further information see its [README][config-gen-readme].
61-
- [New Plugin (`deploy-image.go.kubebuilder.io/v1beta1`) to generate code][new-plugin-gen]: its purpose is to provide an arch-type that will scaffold the APIs and Controllers with the required code to deploy and manage solutions on the cluster.
61+
In this way, by [Extending the Kubebuilder CLI][extending-cli], you can also create custom plugins such this one.
6262

63-
Please, feel to contribute with them as well. Your contribution to the project is very welcome.
63+
Feel free to check the implementation under:
6464

65-
[sdk-cli-pkg]: https://github.com/operator-framework/operator-sdk/blob/master/internal/cmd/operator-sdk/cli/cli.go
66-
[sdk-ansible]: https://github.com/operator-framework/operator-sdk/tree/master/internal/plugins/ansible/v1
67-
[sdk-helm]: https://github.com/operator-framework/operator-sdk/tree/master/internal/plugins/helm/v1
68-
[operator-pattern]: https://kubernetes.io/docs/concepts/extend-kubernetes/operator/
69-
[controller-runtime]: https://github.com/kubernetes-sigs/controller-runtime
70-
[plugin-2.0]: https://github.com/kubernetes-sigs/kubebuilder/issues/1378
71-
[config-gen-readme]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/cli/alpha/config-gen/README.md
72-
[config-gen]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/cli/alpha/config-gen
73-
[plugins-phase1-design-doc-1.5]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/designs/extensible-cli-and-scaffolding-plugins-phase-1-5.md
74-
[extending-cli]: extending-cli.md
75-
[new-plugin-gen]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/designs/code-generate-image-plugin.md
76-
[kubebuilder-declarative-pattern]: https://github.com/kubernetes-sigs/kubebuilder-declarative-pattern
77-
[declarative-code]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/plugins/golang/declarative
78-
[sdk]: https://github.com/operator-framework/operator-sdk
65+
- deploy-image: <https://github.com/kubernetes-sigs/kubebuilder/tree/v3.7.0/pkg/plugins/golang/deploy-image/v1alpha1>
66+
- grafana: <https://github.com/kubernetes-sigs/kubebuilder/tree/v3.7.0/pkg/plugins/optional/grafana/v1alpha>

docs/book/src/plugins/plugins.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This section details how to extend Kubebuilder and create your plugins following
1919
<aside class="note">
2020
<h1>Note</h1>
2121

22-
You can check the existing design proposal docs at [Extensible CLI and Scaffolding Plugins: phase 1][plugins-phase1-design-doc] and [Extensible CLI and Scaffolding Plugins: phase 1.5][plugins-phase1-design-doc-1.5] to know more on what is provided by Kubebuilder CLI and API currently.
22+
You can check the existing design proposal docs at [Extensible CLI and Scaffolding Plugins: phase 1][plugins-phase1-design-doc] and [Extensible CLI and Scaffolding Plugins: phase 1.5][plugins-phase1-design-doc-1.5] to know more on what is provided by Kubebuilder CLI and API currently.
2323

2424
</aside>
2525

@@ -30,12 +30,11 @@ To know more about Kubebuilder's future vision of the Plugins architecture, see
3030

3131
</aside>
3232

33-
- [Extending the CLI and Scaffolds](extending-cli.md)
34-
- [Creating your own plugins](creating-plugins.md)
33+
- [Extending the CLI and Scaffolds](extending-cli.md)
34+
- [Creating your own plugins](creating-plugins.md)
35+
- [Testing your plugins](testing-plugins.md)
3536

36-
[plugins-phase1-design-doc]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/designs/extensible-cli-and-scaffolding-plugins-phase-1.md
37-
[plugins-phase1-design-doc-1.5]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/designs/extensible-cli-and-scaffolding-plugins-phase-1-5.md
38-
[extending-cli]: extending-cli.md
39-
40-
41-
[section-future-vision-plugins]: https://book.kubebuilder.io/plugins/creating-plugins.html#future-vision-for-kubebuilder-plugins
37+
[plugins-phase1-design-doc]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/designs/extensible-cli-and-scaffolding-plugins-phase-1.md
38+
[plugins-phase1-design-doc-1.5]: https://github.com/kubernetes-sigs/kubebuilder/blob/master/designs/extensible-cli-and-scaffolding-plugins-phase-1-5.md
39+
[extending-cli]: extending-cli.md
40+
[section-future-vision-plugins]: https://book.kubebuilder.io/plugins/creating-plugins.html#future-vision-for-kubebuilder-plugins

0 commit comments

Comments
 (0)