k8s.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import type { K8sClient } from '$lib/server/k8s';
  2. import { K8sResource } from '$lib/server/resources/k8s';
  3. import type { ExternalModelCR, ManagedModelCR } from '$lib/types/custom-resources';
  4. import type { ResourceEvent } from '$lib/types/events';
  5. import type { CoreResources } from '$lib/types/workspace';
  6. import type { ModelResources } from './types';
  7. class K8sExternalModelResources extends K8sResource<ExternalModelCR> {
  8. constructor(k8sClient: K8sClient, namespace: string) {
  9. super(
  10. k8sClient,
  11. namespace,
  12. {
  13. group: 'locostack.com',
  14. version: 'v1alpha1',
  15. namespace,
  16. resource: 'externalmodels',
  17. },
  18. [],
  19. );
  20. }
  21. }
  22. class K8sManagedModelResources extends K8sResource<ManagedModelCR> {
  23. constructor(k8sClient: K8sClient, namespace: string) {
  24. super(
  25. k8sClient,
  26. namespace,
  27. {
  28. group: 'locostack.com',
  29. version: 'v1alpha1',
  30. namespace,
  31. resource: 'managedmodels',
  32. },
  33. [
  34. {
  35. group: 'apps',
  36. version: 'v1',
  37. namespace,
  38. resource: 'deployments',
  39. labelSelector: 'locostack.com/component=Model',
  40. },
  41. {
  42. group: 'core',
  43. version: 'v1',
  44. namespace,
  45. resource: 'pods',
  46. labelSelector: 'locostack.com/component=Model',
  47. },
  48. {
  49. group: 'core',
  50. version: 'v1',
  51. namespace,
  52. resource: 'services',
  53. labelSelector: 'locostack.com/component=Model',
  54. },
  55. ],
  56. );
  57. }
  58. }
  59. class K8sModelResources implements ModelResources {
  60. #externalModelResources: K8sExternalModelResources;
  61. #managedModelResources: K8sManagedModelResources;
  62. constructor(k8sClient: K8sClient, namespace: string) {
  63. this.#externalModelResources = new K8sExternalModelResources(k8sClient, namespace);
  64. this.#managedModelResources = new K8sManagedModelResources(k8sClient, namespace);
  65. }
  66. subscribe(handler: (event: ResourceEvent) => void): () => void {
  67. const unsubs = [
  68. this.#externalModelResources.subscribe(handler),
  69. this.#managedModelResources.subscribe(handler),
  70. ];
  71. return () => unsubs.forEach((u) => u());
  72. }
  73. async getExternalModels(): Promise<ExternalModelCR[]> {
  74. return this.#externalModelResources.getResources();
  75. }
  76. async getExternalModelStatus(modelId: string): Promise<ExternalModelCR['status'] | undefined> {
  77. return this.#externalModelResources.getResourceStatus<ExternalModelCR['status']>(modelId);
  78. }
  79. async getManagedModels(): Promise<ManagedModelCR[]> {
  80. return this.#managedModelResources.getResources();
  81. }
  82. async getManagedModelResources(modelId: string): Promise<CoreResources> {
  83. return this.#managedModelResources.getCoreResources(`model-${modelId}`);
  84. }
  85. async getManagedModelStatus(modelId: string): Promise<ManagedModelCR['status'] | undefined> {
  86. return this.#managedModelResources.getResourceStatus<ManagedModelCR['status']>(modelId);
  87. }
  88. async applyExternalModel(cr: ExternalModelCR): Promise<void> {
  89. await this.#externalModelResources.apply(cr);
  90. }
  91. async applyManagedModel(cr: ManagedModelCR): Promise<void> {
  92. await this.#managedModelResources.apply(cr);
  93. }
  94. async deleteExternalModel(id: string): Promise<void> {
  95. await this.#externalModelResources.remove(id);
  96. }
  97. async deleteManagedModel(id: string): Promise<void> {
  98. await this.#managedModelResources.remove(id);
  99. }
  100. }
  101. export { K8sModelResources };