diff --git a/api/operator/v1alpha1/eventing_types.go b/api/operator/v1alpha1/eventing_types.go index c7b21be02..9e1df0ade 100644 --- a/api/operator/v1alpha1/eventing_types.go +++ b/api/operator/v1alpha1/eventing_types.go @@ -18,6 +18,8 @@ limitations under the License. package v1alpha1 import ( + "fmt" + kcorev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -92,6 +94,7 @@ type EventingStatus struct { ActiveBackend BackendType `json:"activeBackend"` BackendConfigHash int64 `json:"specHash"` State string `json:"state"` + PublisherService string `json:"publisherService"` Conditions []kmetav1.Condition `json:"conditions,omitempty"` } @@ -220,3 +223,8 @@ func (e *Eventing) SyncStatusActiveBackend() { func (e *Eventing) IsSpecBackendTypeChanged() bool { return e.Status.ActiveBackend != e.Spec.Backend.Type } + +// SyncPublisherService sets the PublisherService in the Eventing Status. +func (e *Eventing) SyncPublisherService(name, namespace string) { + e.Status.PublisherService = fmt.Sprintf("%s.%s", name, namespace) +} diff --git a/api/operator/v1alpha1/eventing_types_test.go b/api/operator/v1alpha1/eventing_types_test.go index a53e2e0fd..7c947f4d3 100644 --- a/api/operator/v1alpha1/eventing_types_test.go +++ b/api/operator/v1alpha1/eventing_types_test.go @@ -97,3 +97,37 @@ func TestIsSpecBackendTypeChanged(t *testing.T) { }) } } + +func TestSyncPublisherService(t *testing.T) { + // given + t.Parallel() + testCases := []struct { + name string + givenEventing *Eventing + givenServiceName string + givenServiceNamespace string + wantEventingStatus EventingStatus + }{ + { + name: "should set the correct publisher service in the status", + givenEventing: &Eventing{ + Status: EventingStatus{}, + }, + givenServiceName: "test-service", + givenServiceNamespace: "test-namespace", + wantEventingStatus: EventingStatus{ + PublisherService: "test-service.test-namespace", + }, + }, + } + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + // when + tc.givenEventing.SyncPublisherService(tc.givenServiceName, tc.givenServiceNamespace) + + // then + require.Equal(t, tc.wantEventingStatus, tc.givenEventing.Status) + }) + } +} diff --git a/config/crd/bases/operator.kyma-project.io_eventings.yaml b/config/crd/bases/operator.kyma-project.io_eventings.yaml index 67b7fc495..0ce2a433a 100644 --- a/config/crd/bases/operator.kyma-project.io_eventings.yaml +++ b/config/crd/bases/operator.kyma-project.io_eventings.yaml @@ -337,6 +337,8 @@ spec: - type type: object type: array + publisherService: + type: string specHash: format: int64 type: integer @@ -344,6 +346,7 @@ spec: type: string required: - activeBackend + - publisherService - specHash - state type: object diff --git a/internal/controller/operator/eventing/controller.go b/internal/controller/operator/eventing/controller.go index e9efdc71e..7e9192644 100644 --- a/internal/controller/operator/eventing/controller.go +++ b/internal/controller/operator/eventing/controller.go @@ -560,29 +560,32 @@ func (r *Reconciler) checkNATSAvailability(ctx context.Context, eventing *operat func (r *Reconciler) handlePublisherProxy( ctx context.Context, - eventing *operatorv1alpha1.Eventing, + eventingCR *operatorv1alpha1.Eventing, backendType operatorv1alpha1.BackendType, ) (*kappsv1.Deployment, error) { // get nats config with NATS server url var natsConfig *env.NATSConfig if backendType == operatorv1alpha1.NatsBackendType { var err error - natsConfig, err = r.natsConfigHandler.GetNatsConfig(ctx, *eventing) + natsConfig, err = r.natsConfigHandler.GetNatsConfig(ctx, *eventingCR) if err != nil { return nil, err } } // CreateOrUpdate deployment for eventing publisher proxy deployment - deployment, err := r.eventingManager.DeployPublisherProxy(ctx, eventing, natsConfig, backendType) + deployment, err := r.eventingManager.DeployPublisherProxy(ctx, eventingCR, natsConfig, backendType) if err != nil { return nil, err } // deploy publisher proxy resources. - if err = r.eventingManager.DeployPublisherProxyResources(ctx, eventing, deployment); err != nil { + if err = r.eventingManager.DeployPublisherProxyResources(ctx, eventingCR, deployment); err != nil { return deployment, err } + // Update the publisher service in the Eventing CR status after the publisher proxy resources are deployed. + eventingCR.SyncPublisherService(eventing.GetPublisherPublishServiceName(*eventingCR), eventingCR.Namespace) + return deployment, nil } diff --git a/internal/controller/operator/eventing/integrationtests/controller/integration_test.go b/internal/controller/operator/eventing/integrationtests/controller/integration_test.go index 422b9eb5c..1fd715058 100644 --- a/internal/controller/operator/eventing/integrationtests/controller/integration_test.go +++ b/internal/controller/operator/eventing/integrationtests/controller/integration_test.go @@ -214,6 +214,25 @@ func Test_CreateEventingCR_NATS(t *testing.T) { // check if webhook configurations are updated with correct CABundle. testEnvironment.EnsureCABundleInjectedIntoWebhooks(t) } + + // check PublisherService in the EventingCR status + eventingCR, err := testEnvironment.GetEventingFromK8s(tc.givenEventing.Name, givenNamespace) + require.NoError(t, err) + require.NotNil(t, eventingCR) + switch eventingCR.Status.State { + case operatorv1alpha1.StateReady, operatorv1alpha1.StateProcessing: + { + wantPublisherService := fmt.Sprintf( + "%s.%s", + eventing.GetPublisherPublishServiceName(*eventingCR), eventingCR.Namespace, + ) + require.Equal(t, wantPublisherService, eventingCR.Status.PublisherService) + } + default: + { + require.Equal(t, "", eventingCR.Status.PublisherService) + } + } }) } } @@ -274,12 +293,12 @@ func Test_UpdateEventingCR(t *testing.T) { }() // get Eventing CR. - eventing, err := testEnvironment.GetEventingFromK8s(tc.givenExistingEventing.Name, givenNamespace) + eventingCR, err := testEnvironment.GetEventingFromK8s(tc.givenExistingEventing.Name, givenNamespace) require.NoError(t, err) // when // update NATS CR. - newEventing := eventing.DeepCopy() + newEventing := eventingCR.DeepCopy() newEventing.Spec = tc.givenNewEventingForUpdate.Spec testEnvironment.EnsureK8sResourceUpdated(t, newEventing) @@ -287,6 +306,22 @@ func Test_UpdateEventingCR(t *testing.T) { testEnvironment.EnsureEventingSpecPublisherReflected(t, newEventing) testEnvironment.EnsureEventingReplicasReflected(t, newEventing) testEnvironment.EnsureDeploymentOwnerReferenceSet(t, tc.givenExistingEventing) + + // check PublisherService in the EventingCR status + switch eventingCR.Status.State { + case operatorv1alpha1.StateReady, operatorv1alpha1.StateProcessing: + { + wantPublisherService := fmt.Sprintf( + "%s.%s", + eventing.GetPublisherPublishServiceName(*eventingCR), eventingCR.Namespace, + ) + require.Equal(t, wantPublisherService, eventingCR.Status.PublisherService) + } + default: + { + require.Equal(t, "", eventingCR.Status.PublisherService) + } + } }) } } @@ -680,6 +715,25 @@ func Test_CreateEventingCR_EventMesh(t *testing.T) { // check if webhook configurations are updated with correct CABundle. testEnvironment.EnsureCABundleInjectedIntoWebhooks(t) } + + // check PublisherService in the EventingCR status + eventingCR, err := testEnvironment.GetEventingFromK8s(tc.givenEventing.Name, givenNamespace) + require.NoError(t, err) + require.NotNil(t, eventingCR) + switch eventingCR.Status.State { + case operatorv1alpha1.StateReady, operatorv1alpha1.StateProcessing: + { + wantPublisherService := fmt.Sprintf( + "%s.%s", + eventing.GetPublisherPublishServiceName(*eventingCR), eventingCR.Namespace, + ) + require.Equal(t, wantPublisherService, eventingCR.Status.PublisherService) + } + default: + { + require.Equal(t, "", eventingCR.Status.PublisherService) + } + } }) } }