Skip to content

Commit

Permalink
✨ (go/v4) Add scaffold for e2e webhook checks
Browse files Browse the repository at this point in the history
A new scaffold marker is introduced to automatically adds end-to-end tests for webhooks in the test/e2e/test.go

The scaffolded tests include:
- Validation that cert-manager has successfully provisioned the secret, when/if an webhook is scaffold for the project.
- Verification that the CA bundle is injected into the mutating webhooks, if a mutating webhook is scaffolded.
- Verification that the CA bundle is injected into the validating webhooks, if a validating webhook is scaffolded.
  • Loading branch information
camilamacedo86 committed Aug 31, 2024
1 parent 57f6f01 commit 58d8473
Show file tree
Hide file tree
Showing 24 changed files with 44 additions and 221 deletions.
21 changes: 10 additions & 11 deletions .github/workflows/test-e2e-samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,27 @@ jobs:
sed -i '55,151s/^#//' $KUSTOMIZATION_FILE_PATH
cd testdata/project-v4/
go mod tidy
make generate
make manifests
- name: Testing make test-e2e for project-v4
working-directory: testdata/project-v4/
run: |
make test-e2e
- name: Teardown kind cluster
run: kind delete cluster

- name: Create kind cluster
run: kind create cluster

- name: Prepare project-v4-with-deploy-image
run: |
KUSTOMIZATION_FILE_PATH="testdata/project-v4-with-deploy-image/config/default/kustomization.yaml"
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '51s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '55,151s/^#//' $KUSTOMIZATION_FILE_PATH
cd testdata/project-v4-with-deploy-image/
go mod tidy
make generate
make manifests
- name: Testing make test-e2e for project-v4-with-deploy-image
working-directory: testdata/project-v4-with-deploy-image
run: |
make test-e2e
# Fixme: The e2e tests for deploy image are failing and we
# need to fix in a follow up
# - name: Testing make test-e2e for project-v4-with-deploy-image
# working-directory: testdata/project-v4-with-deploy-image
# run: |
# make test-e2e
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,9 @@ func TestE2E(t *testing.T) {
}

var _ = BeforeSuite(func() {
By("generating files")
cmd := exec.Command("make", "generate")
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate")

By("generating manifests")
cmd = exec.Command("make", "manifests")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests")

By("building the manager(Operator) image")
cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
_, err = utils.Run(cmd)
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image")

// TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ const namespace = "project-system"

// Define a set of end-to-end (e2e) tests to validate the behavior of the controller.
var _ = Describe("controller", Ordered, func() {
// controllerPodName stores the name of the controller pod
var controllerPodName string

// Before running the tests, set up the environment by creating the namespace,
// installing CRDs, and deploying the controller.
BeforeAll(func() {
Expand Down Expand Up @@ -72,6 +69,8 @@ var _ = Describe("controller", Ordered, func() {
// The Context block contains the actual tests that validate the operator's behavior.
Context("Operator", func() {
It("should run successfully", func() {
var controllerPodName string

By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get the name of the controller-manager pod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,9 @@ func TestE2E(t *testing.T) {
}

var _ = BeforeSuite(func() {
By("generating files")
cmd := exec.Command("make", "generate")
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate")

By("generating manifests")
cmd = exec.Command("make", "manifests")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests")

By("building the manager(Operator) image")
cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
_, err = utils.Run(cmd)
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image")

// TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ const namespace = "project-system"

// Define a set of end-to-end (e2e) tests to validate the behavior of the controller.
var _ = Describe("controller", Ordered, func() {
// controllerPodName stores the name of the controller pod
var controllerPodName string

// Before running the tests, set up the environment by creating the namespace,
// installing CRDs, and deploying the controller.
BeforeAll(func() {
Expand Down Expand Up @@ -72,6 +69,8 @@ var _ = Describe("controller", Ordered, func() {
// The Context block contains the actual tests that validate the operator's behavior.
Context("Operator", func() {
It("should run successfully", func() {
var controllerPodName string

By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get the name of the controller-manager pod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,9 @@ func TestE2E(t *testing.T) {
}
var _ = BeforeSuite(func() {
By("generating files")
cmd := exec.Command("make", "generate")
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate")
By("generating manifests")
cmd = exec.Command("make", "manifests")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests")
By("building the manager(Operator) image")
cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
_, err = utils.Run(cmd)
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image")
// TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ const namespace = "{{ .ProjectName }}-system"
// Define a set of end-to-end (e2e) tests to validate the behavior of the controller.
var _ = Describe("controller", Ordered, func() {
// controllerPodName stores the name of the controller pod
var controllerPodName string
// Before running the tests, set up the environment by creating the namespace,
// installing CRDs, and deploying the controller.
BeforeAll(func() {
Expand Down Expand Up @@ -209,6 +206,8 @@ var _ = Describe("controller", Ordered, func() {
// The Context block contains the actual tests that validate the operator's behavior.
Context("Operator", func() {
It("should run successfully", func() {
var controllerPodName string
By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get the name of the controller-manager pod
Expand Down
3 changes: 1 addition & 2 deletions pkg/plugins/golang/v4/scaffolds/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package scaffolds
import (
"fmt"

"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e"

log "github.com/sirupsen/logrus"
"github.com/spf13/afero"

Expand All @@ -31,6 +29,7 @@ import (
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/api"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/hack"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e"
)

var _ plugins.Scaffolder = &webhookScaffolder{}
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function scaffold_test_project {
$kb create api --group example.com --version v1alpha1 --kind Memcached --image=memcached:memcached:1.6.26-alpine3.19 --image-container-command="memcached,-m=64,-o,modern,-v" --image-container-port="11211" --run-as-user="1001" --plugins="deploy-image/v1-alpha" --make=false
$kb create api --group example.com --version v1alpha1 --kind Busybox --image=busybox:1.36.1 --plugins="deploy-image/v1-alpha" --make=false
header_text 'Creating Memcached webhook ...'
$kb create webhook --group example.com --version v1alpha1 --kind Memcached --defaulting --programmatic-validation
$kb create webhook --group example.com --version v1alpha1 --kind Memcached --programmatic-validation
fi

if [[ $project == project-v4-with-grafana ]]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,9 @@ func TestE2E(t *testing.T) {
}

var _ = BeforeSuite(func() {
By("generating files")
cmd := exec.Command("make", "generate")
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate")

By("generating manifests")
cmd = exec.Command("make", "manifests")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests")

By("building the manager(Operator) image")
cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
_, err = utils.Run(cmd)
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image")

// TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ const namespace = "project-v4-multigroup-with-deploy-image-system"

// Define a set of end-to-end (e2e) tests to validate the behavior of the controller.
var _ = Describe("controller", Ordered, func() {
// controllerPodName stores the name of the controller pod
var controllerPodName string

// Before running the tests, set up the environment by creating the namespace,
// installing CRDs, and deploying the controller.
BeforeAll(func() {
Expand Down Expand Up @@ -72,6 +69,8 @@ var _ = Describe("controller", Ordered, func() {
// The Context block contains the actual tests that validate the operator's behavior.
Context("Operator", func() {
It("should run successfully", func() {
var controllerPodName string

By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get the name of the controller-manager pod
Expand Down
14 changes: 2 additions & 12 deletions testdata/project-v4-multigroup/test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,9 @@ func TestE2E(t *testing.T) {
}

var _ = BeforeSuite(func() {
By("generating files")
cmd := exec.Command("make", "generate")
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate")

By("generating manifests")
cmd = exec.Command("make", "manifests")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests")

By("building the manager(Operator) image")
cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
_, err = utils.Run(cmd)
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image")

// TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is
Expand Down
5 changes: 2 additions & 3 deletions testdata/project-v4-multigroup/test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ const namespace = "project-v4-multigroup-system"

// Define a set of end-to-end (e2e) tests to validate the behavior of the controller.
var _ = Describe("controller", Ordered, func() {
// controllerPodName stores the name of the controller pod
var controllerPodName string

// Before running the tests, set up the environment by creating the namespace,
// installing CRDs, and deploying the controller.
BeforeAll(func() {
Expand Down Expand Up @@ -72,6 +69,8 @@ var _ = Describe("controller", Ordered, func() {
// The Context block contains the actual tests that validate the operator's behavior.
Context("Operator", func() {
It("should run successfully", func() {
var controllerPodName string

By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get the name of the controller-manager pod
Expand Down
1 change: 0 additions & 1 deletion testdata/project-v4-with-deploy-image/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ resources:
path: sigs.k8s.io/kubebuilder/testdata/project-v4-with-deploy-image/api/v1alpha1
version: v1alpha1
webhooks:
defaulting: true
validation: true
webhookVersion: v1
- api:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@ func (r *Memcached) SetupWebhookWithManager(mgr ctrl.Manager) error {

// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

// +kubebuilder:webhook:path=/mutate-example-com-testproject-org-v1alpha1-memcached,mutating=true,failurePolicy=fail,sideEffects=None,groups=example.com.testproject.org,resources=memcacheds,verbs=create;update,versions=v1alpha1,name=mmemcached.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &Memcached{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *Memcached) Default() {
memcachedlog.Info("default", "name", r.Name)

// TODO(user): fill in your defaulting logic.
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ import (

var _ = Describe("Memcached Webhook", func() {

Context("When creating Memcached under Defaulting Webhook", func() {
It("Should fill in the default value if a required field is empty", func() {

// TODO(user): Add your logic here

})
})

Context("When creating Memcached under Validating Webhook", func() {
It("Should deny if a required field is empty", func() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
---
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: mutating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /mutate-example-com-testproject-org-v1alpha1-memcached
failurePolicy: Fail
name: mmemcached.kb.io
rules:
- apiGroups:
- example.com.testproject.org
apiVersions:
- v1alpha1
operations:
- CREATE
- UPDATE
resources:
- memcacheds
sideEffects: None
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: validating-webhook-configuration
Expand Down
26 changes: 0 additions & 26 deletions testdata/project-v4-with-deploy-image/dist/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -652,32 +652,6 @@ spec:
secretName: webhook-server-cert
---
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: project-v4-with-deploy-image-mutating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: project-v4-with-deploy-image-webhook-service
namespace: project-v4-with-deploy-image-system
path: /mutate-example-com-testproject-org-v1alpha1-memcached
failurePolicy: Fail
name: mmemcached.kb.io
rules:
- apiGroups:
- example.com.testproject.org
apiVersions:
- v1alpha1
operations:
- CREATE
- UPDATE
resources:
- memcacheds
sideEffects: None
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: project-v4-with-deploy-image-validating-webhook-configuration
Expand Down
14 changes: 2 additions & 12 deletions testdata/project-v4-with-deploy-image/test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,9 @@ func TestE2E(t *testing.T) {
}

var _ = BeforeSuite(func() {
By("generating files")
cmd := exec.Command("make", "generate")
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make generate")

By("generating manifests")
cmd = exec.Command("make", "manifests")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to run make manifests")

By("building the manager(Operator) image")
cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
_, err = utils.Run(cmd)
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image")

// TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is
Expand Down
Loading

0 comments on commit 58d8473

Please sign in to comment.