3535import java .net .InetAddress ;
3636import java .net .URL ;
3737import java .net .UnknownHostException ;
38+ import java .nio .file .Path ;
3839import java .util .List ;
3940import java .util .Map ;
4041import java .util .concurrent .locks .Lock ;
@@ -156,6 +157,8 @@ public class KubernetesLookup extends AbstractLookup {
156157 private static final String POD_NAME = "podName" ;
157158
158159 private static KubernetesInfo kubernetesInfo ;
160+ // Used in tests
161+ static Path cgroupPath = ContainerUtil .CGROUP_PATH ;
159162 private static final ReadWriteLock LOCK = new ReentrantReadWriteLock ();
160163 private static final Lock READ_LOCK = LOCK .readLock ();
161164 private static final Lock WRITE_LOCK = LOCK .writeLock ();
@@ -191,7 +194,9 @@ private static void initialize(KubernetesLookup lookup) {
191194 try {
192195 kubernetesInfo = KubernetesLookup .kubernetesInfo ;
193196 if (kubernetesInfo == null || isSpringStatusChanged (kubernetesInfo )) {
194- tryInitializeFields (lookup );
197+ if (lookup .pod == null || lookup .namespace == null || lookup .masterUrl == null ) {
198+ tryInitializeFields (lookup );
199+ }
195200 // Retrieve the data from the fields
196201 kubernetesInfo = new KubernetesInfo ();
197202 kubernetesInfo .isSpringActive = isSpringActive ();
@@ -260,8 +265,8 @@ private static Pod getCurrentPod(final KubernetesClient kubernetesClient) {
260265 if (hostName != null && !hostName .isEmpty ()) {
261266 return kubernetesClient .pods ().withName (hostName ).get ();
262267 }
263- } catch (Throwable t ) {
264- LOGGER .debug ("Unable to locate pod with name {}." , hostName );
268+ } catch (Exception e ) {
269+ LOGGER .debug ("Unable to locate pod with name {}." , hostName , e );
265270 }
266271 return null ;
267272 }
@@ -340,7 +345,7 @@ private static ContainerStatus getContainerStatus(PodStatus podStatus) {
340345 case 1 :
341346 return statuses .get (0 );
342347 default :
343- final String containerId = ContainerUtil .getContainerId (ContainerUtil . CGROUP_FILE );
348+ final String containerId = ContainerUtil .getContainerId (cgroupPath );
344349 return containerId != null ? statuses .stream ()
345350 .filter (cs -> cs .getContainerID ().contains (containerId ))
346351 .findFirst ()
@@ -365,70 +370,67 @@ private static Container getContainer(PodSpec podSpec, String containerName) {
365370
366371 @ Override
367372 public String lookup (final LogEvent event , final String key ) {
368- KubernetesInfo kubernetesInfo = null ;
373+ KubernetesInfo info ;
369374 READ_LOCK .lock ();
370375 try {
371- kubernetesInfo = KubernetesLookup . kubernetesInfo ;
376+ info = kubernetesInfo ;
372377 } finally {
373378 READ_LOCK .unlock ();
374379 }
375- if (kubernetesInfo == null ) {
376- return null ;
377- }
378380 if (key .startsWith (LABELS_PREFIX )) {
379- return kubernetesInfo .labels != null ? kubernetesInfo .labels .get (key .substring (LABELS_PREFIX .length ())) : null ;
381+ return info .labels != null ? info .labels .get (key .substring (LABELS_PREFIX .length ())) : null ;
380382 }
381383 switch (key ) {
382384 case ACCOUNT_NAME : {
383- return kubernetesInfo .accountName ;
385+ return info .accountName ;
384386 }
385387 case ANNOTATIONS : {
386- return kubernetesInfo .annotations != null ? kubernetesInfo .annotations .toString () : null ;
388+ return info .annotations != null ? info .annotations .toString () : null ;
387389 }
388390 case CONTAINER_ID : {
389- return kubernetesInfo .containerId ;
391+ return info .containerId ;
390392 }
391393 case CONTAINER_NAME : {
392- return kubernetesInfo .containerName ;
394+ return info .containerName ;
393395 }
394396 case HOST : {
395- return kubernetesInfo .hostName ;
397+ return info .hostName ;
396398 }
397399 case HOST_IP : {
398- return kubernetesInfo .hostIp ;
400+ return info .hostIp ;
399401 }
400402 case LABELS : {
401- return kubernetesInfo .labels != null ? kubernetesInfo .labels .toString () : null ;
403+ return info .labels != null ? info .labels .toString () : null ;
402404 }
403405 case MASTER_URL : {
404- return kubernetesInfo .masterUrl != null ? kubernetesInfo .masterUrl .toString () : null ;
406+ return info .masterUrl != null ? info .masterUrl .toString () : null ;
405407 }
406408 case NAMESPACE_ANNOTATIONS : {
407- return kubernetesInfo .namespaceAnnotations != null ? kubernetesInfo .namespaceAnnotations .toString () : null ;
409+ return info .namespaceAnnotations != null ? info .namespaceAnnotations .toString () : null ;
408410 }
409411 case NAMESPACE_ID : {
410- return kubernetesInfo .namespaceId ;
412+ return info .namespaceId ;
411413 }
412414 case NAMESPACE_LABELS : {
413- return kubernetesInfo .namespaceLabels != null ? kubernetesInfo .namespaceLabels .toString () : null ;
415+ return info .namespaceLabels != null ? info .namespaceLabels .toString () : null ;
414416 }
415417 case NAMESPACE_NAME : {
416- return kubernetesInfo .namespace ;
418+ return info .namespace ;
417419 }
418420 case POD_ID : {
419- return kubernetesInfo .podId ;
421+ return info .podId ;
420422 }
421423 case POD_IP : {
422- return kubernetesInfo .podIp ;
424+ return info .podIp ;
423425 }
424426 case POD_NAME : {
425- return kubernetesInfo .podName ;
427+ return info .podName ;
426428 }
427429 case IMAGE_ID : {
428- return kubernetesInfo .imageId ;
430+ return info .imageId ;
429431 }
430432 case IMAGE_NAME : {
431- return kubernetesInfo .imageName ;
433+ return info .imageName ;
432434 }
433435 default :
434436 return null ;
@@ -438,8 +440,9 @@ public String lookup(final LogEvent event, final String key) {
438440 /**
439441 * For unit testing only.
440442 */
441- static void clearInfo () {
443+ static void clear () {
442444 kubernetesInfo = null ;
445+ cgroupPath = ContainerUtil .CGROUP_PATH ;
443446 }
444447
445448 private static class KubernetesInfo {
0 commit comments