Use ACTIONS_RUNNER_CONTAINER_HOOK_TEMPLATE to extend service containers (#134)

https://github.com/actions/runner-container-hooks/issues/132

Co-authored-by: Katarzyna Radkowska <katarzyna.radkowska@sabre.com>
This commit is contained in:
Katarzyna
2024-02-20 16:19:29 +01:00
committed by GitHub
parent af27abe1f7
commit 7223e1dbb2
5 changed files with 32 additions and 11 deletions

View File

@@ -9,7 +9,7 @@ spec:
runAsGroup: 3000
restartPolicy: Never
containers:
- name: $job # overwirtes job container
- name: $job # overwrites job container
env:
- name: ENV1
value: "value1"
@@ -20,11 +20,22 @@ spec:
args:
- -c
- sleep 50
- name: $redis # overwrites redis service
env:
- name: ENV2
value: "value2"
image: "busybox:1.28" # Ignored
resources:
requests:
memory: "1Mi"
cpu: "1"
limits:
memory: "1Gi"
cpu: "2"
- name: side-car
image: "ubuntu:latest" # required
command:
- sh
- sh
args:
- -c
- sleep 60
- -c
- sleep 60

View File

@@ -41,6 +41,7 @@ export function getSecretName(): string {
export const MAX_POD_NAME_LENGTH = 63
export const STEP_POD_NAME_SUFFIX_LENGTH = 8
export const CONTAINER_EXTENSION_PREFIX = '$'
export const JOB_CONTAINER_NAME = 'job'
export const JOB_CONTAINER_EXTENSION_NAME = '$job'

View File

@@ -26,7 +26,7 @@ import {
PodPhase,
fixArgs
} from '../k8s/utils'
import { JOB_CONTAINER_EXTENSION_NAME, JOB_CONTAINER_NAME } from './constants'
import { CONTAINER_EXTENSION_PREFIX, JOB_CONTAINER_NAME } from './constants'
export async function prepareJob(
args: PrepareJobArgs,
@@ -60,7 +60,7 @@ export async function prepareJob(
service,
generateContainerName(service.image),
false,
undefined
extension
)
})
}
@@ -235,7 +235,7 @@ export function createContainerSpec(
}
const from = extension.spec?.containers?.find(
c => c.name === JOB_CONTAINER_EXTENSION_NAME
c => c.name === CONTAINER_EXTENSION_PREFIX + name
)
if (from) {

View File

@@ -6,7 +6,7 @@ import { Mount } from 'hooklib'
import * as path from 'path'
import { v1 as uuidv4 } from 'uuid'
import { POD_VOLUME_NAME } from './index'
import { JOB_CONTAINER_EXTENSION_NAME } from '../hooks/constants'
import { CONTAINER_EXTENSION_PREFIX } from '../hooks/constants'
import * as shlex from 'shlex'
export const DEFAULT_CONTAINER_ENTRY_POINT_ARGS = [`-f`, `/dev/null`]
@@ -180,7 +180,7 @@ export function mergeContainerWithOptions(
): void {
for (const [key, value] of Object.entries(from)) {
if (key === 'name') {
if (value !== base.name && value !== JOB_CONTAINER_EXTENSION_NAME) {
if (value !== CONTAINER_EXTENSION_PREFIX + base.name) {
core.warning("Skipping name override: name can't be overwritten")
}
continue
@@ -209,7 +209,9 @@ export function mergePodSpecWithOptions(
for (const [key, value] of Object.entries(from)) {
if (key === 'containers') {
base.containers.push(
...from.containers.filter(e => !e.name?.startsWith('$'))
...from.containers.filter(
e => !e.name?.startsWith(CONTAINER_EXTENSION_PREFIX)
)
)
} else if (key === 'volumes' && value) {
const volumes = value as k8s.V1Volume[]

View File

@@ -133,6 +133,13 @@ describe('Prepare job', () => {
expect(got.spec?.containers[1].image).toBe('redis')
expect(got.spec?.containers[1].command).toBeFalsy()
expect(got.spec?.containers[1].args).toBeFalsy()
expect(got.spec?.containers[1].env).toEqual([
{ name: 'ENV2', value: 'value2' }
])
expect(got.spec?.containers[1].resources).toEqual({
requests: { memory: '1Mi', cpu: '1' },
limits: { memory: '1Gi', cpu: '2' }
})
// side-car
expect(got.spec?.containers[2].name).toBe('side-car')
expect(got.spec?.containers[2].image).toBe('ubuntu:latest')