Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 14 additions & 99 deletions api/v1alpha1/cachebackend_effective.go
Original file line number Diff line number Diff line change
@@ -1,126 +1,41 @@
package v1alpha1

import (
"strings"
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// UsesCanonicalCacheHierarchy reports whether the resource uses the separated
// runtime/cache/storage API. Legacy resources are detected by the absence of
// these fields and retain their historical implicit provider mapping.
func (s *CacheBackendSpec) UsesCanonicalCacheHierarchy() bool {
return s.Runtime != "" || s.LMCache != nil || s.RemoteStorage != nil
}

// EffectiveRuntime returns the canonical inference runtime while preserving
// integration.engine as a read-time compatibility input.
// EffectiveRuntime returns the configured inference runtime.
func (s *CacheBackendSpec) EffectiveRuntime() CacheBackendRuntime {
if s.Runtime != "" {
return normalizeRuntime(s.Runtime)
}
if s.Integration != nil {
if runtime := normalizeRuntime(CacheBackendRuntime(s.Integration.Engine)); runtime != "" {
return runtime
}
}
return CacheBackendRuntimeVLLM
return s.Runtime
}

// EffectiveCacheType returns the engine-side cache implementation. Legacy
// Mooncake and External values represented remote-provider concerns in
// spec.type; both use LMCache engine wiring and normalize to LMCache here.
// EffectiveCacheType returns the engine-side cache implementation, defaulting
// an omitted value to LMCache for callers that do not pass through admission.
func (s *CacheBackendSpec) EffectiveCacheType() CacheBackendType {
switch s.Type {
case CacheBackendTypeMooncake, CacheBackendTypeExternal:
return CacheBackendTypeLMCache
case "":
if s.Type == "" {
return CacheBackendTypeLMCache
default:
return s.Type
}
return s.Type
}

// EffectiveRemoteStorage returns the explicit remote-storage declaration, or a
// synthesized declaration for a legacy resource. In the canonical API a nil
// remoteStorage remains nil: host-only caching must never select a provider as
// an adapter side effect.
// EffectiveRemoteStorage returns the explicit remote-storage declaration. A
// nil remoteStorage remains nil: host-only caching must never select a provider
// as an adapter side effect.
func (s *CacheBackendSpec) EffectiveRemoteStorage() *CacheBackendRemoteStorageSpec {
if s.RemoteStorage != nil {
return s.RemoteStorage
}
if s.UsesCanonicalCacheHierarchy() {
return nil
}

switch s.Type {
case CacheBackendTypeExternal:
return &CacheBackendRemoteStorageSpec{
Provider: CacheBackendRemoteStorageProviderLMCacheServer,
Ownership: CacheBackendRemoteStorageOwnershipExternal,
Endpoint: s.Endpoint,
}
case CacheBackendTypeMooncake:
return &CacheBackendRemoteStorageSpec{
Provider: CacheBackendRemoteStorageProviderMooncake,
Ownership: CacheBackendRemoteStorageOwnershipManaged,
Mooncake: &MooncakeRemoteStorageSpec{
Image: s.BackendConfig["serverImage"],
Resources: s.Resources,
},
}
case CacheBackendTypeLMCache, "":
if s.EffectiveRuntime() == CacheBackendRuntimeSGLang {
return &CacheBackendRemoteStorageSpec{
Provider: CacheBackendRemoteStorageProviderRedis,
Ownership: CacheBackendRemoteStorageOwnershipManaged,
Redis: &RedisRemoteStorageSpec{
Image: s.BackendConfig["redisImage"],
Resources: s.Resources,
},
}
}
return &CacheBackendRemoteStorageSpec{
Provider: CacheBackendRemoteStorageProviderLMCacheServer,
Ownership: CacheBackendRemoteStorageOwnershipManaged,
LMCacheServer: &LMCacheServerRemoteStorageSpec{
Image: s.BackendConfig["serverImage"],
Resources: s.Resources,
},
}
default:
return nil
}
return s.RemoteStorage
}

// EffectiveObservationModelID returns the independently-owned observation
// model id while retaining backendConfig.model for legacy resources.
// model id.
func (s *CacheBackendSpec) EffectiveObservationModelID() string {
if s.Observation != nil {
return s.Observation.ModelID
}
return s.BackendConfig["model"]
return ""
}

// EffectiveFirstEventTimeout returns the observation-owned timeout, falling
// back to the deprecated integration field for compatibility.
// EffectiveFirstEventTimeout returns the observation-owned timeout.
func (s *CacheBackendSpec) EffectiveFirstEventTimeout() *metav1.Duration {
if s.Observation != nil && s.Observation.FirstEventTimeout != nil {
return s.Observation.FirstEventTimeout
}
if s.Integration != nil {
return s.Integration.FirstEventTimeout
}
return nil
}

func normalizeRuntime(value CacheBackendRuntime) CacheBackendRuntime {
switch strings.ToLower(string(value)) {
case "vllm":
return CacheBackendRuntimeVLLM
case "sglang":
return CacheBackendRuntimeSGLang
default:
return value
}
}
69 changes: 18 additions & 51 deletions api/v1alpha1/cachebackend_effective_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,35 @@ package v1alpha1

import "testing"

func TestEffectiveRemoteStorageSeparatesCanonicalAndLegacyHierarchy(t *testing.T) {
t.Run("canonical omission is host-only", func(t *testing.T) {
spec := CacheBackendSpec{
Runtime: CacheBackendRuntimeSGLang,
Type: CacheBackendTypeLMCache,
}
if got := spec.EffectiveRemoteStorage(); got != nil {
t.Fatalf("EffectiveRemoteStorage() = %+v, want nil", got)
}
})

t.Run("legacy sglang lmcache keeps redis compatibility", func(t *testing.T) {
spec := CacheBackendSpec{
Type: CacheBackendTypeLMCache,
Integration: &CacheBackendIntegrationSpec{
Engine: "sglang",
},
}
got := spec.EffectiveRemoteStorage()
if got == nil ||
got.Provider != CacheBackendRemoteStorageProviderRedis ||
got.Ownership != CacheBackendRemoteStorageOwnershipManaged {
t.Fatalf("EffectiveRemoteStorage() = %+v, want Managed Redis", got)
}
})
func TestEffectiveRemoteStorageUsesOnlyExplicitDeclaration(t *testing.T) {
spec := CacheBackendSpec{Type: CacheBackendTypeLMCache}
if got := spec.EffectiveRemoteStorage(); got != nil {
t.Fatalf("EffectiveRemoteStorage() = %+v, want nil", got)
}

t.Run("legacy mooncake normalizes engine cache separately", func(t *testing.T) {
spec := CacheBackendSpec{Type: CacheBackendTypeMooncake}
if got := spec.EffectiveCacheType(); got != CacheBackendTypeLMCache {
t.Fatalf("EffectiveCacheType() = %q, want LMCache", got)
}
storage := spec.EffectiveRemoteStorage()
if storage == nil || storage.Provider != CacheBackendRemoteStorageProviderMooncake {
t.Fatalf("EffectiveRemoteStorage() = %+v, want Mooncake", storage)
}
})
want := &CacheBackendRemoteStorageSpec{
Provider: CacheBackendRemoteStorageProviderMooncake,
Ownership: CacheBackendRemoteStorageOwnershipManaged,
}
spec.RemoteStorage = want
if got := spec.EffectiveRemoteStorage(); got != want {
t.Fatalf("EffectiveRemoteStorage() = %+v, want explicit declaration %+v", got, want)
}
}

func TestEffectiveRuntimePrefersCanonicalField(t *testing.T) {
spec := CacheBackendSpec{
Runtime: CacheBackendRuntimeSGLang,
Integration: &CacheBackendIntegrationSpec{
Engine: "vllm",
},
}
func TestEffectiveRuntimeReturnsConfiguredField(t *testing.T) {
spec := CacheBackendSpec{Runtime: CacheBackendRuntimeSGLang}
if got := spec.EffectiveRuntime(); got != CacheBackendRuntimeSGLang {
t.Fatalf("EffectiveRuntime() = %q, want SGLang", got)
}
}

func TestObservationDoesNotSelectCanonicalHierarchy(t *testing.T) {
func TestObservationDoesNotSynthesizeRemoteStorage(t *testing.T) {
spec := CacheBackendSpec{
Type: CacheBackendTypeLMCache,
Observation: &CacheBackendObservationSpec{ModelID: "model-a"},
}
if spec.UsesCanonicalCacheHierarchy() {
t.Fatal("typed observation must remain independent from cache/provider hierarchy selection")
}
got := spec.EffectiveRemoteStorage()
if got == nil ||
got.Provider != CacheBackendRemoteStorageProviderLMCacheServer ||
got.Ownership != CacheBackendRemoteStorageOwnershipManaged {
t.Fatalf("EffectiveRemoteStorage() = %+v, want legacy Managed LMCacheServer", got)
if got := spec.EffectiveRemoteStorage(); got != nil {
t.Fatalf("EffectiveRemoteStorage() = %+v, want nil", got)
}
}
Loading
Loading