Skip to content

Commit

Permalink
use lo.ContainsBy to match conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
randmonkey committed Jan 9, 2023
1 parent 6b73827 commit 3c11985
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
6 changes: 4 additions & 2 deletions internal/controllers/gateway/httproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
19 changes: 7 additions & 12 deletions internal/controllers/gateway/route_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 3c11985

Please sign in to comment.