Refine search
@Override public void contribute(Builder builder) { try { Pod current = utils.currentPod().get(); Map<String, Object> details = new HashMap<>(); if (current != null) { details.put("inside", true); details.put("namespace", current.getMetadata().getNamespace()); details.put("podName", current.getMetadata().getName()); details.put("podIp", current.getStatus().getPodIP()); details.put("serviceAccount", current.getSpec().getServiceAccountName()); details.put("nodeName", current.getSpec().getNodeName()); details.put("hostIp", current.getStatus().getHostIP()); } else { details.put("inside", false); } builder.withDetail("kubernetes", details); } catch (Exception e) { LOG.warn("Failed to get pod details", e); } } }
@Override public void eventReceived(Action action, Pod aPod) { log(aPod.getMetadata().getName(), aPod.getStatus().getPhase()); if(aPod.getStatus().getPhase().equals("Succeeded")) { log("Logs -> ", client.pods().inNamespace(namespace).withName(aPod.getMetadata().getName()).getLog()); watchLatch.countDown(); } }
public boolean isTerminated(ContainerCreationStatus container) { Pod pod = client.pods().withName(container.getContainerName()).get(); if (pod == null) { logger.info("Container {} has no pod - terminal.", container); return true; } else { List<ContainerStatus> containerStatuses = pod.getStatus().getContainerStatuses(); Optional<ContainerStateTerminated> terminated = containerStatuses.stream() .flatMap(status -> Optional.ofNullable(status.getState()).map(Stream::of).orElse(Stream.empty())) .flatMap(state -> Optional.ofNullable(state.getTerminated()).map(Stream::of).orElse(Stream.empty())) .findFirst(); terminated.ifPresent(state -> logger.info("Container {} is {} - terminal.", container, state)); return terminated.isPresent(); } }
public Pod(io.fabric8.kubernetes.api.model.Pod pod) { this.name = pod.getMetadata().getName(); if (pod.getMetadata().getAnnotations() != null) { this.annotations.putAll(pod.getMetadata().getAnnotations()); } this.kind = pod.getKind(); this.host = pod.getStatus().getPodIP(); this.phase = pod.getStatus().getPhase(); this.ready = getReadyCondition(pod.getStatus().getConditions()); this.portMap = getPortMap(pod.getSpec().getContainers()); }
ObjectNode n = mapper.createObjectNode(); ObjectMeta meta = pod.getMetadata(); n.put("uid", meta.getUid()); n.put("resourceVersion", meta.getResourceVersion()); n.put("selfLink", meta.getSelfLink()); PodSpec spec = pod.getSpec(); n.put("terminationGracePeriodSeconds", spec.getTerminationGracePeriodSeconds()); PodStatus ps = pod.getStatus(); n.put("hostIp", ps.getHostIP()); n.put("message", ps.getMessage()); n.put("phase", ps.getPhase()); n.put("podIp", ps.getPodIP()); n.put("qosClass", ps.getQosClass()); n.put("reason", ps.getReason()); n.put("startTime", ps.getStartTime()); "uid", meta.getUid(), "props", n); ps.getContainerStatuses().forEach(it -> { ObjectNode cs = mapper.createObjectNode(); cs.put("podUid", meta.getUid());
public PodStatusFluentImpl(PodStatus instance){ this.withConditions(instance.getConditions()); this.withContainerStatuses(instance.getContainerStatuses()); this.withHostIP(instance.getHostIP()); this.withMessage(instance.getMessage()); this.withPhase(instance.getPhase()); this.withPodIP(instance.getPodIP()); this.withReason(instance.getReason()); this.withStartTime(instance.getStartTime()); }
/** * Get list of all ready pods * * @param kubernetes client for manipulation with kubernetes cluster * @return */ public static List<Pod> listReadyPods(Kubernetes kubernetes) { return kubernetes.listPods().stream() .filter(pod -> pod.getStatus().getContainerStatuses().stream().allMatch(ContainerStatus::getReady) && !pod.getMetadata().getName().startsWith(SystemtestsOpenshiftApp.MESSAGING_CLIENTS.toString())) .collect(Collectors.toList()); }
private Node getNodeFromPod(Pod pod) { Map<String,String> labels = pod.getMetadata().getLabels(); PodStatus status = pod.getStatus(); Node node = new Node(labels.remove(Protocol.HOST_NAME),status.getPodIP()); node.getProperties().putAll(labels); return node; }
result.put("pod.name", pod.getMetadata().getName()); result.put("pod.startTime", pod.getStatus().getStartTime()); result.put("pod.ip", pod.getStatus().getPodIP()); result.put("host.ip", pod.getStatus().getHostIP()); result.put("phase", pod.getStatus().getPhase()); result.put(AbstractKubernetesDeployer.SPRING_APP_KEY.replace('-', '.'), pod.getMetadata().getLabels().get(AbstractKubernetesDeployer.SPRING_APP_KEY));
@Override public Collection<Instance> getInstanceList() throws Exception { LOGGER.info("Starting Kubernetes instance discovery using master URL: {}", client.getMasterUrl()); return client.pods().inAnyNamespace() .list() .getItems().stream() .filter(pod -> pod.getMetadata().getAnnotations() != null) // Ignore pods without annotations .filter(pod -> pod.getMetadata().getAnnotations().containsKey(PORT_ANNOTATION_KEY)) .map(pod -> { String portString = pod.getMetadata().getAnnotations().get(PORT_ANNOTATION_KEY); if (!Pattern.compile("^[0-9]{2,5}$").matcher(portString).matches()) { LOGGER.warn("Invalid port annotation for pod '{}': {}", pod.getMetadata().getName(), portString); return null; } else { String host = String.format("%s:%s", pod.getStatus().getPodIP(), portString); boolean running = pod.getStatus().getPhase().equals("Running"); LOGGER.info("Found Kubernetes Pod {} at address {}", pod.getMetadata().getName(), host); return new Instance(host, extractClusterNameFor(pod), running); } }) .filter(Objects::nonNull) .collect(Collectors.toList()); }
private void collectLogs(Pod pod) { if (logWatches.containsKey(pod.getMetadata().getName())) { return; } while (!"Running".equals(pod.getStatus().getPhase())) { try { Thread.sleep(1000); } catch (InterruptedException e) { } pod = kubernetes.getPod(pod.getMetadata().getName()); } log.info("Collecting logs for pod {} with uuid {}", pod.getMetadata().getName(), uuid); for (Container container : pod.getSpec().getContainers()) { try { File outputFile = new File(logDir, pod.getMetadata().getName() + "." + container.getName()); FileOutputStream outputFileStream = new FileOutputStream(outputFile); synchronized (logWatches) { logWatches.put(pod.getMetadata().getName(), kubernetes.watchPodLog(pod.getMetadata().getName(), container.getName(), outputFileStream)); } } catch (Exception e) { log.info("Unable to save log for " + pod.getMetadata().getName() + "." + container.getName()); } } }
@Override public List<InetSocketAddress> findSeedNodes(String cluster) throws HekateException { if (log.isDebugEnabled()) { log.debug("Searching for seed node addresses [namespace={}, container-port-name={}]", config.getNamespace(), containerPortName); } try (KubernetesClient client = new DefaultKubernetesClient(config)) { List<InetSocketAddress> seedNodes = new ArrayList<>(); PodList pods = client.pods().list(); // Search through Pods -> Containers -> Ports. nullSafe(pods.getItems()) .filter(pod -> pod.getSpec() != null && pod.getStatus() != null && pod.getStatus().getPodIP() != null) .filter(pod -> ACTIVE_POD_PHASES.contains(pod.getStatus().getPhase())) .forEach(pod -> { String ip = pod.getStatus().getPodIP(); nullSafe(pod.getSpec().getContainers()) .flatMap(container -> nullSafe(container.getPorts())) .filter(port -> containerPortName.equals(port.getName()) && port.getContainerPort() != null) .map(ContainerPort::getContainerPort) .forEach(port -> seedNodes.add(new InetSocketAddress(ip, port)) ); }); return seedNodes; } catch (KubernetesClientException e) { throw new HekateException("Kubernetes seed node discovery failure [" + "namespace=" + config.getNamespace() + ", " + "container-port-name=" + containerPortName + "]", e); } }
Pod pod = getPodTemplate(client, slave, unwrappedTemplate); String podId = pod.getMetadata().getName(); String namespace = StringUtils.defaultIfBlank(slave.getNamespace(), client.getNamespace()); containerStatuses = pod.getStatus().getContainerStatuses(); List<ContainerStatus> terminatedContainers = new ArrayList<>(); Boolean allContainersAreReady = true; if (validStates.contains(pod.getStatus().getPhase())) { break; String status = pod.getStatus().getPhase(); if (!validStates.contains(status)) { throw new IllegalStateException("Container is not running after " + j + " attempts, status: " + status);
private PodStatus getPodStatus(String podName) { io.fabric8.kubernetes.api.model.PodStatus kubernetesPodStatus = kubernetesClient .pods() .inNamespace(environment.getKubernetesNamespace()) .withName(podName) .get() .getStatus(); PodStatus podStatus = new PodStatus(); podStatus.setName(podName); podStatus.setHostIp(kubernetesPodStatus.getHostIP()); podStatus.setPodIp(kubernetesPodStatus.getPodIP()); podStatus.setPhase(kubernetesPodStatus.getPhase()); podStatus.setReason(kubernetesPodStatus.getReason()); podStatus.setStartTime(kubernetesPodStatus.getStartTime()); podStatus.setHasJolokia(getPodJolokiaPort(podName).isPresent()); podStatus.setContainers(getPodContainerNames(podName)); return podStatus; }
public boolean isReady(ContainerCreationStatus container) { Pod pod = client.pods().withName(container.getContainerName()).get(); if (pod == null) { return false; } else { return pod.getStatus().getConditions().stream() .filter(condition -> condition.getType().equals("Ready")) .map(condition -> condition.getStatus().equals("True")) .findFirst() .orElse(false); } }
public PodStatusBuilder( PodStatusFluent<?> fluent , PodStatus instance ){ this.fluent = fluent; fluent.withCondition(instance.getCondition()); fluent.withContainerStatuses(instance.getContainerStatuses()); fluent.withHostIP(instance.getHostIP()); fluent.withMessage(instance.getMessage()); fluent.withPhase(instance.getPhase()); fluent.withPodIP(instance.getPodIP()); fluent.withStartTime(instance.getStartTime()); } public PodStatusBuilder( PodStatus instance ){
public Optional<String> getServiceLatestCreatedPodName(Service service, Optional<String> groupName) { PodList podList = kubernetesClient .pods() .inNamespace(environment.getKubernetesNamespace()) .withLabel(ApolloToKubernetes.getApolloDeploymentUniqueIdentifierKey(), ApolloToKubernetes.getApolloPodUniqueIdentifier(environment, service, groupName)) .list(); if (podList == null) { return Optional.empty(); } Optional<Pod> newestPod = podList.getItems() .stream() .sorted((o1, o2) -> { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); LocalDateTime pod1 = LocalDateTime.parse(o1.getStatus().getStartTime(), formatter); LocalDateTime pod2 = LocalDateTime.parse(o2.getStatus().getStartTime(), formatter); return pod1.compareTo(pod2); }).findFirst(); return newestPod.map(pod -> pod .getMetadata() .getName()); }
PodStatus podStatus = pod.getStatus(); int restartCount = 0; if( "Succeeded".equals(podStatus.getPhase()) ) { List<ContainerStatus> containerStatuses = podStatus.getContainerStatuses(); for (ContainerStatus containerStatus : containerStatuses) { if (restartCount == 0) { List<Container> containers = pod.getSpec().getContainers(); for (Container container : containers) { File logFile = LogHelpers.getLogFileName(session.getBaseDir(), name, container, restartCount);