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
9 changes: 5 additions & 4 deletions pkg/context/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package context

import (
"context"
"fmt"
"sync"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -90,9 +89,11 @@ func (o *inMemoryContextStorage) UpdateInMemoryContext(cm *v1.ConfigMap) {
o.contexts.Put(o.getKey(cm), cm, 0)
}

func (o *inMemoryContextStorage) DeleteInMemoryContext(appName string) {
// DeleteInMemoryContext drops the stored context of an application. The key
// has to be built the same way getKey builds it, with the namespace included,
// or the delete silently matches nothing.
func (o *inMemoryContextStorage) DeleteInMemoryContext(appName, namespace string) {
o.mu.Lock()
defer o.mu.Unlock()
key := fmt.Sprintf("workflow-%s-context", appName)
o.contexts.Delete(key)
o.contexts.Delete(contextKey(generateStoreName(appName), namespace))
}
29 changes: 28 additions & 1 deletion pkg/context/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"testing"

"cuelang.org/go/cue/cuecontext"
"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/crossplane/crossplane-runtime/v2/pkg/test"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -82,6 +82,33 @@ func TestGetOrCreateInMemoryContext(t *testing.T) {
r.NotSame(created, loaded, "existing context should be copied, not shared")
}

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

store.CreateInMemoryContext(newConfigMap(generateStoreName("app"), "prod"))
store.CreateInMemoryContext(newConfigMap(generateStoreName("app"), "staging"))
r.NotNil(store.GetInMemoryContext(generateStoreName("app"), "prod"))

store.DeleteInMemoryContext("app", "prod")
r.Nil(store.GetInMemoryContext(generateStoreName("app"), "prod"), "delete should remove the stored context")
r.NotNil(store.GetInMemoryContext(generateStoreName("app"), "staging"), "delete should leave another namespace's context alone")

// The key is defaulted the same way reads and writes default it, in both
// directions: stored under "", deleted as "default", and the reverse.
store.CreateInMemoryContext(newConfigMap(generateStoreName("app"), ""))
store.DeleteInMemoryContext("app", "default")
r.Nil(store.GetInMemoryContext(generateStoreName("app"), "default"), "an empty namespace is the default namespace")

store.CreateInMemoryContext(newConfigMap(generateStoreName("app"), "default"))
store.DeleteInMemoryContext("app", "")
r.Nil(store.GetInMemoryContext(generateStoreName("app"), ""), "delete should default an empty namespace the same way")

// Deleting something that was never stored is a no-op.
store.DeleteInMemoryContext("missing", "prod")
r.Nil(store.GetInMemoryContext(generateStoreName("missing"), "prod"))
}

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