From 226e2f93c6260affd560e536dc209aec517c5560 Mon Sep 17 00:00:00 2001 From: marcobebway Date: Thu, 18 Jan 2024 16:34:24 +0100 Subject: [PATCH] Remove unsupported conditions --- api/operator/v1alpha1/eventing_types.go | 8 + api/operator/v1alpha1/status.go | 15 ++ api/operator/v1alpha1/status_test.go | 189 +++++++++++++++++- .../controller/operator/eventing/status.go | 3 + 4 files changed, 213 insertions(+), 2 deletions(-) diff --git a/api/operator/v1alpha1/eventing_types.go b/api/operator/v1alpha1/eventing_types.go index 5c0c853e..5592ff3d 100644 --- a/api/operator/v1alpha1/eventing_types.go +++ b/api/operator/v1alpha1/eventing_types.go @@ -27,6 +27,14 @@ type ConditionReason string type ConditionType string +var supportedConditionsTypes = map[ConditionType]interface{}{ + ConditionBackendAvailable: nil, + ConditionPublisherProxyReady: nil, + ConditionWebhookReady: nil, + ConditionSubscriptionManagerReady: nil, + ConditionDeleted: nil, +} + const ( StateReady string = "Ready" StateError string = "Error" diff --git a/api/operator/v1alpha1/status.go b/api/operator/v1alpha1/status.go index a1d34190..15e7e025 100644 --- a/api/operator/v1alpha1/status.go +++ b/api/operator/v1alpha1/status.go @@ -145,3 +145,18 @@ func (es *EventingStatus) ClearPublisherService() { func (es *EventingStatus) SetPublisherService(name, namespace string) { es.PublisherService = fmt.Sprintf("%s.%s", name, namespace) } + +// RemoveUnsupportedConditions removes unsupported conditions from the status and keeps only the supported ones. +func (es *EventingStatus) RemoveUnsupportedConditions() { + if len(es.Conditions) == 0 { + return + } + + supported := make([]kmetav1.Condition, 0, len(es.Conditions)) + for _, c := range es.Conditions { + if _, ok := supportedConditionsTypes[ConditionType(c.Type)]; ok { + supported = append(supported, c) + } + } + es.Conditions = supported +} diff --git a/api/operator/v1alpha1/status_test.go b/api/operator/v1alpha1/status_test.go index e5c61af4..274ed23e 100644 --- a/api/operator/v1alpha1/status_test.go +++ b/api/operator/v1alpha1/status_test.go @@ -1,10 +1,10 @@ package v1alpha1 import ( - "testing" - "github.com/stretchr/testify/require" kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "testing" + "time" ) func TestClearConditions(t *testing.T) { @@ -97,3 +97,188 @@ func TestSetPublisherService(t *testing.T) { }) } } + +func TestRemoveUnsupportedConditions(t *testing.T) { + t.Parallel() + + // given + var ( + // supported conditions + backendAvailableCondition = kmetav1.Condition{ + Type: "BackendAvailable", + Status: kmetav1.ConditionStatus("BackendAvailableStatus"), + ObservedGeneration: int64(1), + LastTransitionTime: kmetav1.Time{Time: time.Date(2001, 01, 01, 01, 01, 01, 000000001, time.UTC)}, + Reason: "BackendAvailableReason", + Message: "BackendAvailableMessage", + } + publisherProxyReadyCondition = kmetav1.Condition{ + Type: "PublisherProxyReady", + Status: kmetav1.ConditionStatus("PublisherProxyReadyStatus"), + ObservedGeneration: int64(2), + LastTransitionTime: kmetav1.Time{Time: time.Date(2002, 02, 02, 02, 02, 02, 000000002, time.UTC)}, + Reason: "PublisherProxyReadyReason", + Message: "PublisherProxyReadyMessage", + } + webhookReadyCondition = kmetav1.Condition{ + Type: "WebhookReady", + Status: kmetav1.ConditionStatus("WebhookReadyStatus"), + ObservedGeneration: int64(3), + LastTransitionTime: kmetav1.Time{Time: time.Date(2003, 03, 03, 03, 03, 03, 000000003, time.UTC)}, + Reason: "WebhookReadyReason", + Message: "WebhookReadyMessage", + } + subscriptionManagerReadyCondition = kmetav1.Condition{ + Type: "SubscriptionManagerReady", + Status: kmetav1.ConditionStatus("SubscriptionManagerReadyStatus"), + ObservedGeneration: int64(4), + LastTransitionTime: kmetav1.Time{Time: time.Date(2004, 04, 04, 04, 04, 04, 000000004, time.UTC)}, + Reason: "SubscriptionManagerReadyReason", + Message: "SubscriptionManagerReadyMessage", + } + deletedCondition = kmetav1.Condition{ + Type: "Deleted", + Status: kmetav1.ConditionStatus("DeletedStatus"), + ObservedGeneration: int64(5), + LastTransitionTime: kmetav1.Time{Time: time.Date(2005, 05, 05, 05, 05, 05, 000000005, time.UTC)}, + Reason: "DeletedReason", + Message: "DeletedMessage", + } + + // unsupported conditions + unsupportedTypeCondition1 = kmetav1.Condition{ + Type: "Unsupported1", + Status: kmetav1.ConditionStatus("UnsupportedStatus1"), + ObservedGeneration: int64(-1), + LastTransitionTime: kmetav1.Time{Time: time.Date(2011, 11, 11, 11, 11, 11, 000000011, time.UTC)}, + Reason: "UnsupportedReason1", + Message: "UnsupportedMessage1", + } + unsupportedTypeCondition2 = kmetav1.Condition{ + Type: "Unsupported2", + Status: kmetav1.ConditionStatus("UnsupportedStatus2"), + ObservedGeneration: int64(-2), + LastTransitionTime: kmetav1.Time{Time: time.Date(2012, 12, 12, 12, 12, 12, 000000012, time.UTC)}, + Reason: "UnsupportedReason2", + Message: "UnsupportedMessage2", + } + unsupportedTypeCondition3 = kmetav1.Condition{ + Type: "Unsupported3", + Status: kmetav1.ConditionStatus("UnsupportedStatus3"), + ObservedGeneration: int64(-3), + LastTransitionTime: kmetav1.Time{Time: time.Date(2013, 13, 13, 13, 13, 13, 000000013, time.UTC)}, + Reason: "UnsupportedReason3", + Message: "UnsupportedMessage3", + } + ) + + tests := []struct { + name string + givenStatus *EventingStatus + wantStatus *EventingStatus + }{ + { + name: "given nil conditions", + givenStatus: &EventingStatus{ + Conditions: nil, + }, + wantStatus: &EventingStatus{ + Conditions: nil, + }, + }, + { + name: "given empty conditions", + givenStatus: &EventingStatus{ + Conditions: []kmetav1.Condition{}, + }, + wantStatus: &EventingStatus{ + Conditions: []kmetav1.Condition{}, + }, + }, + { + name: "given few supported condition", + givenStatus: &EventingStatus{ + Conditions: []kmetav1.Condition{ + backendAvailableCondition, + subscriptionManagerReadyCondition, + }, + }, + wantStatus: &EventingStatus{ + Conditions: []kmetav1.Condition{ + backendAvailableCondition, + subscriptionManagerReadyCondition, + }, + }, + }, + { + name: "given all supported conditions", + givenStatus: &EventingStatus{ + Conditions: []kmetav1.Condition{ + backendAvailableCondition, + publisherProxyReadyCondition, + webhookReadyCondition, + subscriptionManagerReadyCondition, + deletedCondition, + }, + }, + wantStatus: &EventingStatus{ + Conditions: []kmetav1.Condition{ + backendAvailableCondition, + publisherProxyReadyCondition, + webhookReadyCondition, + subscriptionManagerReadyCondition, + deletedCondition, + }, + }, + }, + { + name: "given all unsupported conditions", + givenStatus: &EventingStatus{ + Conditions: []kmetav1.Condition{ + unsupportedTypeCondition1, + unsupportedTypeCondition2, + unsupportedTypeCondition3, + }, + }, + wantStatus: &EventingStatus{ + Conditions: []kmetav1.Condition{}, + }, + }, + { + name: "given supported and unsupported conditions", + givenStatus: &EventingStatus{ + Conditions: []kmetav1.Condition{ + unsupportedTypeCondition1, + unsupportedTypeCondition2, + unsupportedTypeCondition3, + backendAvailableCondition, + publisherProxyReadyCondition, + webhookReadyCondition, + subscriptionManagerReadyCondition, + deletedCondition, + }, + }, + wantStatus: &EventingStatus{ + Conditions: []kmetav1.Condition{ + backendAvailableCondition, + publisherProxyReadyCondition, + webhookReadyCondition, + subscriptionManagerReadyCondition, + deletedCondition, + }, + }, + }, + } + for _, tt := range tests { + ttc := tt + t.Run(ttc.name, func(t *testing.T) { + t.Parallel() + + // when + ttc.givenStatus.RemoveUnsupportedConditions() + + // then + require.Equal(t, ttc.wantStatus, ttc.givenStatus) + }) + } +} diff --git a/internal/controller/operator/eventing/status.go b/internal/controller/operator/eventing/status.go index 095487ad..b7b10f28 100644 --- a/internal/controller/operator/eventing/status.go +++ b/internal/controller/operator/eventing/status.go @@ -169,6 +169,9 @@ func (r *Reconciler) syncEventingStatus(ctx context.Context, func (r *Reconciler) updateStatus(ctx context.Context, oldEventing, newEventing *operatorv1alpha1.Eventing, logger *zap.SugaredLogger, ) error { + // Preserve only supported conditions. + newEventing.Status.RemoveUnsupportedConditions() + // Compare the status taking into consideration lastTransitionTime in conditions if oldEventing.Status.IsEqual(newEventing.Status) { return nil