From b96debe60b9ee0d2c877937628fbf1990c3e80c9 Mon Sep 17 00:00:00 2001 From: paulo-ocean Date: Mon, 16 Dec 2024 17:02:05 +0000 Subject: [PATCH] small refactor, support multiple platforms, most for k8 --- src/@types/C2D/C2D.ts | 4 ++-- src/components/c2d/compute_engine_docker.ts | 11 +++++++---- src/components/core/compute/initialize.ts | 2 +- src/test/unit/compute.test.ts | 4 ++-- src/utils/config.ts | 6 +++--- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/@types/C2D/C2D.ts b/src/@types/C2D/C2D.ts index 9bb978396..fb3ba5957 100644 --- a/src/@types/C2D/C2D.ts +++ b/src/@types/C2D/C2D.ts @@ -33,7 +33,7 @@ export interface ComputeEnvironmentBaseConfig { priceMin: number } -export interface DockerPlatform { +export interface RunningPlatform { architecture: string os: string } @@ -47,7 +47,7 @@ export interface ComputeEnvironment extends ComputeEnvironmentBaseConfig { consumerAddress: string lastSeen?: number free: boolean - platform?: DockerPlatform + platform?: RunningPlatform[] // array due to k8 support } export interface C2DDockerConfig { diff --git a/src/components/c2d/compute_engine_docker.ts b/src/components/c2d/compute_engine_docker.ts index 93767a025..69577ed55 100644 --- a/src/components/c2d/compute_engine_docker.ts +++ b/src/components/c2d/compute_engine_docker.ts @@ -10,7 +10,7 @@ import type { ComputeOutput, DBComputeJob, ComputeResult, - DockerPlatform + RunningPlatform } from '../../@types/C2D/C2D.js' import { getConfiguration } from '../../utils/config.js' import { C2DEngine } from './compute_engine_base.js' @@ -137,7 +137,7 @@ export class C2DEngineDocker extends C2DEngine { */ public static async checkDockerImage( image: string, - platform?: DockerPlatform + platform?: RunningPlatform ): Promise { try { const info = drc.default.parseRepoAndRef(image) @@ -220,7 +220,10 @@ export class C2DEngineDocker extends C2DEngine { environment ) - const validation = await C2DEngineDocker.checkDockerImage(image, env.platform) + const validation = await C2DEngineDocker.checkDockerImage( + image, + env.platform && env.platform.length > 0 ? env.platform[0] : null + ) if (!validation.valid) throw new Error(`Unable to validate docker image ${image}: ${validation.reason}`) @@ -1103,7 +1106,7 @@ export function getAlgorithmImage(algorithm: ComputeAlgorithm): string { export function checkManifestPlatform( manifestPlatform: any, - envPlatform: DockerPlatform + envPlatform?: RunningPlatform ): boolean { if (!manifestPlatform || !envPlatform) return true // skips if not present if ( diff --git a/src/components/core/compute/initialize.ts b/src/components/core/compute/initialize.ts index 134442827..1b07a4882 100644 --- a/src/components/core/compute/initialize.ts +++ b/src/components/core/compute/initialize.ts @@ -146,7 +146,7 @@ export class ComputeInitializeHandler extends Handler { .getExactComputeEnv(task.compute.env, ddo.chainId) const validation: ValidateParams = await C2DEngineDocker.checkDockerImage( algoImage, - env.platform + env.platform && env.platform.length > 0 ? env.platform[0] : null ) if (!validation.valid) { return { diff --git a/src/test/unit/compute.test.ts b/src/test/unit/compute.test.ts index 85db3eee7..e4b7ba00a 100644 --- a/src/test/unit/compute.test.ts +++ b/src/test/unit/compute.test.ts @@ -9,7 +9,7 @@ import { ComputeEnvironment, ComputeJob, DBComputeJob, - DockerPlatform + RunningPlatform } from '../../@types/C2D/C2D.js' // import { computeAsset } from '../data/assets' import { assert, expect } from 'chai' @@ -211,7 +211,7 @@ describe('Compute Jobs Database', () => { it('should check manifest platform against local platform env', () => { const arch = os.machine() // ex: arm const platform = os.platform() // ex: linux - const env: DockerPlatform = { + const env: RunningPlatform = { architecture: arch, os: platform } diff --git a/src/utils/config.ts b/src/utils/config.ts index f51aed39e..c63954e5b 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -401,7 +401,7 @@ function getDockerFreeComputeOptions( feeToken: ZeroAddress, chainId: 8996, free: true, - platform: { architecture: os.machine(), os: os.platform() } + platform: [{ architecture: os.machine(), os: os.platform() }] } if (existsEnvironmentVariable(ENVIRONMENT_VARIABLES.DOCKER_FREE_COMPUTE, isStartup)) { @@ -411,7 +411,7 @@ function getDockerFreeComputeOptions( ) as ComputeEnvironmentBaseConfig doComputeEnvChecks([options]) const env = { ...options } as ComputeEnvironment - env.platform = { architecture: os.machine(), os: os.platform() } + env.platform = [{ architecture: os.machine(), os: os.platform() }] return env } catch (error) { CONFIG_LOGGER.logMessageWithEmoji( @@ -462,7 +462,7 @@ function getDockerComputeEnvironments(isStartup?: boolean): ComputeEnvironment[] doComputeEnvChecks(options) const envs = { ...options } as ComputeEnvironment[] envs.forEach((env) => { - env.platform = { architecture: os.machine(), os: os.platform() } + env.platform = [{ architecture: os.machine(), os: os.platform() }] }) return envs } catch (error) {