import type { K8sClient } from '$lib/server/k8s'; import { K8sResource } from '$lib/server/resources/k8s'; import type { ExternalToolCR, ManagedToolCR } from '$lib/types/custom-resources'; import type { ResourceEvent } from '$lib/types/events'; import type { CoreResources } from '$lib/types/workspace'; import type { ToolResources } from './types'; class K8sExternalToolResources extends K8sResource { constructor(k8sClient: K8sClient, namespace: string) { super( k8sClient, namespace, { group: 'locostack.com', version: 'v1alpha1', namespace, resource: 'externaltools', }, [], ); } } class K8sManagedToolResources extends K8sResource { constructor(k8sClient: K8sClient, namespace: string) { super( k8sClient, namespace, { group: 'locostack.com', version: 'v1alpha1', namespace, resource: 'managedtools', }, [ { group: 'apps', version: 'v1', namespace, resource: 'deployments', labelSelector: 'locostack.com/component=Tool', }, { group: 'core', version: 'v1', namespace, resource: 'pods', labelSelector: 'locostack.com/component=Tool', }, { group: 'core', version: 'v1', namespace, resource: 'services', labelSelector: 'locostack.com/component=Tool', }, ], ); } } class K8sToolResources implements ToolResources { #externalToolResources: K8sExternalToolResources; #managedToolResources: K8sManagedToolResources; constructor(k8sClient: K8sClient, namespace: string) { this.#externalToolResources = new K8sExternalToolResources(k8sClient, namespace); this.#managedToolResources = new K8sManagedToolResources(k8sClient, namespace); } subscribe(handler: (event: ResourceEvent) => void): () => void { const unsubs = [ this.#externalToolResources.subscribe(handler), this.#managedToolResources.subscribe(handler), ]; return () => unsubs.forEach((u) => u()); } async getExternalTools(): Promise { return this.#externalToolResources.getResources(); } async getExternalToolStatus(toolId: string): Promise { return this.#externalToolResources.getResourceStatus(toolId); } async getManagedTools(): Promise { return this.#managedToolResources.getResources(); } async getManagedToolResources(toolId: string): Promise { return this.#managedToolResources.getCoreResources(`tool-${toolId}`); } async getManagedToolStatus(toolId: string): Promise { return this.#managedToolResources.getResourceStatus(toolId); } async applyExternalTool(cr: ExternalToolCR): Promise { await this.#externalToolResources.apply(cr); } async applyManagedTool(cr: ManagedToolCR): Promise { await this.#managedToolResources.apply(cr); } async deleteExternalTool(id: string): Promise { await this.#externalToolResources.remove(id); } async deleteManagedTool(id: string): Promise { await this.#managedToolResources.remove(id); } } export { K8sToolResources };