SystemInfo findContainerDetails()

in apm-agent-core/src/main/java/co/elastic/apm/agent/impl/metadata/SystemInfo.java [272:301]


    SystemInfo findContainerDetails() {
        parseCgroupsFile(FileSystems.getDefault().getPath(SELF_CGROUP));
        if (container == null) {
            parseMountInfo(FileSystems.getDefault().getPath(SELF_MOUNTINFO));
        }

        try {
            // Kubernetes Downward API enables setting environment variables. We are looking for the relevant ones to this discovery
            String podUid = System.getenv("KUBERNETES_POD_UID");
            String podName = System.getenv("KUBERNETES_POD_NAME");
            String nodeName = System.getenv("KUBERNETES_NODE_NAME");
            String namespace = System.getenv("KUBERNETES_NAMESPACE");
            if (podUid != null || podName != null || nodeName != null || namespace != null) {
                // avoid overriding valid info with invalid info
                if (kubernetes != null) {
                    if (kubernetes.getPod() != null) {
                        podUid = (podUid != null) ? podUid : kubernetes.getPod().getUid();
                        podName = (podName != null) ? podName : kubernetes.getPod().getName();
                    }
                }

                kubernetes = new Kubernetes(podName, nodeName, namespace, podUid);
            }
        } catch (Throwable e) {
            logger.warn("Failed to read environment variables for Kubernetes Downward API discovery", e);
        }

        logger.debug("container ID is {}", container != null ? container.getId() : null);
        return this;
    }