@@ -104,7 +104,7 @@ func (r *gatewayAPIReconciler) subscribeAndUpdateStatus(ctx context.Context, ext
104104 panic (err )
105105 }
106106 hCopy := h .DeepCopy ()
107- hCopy .Status .Parents = mergeRouteParentStatus (h .Namespace , h .Status .Parents , val .Parents )
107+ hCopy .Status .Parents = mergeRouteParentStatus (h .Namespace , r . envoyGateway . Gateway . ControllerName , h .Status .Parents , val .Parents )
108108 return hCopy
109109 }),
110110 })
@@ -134,7 +134,7 @@ func (r *gatewayAPIReconciler) subscribeAndUpdateStatus(ctx context.Context, ext
134134 panic (err )
135135 }
136136 gCopy := g .DeepCopy ()
137- gCopy .Status .Parents = mergeRouteParentStatus (g .Namespace , g .Status .Parents , val .Parents )
137+ gCopy .Status .Parents = mergeRouteParentStatus (g .Namespace , r . envoyGateway . Gateway . ControllerName , g .Status .Parents , val .Parents )
138138 return gCopy
139139 }),
140140 })
@@ -166,7 +166,7 @@ func (r *gatewayAPIReconciler) subscribeAndUpdateStatus(ctx context.Context, ext
166166 panic (err )
167167 }
168168 tCopy := t .DeepCopy ()
169- tCopy .Status .Parents = mergeRouteParentStatus (t .Namespace , t .Status .Parents , val .Parents )
169+ tCopy .Status .Parents = mergeRouteParentStatus (t .Namespace , r . envoyGateway . Gateway . ControllerName , t .Status .Parents , val .Parents )
170170 return tCopy
171171 }),
172172 })
@@ -198,7 +198,7 @@ func (r *gatewayAPIReconciler) subscribeAndUpdateStatus(ctx context.Context, ext
198198 panic (err )
199199 }
200200 tCopy := t .DeepCopy ()
201- tCopy .Status .Parents = mergeRouteParentStatus (t .Namespace , t .Status .Parents , val .Parents )
201+ tCopy .Status .Parents = mergeRouteParentStatus (t .Namespace , r . envoyGateway . Gateway . ControllerName , t .Status .Parents , val .Parents )
202202 return tCopy
203203 }),
204204 })
@@ -230,7 +230,7 @@ func (r *gatewayAPIReconciler) subscribeAndUpdateStatus(ctx context.Context, ext
230230 panic (err )
231231 }
232232 uCopy := u .DeepCopy ()
233- uCopy .Status .Parents = mergeRouteParentStatus (u .Namespace , u .Status .Parents , val .Parents )
233+ uCopy .Status .Parents = mergeRouteParentStatus (u .Namespace , r . envoyGateway . Gateway . ControllerName , u .Status .Parents , val .Parents )
234234 return uCopy
235235 }),
236236 })
@@ -501,21 +501,41 @@ func (r *gatewayAPIReconciler) subscribeAndUpdateStatus(ctx context.Context, ext
501501
502502// mergeRouteParentStatus merges the old and new RouteParentStatus.
503503// This is needed because the RouteParentStatus doesn't support strategic merge patch yet.
504- func mergeRouteParentStatus (ns string , old , new []gwapiv1.RouteParentStatus ) []gwapiv1.RouteParentStatus {
505- merged := make ([]gwapiv1.RouteParentStatus , len (old ))
506- _ = copy (merged , old )
507- for _ , parent := range new {
504+ func mergeRouteParentStatus (ns , controllerName string , old , new []gwapiv1.RouteParentStatus ) []gwapiv1.RouteParentStatus {
505+ // Allocating with worst-case capacity to avoid reallocation.
506+ merged := make ([]gwapiv1.RouteParentStatus , 0 , len (old )+ len (new ))
507+
508+ // Range over old status parentRefs in order:
509+ // 1. The parentRef exists in the new status: append the new one to the final status.
510+ // 2. The parentRef doesn't exist in the new status and it's not our controller: append it to the final status.
511+ // 3. The parentRef doesn't exist in the new status, and it is our controller: don't append it to the final status.
512+ for _ , oldP := range old {
508513 found := - 1
509- for i , existing := range old {
510- if isParentRefEqual (parent .ParentRef , existing .ParentRef , ns ) {
511- found = i
514+ for newI , newP := range new {
515+ if isParentRefEqual (oldP .ParentRef , newP .ParentRef , ns ) {
516+ found = newI
512517 break
513518 }
514519 }
515520 if found >= 0 {
516- merged [found ] = parent
517- } else {
518- merged = append (merged , parent )
521+ merged = append (merged , new [found ])
522+ } else if oldP .ControllerName != gwapiv1 .GatewayController (controllerName ) {
523+ merged = append (merged , oldP )
524+ }
525+ }
526+
527+ // Range over new status parentRefs and make sure every parentRef exists in the final status. If not, append it.
528+ for _ , newP := range new {
529+ found := false
530+ for _ , mergedP := range merged {
531+ if isParentRefEqual (newP .ParentRef , mergedP .ParentRef , ns ) {
532+ found = true
533+ break
534+ }
535+ }
536+
537+ if ! found {
538+ merged = append (merged , newP )
519539 }
520540 }
521541 return merged
0 commit comments