Skip to content

Commit

Permalink
Set warning state for empty backend
Browse files Browse the repository at this point in the history
Make state warning and BackendAvailable condition false for empty backend config
  • Loading branch information
muralov committed Nov 30, 2023
1 parent 8d19027 commit 443bf68
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 25 deletions.
10 changes: 6 additions & 4 deletions api/operator/v1alpha1/eventing_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
StateProcessing string = "Processing"
StateWarning string = "Warning"

ConditionNATSAvailable ConditionType = "NATSAvailable"
ConditionBackendAvailable ConditionType = "BackendAvailable"
ConditionPublisherProxyReady ConditionType = "PublisherProxyReady"
ConditionWebhookReady ConditionType = "WebhookReady"
ConditionSubscriptionManagerReady ConditionType = "SubscriptionManagerReady"
Expand All @@ -48,8 +48,9 @@ const (
ConditionReasonDeployed ConditionReason = "Deployed"
ConditionReasonDeployedFailed ConditionReason = "DeployFailed"
ConditionReasonDeploymentStatusSyncFailed ConditionReason = "DeploymentStatusSyncFailed"
ConditionReasonNATSAvailable ConditionReason = "Available"
ConditionReasonNATSNotAvailable ConditionReason = "NotAvailable"
ConditionReasonNATSAvailable ConditionReason = "NATSAvailable"
ConditionReasonNATSNotAvailable ConditionReason = "NATSUnavailable"
ConditionReasonBackendNotSpecified ConditionReason = "BackendNotSpecified"
ConditionReasonForbidden ConditionReason = "Forbidden"
ConditionReasonWebhookFailed ConditionReason = "WebhookFailed"
ConditionReasonWebhookReady ConditionReason = "Ready"
Expand All @@ -63,6 +64,7 @@ const (
ConditionPublisherProxyProcessingMessage = "Eventing publisher proxy deployment is in progress"
ConditionSubscriptionManagerReadyMessage = "Subscription manager is ready"
ConditionSubscriptionManagerStoppedMessage = "Subscription manager is stopped"
ConditionBackendNotSpecifiedMessage = "Backend config is not provided. Please specify a backend."

// subscription manager reasons
ConditionReasonEventMeshSubManagerReady ConditionReason = "EventMeshSubscriptionManagerReady"
Expand Down Expand Up @@ -99,7 +101,7 @@ type EventingStatus struct {
type EventingSpec struct {
// Backend defines the active backend used by Eventing.
// +kubebuilder:validation:XValidation:rule=" (self.type != 'EventMesh') || ((self.type == 'EventMesh') && (self.config.eventMeshSecret != ''))", message="secret cannot be empty if EventMesh backend is used"
Backend Backend `json:"backend,omitempty"`
Backend *Backend `json:"backend,omitempty"`

// Publisher defines the configurations for eventing-publisher-proxy.
// +kubebuilder:default:={replicas:{min:2,max:2}, resources:{limits:{cpu:"500m",memory:"512Mi"}, requests:{cpu:"40m",memory:"256Mi"}}}
Expand Down
8 changes: 4 additions & 4 deletions api/operator/v1alpha1/eventing_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestSyncStatusActiveBackend(t *testing.T) {
name: "it should set ActiveBackend to NATS",
givenEventing: &Eventing{
Spec: EventingSpec{
Backend: Backend{Type: NatsBackendType},
Backend: &Backend{Type: NatsBackendType},
},
Status: EventingStatus{},
},
Expand All @@ -29,7 +29,7 @@ func TestSyncStatusActiveBackend(t *testing.T) {
name: "it should set ActiveBackend to EventMesh",
givenEventing: &Eventing{
Spec: EventingSpec{
Backend: Backend{Type: EventMeshBackendType},
Backend: &Backend{Type: EventMeshBackendType},
},
Status: EventingStatus{},
},
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestIsSpecBackendTypeChanged(t *testing.T) {
name: "it should return false if backend is not changed",
givenEventing: &Eventing{
Spec: EventingSpec{
Backend: Backend{Type: NatsBackendType},
Backend: &Backend{Type: NatsBackendType},
},
Status: EventingStatus{
ActiveBackend: NatsBackendType,
Expand All @@ -77,7 +77,7 @@ func TestIsSpecBackendTypeChanged(t *testing.T) {
name: "it should return true if backend is changed",
givenEventing: &Eventing{
Spec: EventingSpec{
Backend: Backend{Type: NatsBackendType},
Backend: &Backend{Type: NatsBackendType},
},
Status: EventingStatus{
ActiveBackend: EventMeshBackendType,
Expand Down
8 changes: 4 additions & 4 deletions api/operator/v1alpha1/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (es *EventingStatus) UpdateConditionNATSAvailable(status metav1.ConditionStatus, reason ConditionReason,
func (es *EventingStatus) UpdateConditionBackendAvailable(status metav1.ConditionStatus, reason ConditionReason,
message string) {
condition := metav1.Condition{
Type: string(ConditionNATSAvailable),
Type: string(ConditionBackendAvailable),
Status: status,
LastTransitionTime: metav1.Now(),
Reason: string(reason),
Expand Down Expand Up @@ -75,7 +75,7 @@ func (es *EventingStatus) SetSubscriptionManagerReadyConditionToTrue() {

func (es *EventingStatus) SetStateReady() {
es.State = StateReady
es.UpdateConditionNATSAvailable(metav1.ConditionTrue, ConditionReasonNATSAvailable, ConditionNATSAvailableMessage)
es.UpdateConditionBackendAvailable(metav1.ConditionTrue, ConditionReasonNATSAvailable, ConditionNATSAvailableMessage)
es.UpdateConditionPublisherProxyReady(metav1.ConditionTrue, ConditionReasonDeployed, ConditionPublisherProxyReadyMessage)
}

Expand All @@ -84,7 +84,7 @@ func (ns *EventingStatus) SetStateWarning() {
}

func (es *EventingStatus) SetNATSAvailableConditionToTrue() {
es.UpdateConditionNATSAvailable(metav1.ConditionTrue, ConditionReasonNATSAvailable, ConditionNATSAvailableMessage)
es.UpdateConditionBackendAvailable(metav1.ConditionTrue, ConditionReasonNATSAvailable, ConditionNATSAvailableMessage)
}

func (es *EventingStatus) SetSubscriptionManagerReadyConditionToFalse(reason ConditionReason, message string) {
Expand Down
6 changes: 5 additions & 1 deletion api/operator/v1alpha1/zz_generated.deepcopy.go

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

4 changes: 2 additions & 2 deletions hack/e2e/common/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func EventingNATSCR() *eventingv1alpha1.Eventing {
Namespace: NamespaceName,
},
Spec: eventingv1alpha1.EventingSpec{
Backend: eventingv1alpha1.Backend{
Backend: &eventingv1alpha1.Backend{
Type: "NATS",
Config: eventingv1alpha1.BackendConfig{
NATSStreamStorageType: "File",
Expand All @@ -90,7 +90,7 @@ func EventingEventMeshCR() *eventingv1alpha1.Eventing {
Namespace: NamespaceName,
},
Spec: eventingv1alpha1.EventingSpec{
Backend: eventingv1alpha1.Backend{
Backend: &eventingv1alpha1.Backend{
Type: "EventMesh",
Config: eventingv1alpha1.BackendConfig{
EventMeshSecret: fmt.Sprintf("%s/%s", EventMeshSecretNamespace, EventMeshSecretName),
Expand Down
9 changes: 9 additions & 0 deletions internal/controller/operator/eventing/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ func (r *Reconciler) handleEventingDeletion(ctx context.Context, eventing *event
return ctrl.Result{}, nil
}

if eventing.Spec.Backend == nil {
// backend config can only be empty during creation and nothing is created if it is missing.
// Hence, eventing can be safely removed.
return r.removeFinalizer(ctx, eventing)
}

// check if subscription resources exist
exists, err := r.eventingManager.SubscriptionExists(ctx)
if err != nil {
Expand Down Expand Up @@ -438,6 +444,9 @@ func (r *Reconciler) handleEventingReconcile(ctx context.Context,

// set state processing if not set yet
r.InitStateProcessing(eventing)
if eventing.Spec.Backend == nil {
return ctrl.Result{Requeue: true}, r.syncStatusForEmptyBackend(ctx, eventing, log)
}

// sync webhooks CABundle.
if err := r.reconcileWebhooksWithCABundle(ctx); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ func Test_CreateEventingCR_NATS(t *testing.T) {
matchers.HaveFinalizer(),
),
},
{
name: "Eventing CR should have warning state when backend config is empty",
givenEventing: utils.NewEventingCR(
utils.WithEventingEmptyBackend(),
utils.WithEventingPublisherData(2, 2, "199m", "99Mi", "399m", "199Mi"),
),
givenNATS: natstestutils.NewNATSCR(
natstestutils.WithNATSCRDefaults(),
),
wantMatches: gomega.And(
matchers.HaveStatusWarning(),
matchers.HaveBackendNotAvailableConditionWith(eventingv1alpha1.ConditionBackendNotSpecifiedMessage,
eventingv1alpha1.ConditionReasonBackendNotSpecified),
matchers.HaveFinalizer(),
),
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -204,13 +220,13 @@ func Test_CreateEventingCR_NATS(t *testing.T) {
// then
// check Eventing CR status.
testEnvironment.GetEventingAssert(g, tc.givenEventing).Should(tc.wantMatches)
if tc.givenDeploymentReady {
if tc.givenDeploymentReady && tc.givenEventing.Spec.Backend != nil {
// check if EPP deployment, HPA resources created and values are reflected including owner reference.
ensureEPPDeploymentAndHPAResources(t, tc.givenEventing, testEnvironment)
// TODO: ensure NATS Backend config is reflected. Done as subscription controller is implemented.
}

if tc.wantEnsureK8sObjects {
if tc.wantEnsureK8sObjects && tc.givenEventing.Spec.Backend != nil {
// check if EPP resources exists.
ensureK8sResources(t, tc.givenEventing, testEnvironment)
// check if webhook configurations are updated with correct CABundle.
Expand Down
13 changes: 12 additions & 1 deletion internal/controller/operator/eventing/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,23 @@ func (r *Reconciler) syncStatusWithNATSErr(ctx context.Context,
eventing *eventingv1alpha1.Eventing, err error, log *zap.SugaredLogger) error {
// Set error state in status
eventing.Status.SetStateError()
eventing.Status.UpdateConditionNATSAvailable(metav1.ConditionFalse, eventingv1alpha1.ConditionReasonNATSNotAvailable,
eventing.Status.UpdateConditionBackendAvailable(metav1.ConditionFalse, eventingv1alpha1.ConditionReasonNATSNotAvailable,
err.Error())

return errors.Join(err, r.syncEventingStatus(ctx, eventing, log))
}

func (r *Reconciler) syncStatusForEmptyBackend(ctx context.Context,
eventing *eventingv1alpha1.Eventing, log *zap.SugaredLogger) error {
// Set error state in status
eventing.Status.SetStateWarning()
eventing.Status.UpdateConditionBackendAvailable(
metav1.ConditionFalse,
eventingv1alpha1.ConditionReasonBackendNotSpecified,
eventingv1alpha1.ConditionBackendNotSpecifiedMessage)
return r.syncEventingStatus(ctx, eventing, log)
}

// syncStatusWithPublisherProxyErr updates Publisher Proxy condition and sets an error state.
// Returns the relevant error.
func (r *Reconciler) syncStatusWithPublisherProxyErr(ctx context.Context,
Expand Down
2 changes: 1 addition & 1 deletion pkg/env/nats_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Test_GetNewNATSConfig(t *testing.T) {
UID: "1234-5678-1234-5678",
},
Spec: v1alpha1.EventingSpec{
Backend: v1alpha1.Backend{
Backend: &v1alpha1.Backend{
Type: v1alpha1.NatsBackendType,
Config: v1alpha1.BackendConfig{
EventTypePrefix: "sap.kyma.custom",
Expand Down
13 changes: 11 additions & 2 deletions test/matchers/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,25 @@ func HavePublisherProxyConditionForbiddenWithMsg(msg string) gomegatypes.GomegaM

func HaveNATSAvailableCondition() gomegatypes.GomegaMatcher {
return HaveCondition(metav1.Condition{
Type: string(v1alpha1.ConditionNATSAvailable),
Type: string(v1alpha1.ConditionBackendAvailable),
Status: metav1.ConditionTrue,
Reason: string(v1alpha1.ConditionReasonNATSAvailable),
Message: v1alpha1.ConditionNATSAvailableMessage,
})
}

func HaveBackendNotAvailableConditionWith(message string, reason v1alpha1.ConditionReason) gomegatypes.GomegaMatcher {
return HaveCondition(metav1.Condition{
Type: string(v1alpha1.ConditionBackendAvailable),
Status: metav1.ConditionFalse,
Reason: string(reason),
Message: message,
})
}

func HaveNATSNotAvailableConditionWith(message string) gomegatypes.GomegaMatcher {
return HaveCondition(metav1.Condition{
Type: string(v1alpha1.ConditionNATSAvailable),
Type: string(v1alpha1.ConditionBackendAvailable),
Status: metav1.ConditionFalse,
Reason: string(v1alpha1.ConditionReasonNATSNotAvailable),
Message: message,
Expand Down
13 changes: 10 additions & 3 deletions test/utils/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ import (
func WithEventingCRMinimal() EventingOption {
return func(e *v1alpha1.Eventing) error {
e.Spec = v1alpha1.EventingSpec{
Backend: v1alpha1.Backend{
Backend: &v1alpha1.Backend{
Type: v1alpha1.NatsBackendType,
},
}
return nil
}
}

func WithEventingEmptyBackend() EventingOption {
return func(e *v1alpha1.Eventing) error {
e.Spec = v1alpha1.EventingSpec{}
return nil
}
}

func WithEventingCRName(name string) EventingOption {
return func(e *v1alpha1.Eventing) error {
e.Name = name
Expand Down Expand Up @@ -77,7 +84,7 @@ func WithEventingPublisherData(minReplicas, maxReplicas int, requestCPU, request
func WithEventingInvalidBackend() EventingOption {
return func(e *v1alpha1.Eventing) error {
e.Spec = v1alpha1.EventingSpec{
Backend: v1alpha1.Backend{
Backend: &v1alpha1.Backend{
Type: "invalid",
},
}
Expand Down Expand Up @@ -108,7 +115,7 @@ func WithEventingLogLevel(logLevel string) EventingOption {

func WithEventMeshBackend(eventMeshSecretName string) EventingOption {
return func(e *v1alpha1.Eventing) error {
e.Spec.Backend = v1alpha1.Backend{
e.Spec.Backend = &v1alpha1.Backend{
Type: v1alpha1.EventMeshBackendType,
Config: v1alpha1.BackendConfig{
EventMeshSecret: e.Namespace + "/" + eventMeshSecretName,
Expand Down
2 changes: 1 addition & 1 deletion test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func NewEventingCR(opts ...EventingOption) *v1alpha1.Eventing {
UID: "1234-5678-1234-5678",
},
Spec: v1alpha1.EventingSpec{
Backend: v1alpha1.Backend{
Backend: &v1alpha1.Backend{
Type: v1alpha1.NatsBackendType,
},
},
Expand Down

0 comments on commit 443bf68

Please sign in to comment.