From 8a98e196f9d95e2af731a8d5c09251e99320559b Mon Sep 17 00:00:00 2001 From: Danail Branekov Date: Fri, 20 Dec 2024 14:42:21 +0000 Subject: [PATCH] Simplify webhooks suites - Generate manifests only for the webhooks we are about to test - Do not wire in unrelated webhooks in the test suite Co-authored-by: Georgi Sabev --- controllers/api/v1alpha1/cforg_types_test.go | 76 --------------- .../api/v1alpha1/cfspace_types_test.go | 94 ------------------- .../api/v1alpha1/webhook_suite_test.go | 32 ++----- controllers/webhooks/finalizer/suite_test.go | 69 ++------------ .../webhooks/relationships/suite_test.go | 24 +---- controllers/webhooks/version/suite_test.go | 67 ++----------- .../webhooks/workloads/apps/suite_test.go | 17 ++-- .../webhooks/workloads/orgs/suite_test.go | 16 ++-- .../webhooks/workloads/packages/suite_test.go | 17 ++-- .../webhooks/workloads/spaces/suite_test.go | 21 ++--- .../webhooks/workloads/tasks/suite_test.go | 17 ++-- go.mod | 3 +- scripts/create-new-user.sh | 2 +- tests/helpers/webhooks.go | 29 ++++++ 14 files changed, 99 insertions(+), 385 deletions(-) delete mode 100644 controllers/api/v1alpha1/cforg_types_test.go delete mode 100644 controllers/api/v1alpha1/cfspace_types_test.go create mode 100644 tests/helpers/webhooks.go diff --git a/controllers/api/v1alpha1/cforg_types_test.go b/controllers/api/v1alpha1/cforg_types_test.go deleted file mode 100644 index 1e49a36f4..000000000 --- a/controllers/api/v1alpha1/cforg_types_test.go +++ /dev/null @@ -1,76 +0,0 @@ -package v1alpha1_test - -import ( - korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" - "github.com/google/uuid" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -var _ = Describe("CF Org", func() { - Describe("display name validation", func() { - var ( - cfOrg *korifiv1alpha1.CFOrg - createErr error - ) - - BeforeEach(func() { - cfOrg = &korifiv1alpha1.CFOrg{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: namespace, - Name: uuid.NewString(), - }, - Spec: korifiv1alpha1.CFOrgSpec{ - DisplayName: "org-name-" + uuid.NewString(), - }, - } - }) - - JustBeforeEach(func() { - createErr = adminClient.Create(ctx, cfOrg) - }) - - It("accepts a valid name", func() { - Expect(createErr).NotTo(HaveOccurred()) - }) - - When("an org with the same display name already exists", func() { - BeforeEach(func() { - Expect(adminClient.Create(ctx, &korifiv1alpha1.CFOrg{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: namespace, - Name: uuid.NewString(), - }, - Spec: korifiv1alpha1.CFOrgSpec{ - DisplayName: cfOrg.Spec.DisplayName, - }, - })).To(Succeed()) - }) - - It("fails", func() { - Expect(createErr).To(HaveOccurred()) - }) - }) - - When("name contains a space", func() { - BeforeEach(func() { - cfOrg.Spec.DisplayName = "hello there" - }) - - It("is allowed", func() { - Expect(createErr).NotTo(HaveOccurred()) - }) - }) - - When("display name contains disallowed characters", func() { - BeforeEach(func() { - cfOrg.Spec.DisplayName = "Nope\t\n\n" - }) - - It("fails", func() { - Expect(createErr).To(HaveOccurred()) - }) - }) - }) -}) diff --git a/controllers/api/v1alpha1/cfspace_types_test.go b/controllers/api/v1alpha1/cfspace_types_test.go deleted file mode 100644 index fccb9cec9..000000000 --- a/controllers/api/v1alpha1/cfspace_types_test.go +++ /dev/null @@ -1,94 +0,0 @@ -package v1alpha1_test - -import ( - "github.com/google/uuid" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -var _ = Describe("CF Space", func() { - Describe("display name validation", func() { - var ( - cfOrg *korifiv1alpha1.CFOrg - cfSpace *korifiv1alpha1.CFSpace - createErr error - ) - - BeforeEach(func() { - cfOrg = &korifiv1alpha1.CFOrg{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: namespace, - Name: uuid.NewString(), - }, - Spec: korifiv1alpha1.CFOrgSpec{ - DisplayName: uuid.NewString(), - }, - } - Expect(adminClient.Create(ctx, cfOrg)).To(Succeed()) - - Expect(adminClient.Create(ctx, &corev1.Namespace{ - ObjectMeta: metav1.ObjectMeta{Name: cfOrg.Name}, - })).To(Succeed()) - - cfSpace = &korifiv1alpha1.CFSpace{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: cfOrg.Name, - Name: uuid.NewString(), - }, - Spec: korifiv1alpha1.CFSpaceSpec{ - DisplayName: "space-name-" + uuid.NewString(), - }, - } - }) - - JustBeforeEach(func() { - createErr = adminClient.Create(ctx, cfSpace) - }) - - It("accepts a valid name", func() { - Expect(createErr).NotTo(HaveOccurred()) - }) - - When("a space with the same display name already exists", func() { - BeforeEach(func() { - Expect(adminClient.Create(ctx, &korifiv1alpha1.CFSpace{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: cfOrg.Name, - Name: uuid.NewString(), - }, - Spec: korifiv1alpha1.CFSpaceSpec{ - DisplayName: cfSpace.Spec.DisplayName, - }, - })).To(Succeed()) - }) - - It("fails", func() { - Expect(createErr).To(HaveOccurred()) - }) - }) - - When("name contains a space", func() { - BeforeEach(func() { - cfSpace.Spec.DisplayName = "hello there" - }) - - It("is allowed", func() { - Expect(createErr).NotTo(HaveOccurred()) - }) - }) - - When("display name contains disallowed characters", func() { - BeforeEach(func() { - cfSpace.Spec.DisplayName = "Nope\t\n\n" - }) - - It("fails", func() { - Expect(createErr).To(HaveOccurred()) - }) - }) - }) -}) diff --git a/controllers/api/v1alpha1/webhook_suite_test.go b/controllers/api/v1alpha1/webhook_suite_test.go index 99d7f840a..728a45fd4 100644 --- a/controllers/api/v1alpha1/webhook_suite_test.go +++ b/controllers/api/v1alpha1/webhook_suite_test.go @@ -19,29 +19,22 @@ package v1alpha1_test import ( "context" + "os" "path/filepath" "testing" "time" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" - "code.cloudfoundry.org/korifi/controllers/controllers/shared" "code.cloudfoundry.org/korifi/controllers/coordination" - "code.cloudfoundry.org/korifi/controllers/webhooks/finalizer" "code.cloudfoundry.org/korifi/controllers/webhooks/networking/domains" "code.cloudfoundry.org/korifi/controllers/webhooks/networking/routes" "code.cloudfoundry.org/korifi/controllers/webhooks/validation" - "code.cloudfoundry.org/korifi/controllers/webhooks/version" "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/apps" - "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/orgs" - packageswebhook "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/packages" - "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/spaces" "code.cloudfoundry.org/korifi/tests/helpers" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - admissionv1beta1 "k8s.io/api/admission/v1beta1" - coordinationv1 "k8s.io/api/coordination/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" @@ -79,11 +72,17 @@ var _ = BeforeSuite(func() { ctx, stopManager = context.WithCancel(context.TODO()) + webhookManifestsPath := helpers.GenerateWebhookManifest( + "code.cloudfoundry.org/korifi/controllers/api/v1alpha1", + ) + DeferCleanup(func() { + Expect(os.RemoveAll(filepath.Dir(webhookManifestsPath))).To(Succeed()) + }) testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "helm", "korifi", "controllers", "crds")}, ErrorIfCRDPathMissing: false, WebhookInstallOptions: envtest.WebhookInstallOptions{ - Paths: []string{filepath.Join("..", "..", "..", "helm", "korifi", "controllers", "manifests.yaml")}, + Paths: []string{webhookManifestsPath}, }, } @@ -92,13 +91,9 @@ var _ = BeforeSuite(func() { _, err := testEnv.Start() Expect(err).NotTo(HaveOccurred()) - Expect(corev1.AddToScheme(scheme.Scheme)).To(Succeed()) Expect(korifiv1alpha1.AddToScheme(scheme.Scheme)).To(Succeed()) - Expect(admissionv1beta1.AddToScheme(scheme.Scheme)).To(Succeed()) - Expect(coordinationv1.AddToScheme(scheme.Scheme)).To(Succeed()) k8sManager := helpers.NewK8sManager(testEnv, filepath.Join("helm", "korifi", "controllers", "role.yaml")) - Expect(shared.SetupIndexWithManager(k8sManager)).To(Succeed()) adminClient, stopClientCache = helpers.NewCachedClient(testEnv.Config) @@ -124,17 +119,6 @@ var _ = BeforeSuite(func() { Expect((&korifiv1alpha1.CFBuild{}).SetupWebhookWithManager(k8sManager)).To(Succeed()) - orgNameDuplicateValidator := validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, orgs.CFOrgEntityType)) - orgPlacementValidator := validation.NewPlacementValidator(uncachedClient, namespace) - Expect(orgs.NewValidator(orgNameDuplicateValidator, orgPlacementValidator).SetupWebhookWithManager(k8sManager)).To(Succeed()) - - spaceNameDuplicateValidator := validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, spaces.CFSpaceEntityType)) - spacePlacementValidator := validation.NewPlacementValidator(uncachedClient, namespace) - Expect(spaces.NewValidator(spaceNameDuplicateValidator, spacePlacementValidator).SetupWebhookWithManager(k8sManager)).To(Succeed()) - version.NewVersionWebhook("some-version").SetupWebhookWithManager(k8sManager) - finalizer.NewControllersFinalizerWebhook().SetupWebhookWithManager(k8sManager) - Expect(packageswebhook.NewValidator().SetupWebhookWithManager(k8sManager)).To(Succeed()) - Expect(adminClient.Create(ctx, &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: namespace, diff --git a/controllers/webhooks/finalizer/suite_test.go b/controllers/webhooks/finalizer/suite_test.go index 26d5348c2..63e98dea4 100644 --- a/controllers/webhooks/finalizer/suite_test.go +++ b/controllers/webhooks/finalizer/suite_test.go @@ -2,30 +2,17 @@ package finalizer_test import ( "context" + "os" "path/filepath" "testing" "time" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" - "code.cloudfoundry.org/korifi/controllers/controllers/shared" - "code.cloudfoundry.org/korifi/controllers/coordination" "code.cloudfoundry.org/korifi/controllers/webhooks/finalizer" - "code.cloudfoundry.org/korifi/controllers/webhooks/networking/domains" - "code.cloudfoundry.org/korifi/controllers/webhooks/networking/routes" - bindingswebhook "code.cloudfoundry.org/korifi/controllers/webhooks/services/bindings" - "code.cloudfoundry.org/korifi/controllers/webhooks/services/instances" - "code.cloudfoundry.org/korifi/controllers/webhooks/validation" - "code.cloudfoundry.org/korifi/controllers/webhooks/version" - "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/apps" - "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/orgs" - "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/packages" - "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/spaces" "code.cloudfoundry.org/korifi/tests/helpers" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - admissionv1beta1 "k8s.io/api/admission/v1beta1" - coordinationv1 "k8s.io/api/coordination/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" @@ -59,13 +46,19 @@ func TestWorkloadsWebhooks(t *testing.T) { var _ = BeforeSuite(func() { logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + webhookManifestsPath := helpers.GenerateWebhookManifest( + "code.cloudfoundry.org/korifi/controllers/webhooks/finalizer", + ) + DeferCleanup(func() { + Expect(os.RemoveAll(filepath.Dir(webhookManifestsPath))).To(Succeed()) + }) testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{ filepath.Join("..", "..", "..", "helm", "korifi", "controllers", "crds"), }, ErrorIfCRDPathMissing: true, WebhookInstallOptions: envtest.WebhookInstallOptions{ - Paths: []string{filepath.Join("..", "..", "..", "helm", "korifi", "controllers", "manifests.yaml")}, + Paths: []string{webhookManifestsPath}, }, } @@ -73,49 +66,13 @@ var _ = BeforeSuite(func() { Expect(err).NotTo(HaveOccurred()) Expect(korifiv1alpha1.AddToScheme(scheme.Scheme)).To(Succeed()) - Expect(admissionv1beta1.AddToScheme(scheme.Scheme)).To(Succeed()) - Expect(corev1.AddToScheme(scheme.Scheme)).To(Succeed()) - Expect(coordinationv1.AddToScheme(scheme.Scheme)).To(Succeed()) k8sManager := helpers.NewK8sManager(testEnv, filepath.Join("helm", "korifi", "controllers", "role.yaml")) - Expect(shared.SetupIndexWithManager(k8sManager)).To(Succeed()) adminClient, stopClientCache = helpers.NewCachedClient(testEnv.Config) finalizer.NewControllersFinalizerWebhook().SetupWebhookWithManager(k8sManager) - version.NewVersionWebhook("some-version").SetupWebhookWithManager(k8sManager) - Expect((&korifiv1alpha1.CFApp{}).SetupWebhookWithManager(k8sManager)).To(Succeed()) - - uncachedClient := helpers.NewUncachedClient(k8sManager.GetConfig()) - Expect(apps.NewValidator( - validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, apps.AppEntityType)), - ).SetupWebhookWithManager(k8sManager)).To(Succeed()) - - orgNameDuplicateValidator := validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, orgs.CFOrgEntityType)) - orgPlacementValidator := validation.NewPlacementValidator(uncachedClient, rootNamespace) - Expect(orgs.NewValidator(orgNameDuplicateValidator, orgPlacementValidator).SetupWebhookWithManager(k8sManager)).To(Succeed()) - - spaceNameDuplicateValidator := validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, spaces.CFSpaceEntityType)) - spacePlacementValidator := validation.NewPlacementValidator(uncachedClient, rootNamespace) - Expect(spaces.NewValidator(spaceNameDuplicateValidator, spacePlacementValidator).SetupWebhookWithManager(k8sManager)).To(Succeed()) - - Expect(domains.NewValidator(uncachedClient).SetupWebhookWithManager(k8sManager)).To(Succeed()) - - Expect((&korifiv1alpha1.CFPackage{}).SetupWebhookWithManager(k8sManager)).To(Succeed()) - - Expect((&korifiv1alpha1.CFRoute{}).SetupWebhookWithManager(k8sManager)).To(Succeed()) - Expect(routes.NewValidator( - validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, routes.RouteEntityType)), - rootNamespace, - uncachedClient, - ).SetupWebhookWithManager(k8sManager)).To(Succeed()) - Expect(packages.NewValidator().SetupWebhookWithManager(k8sManager)).To(Succeed()) - Expect(instances.NewValidator(validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, instances.ServiceInstanceEntityType))).SetupWebhookWithManager(k8sManager)).To(Succeed()) - Expect(bindingswebhook.NewCFServiceBindingValidator( - validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, bindingswebhook.ServiceBindingEntityType)), - ).SetupWebhookWithManager(k8sManager)).To(Succeed()) - stopManager = helpers.StartK8sManager(k8sManager) ctx := context.Background() @@ -124,16 +81,6 @@ var _ = BeforeSuite(func() { Name: rootNamespace, }, })).To(Succeed()) - - Expect(adminClient.Create(ctx, &korifiv1alpha1.CFDomain{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: rootNamespace, - Name: defaultDomainName, - }, - Spec: korifiv1alpha1.CFDomainSpec{ - Name: "my.domain", - }, - })).To(Succeed()) }) var _ = AfterSuite(func() { diff --git a/controllers/webhooks/relationships/suite_test.go b/controllers/webhooks/relationships/suite_test.go index 157e219a5..6ede2a812 100644 --- a/controllers/webhooks/relationships/suite_test.go +++ b/controllers/webhooks/relationships/suite_test.go @@ -2,9 +2,7 @@ package relationships_test import ( "context" - "fmt" "os" - "os/exec" "path/filepath" "testing" "time" @@ -15,7 +13,6 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/onsi/gomega/gexec" //lint:ignore ST1001 this is a test file admissionv1beta1 "k8s.io/api/admission/v1beta1" "k8s.io/client-go/kubernetes/scheme" "sigs.k8s.io/controller-runtime/pkg/client" @@ -44,7 +41,9 @@ func TestWorkloadsWebhooks(t *testing.T) { var _ = BeforeSuite(func() { logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) - webhookManifestsPath := generateWebhookManifest() + webhookManifestsPath := helpers.GenerateWebhookManifest( + "code.cloudfoundry.org/korifi/controllers/webhooks/relationships", + ) DeferCleanup(func() { Expect(os.RemoveAll(filepath.Dir(webhookManifestsPath))).To(Succeed()) }) @@ -82,20 +81,3 @@ var _ = AfterSuite(func() { stopManager() Expect(testEnv.Stop()).To(Succeed()) }) - -func generateWebhookManifest() string { - tmpDir, err := os.MkdirTemp("", "") - Expect(err).NotTo(HaveOccurred()) - - controllerGenSession, err := gexec.Start(exec.Command( - "controller-gen", - "paths=code.cloudfoundry.org/korifi/controllers/webhooks/relationships", - "webhook", - fmt.Sprintf("output:webhook:artifacts:config=%s", tmpDir), - ), GinkgoWriter, GinkgoWriter) - - Expect(err).NotTo(HaveOccurred()) - Eventually(controllerGenSession).Should(gexec.Exit(0)) - - return filepath.Join(tmpDir, "manifests.yaml") -} diff --git a/controllers/webhooks/version/suite_test.go b/controllers/webhooks/version/suite_test.go index 39532898d..2802ad50c 100644 --- a/controllers/webhooks/version/suite_test.go +++ b/controllers/webhooks/version/suite_test.go @@ -2,31 +2,17 @@ package version_test import ( "context" + "os" "path/filepath" "testing" "time" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" - "code.cloudfoundry.org/korifi/controllers/controllers/shared" - "code.cloudfoundry.org/korifi/controllers/coordination" "code.cloudfoundry.org/korifi/tests/helpers" - "code.cloudfoundry.org/korifi/controllers/webhooks/finalizer" - "code.cloudfoundry.org/korifi/controllers/webhooks/networking/domains" - "code.cloudfoundry.org/korifi/controllers/webhooks/networking/routes" - "code.cloudfoundry.org/korifi/controllers/webhooks/services/bindings" - "code.cloudfoundry.org/korifi/controllers/webhooks/services/instances" - "code.cloudfoundry.org/korifi/controllers/webhooks/validation" "code.cloudfoundry.org/korifi/controllers/webhooks/version" - "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/apps" - "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/orgs" - "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/packages" - "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/spaces" - "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/tasks" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - admissionv1beta1 "k8s.io/api/admission/v1beta1" - coordinationv1 "k8s.io/api/coordination/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" @@ -57,13 +43,19 @@ func TestWorkloadsWebhooks(t *testing.T) { var _ = BeforeSuite(func() { logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + webhookManifestsPath := helpers.GenerateWebhookManifest( + "code.cloudfoundry.org/korifi/controllers/webhooks/version", + ) + DeferCleanup(func() { + Expect(os.RemoveAll(filepath.Dir(webhookManifestsPath))).To(Succeed()) + }) testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{ filepath.Join("..", "..", "..", "helm", "korifi", "controllers", "crds"), }, ErrorIfCRDPathMissing: true, WebhookInstallOptions: envtest.WebhookInstallOptions{ - Paths: []string{filepath.Join("..", "..", "..", "helm", "korifi", "controllers", "manifests.yaml")}, + Paths: []string{webhookManifestsPath}, }, } @@ -71,56 +63,13 @@ var _ = BeforeSuite(func() { Expect(err).NotTo(HaveOccurred()) Expect(korifiv1alpha1.AddToScheme(scheme.Scheme)).To(Succeed()) - Expect(admissionv1beta1.AddToScheme(scheme.Scheme)).To(Succeed()) - Expect(corev1.AddToScheme(scheme.Scheme)).To(Succeed()) - Expect(coordinationv1.AddToScheme(scheme.Scheme)).To(Succeed()) k8sManager := helpers.NewK8sManager(testEnv, filepath.Join("helm", "korifi", "controllers", "role.yaml")) - Expect(shared.SetupIndexWithManager(k8sManager)).To(Succeed()) adminClient, stopClientCache = helpers.NewCachedClient(testEnv.Config) version.NewVersionWebhook("some-version").SetupWebhookWithManager(k8sManager) - // other required hooks - Expect((&korifiv1alpha1.CFApp{}).SetupWebhookWithManager(k8sManager)).To(Succeed()) - - uncachedClient := helpers.NewUncachedClient(k8sManager.GetConfig()) - orgNameDuplicateValidator := validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, orgs.CFOrgEntityType)) - orgPlacementValidator := validation.NewPlacementValidator(uncachedClient, rootNamespace) - Expect(orgs.NewValidator(orgNameDuplicateValidator, orgPlacementValidator).SetupWebhookWithManager(k8sManager)).To(Succeed()) - - spaceNameDuplicateValidator := validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, spaces.CFSpaceEntityType)) - spacePlacementValidator := validation.NewPlacementValidator(uncachedClient, rootNamespace) - Expect(spaces.NewValidator(spaceNameDuplicateValidator, spacePlacementValidator).SetupWebhookWithManager(k8sManager)).To(Succeed()) - - Expect(domains.NewValidator(uncachedClient).SetupWebhookWithManager(k8sManager)).To(Succeed()) - Expect(instances.NewValidator( - validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, instances.ServiceInstanceEntityType)), - ).SetupWebhookWithManager(k8sManager)).To(Succeed()) - Expect(apps.NewValidator( - validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, apps.AppEntityType)), - ).SetupWebhookWithManager(k8sManager)).To(Succeed()) - - Expect((&korifiv1alpha1.CFPackage{}).SetupWebhookWithManager(k8sManager)).To(Succeed()) - - Expect(tasks.NewValidator().SetupWebhookWithManager(k8sManager)).To(Succeed()) - - Expect(korifiv1alpha1.NewCFProcessDefaulter(128, 256, 60). - SetupWebhookWithManager(k8sManager)).To(Succeed()) - Expect((&korifiv1alpha1.CFBuild{}).SetupWebhookWithManager(k8sManager)).To(Succeed()) - Expect((&korifiv1alpha1.CFRoute{}).SetupWebhookWithManager(k8sManager)).To(Succeed()) - Expect(routes.NewValidator( - validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, routes.RouteEntityType)), - rootNamespace, - uncachedClient, - ).SetupWebhookWithManager(k8sManager)).To(Succeed()) - Expect(bindings.NewCFServiceBindingValidator( - validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, bindings.ServiceBindingEntityType)), - ).SetupWebhookWithManager(k8sManager)).To(Succeed()) - finalizer.NewControllersFinalizerWebhook().SetupWebhookWithManager(k8sManager) - Expect(packages.NewValidator().SetupWebhookWithManager(k8sManager)).To(Succeed()) - stopManager = helpers.StartK8sManager(k8sManager) Expect(adminClient.Create(context.Background(), &corev1.Namespace{ diff --git a/controllers/webhooks/workloads/apps/suite_test.go b/controllers/webhooks/workloads/apps/suite_test.go index f97f5832e..f2d85a6f9 100644 --- a/controllers/webhooks/workloads/apps/suite_test.go +++ b/controllers/webhooks/workloads/apps/suite_test.go @@ -2,18 +2,16 @@ package apps_test import ( "context" + "os" "path/filepath" "testing" "time" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" - "code.cloudfoundry.org/korifi/controllers/controllers/shared" "code.cloudfoundry.org/korifi/controllers/coordination" "code.cloudfoundry.org/korifi/tests/helpers" - "code.cloudfoundry.org/korifi/controllers/webhooks/finalizer" "code.cloudfoundry.org/korifi/controllers/webhooks/validation" - "code.cloudfoundry.org/korifi/controllers/webhooks/version" "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/apps" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" @@ -50,13 +48,19 @@ func TestWorkloadsWebhooks(t *testing.T) { var _ = BeforeSuite(func() { logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + webhookManifestsPath := helpers.GenerateWebhookManifest( + "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/apps", + ) + DeferCleanup(func() { + Expect(os.RemoveAll(filepath.Dir(webhookManifestsPath))).To(Succeed()) + }) testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{ filepath.Join("..", "..", "..", "..", "helm", "korifi", "controllers", "crds"), }, ErrorIfCRDPathMissing: true, WebhookInstallOptions: envtest.WebhookInstallOptions{ - Paths: []string{filepath.Join("..", "..", "..", "..", "helm", "korifi", "controllers", "manifests.yaml")}, + Paths: []string{webhookManifestsPath}, }, } @@ -65,10 +69,8 @@ var _ = BeforeSuite(func() { Expect(adminConfig).NotTo(BeNil()) Expect(korifiv1alpha1.AddToScheme(scheme.Scheme)).To(Succeed()) - Expect(corev1.AddToScheme(scheme.Scheme)).To(Succeed()) k8sManager := helpers.NewK8sManager(testEnv, filepath.Join("helm", "korifi", "controllers", "role.yaml")) - Expect(shared.SetupIndexWithManager(k8sManager)).To(Succeed()) adminNonSyncClient, err = client.New(testEnv.Config, client.Options{ Scheme: scheme.Scheme, @@ -77,9 +79,6 @@ var _ = BeforeSuite(func() { adminClient, stopClientCache = helpers.NewCachedClient(testEnv.Config) - version.NewVersionWebhook("some-version").SetupWebhookWithManager(k8sManager) - finalizer.NewControllersFinalizerWebhook().SetupWebhookWithManager(k8sManager) - (&apps.AppRevWebhook{}).SetupWebhookWithManager(k8sManager) uncachedClient := helpers.NewUncachedClient(k8sManager.GetConfig()) diff --git a/controllers/webhooks/workloads/orgs/suite_test.go b/controllers/webhooks/workloads/orgs/suite_test.go index fad4cec2c..a1c91b6f7 100644 --- a/controllers/webhooks/workloads/orgs/suite_test.go +++ b/controllers/webhooks/workloads/orgs/suite_test.go @@ -2,18 +2,16 @@ package orgs_test import ( "context" + "os" "path/filepath" "testing" "time" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" - "code.cloudfoundry.org/korifi/controllers/controllers/shared" "code.cloudfoundry.org/korifi/controllers/coordination" "code.cloudfoundry.org/korifi/tests/helpers" - "code.cloudfoundry.org/korifi/controllers/webhooks/finalizer" "code.cloudfoundry.org/korifi/controllers/webhooks/validation" - "code.cloudfoundry.org/korifi/controllers/webhooks/version" "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/orgs" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" @@ -52,13 +50,19 @@ var _ = BeforeSuite(func() { ctx = context.Background() + webhookManifestsPath := helpers.GenerateWebhookManifest( + "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/orgs", + ) + DeferCleanup(func() { + Expect(os.RemoveAll(filepath.Dir(webhookManifestsPath))).To(Succeed()) + }) testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{ filepath.Join("..", "..", "..", "..", "helm", "korifi", "controllers", "crds"), }, ErrorIfCRDPathMissing: true, WebhookInstallOptions: envtest.WebhookInstallOptions{ - Paths: []string{filepath.Join("..", "..", "..", "..", "helm", "korifi", "controllers", "manifests.yaml")}, + Paths: []string{webhookManifestsPath}, }, } @@ -70,7 +74,6 @@ var _ = BeforeSuite(func() { Expect(corev1.AddToScheme(scheme.Scheme)).To(Succeed()) k8sManager := helpers.NewK8sManager(testEnv, filepath.Join("helm", "korifi", "controllers", "role.yaml")) - Expect(shared.SetupIndexWithManager(k8sManager)).To(Succeed()) adminNonSyncClient, err = client.New(testEnv.Config, client.Options{ Scheme: scheme.Scheme, @@ -79,9 +82,6 @@ var _ = BeforeSuite(func() { adminClient, stopClientCache = helpers.NewCachedClient(testEnv.Config) - version.NewVersionWebhook("some-version").SetupWebhookWithManager(k8sManager) - finalizer.NewControllersFinalizerWebhook().SetupWebhookWithManager(k8sManager) - uncachedClient := helpers.NewUncachedClient(k8sManager.GetConfig()) rootNamespace = uuid.NewString() diff --git a/controllers/webhooks/workloads/packages/suite_test.go b/controllers/webhooks/workloads/packages/suite_test.go index 296efe5f0..efadab5cc 100644 --- a/controllers/webhooks/workloads/packages/suite_test.go +++ b/controllers/webhooks/workloads/packages/suite_test.go @@ -2,16 +2,14 @@ package packages_test import ( "context" + "os" "path/filepath" "testing" "time" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" - "code.cloudfoundry.org/korifi/controllers/controllers/shared" "code.cloudfoundry.org/korifi/tests/helpers" - "code.cloudfoundry.org/korifi/controllers/webhooks/finalizer" - "code.cloudfoundry.org/korifi/controllers/webhooks/version" "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/packages" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" @@ -48,13 +46,19 @@ func TestWorkloadsWebhooks(t *testing.T) { var _ = BeforeSuite(func() { logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + webhookManifestsPath := helpers.GenerateWebhookManifest( + "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/packages", + ) + DeferCleanup(func() { + Expect(os.RemoveAll(filepath.Dir(webhookManifestsPath))).To(Succeed()) + }) testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{ filepath.Join("..", "..", "..", "..", "helm", "korifi", "controllers", "crds"), }, ErrorIfCRDPathMissing: true, WebhookInstallOptions: envtest.WebhookInstallOptions{ - Paths: []string{filepath.Join("..", "..", "..", "..", "helm", "korifi", "controllers", "manifests.yaml")}, + Paths: []string{webhookManifestsPath}, }, } @@ -63,10 +67,8 @@ var _ = BeforeSuite(func() { Expect(adminConfig).NotTo(BeNil()) Expect(korifiv1alpha1.AddToScheme(scheme.Scheme)).To(Succeed()) - Expect(corev1.AddToScheme(scheme.Scheme)).To(Succeed()) k8sManager := helpers.NewK8sManager(testEnv, filepath.Join("helm", "korifi", "controllers", "role.yaml")) - Expect(shared.SetupIndexWithManager(k8sManager)).To(Succeed()) adminNonSyncClient, err = client.New(testEnv.Config, client.Options{ Scheme: scheme.Scheme, @@ -75,9 +77,6 @@ var _ = BeforeSuite(func() { adminClient, stopClientCache = helpers.NewCachedClient(testEnv.Config) - version.NewVersionWebhook("some-version").SetupWebhookWithManager(k8sManager) - finalizer.NewControllersFinalizerWebhook().SetupWebhookWithManager(k8sManager) - Expect(packages.NewValidator().SetupWebhookWithManager(k8sManager)).To(Succeed()) stopManager = helpers.StartK8sManager(k8sManager) diff --git a/controllers/webhooks/workloads/spaces/suite_test.go b/controllers/webhooks/workloads/spaces/suite_test.go index 622510d3b..876d77447 100644 --- a/controllers/webhooks/workloads/spaces/suite_test.go +++ b/controllers/webhooks/workloads/spaces/suite_test.go @@ -2,19 +2,16 @@ package spaces_test import ( "context" + "os" "path/filepath" "testing" "time" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" - "code.cloudfoundry.org/korifi/controllers/controllers/shared" "code.cloudfoundry.org/korifi/controllers/coordination" "code.cloudfoundry.org/korifi/tests/helpers" - "code.cloudfoundry.org/korifi/controllers/webhooks/finalizer" "code.cloudfoundry.org/korifi/controllers/webhooks/validation" - "code.cloudfoundry.org/korifi/controllers/webhooks/version" - "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/orgs" "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/spaces" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" @@ -53,13 +50,19 @@ var _ = BeforeSuite(func() { ctx = context.Background() + webhookManifestsPath := helpers.GenerateWebhookManifest( + `code.cloudfoundry.org/korifi/controllers/webhooks/workloads/spaces`, + ) + DeferCleanup(func() { + Expect(os.RemoveAll(filepath.Dir(webhookManifestsPath))).To(Succeed()) + }) testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{ filepath.Join("..", "..", "..", "..", "helm", "korifi", "controllers", "crds"), }, ErrorIfCRDPathMissing: true, WebhookInstallOptions: envtest.WebhookInstallOptions{ - Paths: []string{filepath.Join("..", "..", "..", "..", "helm", "korifi", "controllers", "manifests.yaml")}, + Paths: []string{webhookManifestsPath}, }, } @@ -71,7 +74,6 @@ var _ = BeforeSuite(func() { Expect(corev1.AddToScheme(scheme.Scheme)).To(Succeed()) k8sManager := helpers.NewK8sManager(testEnv, filepath.Join("helm", "korifi", "controllers", "role.yaml")) - Expect(shared.SetupIndexWithManager(k8sManager)).To(Succeed()) adminNonSyncClient, err = client.New(testEnv.Config, client.Options{ Scheme: scheme.Scheme, @@ -89,13 +91,6 @@ var _ = BeforeSuite(func() { uncachedClient := helpers.NewUncachedClient(k8sManager.GetConfig()) - version.NewVersionWebhook("some-version").SetupWebhookWithManager(k8sManager) - finalizer.NewControllersFinalizerWebhook().SetupWebhookWithManager(k8sManager) - - orgNameDuplicateValidator := validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, orgs.CFOrgEntityType)) - orgPlacementValidator := validation.NewPlacementValidator(uncachedClient, rootNamespace) - Expect(orgs.NewValidator(orgNameDuplicateValidator, orgPlacementValidator).SetupWebhookWithManager(k8sManager)).To(Succeed()) - spaceNameDuplicateValidator := validation.NewDuplicateValidator(coordination.NewNameRegistry(uncachedClient, spaces.CFSpaceEntityType)) spacePlacementValidator := validation.NewPlacementValidator(uncachedClient, rootNamespace) Expect(spaces.NewValidator(spaceNameDuplicateValidator, spacePlacementValidator).SetupWebhookWithManager(k8sManager)).To(Succeed()) diff --git a/controllers/webhooks/workloads/tasks/suite_test.go b/controllers/webhooks/workloads/tasks/suite_test.go index 274f5d4d1..c44604088 100644 --- a/controllers/webhooks/workloads/tasks/suite_test.go +++ b/controllers/webhooks/workloads/tasks/suite_test.go @@ -2,17 +2,15 @@ package tasks_test import ( "context" + "os" "path/filepath" "testing" "time" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" "code.cloudfoundry.org/korifi/controllers/config" - "code.cloudfoundry.org/korifi/controllers/controllers/shared" "code.cloudfoundry.org/korifi/tests/helpers" - "code.cloudfoundry.org/korifi/controllers/webhooks/finalizer" - "code.cloudfoundry.org/korifi/controllers/webhooks/version" "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/tasks" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" @@ -49,13 +47,19 @@ func TestWorkloadsWebhooks(t *testing.T) { var _ = BeforeSuite(func() { logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + webhookManifestsPath := helpers.GenerateWebhookManifest( + "code.cloudfoundry.org/korifi/controllers/webhooks/workloads/tasks", + ) + DeferCleanup(func() { + Expect(os.RemoveAll(filepath.Dir(webhookManifestsPath))).To(Succeed()) + }) testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{ filepath.Join("..", "..", "..", "..", "helm", "korifi", "controllers", "crds"), }, ErrorIfCRDPathMissing: true, WebhookInstallOptions: envtest.WebhookInstallOptions{ - Paths: []string{filepath.Join("..", "..", "..", "..", "helm", "korifi", "controllers", "manifests.yaml")}, + Paths: []string{webhookManifestsPath}, }, } @@ -64,10 +68,8 @@ var _ = BeforeSuite(func() { Expect(adminConfig).NotTo(BeNil()) Expect(korifiv1alpha1.AddToScheme(scheme.Scheme)).To(Succeed()) - Expect(corev1.AddToScheme(scheme.Scheme)).To(Succeed()) k8sManager := helpers.NewK8sManager(testEnv, filepath.Join("helm", "korifi", "controllers", "role.yaml")) - Expect(shared.SetupIndexWithManager(k8sManager)).To(Succeed()) adminNonSyncClient, err = client.New(testEnv.Config, client.Options{ Scheme: scheme.Scheme, @@ -76,9 +78,6 @@ var _ = BeforeSuite(func() { adminClient, stopClientCache = helpers.NewCachedClient(testEnv.Config) - version.NewVersionWebhook("some-version").SetupWebhookWithManager(k8sManager) - finalizer.NewControllersFinalizerWebhook().SetupWebhookWithManager(k8sManager) - Expect(tasks.NewDefaulter(config.CFProcessDefaults{ MemoryMB: 500, DiskQuotaMB: 512, diff --git a/go.mod b/go.mod index 232c38f06..40eb64034 100644 --- a/go.mod +++ b/go.mod @@ -12,12 +12,14 @@ require ( github.com/aws/aws-sdk-go-v2/config v1.28.7 github.com/aws/aws-sdk-go-v2/service/ecr v1.36.8 github.com/blendle/zapdriver v1.3.1 + github.com/bsm/gomega v1.26.0 github.com/buildpacks/pack v0.36.1 github.com/cloudfoundry/cf-test-helpers v1.0.1-0.20220603211108-d498b915ef74 github.com/distribution/distribution/v3 v3.0.0-rc.2 github.com/foomo/htpasswd v0.0.0-20200116085101-e3a90e78da9c github.com/go-chi/chi v4.1.2+incompatible github.com/go-logr/logr v1.4.2 + github.com/go-logr/stdr v1.2.2 github.com/go-resty/resty/v2 v2.16.2 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/google/go-containerregistry v0.20.2 @@ -52,7 +54,6 @@ require ( github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/distribution/reference v0.6.0 // indirect github.com/fxamacker/cbor/v2 v2.7.0 // indirect - github.com/go-logr/stdr v1.2.2 // indirect github.com/go-task/slim-sprig/v3 v3.0.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 // indirect github.com/hashicorp/golang-lru/arc/v2 v2.0.5 // indirect diff --git a/scripts/create-new-user.sh b/scripts/create-new-user.sh index 81fcd72b3..0642c807d 100755 --- a/scripts/create-new-user.sh +++ b/scripts/create-new-user.sh @@ -16,7 +16,7 @@ createCert() { echo "Error: 'openssl' is not installed or not available in PATH." >&2 exit 1 fi - + openssl req -new -newkey rsa:4096 \ -keyout "${priv_key_file}" \ -out "${csr_file}" \ diff --git a/tests/helpers/webhooks.go b/tests/helpers/webhooks.go new file mode 100644 index 000000000..76ae856c1 --- /dev/null +++ b/tests/helpers/webhooks.go @@ -0,0 +1,29 @@ +package helpers + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + + . "github.com/onsi/ginkgo/v2" //lint:ignore ST1001 this is a test file + . "github.com/onsi/gomega" //lint:ignore ST1001 this is a test file + "github.com/onsi/gomega/gexec" +) + +func GenerateWebhookManifest(path string) string { + tmpDir, err := os.MkdirTemp("", "") + Expect(err).NotTo(HaveOccurred()) + + controllerGenSession, err := gexec.Start(exec.Command( + "controller-gen", + "paths="+path, + "webhook", + fmt.Sprintf("output:webhook:artifacts:config=%s", tmpDir), + ), GinkgoWriter, GinkgoWriter) + + Expect(err).NotTo(HaveOccurred()) + Eventually(controllerGenSession).Should(gexec.Exit(0)) + + return filepath.Join(tmpDir, "manifests.yaml") +}