Skip to content

Commit

Permalink
Merge pull request #1 from isegall-da/isegall/multi-service-containers
Browse files Browse the repository at this point in the history
support --name create option
  • Loading branch information
isegall-da authored Jan 10, 2025
2 parents 120636d + b65625d commit abe03b9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/k8s/src/hooks/prepare-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function prepareJob(
core.debug(`Adding service '${service.image}' to pod definition`)
return createContainerSpec(
service,
generateContainerName(service.image),
generateContainerName(service),
false,
extension
)
Expand Down Expand Up @@ -159,7 +159,7 @@ function generateResponseFile(

if (args.services?.length) {
const serviceContainerNames =
args.services?.map(s => generateContainerName(s.image)) || []
args.services?.map(s => generateContainerName(s)) || []

response.context['services'] = appPod?.spec?.containers
?.filter(c => serviceContainerNames.includes(c.name))
Expand Down
20 changes: 17 additions & 3 deletions packages/k8s/src/k8s/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as k8s from '@kubernetes/client-node'
import * as fs from 'fs'
import * as yaml from 'js-yaml'
import * as core from '@actions/core'
import { Mount } from 'hooklib'
import { ServiceContainerInfo, Mount } from 'hooklib'
import * as path from 'path'
import { v1 as uuidv4 } from 'uuid'
import { POD_VOLUME_NAME } from './index'
Expand Down Expand Up @@ -160,14 +160,28 @@ exec ${environmentPrefix} ${entryPoint} ${
}
}

export function generateContainerName(image: string): string {
export function generateContainerName(service: ServiceContainerInfo): string {
const image = service.image
const nameWithTag = image.split('/').pop()
const name = nameWithTag?.split(':').at(0)
let name = nameWithTag?.split(':').at(0)

if (!name) {
throw new Error(`Image definition '${image}' is invalid`)
}

if (service.createOptions) {
const optionsArr = service.createOptions.split(/[ ]+/)
for (let i = 0; i < optionsArr.length; i++) {
if (optionsArr[i] === '--name') {
if (i + 1 >= optionsArr.length) {
throw new Error(`Invalid create options: ${service.createOptions} (missing a value after --name)`)
}
name = optionsArr[++i]
core.debug(`Overriding service container name with: ${name}`)
}
}
}

return name
}

Expand Down

0 comments on commit abe03b9

Please sign in to comment.