Repository: nginx/nginx-gateway-fabric

Issue:
Panic when preparing route statuses
**Describe the bug**
Sometimes there can be a panic when building the route statuses by accessing an index out of range. I believe it occurs when a Route is configured without a sectionName, referencing a Gateway with a single listener. I also had multiple different Gateway API implementations deployed, so that could play a role in this as well.

The code basically attempts to do some de-duplication of shared indices of ParentRefs in this scenario, but accesses an index that doesn't exist.

Discussion:

Solution patch:
diff --git a/internal/controller/status/prepare_requests.go b/internal/controller/status/prepare_requests.go
index 4cb8fd5e..f1909c15 100644
--- a/internal/controller/status/prepare_requests.go
+++ b/internal/controller/status/prepare_requests.go
@@ -103,11 +103,11 @@ func removeDuplicateIndexParentRefs(parentRefs []graph.ParentRef) []graph.Parent
 		idxToParentRef[ref.Idx] = append(idxToParentRef[ref.Idx], ref)
 	}
 
-	results := make([]graph.ParentRef, len(idxToParentRef))
+	results := make([]graph.ParentRef, 0, len(idxToParentRef))
 
 	for idx, refs := range idxToParentRef {
 		if len(refs) == 1 {
-			results[idx] = refs[0]
+			results = append(results, refs[0])
 			continue
 		}
 
@@ -124,7 +124,7 @@ func removeDuplicateIndexParentRefs(parentRefs []graph.ParentRef) []graph.Parent
 				}
 			}
 		}
-		results[idx] = winningParentRef
+		results = append(results, winningParentRef)
 	}
 
 	return results



Test patch:
diff --git a/internal/controller/status/prepare_requests_test.go b/internal/controller/status/prepare_requests_test.go
index 2480423d..f813f183 100644
--- a/internal/controller/status/prepare_requests_test.go
+++ b/internal/controller/status/prepare_requests_test.go
@@ -445,7 +445,7 @@ func TestBuildHTTPRouteStatuses(t *testing.T) {
 
 		err := k8sClient.Get(context.Background(), nsname, &hr)
 		g.Expect(err).ToNot(HaveOccurred())
-		g.Expect(helpers.Diff(expected, hr.Status)).To(BeEmpty())
+		g.Expect(expected.RouteStatus.Parents).To(ConsistOf(hr.Status.Parents))
 	}
 }
 
@@ -524,7 +524,7 @@ func TestBuildGRPCRouteStatuses(t *testing.T) {
 
 		err := k8sClient.Get(context.Background(), nsname, &hr)
 		g.Expect(err).ToNot(HaveOccurred())
-		g.Expect(helpers.Diff(expected, hr.Status)).To(BeEmpty())
+		g.Expect(expected.RouteStatus.Parents).To(ConsistOf(hr.Status.Parents))
 	}
 }
 
@@ -601,7 +601,7 @@ func TestBuildTLSRouteStatuses(t *testing.T) {
 
 		err := k8sClient.Get(context.Background(), nsname, &hr)
 		g.Expect(err).ToNot(HaveOccurred())
-		g.Expect(helpers.Diff(expected, hr.Status)).To(BeEmpty())
+		g.Expect(expected.RouteStatus.Parents).To(ConsistOf(hr.Status.Parents))
 	}
 }