k8s.ts 3.1 KB

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