@@ -297,58 +297,30 @@ protected Container startContainer(ContainerSpec spec, Proxy proxy) throws Excep
297297 Pod startedPod = kubeClient .pods ().inNamespace (effectiveKubeNamespace ).create (patchedPod );
298298
299299 int totalWaitMs = Integer .parseInt (environment .getProperty ("proxy.kubernetes.pod-wait-time" , "60000" ));
300- int maxTries = totalWaitMs / 1000 ;
301- Retrying .retry (i -> {
302- if (!Readiness .getInstance ().isReady (kubeClient .resource (startedPod ).fromServer ().get ())) {
303- if (i > 1 && log != null ) log .debug (String .format ("Container not ready yet, trying again (%d/%d)" , i , maxTries ));
304- return false ;
305- }
306- return true ;
300+ boolean podReady = Retrying .retry ((currentAttempt , maxAttempts ) -> {
301+ if (!Readiness .getInstance ().isReady (kubeClient .resource (startedPod ).fromServer ().get ())) {
302+ if (currentAttempt > 0 && log != null ) log .debug (String .format ("Container not ready yet, trying again (%d/%d)" , currentAttempt , maxAttempts ));
303+ return false ;
307304 }
308- , maxTries , 1000 );
309- if (!Readiness .getInstance ().isReady (kubeClient .resource (startedPod ).fromServer ().get ())) {
310- Pod pod = kubeClient .resource (startedPod ).fromServer ().get ();
311- container .getParameters ().put (PARAM_POD , pod );
312- proxy .getContainers ().add (container );
313-
314- proxyStatusService .containerStartFailed (proxy , container );
315- throw new ContainerProxyException ("Container did not become ready in time" );
316- }
317- proxyStatusService .containerStarted (proxy , container );
318- Pod pod = kubeClient .resource (startedPod ).fromServer ().get ();
319-
320- // TODO check k8s compatibility
321- List <Event > events = kubeClient .v1 ().events ().withInvolvedObject (new ObjectReferenceBuilder ()
322- .withKind ("Pod" )
323- .withName (pod .getMetadata ().getName ())
324- .withNamespace (pod .getMetadata ().getNamespace ())
325- .build ()).list ().getItems ();
326-
327- LocalDateTime pullingTime = null ;
328- LocalDateTime pulledTime = null ;
329- LocalDateTime scheduledTime = null ;
330-
331- for (Event event : events ) {
332- if (event .getCount () != null && event .getCount () > 1 ) {
333- // ignore events which happened multiple time as we are unable to properly process them
334- continue ;
335- }
336- if (event .getReason ().equalsIgnoreCase ("Pulling" )) {
337- pullingTime = OffsetDateTime .parse (event .getLastTimestamp ()).atZoneSameInstant (ZoneId .systemDefault ()).toLocalDateTime ();
338- } else if (event .getReason ().equalsIgnoreCase ("Pulled" )) {
339- pulledTime = OffsetDateTime .parse (event .getLastTimestamp ()).atZoneSameInstant (ZoneId .systemDefault ()).toLocalDateTime ();
340- } else if (event .getReason ().equalsIgnoreCase ("Scheduled" )) {
341- scheduledTime = OffsetDateTime .parse (event .getEventTime ().getTime ()).atZoneSameInstant (ZoneId .systemDefault ()).toLocalDateTime ();
305+ return true ;
306+ }, totalWaitMs );
307+
308+ if (!podReady ) {
309+ // check a final time whether the pod is ready
310+ if (!Readiness .getInstance ().isReady (kubeClient .resource (startedPod ).fromServer ().get ())) {
311+ Pod pod = kubeClient .resource (startedPod ).fromServer ().get ();
312+ container .getParameters ().put (PARAM_POD , pod );
313+ proxy .getContainers ().add (container );
314+
315+ proxyStatusService .containerStartFailed (proxy , container );
316+ throw new ContainerProxyException ("Container did not become ready in time" );
342317 }
343318 }
344319
345- if (pullingTime != null && pulledTime != null ) {
346- proxyStatusService .imagePulled (proxy , container , pullingTime , pulledTime );
347- }
320+ proxyStatusService .containerStarted (proxy , container );
321+ Pod pod = kubeClient .resource (startedPod ).fromServer ().get ();
348322
349- if (scheduledTime != null ) {
350- proxyStatusService .containerScheduled (proxy , container , scheduledTime );
351- }
323+ parseKubernetesEvents (proxy , container , pod );
352324
353325 Service service = null ;
354326 if (isUseInternalNetwork ()) {
@@ -374,7 +346,7 @@ protected Container startContainer(ContainerSpec spec, Proxy proxy) throws Excep
374346 .build ());
375347
376348 // Workaround: waitUntilReady appears to be buggy.
377- Retrying .retry (i -> isServiceReady (kubeClient .resource (startupService ).fromServer ().get ()), 60 , 1000 );
349+ Retrying .retry (( currentAttempts , maxAttempts ) -> isServiceReady (kubeClient .resource (startupService ).fromServer ().get ()), 60_000 );
378350
379351 service = kubeClient .resource (startupService ).fromServer ().get ();
380352 }
@@ -400,6 +372,56 @@ protected Container startContainer(ContainerSpec spec, Proxy proxy) throws Excep
400372 return container ;
401373 }
402374
375+ private LocalDateTime getEventTime (Event event ) {
376+ if (event .getEventTime () != null && event .getEventTime ().getTime () != null ) {
377+ return OffsetDateTime .parse (event .getEventTime ().getTime ()).atZoneSameInstant (ZoneId .systemDefault ()).toLocalDateTime ();
378+ }
379+
380+ if (event .getFirstTimestamp () != null ) {
381+ return OffsetDateTime .parse (event .getFirstTimestamp ()).atZoneSameInstant (ZoneId .systemDefault ()).toLocalDateTime ();
382+ }
383+
384+ if (event .getLastTimestamp () != null ) {
385+ return OffsetDateTime .parse (event .getLastTimestamp ()).atZoneSameInstant (ZoneId .systemDefault ()).toLocalDateTime ();
386+ }
387+
388+ return null ;
389+ }
390+
391+ private void parseKubernetesEvents (Proxy proxy , Container container , Pod pod ) {
392+ List <Event > events = kubeClient .v1 ().events ().withInvolvedObject (new ObjectReferenceBuilder ()
393+ .withKind ("Pod" )
394+ .withName (pod .getMetadata ().getName ())
395+ .withNamespace (pod .getMetadata ().getNamespace ())
396+ .build ()).list ().getItems ();
397+
398+ LocalDateTime pullingTime = null ;
399+ LocalDateTime pulledTime = null ;
400+ LocalDateTime scheduledTime = null ;
401+
402+ for (Event event : events ) {
403+ if (event .getCount () != null && event .getCount () > 1 ) {
404+ // ignore events which happened multiple time as we are unable to properly process them
405+ continue ;
406+ }
407+ if (event .getReason ().equalsIgnoreCase ("Pulling" )) {
408+ pullingTime = getEventTime (event );
409+ } else if (event .getReason ().equalsIgnoreCase ("Pulled" )) {
410+ pulledTime = getEventTime (event );
411+ } else if (event .getReason ().equalsIgnoreCase ("Scheduled" )) {
412+ scheduledTime = getEventTime (event );
413+ }
414+ }
415+
416+ if (pullingTime != null && pulledTime != null ) {
417+ proxyStatusService .imagePulled (proxy , container , pullingTime , pulledTime );
418+ }
419+
420+ if (scheduledTime != null ) {
421+ proxyStatusService .containerScheduled (proxy , container , scheduledTime );
422+ }
423+ }
424+
403425 private JsonPatch readPatchFromSpec (ContainerSpec containerSpec , Proxy proxy ) throws JsonMappingException , JsonProcessingException {
404426 String patchAsString = proxy .getSpec ().getKubernetesPodPatch ();
405427 if (patchAsString == null ) {
0 commit comments