Skip to content

Commit fd58743

Browse files
bjee19ciarams87
authored andcommitted
Add fix for inference pool status (#4088)
When setting inference pool statuses, it loops through all the inference pools and checks if there are any nginx gateways that have parentRefs in the statuses. If there are AND the infernece pool is not referenced (not connected to the graph), it will remove that parentRef.
1 parent 3778e08 commit fd58743

File tree

5 files changed

+251
-44
lines changed

5 files changed

+251
-44
lines changed

deploy/inference-nginx-plus/deploy.yaml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ metadata:
1515
namespace: nginx-gateway
1616
---
1717
apiVersion: v1
18+
automountServiceAccountToken: false
1819
kind: ServiceAccount
1920
metadata:
2021
labels:
@@ -54,15 +55,33 @@ metadata:
5455
rules:
5556
- apiGroups:
5657
- ""
57-
- apps
58-
- autoscaling
5958
resources:
6059
- secrets
6160
- configmaps
6261
- serviceaccounts
6362
- services
63+
verbs:
64+
- create
65+
- update
66+
- delete
67+
- list
68+
- get
69+
- watch
70+
- apiGroups:
71+
- apps
72+
resources:
6473
- deployments
6574
- daemonsets
75+
verbs:
76+
- create
77+
- update
78+
- delete
79+
- list
80+
- get
81+
- watch
82+
- apiGroups:
83+
- autoscaling
84+
resources:
6685
- horizontalpodautoscalers
6786
verbs:
6887
- create
@@ -358,6 +377,7 @@ spec:
358377
metadata:
359378
annotations: null
360379
spec:
380+
automountServiceAccountToken: true
361381
containers:
362382
- args:
363383
- generate-certs

deploy/inference/deploy.yaml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ metadata:
1515
namespace: nginx-gateway
1616
---
1717
apiVersion: v1
18+
automountServiceAccountToken: false
1819
kind: ServiceAccount
1920
metadata:
2021
labels:
@@ -54,15 +55,33 @@ metadata:
5455
rules:
5556
- apiGroups:
5657
- ""
57-
- apps
58-
- autoscaling
5958
resources:
6059
- secrets
6160
- configmaps
6261
- serviceaccounts
6362
- services
63+
verbs:
64+
- create
65+
- update
66+
- delete
67+
- list
68+
- get
69+
- watch
70+
- apiGroups:
71+
- apps
72+
resources:
6473
- deployments
6574
- daemonsets
75+
verbs:
76+
- create
77+
- update
78+
- delete
79+
- list
80+
- get
81+
- watch
82+
- apiGroups:
83+
- autoscaling
84+
resources:
6685
- horizontalpodautoscalers
6786
verbs:
6887
- create
@@ -354,6 +373,7 @@ spec:
354373
metadata:
355374
annotations: null
356375
spec:
376+
automountServiceAccountToken: true
357377
containers:
358378
- args:
359379
- generate-certs

internal/controller/handler.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"k8s.io/client-go/tools/record"
1919
"sigs.k8s.io/controller-runtime/pkg/client"
2020
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
21+
inference "sigs.k8s.io/gateway-api-inference-extension/api/v1"
2122
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
2223

2324
ngfAPI "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha1"
@@ -368,7 +369,28 @@ func (h *eventHandlerImpl) updateStatuses(ctx context.Context, gr *graph.Graph,
368369
transitionTime,
369370
h.cfg.gatewayCtlrName,
370371
)
371-
inferencePoolReqs := status.PrepareInferencePoolRequests(gr.ReferencedInferencePools, transitionTime)
372+
373+
// unfortunately, status is not on clusterState stored by the change processor, so we need to make a k8sAPI call here
374+
ipList := &inference.InferencePoolList{}
375+
err = h.cfg.k8sClient.List(ctx, ipList)
376+
if err != nil {
377+
msg := "error listing InferencePools for status update"
378+
h.cfg.logger.Error(err, msg)
379+
h.cfg.eventRecorder.Eventf(
380+
&inference.InferencePoolList{},
381+
v1.EventTypeWarning,
382+
"ListInferencePoolsFailed",
383+
msg+": %s",
384+
err.Error(),
385+
)
386+
ipList = &inference.InferencePoolList{} // reset to empty list to avoid nil pointer dereference
387+
}
388+
inferencePoolReqs := status.PrepareInferencePoolRequests(
389+
gr.ReferencedInferencePools,
390+
ipList,
391+
gr.Gateways,
392+
transitionTime,
393+
)
372394

373395
reqs := make(
374396
[]status.UpdateRequest,

internal/controller/status/prepare_requests.go

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,64 @@ func PrepareNginxGatewayStatus(
523523

524524
// PrepareInferencePoolRequests prepares status UpdateRequests for the given InferencePools.
525525
func PrepareInferencePoolRequests(
526-
inferencePools map[types.NamespacedName]*graph.ReferencedInferencePool,
526+
referencedInferencePools map[types.NamespacedName]*graph.ReferencedInferencePool,
527+
clusterInferencePoolList *inference.InferencePoolList,
528+
referencedGateways map[types.NamespacedName]*graph.Gateway,
527529
transitionTime metav1.Time,
528530
) []UpdateRequest {
529-
reqs := make([]UpdateRequest, 0, len(inferencePools))
531+
reqs := make([]UpdateRequest, 0, len(referencedInferencePools))
532+
533+
// Create parent references from referenced gateways
534+
nginxGatewayParentRefs := make([]inference.ParentReference, 0, len(referencedGateways))
535+
for _, gateway := range referencedGateways {
536+
parentRef := inference.ParentReference{
537+
Name: inference.ObjectName(gateway.Source.GetName()),
538+
Namespace: inference.Namespace(gateway.Source.GetNamespace()),
539+
Group: helpers.GetPointer(inference.Group(gateway.Source.GroupVersionKind().Group)),
540+
Kind: kinds.Gateway,
541+
}
542+
nginxGatewayParentRefs = append(nginxGatewayParentRefs, parentRef)
543+
}
544+
545+
if clusterInferencePoolList != nil {
546+
for _, pool := range clusterInferencePoolList.Items {
547+
nsname := types.NamespacedName{
548+
Namespace: pool.Namespace,
549+
Name: pool.Name,
550+
}
551+
552+
// If the pool is in the cluster, but not referenced, we need to check
553+
// if any of its parents are an nginx Gateway, if so, we need to remove them.
554+
if referencedInferencePools[nsname] == nil {
555+
// represents parentRefs that are NOT nginx gateways
556+
filteredParents := make([]inference.ParentStatus, 0, len(pool.Status.Parents))
557+
for _, parent := range pool.Status.Parents {
558+
// if the parent.ParentRef is not in the list of nginx gateways, keep it
559+
// otherwise, we are removing it from the status
560+
if !containsParentReference(nginxGatewayParentRefs, parent.ParentRef) {
561+
filteredParents = append(filteredParents, parent)
562+
}
563+
}
564+
565+
// Create an update request to set the filtered parents
566+
if len(filteredParents) != len(pool.Status.Parents) {
567+
status := inference.InferencePoolStatus{
568+
Parents: filteredParents,
569+
}
570+
571+
req := UpdateRequest{
572+
NsName: nsname,
573+
ResourceType: &inference.InferencePool{},
574+
Setter: newInferencePoolStatusSetter(status),
575+
}
530576

531-
for nsname, pool := range inferencePools {
577+
reqs = append(reqs, req)
578+
}
579+
}
580+
}
581+
}
582+
583+
for nsname, pool := range referencedInferencePools {
532584
if pool.Source == nil {
533585
continue
534586
}
@@ -573,3 +625,17 @@ func PrepareInferencePoolRequests(
573625

574626
return reqs
575627
}
628+
629+
// containsParentReference checks if a ParentReference exists in a slice of ParentReferences
630+
// by comparing Name, Namespace, and Kind fields.
631+
func containsParentReference(parentRefs []inference.ParentReference, target inference.ParentReference) bool {
632+
for _, ref := range parentRefs {
633+
if ref.Name == target.Name &&
634+
ref.Namespace == target.Namespace &&
635+
ref.Kind == target.Kind {
636+
return true
637+
}
638+
}
639+
640+
return false
641+
}

0 commit comments

Comments
 (0)