Skip to content

Commit abe03b9

Browse files
authored
Merge pull request #1 from isegall-da/isegall/multi-service-containers
support --name create option
2 parents 120636d + b65625d commit abe03b9

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

Diff for: packages/k8s/src/hooks/prepare-job.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export async function prepareJob(
5858
core.debug(`Adding service '${service.image}' to pod definition`)
5959
return createContainerSpec(
6060
service,
61-
generateContainerName(service.image),
61+
generateContainerName(service),
6262
false,
6363
extension
6464
)
@@ -159,7 +159,7 @@ function generateResponseFile(
159159

160160
if (args.services?.length) {
161161
const serviceContainerNames =
162-
args.services?.map(s => generateContainerName(s.image)) || []
162+
args.services?.map(s => generateContainerName(s)) || []
163163

164164
response.context['services'] = appPod?.spec?.containers
165165
?.filter(c => serviceContainerNames.includes(c.name))

Diff for: packages/k8s/src/k8s/utils.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as k8s from '@kubernetes/client-node'
22
import * as fs from 'fs'
33
import * as yaml from 'js-yaml'
44
import * as core from '@actions/core'
5-
import { Mount } from 'hooklib'
5+
import { ServiceContainerInfo, Mount } from 'hooklib'
66
import * as path from 'path'
77
import { v1 as uuidv4 } from 'uuid'
88
import { POD_VOLUME_NAME } from './index'
@@ -160,14 +160,28 @@ exec ${environmentPrefix} ${entryPoint} ${
160160
}
161161
}
162162

163-
export function generateContainerName(image: string): string {
163+
export function generateContainerName(service: ServiceContainerInfo): string {
164+
const image = service.image
164165
const nameWithTag = image.split('/').pop()
165-
const name = nameWithTag?.split(':').at(0)
166+
let name = nameWithTag?.split(':').at(0)
166167

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

172+
if (service.createOptions) {
173+
const optionsArr = service.createOptions.split(/[ ]+/)
174+
for (let i = 0; i < optionsArr.length; i++) {
175+
if (optionsArr[i] === '--name') {
176+
if (i + 1 >= optionsArr.length) {
177+
throw new Error(`Invalid create options: ${service.createOptions} (missing a value after --name)`)
178+
}
179+
name = optionsArr[++i]
180+
core.debug(`Overriding service container name with: ${name}`)
181+
}
182+
}
183+
}
184+
171185
return name
172186
}
173187

0 commit comments

Comments
 (0)