Skip to content

Commit

Permalink
small refactor, support multiple platforms, most for k8
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Dec 16, 2024
1 parent ddfc8a5 commit b96debe
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/@types/C2D/C2D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface ComputeEnvironmentBaseConfig {
priceMin: number
}

export interface DockerPlatform {
export interface RunningPlatform {
architecture: string
os: string
}
Expand All @@ -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 {
Expand Down
11 changes: 7 additions & 4 deletions src/components/c2d/compute_engine_docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -137,7 +137,7 @@ export class C2DEngineDocker extends C2DEngine {
*/
public static async checkDockerImage(
image: string,
platform?: DockerPlatform
platform?: RunningPlatform
): Promise<ValidateParams> {
try {
const info = drc.default.parseRepoAndRef(image)
Expand Down Expand Up @@ -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}`)

Expand Down Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion src/components/core/compute/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/test/unit/compute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b96debe

Please sign in to comment.