diff --git a/Makefile b/Makefile index 1211c00f..67475a2b 100644 --- a/Makefile +++ b/Makefile @@ -160,8 +160,8 @@ install: download-external-crds manifests kustomize ## Install CRDs into the K8s .PHONY: download-external-crds download-external-crds: curl -s -L -o config/crd/external/subscriptions.eventing.kyma-project.io.crd.yaml https://raw.githubusercontent.com/kyma-project/kyma/main/installation/resources/crds/eventing/subscriptions.eventing.kyma-project.io.crd.yaml - curl -s -L -o config/crd/external/applications.applicationconnector.crd.yaml https://raw.githubusercontent.com/kyma-project/kyma/main/installation/resources/crds/application-connector/applications.applicationconnector.crd.yaml - curl -s -L -o config/crd/external/apirules.gateway.crd.yaml https://raw.githubusercontent.com/kyma-project/kyma/main/installation/resources/crds/api-gateway/apirules.gateway.crd.yaml + curl -s -L -o config/crd/for-tests/applications.applicationconnector.crd.yaml https://raw.githubusercontent.com/kyma-project/kyma/main/installation/resources/crds/application-connector/applications.applicationconnector.crd.yaml + curl -s -L -o config/crd/for-tests/apirules.gateway.crd.yaml https://raw.githubusercontent.com/kyma-project/kyma/main/installation/resources/crds/api-gateway/apirules.gateway.crd.yaml .PHONY: uninstall uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion. diff --git a/cmd/main.go b/cmd/main.go index 2d3acd81..ca98ca12 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -124,8 +124,8 @@ func main() { //nolint:funlen // main function needs to initialize many object HealthProbeBindAddress: opts.ProbeAddr, LeaderElection: enableLeaderElection, LeaderElectionID: leaderElectionID, - WebhookServer: webhook.NewServer(webhook.Options{Port: 9443}), - Cache: cache.Options{SyncPeriod: &opts.ReconcilePeriod}, + WebhookServer: webhook.NewServer(webhook.Options{Port: 9443}), + Cache: cache.Options{SyncPeriod: &opts.ReconcilePeriod}, Metrics: server.Options{BindAddress: opts.MetricsAddr}, }) if err != nil { diff --git a/config/crd/external/apirules.gateway.crd.yaml b/config/crd/for-tests/apirules.gateway.crd.yaml similarity index 100% rename from config/crd/external/apirules.gateway.crd.yaml rename to config/crd/for-tests/apirules.gateway.crd.yaml diff --git a/config/crd/external/applications.applicationconnector.crd.yaml b/config/crd/for-tests/applications.applicationconnector.crd.yaml similarity index 100% rename from config/crd/external/applications.applicationconnector.crd.yaml rename to config/crd/for-tests/applications.applicationconnector.crd.yaml diff --git a/config/crd/external/operator.kyma-project.io_nats.yaml b/config/crd/for-tests/operator.kyma-project.io_nats.yaml similarity index 100% rename from config/crd/external/operator.kyma-project.io_nats.yaml rename to config/crd/for-tests/operator.kyma-project.io_nats.yaml diff --git a/internal/controller/eventing/controller.go b/internal/controller/eventing/controller.go index b4cdbcb7..6f4d5bfe 100644 --- a/internal/controller/eventing/controller.go +++ b/internal/controller/eventing/controller.go @@ -515,8 +515,17 @@ func (r *Reconciler) handlePublisherProxy( } func (r *Reconciler) reconcileEventMeshBackend(ctx context.Context, eventing *eventingv1alpha1.Eventing, log *zap.SugaredLogger) (ctrl.Result, error) { + // check if APIRule CRD is installed. + isAPIRuleCRDEnabled, err := r.kubeClient.APIRuleCRDExists(ctx) + if err != nil { + return ctrl.Result{}, r.syncStatusWithSubscriptionManagerErr(ctx, eventing, err, log) + } else if !isAPIRuleCRDEnabled { + apiRuleMissingErr := errors.New("API-Gateway module is needed for EventMesh backend. APIRules CRD is not installed") + return ctrl.Result{}, r.syncStatusWithSubscriptionManagerErr(ctx, eventing, apiRuleMissingErr, log) + } + // Start the EventMesh subscription controller - err := r.reconcileEventMeshSubManager(ctx, eventing) + err = r.reconcileEventMeshSubManager(ctx, eventing) if err != nil { return ctrl.Result{}, r.syncStatusWithSubscriptionManagerErr(ctx, eventing, err, log) } diff --git a/internal/controller/eventing/controller_test.go b/internal/controller/eventing/controller_test.go index 429835ee..fe41b8d2 100644 --- a/internal/controller/eventing/controller_test.go +++ b/internal/controller/eventing/controller_test.go @@ -3,9 +3,10 @@ package eventing import ( "errors" "fmt" - "github.com/stretchr/testify/mock" "testing" + "github.com/stretchr/testify/mock" + "github.com/kyma-project/eventing-manager/pkg/watcher" eventingv1alpha1 "github.com/kyma-project/eventing-manager/api/v1alpha1" diff --git a/internal/controller/eventing/integrationtests/controller/integration_test.go b/internal/controller/eventing/integrationtests/controller/integration_test.go index 56e353e7..b24305ee 100644 --- a/internal/controller/eventing/integrationtests/controller/integration_test.go +++ b/internal/controller/eventing/integrationtests/controller/integration_test.go @@ -42,7 +42,14 @@ func TestMain(m *testing.M) { // setup env test var err error - testEnvironment, err = testutils.NewTestEnvironment(projectRootDir, false, nil) + testEnvironment, err = testutils.NewTestEnvironment(testutils.TestEnvironmentConfig{ + ProjectRootDir: projectRootDir, + CELValidationEnabled: false, + APIRuleCRDEnabled: true, + ApplicationRuleCRDEnabled: true, + NATSCRDEnabled: true, + AllowedEventingCR: nil, + }) if err != nil { panic(err) } diff --git a/internal/controller/eventing/integrationtests/controller_switching/integration_test.go b/internal/controller/eventing/integrationtests/controller_switching/integration_test.go index d69dae7a..6cebfc2a 100644 --- a/internal/controller/eventing/integrationtests/controller_switching/integration_test.go +++ b/internal/controller/eventing/integrationtests/controller_switching/integration_test.go @@ -32,7 +32,14 @@ func TestMain(m *testing.M) { // setup env test var err error - testEnvironment, err = testutils.NewTestEnvironment(projectRootDir, false, nil) + testEnvironment, err = testutils.NewTestEnvironment(testutils.TestEnvironmentConfig{ + ProjectRootDir: projectRootDir, + CELValidationEnabled: false, + APIRuleCRDEnabled: true, + ApplicationRuleCRDEnabled: true, + NATSCRDEnabled: true, + AllowedEventingCR: nil, + }) if err != nil { panic(err) } diff --git a/internal/controller/eventing/integrationtests/controllersinglecr/integration_test.go b/internal/controller/eventing/integrationtests/controllersinglecr/integration_test.go index 555bdfbd..c0c2e38e 100644 --- a/internal/controller/eventing/integrationtests/controllersinglecr/integration_test.go +++ b/internal/controller/eventing/integrationtests/controllersinglecr/integration_test.go @@ -37,7 +37,14 @@ func TestMain(m *testing.M) { // setup env test var err error - testEnvironment, err = integrationutils.NewTestEnvironment(projectRootDir, false, givenAllowedEventingCR) + testEnvironment, err = integrationutils.NewTestEnvironment(integrationutils.TestEnvironmentConfig{ + ProjectRootDir: projectRootDir, + CELValidationEnabled: false, + APIRuleCRDEnabled: true, + ApplicationRuleCRDEnabled: true, + NATSCRDEnabled: true, + AllowedEventingCR: givenAllowedEventingCR, + }) if err != nil { panic(err) } diff --git a/internal/controller/eventing/integrationtests/validation/integration_test.go b/internal/controller/eventing/integrationtests/validation/integration_test.go index dd59d746..86a32588 100644 --- a/internal/controller/eventing/integrationtests/validation/integration_test.go +++ b/internal/controller/eventing/integrationtests/validation/integration_test.go @@ -83,7 +83,14 @@ func TestMain(m *testing.M) { // setup env test var err error - testEnvironment, err = integration.NewTestEnvironment(projectRootDir, true, nil) + testEnvironment, err = integration.NewTestEnvironment(integration.TestEnvironmentConfig{ + ProjectRootDir: projectRootDir, + CELValidationEnabled: true, + APIRuleCRDEnabled: true, + ApplicationRuleCRDEnabled: true, + NATSCRDEnabled: true, + AllowedEventingCR: nil, + }) if err != nil { panic(err) } diff --git a/internal/controller/eventing/integrationtests/without_apirule_crd/integration_test.go b/internal/controller/eventing/integrationtests/without_apirule_crd/integration_test.go new file mode 100644 index 00000000..15df3e46 --- /dev/null +++ b/internal/controller/eventing/integrationtests/without_apirule_crd/integration_test.go @@ -0,0 +1,103 @@ +package controller_switching + +import ( + "os" + "testing" + + eventingv1alpha1 "github.com/kyma-project/eventing-manager/api/v1alpha1" + "github.com/kyma-project/eventing-manager/test/matchers" + "github.com/kyma-project/eventing-manager/test/utils" + testutils "github.com/kyma-project/eventing-manager/test/utils/integration" + "github.com/onsi/gomega" + gomegatypes "github.com/onsi/gomega/types" +) + +const ( + projectRootDir = "../../../../../" +) + +var testEnvironment *testutils.TestEnvironment //nolint:gochecknoglobals // used in tests + +// TestMain pre-hook and post-hook to run before and after all tests. +func TestMain(m *testing.M) { + // Note: The setup will provision a single K8s env and + // all the tests need to create and use a separate namespace + + // setup env test + var err error + testEnvironment, err = testutils.NewTestEnvironment(testutils.TestEnvironmentConfig{ + ProjectRootDir: projectRootDir, + CELValidationEnabled: true, + APIRuleCRDEnabled: false, + ApplicationRuleCRDEnabled: true, + NATSCRDEnabled: true, + AllowedEventingCR: nil, + }) + if err != nil { + panic(err) + } + + // run tests + code := m.Run() + + // tear down test env + if err = testEnvironment.TearDown(); err != nil { + panic(err) + } + + os.Exit(code) +} + +// Test_EventMesh_APIRule_Dependency_Check tests that when the APIRule CRD is missing in case of EventMesh, +// the Eventing CR should have Error status with a message. The success case where APIRule exists is +// covered in integrationtests/controller/integration_test.go. +func Test_EventMesh_APIRule_Dependency_Check(t *testing.T) { + t.Parallel() + + testCases := []struct { + name string + givenEventing *eventingv1alpha1.Eventing + wantMatches gomegatypes.GomegaMatcher + }{ + { + name: "Eventing CR should error state due to APIRule CRD not available", + givenEventing: utils.NewEventingCR( + utils.WithEventMeshBackend("test-secret-name1"), + utils.WithEventingPublisherData(1, 1, "199m", "99Mi", "399m", "199Mi"), + utils.WithEventingEventTypePrefix("test-prefix"), + utils.WithEventingDomain(utils.Domain), + ), + wantMatches: gomega.And( + matchers.HaveStatusError(), + matchers.HaveEventMeshSubManagerNotReadyCondition( + "API-Gateway module is needed for EventMesh backend. APIRules CRD is not installed"), + ), + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + g := gomega.NewWithT(t) + + // given + // create unique namespace for this test run. + givenNamespace := tc.givenEventing.Namespace + testEnvironment.EnsureNamespaceCreation(t, givenNamespace) + + // create eventing-webhook-auth secret. + testEnvironment.EnsureOAuthSecretCreated(t, tc.givenEventing) + + // create EventMesh secret. + testEnvironment.EnsureEventMeshSecretCreated(t, tc.givenEventing) + + // when + // create Eventing CR. + testEnvironment.EnsureK8sResourceCreated(t, tc.givenEventing) + + // then + // check Eventing CR status. + testEnvironment.GetEventingAssert(g, tc.givenEventing).Should(tc.wantMatches) + }) + } +} diff --git a/internal/controller/eventing/mocks/controller.go b/internal/controller/eventing/mocks/controller.go index b78a62f1..38727445 100644 --- a/internal/controller/eventing/mocks/controller.go +++ b/internal/controller/eventing/mocks/controller.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.35.4. DO NOT EDIT. +// Code generated by mockery v2.30.1. DO NOT EDIT. package mocks diff --git a/internal/controller/eventing/mocks/manager.go b/internal/controller/eventing/mocks/manager.go index b16fb5e9..e06ec50e 100644 --- a/internal/controller/eventing/mocks/manager.go +++ b/internal/controller/eventing/mocks/manager.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.35.4. DO NOT EDIT. +// Code generated by mockery v2.30.1. DO NOT EDIT. package mocks @@ -129,49 +129,6 @@ func (_c *Manager_AddHealthzCheck_Call) RunAndReturn(run func(string, healthz.Ch return _c } -// AddMetricsExtraHandler provides a mock function with given fields: path, handler -func (_m *Manager) AddMetricsExtraHandler(path string, handler http.Handler) error { - ret := _m.Called(path, handler) - - var r0 error - if rf, ok := ret.Get(0).(func(string, http.Handler) error); ok { - r0 = rf(path, handler) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// Manager_AddMetricsExtraHandler_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'AddMetricsExtraHandler' -type Manager_AddMetricsExtraHandler_Call struct { - *mock.Call -} - -// AddMetricsExtraHandler is a helper method to define mock.On call -// - path string -// - handler http.Handler -func (_e *Manager_Expecter) AddMetricsExtraHandler(path interface{}, handler interface{}) *Manager_AddMetricsExtraHandler_Call { - return &Manager_AddMetricsExtraHandler_Call{Call: _e.mock.On("AddMetricsExtraHandler", path, handler)} -} - -func (_c *Manager_AddMetricsExtraHandler_Call) Run(run func(path string, handler http.Handler)) *Manager_AddMetricsExtraHandler_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string), args[1].(http.Handler)) - }) - return _c -} - -func (_c *Manager_AddMetricsExtraHandler_Call) Return(_a0 error) *Manager_AddMetricsExtraHandler_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *Manager_AddMetricsExtraHandler_Call) RunAndReturn(run func(string, http.Handler) error) *Manager_AddMetricsExtraHandler_Call { - _c.Call.Return(run) - return _c -} - // AddReadyzCheck provides a mock function with given fields: name, check func (_m *Manager) AddReadyzCheck(name string, check healthz.Checker) error { ret := _m.Called(name, check) diff --git a/internal/controller/eventing/mocks/nats_config_handler.go b/internal/controller/eventing/mocks/nats_config_handler.go index 7a9b699f..75479e12 100644 --- a/internal/controller/eventing/mocks/nats_config_handler.go +++ b/internal/controller/eventing/mocks/nats_config_handler.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.35.4. DO NOT EDIT. +// Code generated by mockery v2.30.1. DO NOT EDIT. package mocks diff --git a/internal/controller/subscription/eventmesh/test/utils.go b/internal/controller/subscription/eventmesh/test/utils.go index 3921f6fc..33f201a1 100644 --- a/internal/controller/subscription/eventmesh/test/utils.go +++ b/internal/controller/subscription/eventmesh/test/utils.go @@ -206,6 +206,7 @@ func startTestEnv() (*rest.Config, error) { CRDDirectoryPaths: []string{ filepath.Join("../../../../../", "config", "crd", "bases"), filepath.Join("../../../../../", "config", "crd", "external"), + filepath.Join("../../../../../", "config", "crd", "for-tests"), }, AttachControlPlaneOutput: attachControlPlaneOutput, UseExistingCluster: &useExistingCluster, diff --git a/internal/controller/subscription/eventmesh/testwebhookauth/utils.go b/internal/controller/subscription/eventmesh/testwebhookauth/utils.go index 18c734bb..2f5a85c4 100644 --- a/internal/controller/subscription/eventmesh/testwebhookauth/utils.go +++ b/internal/controller/subscription/eventmesh/testwebhookauth/utils.go @@ -201,6 +201,7 @@ func startTestEnv() (*rest.Config, error) { CRDDirectoryPaths: []string{ filepath.Join("../../../../../", "config", "crd", "bases"), filepath.Join("../../../../../", "config", "crd", "external"), + filepath.Join("../../../../../", "config", "crd", "for-tests"), }, AttachControlPlaneOutput: attachControlPlaneOutput, UseExistingCluster: utils.BoolPtr(useExistingCluster), diff --git a/internal/controller/subscription/eventmesh/testwithory/utils.go b/internal/controller/subscription/eventmesh/testwithory/utils.go index af798ed5..7147114a 100644 --- a/internal/controller/subscription/eventmesh/testwithory/utils.go +++ b/internal/controller/subscription/eventmesh/testwithory/utils.go @@ -205,6 +205,7 @@ func startTestEnv() (*rest.Config, error) { CRDDirectoryPaths: []string{ filepath.Join("../../../../../", "config", "crd", "bases"), filepath.Join("../../../../../", "config", "crd", "external"), + filepath.Join("../../../../../", "config", "crd", "for-tests"), }, AttachControlPlaneOutput: attachControlPlaneOutput, UseExistingCluster: &useExistingCluster, diff --git a/pkg/backend/jetstream/mocks/Backend.go b/pkg/backend/jetstream/mocks/Backend.go index c0965e78..ebfd5b94 100644 --- a/pkg/backend/jetstream/mocks/Backend.go +++ b/pkg/backend/jetstream/mocks/Backend.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.35.4. DO NOT EDIT. +// Code generated by mockery v2.30.1. DO NOT EDIT. package mocks diff --git a/pkg/backend/jetstream/mocks/JetStreamContext.go b/pkg/backend/jetstream/mocks/JetStreamContext.go index a180b0c0..e3cda65a 100644 --- a/pkg/backend/jetstream/mocks/JetStreamContext.go +++ b/pkg/backend/jetstream/mocks/JetStreamContext.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.35.4. DO NOT EDIT. +// Code generated by mockery v2.30.1. DO NOT EDIT. package mocks diff --git a/pkg/ems/api/events/client/mocks/PublisherManager.go b/pkg/ems/api/events/client/mocks/PublisherManager.go index ccb081a7..e2bea779 100644 --- a/pkg/ems/api/events/client/mocks/PublisherManager.go +++ b/pkg/ems/api/events/client/mocks/PublisherManager.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.35.4. DO NOT EDIT. +// Code generated by mockery v2.30.1. DO NOT EDIT. package mocks diff --git a/pkg/eventing/mocks/manager.go b/pkg/eventing/mocks/manager.go index d22a8ba2..668e0d87 100644 --- a/pkg/eventing/mocks/manager.go +++ b/pkg/eventing/mocks/manager.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.35.4. DO NOT EDIT. +// Code generated by mockery v2.30.1. DO NOT EDIT. package mocks diff --git a/pkg/k8s/client.go b/pkg/k8s/client.go index 4b3a5f08..c4e08cea 100644 --- a/pkg/k8s/client.go +++ b/pkg/k8s/client.go @@ -46,6 +46,7 @@ type Client interface { name string) (*admissionv1.ValidatingWebhookConfiguration, error) GetCRD(context.Context, string) (*apiextensionsv1.CustomResourceDefinition, error) ApplicationCRDExists(context.Context) (bool, error) + APIRuleCRDExists(context.Context) (bool, error) GetSubscriptions(ctx context.Context) (*eventingv1alpha2.SubscriptionList, error) GetConfigMap(ctx context.Context, name, namespace string) (*corev1.ConfigMap, error) } @@ -182,6 +183,14 @@ func (c *KubeClient) ApplicationCRDExists(ctx context.Context) (bool, error) { return true, nil } +func (c *KubeClient) APIRuleCRDExists(ctx context.Context) (bool, error) { + _, err := c.GetCRD(ctx, APIRuleCrdName) + if err != nil { + return false, client.IgnoreNotFound(err) + } + return true, nil +} + // GetMutatingWebHookConfiguration returns the MutatingWebhookConfiguration k8s resource. func (c *KubeClient) GetMutatingWebHookConfiguration(ctx context.Context, name string) (*admissionv1.MutatingWebhookConfiguration, error) { diff --git a/pkg/k8s/client_test.go b/pkg/k8s/client_test.go index 441646b6..893bba25 100644 --- a/pkg/k8s/client_test.go +++ b/pkg/k8s/client_test.go @@ -754,3 +754,47 @@ func Test_GetConfigMap(t *testing.T) { }) } } + +func Test_APIRuleCRDExists(t *testing.T) { + t.Parallel() + + // define test cases + testCases := []struct { + name string + wantResult bool + }{ + { + name: "should return false when CRD is missing in k8s", + wantResult: false, + }, + { + name: "should return true when CRD exists in k8s", + wantResult: true, + }, + } + + // run test cases + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + // given + var objs []runtime.Object + if tc.wantResult { + sampleCRD := testutils.NewAPIRuleCRD() + objs = append(objs, sampleCRD) + } + + fakeClientSet := apiclientsetfake.NewSimpleClientset(objs...) + kubeClient := NewKubeClient(nil, fakeClientSet, testFieldManager, nil) + + // when + gotResult, err := kubeClient.APIRuleCRDExists(context.Background()) + + // then + require.NoError(t, err) + require.Equal(t, tc.wantResult, gotResult) + }) + } +} diff --git a/pkg/k8s/mocks/client.go b/pkg/k8s/mocks/client.go index dd6bb785..7ac91d0f 100644 --- a/pkg/k8s/mocks/client.go +++ b/pkg/k8s/mocks/client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.35.4. DO NOT EDIT. +// Code generated by mockery v2.30.1. DO NOT EDIT. package mocks @@ -34,6 +34,58 @@ func (_m *Client) EXPECT() *Client_Expecter { return &Client_Expecter{mock: &_m.Mock} } +// APIRuleCRDExists provides a mock function with given fields: _a0 +func (_m *Client) APIRuleCRDExists(_a0 context.Context) (bool, error) { + ret := _m.Called(_a0) + + var r0 bool + var r1 error + if rf, ok := ret.Get(0).(func(context.Context) (bool, error)); ok { + return rf(_a0) + } + if rf, ok := ret.Get(0).(func(context.Context) bool); ok { + r0 = rf(_a0) + } else { + r0 = ret.Get(0).(bool) + } + + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(_a0) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Client_APIRuleCRDExists_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'APIRuleCRDExists' +type Client_APIRuleCRDExists_Call struct { + *mock.Call +} + +// APIRuleCRDExists is a helper method to define mock.On call +// - _a0 context.Context +func (_e *Client_Expecter) APIRuleCRDExists(_a0 interface{}) *Client_APIRuleCRDExists_Call { + return &Client_APIRuleCRDExists_Call{Call: _e.mock.On("APIRuleCRDExists", _a0)} +} + +func (_c *Client_APIRuleCRDExists_Call) Run(run func(_a0 context.Context)) *Client_APIRuleCRDExists_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context)) + }) + return _c +} + +func (_c *Client_APIRuleCRDExists_Call) Return(_a0 bool, _a1 error) *Client_APIRuleCRDExists_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *Client_APIRuleCRDExists_Call) RunAndReturn(run func(context.Context) (bool, error)) *Client_APIRuleCRDExists_Call { + _c.Call.Return(run) + return _c +} + // ApplicationCRDExists provides a mock function with given fields: _a0 func (_m *Client) ApplicationCRDExists(_a0 context.Context) (bool, error) { ret := _m.Called(_a0) diff --git a/pkg/k8s/types.go b/pkg/k8s/types.go index 2d261c9d..ef709454 100644 --- a/pkg/k8s/types.go +++ b/pkg/k8s/types.go @@ -7,4 +7,7 @@ const ( ApplicationKind string = "Application" // ApplicationAPIVersion defines the API version for Application of application-connector module. ApplicationAPIVersion string = "applicationconnector.kyma-project.io/v1alpha1" + + // APIRuleCrdName defines the CRD name for APIRule of kyma api-gateway module. + APIRuleCrdName string = "apirules.gateway.kyma-project.io" ) diff --git a/pkg/subscriptionmanager/manager/mocks/manager.go b/pkg/subscriptionmanager/manager/mocks/manager.go index b245da66..0557a647 100644 --- a/pkg/subscriptionmanager/manager/mocks/manager.go +++ b/pkg/subscriptionmanager/manager/mocks/manager.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.35.4. DO NOT EDIT. +// Code generated by mockery v2.30.1. DO NOT EDIT. package mocks diff --git a/pkg/subscriptionmanager/mocks/manager_factory.go b/pkg/subscriptionmanager/mocks/manager_factory.go index d672c9ff..af0bbbe6 100644 --- a/pkg/subscriptionmanager/mocks/manager_factory.go +++ b/pkg/subscriptionmanager/mocks/manager_factory.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.35.4. DO NOT EDIT. +// Code generated by mockery v2.30.1. DO NOT EDIT. package mocks diff --git a/pkg/watcher/mocks/watcher.go b/pkg/watcher/mocks/watcher.go index c8d133d4..3f34bb48 100644 --- a/pkg/watcher/mocks/watcher.go +++ b/pkg/watcher/mocks/watcher.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.35.4. DO NOT EDIT. +// Code generated by mockery v2.30.1. DO NOT EDIT. package mocks diff --git a/test/utils/integration/integration.go b/test/utils/integration/integration.go index 35ca9832..11ac5fe6 100644 --- a/test/utils/integration/integration.go +++ b/test/utils/integration/integration.go @@ -85,9 +85,17 @@ type TestEnvironment struct { JetStreamSubManager manager.Manager } +type TestEnvironmentConfig struct { + ProjectRootDir string + CELValidationEnabled bool + APIRuleCRDEnabled bool + ApplicationRuleCRDEnabled bool + NATSCRDEnabled bool + AllowedEventingCR *v1alpha1.Eventing +} + //nolint:funlen // Used in testing -func NewTestEnvironment(projectRootDir string, celValidationEnabled bool, - allowedEventingCR *v1alpha1.Eventing) (*TestEnvironment, error) { +func NewTestEnvironment(config TestEnvironmentConfig) (*TestEnvironment, error) { var err error // setup context ctx := context.Background() @@ -105,7 +113,7 @@ func NewTestEnvironment(projectRootDir string, celValidationEnabled bool, // Set controller core logger. ctrl.SetLogger(zapr.NewLogger(ctrLogger.WithContext().Desugar())) - testEnv, envTestKubeCfg, err := StartEnvTest(projectRootDir, celValidationEnabled) + testEnv, envTestKubeCfg, err := StartEnvTest(config) if err != nil { return nil, err } @@ -198,7 +206,7 @@ func NewTestEnvironment(projectRootDir string, celValidationEnabled bool, backendConfig, subManagerFactoryMock, opts, - allowedEventingCR, + config.AllowedEventingCR, ) if err = (eventingReconciler).SetupWithManager(ctrlMgr); err != nil { @@ -247,7 +255,7 @@ func NewTestEnvironment(projectRootDir string, celValidationEnabled bool, }, nil } -func StartEnvTest(projectRootDir string, celValidationEnabled bool) (*envtest.Environment, *rest.Config, error) { +func StartEnvTest(config TestEnvironmentConfig) (*envtest.Environment, *rest.Config, error) { // Reference: https://book.kubebuilder.io/reference/envtest.html useExistingCluster := useExistingCluster @@ -285,11 +293,26 @@ func StartEnvTest(projectRootDir string, celValidationEnabled bool) (*envtest.En }) vwh.Name = getTestBackendConfig().ValidatingWebhookName + // define CRDs to include. + includedCRDs := []string{ + filepath.Join(config.ProjectRootDir, "config", "crd", "bases"), + filepath.Join(config.ProjectRootDir, "config", "crd", "external"), + } + if config.ApplicationRuleCRDEnabled { + includedCRDs = append(includedCRDs, + filepath.Join(config.ProjectRootDir, "config", "crd", "for-tests", "applications.applicationconnector.crd.yaml")) + } + if config.APIRuleCRDEnabled { + includedCRDs = append(includedCRDs, + filepath.Join(config.ProjectRootDir, "config", "crd", "for-tests", "apirules.gateway.crd.yaml")) + } + if config.NATSCRDEnabled { + includedCRDs = append(includedCRDs, + filepath.Join(config.ProjectRootDir, "config", "crd", "for-tests", "operator.kyma-project.io_nats.yaml")) + } + testEnv := &envtest.Environment{ - CRDDirectoryPaths: []string{ - filepath.Join(projectRootDir, "config", "crd", "bases"), - filepath.Join(projectRootDir, "config", "crd", "external"), - }, + CRDDirectoryPaths: includedCRDs, ErrorIfCRDPathMissing: true, AttachControlPlaneOutput: attachControlPlaneOutput, UseExistingCluster: &useExistingCluster, @@ -300,7 +323,7 @@ func StartEnvTest(projectRootDir string, celValidationEnabled bool) (*envtest.En } args := testEnv.ControlPlane.GetAPIServer().Configure() - if celValidationEnabled { + if config.CELValidationEnabled { args.Set("feature-gates", "CustomResourceValidationExpressions=true") } else { args.Set("feature-gates", "CustomResourceValidationExpressions=false") diff --git a/test/utils/utils.go b/test/utils/utils.go index ec5e8c7c..17cd9ff6 100644 --- a/test/utils/utils.go +++ b/test/utils/utils.go @@ -3,11 +3,12 @@ package utils import ( "errors" "fmt" - eventinv1alpha2 "github.com/kyma-project/kyma/components/eventing-controller/api/v1alpha2" "math/rand" "reflect" "time" + eventinv1alpha2 "github.com/kyma-project/kyma/components/eventing-controller/api/v1alpha2" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" appsv1 "k8s.io/api/apps/v1" @@ -70,6 +71,25 @@ func NewApplicationCRD() *apiextensionsv1.CustomResourceDefinition { return result } +func NewAPIRuleCRD() *apiextensionsv1.CustomResourceDefinition { + result := &apiextensionsv1.CustomResourceDefinition{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "apiextensions.k8s.io/v1", + Kind: "CustomResourceDefinition", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "apirules.gateway.kyma-project.io", + }, + Spec: apiextensionsv1.CustomResourceDefinitionSpec{ + Names: apiextensionsv1.CustomResourceDefinitionNames{}, + Scope: "Namespaced", + PreserveUnknownFields: false, + }, + } + + return result +} + func NewEventingCR(opts ...EventingOption) *v1alpha1.Eventing { name := fmt.Sprintf(NameFormat, GetRandString(randomNameLen)) namespace := fmt.Sprintf(NamespaceFormat, GetRandString(randomNameLen))