Skip to content

Commit

Permalink
Set Warning state if EventMes secret missing
Browse files Browse the repository at this point in the history
 * Set Warning state if EventMes secret missing
 * Fix failing tests
  • Loading branch information
muralov committed Dec 6, 2023
1 parent 17ad780 commit fb10b89
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 20 deletions.
4 changes: 2 additions & 2 deletions internal/controller/operator/eventing/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ func (r *Reconciler) reconcileNATSBackend(ctx context.Context, eventing *operato
// check nats CR if it exists and is in natsAvailable state
err = r.checkNATSAvailability(ctx, eventing)
if err != nil {
return kctrl.Result{}, r.syncStatusWithNATSState(ctx, operatorv1alpha1.StateWarning, eventing, err, log)
return kctrl.Result{}, r.syncStatusWithNATSErr(ctx, eventing, err, log)
}

// set NATSAvailable condition to true and update status
Expand Down Expand Up @@ -613,7 +613,7 @@ func (r *Reconciler) reconcileEventMeshBackend(ctx context.Context, eventing *op
}

// Start the EventMesh subscription controller
err = r.reconcileEventMeshSubManager(ctx, eventing)
err = r.reconcileEventMeshSubManager(ctx, eventing, log)
if err != nil {
return kctrl.Result{}, r.syncStatusWithSubscriptionManagerErr(ctx, eventing, err, log)
}
Expand Down
9 changes: 8 additions & 1 deletion internal/controller/operator/eventing/eventmesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ type oauth2Credentials struct {
certsURL []byte
}

func (r *Reconciler) reconcileEventMeshSubManager(ctx context.Context, eventing *v1alpha1.Eventing) error {
const EventMeshSecretMissingMessage = "the specified EventMesh secret is missing. Please provide the secret"

func (r *Reconciler) reconcileEventMeshSubManager(ctx context.Context, eventing *v1alpha1.Eventing,
log *zap.SugaredLogger) error {

Check failure on line 42 in internal/controller/operator/eventing/eventmesh.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
// gets oauth2ClientID and secret and stops the EventMesh subscription manager if changed
err := r.syncOauth2ClientIDAndSecret(ctx, eventing)
if err != nil {
Expand All @@ -45,6 +48,10 @@ func (r *Reconciler) reconcileEventMeshSubManager(ctx context.Context, eventing
// retrieve secret to authenticate with EventMesh
eventMeshSecret, err := r.kubeClient.GetSecret(ctx, eventing.Spec.Backend.Config.EventMeshSecret)
if err != nil {
if kerrors.IsNotFound(err) {
return r.syncStatusWithNATSState(ctx, v1alpha1.StateWarning, eventing,
errors.New(EventMeshSecretMissingMessage), log)
}
return errors.Errorf("failed to get EventMesh secret: %v", err)
}
// CreateOrUpdate deployment for publisher proxy secret
Expand Down
12 changes: 8 additions & 4 deletions internal/controller/operator/eventing/eventmesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ func Test_reconcileEventMeshSubManager(t *testing.T) {
testEnv := NewMockedUnitTestEnvironment(t, givenEventing, givenOauthSecret)
testEnv.Reconciler.backendConfig = *givenBackendConfig

logger := testEnv.Reconciler.logger.WithContext().Named(ControllerName)

// get mocks from test-case.
givenEventMeshSubManagerMock := tc.givenEventMeshSubManagerMock()
givenManagerFactoryMock := tc.givenManagerFactoryMock(givenEventMeshSubManagerMock)
Expand All @@ -296,15 +298,15 @@ func Test_reconcileEventMeshSubManager(t *testing.T) {
givenEventing.Status.BackendConfigHash = tc.givenHashBefore

// when
err := testEnv.Reconciler.reconcileEventMeshSubManager(ctx, givenEventing)
err := testEnv.Reconciler.reconcileEventMeshSubManager(ctx, givenEventing, logger)
if err != nil && tc.givenShouldRetry {
// This is to test the scenario where initialization of eventMeshSubManager was successful but
// starting the eventMeshSubManager failed. So on next try it should again try to start the eventMeshSubManager.
err = testEnv.Reconciler.reconcileEventMeshSubManager(ctx, givenEventing)
err = testEnv.Reconciler.reconcileEventMeshSubManager(ctx, givenEventing, logger)
}
if tc.givenUpdateTest {
// Run reconcile again with newBackendConfig:
err = testEnv.Reconciler.reconcileEventMeshSubManager(ctx, givenEventing)
err = testEnv.Reconciler.reconcileEventMeshSubManager(ctx, givenEventing, logger)
require.NoError(t, err)
}

Expand Down Expand Up @@ -433,6 +435,8 @@ func Test_reconcileEventMeshSubManager_ReadClusterDomain(t *testing.T) {
testEnv := NewMockedUnitTestEnvironment(t, tc.givenEventing, givenOauthSecret)
testEnv.Reconciler.backendConfig = *givenBackendConfig

logger := testEnv.Reconciler.logger.WithContext().Named(ControllerName)

givenEventMeshSubManagerMock := tc.givenEventMeshSubManagerMock()
givenManagerFactoryMock := tc.givenManagerFactoryMock(givenEventMeshSubManagerMock)
givenEventingManagerMock := tc.givenEventingManagerMock()
Expand All @@ -448,7 +452,7 @@ func Test_reconcileEventMeshSubManager_ReadClusterDomain(t *testing.T) {
testEnv.Reconciler.eventMeshSubManager = nil

// when
err := testEnv.Reconciler.reconcileEventMeshSubManager(ctx, tc.givenEventing)
err := testEnv.Reconciler.reconcileEventMeshSubManager(ctx, tc.givenEventing, logger)

// then
require.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
kapiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
client "sigs.k8s.io/controller-runtime/pkg/client"

Check failure on line 19 in internal/controller/operator/eventing/integrationtests/controller/integration_test.go

View workflow job for this annotation

GitHub Actions / lint

import "sigs.k8s.io/controller-runtime/pkg/client" has alias "client" which is not part of config (importas)

eventingv1alpha2 "github.com/kyma-project/eventing-manager/api/eventing/v1alpha2"
operatorv1alpha1 "github.com/kyma-project/eventing-manager/api/operator/v1alpha1"
Expand Down Expand Up @@ -87,7 +88,7 @@ func Test_CreateEventingCR_NATS(t *testing.T) {
),
givenNATSReady: false,
wantMatches: gomega.And(
matchers.HaveStatusWarning(),
matchers.HaveStatusError(),
matchers.HaveNATSNotAvailableCondition(),
matchers.HaveFinalizer(),
),
Expand Down Expand Up @@ -163,8 +164,8 @@ func Test_CreateEventingCR_NATS(t *testing.T) {
),
wantMatches: gomega.And(
matchers.HaveStatusWarning(),
matchers.HaveBackendNotAvailableConditionWith(eventingv1alpha1.ConditionBackendNotSpecifiedMessage,
eventingv1alpha1.ConditionReasonBackendNotSpecified),
matchers.HaveBackendNotAvailableConditionWith(operatorv1alpha1.ConditionBackendNotSpecifiedMessage,
operatorv1alpha1.ConditionReasonBackendNotSpecified),
matchers.HaveFinalizer(),
),
},
Expand Down Expand Up @@ -590,12 +591,13 @@ func Test_WatcherEventingCRK8sObjects(t *testing.T) {

func Test_CreateEventingCR_EventMesh(t *testing.T) {
testCases := []struct {
name string
givenEventing *operatorv1alpha1.Eventing
givenDeploymentReady bool
shouldFailSubManager bool
wantMatches gomegatypes.GomegaMatcher
wantEnsureK8sObjects bool
name string
givenEventing *operatorv1alpha1.Eventing
givenDeploymentReady bool
shouldFailSubManager bool
shouldEventMeshSecretNotFound bool
wantMatches gomegatypes.GomegaMatcher
wantEnsureK8sObjects bool
}{
{
name: "Eventing CR should have error state when subscription manager is not ready",
Expand All @@ -607,11 +609,26 @@ func Test_CreateEventingCR_EventMesh(t *testing.T) {
wantMatches: gomega.And(
matchers.HaveStatusError(),
matchers.HaveEventMeshSubManagerNotReadyCondition(
"failed to get EventMesh secret: Secret \"test-secret-name1\" not found"),
"failed to sync Publisher Proxy secret: unexpected error"),
matchers.HaveFinalizer(),
),
shouldFailSubManager: true,
},
{
name: "Eventing CR should have warning state when EventMesh secret is missing",
givenEventing: utils.NewEventingCR(
utils.WithEventMeshBackend("test-secret-name2"),
utils.WithEventingPublisherData(1, 1, "199m", "99Mi", "399m", "199Mi"),
utils.WithEventingEventTypePrefix("test-prefix"),
),
wantMatches: gomega.And(
matchers.HaveStatusWarning(),
matchers.HaveEventMeshSubManagerNotReadyCondition(
eventingcontroller.EventMeshSecretMissingMessage),
matchers.HaveFinalizer(),
),
shouldEventMeshSecretNotFound: true,
},
{
name: "Eventing CR should have ready state when all deployment replicas are ready",
givenEventing: utils.NewEventingCR(
Expand Down Expand Up @@ -664,18 +681,30 @@ func Test_CreateEventingCR_EventMesh(t *testing.T) {
// create eventing-webhook-auth secret.
testEnvironment.EnsureOAuthSecretCreated(t, tc.givenEventing)

if !tc.shouldFailSubManager {
if !tc.shouldEventMeshSecretNotFound {
// create EventMesh secret.
testEnvironment.EnsureEventMeshSecretCreated(t, tc.givenEventing)
}

originalKubeClient := testEnvironment.KubeClient
if tc.shouldFailSubManager {
mockedKubeClient := &MockKubeClient{
Client: originalKubeClient,
}
testEnvironment.KubeClient = mockedKubeClient
testEnvironment.Reconciler.SetKubeClient(mockedKubeClient)
}

// when
// create Eventing CR.
testEnvironment.EnsureK8sResourceCreated(t, tc.givenEventing)

defer func() {
testEnvironment.KubeClient = originalKubeClient
testEnvironment.Reconciler.SetKubeClient(originalKubeClient)

testEnvironment.EnsureEventingResourceDeletion(t, tc.givenEventing.Name, givenNamespace)
if !*testEnvironment.EnvTestInstance.UseExistingCluster && !tc.shouldFailSubManager {
if !*testEnvironment.EnvTestInstance.UseExistingCluster && !tc.shouldFailSubManager && !tc.shouldEventMeshSecretNotFound {
testEnvironment.EnsureDeploymentDeletion(t, eventing.GetPublisherDeploymentName(*tc.givenEventing), givenNamespace)
}
testEnvironment.EnsureNamespaceDeleted(t, givenNamespace)
Expand Down Expand Up @@ -1077,3 +1106,7 @@ func (mkc *MockKubeClient) GetCRD(ctx context.Context, name string) (*kapiextens
}
return nil, notFoundError
}

func (mkc *MockKubeClient) PatchApply(ctx context.Context, object client.Object) error {
return fmt.Errorf("unexpected error")
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func Test_DeletionOfPublisherResourcesWhenNATSNotEnabled(t *testing.T) {
// then
// wait until Eventing CR status is Error.
testEnvironment.GetEventingAssert(g, givenEventing).Should(gomega.And(
matchers.HaveStatusError(),
matchers.HaveStatusWarning(),
matchers.HaveNATSNotAvailableConditionWith("NATS module has to be installed: customresourcedefinitions.apiextensions.k8s.io \"nats.operator.kyma-project.io\" not found"),
))

Expand Down

0 comments on commit fb10b89

Please sign in to comment.