k8s.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import type { K8sClient } from '$lib/server/k8s';
  2. import { K8sResource } from '$lib/server/resources/k8s';
  3. import type { StackCR } from '$lib/types/custom-resources';
  4. import type { ResourceEvent } from '$lib/types/events';
  5. import type { CoreResources } from '$lib/types/workspace';
  6. import type { StackResources } from './types';
  7. class K8sStackResourceImpl extends K8sResource<StackCR> {
  8. constructor(k8sClient: K8sClient, namespace: string) {
  9. super(
  10. k8sClient,
  11. namespace,
  12. {
  13. group: 'locostack.com',
  14. version: 'v1alpha1',
  15. namespace,
  16. resource: 'stacks',
  17. },
  18. [],
  19. );
  20. }
  21. }
  22. class K8sStackResources implements StackResources {
  23. #stackResources: K8sStackResourceImpl;
  24. constructor(k8sClient: K8sClient, namespace: string) {
  25. this.#stackResources = new K8sStackResourceImpl(k8sClient, namespace);
  26. }
  27. subscribe(handler: (event: ResourceEvent) => void): () => void {
  28. return this.#stackResources.subscribe(handler);
  29. }
  30. async getStacks(): Promise<StackCR[]> {
  31. return this.#stackResources.getResources();
  32. }
  33. async getStackResources(stackId: string): Promise<CoreResources> {
  34. return this.#stackResources.getCoreResources(stackId);
  35. }
  36. async getStackStatus(stackId: string): Promise<StackCR['status'] | undefined> {
  37. return this.#stackResources.getResourceStatus<StackCR['status']>(stackId);
  38. }
  39. async applyStack(cr: StackCR): Promise<void> {
  40. await this.#stackResources.apply(cr);
  41. }
  42. async deleteStack(id: string): Promise<void> {
  43. await this.#stackResources.remove(id);
  44. }
  45. }
  46. export { K8sStackResources };