Skip to content

Commit

Permalink
Check if APIRule CRD is installed, otherwise stop reconciliation for …
Browse files Browse the repository at this point in the history
…EventMesh (#194)

* code

* fixed lint issues

* added tests

* addressed review comments
  • Loading branch information
mfaizanse authored Oct 27, 2023
1 parent 0afa6bb commit c0eecb2
Show file tree
Hide file tree
Showing 31 changed files with 327 additions and 75 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion internal/controller/eventing/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/eventing/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
2 changes: 1 addition & 1 deletion internal/controller/eventing/mocks/controller.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 1 addition & 44 deletions internal/controller/eventing/mocks/manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/controller/eventing/mocks/nats_config_handler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/controller/subscription/eventmesh/test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/backend/jetstream/mocks/Backend.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/backend/jetstream/mocks/JetStreamContext.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/ems/api/events/client/mocks/PublisherManager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/eventing/mocks/manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions pkg/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit c0eecb2

Please sign in to comment.