Skip to content

Commit

Permalink
chore: port env vars (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
avatxus authored Apr 25, 2024
1 parent 854613d commit 6f06187
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 6,451 deletions.
3,942 changes: 0 additions & 3,942 deletions operator/package-lock.json

This file was deleted.

5 changes: 1 addition & 4 deletions operator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@
},
"dependencies": {
"@demeter-features/cardano-blockfrost": "^1.1.2",
"@demeter-features/cardano-dbsync": "^2.0.0",
"@demeter-features/cardano-kuber": "^1.1.7",
"@demeter-features/cardano-kupo": "^1.1.7",
"@demeter-features/cardano-marlowe": "^1.2.1",
"@demeter-features/cardano-marlowe-port": "^1.0.0",
"@demeter-features/cardano-node-port": "^1.0.0",
"@demeter-features/cardano-nodes": "^2.0.0",
"@demeter-features/cardano-ogmios": "^1.1.6",
"@demeter-features/cardano-submit-api": "^1.1.4",
"@demeter-run/workloads-types": "*",
"@demeter-sdk/framework": "^2.2.2",
"@demeter-sdk/framework": "^2.2.4",
"@dot-i/k8s-operator": "^1.3.5",
"@kubernetes/client-node": "^0.17.1",
"axios": "^1.3.4",
Expand Down
15 changes: 7 additions & 8 deletions operator/src/backend-with-storage/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { V1StatefulSet, V1PersistentVolumeClaim, PatchUtils, V1Container, V1EnvV
import { getClients, readProjectUnsecure, Network, namespaceToSlug, DependencyResource, ServicePlugin } from '@demeter-sdk/framework';
import { API_VERSION, API_GROUP, PLURAL, SINGULAR, KIND } from './constants';
import { CustomResource, CustomResourceResponse, BackendWithStorage, StorageClass } from '@demeter-run/workloads-types';
import { buildEnvVars, cardanoNodeDep, cardanoNodePort, getDependenciesForNetwork, isCardanoNodeEnabled } from '../shared/dependencies';
import { buildEnvVars, cardanoNodeDep, cleanDependencies, getDependenciesForNetwork } from '../shared/dependencies';
import {
getComputeDCUPerMin,
getNetworkFromAnnotations,
Expand All @@ -16,7 +16,7 @@ import {
} from '../shared';
import { checkConfigMapExistsOrCreate, configmap } from '../shared/configmap';
import { buildSocatContainer, buildSocatContainerForPort } from '../shared/cardano-node-helper';
import { buildPortEnvVars, getPortsForNetwork } from '../shared/ports';
import { buildPortEnvVars, getPortsForNetwork, portExists } from '../shared/ports';
import { ServiceInstanceWithStatusAndKind } from '../services';

const tolerations = [
Expand All @@ -40,7 +40,6 @@ const tolerations = [
},
];


export async function handleResource(
ns: string,
name: string,
Expand All @@ -59,12 +58,12 @@ export async function handleResource(
const deps = await getDependenciesForNetwork(project, network);
const ports = await getPortsForNetwork(project, network);
const portEnvVars = await buildPortEnvVars(ports);
const depsEnvVars = await buildEnvVars(deps, network);
const depsEnvVars = await buildEnvVars(cleanDependencies(deps, ports), network);
const envVars = [...depsEnvVars, ...portEnvVars];
const cardanoNode = cardanoNodeDep(deps);
const cardanoNodePortInstance = cardanoNodePort(ports);
const volumesList = workloadVolumes(name, !!cardanoNode);
const containerList = containers(spec, envVars, cardanoNode, cardanoNodePortInstance);
const cardanoNodePort = portExists(ports, 'CardanoNodePort');
const volumesList = workloadVolumes(name, !!cardanoNode || !!cardanoNodePort);
const containerList = containers(spec, envVars, cardanoNode, cardanoNodePort);
try {
await apps.readNamespacedStatefulSet(name, ns);
//@TODO sync
Expand Down Expand Up @@ -268,7 +267,7 @@ function sts(
'demeter.run/kind': owner.kind!,
},
annotations: {
...spec.annotations
...spec.annotations,
},
ownerReferences: [
{
Expand Down
12 changes: 6 additions & 6 deletions operator/src/backend/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { PatchUtils, V1Container, V1EnvVar, V1Volume, V1VolumeMount, V1Deploymen
import { getClients, readProjectUnsecure, Network, namespaceToSlug, DependencyResource, ServicePlugin } from '@demeter-sdk/framework';
import { API_VERSION, API_GROUP, PLURAL } from './constants';
import { CustomResource, CustomResourceResponse, Backend, Pod, WorkloadStatus } from '@demeter-run/workloads-types';
import { buildEnvVars, cardanoNodeDep, cardanoNodePort, getDependenciesForNetwork } from '../shared/dependencies';
import { buildEnvVars, cardanoNodeDep, cleanDependencies, getDependenciesForNetwork } from '../shared/dependencies';
import { getComputeDCUPerMin, getDeploymentStatus, getNetworkFromAnnotations, getResourcesFromComputeClass, workloadVolumes } from '../shared';
import { checkConfigMapExistsOrCreate, configmap } from '../shared/configmap';
import { buildSocatContainer, buildSocatContainerForPort } from '../shared/cardano-node-helper';
import { buildPortEnvVars, getPortsForNetwork } from '../shared/ports';
import { buildPortEnvVars, getPortsForNetwork, portExists } from '../shared/ports';
import { ServiceInstanceWithStatusAndKind } from '../services';

const tolerations = [
Expand Down Expand Up @@ -48,12 +48,12 @@ export async function handleResource(
const deps = await getDependenciesForNetwork(project, network);
const ports = await getPortsForNetwork(project, network);
const portEnvVars = await buildPortEnvVars(ports);
const depsEnvVars = await buildEnvVars(deps, network);
const depsEnvVars = await buildEnvVars(cleanDependencies(deps, ports), network);
const envVars = [...depsEnvVars, ...portEnvVars];
const cardanoNode = cardanoNodeDep(deps);
const cardanoNodePortInstance = cardanoNodePort(ports);
const volumesList = workloadVolumes(name, !!cardanoNode);
const containerList = containers(spec, envVars, cardanoNode, cardanoNodePortInstance);
const cardanoNodePort = portExists(ports, 'CardanoNodePort');
const volumesList = workloadVolumes(name, !!cardanoNode || !!cardanoNodePort);
const containerList = containers(spec, envVars, cardanoNode, cardanoNodePort);
try {
await apps.readNamespacedDeployment(name, ns);
await checkConfigMapExistsOrCreate(core, ns, name, spec, owner);
Expand Down
12 changes: 6 additions & 6 deletions operator/src/frontend/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { PatchUtils, V1Container, V1EnvVar, V1Volume, V1VolumeMount, V1Deploymen
import { getClients, readProjectUnsecure, Network, namespaceToSlug, DependencyResource, ServicePlugin } from '@demeter-sdk/framework';
import { API_VERSION, API_GROUP, PLURAL } from './constants';
import { CustomResource, CustomResourceResponse, Frontend, WorkloadStatus } from '@demeter-run/workloads-types';
import { buildEnvVars, cardanoNodeDep, cardanoNodePort, getDependenciesForNetwork } from '../shared/dependencies';
import { buildEnvVars, cardanoNodeDep, cleanDependencies, getDependenciesForNetwork } from '../shared/dependencies';
import { getComputeDCUPerMin, getDeploymentStatus, getNetworkFromAnnotations, getResourcesFromComputeClass, workloadVolumes } from '../shared';
import { checkConfigMapExistsOrCreate, configmap } from '../shared/configmap';
import { buildSocatContainer, buildSocatContainerForPort } from '../shared/cardano-node-helper';
import { buildPortEnvVars, getPortsForNetwork } from '../shared/ports';
import { buildPortEnvVars, getPortsForNetwork, portExists } from '../shared/ports';
import { ServiceInstanceWithStatusAndKind } from '../services';

const tolerations = [
Expand Down Expand Up @@ -48,12 +48,12 @@ export async function handleResource(
const deps = await getDependenciesForNetwork(project, network);
const ports = await getPortsForNetwork(project, network);
const portEnvVars = await buildPortEnvVars(ports);
const depsEnvVars = await buildEnvVars(deps, network);
const depsEnvVars = await buildEnvVars(cleanDependencies(deps, ports), network);
const envVars = [...depsEnvVars, ...portEnvVars];
const cardanoNode = cardanoNodeDep(deps);
const cardanoNodePortInstance = cardanoNodePort(ports);
const volumesList = workloadVolumes(name, !!cardanoNode);
const containerList = containers(spec, envVars, cardanoNode, cardanoNodePortInstance);
const cardanoNodePort = portExists(ports, 'CardanoNodePort');
const volumesList = workloadVolumes(name, !!cardanoNode || !!cardanoNodePort);
const containerList = containers(spec, envVars, cardanoNode, cardanoNodePort);
try {
await apps.readNamespacedDeployment(name, ns);
await checkConfigMapExistsOrCreate(core, ns, name, spec, owner);
Expand Down
11 changes: 0 additions & 11 deletions operator/src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ import { ServiceMetadata, ServicePlugin, ServiceInstance, getAllRegisteredServic
import { getServicePlugin } from '@demeter-sdk/framework';
import { registerService } from '@demeter-sdk/framework';
import { SERVICE_PLUGIN as submitApiService } from '@demeter-features/cardano-submit-api';
import { SERVICE_PLUGIN as nodesService } from '@demeter-features/cardano-nodes';
import { SERVICE_PLUGIN as nodePortService } from '@demeter-features/cardano-node-port';
import { SERVICE_PLUGIN as kuberService } from '@demeter-features/cardano-kuber';
import { SERVICE_PLUGIN as blockfrostService } from '@demeter-features/cardano-blockfrost';
import { SERVICE_PLUGIN as ogmiosService } from '@demeter-features/cardano-ogmios';
import { SERVICE_PLUGIN as dBSyncService } from '@demeter-features/cardano-dbsync';
import { SERVICE_PLUGIN as kupoService } from '@demeter-features/cardano-kupo';
import { SERVICE_PLUGIN as marloweService } from '@demeter-features/cardano-marlowe';
import { SERVICE_PLUGIN as marlowePortService } from '@demeter-features/cardano-marlowe-port';
import { V2 as dbSyncV2 } from '@demeter-features/cardano-dbsync';
import { V2 as nodesV2 } from '@demeter-features/cardano-nodes';
const dbSyncServiceV2 = dbSyncV2.SERVICE_PLUGIN;
const nodesServiceV2 = nodesV2.SERVICE_PLUGIN;


Expand Down Expand Up @@ -72,15 +66,10 @@ export function getService(id: string): ServicePlugin | null {
*/
export function registerServices() {
registerService(submitApiService);
registerService(nodesService);
registerService(nodePortService);
registerService(ogmiosService);
registerService(kuberService);
registerService(kupoService);
registerService(blockfrostService);
registerService(marlowePortService);
registerService(dBSyncService);
registerService(marloweService);
registerService(dbSyncServiceV2);
registerService(nodesServiceV2);
}
5 changes: 2 additions & 3 deletions operator/src/shared/cardano-node-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const MAGIC_BY_NETWORK: Record<string, string> = {
testnet: '1097911063',
mainnet: '764824073',
sanchonet: '4',
'cc-private': '5',
'vector-testnet': '1177',
};

Expand Down Expand Up @@ -47,7 +46,7 @@ export function getCardanoNodeEnvVars(dep: DependencyResource, service: ServiceP
const port = nodePrivateDNS.split(':')[1];
return [
{ name: 'CARDANO_NODE_HOST', value: host },
{ name: 'CARDANO_NODE_PORT', value: port },
{ name: 'CARDANO_NODE_PORT', value: String(port) },
{ name: 'CARDANO_NODE_MAGIC', value: networkMagic(network)! },
{ name: 'CARDANO_TESTNET_MAGIC', value: networkMagic(network)! },
{ name: 'CARDANO_NODE_NETWORK_ID', value: networkMagic(network)! },
Expand All @@ -61,7 +60,7 @@ export function getCardanoNodePortEnvVars(instance: ServiceInstanceWithStatus):
const port = CARDANO_NODE_PORT_PORT;
return [
{ name: 'CARDANO_NODE_HOST', value: host },
{ name: 'CARDANO_NODE_PORT', value: port },
{ name: 'CARDANO_NODE_PORT', value: String(port) },
{ name: 'CARDANO_NODE_MAGIC', value: networkMagic(network)! },
{ name: 'CARDANO_TESTNET_MAGIC', value: networkMagic(network)! },
{ name: 'CARDANO_NODE_NETWORK_ID', value: networkMagic(network)! },
Expand Down
28 changes: 15 additions & 13 deletions operator/src/shared/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { V1EnvVar } from '@kubernetes/client-node';
import { getService, ServiceInstanceWithStatusAndKind } from '../services';
import { getNetworkFromAnnotations } from '.';
import { getCardanoNodeEnvVars } from './cardano-node-helper';
import { portExists } from './ports';

export async function getDependenciesForNetwork(project: ProjectSpec, network: Network) {
const deps = await listDependencies(project);
Expand All @@ -21,19 +22,6 @@ export function isCardanoNodeEnabled(deps: DependencyResource[]): boolean {
return false;
}

export function cardanoNodePort(instances: ServiceInstanceWithStatusAndKind[]): ServiceInstanceWithStatusAndKind | null{
for (const instance of instances) {
if (instance.kind === 'CardanoNodePort') {
return instance;
}
}
return null;
}

export function isCardanoNodePortEnabled(instances: ServiceInstanceWithStatusAndKind[]): boolean {
return !!cardanoNodePort(instances);
}

export function cardanoNodeDep(deps: DependencyResource[]): { dependency: DependencyResource; service: ServicePlugin } | null {
for (const dep of deps) {
const service = getService(dep.spec.serviceId);
Expand Down Expand Up @@ -64,3 +52,17 @@ export async function buildEnvVars(deps: DependencyResource[], network: Network)
}
return output;
}

// check if we should filter some dependencies because ports exists

export function cleanDependencies(deps: DependencyResource[], ports: ServiceInstanceWithStatusAndKind[]): DependencyResource[] {
const depsToIgnore: string[] = [];
if (portExists(ports, 'CardanoNodePort')) {
depsToIgnore.push('CardanoNode');
}
if (portExists(ports, 'MarlowePort')) {
depsToIgnore.push('Marlowe');
}

return deps.filter(dep => !depsToIgnore.includes(dep.spec.serviceKind));
}
40 changes: 25 additions & 15 deletions operator/src/shared/ports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import type { Network, ServiceMetadata } from '@demeter-sdk/framework';
import { getService, ServiceInstanceWithStatus, ServiceInstanceWithStatusAndKind, getAllServices } from '../services';
import { getCardanoNodePortEnvVars } from './cardano-node-helper';


function removeSchema(url: string): string {
return url.replace("https://", '').replace("http://", '')
return url.replace('https://', '').replace('http://', '');
}

const DEFAULT_MARLOWE_VERSION = 'patch6';

export async function getPortsForNetwork(project: ProjectSpec, network: Network): Promise<ServiceInstanceWithStatusAndKind[]> {
const output: ServiceInstanceWithStatusAndKind[] = [];
const services = (await getAllServices()).filter(service => service.key.includes('port'));
Expand All @@ -20,27 +21,26 @@ export async function getPortsForNetwork(project: ProjectSpec, network: Network)
for (const projectInstance of projectInstances) {
const inst = await projectInstance.instances;
inst.forEach(instance => {
if (instance.spec.network === network) {
output.push({ ...instance, kind: projectInstance.metadata.kind })
}
})
output.push()
if (instance.spec.network === network) {
output.push({ ...instance, kind: projectInstance.metadata.kind });
}
});
output.push();
}
return output;
}

export function parseInstanceToEnvVars(instance: ServiceInstanceWithStatusAndKind): EnvVar[] {
switch (instance.kind) {
case 'CardanoNodePort':
return getCardanoNodePortEnvVars(instance)
return getCardanoNodePortEnvVars(instance);
case 'MarlowePort':
const rt_host = `${instance.spec.network}-${instance.spec.marlowe_version}-rt.ext-marlowe-m1.svc.cluster.local`;
const rt_host = `${instance.spec.network}-${instance.spec.marloweVersion || DEFAULT_MARLOWE_VERSION}-rt.ext-marlowe-m1.svc.cluster.local`;
return [
{ name: "MARLOWE_RT_WEBSERVER_HOST", value: removeSchema(instance.status.authenticatedEndpointUrl)},
{ name: "MARLOWE_RT_WEBSERVER_PORT", value: "3700"},
{ name: "MARLOWE_RT_HOST", value: rt_host},
{ name: "MARLOWE_RT_PORT", value: "3701"},

{ name: 'MARLOWE_RT_WEBSERVER_HOST', value: removeSchema(instance.status.authenticatedEndpointUrl) },
{ name: 'MARLOWE_RT_WEBSERVER_PORT', value: '3700' },
{ name: 'MARLOWE_RT_HOST', value: rt_host },
{ name: 'MARLOWE_RT_PORT', value: '3701' },
];
default: {
return [];
Expand All @@ -54,5 +54,15 @@ export async function buildPortEnvVars(instances: ServiceInstanceWithStatusAndKi
const envVars = parseInstanceToEnvVars(item);
output.push(...envVars);
});
return output
return output;
}

export function portExists(instances: ServiceInstanceWithStatusAndKind[], kind: string): ServiceInstanceWithStatusAndKind | null {
for (const instance of instances) {
if (instance.kind === kind) {
return instance;
}
}
return null;
}

16 changes: 8 additions & 8 deletions operator/src/workspaces/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { getClients, readProjectUnsecure, Network, namespaceToSlug, DependencySpec, ServicePlugin, DependencyResource } from '@demeter-sdk/framework';
import { API_VERSION, API_GROUP, PLURAL, SINGULAR, KIND, DEFAULT_VSCODE_IMAGE } from './constants';
import { CustomResource, Workspace, StorageClass, CustomResourceResponse } from '@demeter-run/workloads-types';
import { buildEnvVars, cardanoNodeDep, cardanoNodePort, getDependenciesForNetwork, isCardanoNodeEnabled } from '../shared/dependencies';
import { buildEnvVars, cardanoNodeDep, cleanDependencies, getDependenciesForNetwork, isCardanoNodeEnabled } from '../shared/dependencies';
import {
getComputeDCUPerMin,
getNetworkFromAnnotations,
Expand All @@ -25,7 +25,7 @@ import {
} from '../shared';
import { buildDefaultEnvVars, buildDnsZone, INITIAL_ENV_VAR_NAMES } from './helpers';
import { buildSocatContainer, buildSocatContainerForPort } from '../shared/cardano-node-helper';
import { buildPortEnvVars, getPortsForNetwork } from '../shared/ports';
import { buildPortEnvVars, getPortsForNetwork, portExists } from '../shared/ports';
import { ServiceInstanceWithStatusAndKind } from '../services';

const tolerations = [
Expand Down Expand Up @@ -67,13 +67,13 @@ export async function handleResource(
const deps = await getDependenciesForNetwork(project, network);
const ports = await getPortsForNetwork(project, network);
const portEnvVars = await buildPortEnvVars(ports);
const depsEnvVars = await buildEnvVars(deps, network);
const defaultEnvVars = buildDefaultEnvVars(spec);
const cardanoNode = cardanoNodeDep(deps);
const cardanoNodePortInstance = cardanoNodePort(ports);
const depsEnvVars = await buildEnvVars(cleanDependencies(deps, ports), network);
const envVars = [...depsEnvVars, ...defaultEnvVars, ...portEnvVars];
const volumesList = volumes(!!cardanoNode);
const containerList = containers(spec, envVars, cardanoNode, cardanoNodePortInstance);
const cardanoNode = cardanoNodeDep(deps);
const cardanoNodePort = portExists(ports, 'CardanoNodePort');
const volumesList = volumes(!!cardanoNode || !!cardanoNodePort);
const containerList = containers(spec, envVars, cardanoNode, cardanoNodePort);
try {
await apps.readNamespacedStatefulSet(name, ns);
await updateResource(ns, name, spec, containerList, volumesList, owner);
Expand Down Expand Up @@ -207,7 +207,7 @@ export async function updateResourceStatus(ns: string, name: string, resource: V
const owner = await loadResource(ns, name);

const runningStatus = getSTSStatus(resource.status!, resource.spec?.replicas!, owner.status.runningStatus);

let computeDCUPerMin = 0;
if (runningStatus === 'running') {
computeDCUPerMin = getComputeDCUPerMin(owner.spec.computeClass, resource.spec?.replicas!);
Expand Down
Loading

0 comments on commit 6f06187

Please sign in to comment.