Skip to content

Commit f797ffe

Browse files
committed
f:
1 parent 225f296 commit f797ffe

File tree

3 files changed

+41
-45
lines changed

3 files changed

+41
-45
lines changed

internal/controller/httproute_controller.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
"sigs.k8s.io/gateway-api/apis/v1alpha2"
3939

4040
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
41-
"github.com/apache/apisix-ingress-controller/internal/controller/config"
4241
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
4342
"github.com/apache/apisix-ingress-controller/internal/provider"
4443
)
@@ -210,30 +209,24 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
210209
// TODO: diff the old and new status
211210
hr.Status.Parents = make([]gatewayv1.RouteParentStatus, 0, len(gateways))
212211
for _, gateway := range gateways {
213-
parentStatus := gatewayv1.RouteParentStatus{
214-
ParentRef: gatewayv1.ParentReference{
215-
Kind: ptr.To(gatewayv1.Kind(KindGateway)),
216-
Group: ptr.To(gatewayv1.Group(gatewayv1.GroupName)),
217-
Name: gatewayv1.ObjectName(gateway.Gateway.GetName()),
218-
Namespace: ptr.To(gatewayv1.Namespace(gateway.Gateway.GetNamespace())),
219-
},
220-
ControllerName: gatewayv1.GatewayController(config.ControllerConfig.ControllerName),
221-
}
212+
parentStatus := gatewayv1.RouteParentStatus{}
213+
SetRouteParentRef(&parentStatus, gateway.Gateway.Name, gateway.Gateway.Namespace)
222214
for _, condition := range gateway.Conditions {
223215
parentStatus.Conditions = MergeCondition(parentStatus.Conditions, condition)
224216
}
225217
if gateway.ListenerName == "" {
226218
continue
227219
}
228220
SetRouteConditionResolvedRefs(&parentStatus, hr.GetGeneration(), resolveRefStatus.status, resolveRefStatus.msg)
229-
// if the HTTPRoute is not accepted, the length of .Status.Parents should be 0 or 1. If it is 1, it can only contain a condition with Status="False"
230-
if accepted := SetRouteConditionAccepted(&parentStatus, gateway.Gateway, hr, acceptStatus.status, acceptStatus.msg); !accepted {
231-
hr.Status.Parents = []gatewayv1.RouteParentStatus{
232-
parentStatus,
233-
}
234-
break
221+
SetRouteConditionAccepted(&parentStatus, hr.GetGeneration(), acceptStatus.status, acceptStatus.msg)
222+
var accepted = !slices.ContainsFunc(parentStatus.Conditions, func(condition metav1.Condition) bool {
223+
return condition.Status == metav1.ConditionFalse
224+
})
225+
if accepted {
226+
hr.Status.Parents = append(hr.Status.Parents, parentStatus)
227+
} else {
228+
hr.Status.Parents = []gatewayv1.RouteParentStatus{parentStatus}
235229
}
236-
hr.Status.Parents = append(hr.Status.Parents, parentStatus)
237230
}
238231
if err := r.Status().Update(ctx, hr); err != nil {
239232
return ctrl.Result{}, err

internal/controller/utils.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"fmt"
1818
"path"
1919
"reflect"
20+
"slices"
2021
"strings"
2122

2223
"github.com/api7/gopkg/pkg/log"
@@ -230,26 +231,21 @@ func ConditionStatus(status bool) metav1.ConditionStatus {
230231
return metav1.ConditionFalse
231232
}
232233

233-
func SetRouteConditionAccepted(routeParentStatus *gatewayv1.RouteParentStatus, gw *gatewayv1.Gateway, hr *gatewayv1.HTTPRoute, acceptedStatus bool, acceptStatusMsg string) (accepted bool) {
234+
func SetRouteConditionAccepted(routeParentStatus *gatewayv1.RouteParentStatus, generation int64, status bool, message string) {
234235
condition := metav1.Condition{
235236
Type: string(gatewayv1.RouteConditionAccepted),
236-
Status: ConditionStatus(acceptedStatus),
237-
ObservedGeneration: hr.GetGeneration(),
238-
LastTransitionTime: metav1.Now(),
237+
Status: ConditionStatus(status),
239238
Reason: string(gatewayv1.RouteReasonAccepted),
240-
Message: acceptStatusMsg,
241-
}
242-
// if it is across the namespace between the Gateway and HTTPRoute, set .Status="False"
243-
if gw.GetNamespace() != hr.GetNamespace() {
244-
condition.Status = metav1.ConditionFalse
245-
condition.Reason = string(gatewayv1.RouteReasonNotAllowedByListeners)
246-
condition.Message = fmt.Sprintf("A HTTPRoute in the namespace %s failed to attach to a Gateway in another namespace %s",
247-
hr.GetNamespace(), gw.GetNamespace())
239+
ObservedGeneration: generation,
240+
Message: message,
241+
LastTransitionTime: metav1.Now(),
248242
}
249-
if !IsConditionPresentAndEqual(routeParentStatus.Conditions, condition) {
243+
244+
if !IsConditionPresentAndEqual(routeParentStatus.Conditions, condition) && !slices.ContainsFunc(routeParentStatus.Conditions, func(item metav1.Condition) bool {
245+
return item.Type == condition.Type && item.Status == metav1.ConditionFalse && condition.Status == metav1.ConditionTrue
246+
}) {
250247
routeParentStatus.Conditions = MergeCondition(routeParentStatus.Conditions, condition)
251248
}
252-
return condition.Status == metav1.ConditionTrue
253249
}
254250

255251
func SetRouteConditionResolvedRefs(routeParentStatus *gatewayv1.RouteParentStatus, generation int64, status bool, message string) {
@@ -273,6 +269,19 @@ func SetRouteConditionResolvedRefs(routeParentStatus *gatewayv1.RouteParentStatu
273269
}
274270
}
275271

272+
func SetRouteParentRef(routeParentStatus *gatewayv1.RouteParentStatus, gatewayName string, namespace string) {
273+
kind := gatewayv1.Kind(KindGateway)
274+
group := gatewayv1.Group(gatewayv1.GroupName)
275+
ns := gatewayv1.Namespace(namespace)
276+
routeParentStatus.ParentRef = gatewayv1.ParentReference{
277+
Kind: &kind,
278+
Group: &group,
279+
Name: gatewayv1.ObjectName(gatewayName),
280+
Namespace: &ns,
281+
}
282+
routeParentStatus.ControllerName = gatewayv1.GatewayController(config.ControllerConfig.ControllerName)
283+
}
284+
276285
func ParseRouteParentRefs(
277286
ctx context.Context, mgrc client.Client, route client.Object, parentRefs []gatewayv1.ParentReference,
278287
) ([]RouteParentRefContext, error) {
@@ -339,19 +348,21 @@ func ParseRouteParentRefs(
339348
continue
340349
}
341350

342-
if ok, err := routeMatchesListenerAllowedRoutes(ctx, mgrc, route, listener.AllowedRoutes, gateway.Namespace, parentRef.Namespace); err != nil {
343-
return nil, fmt.Errorf("failed matching listener %s to a route %s for gateway %s: %w",
351+
listenerName = string(listener.Name)
352+
ok, err := routeMatchesListenerAllowedRoutes(ctx, mgrc, route, listener.AllowedRoutes, gateway.Namespace, parentRef.Namespace)
353+
if err != nil {
354+
log.Warnf("failed matching listener %s to a route %s for gateway %s: %v",
344355
listener.Name, route.GetName(), gateway.Name, err,
345356
)
346-
} else if !ok {
357+
}
358+
if !ok {
347359
reason = gatewayv1.RouteReasonNotAllowedByListeners
348360
continue
349361
}
350362

351363
// TODO: check if the listener status is programmed
352364

353365
matched = true
354-
listenerName = string(listener.Name)
355366
break
356367
}
357368

@@ -368,7 +379,8 @@ func ParseRouteParentRefs(
368379
})
369380
} else {
370381
gateways = append(gateways, RouteParentRefContext{
371-
Gateway: &gateway,
382+
Gateway: &gateway,
383+
ListenerName: listenerName,
372384
Conditions: []metav1.Condition{{
373385
Type: string(gatewayv1.RouteConditionAccepted),
374386
Status: metav1.ConditionFalse,

test/conformance/suite_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/gruntwork-io/terratest/modules/retry"
2323
. "github.com/onsi/ginkgo/v2"
2424
. "github.com/onsi/gomega"
25-
"k8s.io/utils/ptr"
2625
"sigs.k8s.io/controller-runtime/pkg/client"
2726
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
2827

@@ -223,14 +222,6 @@ func patchGatewaysForConformanceTest(ctx context.Context, k8sClient client.Clien
223222
},
224223
}
225224

226-
if len(gateway.Spec.Listeners) > 0 {
227-
gateway.Spec.Listeners[0].AllowedRoutes = &gatewayv1.AllowedRoutes{
228-
Namespaces: &gatewayv1.RouteNamespaces{
229-
From: ptr.To(gatewayv1.FromNamespaces("All")),
230-
},
231-
}
232-
}
233-
234225
if err := k8sClient.Update(ctx, gateway); err != nil {
235226
GinkgoT().Logf("Failed to patch Gateway %s: %v", gateway.Name, err)
236227
continue

0 commit comments

Comments
 (0)