Skip to content

Commit 115aa85

Browse files
authored
feat: changed to support both v1 and v1a2 ip in EPP (#1277)
* changed to support both v1 and v1a2 ip # Conflicts: # pkg/epp/controller/inferencemodel_reconciler.go # pkg/epp/datastore/datastore.go # pkg/epp/server/controller_manager.go # pkg/epp/server/runserver.go # Conflicts: # cmd/epp/runner/runner.go * rebase with main * support both v1 and v1a2 IP * change import order * fixed imports * fixed pipeline * fixed comments Signed-off-by: Xiyue Yu <[email protected]> * fixed merge failure * fixed missing arguments Signed-off-by: Xiyue Yu <[email protected]> * fixed boilplate * pass verify * rebase main * changed to avoid duplicate code * changed logger info --------- Signed-off-by: Xiyue Yu <[email protected]>
1 parent ce55fe6 commit 115aa85

File tree

12 files changed

+470
-49
lines changed

12 files changed

+470
-49
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ RUN go mod download
1818

1919
# Sources
2020
COPY cmd/epp ./cmd/epp
21+
COPY pkg/common ./pkg/common
2122
COPY pkg/epp ./pkg/epp
2223
COPY internal ./internal
2324
COPY apix ./apix

cmd/epp/runner/runner.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"go.uber.org/zap/zapcore"
3333
"google.golang.org/grpc"
3434
healthPb "google.golang.org/grpc/health/grpc_health_v1"
35+
"k8s.io/apimachinery/pkg/runtime/schema"
3536
"k8s.io/apimachinery/pkg/types"
3637
ctrl "sigs.k8s.io/controller-runtime"
3738
"sigs.k8s.io/controller-runtime/pkg/log"
@@ -41,6 +42,7 @@ import (
4142
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
4243

4344
"sigs.k8s.io/gateway-api-inference-extension/internal/runnable"
45+
"sigs.k8s.io/gateway-api-inference-extension/pkg/common"
4446
backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics"
4547
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/config/loader"
4648
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore"
@@ -82,6 +84,10 @@ var (
8284
"pool-name",
8385
runserver.DefaultPoolName,
8486
"Name of the InferencePool this Endpoint Picker is associated with.")
87+
poolGroup = flag.String(
88+
"pool-group",
89+
runserver.DefaultPoolGroup,
90+
"group of the InferencePool this Endpoint Picker is associated with.")
8591
poolNamespace = flag.String(
8692
"pool-namespace",
8793
runserver.DefaultPoolNamespace,
@@ -301,7 +307,15 @@ func (r *Runner) Run(ctx context.Context) error {
301307
Name: *poolName,
302308
Namespace: *poolNamespace,
303309
}
304-
mgr, err := runserver.NewDefaultManager(poolNamespacedName, cfg, metricsServerOptions)
310+
poolGroupKind := schema.GroupKind{
311+
Group: *poolGroup,
312+
Kind: "InferencePool",
313+
}
314+
poolGKNN := common.GKNN{
315+
NamespacedName: poolNamespacedName,
316+
GroupKind: poolGroupKind,
317+
}
318+
mgr, err := runserver.NewDefaultManager(poolGKNN, cfg, metricsServerOptions)
305319
if err != nil {
306320
setupLog.Error(err, "Failed to create controller manager")
307321
return err
@@ -344,6 +358,7 @@ func (r *Runner) Run(ctx context.Context) error {
344358
DestinationEndpointHintKey: *destinationEndpointHintKey,
345359
FairnessIDHeaderKey: *fairnessIDHeaderKey,
346360
PoolNamespacedName: poolNamespacedName,
361+
PoolGKNN: poolGKNN,
347362
Datastore: datastore,
348363
SecureServing: *secureServing,
349364
HealthChecking: *healthChecking,

pkg/common/convert.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package common defines structs for referring to fully qualified k8s resources.
18+
package common
19+
20+
import (
21+
"fmt"
22+
23+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24+
"k8s.io/apimachinery/pkg/runtime"
25+
26+
v1 "sigs.k8s.io/gateway-api-inference-extension/api/v1"
27+
"sigs.k8s.io/gateway-api-inference-extension/apix/v1alpha2"
28+
)
29+
30+
func ToUnstructured(obj any) (*unstructured.Unstructured, error) {
31+
u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
32+
if err != nil {
33+
return nil, err
34+
}
35+
return &unstructured.Unstructured{Object: u}, nil
36+
}
37+
38+
var ToInferencePool = convert[v1.InferencePool]
39+
40+
var ToXInferencePool = convert[v1alpha2.InferencePool]
41+
42+
func convert[T any](u *unstructured.Unstructured) (*T, error) {
43+
var res T
44+
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, &res); err != nil {
45+
return nil, fmt.Errorf("error converting unstructured to T: %v", err)
46+
}
47+
return &res, nil
48+
}

pkg/common/kubemeta.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package common defines structs for referring to fully qualified k8s resources.
18+
package common
19+
20+
import (
21+
"fmt"
22+
"strings"
23+
24+
"k8s.io/apimachinery/pkg/runtime/schema"
25+
"k8s.io/apimachinery/pkg/types"
26+
)
27+
28+
// GKNN represents a fully qualified k8s resource.
29+
type GKNN struct {
30+
types.NamespacedName
31+
schema.GroupKind
32+
}
33+
34+
// String implements Stringer.
35+
func (g *GKNN) String() string {
36+
return fmt.Sprintf("%s %s", g.GroupKind.String(), g.NamespacedName.String())
37+
}
38+
39+
// Compare returns the comparison of a and b where less than, equal, and greater than return -1, 0,
40+
// and 1 respectively.
41+
func Compare(a, b GKNN) int {
42+
if v := strings.Compare(a.Group, b.Group); v != 0 {
43+
return v
44+
}
45+
if v := strings.Compare(a.Kind, b.Kind); v != 0 {
46+
return v
47+
}
48+
if v := strings.Compare(a.Namespace, b.Namespace); v != 0 {
49+
return v
50+
}
51+
return strings.Compare(a.Name, b.Name)
52+
}
53+
54+
// Less returns true if a is less than b.
55+
func Less(a, b GKNN) bool {
56+
return Compare(a, b) < 0
57+
}

pkg/epp/controller/inferenceobjective_reconciler.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ import (
2929
"sigs.k8s.io/controller-runtime/pkg/predicate"
3030

3131
"sigs.k8s.io/gateway-api-inference-extension/apix/v1alpha2"
32+
"sigs.k8s.io/gateway-api-inference-extension/pkg/common"
3233
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore"
3334
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
3435
)
3536

3637
type InferenceObjectiveReconciler struct {
3738
client.Reader
38-
Datastore datastore.Datastore
39-
PoolNamespacedName types.NamespacedName
39+
Datastore datastore.Datastore
40+
PoolGKNN common.GKNN
4041
}
4142

4243
func (c *InferenceObjectiveReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
@@ -55,7 +56,7 @@ func (c *InferenceObjectiveReconciler) Reconcile(ctx context.Context, req ctrl.R
5556
notFound = true
5657
}
5758

58-
if notFound || !infObjective.DeletionTimestamp.IsZero() || infObjective.Spec.PoolRef.Name != v1alpha2.ObjectName(c.PoolNamespacedName.Name) {
59+
if notFound || !infObjective.DeletionTimestamp.IsZero() || infObjective.Spec.PoolRef.Name != v1alpha2.ObjectName(c.PoolGKNN.Name) {
5960
// InferenceObjective object got deleted or changed the referenced pool.
6061
err := c.handleObjectiveDeleted(ctx, req.NamespacedName)
6162
return ctrl.Result{}, err
@@ -125,5 +126,5 @@ func (c *InferenceObjectiveReconciler) SetupWithManager(ctx context.Context, mgr
125126
}
126127

127128
func (c *InferenceObjectiveReconciler) eventPredicate(infObjective *v1alpha2.InferenceObjective) bool {
128-
return string(infObjective.Spec.PoolRef.Name) == c.PoolNamespacedName.Name
129+
return string(infObjective.Spec.PoolRef.Name) == c.PoolGKNN.Name
129130
}

pkg/epp/controller/inferenceobjective_reconciler_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/google/go-cmp/cmp"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/runtime"
27+
"k8s.io/apimachinery/pkg/runtime/schema"
2728
"k8s.io/apimachinery/pkg/types"
2829
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2930
ctrl "sigs.k8s.io/controller-runtime"
@@ -32,6 +33,7 @@ import (
3233

3334
v1 "sigs.k8s.io/gateway-api-inference-extension/api/v1"
3435
"sigs.k8s.io/gateway-api-inference-extension/apix/v1alpha2"
36+
"sigs.k8s.io/gateway-api-inference-extension/pkg/common"
3537
backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics"
3638
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore"
3739
utiltest "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/testing"
@@ -203,9 +205,12 @@ func TestInferenceObjectiveReconciler(t *testing.T) {
203205
}
204206
_ = ds.PoolSet(context.Background(), fakeClient, pool)
205207
reconciler := &InferenceObjectiveReconciler{
206-
Reader: fakeClient,
207-
Datastore: ds,
208-
PoolNamespacedName: types.NamespacedName{Name: pool.Name, Namespace: pool.Namespace},
208+
Reader: fakeClient,
209+
Datastore: ds,
210+
PoolGKNN: common.GKNN{
211+
NamespacedName: types.NamespacedName{Name: pool.Name, Namespace: pool.Namespace},
212+
GroupKind: schema.GroupKind{Group: pool.GroupVersionKind().Group, Kind: pool.GroupVersionKind().Kind},
213+
},
209214
}
210215
if test.incomingReq == nil {
211216
test.incomingReq = &types.NamespacedName{Name: test.objective.Name, Namespace: test.objective.Namespace}

pkg/epp/controller/inferencepool_reconciler.go

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ package controller
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"k8s.io/apimachinery/pkg/api/errors"
24+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2325
ctrl "sigs.k8s.io/controller-runtime"
2426
"sigs.k8s.io/controller-runtime/pkg/client"
2527
"sigs.k8s.io/controller-runtime/pkg/log"
2628

2729
v1 "sigs.k8s.io/gateway-api-inference-extension/api/v1"
30+
"sigs.k8s.io/gateway-api-inference-extension/apix/v1alpha2"
31+
"sigs.k8s.io/gateway-api-inference-extension/pkg/common"
2832
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore"
2933
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
3034
)
@@ -35,31 +39,72 @@ import (
3539
type InferencePoolReconciler struct {
3640
client.Reader
3741
Datastore datastore.Datastore
42+
PoolGKNN common.GKNN
3843
}
3944

4045
func (c *InferencePoolReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
41-
logger := log.FromContext(ctx).WithValues("inferencePool", req.NamespacedName).V(logutil.DEFAULT)
46+
logger := log.FromContext(ctx).WithValues("group", c.PoolGKNN.Group, "inferencePool", req.NamespacedName).V(logutil.DEFAULT)
4247
ctx = ctrl.LoggerInto(ctx, logger)
4348

44-
logger.Info("Reconciling InferencePool")
49+
logger.Info("Reconciling InferencePool", "group", c.PoolGKNN.Group, "inferencePool", req.NamespacedName)
4550

46-
infPool := &v1.InferencePool{}
51+
// 1. Initialize a generic client.Object based on the group.
52+
var obj client.Object
53+
switch c.PoolGKNN.Group {
54+
case v1.GroupName:
55+
obj = &v1.InferencePool{}
56+
case v1alpha2.GroupName:
57+
obj = &v1alpha2.InferencePool{}
58+
default:
59+
// Handle unsupported groups gracefully.
60+
err := fmt.Errorf("unsupported API group: %s", c.PoolGKNN.Group)
61+
logger.Error(err, "Cannot reconcile InferencePool")
62+
return ctrl.Result{}, err
63+
}
4764

48-
if err := c.Get(ctx, req.NamespacedName, infPool); err != nil {
65+
// 2. Perform a single, generic fetch for the object.
66+
if err := c.Get(ctx, req.NamespacedName, obj); err != nil {
4967
if errors.IsNotFound(err) {
5068
logger.Info("InferencePool not found. Clearing the datastore")
5169
c.Datastore.Clear()
5270
return ctrl.Result{}, nil
5371
}
5472
logger.Error(err, "Unable to get InferencePool")
5573
return ctrl.Result{}, err
56-
} else if !infPool.DeletionTimestamp.IsZero() {
74+
}
75+
76+
// 3. Perform common checks using the client.Object interface.
77+
if !obj.GetDeletionTimestamp().IsZero() {
5778
logger.Info("InferencePool is marked for deletion. Clearing the datastore")
5879
c.Datastore.Clear()
5980
return ctrl.Result{}, nil
6081
}
61-
// update pool in datastore
62-
if err := c.Datastore.PoolSet(ctx, c.Reader, infPool); err != nil {
82+
83+
// 4. Convert the fetched object to the canonical v1.InferencePool.
84+
var v1infPool *v1.InferencePool
85+
86+
switch pool := obj.(type) {
87+
case *v1.InferencePool:
88+
// If it's already a v1 object, just use it.
89+
v1infPool = pool
90+
case *v1alpha2.InferencePool:
91+
// If it's a v1alpha2 object, convert it to v1.
92+
var uns *unstructured.Unstructured
93+
uns, err := common.ToUnstructured(pool)
94+
if err != nil {
95+
logger.Error(err, "Failed to convert inferencePool to unstructured")
96+
return ctrl.Result{}, err
97+
}
98+
v1infPool, err = common.ToInferencePool(uns)
99+
if err != nil {
100+
logger.Error(err, "Failed to convert unstructured to inferencePool")
101+
return ctrl.Result{}, err
102+
}
103+
default:
104+
return ctrl.Result{}, fmt.Errorf("unsupported API group: %s", c.PoolGKNN.Group)
105+
}
106+
107+
if err := c.Datastore.PoolSet(ctx, c.Reader, v1infPool); err != nil {
63108
logger.Error(err, "Failed to update datastore")
64109
return ctrl.Result{}, err
65110
}
@@ -68,7 +113,16 @@ func (c *InferencePoolReconciler) Reconcile(ctx context.Context, req ctrl.Reques
68113
}
69114

70115
func (c *InferencePoolReconciler) SetupWithManager(mgr ctrl.Manager) error {
71-
return ctrl.NewControllerManagedBy(mgr).
72-
For(&v1.InferencePool{}).
73-
Complete(c)
116+
switch c.PoolGKNN.Group {
117+
case v1alpha2.GroupName:
118+
return ctrl.NewControllerManagedBy(mgr).
119+
For(&v1alpha2.InferencePool{}).
120+
Complete(c)
121+
case v1.GroupName:
122+
return ctrl.NewControllerManagedBy(mgr).
123+
For(&v1.InferencePool{}).
124+
Complete(c)
125+
default:
126+
return fmt.Errorf("unknown group %s", c.PoolGKNN.Group)
127+
}
74128
}

0 commit comments

Comments
 (0)