Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into 66-eventing-default…
Browse files Browse the repository at this point in the history
…ing-values
  • Loading branch information
grischperl committed Oct 27, 2023
2 parents b2977a5 + c0eecb2 commit 689642b
Show file tree
Hide file tree
Showing 23 changed files with 319 additions and 24 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/cloudevents/sdk-go/v2 v2.14.0
github.com/go-logr/logr v1.2.4
github.com/go-logr/zapr v1.2.4
github.com/google/uuid v1.3.1
github.com/google/uuid v1.4.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/kyma-incubator/api-gateway v0.0.0-20220819093753-296e6704d413
github.com/kyma-project/kyma/common/logging v0.0.0-20231020092259-d58329d50da1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
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)
})
}
}
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
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
44 changes: 44 additions & 0 deletions pkg/k8s/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
52 changes: 52 additions & 0 deletions pkg/k8s/mocks/client.go

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

Loading

0 comments on commit 689642b

Please sign in to comment.