Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time
Copyright 2023 The Kubernetes authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
// +kubebuilder:docs-gen:collapse=Apache License
When we created the CronJob API with `kubebuilder create api` in a [previous chapter](/cronjob-tutorial/, Kubebuilder already did some test work for you.
Kubebuilder scaffolded a `internal/controller/suite_test.go` file that does the bare bones of setting up a test environment.
First, it will contain the necessary imports.
package controller
import (
ctrl ""
. ""
. ""
logf ""
batchv1 ""
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// to learn more about Ginkgo.
// +kubebuilder:docs-gen:collapse=Imports
Now, let's go through the code generated.
var (
cfg *rest.Config
k8sClient client.Client // You'll be using this client in your tests.
testEnv *envtest.Environment
ctx context.Context
cancel context.CancelFunc
func TestControllers(t *testing.T) {
RunSpecs(t, "Controller Suite")
var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
ctx, cancel = context.WithCancel(context.TODO())
First, the envtest cluster is configured to read CRDs from the CRD directory Kubebuilder scaffolds for you.
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,
Then, we start the envtest cluster.
var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
The autogenerated test code will add the CronJob Kind schema to the default client-go k8s scheme.
This ensures that the CronJob API/Kind will be used in our test controller.
err = batchv1.AddToScheme(scheme.Scheme)
After the schemas, you will see the following marker.
This marker is what allows new schemas to be added here automatically when a new API is added to the project.
A client is created for our test CRUD operations.
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
One thing that this autogenerated file is missing, however, is a way to actually start your controller.
The code above will set up a client for interacting with your custom Kind,
but will not be able to test your controller behavior.
If you want to test your custom controller logic, you’ll need to add some familiar-looking manager logic
to your BeforeSuite() function, so you can register your custom controller to run on this test cluster.
You may notice that the code below runs your controller with nearly identical logic to your CronJob project’s main.go!
The only difference is that the manager is started in a separate goroutine so it does not block the cleanup of envtest
when you’re done running your tests.
Note that we set up both a "live" k8s client and a separate client from the manager. This is because when making
assertions in tests, you generally want to assert against the live state of the API server. If you use the client
from the manager (`k8sManager.GetClient`), you'd end up asserting against the contents of the cache instead, which is
slower and can introduce flakiness into your tests. We could use the manager's `APIReader` to accomplish the same
thing, but that would leave us with two clients in our test assertions and setup (one for reading, one for writing),
and it'd be easy to make mistakes.
Note that we keep the reconciler running against the manager's cache client, though -- we want our controller to
behave as it would in production, and we use features of the cache (like indicies) in our controller which aren't
available when talking directly to the API server.
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
err = (&CronJobReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
go func() {
defer GinkgoRecover()
err = k8sManager.Start(ctx)
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
Kubebuilder also generates boilerplate functions for cleaning up envtest and actually running your test files in your controllers/ directory.
You won't need to touch these.
var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Now that you have your controller running on a test cluster and a client ready to perform operations on your CronJob, we can start writing integration tests!