55 JobContainerInfo ,
66 ContextPorts ,
77 PrepareJobArgs ,
8+ ServiceContainerInfo ,
89 writeToResponseFile
910} from 'hooklib'
1011import path from 'path'
@@ -69,20 +70,11 @@ export async function prepareJob(
6970 )
7071 }
7172
72- let services : k8s . V1Container [ ] = [ ]
73- if ( args . services ?. length ) {
74- generateServicesName ( args . services )
75- services = args . services . map ( service => {
76- core . debug ( `Adding service '${ service . image } ' to pod definition` )
77- return createContainerSpec (
78- service ,
79- generateContainerName ( service . image ) ,
80- false ,
81- extension ,
82- service . createOptions
83- )
84- } )
85- }
73+ const services : k8s . V1Container [ ] = processServiceContainers (
74+ args . services ,
75+ container ,
76+ extension
77+ )
8678
8779 if ( ! container && ! services ?. length ) {
8880 throw new Error ( 'No containers exist, skipping hook invocation' )
@@ -152,6 +144,58 @@ export async function prepareJob(
152144 generateResponseFile ( responseFile , args , createdPod , isAlpine )
153145}
154146
147+ export function processServiceContainers (
148+ services ?: ServiceContainerInfo [ ] ,
149+ container ?: k8s . V1Container ,
150+ extension ?: k8s . V1PodTemplateSpec
151+ ) : k8s . V1Container [ ] {
152+ if ( ! services ?. length ) {
153+ return [ ]
154+ }
155+ generateServicesName ( services )
156+ const serviceContainers = services . map ( service => {
157+ core . debug ( `Adding service '${ service . image } ' to pod definition` )
158+ return createContainerSpec (
159+ service ,
160+ service . name ,
161+ false ,
162+ extension ,
163+ service . createOptions
164+ )
165+ } )
166+
167+ const tpuRequestingContainers = services . filter (
168+ service =>
169+ service . resources ?. limits && service . resources . limits [ 'google.com/tpu' ]
170+ )
171+
172+ if ( tpuRequestingContainers . length > 1 ) {
173+ throw new Error (
174+ `${ tpuRequestingContainers . length } containers request for TPU's. Only 1 container per pod can request for TPU's.`
175+ )
176+ }
177+
178+ if ( tpuRequestingContainers . length === 1 ) {
179+ if (
180+ container ?. resources ?. requests &&
181+ container . resources . requests [ 'google.com/tpu' ]
182+ ) {
183+ core . debug (
184+ 'removing tpu from main container resources request and limits as they are requested by the service container and only 1 container in a pod can request TPU.'
185+ )
186+ delete container . resources . requests [ 'google.com/tpu' ]
187+ if (
188+ container . resources . limits &&
189+ container . resources . limits [ 'google.com/tpu' ]
190+ ) {
191+ core . debug ( 'removing tpu from main container resource limits' )
192+ delete container . resources . limits [ 'google.com/tpu' ]
193+ }
194+ }
195+ }
196+ return serviceContainers
197+ }
198+
155199// Create JobSet and waits for it to come online
156200async function prepareJobSet (
157201 args : PrepareJobArgs ,
@@ -356,11 +400,20 @@ export function createContainerSpec(
356400 }
357401
358402 podContainer . env = [ ]
359- for ( const [ key , value ] of Object . entries (
360- container [ 'environmentVariables' ]
361- ) ) {
362- if ( value && key !== 'HOME' ) {
363- podContainer . env . push ( { name : key , value : value as string } )
403+ if ( container [ 'environmentVariables' ] ) {
404+ for ( const [ key , value ] of Object . entries (
405+ container [ 'environmentVariables' ]
406+ ) ) {
407+ if ( value && key !== 'HOME' ) {
408+ podContainer . env . push ( { name : key , value : value as string } )
409+ }
410+ }
411+
412+ if ( ! ( 'CI' in container [ 'environmentVariables' ] ) ) {
413+ podContainer . env . push ( {
414+ name : 'CI' ,
415+ value : 'true'
416+ } )
364417 }
365418 }
366419
@@ -369,13 +422,6 @@ export function createContainerSpec(
369422 value : 'true'
370423 } )
371424
372- if ( ! ( 'CI' in container [ 'environmentVariables' ] ) ) {
373- podContainer . env . push ( {
374- name : 'CI' ,
375- value : 'true'
376- } )
377- }
378-
379425 podContainer . volumeMounts = containerVolumes (
380426 container . userMountVolumes ,
381427 jobContainer
0 commit comments