Skip to content

Commit 225f296

Browse files
committed
f:
1 parent f1813fc commit 225f296

File tree

3 files changed

+44
-49
lines changed

3 files changed

+44
-49
lines changed

internal/controller/httproute_controller.go

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ 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"
4142
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
4243
"github.com/apache/apisix-ingress-controller/internal/provider"
4344
)
@@ -207,44 +208,32 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
207208
}
208209

209210
// TODO: diff the old and new status
210-
hrNN := types.NamespacedName{Namespace: hr.GetNamespace(), Name: hr.GetName()}
211211
hr.Status.Parents = make([]gatewayv1.RouteParentStatus, 0, len(gateways))
212212
for _, gateway := range gateways {
213-
var (
214-
parentStatus gatewayv1.RouteParentStatus
215-
gwNN = types.NamespacedName{
216-
Namespace: gateway.Gateway.GetNamespace(),
217-
Name: gateway.Gateway.GetName(),
218-
}
219-
conditionAccepted = metav1.Condition{
220-
Type: string(gatewayv1.RouteConditionAccepted),
221-
Status: ConditionStatus(acceptStatus.status),
222-
ObservedGeneration: hr.GetGeneration(),
223-
LastTransitionTime: metav1.Now(),
224-
Reason: string(gatewayv1.RouteReasonAccepted),
225-
Message: acceptStatus.msg,
226-
}
227-
)
228-
if ok := setControllerNameAndParentRef(&parentStatus, gwNN, hrNN); !ok {
229-
conditionAccepted = metav1.Condition{
230-
Type: string(gatewayv1.RouteConditionAccepted),
231-
Status: metav1.ConditionFalse,
232-
ObservedGeneration: hr.GetGeneration(),
233-
LastTransitionTime: metav1.Now(),
234-
Reason: string(gatewayv1.RouteReasonNotAllowedByListeners),
235-
Message: fmt.Sprintf("A HTTPRoute in the namespace %s failed to attach to a Gateway in another namespace %s",
236-
hrNN.Namespace, gwNN.Namespace),
237-
}
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),
238221
}
239222
for _, condition := range gateway.Conditions {
240223
parentStatus.Conditions = MergeCondition(parentStatus.Conditions, condition)
241224
}
242-
SetRouteConditionResolvedRefs(&parentStatus, hr.GetGeneration(), resolveRefStatus.status, resolveRefStatus.msg)
243-
hr.Status.Parents = append(hr.Status.Parents, parentStatus)
244225
if gateway.ListenerName == "" {
245226
continue
246227
}
247-
SetRouteParentStatusCondtion(&parentStatus, conditionAccepted)
228+
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
235+
}
236+
hr.Status.Parents = append(hr.Status.Parents, parentStatus)
248237
}
249238
if err := r.Status().Update(ctx, hr); err != nil {
250239
return ctrl.Result{}, err

internal/controller/utils.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/labels"
2929
"k8s.io/apimachinery/pkg/types"
30-
"k8s.io/utils/ptr"
3130
"sigs.k8s.io/controller-runtime/pkg/client"
3231
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3332
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
@@ -231,10 +230,26 @@ func ConditionStatus(status bool) metav1.ConditionStatus {
231230
return metav1.ConditionFalse
232231
}
233232

234-
func SetRouteParentStatusCondtion(routeParentStatus *gatewayv1.RouteParentStatus, condition metav1.Condition) {
233+
func SetRouteConditionAccepted(routeParentStatus *gatewayv1.RouteParentStatus, gw *gatewayv1.Gateway, hr *gatewayv1.HTTPRoute, acceptedStatus bool, acceptStatusMsg string) (accepted bool) {
234+
condition := metav1.Condition{
235+
Type: string(gatewayv1.RouteConditionAccepted),
236+
Status: ConditionStatus(acceptedStatus),
237+
ObservedGeneration: hr.GetGeneration(),
238+
LastTransitionTime: metav1.Now(),
239+
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())
248+
}
235249
if !IsConditionPresentAndEqual(routeParentStatus.Conditions, condition) {
236250
routeParentStatus.Conditions = MergeCondition(routeParentStatus.Conditions, condition)
237251
}
252+
return condition.Status == metav1.ConditionTrue
238253
}
239254

240255
func SetRouteConditionResolvedRefs(routeParentStatus *gatewayv1.RouteParentStatus, generation int64, status bool, message string) {
@@ -258,24 +273,6 @@ func SetRouteConditionResolvedRefs(routeParentStatus *gatewayv1.RouteParentStatu
258273
}
259274
}
260275

261-
// setControllerNameAndParentRef sets the ControllerName and ParentRef fields in the RouteParentStatus object.
262-
// It ensures ParentRef is only set if the Gateway and HTTPRoute are in the same namespace.
263-
// Returns true if ParentRef is set, otherwise false.
264-
func setControllerNameAndParentRef(status *gatewayv1.RouteParentStatus, gwNN, hrNN types.NamespacedName) bool {
265-
status.ControllerName = gatewayv1.GatewayController(config.ControllerConfig.ControllerName)
266-
// route should not have Parents set in status if cross namespace
267-
if gwNN.Namespace == hrNN.Namespace {
268-
status.ParentRef = gatewayv1.ParentReference{
269-
Kind: ptr.To(gatewayv1.Kind(KindGateway)),
270-
Group: ptr.To(gatewayv1.Group(gatewayv1.GroupName)),
271-
Name: gatewayv1.ObjectName(gwNN.Name),
272-
Namespace: ptr.To(gatewayv1.Namespace(gwNN.Namespace)),
273-
}
274-
return true
275-
}
276-
return false
277-
}
278-
279276
func ParseRouteParentRefs(
280277
ctx context.Context, mgrc client.Client, route client.Object, parentRefs []gatewayv1.ParentReference,
281278
) ([]RouteParentRefContext, error) {

test/conformance/suite_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ 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"
2526
"sigs.k8s.io/controller-runtime/pkg/client"
2627
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
2728

@@ -222,6 +223,14 @@ func patchGatewaysForConformanceTest(ctx context.Context, k8sClient client.Clien
222223
},
223224
}
224225

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+
225234
if err := k8sClient.Update(ctx, gateway); err != nil {
226235
GinkgoT().Logf("Failed to patch Gateway %s: %v", gateway.Name, err)
227236
continue

0 commit comments

Comments
 (0)