Skip to content

Commit a399f6d

Browse files
committed
feat: Add GetActivePods functionto plugins handle and datastore
Signed-off-by: Kfir Toledo <[email protected]>
1 parent f92d5bf commit a399f6d

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

cmd/epp/runner/runner.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func (r *Runner) Run(ctx context.Context) error {
336336
}
337337
}
338338

339-
err = r.parsePluginsConfiguration(ctx)
339+
err = r.parsePluginsConfiguration(ctx, datastore)
340340
if err != nil {
341341
setupLog.Error(err, "Failed to parse plugins configuration")
342342
return err
@@ -411,7 +411,7 @@ func (r *Runner) registerInTreePlugins() {
411411
plugins.Register(testfilter.HeaderBasedTestingFilterType, testfilter.HeaderBasedTestingFilterFactory)
412412
}
413413

414-
func (r *Runner) parsePluginsConfiguration(ctx context.Context) error {
414+
func (r *Runner) parsePluginsConfiguration(ctx context.Context, ds datastore.Datastore) error {
415415
if *configText == "" && *configFile == "" {
416416
return nil // configuring through code, not through file
417417
}
@@ -430,8 +430,9 @@ func (r *Runner) parsePluginsConfiguration(ctx context.Context) error {
430430
}
431431

432432
r.registerInTreePlugins()
433-
handle := plugins.NewEppHandle(ctx)
433+
handle := plugins.NewEppHandle(ctx, ds.GetActivePods)
434434
config, err := loader.LoadConfig(configBytes, handle, logger)
435+
435436
if err != nil {
436437
return fmt.Errorf("failed to load the configuration - %w", err)
437438
}

pkg/epp/datastore/datastore.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type Datastore interface {
6161
PodList(predicate func(backendmetrics.PodMetrics) bool) []backendmetrics.PodMetrics
6262
PodUpdateOrAddIfNotExist(pod *corev1.Pod) bool
6363
PodDelete(namespacedName types.NamespacedName)
64+
GetActivePods() []types.NamespacedName
6465

6566
// Clears the store state, happens when the pool gets deleted.
6667
Clear()
@@ -225,6 +226,16 @@ func (ds *datastore) PodUpdateOrAddIfNotExist(pod *corev1.Pod) bool {
225226
return ok
226227
}
227228

229+
// GetActivePods returns a list of all active pods.
230+
func (ds *datastore) GetActivePods() []types.NamespacedName {
231+
var namespacedNames []types.NamespacedName
232+
ds.pods.Range(func(k, _ any) bool {
233+
namespacedNames = append(namespacedNames, k.(types.NamespacedName))
234+
return true
235+
})
236+
return namespacedNames
237+
}
238+
228239
func (ds *datastore) PodDelete(namespacedName types.NamespacedName) {
229240
v, ok := ds.pods.LoadAndDelete(namespacedName)
230241
if ok {

pkg/epp/plugins/handle.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package plugins
1919
import (
2020
"context"
2121
"fmt"
22+
23+
"k8s.io/apimachinery/pkg/types"
2224
)
2325

2426
// Handle provides plugins a set of standard data and tools to work with
@@ -27,6 +29,9 @@ type Handle interface {
2729
Context() context.Context
2830

2931
HandlePlugins
32+
33+
// GetActivePods returns a list of all active pods
34+
GetActivePods() []types.NamespacedName
3035
}
3136

3237
// HandlePlugins defines a set of APIs to work with instantiated plugins
@@ -44,10 +49,14 @@ type HandlePlugins interface {
4449
GetAllPluginsWithNames() map[string]Plugin
4550
}
4651

52+
// GetActivePodsFunc is a function that returns a list of all active pods.
53+
type GetActivePodsFunc func() []types.NamespacedName
54+
4755
// eppHandle is an implementation of the interface plugins.Handle
4856
type eppHandle struct {
4957
ctx context.Context
5058
HandlePlugins
59+
getActivePods GetActivePodsFunc
5160
}
5261

5362
// Context returns a context the plugins can use, if they need one
@@ -84,7 +93,12 @@ func (h *eppHandlePlugins) GetAllPluginsWithNames() map[string]Plugin {
8493
return h.plugins
8594
}
8695

87-
func NewEppHandle(ctx context.Context) Handle {
96+
// GetActivePods returns a function that returns a list of all active pods
97+
func (h *eppHandle) GetActivePods() []types.NamespacedName {
98+
return h.getActivePods()
99+
}
100+
101+
func NewEppHandle(ctx context.Context, getActivePods GetActivePodsFunc) Handle {
88102
return &eppHandle{
89103
ctx: ctx,
90104
HandlePlugins: &eppHandlePlugins{

test/utils/handle.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package utils
1919
import (
2020
"context"
2121

22+
k8stypes "k8s.io/apimachinery/pkg/types"
23+
2224
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
2325
)
2426

@@ -33,6 +35,10 @@ func (h *testHandle) Context() context.Context {
3335
return h.ctx
3436
}
3537

38+
func (h *testHandle) GetActivePods() []k8stypes.NamespacedName {
39+
return []k8stypes.NamespacedName{}
40+
}
41+
3642
type testHandlePlugins struct {
3743
plugins map[string]plugins.Plugin
3844
}

0 commit comments

Comments
 (0)