Skip to content

Commit

Permalink
fix: update the Eventing CR status accordingly after switching from a…
Browse files Browse the repository at this point in the history
…n empty backend (#538)
  • Loading branch information
marcobebway authored Mar 25, 2024
1 parent 67bf682 commit 626bfd2
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 13 deletions.
12 changes: 8 additions & 4 deletions api/operator/v1alpha1/eventing_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package v1alpha1

import (
"strings"

kcorev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -39,12 +41,10 @@ const (
ConditionSubscriptionManagerReady ConditionType = "SubscriptionManagerReady"
ConditionDeleted ConditionType = "Deleted"

// common reasons.
ConditionReasonProcessing ConditionReason = "Processing"
ConditionReasonDeleted ConditionReason = "Deleted"
ConditionReasonStopped ConditionReason = "Stopped"

// publisher proxy reasons.
ConditionReasonDeployed ConditionReason = "Deployed"
ConditionReasonDeployedFailed ConditionReason = "DeployFailed"
ConditionReasonDeploymentStatusSyncFailed ConditionReason = "DeploymentStatusSyncFailed"
Expand All @@ -55,8 +55,8 @@ const (
ConditionReasonWebhookFailed ConditionReason = "WebhookFailed"
ConditionReasonWebhookReady ConditionReason = "Ready"
ConditionReasonDeletionError ConditionReason = "DeletionError"
ConditionReasonEventMeshConfigAvailable ConditionReason = "EventMeshConfigAvailable"

// message for conditions.
ConditionPublisherProxyReadyMessage = "Publisher proxy is deployed"
ConditionPublisherProxyDeletedMessage = "Publisher proxy is deleted"
ConditionNATSAvailableMessage = "NATS is available"
Expand All @@ -65,8 +65,8 @@ const (
ConditionSubscriptionManagerReadyMessage = "Subscription manager is ready"
ConditionSubscriptionManagerStoppedMessage = "Subscription manager is stopped"
ConditionBackendNotSpecifiedMessage = "Backend config is not provided. Please specify a backend."
ConditionEventMeshConfigAvailableMessage = "EventMesh config is available"

// subscription manager reasons.
ConditionReasonEventMeshSubManagerReady ConditionReason = "EventMeshSubscriptionManagerReady"
ConditionReasonEventMeshSubManagerFailed ConditionReason = "EventMeshSubscriptionManagerFailed"
ConditionReasonEventMeshSubManagerStopFailed ConditionReason = "EventMeshSubscriptionManagerStopFailed"
Expand Down Expand Up @@ -243,3 +243,7 @@ func (e *Eventing) IsPreviousBackendEmpty() bool {
func (e *Eventing) IsSpecBackendTypeChanged() bool {
return e.Status.ActiveBackend != e.Spec.Backend.Type
}

func (es EventingSpec) HasEmptyBackend() bool {
return es.Backend == nil || len(strings.TrimSpace(string(es.Backend.Type))) == 0
}
50 changes: 50 additions & 0 deletions api/operator/v1alpha1/eventing_types_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1alpha1

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -109,3 +110,52 @@ func Test_getSupportedConditionsTypes(t *testing.T) {
got := getSupportedConditionsTypes()
require.Equal(t, want, got)
}

func TestHasEmptyBackend(t *testing.T) {
tests := []struct {
name string
givenEventingSpec EventingSpec
wantHasEmptyBackend bool
}{
{
name: "with nil backend",
givenEventingSpec: EventingSpec{
Backend: nil,
},
wantHasEmptyBackend: true,
},
{
name: "with empty backend type",
givenEventingSpec: EventingSpec{
Backend: &Backend{
Type: "",
},
},
wantHasEmptyBackend: true,
},
{
name: "with non-empty backend type all whitespaces",
givenEventingSpec: EventingSpec{
Backend: &Backend{
Type: " ",
},
},
wantHasEmptyBackend: true,
},
{
name: "with non-empty backend type",
givenEventingSpec: EventingSpec{
Backend: &Backend{
Type: "any",
},
},
wantHasEmptyBackend: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.givenEventingSpec.HasEmptyBackend()
assert.Equal(t, tt.wantHasEmptyBackend, got)
})
}
}
6 changes: 6 additions & 0 deletions api/operator/v1alpha1/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ func (es *EventingStatus) SetNATSAvailableConditionToTrue() {
es.UpdateConditionBackendAvailable(kmetav1.ConditionTrue, ConditionReasonNATSAvailable, ConditionNATSAvailableMessage)
}

func (es *EventingStatus) SetEventMeshAvailableConditionToTrue() {
es.UpdateConditionBackendAvailable(
kmetav1.ConditionTrue, ConditionReasonEventMeshConfigAvailable, ConditionEventMeshConfigAvailableMessage,
)
}

func (es *EventingStatus) SetSubscriptionManagerReadyConditionToFalse(reason ConditionReason, message string) {
es.UpdateConditionSubscriptionManagerReady(kmetav1.ConditionFalse, reason,
message)
Expand Down
166 changes: 166 additions & 0 deletions api/operator/v1alpha1/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -285,3 +286,168 @@ func TestRemoveUnsupportedConditions(t *testing.T) {
})
}
}

func TestSetEventMeshAvailableConditionToTrue(t *testing.T) {
var (
anyCondition0 = kmetav1.Condition{
Type: "any-type-0",
Status: kmetav1.ConditionStatus("any-status-0"),
Reason: "any-reason-0",
Message: "any-message-0",
}

anyCondition1 = kmetav1.Condition{
Type: "any-type-1",
Status: kmetav1.ConditionStatus("any-status-1"),
Reason: "any-reason-1",
Message: "any-message-1",
}

anyCondition2 = kmetav1.Condition{
Type: "any-type-2",
Status: kmetav1.ConditionStatus("any-status-2"),
Reason: "any-reason-2",
Message: "any-message-2",
}

backendAvailableConditionFalse = kmetav1.Condition{
Type: string(ConditionBackendAvailable),
Status: kmetav1.ConditionFalse,
Reason: string(ConditionReasonBackendNotSpecified),
Message: ConditionBackendNotSpecifiedMessage,
}

backendAvailableConditionTrue = kmetav1.Condition{
Type: string(ConditionBackendAvailable),
Status: kmetav1.ConditionTrue,
Reason: string(ConditionReasonEventMeshConfigAvailable),
Message: ConditionEventMeshConfigAvailableMessage,
}
)

tests := []struct {
name string
givenEventingStatus EventingStatus
wantEventingStatus EventingStatus
}{
// add new condition
{
name: "should add a new condition in case of nil conditions",
givenEventingStatus: EventingStatus{
Conditions: nil,
},
wantEventingStatus: EventingStatus{
Conditions: []kmetav1.Condition{
backendAvailableConditionTrue,
},
},
},
{
name: "should add a new condition in case of empty conditions",
givenEventingStatus: EventingStatus{
Conditions: []kmetav1.Condition{},
},
wantEventingStatus: EventingStatus{
Conditions: []kmetav1.Condition{
backendAvailableConditionTrue,
},
},
},
{
name: "should add a new condition and preserve existing ones",
givenEventingStatus: EventingStatus{
Conditions: []kmetav1.Condition{
anyCondition0,
anyCondition1,
anyCondition2,
},
},
wantEventingStatus: EventingStatus{
Conditions: []kmetav1.Condition{
anyCondition0,
anyCondition1,
anyCondition2,
backendAvailableConditionTrue,
},
},
},
// update existing condition
{
name: "should update existing condition",
givenEventingStatus: EventingStatus{
Conditions: []kmetav1.Condition{
backendAvailableConditionFalse,
},
},
wantEventingStatus: EventingStatus{
Conditions: []kmetav1.Condition{
backendAvailableConditionTrue,
},
},
},
{
name: "should update condition and preserve existing ones",
givenEventingStatus: EventingStatus{
Conditions: []kmetav1.Condition{
anyCondition0,
anyCondition1,
backendAvailableConditionTrue,
anyCondition2,
},
},
wantEventingStatus: EventingStatus{
Conditions: []kmetav1.Condition{
anyCondition0,
anyCondition1,
backendAvailableConditionTrue,
anyCondition2,
},
},
},
{
name: "should update condition from false to true and preserve existing ones",
givenEventingStatus: EventingStatus{
Conditions: []kmetav1.Condition{
anyCondition0,
backendAvailableConditionFalse,
anyCondition1,
anyCondition2,
},
},
wantEventingStatus: EventingStatus{
Conditions: []kmetav1.Condition{
anyCondition0,
backendAvailableConditionTrue,
anyCondition1,
anyCondition2,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.givenEventingStatus.SetEventMeshAvailableConditionToTrue()
assertConditionsEqual(t, tt.wantEventingStatus.Conditions, tt.givenEventingStatus.Conditions)
})
}
}

// assertConditionsEqual ensures conditions are equal.
// Note: This function takes into consideration the order of conditions while doing the equality check.
func assertConditionsEqual(t *testing.T, expected, actual []kmetav1.Condition) {
t.Helper()

assert.Equal(t, len(expected), len(actual))
for i := 0; i < len(expected); i++ {
assertConditionEqual(t, expected[i], actual[i])
}
}

func assertConditionEqual(t *testing.T, expected, actual kmetav1.Condition) {
t.Helper()

assert.Equal(t, expected.Type, actual.Type)
assert.Equal(t, expected.Status, actual.Status)
assert.Equal(t, expected.Reason, actual.Reason)
assert.Equal(t, expected.Message, actual.Message)
}
9 changes: 7 additions & 2 deletions internal/controller/operator/eventing/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,10 @@ func (r *Reconciler) handleEventingReconcile(ctx context.Context,

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

// Handle empty backend.
if eventing.Spec.HasEmptyBackend() {
return kctrl.Result{Requeue: false}, r.syncStatusForEmptyBackend(ctx,
operatorv1alpha1.ConditionBackendNotSpecifiedMessage,
eventing, log)
}
Expand Down Expand Up @@ -677,6 +679,9 @@ func (r *Reconciler) reconcileEventMeshBackend(ctx context.Context, eventing *op
}
return kctrl.Result{}, r.syncStatusWithSubscriptionManagerErr(ctx, eventing, err, log)
}

// Set the Eventing CR status accordingly.
eventing.Status.SetEventMeshAvailableConditionToTrue()
eventing.Status.SetSubscriptionManagerReadyConditionToTrue()

deployment, err := r.handlePublisherProxy(ctx, eventing, eventing.Spec.Backend.Type)
Expand Down
Loading

0 comments on commit 626bfd2

Please sign in to comment.