-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
What broke? What's expected?
The e2e tests should not be running by default!
I did a review of a project that uses kubebuilder, and I naiively thought I could just clone the repository and do go test ./...
But then the e2e tests started installing CRDs and stuff into a Kubernetes cluster I just happened to have selected in my ~/.kube/config.
I've tracked it to this part of the template:
kubebuilder/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/test.go
Lines 59 to 78 in e93492b
// Define a set of end-to-end (e2e) tests to validate the behavior of the controller. | |
var _ = Describe("controller", Ordered, func() { | |
// Before running the tests, set up the environment by creating the namespace, | |
// installing CRDs, and deploying the controller. | |
BeforeAll(func() { | |
By("creating manager namespace") | |
cmd := exec.Command("kubectl", "create", "ns", namespace) | |
_, err := utils.Run(cmd) | |
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to create namespace") | |
By("installing CRDs") | |
cmd = exec.Command("make", "install") | |
_, err = utils.Run(cmd) | |
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to install CRDs") | |
By("deploying the controller-manager") | |
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", projectImage)) | |
_, err = utils.Run(cmd) | |
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to deploy the controller-manager") | |
}) |
Had to spend some hours cleaning up, as the tests installed a bunch of other stuff too, like a Prometheus Operator and cert-manager.
What I suggest kubebuilder does is add a build tag to the top of these files, like so:
//go:build e2e
// +build e2e
And then the Makefile needs to be slightly adjusted:
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
test-e2e:
- go test ./test/e2e/ -v -ginkgo.v
+ go test ./test/e2e/ -v -tags e2e -ginkgo.v
That way, tests don't default to modifying your production infrastructure.
Reproducing this issue
- New project
- go test ./...
KubeBuilder (CLI) Version
Version: main.version{KubeBuilderVersion:"v4.1.1", KubernetesVendor:"unknown", GitCommit:"unknown", BuildDate:"unknown", GoOs:"linux", GoArch:"amd64"}
PROJECT version
3
Plugin versions
- go.kubebuilder.io/v4
Other versions
go version go1.23.0 linux/amd64
from go.mod
k8s.io/api v0.31.0
k8s.io/apimachinery v0.31.0
k8s.io/client-go v0.31.0
sigs.k8s.io/controller-runtime v0.19.0
$ kubectl version --client
Client Version: v1.30.2
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Extra Labels
No response