| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- 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<ExternalToolCR> {
- constructor(k8sClient: K8sClient, namespace: string) {
- super(
- k8sClient,
- namespace,
- {
- group: 'locostack.com',
- version: 'v1alpha1',
- namespace,
- resource: 'externaltools',
- },
- [],
- );
- }
- }
- class K8sManagedToolResources extends K8sResource<ManagedToolCR> {
- 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<ExternalToolCR[]> {
- return this.#externalToolResources.getResources();
- }
- async getExternalToolStatus(toolId: string): Promise<ExternalToolCR['status'] | undefined> {
- return this.#externalToolResources.getResourceStatus<ExternalToolCR['status']>(toolId);
- }
- async getManagedTools(): Promise<ManagedToolCR[]> {
- return this.#managedToolResources.getResources();
- }
- async getManagedToolResources(toolId: string): Promise<CoreResources> {
- return this.#managedToolResources.getCoreResources(`tool-${toolId}`);
- }
- async getManagedToolStatus(toolId: string): Promise<ManagedToolCR['status'] | undefined> {
- return this.#managedToolResources.getResourceStatus<ManagedToolCR['status']>(toolId);
- }
- async applyExternalTool(cr: ExternalToolCR): Promise<void> {
- await this.#externalToolResources.apply(cr);
- }
- async applyManagedTool(cr: ManagedToolCR): Promise<void> {
- await this.#managedToolResources.apply(cr);
- }
- async deleteExternalTool(id: string): Promise<void> {
- await this.#externalToolResources.remove(id);
- }
- async deleteManagedTool(id: string): Promise<void> {
- await this.#managedToolResources.remove(id);
- }
- }
- export { K8sToolResources };
|