Skip to content

Commit

Permalink
fix: webhook testEnv
Browse files Browse the repository at this point in the history
Signed-off-by: Mateus Oliveira <[email protected]>
  • Loading branch information
mateusoliveira43 committed Dec 30, 2024
1 parent 6fb374f commit d540c85
Show file tree
Hide file tree
Showing 30 changed files with 259 additions and 323 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ var _ = BeforeSuite(func() {

ctx, cancel = context.WithCancel(context.TODO())

var err error
/*
The CronJob Kind is added to the runtime scheme used by the test environment.
This ensures that the CronJob API is registered with the scheme, allowing the test controller to recognize and interact with CronJob resources.
*/
err = batchv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
/*
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.
*/

// +kubebuilder:scaffold:scheme

/*
The envtest environment is configured to load Custom Resource Definitions (CRDs) from the specified directory.
This setup enables the test environment to recognize and interact with the custom resources defined by these CRDs.
Expand All @@ -92,25 +106,11 @@ var _ = BeforeSuite(func() {
Then, we start the envtest cluster.
*/

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

/*
The CronJob Kind is added to the runtime scheme used by the test environment.
This ensures that the CronJob API is registered with the scheme, allowing the test controller to recognize and interact with CronJob resources.
*/
err = batchv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
/*
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.
*/

// +kubebuilder:scaffold:scheme

/*
A client is created for our test CRUD operations.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

admissionv1 "k8s.io/api/admission/v1"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -66,6 +65,12 @@ var _ = BeforeSuite(func() {

ctx, cancel = context.WithCancel(context.TODO())

var err error
err = batchv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
Expand All @@ -81,29 +86,19 @@ var _ = BeforeSuite(func() {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

scheme := apimachineryruntime.NewScheme()
err = batchv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = admissionv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

// start webhook server using Manager.
webhookInstallOptions := &testEnv.WebhookInstallOptions
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
Scheme: scheme.Scheme,
WebhookServer: webhook.NewServer(webhook.Options{
Host: webhookInstallOptions.LocalServingHost,
Port: webhookInstallOptions.LocalServingPort,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ var _ = BeforeSuite(func() {

ctx, cancel = context.WithCancel(context.TODO())

var err error
err = cachev1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
Expand All @@ -69,17 +75,11 @@ var _ = BeforeSuite(func() {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

err = cachev1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ var _ = BeforeSuite(func() {

ctx, cancel = context.WithCancel(context.TODO())

var err error
/*
The CronJob Kind is added to the runtime scheme used by the test environment.
This ensures that the CronJob API is registered with the scheme, allowing the test controller to recognize and interact with CronJob resources.
*/
err = batchv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
/*
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.
*/

// +kubebuilder:scaffold:scheme

/*
The envtest environment is configured to load Custom Resource Definitions (CRDs) from the specified directory.
This setup enables the test environment to recognize and interact with the custom resources defined by these CRDs.
Expand All @@ -92,25 +106,11 @@ var _ = BeforeSuite(func() {
Then, we start the envtest cluster.
*/

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

/*
The CronJob Kind is added to the runtime scheme used by the test environment.
This ensures that the CronJob API is registered with the scheme, allowing the test controller to recognize and interact with CronJob resources.
*/
err = batchv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
/*
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.
*/

// +kubebuilder:scaffold:scheme

/*
A client is created for our test CRUD operations.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

admissionv1 "k8s.io/api/admission/v1"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -66,6 +65,12 @@ var _ = BeforeSuite(func() {

ctx, cancel = context.WithCancel(context.TODO())

var err error
err = batchv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
Expand All @@ -81,29 +86,19 @@ var _ = BeforeSuite(func() {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

scheme := apimachineryruntime.NewScheme()
err = batchv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = admissionv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

// start webhook server using Manager.
webhookInstallOptions := &testEnv.WebhookInstallOptions
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
Scheme: scheme.Scheme,
WebhookServer: webhook.NewServer(webhook.Options{
Host: webhookInstallOptions.LocalServingHost,
Port: webhookInstallOptions.LocalServingPort,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

admissionv1 "k8s.io/api/admission/v1"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -66,6 +65,12 @@ var _ = BeforeSuite(func() {

ctx, cancel = context.WithCancel(context.TODO())

var err error
err = batchv2.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
Expand All @@ -81,29 +86,19 @@ var _ = BeforeSuite(func() {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

scheme := apimachineryruntime.NewScheme()
err = batchv2.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = admissionv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

// start webhook server using Manager.
webhookInstallOptions := &testEnv.WebhookInstallOptions
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
Scheme: scheme.Scheme,
WebhookServer: webhook.NewServer(webhook.Options{
Host: webhookInstallOptions.LocalServingHost,
Port: webhookInstallOptions.LocalServingPort,
Expand Down
23 changes: 13 additions & 10 deletions hack/docs/internal/cronjob-tutorial/generate_cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,15 @@ var (
`, SuiteTestEnv)
hackutils.CheckError("updating suite_test.go to add more variables", err)

err = pluginutil.InsertCode(
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`ctx, cancel = context.WithCancel(context.TODO())
`, SuiteTestReadCRD)
hackutils.CheckError("updating suite_test.go to add text about CRD", err)
`
err = batchv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
// +kubebuilder:scaffold:scheme
`, SuiteTestAddSchema)
hackutils.CheckError("updating suite_test.go to add schema", err)

err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
Expand All @@ -552,15 +556,14 @@ var (
*/`)
hackutils.CheckError("updating suite_test.go to add text to show where envtest cluster start", err)

err = pluginutil.ReplaceInFile(
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
err = batchv1.AddToScheme(scheme.Scheme)
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
// +kubebuilder:scaffold:scheme
`, SuiteTestAddSchema)
hackutils.CheckError("updating suite_test.go to add schema", err)
Expect(cfg).NotTo(BeNil())
`, SuiteTestClient)
hackutils.CheckError("updating suite_test.go to add text about test client", err)

err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
Expand Down
12 changes: 6 additions & 6 deletions hack/docs/internal/cronjob-tutorial/writing_tests_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ var (
)
`

const SuiteTestReadCRD = `
/*
The envtest environment is configured to load Custom Resource Definitions (CRDs) from the specified directory.
This setup enables the test environment to recognize and interact with the custom resources defined by these CRDs.
*/`

const SuiteTestAddSchema = `
/*
The CronJob Kind is added to the runtime scheme used by the test environment.
Expand All @@ -63,6 +57,12 @@ const SuiteTestAddSchema = `
// +kubebuilder:scaffold:scheme
/*
The envtest environment is configured to load Custom Resource Definitions (CRDs) from the specified directory.
This setup enables the test environment to recognize and interact with the custom resources defined by these CRDs.
*/`

const SuiteTestClient = `
/*
A client is created for our test CRUD operations.
*/`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ var _ = BeforeSuite(func() {
ctx, cancel = context.WithCancel(context.TODO())
var err error
%s
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join({{ .CRDDirectoryRelativePath }}, "config", "crd", "bases")},
Expand All @@ -182,14 +185,11 @@ var _ = BeforeSuite(func() {
testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}
var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
%s
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
Expand Down
Loading

0 comments on commit d540c85

Please sign in to comment.