Skip to content

Commit

Permalink
Adding installing steps for testing webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash Singh committed Oct 6, 2022
1 parent 88ef026 commit 5947ae5
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions docs/book/src/reference/envtest.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ You can use environment variables and/or flags to specify the `kubectl`,`api-ser
| `TEST_ASSET_KUBE_APISERVER`, `TEST_ASSET_ETCD`, `TEST_ASSET_KUBECTL` | paths to, respectively, api-server, etcd and kubectl binaries | Similar to `KUBEBUILDER_ASSETS`, but more granular. Point integration tests to use binaries other than the default ones. These environment variables can also be used to ensure specific tests run with expected versions of these binaries. |
| `KUBEBUILDER_CONTROLPLANE_START_TIMEOUT` and `KUBEBUILDER_CONTROLPLANE_STOP_TIMEOUT` | durations in format supported by [`time.ParseDuration`](https://golang.org/pkg/time/#ParseDuration) | Specify timeouts different from the default for the test control plane to (respectively) start and stop; any test run that exceeds them will fail. |
| `KUBEBUILDER_ATTACH_CONTROL_PLANE_OUTPUT` | boolean | Set to `true` to attach the control plane's stdout and stderr to os.Stdout and os.Stderr. This can be useful when debugging test failures, as output will include output from the control plane. |
| `TEST_ASSET_CERT_MANAGER` | path to directory | Point integration tests to a directory containing cert-manager binaries. Follow [cert-manager documentation](https://cert-manager.io/docs/installation/) to install it. |
| `TEST_ASSET_PROMETHEUS` | path to directory | Point integration tests to a directory containing prometheus binaries. Follow the guide to [install the prometheus](https://grafana.com/docs/grafana-cloud/kubernetes-monitoring/prometheus/prometheus_operator/#step-1--install-prometheus-operator). |

See that the `test` makefile target will ensure that all is properly setup when you are using it. However, if you would like to run the tests without use the Makefile targets, for example via an IDE, then you can set the environment variables directly in the code of your `suite_test.go`:

Expand All @@ -115,8 +113,6 @@ var _ = BeforeSuite(func(done Done) {
Expect(os.Setenv("TEST_ASSET_KUBECTL", "../bin/k8s/1.25.0-darwin-amd64/kubectl")).To(Succeed())
// OR
Expect(os.Setenv("KUBEBUILDER_ASSETS", "../bin/k8s/1.25.0-darwin-amd64")).To(Succeed())
Expect(os.Setenv("TEST_ASSET_CERT_MANAGER", "../bin/k8s/1.25.0-darwin-amd64/cert-manager")).To(Succeed())
Expect(os.Setenv("TEST_ASSET_PROMETHEUS", "../bin/k8s/1.25.0-darwin-amd64/prometheus")).To(Succeed())
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
testenv = &envtest.Environment{}
Expand All @@ -133,8 +129,6 @@ var _ = AfterSuite(func() {
Expect(os.Unsetenv("TEST_ASSET_KUBE_APISERVER")).To(Succeed())
Expect(os.Unsetenv("TEST_ASSET_ETCD")).To(Succeed())
Expect(os.Unsetenv("TEST_ASSET_KUBECTL")).To(Succeed())
Expect(os.Unsetenv("TEST_ASSET_CERT_MANAGER")).To(Succeed())
Expect(os.Unsetenv("TEST_ASSET_PROMETHEUS")).To(Succeed())
})
```
Expand Down Expand Up @@ -175,3 +169,17 @@ Expect(deployment.ObjectMeta.OwnerReferences).To(ContainElement(expectedOwnerRef

[envtest]:https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest
[setup-envtest]:https://pkg.go.dev/sigs.k8s.io/controller-runtime/tools/setup-envtest


## Cert-Manager and Prometheus options

EnvTest does also support the testing of `Cert-Manager` and `Prometheus`. To test these components one should install [cert-manager](https://book.kubebuilder.io/cronjob-tutorial/cert-manager.html) and [Prometheus Operator](https://book.kubebuilder.io/reference/metrics.html) in the existing cluster. Kubebuilder has the plugin `deploy-image/v1-alpha` which allows users to scaffold API/Controllers to deploy and manage an Operand (image) on the cluster. You can follow these steps to [Deploy the Image Plugin](https://book.kubebuilder.io/plugins/deploy-image-plugin-v1-alpha.html). Once the cluster step is complete and components are being installed then you can enable the webhooks to test them. The `controller-runtime/pkg/envtest` Go library provides an option to Install the Webhooks which you can read them [here](https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest#WebhookInstallOptions).

Examples of Installing the Webhooks:
```go
WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "..", "config", "webhook")},
},
```

Note: Follow this [example](https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v3-with-deploy-image/controllers/busybox_controller_test.go) to test the controller implementation.

0 comments on commit 5947ae5

Please sign in to comment.