| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import type { K8sClient } from '$lib/server/k8s';
- import { K8sResource } from '$lib/server/resources/k8s';
- import type { ExternalModelCR, ManagedModelCR } from '$lib/types/custom-resources';
- import type { ResourceEvent } from '$lib/types/events';
- import type { CoreResources } from '$lib/types/workspace';
- import type { ModelResources } from './types';
- class K8sExternalModelResources extends K8sResource<ExternalModelCR> {
- constructor(k8sClient: K8sClient, namespace: string) {
- super(
- k8sClient,
- namespace,
- {
- group: 'locostack.com',
- version: 'v1alpha1',
- namespace,
- resource: 'externalmodels',
- },
- [],
- );
- }
- }
- class K8sManagedModelResources extends K8sResource<ManagedModelCR> {
- constructor(k8sClient: K8sClient, namespace: string) {
- super(
- k8sClient,
- namespace,
- {
- group: 'locostack.com',
- version: 'v1alpha1',
- namespace,
- resource: 'managedmodels',
- },
- [
- {
- group: 'apps',
- version: 'v1',
- namespace,
- resource: 'deployments',
- labelSelector: 'locostack.com/component=Model',
- },
- {
- group: 'core',
- version: 'v1',
- namespace,
- resource: 'pods',
- labelSelector: 'locostack.com/component=Model',
- },
- {
- group: 'core',
- version: 'v1',
- namespace,
- resource: 'services',
- labelSelector: 'locostack.com/component=Model',
- },
- ],
- );
- }
- }
- class K8sModelResources implements ModelResources {
- #externalModelResources: K8sExternalModelResources;
- #managedModelResources: K8sManagedModelResources;
- constructor(k8sClient: K8sClient, namespace: string) {
- this.#externalModelResources = new K8sExternalModelResources(k8sClient, namespace);
- this.#managedModelResources = new K8sManagedModelResources(k8sClient, namespace);
- }
- subscribe(handler: (event: ResourceEvent) => void): () => void {
- const unsubs = [
- this.#externalModelResources.subscribe(handler),
- this.#managedModelResources.subscribe(handler),
- ];
- return () => unsubs.forEach((u) => u());
- }
- async getExternalModels(): Promise<ExternalModelCR[]> {
- return this.#externalModelResources.getResources();
- }
- async getExternalModelStatus(modelId: string): Promise<ExternalModelCR['status'] | undefined> {
- return this.#externalModelResources.getResourceStatus<ExternalModelCR['status']>(modelId);
- }
- async getManagedModels(): Promise<ManagedModelCR[]> {
- return this.#managedModelResources.getResources();
- }
- async getManagedModelResources(modelId: string): Promise<CoreResources> {
- return this.#managedModelResources.getCoreResources(`model-${modelId}`);
- }
- async getManagedModelStatus(modelId: string): Promise<ManagedModelCR['status'] | undefined> {
- return this.#managedModelResources.getResourceStatus<ManagedModelCR['status']>(modelId);
- }
- async applyExternalModel(cr: ExternalModelCR): Promise<void> {
- await this.#externalModelResources.apply(cr);
- }
- async applyManagedModel(cr: ManagedModelCR): Promise<void> {
- await this.#managedModelResources.apply(cr);
- }
- async deleteExternalModel(id: string): Promise<void> {
- await this.#externalModelResources.remove(id);
- }
- async deleteManagedModel(id: string): Promise<void> {
- await this.#managedModelResources.remove(id);
- }
- }
- export { K8sModelResources };
|