diff --git a/pkg/context/storage.go b/pkg/context/storage.go index fcf0680f..6cc9f900 100644 --- a/pkg/context/storage.go +++ b/pkg/context/storage.go @@ -18,7 +18,6 @@ package context import ( "context" - "fmt" "sync" v1 "k8s.io/api/core/v1" @@ -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)) } diff --git a/pkg/context/storage_test.go b/pkg/context/storage_test.go index 9d91300b..4f405aa2 100644 --- a/pkg/context/storage_test.go +++ b/pkg/context/storage_test.go @@ -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" @@ -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()