Skip to content

Commit 244ade5

Browse files
committed
move interfaces from backend.metrics to datalayer
Signed-off-by: Etai Lev Ran <elevran@gmail.com>
1 parent ffbc6a3 commit 244ade5

File tree

5 files changed

+54
-19
lines changed

5 files changed

+54
-19
lines changed

pkg/epp/backend/metrics/logger.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/go-logr/logr"
2525
"sigs.k8s.io/controller-runtime/pkg/log"
2626

27-
v1 "sigs.k8s.io/gateway-api-inference-extension/api/v1"
27+
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datalayer"
2828
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metrics"
2929
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
3030
)
@@ -33,15 +33,9 @@ const (
3333
debugPrintInterval = 5 * time.Second
3434
)
3535

36-
type Datastore interface {
37-
PoolGet() (*v1.InferencePool, error)
38-
// PodMetrics operations
39-
PodList(func(PodMetrics) bool) []PodMetrics
40-
}
41-
4236
// StartMetricsLogger starts goroutines to 1) Print metrics debug logs if the DEBUG log level is
4337
// enabled; 2) flushes Prometheus metrics about the backend servers.
44-
func StartMetricsLogger(ctx context.Context, datastore Datastore, refreshPrometheusMetricsInterval, metricsStalenessThreshold time.Duration) {
38+
func StartMetricsLogger(ctx context.Context, datastore datalayer.PoolInfo, refreshPrometheusMetricsInterval, metricsStalenessThreshold time.Duration) {
4539
logger := log.FromContext(ctx)
4640
ticker := time.NewTicker(refreshPrometheusMetricsInterval)
4741
go func() {
@@ -82,7 +76,7 @@ func StartMetricsLogger(ctx context.Context, datastore Datastore, refreshPrometh
8276
}
8377
}
8478

85-
func refreshPrometheusMetrics(logger logr.Logger, datastore Datastore, metricsStalenessThreshold time.Duration) {
79+
func refreshPrometheusMetrics(logger logr.Logger, datastore datalayer.PoolInfo, metricsStalenessThreshold time.Duration) {
8680
pool, err := datastore.PoolGet()
8781
if err != nil {
8882
// No inference pool or not initialize.

pkg/epp/backend/metrics/pod_metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type podMetrics struct {
4040
pod atomic.Pointer[backend.Pod]
4141
metrics atomic.Pointer[MetricsState]
4242
pmc PodMetricsClient
43-
ds Datastore
43+
ds datalayer.PoolInfo
4444
interval time.Duration
4545

4646
startOnce sync.Once // ensures the refresh loop goroutine is started only once

pkg/epp/backend/metrics/types.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ var (
3232
AllPodPredicate = func(PodMetrics) bool { return true }
3333
)
3434

35-
// EndpointFactory defines an interface for allocating and retiring endpoints.
36-
type EndpointFactory interface {
37-
NewEndpoint(parent context.Context, in *corev1.Pod, ds Datastore) PodMetrics
38-
ReleaseEndpoint(ep PodMetrics)
39-
}
40-
4135
func NewPodMetricsFactory(pmc PodMetricsClient, refreshMetricsInterval, metricsStalenessThreshold time.Duration) *PodMetricsFactory {
4236
return &PodMetricsFactory{
4337
pmc: pmc,
@@ -52,7 +46,7 @@ type PodMetricsFactory struct {
5246
metricsStalenessThreshold time.Duration
5347
}
5448

55-
func (f *PodMetricsFactory) NewEndpoint(parentCtx context.Context, in *corev1.Pod, ds Datastore) PodMetrics {
49+
func (f *PodMetricsFactory) NewEndpoint(parentCtx context.Context, in *corev1.Pod, ds datalayer.PoolInfo) PodMetrics {
5650
pod := toInternalPod(in)
5751
pm := &podMetrics{
5852
pmc: f.pmc,

pkg/epp/datalayer/factory.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 datalayer
18+
19+
import (
20+
"context"
21+
22+
corev1 "k8s.io/api/core/v1"
23+
24+
v1 "sigs.k8s.io/gateway-api-inference-extension/api/v1"
25+
)
26+
27+
// PoolInfo represents the DataStore information needed for endpoints.
28+
// TODO:
29+
// Consider if to remove/simplify in follow-ups. This is mostly for backward
30+
// compatibility with backend.metrics' expectations and allowing a shared
31+
// implementation during the transition.
32+
// - Endpoint metric scraping uses PoolGet to access the pool's Port and Name.
33+
// - Global metrics logging uses PoolGet solely for error return and PodList to enumerate
34+
// all endpoints for metrics summarization.
35+
type PoolInfo interface {
36+
PoolGet() (*v1.InferencePool, error)
37+
PodList(func(Endpoint) bool) []Endpoint
38+
}
39+
40+
// EndpointFactory defines an interface for managing Endpoint lifecycle. Specifically,
41+
// providing methods to allocate and retire endpoints. This can potentially be used for
42+
// pooled memory or other management chores in the implementation.
43+
type EndpointFactory interface {
44+
NewEndpoint(parent context.Context, inpod *corev1.Pod, poolinfo PoolInfo) Endpoint
45+
ReleaseEndpoint(ep Endpoint)
46+
}

pkg/epp/datastore/datastore.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
v1 "sigs.k8s.io/gateway-api-inference-extension/api/v1"
3333
"sigs.k8s.io/gateway-api-inference-extension/apix/v1alpha2"
3434
backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics"
35+
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datalayer"
3536
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
3637
podutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/pod"
3738
)
@@ -66,7 +67,7 @@ type Datastore interface {
6667
Clear()
6768
}
6869

69-
func NewDatastore(parentCtx context.Context, epFactory backendmetrics.EndpointFactory) Datastore {
70+
func NewDatastore(parentCtx context.Context, epFactory datalayer.EndpointFactory) Datastore {
7071
store := &datastore{
7172
parentCtx: parentCtx,
7273
poolAndObjectivesMu: sync.RWMutex{},
@@ -87,7 +88,7 @@ type datastore struct {
8788
objectives map[string]*v1alpha2.InferenceObjective
8889
// key: types.NamespacedName, value: backendmetrics.PodMetrics
8990
pods *sync.Map
90-
epf backendmetrics.EndpointFactory
91+
epf datalayer.EndpointFactory
9192
}
9293

9394
func (ds *datastore) Clear() {

0 commit comments

Comments
 (0)