From 3c119857d1d0314e33aec4f632d5fca77831825a Mon Sep 17 00:00:00 2001 From: Yi Tao Date: Tue, 10 Jan 2023 00:32:05 +0800 Subject: [PATCH] use lo.ContainsBy to match conditions --- .../gateway/httproute_controller.go | 6 ++++-- internal/controllers/gateway/route_utils.go | 19 +++++++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/internal/controllers/gateway/httproute_controller.go b/internal/controllers/gateway/httproute_controller.go index d59f62dee38..88b96a69823 100644 --- a/internal/controllers/gateway/httproute_controller.go +++ b/internal/controllers/gateway/httproute_controller.go @@ -435,10 +435,12 @@ func (r *HTTPRouteReconciler) ensureGatewayReferenceStatusAdded(ctx context.Cont // if the reference already exists and doesn't require any changes // then just leave it alone. if existingGatewayParentStatus, exists := parentStatuses[key]; exists { - // check if the parentRef and controllerName are equal, and expected condition presents in conditions + // check if the parentRef and controllerName are equal, and whether the new condition is present in existing conditions if reflect.DeepEqual(existingGatewayParentStatus.ParentRef, gatewayParentStatus.ParentRef) && existingGatewayParentStatus.ControllerName == gatewayParentStatus.ControllerName && - containsCondition(existingGatewayParentStatus.Conditions, gatewayParentStatus.Conditions[0]) { + lo.ContainsBy(existingGatewayParentStatus.Conditions, func(c metav1.Condition) bool { + return sameCondition(gatewayParentStatus.Conditions[0], c) + }) { continue } } diff --git a/internal/controllers/gateway/route_utils.go b/internal/controllers/gateway/route_utils.go index 4c3b6f4a885..7e8d168b304 100644 --- a/internal/controllers/gateway/route_utils.go +++ b/internal/controllers/gateway/route_utils.go @@ -631,16 +631,11 @@ func isHTTPReferenceGranted(grantSpec gatewayv1alpha2.ReferenceGrantSpec, backen return false } -// containsCondition returns true if a condition with same type, status, reason and message -// of expectedCondition in conditions -func containsCondition(conditions []metav1.Condition, expectedCondition metav1.Condition) bool { - for _, condition := range conditions { - if condition.Type == expectedCondition.Type && - condition.Status == expectedCondition.Status && - condition.Reason == expectedCondition.Reason && - condition.Message == expectedCondition.Message { - return true - } - } - return false +// sameCondition returns true if the condition has the same type, status, reason and message +// with expectedCondition in conditions. +func sameCondition(expectedCondition metav1.Condition, condition metav1.Condition) bool { + return condition.Type == expectedCondition.Type && + condition.Status == expectedCondition.Status && + condition.Reason == expectedCondition.Reason && + condition.Message == expectedCondition.Message }