Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
feat(hcc): read before creating GFS resources
Alfredo Lopez87c9f1059f9b
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.jsonindex b89eaa68a..626217c80 100644--- a/host-context-controller/package-lock.json+++ b/host-context-controller/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-host-context-controller",- "version": "0.1.304",+ "version": "0.1.305","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-host-context-controller",- "version": "0.1.304",+ "version": "0.1.305","license": "SEE LICENSE IN LICENSE","dependencies": {"@clerum/codex-catalog-projection": "file:../packages/codex-catalog-projection",diff --git a/host-context-controller/package.json b/host-context-controller/package.jsonindex 5efd6438d..e3773afa1 100644--- a/host-context-controller/package.json+++ b/host-context-controller/package.json@@ -1,6 +1,6 @@{"name": "clerum-host-context-controller",- "version": "0.1.304",+ "version": "0.1.305","description": "Host Context Controller - provides McpServer CRDs to mcp-host via REST API","main": "dist/main.js","engines": {diff --git a/host-context-controller/src/existenceReadMetrics.test.ts b/host-context-controller/src/existenceReadMetrics.test.tsindex 409fdeb04..8992d1c78 100644--- a/host-context-controller/src/existenceReadMetrics.test.ts+++ b/host-context-controller/src/existenceReadMetrics.test.ts@@ -186,7 +186,7 @@ function assertReadInventory(sources: Record<string, string>): void {'reconciler.ts': 3,'llmHookReconciler.ts': 3,'sharedFileSystemReconciler.ts': 1,- 'k8s/gfsK8sApi.ts': 3,+ 'k8s/gfsK8sApi.ts': 5,'networkPolicyReconciler.ts': 2,}const wrapped: Record<string, number> = {}@@ -250,7 +250,7 @@ function assertReadInventory(sources: Record<string, string>): void {expect(reason.length, id).toBeGreaterThan(0)expect(excluded[id], `Stale or changed read exclusion: ${id}`).toBe(count)}- expect(Object.values(wrapped).reduce((sum, count) => sum + count, 0)).toBe(21)+ expect(Object.values(wrapped).reduce((sum, count) => sum + count, 0)).toBe(23)expect(Object.values(excluded).reduce((sum, count) => sum + count, 0)).toBe(43)}diff --git a/host-context-controller/src/k8s/gfsK8sApi.readFirst.test.ts b/host-context-controller/src/k8s/gfsK8sApi.readFirst.test.tsnew file mode 100644index 000000000..b8253cda8--- /dev/null+++ b/host-context-controller/src/k8s/gfsK8sApi.readFirst.test.ts@@ -0,0 +1,257 @@+import { beforeEach, describe, expect, it, vi } from 'vitest'+import type * as k8s from '@kubernetes/client-node'+import { createsTotal, existenceReadsTotal } from '../metrics'+import { K8sGfsApi } from './gfsK8sApi'++const namespace = 'mcp-host'+const metadata = {+ name: 'gfs-resource',+ namespace,+ labels: { 'app.kubernetes.io/managed-by': 'host-context-controller' },+}+const cases = [+ {+ kind: 'PersistentVolumeClaim',+ updates: false,+ body: {+ kind: 'PersistentVolumeClaim',+ metadata,+ spec: { accessModes: ['ReadWriteOnce'], resources: { requests: { storage: '1Gi' } } },+ },+ apply: (api: K8sGfsApi, body: k8s.V1PersistentVolumeClaim) => api.applyPvc(body, namespace),+ },+ {+ kind: 'Service',+ updates: false,+ body: {+ kind: 'Service',+ metadata,+ spec: { selector: { app: 'gfs' }, ports: [{ port: 8080 }] },+ },+ apply: (api: K8sGfsApi, body: k8s.V1Service) => api.applyService(body, namespace),+ },+ {+ kind: 'Deployment',+ updates: true,+ body: {+ kind: 'Deployment',+ metadata,+ spec: {+ selector: { matchLabels: { app: 'gfs' } },+ template: {+ metadata: { labels: { app: 'gfs' } },+ spec: { containers: [{ name: 'gfs', image: 'example/gfs:1' }] },+ },+ },+ },+ apply: (api: K8sGfsApi, body: k8s.V1Deployment) => api.applyDeployment(body, namespace),+ },+ {+ kind: 'PodDisruptionBudget',+ updates: true,+ body: {+ kind: 'PodDisruptionBudget',+ metadata,+ spec: { minAvailable: 1, selector: { matchLabels: { app: 'gfs' } } },+ },+ apply: (api: K8sGfsApi, body: k8s.V1PodDisruptionBudget) =>+ api.applyPodDisruptionBudget(body, namespace),+ },+] as const++// The SDK mock boundary records real adapter calls, not a fake GfsK8sApi implementation.+function harness(kind: string) {+ const events: string[] = []+ const read = vi.fn(async (): Promise<unknown> => {+ events.push('GET')+ throw new Error('Arrange the SDK read outcome for this scenario')+ })+ const create = vi.fn(async (): Promise<unknown> => {+ events.push('POST')+ return {}+ })+ const replace = vi.fn(async ({ body }: { body: { metadata?: { resourceVersion?: string } } }) => {+ events.push(`PUT:${body.metadata?.resourceVersion}`)+ return body+ })+ const sdk = {+ [`readNamespaced${kind}`]: read,+ [`createNamespaced${kind}`]: create,+ [`replaceNamespaced${kind}`]: replace,+ }+ const api = new K8sGfsApi(+ sdk as unknown as k8s.CoreV1Api,+ sdk as unknown as k8s.AppsV1Api,+ sdk as unknown as k8s.NetworkingV1Api,+ sdk as unknown as k8s.PolicyV1Api,+ {} as k8s.CustomObjectsApi+ )+ return { api, read, create, replace, events }+}++async function count(metric: typeof createsTotal, kind: string, outcome: string) {+ return (+ (await metric.get()).values.find(+ row => row.labels.kind === kind && row.labels.outcome === outcome+ )?.value ?? 0+ )+}++beforeEach(() => {+ createsTotal.reset()+ existenceReadsTotal.reset()+})++describe.each(cases)('K8sGfsApi $kind read-first', resource => {+ // This union is passed only to its matching case adapter above.+ const apply = (api: K8sGfsApi) => resource.apply(api, resource.body as never)+ const live = (version = '1') => ({+ ...resource.body,+ metadata: { ...metadata, resourceVersion: version },+ })++ it('preserves or converges an existing object without POST or a duplicate GET', async () => {+ const h = harness(resource.kind)+ h.read.mockImplementation(async () => {+ h.events.push('GET')+ return live()+ })+ await apply(h.api)+ expect(h.events).toEqual(resource.updates ? ['GET', 'PUT:1'] : ['GET'])+ expect(h.read).toHaveBeenCalledExactlyOnceWith({ name: metadata.name, namespace })+ expect(h.create).not.toHaveBeenCalled()+ expect(h.replace).toHaveBeenCalledTimes(resource.updates ? 1 : 0)+ if (resource.updates)+ expect(h.replace).toHaveBeenCalledWith({ name: metadata.name, namespace, body: live() })+ expect(await count(createsTotal, resource.kind, 'skipped')).toBe(1)+ expect(await count(existenceReadsTotal, resource.kind, 'found')).toBe(1)+ })++ it('creates an absent object after its single existence GET', async () => {+ const h = harness(resource.kind)+ h.read.mockImplementationOnce(async () => {+ h.events.push('GET')+ throw { code: 404 }+ })+ await apply(h.api)+ expect(h.events).toEqual(['GET', 'POST'])+ expect(h.create).toHaveBeenCalledExactlyOnceWith({ namespace, body: resource.body })+ expect(h.replace).not.toHaveBeenCalled()+ expect(await count(createsTotal, resource.kind, 'created')).toBe(1)+ expect(await count(createsTotal, resource.kind, 'skipped')).toBe(0)+ expect(await count(existenceReadsTotal, resource.kind, 'absent')).toBe(1)+ })++ it.each([{ code: 403 }, { response: { statusCode: 500 } }, new Error('transport failure')])(+ 'propagates initial GET failure without a write: %j',+ async error => {+ const h = harness(resource.kind)+ h.read.mockRejectedValueOnce(error)+ await expect(apply(h.api)).rejects.toBe(error)+ expect(h.read).toHaveBeenCalledTimes(1)+ expect(h.create).not.toHaveBeenCalled()+ expect(h.replace).not.toHaveBeenCalled()+ expect(await count(existenceReadsTotal, resource.kind, 'error')).toBe(1)+ expect(await count(createsTotal, resource.kind, 'skipped')).toBe(0)+ }+ )++ it('rereads after absence races with POST409, without counting a suppressed create', async () => {+ const h = harness(resource.kind)+ h.read+ .mockImplementationOnce(async () => {+ h.events.push('GET')+ throw { code: 404 }+ })+ .mockImplementationOnce(async () => {+ h.events.push('GET')+ return live('2')+ })+ h.create.mockImplementationOnce(async () => {+ h.events.push('POST')+ throw { code: 409 }+ })+ await apply(h.api)+ expect(h.events).toEqual(+ resource.updates ? ['GET', 'POST', 'GET', 'PUT:2'] : ['GET', 'POST', 'GET']+ )+ expect(h.create).toHaveBeenCalledTimes(1)+ expect(h.read).toHaveBeenCalledTimes(2)+ expect(await count(createsTotal, resource.kind, 'conflict')).toBe(1)+ expect(await count(createsTotal, resource.kind, 'skipped')).toBe(0)+ })++ it('propagates the fresh GET403 after POST409', async () => {+ const h = harness(resource.kind)+ const denied = { code: 403 }+ h.read.mockRejectedValueOnce({ code: 404 }).mockRejectedValueOnce(denied)+ h.create.mockRejectedValueOnce({ code: 409 })+ await expect(apply(h.api)).rejects.toBe(denied)+ expect(h.read).toHaveBeenCalledTimes(2)+ expect(h.create).toHaveBeenCalledTimes(1)+ expect(h.replace).not.toHaveBeenCalled()+ expect(await count(createsTotal, resource.kind, 'skipped')).toBe(0)+ })++ it('preserves benign disappearance after a create conflict', async () => {+ const h = harness(resource.kind)+ h.read.mockRejectedValueOnce({ code: 404 }).mockRejectedValueOnce({ code: 404 })+ h.create.mockRejectedValueOnce({ code: 409 })+ await expect(apply(h.api)).resolves.toBeUndefined()+ expect(h.read).toHaveBeenCalledTimes(2)+ expect(h.create).toHaveBeenCalledTimes(1)+ expect(h.replace).not.toHaveBeenCalled()+ expect(await count(createsTotal, resource.kind, 'skipped')).toBe(0)+ })++ it('preserves a non-conflict POST failure without convergence', async () => {+ const h = harness(resource.kind)+ h.read.mockRejectedValueOnce({ code: 404 })+ const denied = { code: 403 }+ h.create.mockRejectedValueOnce(denied)+ await expect(apply(h.api)).rejects.toBe(denied)+ expect(h.read).toHaveBeenCalledTimes(1)+ expect(h.create).toHaveBeenCalledTimes(1)+ expect(h.replace).not.toHaveBeenCalled()+ expect(await count(createsTotal, resource.kind, 'error')).toBe(1)+ })+})++describe.each(cases.filter(resource => resource.updates))(+ 'K8sGfsApi $kind PUT conflicts',+ resource => {+ const apply = (api: K8sGfsApi) => resource.apply(api, resource.body as never)+ it('uses the cached first read once, then fresh resourceVersion for the retry', async () => {+ const h = harness(resource.kind)+ let version = 0+ h.read.mockImplementation(async () => {+ h.events.push('GET')+ return { ...resource.body, metadata: { ...metadata, resourceVersion: String(++version) } }+ })+ h.replace.mockImplementationOnce(async ({ body }) => {+ h.events.push(`PUT:${body.metadata?.resourceVersion}`)+ throw { code: 409 }+ })+ await apply(h.api)+ expect(h.events).toEqual(['GET', 'PUT:1', 'GET', 'PUT:2'])+ expect(h.create).not.toHaveBeenCalled()+ expect(await count(existenceReadsTotal, resource.kind, 'found')).toBe(2)+ expect(await count(createsTotal, resource.kind, 'skipped')).toBe(1)+ })++ it('retains the three-attempt PUT budget and propagates exhaustion', async () => {+ const h = harness(resource.kind)+ h.read.mockResolvedValue({+ ...resource.body,+ metadata: { ...metadata, resourceVersion: '1' },+ })+ const conflict = { code: 409 }+ h.replace.mockRejectedValue(conflict)+ await expect(apply(h.api)).rejects.toBe(conflict)+ expect(h.read).toHaveBeenCalledTimes(3)+ expect(h.replace).toHaveBeenCalledTimes(3)+ expect(h.create).not.toHaveBeenCalled()+ expect(await count(createsTotal, resource.kind, 'skipped')).toBe(0)+ })+ }+)diff --git a/host-context-controller/src/k8s/gfsK8sApi.ts b/host-context-controller/src/k8s/gfsK8sApi.tsindex 8b7d6b926..6eed0f49e 100644--- a/host-context-controller/src/k8s/gfsK8sApi.ts+++ b/host-context-controller/src/k8s/gfsK8sApi.ts@@ -1,8 +1,10 @@import * as k8s from '@kubernetes/client-node'import type { GfsK8sApi } from '../gfsReconciler'+import { createsTotal } from '../metrics'import type { GlobalFileSystemStatus } from '../types'import {applyNetworkPolicy,+ ensureResource,getErrorCode,observeCreate,observeExistenceRead,@@ -42,14 +44,21 @@ export class K8sGfsApi implements GfsK8sApi {}async applyPvc(pvc: k8s.V1PersistentVolumeClaim, namespace: string): Promise<void> {- try {- await observeCreate('PersistentVolumeClaim', () =>- this.coreApi.createNamespacedPersistentVolumeClaim({ namespace, body: pvc })- )- } catch (err) {- // A PVC spec is immutable once bound; an existing one is left as-is.- if (getErrorCode(err) !== 409) throw err- }+ const name = pvc.metadata?.name ?? ''+ await ensureResource<k8s.V1PersistentVolumeClaim>({+ read: () =>+ observeExistenceRead('PersistentVolumeClaim', () =>+ this.coreApi.readNamespacedPersistentVolumeClaim({ name, namespace })+ ),+ create: () =>+ observeCreate('PersistentVolumeClaim', () =>+ this.coreApi.createNamespacedPersistentVolumeClaim({ namespace, body: pvc })+ ),+ onSkipped: () => createsTotal.inc({ kind: 'PersistentVolumeClaim', outcome: 'skipped' }),+ // Bound PVCs stay unchanged. A create conflict must consume a fresh read;+ // disappearance remains benign, while permission/transport failures propagate.+ converge: read => this.ignoreNotFound(read),+ })}async deploymentNeedsUpdate(dep: k8s.V1Deployment, namespace: string): Promise<boolean> {@@ -97,59 +106,67 @@ export class K8sGfsApi implements GfsK8sApi {async applyDeployment(dep: k8s.V1Deployment, namespace: string): Promise<void> {const name = dep.metadata?.name ?? ''- try {- await observeCreate('Deployment', () =>- this.appsApi.createNamespacedDeployment({ namespace, body: dep })- )- return- } catch (err) {- if (getErrorCode(err) !== 409) throw err- }- await replaceWithConflictRetry<k8s.V1Deployment>({- description: `deployment "${name}" in ${namespace}`,- logPrefix: LOG,- body: dep,+ await ensureResource<k8s.V1Deployment>({read: () =>observeExistenceRead('Deployment', () =>this.appsApi.readNamespacedDeployment({ name, namespace })),- replace: body => this.appsApi.replaceNamespacedDeployment({ name, namespace, body }),+ create: () =>+ observeCreate('Deployment', () =>+ this.appsApi.createNamespacedDeployment({ namespace, body: dep })+ ),+ onSkipped: () => createsTotal.inc({ kind: 'Deployment', outcome: 'skipped' }),+ converge: read =>+ replaceWithConflictRetry<k8s.V1Deployment>({+ description: `deployment "${name}" in ${namespace}`,+ logPrefix: LOG,+ body: dep,+ read,+ replace: body => this.appsApi.replaceNamespacedDeployment({ name, namespace, body }),+ }),})}async applyPodDisruptionBudget(pdb: k8s.V1PodDisruptionBudget, namespace: string): Promise<void> {const name = pdb.metadata?.name ?? ''- try {- await observeCreate('PodDisruptionBudget', () =>- this.policyApi.createNamespacedPodDisruptionBudget({ namespace, body: pdb })- )- return- } catch (err) {- if (getErrorCode(err) !== 409) throw err- }- await replaceWithConflictRetry<k8s.V1PodDisruptionBudget>({- description: `pod disruption budget "${name}" in ${namespace}`,- logPrefix: LOG,- body: pdb,+ await ensureResource<k8s.V1PodDisruptionBudget>({read: () =>observeExistenceRead('PodDisruptionBudget', () =>this.policyApi.readNamespacedPodDisruptionBudget({ name, namespace })),- replace: body =>- this.policyApi.replaceNamespacedPodDisruptionBudget({ name, namespace, body }),+ create: () =>+ observeCreate('PodDisruptionBudget', () =>+ this.policyApi.createNamespacedPodDisruptionBudget({ namespace, body: pdb })+ ),+ onSkipped: () => createsTotal.inc({ kind: 'PodDisruptionBudget', outcome: 'skipped' }),+ converge: read =>+ replaceWithConflictRetry<k8s.V1PodDisruptionBudget>({+ description: `pod disruption budget "${name}" in ${namespace}`,+ logPrefix: LOG,+ body: pdb,+ read,+ replace: body =>+ this.policyApi.replaceNamespacedPodDisruptionBudget({ name, namespace, body }),+ }),})}async applyService(svc: k8s.V1Service, namespace: string): Promise<void> {- try {- await observeCreate('Service', () =>- this.coreApi.createNamespacedService({ namespace, body: svc })- )- } catch (err) {- // clusterIP is immutable; the gfsc Service spec is stable, so an existing- // Service is left in place rather than risking an invalid replace.- if (getErrorCode(err) !== 409) throw err- }+ const name = svc.metadata?.name ?? ''+ await ensureResource<k8s.V1Service>({+ read: () =>+ observeExistenceRead('Service', () =>+ this.coreApi.readNamespacedService({ name, namespace })+ ),+ create: () =>+ observeCreate('Service', () =>+ this.coreApi.createNamespacedService({ namespace, body: svc })+ ),+ onSkipped: () => createsTotal.inc({ kind: 'Service', outcome: 'skipped' }),+ // Preserve the stable Service, including its immutable clusterIP. Read+ // through a create conflict without introducing an update or another POST.+ converge: read => this.ignoreNotFound(read),+ })}async applyNetworkPolicy(np: k8s.V1NetworkPolicy, namespace: string): Promise<void> {