Skip to content

Commit c95fe86

Browse files
committed
Remove duplicate parentRefs from httproute status
1 parent 60a80d2 commit c95fe86

File tree

5 files changed

+89
-10
lines changed

5 files changed

+89
-10
lines changed

internal/controller/state/graph/graph_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ func TestBuildGraph(t *testing.T) {
786786
SectionName: &gw1.Source.Spec.Listeners[0].Name,
787787
},
788788
{
789-
Idx: 1,
789+
Idx: 0,
790790
Gateway: &ParentRefGateway{
791791
NamespacedName: client.ObjectKeyFromObject(gw1.Source),
792792
EffectiveNginxProxy: np1Effective,
@@ -799,7 +799,7 @@ func TestBuildGraph(t *testing.T) {
799799
SectionName: &gw1.Source.Spec.Listeners[1].Name,
800800
},
801801
{
802-
Idx: 2,
802+
Idx: 0,
803803
Gateway: &ParentRefGateway{
804804
NamespacedName: client.ObjectKeyFromObject(gw1.Source),
805805
EffectiveNginxProxy: np1Effective,
@@ -816,7 +816,7 @@ func TestBuildGraph(t *testing.T) {
816816
SectionName: &gw1.Source.Spec.Listeners[2].Name,
817817
},
818818
{
819-
Idx: 3,
819+
Idx: 0,
820820
Gateway: &ParentRefGateway{
821821
NamespacedName: client.ObjectKeyFromObject(gw1.Source),
822822
EffectiveNginxProxy: np1Effective,
@@ -868,7 +868,7 @@ func TestBuildGraph(t *testing.T) {
868868
SectionName: &gw1.Source.Spec.Listeners[0].Name,
869869
},
870870
{
871-
Idx: 1,
871+
Idx: 0,
872872
Gateway: &ParentRefGateway{
873873
NamespacedName: client.ObjectKeyFromObject(gw1.Source),
874874
EffectiveNginxProxy: np1Effective,
@@ -881,7 +881,7 @@ func TestBuildGraph(t *testing.T) {
881881
SectionName: &gw1.Source.Spec.Listeners[1].Name,
882882
},
883883
{
884-
Idx: 2,
884+
Idx: 0,
885885
Gateway: &ParentRefGateway{
886886
NamespacedName: client.ObjectKeyFromObject(gw1.Source),
887887
EffectiveNginxProxy: np1Effective,
@@ -894,7 +894,7 @@ func TestBuildGraph(t *testing.T) {
894894
SectionName: &gw1.Source.Spec.Listeners[2].Name,
895895
},
896896
{
897-
Idx: 3,
897+
Idx: 0,
898898
Gateway: &ParentRefGateway{
899899
NamespacedName: client.ObjectKeyFromObject(gw1.Source),
900900
EffectiveNginxProxy: np1Effective,

internal/controller/state/graph/route_common.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,12 @@ func buildSectionNameRefs(
322322
SectionName: &l.Source.Name,
323323
Port: p.Port,
324324
})
325-
parentRefIndex++
326325
}
326+
327+
// if the ParentRefs we create are for each listener in the same gateway, we keep the
328+
// parentRefIndex the same so when we look at a route's parentRef's we can see
329+
// if the parentRef is a unique parentRef or one we created internally
330+
parentRefIndex++
327331
continue
328332
}
329333

internal/controller/state/graph/route_common_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func TestBuildSectionNameRefs(t *testing.T) {
124124
SectionName: helpers.GetPointer[gatewayv1.SectionName]("http"),
125125
},
126126
{
127-
Idx: 5,
127+
Idx: 4,
128128
Gateway: CreateParentRefGateway(gws[gwNsName3]),
129129
SectionName: helpers.GetPointer[gatewayv1.SectionName]("https"),
130130
},

internal/controller/status/prepare_requests.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,32 @@ func PrepareRouteRequests(
9595
return reqs
9696
}
9797

98+
func removeDuplicateIndexParentRefs(parentRefs []graph.ParentRef) []graph.ParentRef {
99+
idxCount := make(map[int]int)
100+
for _, ref := range parentRefs {
101+
idxCount[ref.Idx]++
102+
}
103+
104+
seen := make(map[int]bool)
105+
result := make([]graph.ParentRef, 0, len(parentRefs))
106+
107+
for _, ref := range parentRefs {
108+
if seen[ref.Idx] {
109+
continue // skip duplicates
110+
}
111+
seen[ref.Idx] = true
112+
113+
// If this Idx was duplicated, clear SectionName
114+
if idxCount[ref.Idx] > 1 {
115+
ref.SectionName = nil
116+
}
117+
118+
result = append(result, ref)
119+
}
120+
121+
return result
122+
}
123+
98124
func prepareRouteStatus(
99125
gatewayCtlrName string,
100126
parentRefs []graph.ParentRef,
@@ -103,11 +129,13 @@ func prepareRouteStatus(
103129
transitionTime metav1.Time,
104130
srcGeneration int64,
105131
) v1.RouteStatus {
106-
parents := make([]v1.RouteParentStatus, 0, len(parentRefs))
132+
processedParentRefs := removeDuplicateIndexParentRefs(parentRefs)
133+
134+
parents := make([]v1.RouteParentStatus, 0, len(processedParentRefs))
107135

108136
defaultConds := conditions.NewDefaultRouteConditions()
109137

110-
for _, ref := range parentRefs {
138+
for _, ref := range processedParentRefs {
111139
failedAttachmentCondCount := 0
112140
if ref.Attachment != nil {
113141
failedAttachmentCondCount = len(ref.Attachment.FailedConditions)

internal/controller/status/prepare_requests_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ var (
7272
{
7373
SectionName: helpers.GetPointer[v1.SectionName]("listener-80-3"),
7474
},
75+
{
76+
SectionName: helpers.GetPointer[v1.SectionName]("listener-443-1"),
77+
},
78+
{
79+
SectionName: helpers.GetPointer[v1.SectionName]("listener-443-2"),
80+
},
7581
},
7682
}
7783

@@ -110,6 +116,22 @@ var (
110116
FailedConditions: []conditions.Condition{invalidAttachmentCondition},
111117
},
112118
},
119+
{
120+
Idx: 3,
121+
Gateway: &graph.ParentRefGateway{NamespacedName: gwNsName},
122+
SectionName: commonRouteSpecValid.ParentRefs[3].SectionName,
123+
Attachment: &graph.ParentRefAttachmentStatus{
124+
Attached: true,
125+
},
126+
},
127+
{
128+
Idx: 3,
129+
Gateway: &graph.ParentRefGateway{NamespacedName: gwNsName},
130+
SectionName: commonRouteSpecValid.ParentRefs[4].SectionName,
131+
Attachment: &graph.ParentRefAttachmentStatus{
132+
Attached: true,
133+
},
134+
},
113135
}
114136

115137
parentRefsInvalid = []graph.ParentRef{
@@ -213,6 +235,31 @@ var (
213235
},
214236
},
215237
},
238+
{
239+
ParentRef: v1.ParentReference{
240+
Namespace: helpers.GetPointer(v1.Namespace(gwNsName.Namespace)),
241+
Name: v1.ObjectName(gwNsName.Name),
242+
},
243+
ControllerName: gatewayCtlrName,
244+
Conditions: []metav1.Condition{
245+
{
246+
Type: string(v1.RouteConditionAccepted),
247+
Status: metav1.ConditionTrue,
248+
ObservedGeneration: 3,
249+
LastTransitionTime: transitionTime,
250+
Reason: string(v1.RouteReasonAccepted),
251+
Message: "The route is accepted",
252+
},
253+
{
254+
Type: string(v1.RouteConditionResolvedRefs),
255+
Status: metav1.ConditionTrue,
256+
ObservedGeneration: 3,
257+
LastTransitionTime: transitionTime,
258+
Reason: string(v1.RouteReasonResolvedRefs),
259+
Message: "All references are resolved",
260+
},
261+
},
262+
},
216263
},
217264
}
218265

0 commit comments

Comments
 (0)