Skip to content

Commit

Permalink
Use ACTIONS_RUNNER_CONTAINER_HOOK_TEMPLATE to extend service containe…
Browse files Browse the repository at this point in the history
…rs (#134)

#132

Co-authored-by: Katarzyna Radkowska <[email protected]>
  • Loading branch information
katarzynainit and kr-sabre authored Feb 20, 2024
1 parent af27abe commit 7223e1d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
21 changes: 16 additions & 5 deletions examples/extension.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
1 change: 1 addition & 0 deletions packages/k8s/src/hooks/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
6 changes: 3 additions & 3 deletions packages/k8s/src/hooks/prepare-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -60,7 +60,7 @@ export async function prepareJob(
service,
generateContainerName(service.image),
false,
undefined
extension
)
})
}
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions packages/k8s/src/k8s/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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[]
Expand Down
7 changes: 7 additions & 0 deletions packages/k8s/tests/prepare-job-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 7223e1d

Please sign in to comment.