Skip to content

support --name create option #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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