Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions internal/provider/kubernetes/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (r *gatewayAPIReconciler) subscribeAndUpdateStatus(ctx context.Context, ext
panic(err)
}
hCopy := h.DeepCopy()
hCopy.Status.Parents = mergeRouteParentStatus(h.Namespace, h.Status.Parents, val.Parents)
hCopy.Status.Parents = mergeRouteParentStatus(h.Namespace, r.envoyGateway.Gateway.ControllerName, h.Status.Parents, val.Parents)
return hCopy
}),
})
Expand Down Expand Up @@ -134,7 +134,7 @@ func (r *gatewayAPIReconciler) subscribeAndUpdateStatus(ctx context.Context, ext
panic(err)
}
gCopy := g.DeepCopy()
gCopy.Status.Parents = mergeRouteParentStatus(g.Namespace, g.Status.Parents, val.Parents)
gCopy.Status.Parents = mergeRouteParentStatus(g.Namespace, r.envoyGateway.Gateway.ControllerName, g.Status.Parents, val.Parents)
return gCopy
}),
})
Expand Down Expand Up @@ -166,7 +166,7 @@ func (r *gatewayAPIReconciler) subscribeAndUpdateStatus(ctx context.Context, ext
panic(err)
}
tCopy := t.DeepCopy()
tCopy.Status.Parents = mergeRouteParentStatus(t.Namespace, t.Status.Parents, val.Parents)
tCopy.Status.Parents = mergeRouteParentStatus(t.Namespace, r.envoyGateway.Gateway.ControllerName, t.Status.Parents, val.Parents)
return tCopy
}),
})
Expand Down Expand Up @@ -198,7 +198,7 @@ func (r *gatewayAPIReconciler) subscribeAndUpdateStatus(ctx context.Context, ext
panic(err)
}
tCopy := t.DeepCopy()
tCopy.Status.Parents = mergeRouteParentStatus(t.Namespace, t.Status.Parents, val.Parents)
tCopy.Status.Parents = mergeRouteParentStatus(t.Namespace, r.envoyGateway.Gateway.ControllerName, t.Status.Parents, val.Parents)
return tCopy
}),
})
Expand Down Expand Up @@ -230,7 +230,7 @@ func (r *gatewayAPIReconciler) subscribeAndUpdateStatus(ctx context.Context, ext
panic(err)
}
uCopy := u.DeepCopy()
uCopy.Status.Parents = mergeRouteParentStatus(u.Namespace, u.Status.Parents, val.Parents)
uCopy.Status.Parents = mergeRouteParentStatus(u.Namespace, r.envoyGateway.Gateway.ControllerName, u.Status.Parents, val.Parents)
return uCopy
}),
})
Expand Down Expand Up @@ -501,21 +501,41 @@ func (r *gatewayAPIReconciler) subscribeAndUpdateStatus(ctx context.Context, ext

// mergeRouteParentStatus merges the old and new RouteParentStatus.
// This is needed because the RouteParentStatus doesn't support strategic merge patch yet.
func mergeRouteParentStatus(ns string, old, new []gwapiv1.RouteParentStatus) []gwapiv1.RouteParentStatus {
merged := make([]gwapiv1.RouteParentStatus, len(old))
_ = copy(merged, old)
for _, parent := range new {
func mergeRouteParentStatus(ns, controllerName string, old, new []gwapiv1.RouteParentStatus) []gwapiv1.RouteParentStatus {
// Allocating with worst-case capacity to avoid reallocation.
merged := make([]gwapiv1.RouteParentStatus, 0, len(old)+len(new))

// Range over old status parentRefs in order:
// 1. The parentRef exists in the new status: append the new one to the final status.
// 2. The parentRef doesn't exist in the new status and it's not our controller: append it to the final status.
// 3. The parentRef doesn't exist in the new status, and it is our controller: don't append it to the final status.
for _, oldP := range old {
found := -1
for i, existing := range old {
if isParentRefEqual(parent.ParentRef, existing.ParentRef, ns) {
found = i
for newI, newP := range new {
if isParentRefEqual(oldP.ParentRef, newP.ParentRef, ns) {
found = newI
break
}
}
if found >= 0 {
merged[found] = parent
} else {
merged = append(merged, parent)
merged = append(merged, new[found])
} else if oldP.ControllerName != gwapiv1.GatewayController(controllerName) {
merged = append(merged, oldP)
}
}

// Range over new status parentRefs and make sure every parentRef exists in the final status. If not, append it.
for _, newP := range new {
found := false
for _, mergedP := range merged {
if isParentRefEqual(newP.ParentRef, mergedP.ParentRef, ns) {
found = true
break
}
}

if !found {
merged = append(merged, newP)
}
}
return merged
Expand Down
Loading