diff --git a/cmd/plugins/main.go b/cmd/plugins/main.go index d593712aa..9d17b8228 100644 --- a/cmd/plugins/main.go +++ b/cmd/plugins/main.go @@ -155,6 +155,7 @@ func main() { remoteTokenizerEnabled := utils.LoadEnvBool(constants.EnvPrefixCacheUseRemoteTokenizer, false) cache.InitWithOptions(config, stopCh, cache.InitOptions{ + IsGateway: true, EnableKVSync: kvSyncEnabled && remoteTokenizerEnabled, RedisClient: redisClient, ModelRouterProvider: routing.ModelRouterFactory, diff --git a/pkg/cache/cache_init.go b/pkg/cache/cache_init.go index a8182bfcd..06b17288f 100644 --- a/pkg/cache/cache_init.go +++ b/pkg/cache/cache_init.go @@ -46,6 +46,9 @@ var ( // InitOptions configures the cache initialization behavior type InitOptions struct { + // IsGateway marks the caller as the gateway-plugins service. + IsGateway bool + // EnableKVSync configures whether to start the ZMQ KV event sync EnableKVSync bool @@ -333,14 +336,7 @@ func InitForTest() *Store { func InitWithOptions(config *rest.Config, stopCh <-chan struct{}, opts InitOptions) *Store { once.Do(func() { // Log initialization based on configuration - var service string - if opts.EnableKVSync { - service = "gateway" - } else if opts.RedisClient != nil { - service = "metadata" - } else { - service = "controllers" - } + service := serviceIdentity(opts) klog.InfoS("initialize cache", "service", service, @@ -402,6 +398,18 @@ func InitWithOptions(config *rest.Config, stopCh <-chan struct{}, opts InitOptio return store } +// serviceIdentity returns the component identity implied by opts. +func serviceIdentity(opts InitOptions) string { + switch { + case opts.IsGateway: + return "gateway" + case opts.RedisClient != nil: + return "metadata" + default: + return "controllers" + } +} + // initMetricsCache initializes metrics cache update loop // Parameters: // diff --git a/pkg/cache/cache_init_test.go b/pkg/cache/cache_init_test.go index 6ffe73c6a..c74adbb23 100644 --- a/pkg/cache/cache_init_test.go +++ b/pkg/cache/cache_init_test.go @@ -378,3 +378,54 @@ func TestInitWithOptions_KVSyncBehavior(t *testing.T) { }) } } + +func TestServiceIdentity(t *testing.T) { + scenarios := []struct { + name string + opts InitOptions + expected string + }{ + { + name: "gateway without redis", + opts: InitOptions{ + IsGateway: true, + }, + expected: "gateway", + }, + { + name: "gateway with KV sync disabled is still the gateway", + opts: InitOptions{ + IsGateway: true, + EnableKVSync: false, + RedisClient: &redis.Client{}, + }, + expected: "gateway", + }, + { + name: "metadata service", + opts: InitOptions{ + RedisClient: &redis.Client{}, + }, + expected: "metadata", + }, + { + name: "EnableKVSync alone does not imply gateway", + opts: InitOptions{ + EnableKVSync: true, + RedisClient: &redis.Client{}, + }, + expected: "metadata", + }, + { + name: "controllers", + opts: InitOptions{}, + expected: "controllers", + }, + } + + for _, sc := range scenarios { + t.Run(sc.name, func(t *testing.T) { + assert.Equal(t, sc.expected, serviceIdentity(sc.opts)) + }) + } +}