Skip to content

Commit 9863d41

Browse files
committed
feat: label step pods
1 parent b05a88f commit 9863d41

1 file changed

Lines changed: 41 additions & 28 deletions

File tree

packages/k8s/src/k8s/index.ts

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import * as core from '@actions/core'
2-
<<<<<<< HEAD
32
import * as path from 'path'
43
import { spawn } from 'child_process'
5-
=======
64
import { context as jobContext } from '@actions/github'
7-
>>>>>>> a3d6427 (feat: label job pods with context info)
85
import * as k8s from '@kubernetes/client-node'
96
import tar from 'tar-fs'
107
import * as stream from 'stream'
@@ -66,10 +63,6 @@ export const requiredPermissions = [
6663
}
6764
]
6865

69-
<<<<<<< HEAD
70-
export async function createJobPod(
71-
name: string,
72-
=======
7366
/**
7467
* Valid k8s labels conform to the following rules:
7568
* - must be 63 characters or less (can be empty),
@@ -96,8 +89,27 @@ function sanitizeLabel(label: string): string {
9689
return sluggedLabel
9790
}
9891

92+
function getArcContextLabels(): { [key: string]: string } {
93+
const { GITHUB_RUN_ID, GITHUB_RUN_NUMBER, GITHUB_RUN_ATTEMPT } = process.env
94+
return Object.fromEntries(
95+
Object.entries({
96+
'arc-context-event-name': jobContext.eventName,
97+
'arc-context-sha': jobContext.sha,
98+
'arc-context-workflow': jobContext.workflow,
99+
'arc-context-actor': jobContext.actor,
100+
'arc-context-job': jobContext.job,
101+
'arc-context-repository': jobContext.repo.repo,
102+
'arc-context-repository-owner': jobContext.repo.owner,
103+
'arc-context-run-id': GITHUB_RUN_ID || '',
104+
'arc-context-run-number': GITHUB_RUN_NUMBER || '',
105+
'arc-context-run-attempt': GITHUB_RUN_ATTEMPT || ''
106+
})
107+
.map(([key, value]) => [key, sanitizeLabel(value)])
108+
.filter(([, value]) => value !== '')
109+
)
110+
}
111+
99112
export async function createPod(
100-
>>>>>>> a3d6427 (feat: label job pods with context info)
101113
jobContainer?: k8s.V1Container,
102114
services?: k8s.V1Container[],
103115
registry?: Registry,
@@ -119,22 +131,7 @@ export async function createPod(
119131
appPod.metadata = new k8s.V1ObjectMeta()
120132
appPod.metadata.name = name
121133

122-
const { GITHUB_RUN_ID, GITHUB_RUN_NUMBER, GITHUB_RUN_ATTEMPT } = process.env
123-
const arcLabels = Object.fromEntries(
124-
Object.entries({
125-
'arc-context-event-name': jobContext.eventName,
126-
'arc-context-sha': jobContext.sha,
127-
'arc-context-workflow': jobContext.workflow,
128-
'arc-context-actor': jobContext.actor,
129-
'arc-context-job': jobContext.job,
130-
'arc-context-repository': jobContext.repo.repo,
131-
'arc-context-repository-owner': jobContext.repo.owner,
132-
'arc-context-run-id': GITHUB_RUN_ID || '',
133-
'arc-context-run-number': GITHUB_RUN_NUMBER || '',
134-
'arc-context-run-attempt': GITHUB_RUN_ATTEMPT || ''
135-
}).map(([key, value]) => [key, sanitizeLabel(value)])
136-
)
137-
134+
const arcLabels = getArcContextLabels()
138135
const instanceLabel = new RunnerInstanceLabel()
139136
appPod.metadata.labels = {
140137
[instanceLabel.key]: instanceLabel.value,
@@ -250,8 +247,20 @@ export async function createContainerStepPod(
250247
appPod.metadata.name = name
251248

252249
const instanceLabel = new RunnerInstanceLabel()
253-
appPod.metadata.labels = {
254-
[instanceLabel.key]: instanceLabel.value
250+
const arcLabels = getArcContextLabels()
251+
appPod.metadata.labels = arcLabels
252+
job.spec.template.spec = new k8s.V1PodSpec()
253+
job.spec.template.metadata = new k8s.V1ObjectMeta()
254+
job.spec.template.metadata.labels = arcLabels
255+
job.spec.template.metadata.annotations = {}
256+
job.spec.template.spec.containers = [container]
257+
job.spec.template.spec.restartPolicy = 'Never'
258+
259+
const nodeName = await getCurrentNodeName()
260+
if (useKubeScheduler()) {
261+
job.spec.template.spec.affinity = await getPodAffinity(nodeName)
262+
} else {
263+
job.spec.template.spec.nodeName = nodeName
255264
}
256265
appPod.metadata.annotations = {}
257266

@@ -581,7 +590,9 @@ export async function execCpFromPod(
581590
attempt++
582591
if (attempt >= 30) {
583592
throw new Error(
584-
`execCpFromPod failed after ${attempt} attempts: ${JSON.stringify(error)}`
593+
`execCpFromPod failed after ${attempt} attempts: ${JSON.stringify(
594+
error
595+
)}`
585596
)
586597
}
587598
await sleep(1000)
@@ -751,7 +762,9 @@ export async function waitForPodPhases(
751762
}
752763
} catch (error) {
753764
throw new Error(
754-
`Pod ${podName} is unhealthy with phase status ${phase}: ${JSON.stringify(error)}`
765+
`Pod ${podName} is unhealthy with phase status ${phase}: ${JSON.stringify(
766+
error
767+
)}`
755768
)
756769
}
757770
}

0 commit comments

Comments
 (0)