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
42 changes: 30 additions & 12 deletions pkg/context/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,82 @@ limitations under the License.
package context

import (
"context"
"fmt"
"sync"

v1 "k8s.io/api/core/v1"

"github.com/kubevela/pkg/cache"
)

var (
// EnableInMemoryContext optimize workflow context storage by storing it in memory instead of etcd
EnableInMemoryContext = false
)

// inMemoryContextStorage holds live workflow contexts, not a disposable cache:
// entries are stored without expiration and are only removed explicitly.
type inMemoryContextStorage struct {
// mu serializes writes so that GetOrCreateInMemoryContext is atomic.
mu sync.Mutex
contexts map[string]*v1.ConfigMap
contexts cache.Cache[string]
}

// MemStore store in-memory context
var MemStore = &inMemoryContextStorage{
contexts: map[string]*v1.ConfigMap{},
contexts: cache.NewMemoryCacheStore[string](context.Background()),
}

func (o *inMemoryContextStorage) getKey(cm *v1.ConfigMap) string {
ns := cm.GetNamespace()
func contextKey(name, ns string) string {
if ns == "" {
ns = "default"
}
name := cm.GetName()
return ns + "/" + name
}

func (o *inMemoryContextStorage) getKey(cm *v1.ConfigMap) string {
return contextKey(cm.GetName(), cm.GetNamespace())
}

func (o *inMemoryContextStorage) load(key string) *v1.ConfigMap {
if obj, ok := o.contexts.Get(key); ok {
return obj.(*v1.ConfigMap)
}
return nil
}

func (o *inMemoryContextStorage) GetOrCreateInMemoryContext(cm *v1.ConfigMap) {
if obj := o.GetInMemoryContext(cm.Name, cm.Namespace); obj != nil {
o.mu.Lock()
defer o.mu.Unlock()
if obj := o.load(o.getKey(cm)); obj != nil {
obj.DeepCopyInto(cm)
} else {
o.CreateInMemoryContext(cm)
return
}
cm.Data = map[string]string{}
o.contexts.Put(o.getKey(cm), cm, 0)
}

func (o *inMemoryContextStorage) GetInMemoryContext(name, ns string) *v1.ConfigMap {
return o.contexts[ns+"/"+name]
return o.load(contextKey(name, ns))
}

func (o *inMemoryContextStorage) CreateInMemoryContext(cm *v1.ConfigMap) {
o.mu.Lock()
defer o.mu.Unlock()
cm.Data = map[string]string{}
o.contexts[o.getKey(cm)] = cm
o.contexts.Put(o.getKey(cm), cm, 0)
}

func (o *inMemoryContextStorage) UpdateInMemoryContext(cm *v1.ConfigMap) {
o.mu.Lock()
defer o.mu.Unlock()
o.contexts[o.getKey(cm)] = cm
o.contexts.Put(o.getKey(cm), cm, 0)
}

func (o *inMemoryContextStorage) DeleteInMemoryContext(appName string) {
o.mu.Lock()
defer o.mu.Unlock()
key := fmt.Sprintf("workflow-%s-context", appName)
delete(o.contexts, key)
o.contexts.Delete(key)
}
145 changes: 145 additions & 0 deletions pkg/context/storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
Copyright 2026 The KubeVela Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package context

import (
"context"
"errors"
"fmt"
"sync"
"testing"

"cuelang.org/go/cue/cuecontext"
"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kubevela/pkg/cache"
"github.com/kubevela/pkg/util/singleton"
)

func newStoreForTest() *inMemoryContextStorage {
return &inMemoryContextStorage{
contexts: cache.NewMemoryCacheStore[string](context.Background()),
}
}

func newConfigMap(name, ns string) *corev1.ConfigMap {
return &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: ns}}
}

func TestInMemoryContextStorage(t *testing.T) {
r := require.New(t)
store := newStoreForTest()

r.Nil(store.GetInMemoryContext("workflow-app-context", "prod"))

cm := newConfigMap("workflow-app-context", "prod")
cm.Data = map[string]string{"stale": "value"}
store.CreateInMemoryContext(cm)
r.Empty(cm.Data, "create should reset the context data")
r.Same(cm, store.GetInMemoryContext("workflow-app-context", "prod"))
r.Nil(store.GetInMemoryContext("workflow-app-context", "other"))

updated := newConfigMap("workflow-app-context", "prod")
updated.Data = map[string]string{ConfigMapKeyVars: "vars"}
store.UpdateInMemoryContext(updated)
r.Equal("vars", store.GetInMemoryContext("workflow-app-context", "prod").Data[ConfigMapKeyVars])

// An empty namespace resolves to "default" for both reads and writes.
store.CreateInMemoryContext(newConfigMap("workflow-app-context", ""))
r.NotNil(store.GetInMemoryContext("workflow-app-context", ""))
r.NotNil(store.GetInMemoryContext("workflow-app-context", "default"))
}

func TestGetOrCreateInMemoryContext(t *testing.T) {
r := require.New(t)
store := newStoreForTest()

created := newConfigMap("workflow-app-context", "prod")
store.GetOrCreateInMemoryContext(created)
r.NotNil(created.Data)
created.Data[ConfigMapKeyVars] = "vars"

loaded := newConfigMap("workflow-app-context", "prod")
store.GetOrCreateInMemoryContext(loaded)
r.Equal("vars", loaded.Data[ConfigMapKeyVars])
r.NotSame(created, loaded, "existing context should be copied, not shared")
}

func TestInMemoryContextStorageConcurrentGetOrCreate(t *testing.T) {
r := require.New(t)
store := newStoreForTest()

const workers = 16
start := make(chan struct{})
results := make([]*corev1.ConfigMap, workers)
var wg sync.WaitGroup
for i := 0; i < workers; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
cm := newConfigMap("workflow-app-context", "prod")
cm.Annotations = map[string]string{"worker": fmt.Sprint(i)}
<-start
store.GetOrCreateInMemoryContext(cm)
results[i] = cm
}(i)
}
close(start)
wg.Wait()

stored := store.GetInMemoryContext("workflow-app-context", "prod")
r.NotNil(stored)
winner := stored.Annotations["worker"]
for i, cm := range results {
r.Equal(winner, cm.Annotations["worker"], "worker %d did not get the stored context", i)
}
}

func TestWorkflowContextInMemory(t *testing.T) {
r := require.New(t)
ctx := context.Background()

origEnabled, origStore := EnableInMemoryContext, MemStore
t.Cleanup(func() { EnableInMemoryContext, MemStore = origEnabled, origStore })
EnableInMemoryContext = true
MemStore = newStoreForTest()

// With in-memory context enabled, the context must not be read from or created in the API server.
errUnexpected := errors.New("unexpected API server call")
singleton.KubeClient.Set(&test.MockClient{
MockGet: test.NewMockGetFn(errUnexpected),
MockCreate: test.NewMockCreateFn(errUnexpected),
MockPatch: test.NewMockPatchFn(nil),
})

wfCtx, err := NewContext(ctx, "prod", "app", nil)
r.NoError(err)
r.NoError(wfCtx.SetVar(cuecontext.New().CompileString(`"hello"`), "greeting"))
r.NoError(wfCtx.Commit(ctx))
r.NotNil(MemStore.GetInMemoryContext(generateStoreName("app"), "prod"))

loaded, err := LoadContext(ctx, "prod", "app", generateStoreName("app"))
r.NoError(err)
v, err := loaded.GetVar("greeting")
r.NoError(err)
s, err := v.String()
r.NoError(err)
r.Equal("hello", s)
}
Loading