Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

refactor(wrc): let NetworkPolicy convergence take the caller's owned annotation keys

Alfredo Lopez0ba0f96aae30
diff --git a/workflow-recipes/package-lock.json b/workflow-recipes/package-lock.json
index 9ccb0173f..7617292a8 100644
--- a/workflow-recipes/package-lock.json
+++ b/workflow-recipes/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "workload-recipes-controller",
- "version": "0.1.371",
+ "version": "0.1.372",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "workload-recipes-controller",
- "version": "0.1.371",
+ "version": "0.1.372",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@aws-sdk/client-s3": "^3.1009.0",
diff --git a/workflow-recipes/package.json b/workflow-recipes/package.json
index 7c16112ed..bc2495ef8 100644
--- a/workflow-recipes/package.json
+++ b/workflow-recipes/package.json
@@ -1,6 +1,6 @@
{
"name": "workload-recipes-controller",
- "version": "0.1.371",
+ "version": "0.1.372",
"description": "Workload Recipes Controller (WRC) - WorkloadRecipe lifecycle management and MCP server interface",
"main": "dist/main.js",
"engines": {
diff --git a/workflow-recipes/src/reconciler/networkPolicyConvergence.test.ts b/workflow-recipes/src/reconciler/networkPolicyConvergence.test.ts
index c69694851..939f5d490 100644
--- a/workflow-recipes/src/reconciler/networkPolicyConvergence.test.ts
+++ b/workflow-recipes/src/reconciler/networkPolicyConvergence.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest'
import * as k8s from '@kubernetes/client-node'
import {
+ buildNetworkPolicyReplacement,
classifyNetworkPolicyOwnership,
decideNetworkPolicyConvergence,
networkPolicyMatchesDesired,
@@ -9,6 +10,37 @@ import {
const SPEC_HASH = 'clerum.io/spec-hash'
+// The run lane's runtime HTTP egress state keys (workflowReconciler.ts). They
+// are module-private there, so the literal strings are repeated here.
+const RUN_LANE_PREVIOUS_CIDRS = 'clerum.io/runtime-http-egress-previous-cidrs'
+const RUN_LANE_OWNED_ANNOTATIONS = new Set([
+ 'clerum.io/runtime-http-egress-current-cidrs',
+ RUN_LANE_PREVIOUS_CIDRS,
+ 'clerum.io/runtime-http-egress-previous-expires-at',
+ 'clerum.io/runtime-http-egress-previous-cidr-expiries',
+ 'clerum.io/runtime-http-egress-resolved-at',
+])
+
+function runLanePolicy(): k8s.V1NetworkPolicy {
+ return {
+ apiVersion: 'networking.k8s.io/v1',
+ kind: 'NetworkPolicy',
+ metadata: {
+ name: 'r-coord-to-wrc',
+ namespace: 'sandbox-recipes',
+ labels: { 'clerum.io/managed-by': 'wrc', 'clerum.io/recipe': 'r' },
+ annotations: {
+ 'clerum.io/runtime-http-egress-current-cidrs': '203.0.113.10/32',
+ },
+ },
+ spec: {
+ podSelector: { matchLabels: { 'clerum.io/recipe': 'r' } },
+ policyTypes: ['Egress'],
+ egress: [{ to: [{ ipBlock: { cidr: '203.0.113.10/32' } }] }],
+ },
+ }
+}
+
// These minimal fixtures isolate comparison and ownership edge cases. Real
// producer output and Kubernetes serialization are covered by the property suite.
function policy(): k8s.V1NetworkPolicy {
@@ -278,4 +310,57 @@ describe('NetworkPolicy live convergence', () => {
reason: 'terminating',
})
})
+
+ describe('caller-owned annotation keys', () => {
+ function liveWithStalePreviousCidrs(desired: k8s.V1NetworkPolicy): k8s.V1NetworkPolicy {
+ const live = structuredClone(desired)
+ live.metadata!.resourceVersion = '7'
+ live.metadata!.annotations = {
+ ...desired.metadata!.annotations,
+ [RUN_LANE_PREVIOUS_CIDRS]: '198.51.100.4/32',
+ 'admission.example/audit': 'true',
+ }
+ return live
+ }
+
+ it('repairs a stale owned key absent from desired and drops it from the PUT body', () => {
+ const desired = runLanePolicy()
+ const live = liveWithStalePreviousCidrs(desired)
+
+ expect(networkPolicyMatchesDesired(desired, live, RUN_LANE_OWNED_ANNOTATIONS)).toBe(false)
+ expect(
+ decideNetworkPolicyConvergence('workload-egress', desired, live, RUN_LANE_OWNED_ANNOTATIONS)
+ ).toEqual({ action: 'replace', reason: 'live-drift' })
+
+ const body = buildNetworkPolicyReplacement(desired, live, RUN_LANE_OWNED_ANNOTATIONS)
+ expect(body.metadata!.annotations).toEqual({
+ 'clerum.io/runtime-http-egress-current-cidrs': '203.0.113.10/32',
+ 'admission.example/audit': 'true',
+ })
+ expect(body.metadata!.resourceVersion).toBe('7')
+ })
+
+ it('keeps the default owned set: an unknown key is preserved and does not count as drift', () => {
+ const desired = runLanePolicy()
+ const live = liveWithStalePreviousCidrs(desired)
+
+ expect(networkPolicyMatchesDesired(desired, live)).toBe(true)
+ expect(decideNetworkPolicyConvergence('workload-egress', desired, live)).toEqual({
+ action: 'unchanged',
+ })
+ expect(
+ buildNetworkPolicyReplacement(desired, live).metadata!.annotations![RUN_LANE_PREVIOUS_CIDRS]
+ ).toBe('198.51.100.4/32')
+ })
+
+ it('reports the converged run-lane policy as unchanged with the caller-owned set', () => {
+ const desired = runLanePolicy()
+ const live = structuredClone(desired)
+ live.metadata!.resourceVersion = '7'
+
+ expect(
+ decideNetworkPolicyConvergence('workload-egress', desired, live, RUN_LANE_OWNED_ANNOTATIONS)
+ ).toEqual({ action: 'unchanged' })
+ })
+ })
})
diff --git a/workflow-recipes/src/reconciler/networkPolicyConvergence.ts b/workflow-recipes/src/reconciler/networkPolicyConvergence.ts
index b59c27fc3..2a7096498 100644
--- a/workflow-recipes/src/reconciler/networkPolicyConvergence.ts
+++ b/workflow-recipes/src/reconciler/networkPolicyConvergence.ts
@@ -58,29 +58,38 @@ const EGRESS_STATE_ANNOTATIONS = new Set([
function projectOwnedAnnotations(
desired: Record<string, string> | undefined,
existing: Record<string, string> | undefined,
- temporal = false
+ temporal = false,
+ ownedAnnotationKeys: ReadonlySet<string> = EGRESS_STATE_ANNOTATIONS
): Record<string, string> {
- const keys = new Set([...Object.keys(desired ?? {}), ...EGRESS_STATE_ANNOTATIONS])
+ const keys = new Set([...Object.keys(desired ?? {}), ...ownedAnnotationKeys])
return Object.fromEntries(
[...keys].flatMap(key => {
if (key === SPEC_HASH_ANNOTATION) return []
// The accumulator owns renewal cadence for present temporal state. Its
// removal still has to reach apply, even when the enforced rules match.
- if (temporal && EGRESS_STATE_ANNOTATIONS.has(key) && desired?.[key] !== undefined) return []
+ if (temporal && ownedAnnotationKeys.has(key) && desired?.[key] !== undefined) return []
return existing?.[key] === undefined ? [] : [[key, existing[key]]]
})
)
}
-/** Build a PUT from the same live snapshot used for ownership and drift checks. */
+/**
+ * Build a PUT from the same live snapshot used for ownership and drift checks.
+ *
+ * `ownedAnnotationKeys` names the annotations this caller authors even when
+ * they are absent from desired: a live value for one of them is removed rather
+ * than carried over. The default is the recipe lane's egress state keys; a
+ * caller with its own state keys must pass the same set to the comparison.
+ */
export function buildNetworkPolicyReplacement(
desired: k8s.V1NetworkPolicy,
- existing: k8s.V1NetworkPolicy
+ existing: k8s.V1NetworkPolicy,
+ ownedAnnotationKeys: ReadonlySet<string> = EGRESS_STATE_ANNOTATIONS
): k8s.V1NetworkPolicy {
const replacement = structuredClone(desired)
const liveMetadata = existing.metadata ?? {}
const annotations = { ...liveMetadata.annotations }
- for (const key of EGRESS_STATE_ANNOTATIONS) delete annotations[key]
+ for (const key of ownedAnnotationKeys) delete annotations[key]
Object.assign(annotations, desired.metadata?.annotations)
delete annotations[SPEC_HASH_ANNOTATION]
replacement.metadata = {
@@ -128,7 +137,8 @@ function projectAuthoredMap(
function projectNetworkPolicyForComparison(
desired: k8s.V1NetworkPolicy,
- candidate: k8s.V1NetworkPolicy
+ candidate: k8s.V1NetworkPolicy,
+ ownedAnnotationKeys: ReadonlySet<string>
): k8s.V1NetworkPolicy {
const desiredMetadata = desired.metadata ?? {}
const candidateMetadata = candidate.metadata ?? {}
@@ -141,7 +151,9 @@ function projectNetworkPolicyForComparison(
labels: projectAuthoredMap(desiredMetadata.labels, candidateMetadata.labels),
annotations: projectOwnedAnnotations(
desiredMetadata.annotations,
- candidateMetadata.annotations
+ candidateMetadata.annotations,
+ false,
+ ownedAnnotationKeys
),
...(desiredMetadata.ownerReferences?.length
? { ownerReferences: candidateMetadata.ownerReferences }
@@ -182,16 +194,21 @@ function normalizeProjectedNetworkPolicy(normalized: k8s.V1NetworkPolicy): unkno
export function networkPolicyMatchesDesired(
desired: k8s.V1NetworkPolicy | undefined,
- existing: k8s.V1NetworkPolicy | undefined
+ existing: k8s.V1NetworkPolicy | undefined,
+ ownedAnnotationKeys: ReadonlySet<string> = EGRESS_STATE_ANNOTATIONS
): boolean {
try {
if (!desired?.spec || !existing?.spec) return false
return (
JSON.stringify(
- normalizeProjectedNetworkPolicy(projectNetworkPolicyForComparison(desired, desired))
+ normalizeProjectedNetworkPolicy(
+ projectNetworkPolicyForComparison(desired, desired, ownedAnnotationKeys)
+ )
) ===
JSON.stringify(
- normalizeProjectedNetworkPolicy(projectNetworkPolicyForComparison(desired, existing))
+ normalizeProjectedNetworkPolicy(
+ projectNetworkPolicyForComparison(desired, existing, ownedAnnotationKeys)
+ )
)
)
} catch {
@@ -326,7 +343,8 @@ export function classifyNetworkPolicyOwnership(
export function decideNetworkPolicyConvergence(
family: NetworkPolicyFamily,
desired: k8s.V1NetworkPolicy,
- existing: k8s.V1NetworkPolicy
+ existing: k8s.V1NetworkPolicy,
+ ownedAnnotationKeys: ReadonlySet<string> = EGRESS_STATE_ANNOTATIONS
): NetworkPolicyConvergenceDecision {
if (existing.metadata?.deletionTimestamp) return { action: 'retry', reason: 'terminating' }
@@ -335,6 +353,8 @@ export function decideNetworkPolicyConvergence(
if (ownership.kind === 'repairable-owner') {
return { action: 'replace', reason: 'owner-repair' }
}
- if (networkPolicyMatchesDesired(desired, existing)) return { action: 'unchanged' }
+ if (networkPolicyMatchesDesired(desired, existing, ownedAnnotationKeys)) {
+ return { action: 'unchanged' }
+ }
return { action: 'replace', reason: 'live-drift' }
}