Skip to content

Commit 7223e1d

Browse files
Use ACTIONS_RUNNER_CONTAINER_HOOK_TEMPLATE to extend service containers (actions#134)
actions#132 Co-authored-by: Katarzyna Radkowska <[email protected]>
1 parent af27abe commit 7223e1d

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

examples/extension.yaml

+16-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ spec:
99
runAsGroup: 3000
1010
restartPolicy: Never
1111
containers:
12-
- name: $job # overwirtes job container
12+
- name: $job # overwrites job container
1313
env:
1414
- name: ENV1
1515
value: "value1"
@@ -20,11 +20,22 @@ spec:
2020
args:
2121
- -c
2222
- sleep 50
23+
- name: $redis # overwrites redis service
24+
env:
25+
- name: ENV2
26+
value: "value2"
27+
image: "busybox:1.28" # Ignored
28+
resources:
29+
requests:
30+
memory: "1Mi"
31+
cpu: "1"
32+
limits:
33+
memory: "1Gi"
34+
cpu: "2"
2335
- name: side-car
2436
image: "ubuntu:latest" # required
2537
command:
26-
- sh
38+
- sh
2739
args:
28-
- -c
29-
- sleep 60
30-
40+
- -c
41+
- sleep 60

packages/k8s/src/hooks/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function getSecretName(): string {
4141

4242
export const MAX_POD_NAME_LENGTH = 63
4343
export const STEP_POD_NAME_SUFFIX_LENGTH = 8
44+
export const CONTAINER_EXTENSION_PREFIX = '$'
4445
export const JOB_CONTAINER_NAME = 'job'
4546
export const JOB_CONTAINER_EXTENSION_NAME = '$job'
4647

packages/k8s/src/hooks/prepare-job.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
PodPhase,
2727
fixArgs
2828
} from '../k8s/utils'
29-
import { JOB_CONTAINER_EXTENSION_NAME, JOB_CONTAINER_NAME } from './constants'
29+
import { CONTAINER_EXTENSION_PREFIX, JOB_CONTAINER_NAME } from './constants'
3030

3131
export async function prepareJob(
3232
args: PrepareJobArgs,
@@ -60,7 +60,7 @@ export async function prepareJob(
6060
service,
6161
generateContainerName(service.image),
6262
false,
63-
undefined
63+
extension
6464
)
6565
})
6666
}
@@ -235,7 +235,7 @@ export function createContainerSpec(
235235
}
236236

237237
const from = extension.spec?.containers?.find(
238-
c => c.name === JOB_CONTAINER_EXTENSION_NAME
238+
c => c.name === CONTAINER_EXTENSION_PREFIX + name
239239
)
240240

241241
if (from) {

packages/k8s/src/k8s/utils.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Mount } from 'hooklib'
66
import * as path from 'path'
77
import { v1 as uuidv4 } from 'uuid'
88
import { POD_VOLUME_NAME } from './index'
9-
import { JOB_CONTAINER_EXTENSION_NAME } from '../hooks/constants'
9+
import { CONTAINER_EXTENSION_PREFIX } from '../hooks/constants'
1010
import * as shlex from 'shlex'
1111

1212
export const DEFAULT_CONTAINER_ENTRY_POINT_ARGS = [`-f`, `/dev/null`]
@@ -180,7 +180,7 @@ export function mergeContainerWithOptions(
180180
): void {
181181
for (const [key, value] of Object.entries(from)) {
182182
if (key === 'name') {
183-
if (value !== base.name && value !== JOB_CONTAINER_EXTENSION_NAME) {
183+
if (value !== CONTAINER_EXTENSION_PREFIX + base.name) {
184184
core.warning("Skipping name override: name can't be overwritten")
185185
}
186186
continue
@@ -209,7 +209,9 @@ export function mergePodSpecWithOptions(
209209
for (const [key, value] of Object.entries(from)) {
210210
if (key === 'containers') {
211211
base.containers.push(
212-
...from.containers.filter(e => !e.name?.startsWith('$'))
212+
...from.containers.filter(
213+
e => !e.name?.startsWith(CONTAINER_EXTENSION_PREFIX)
214+
)
213215
)
214216
} else if (key === 'volumes' && value) {
215217
const volumes = value as k8s.V1Volume[]

packages/k8s/tests/prepare-job-test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ describe('Prepare job', () => {
133133
expect(got.spec?.containers[1].image).toBe('redis')
134134
expect(got.spec?.containers[1].command).toBeFalsy()
135135
expect(got.spec?.containers[1].args).toBeFalsy()
136+
expect(got.spec?.containers[1].env).toEqual([
137+
{ name: 'ENV2', value: 'value2' }
138+
])
139+
expect(got.spec?.containers[1].resources).toEqual({
140+
requests: { memory: '1Mi', cpu: '1' },
141+
limits: { memory: '1Gi', cpu: '2' }
142+
})
136143
// side-car
137144
expect(got.spec?.containers[2].name).toBe('side-car')
138145
expect(got.spec?.containers[2].image).toBe('ubuntu:latest')

0 commit comments

Comments
 (0)