Skip to content

Commit ca4ea17

Browse files
authored
Skip writing extension containers in output context file (#154)
1 parent ed70e2f commit ca4ea17

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,12 @@ export async function prepareJob(
119119
throw new Error(`failed to determine if the pod is alpine: ${message}`)
120120
}
121121
core.debug(`Setting isAlpine to ${isAlpine}`)
122-
generateResponseFile(responseFile, createdPod, isAlpine)
122+
generateResponseFile(responseFile, args, createdPod, isAlpine)
123123
}
124124

125125
function generateResponseFile(
126126
responseFile: string,
127+
args: PrepareJobArgs,
127128
appPod: k8s.V1Pod,
128129
isAlpine
129130
): void {
@@ -156,24 +157,27 @@ function generateResponseFile(
156157
}
157158
}
158159

159-
const serviceContainers = appPod.spec?.containers.filter(
160-
c => c.name !== JOB_CONTAINER_NAME
161-
)
162-
if (serviceContainers?.length) {
163-
response.context['services'] = serviceContainers.map(c => {
164-
const ctxPorts: ContextPorts = {}
165-
if (c.ports?.length) {
166-
for (const port of c.ports) {
167-
ctxPorts[port.containerPort] = port.hostPort
160+
if (args.services?.length) {
161+
const serviceContainerNames =
162+
args.services?.map(s => generateContainerName(s.image)) || []
163+
164+
response.context['services'] = appPod?.spec?.containers
165+
?.filter(c => serviceContainerNames.includes(c.name))
166+
.map(c => {
167+
const ctxPorts: ContextPorts = {}
168+
if (c.ports?.length) {
169+
for (const port of c.ports) {
170+
ctxPorts[port.containerPort] = port.hostPort
171+
}
168172
}
169-
}
170173

171-
return {
172-
image: c.image,
173-
ports: ctxPorts
174-
}
175-
})
174+
return {
175+
image: c.image,
176+
ports: ctxPorts
177+
}
178+
})
176179
}
180+
177181
writeToResponseFile(responseFile, JSON.stringify(response))
178182
}
179183

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ describe('Prepare job', () => {
147147
expect(got.spec?.containers[2].args).toEqual(['-c', 'sleep 60'])
148148
})
149149

150+
it('should put only job and services in output context file', async () => {
151+
process.env[ENV_HOOK_TEMPLATE_PATH] = path.join(
152+
__dirname,
153+
'../../../examples/extension.yaml'
154+
)
155+
156+
await expect(
157+
prepareJob(prepareJobData.args, prepareJobOutputFilePath)
158+
).resolves.not.toThrow()
159+
160+
const content = JSON.parse(
161+
fs.readFileSync(prepareJobOutputFilePath).toString()
162+
)
163+
164+
expect(content.state.jobPod).toBeTruthy()
165+
expect(content.context.container).toBeTruthy()
166+
expect(content.context.services).toBeTruthy()
167+
expect(content.context.services.length).toBe(1)
168+
})
169+
150170
it('should not throw exception using kube scheduler', async () => {
151171
// only for ReadWriteMany volumes or single node cluster
152172
process.env[ENV_USE_KUBE_SCHEDULER] = 'true'

0 commit comments

Comments
 (0)