Fix: delete an in-memory context under the key it was stored as

Merged
MA
6d ago
+33-5

Description of your changes

Fixes #333

DeleteInMemoryContext built its key as workflow-<app>-context, but every write and read goes through contextKey, which is <namespace>/workflow-<app>-context (getKey, storage.go:39). The two could never match, so the delete was a no-op and the context of a deleted Application stayed in memory for the life of the controller whenever EnableInMemoryContext was on.

  • DeleteInMemoryContext(appName) now takes (appName, namespace) and builds the key with contextKey(generateStoreName(appName), namespace), the same helpers the store writes with. fmt is no longer needed in the file.
  • New TestDeleteInMemoryContext: deleting removes that namespace's context, leaves another namespace's alone, defaults an empty namespace the same way reads and writes do (both directions), and is a no-op for a context that was never stored.

Two things worth your attention:

  1. The signature change breaks the call site in kubevela/kubevela (application_controller.go calls MemStore.DeleteInMemoryContext(app.Name)); that repo has to pass app.Namespace after it bumps this module. The issue says the same, and a variadic or an extra method would leave the broken no-op delete reachable.
  2. pkg/context/storage_test.go still imported github.com/crossplane/crossplane-runtime/pkg/test after the v2 dependency bump, so go test ./pkg/context/ fails to build on main right now with missing go.sum entry for module providing package github.com/crossplane/crossplane-runtime/pkg/test. I changed that one line to /v2/pkg/test; otherwise the package's tests, including the new one, cannot run. Nothing else from the v1 module is imported anywhere in the repo (git grep crossplane-runtime/pkg), so this is the only stale import.

I have:

  • Read and followed KubeVela's contribution process.
  • Related Docs updated properly. (No user-facing configuration changed.)
  • Run make reviewable for the packages I touched: gofmt -l pkg/context clean, go vet ./pkg/context/ clean. I did not run the full target because it regenerates manifests and CRDs unrelated to this change.
  • Added backport release-x.y labels.

How has this code been tested

  • go test ./pkg/context/ — ok, all tests in the package pass.
  • Fault injection: restoring the old key (contextKey(generateStoreName(app.Name), ""), namespace dropped) makes the new test fail at storage_test.go:94 with "delete should remove the stored context"; the fix makes it pass. So the test fails for the reason the issue describes and not only for the signature.
  • The new test drives the exported methods only; the controller-level effect (a deleted Application's context going away) is not exercised here because MemStore is package-global and the call site lives in the other repo.

Special notes for your reviewer

The namespace argument is deliberately explicit rather than defaulted inside the delete: the store already treats "" as default, and the test pins both directions, so a caller that forgets app.Namespace gets default — which is what NewContext writes for a cluster-scoped or default-namespace Application.


Summary by cubic

Fixes in-memory context deletion so the stored context of a deleted Application is actually removed instead of remaining in memory for the life of the controller.

DeleteInMemoryContext built its key as workflow-<app>-context, but every read and write uses <namespace>/workflow-<app>-context; the keys never matched, so the delete was a no-op.

  • The method now takes (appName, namespace) and builds the key with the same helpers reads and writes use. This is a breaking change for callers, who must pass app.Namespace.
  • Adds TestDeleteInMemoryContext covering per-namespace deletion, empty-namespace defaulting in both directions, and no-ops on never-stored contexts.
  • Fixes a stale crossplane-runtime v1 import in pkg/context/storage_test.go that broke go test ./pkg/context/.