Refactor: Replace sync.map with cache pkg for email routine.

KA
1mo ago
+610-108

Description of your changes

This PR replaces the sync.map with the cache.Cache[string] that has been introduced in kubevela/pkg PR to use the centralized package instead of declaring separate sync.map for email routine cache.

Fixes #253

I have:

  • Read and followed KubeVela's contribution process.
  • Related Docs updated properly. In a new feature or configuration option, an update to the documentation is necessary.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR if necessary.

How has this code been tested

Since this PR for the email routine cache hasn't been merged yet, used workspaces to test this feature and this email routine is being used in the kubevela repo.

  1. Create a go workspace
go work init
go work use <workspace-folder-name>
go work use <kubevela-folder-name>
  1. Build the project to ensure the workspace dependency is used.
go build ./...

Note: Avoid running go mod tidy in any repo, as it updates the module graph and throws error saying that package cache doesn't exist in kubevela/pkg

  1. Update the sync-crds target in the KubeVela dependency.mk to disable the workspace when downloading the pinned module version for CRDs.
sync-crds: ## Copy CRD from pinned module version in go.mod
	@moddir=$$(GOWORK=off go mod download -json $(PKG_MODULE) 2>/dev/null | grep '"Dir"' | cut -d'"' -f4); \
	mkdir -p config/crd/base; \
	for file in $(COMMON_CRD_FILES); do \
		src="$$moddir/crds/$$file"; \
		cp -f "$$src" "config/crd/base/"; \
	done

This ensures that the CRDs are copied from the module version pinned in go.mod rather than from the local workspace checkout.

  1. Build and install the updated KubeVela controller.
  2. Deploy a sample application:
vela up -f https://kubevela.io/example/applications/first-app.yaml
  1. Verify that the application is successfully reconciled and deployed, confirming that the local workspace version of github.com/kubevela/workflow and github.com/kubevela/kubevela is being used and that the changes work as expected.

Special notes for your reviewer

This would also require changes in the github.com/kubevela/kubevela repo since we need to initialize the cache and register similar to what has been done with rateLimiter and stepStatusCache.


Summary by cubic

Swaps the per-provider sync.Map tracking email send status for cache.Cache[string] from kubevela/pkg, and guards cache reads with a mutex so concurrent cold invocations can't both dial and send duplicate emails. Completed sends now delete their cache entry, so a later invocation for the same session ID starts a fresh send instead of hanging on the previous result.

Migration

  • Consumers must call the new InitEmailCache(ctx) during startup; the kubevela repo needs a matching change.
  • Requires the kubevela/pkg release that introduces the cache.Cache package.

Tests

  • Adds coverage for exactly-once sends, reconciles arriving during an in-flight send, and concurrent cold invocations never sending duplicates.