|
|
@@ -0,0 +1,117 @@
|
|
|
+import type { V1ObjectMeta } from '@kubernetes/client-node';
|
|
|
+
|
|
|
+import type { Stack } from '$lib/types/entities';
|
|
|
+import * as V1alpha1Stack from '$lib/types/locostack.com/v1alpha1/Stack';
|
|
|
+
|
|
|
+import { fromTemplate, toTemplate } from './types';
|
|
|
+
|
|
|
+type StackCR = Omit<V1alpha1Stack.components['schemas']['Stack'], 'metadata'> & {
|
|
|
+ metadata: V1ObjectMeta;
|
|
|
+};
|
|
|
+
|
|
|
+const stackAnnotation = (key: string, value: string | undefined): { [key: string]: string } =>
|
|
|
+ value
|
|
|
+ ? {
|
|
|
+ [`stacks.locostack.com/${key}`]: value,
|
|
|
+ }
|
|
|
+ : {};
|
|
|
+
|
|
|
+const stackAnnotations = (stack: Stack): { [key: string]: string } => ({
|
|
|
+ ...stackAnnotation('name', stack.name),
|
|
|
+});
|
|
|
+
|
|
|
+const readStackAnnotation = (metadata: V1ObjectMeta, key: string): string | undefined =>
|
|
|
+ metadata.annotations?.[`stacks.locostack.com/${key}`];
|
|
|
+
|
|
|
+const toStackCR = (stack: Stack): StackCR => {
|
|
|
+ const gatewayEnabled = stack.gateway?.enabled ?? true;
|
|
|
+ const vectorStoreEnabled = stack.vectorStore?.enabled ?? true;
|
|
|
+ const graphStoreEnabled = stack.graphStore?.enabled ?? false;
|
|
|
+ const databaseEnabled = stack.database?.enabled ?? true;
|
|
|
+ const observabilityEnabled = stack.observability?.enabled ?? false;
|
|
|
+
|
|
|
+ return {
|
|
|
+ apiVersion: 'locostack.com/v1alpha1',
|
|
|
+ kind: 'Stack',
|
|
|
+ metadata: {
|
|
|
+ name: stack.id,
|
|
|
+ annotations: stackAnnotations(stack),
|
|
|
+ },
|
|
|
+ spec: {
|
|
|
+ ...(stack.sharedVolumes ? { sharedVolumes: stack.sharedVolumes } : {}),
|
|
|
+ gateway: {
|
|
|
+ enabled: gatewayEnabled,
|
|
|
+ ...(stack.gateway?.runtime ? { template: toTemplate(stack.gateway.runtime) } : {}),
|
|
|
+ },
|
|
|
+ vectorStore: {
|
|
|
+ enabled: vectorStoreEnabled,
|
|
|
+ ...(stack.vectorStore?.runtime ? { template: toTemplate(stack.vectorStore.runtime) } : {}),
|
|
|
+ },
|
|
|
+ ...(graphStoreEnabled && {
|
|
|
+ graphStore: {
|
|
|
+ enabled: true,
|
|
|
+ ...(stack.graphStore?.runtime ? { template: toTemplate(stack.graphStore.runtime) } : {}),
|
|
|
+ },
|
|
|
+ }),
|
|
|
+ database: {
|
|
|
+ enabled: databaseEnabled,
|
|
|
+ ...(stack.database?.runtime ? { template: toTemplate(stack.database.runtime) } : {}),
|
|
|
+ },
|
|
|
+ ...(observabilityEnabled && {
|
|
|
+ observability: {
|
|
|
+ enabled: true,
|
|
|
+ ...(stack.observability?.runtime
|
|
|
+ ? { template: toTemplate(stack.observability.runtime) }
|
|
|
+ : {}),
|
|
|
+ },
|
|
|
+ }),
|
|
|
+ },
|
|
|
+ };
|
|
|
+};
|
|
|
+
|
|
|
+const fromStackCR = (cr: StackCR): Stack => {
|
|
|
+ const spec = cr.spec;
|
|
|
+ const metadata = cr.metadata;
|
|
|
+ const id = metadata.name;
|
|
|
+ if (id === undefined) {
|
|
|
+ throw new Error('metadata.name is required');
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ id,
|
|
|
+ name: readStackAnnotation(metadata, 'name') ?? id,
|
|
|
+ sharedVolumes: spec.sharedVolumes,
|
|
|
+ gateway: spec.gateway
|
|
|
+ ? {
|
|
|
+ enabled: spec.gateway.enabled ?? false,
|
|
|
+ runtime: spec.gateway.template && fromTemplate(spec.gateway.template),
|
|
|
+ }
|
|
|
+ : undefined,
|
|
|
+ vectorStore: spec.vectorStore
|
|
|
+ ? {
|
|
|
+ enabled: spec.vectorStore.enabled ?? false,
|
|
|
+ runtime: spec.vectorStore.template && fromTemplate(spec.vectorStore.template),
|
|
|
+ }
|
|
|
+ : undefined,
|
|
|
+ graphStore: spec.graphStore
|
|
|
+ ? {
|
|
|
+ enabled: spec.graphStore.enabled ?? false,
|
|
|
+ runtime: spec.graphStore.template && fromTemplate(spec.graphStore.template),
|
|
|
+ }
|
|
|
+ : undefined,
|
|
|
+ database: spec.database
|
|
|
+ ? {
|
|
|
+ enabled: spec.database.enabled ?? false,
|
|
|
+ runtime: spec.database.template && fromTemplate(spec.database.template),
|
|
|
+ }
|
|
|
+ : undefined,
|
|
|
+ observability: spec.observability
|
|
|
+ ? {
|
|
|
+ enabled: spec.observability.enabled ?? false,
|
|
|
+ runtime: spec.observability.template && fromTemplate(spec.observability.template),
|
|
|
+ }
|
|
|
+ : undefined,
|
|
|
+ };
|
|
|
+};
|
|
|
+
|
|
|
+export type { StackCR };
|
|
|
+export { fromStackCR, toStackCR };
|