io.fabric8.kubernetes.api.model.PodCondition Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.PodCondition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KubernetesClientUtil.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
protected static String getPodCondition(Pod pod) {
    PodStatus podStatus = pod.getStatus();
    if (podStatus == null) {
        return "";
    }
    List<PodCondition> conditions = podStatus.getConditions();
    if (conditions == null || conditions.isEmpty()) {
        return "";
    }


    for (PodCondition condition : conditions) {
        String type = condition.getType();
        if (StringUtils.isNotBlank(type)) {
            if ("ready".equalsIgnoreCase(type)) {
                String statusText = condition.getStatus();
                if (StringUtils.isNotBlank(statusText)) {
                    if (Boolean.parseBoolean(statusText)) {
                        return type;
                    }
                }
            }
        }
    }
    return "";
}
 
Example #2
Source File: KubernetesHelper.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns true if the pod is running and ready
 *
 * @param pod Pod object
 * @return boolean value indicating it's status
 */
public static boolean isPodReady(Pod pod) {
    if (!isPodRunning(pod)) {
        return false;
    }

    PodStatus podStatus = pod.getStatus();
    if (podStatus == null) {
        return true;
    }

    List<PodCondition> conditions = podStatus.getConditions();
    if (conditions == null || conditions.isEmpty()) {
        return true;
    }

    // Check "ready" condition
    for (PodCondition condition : conditions) {
        if ("ready".equalsIgnoreCase(condition.getType())) {
            return Boolean.parseBoolean(condition.getStatus());
        }
    }

    return true;
}
 
Example #3
Source File: KubernetesPodDetails.java    From kubernetes-elastic-agents with Apache License 2.0 6 votes vote down vote up
public static KubernetesPodDetails fromPod(Pod pod) {
    KubernetesPodDetails podDetails = new KubernetesPodDetails();

    podDetails.name = pod.getMetadata().getName();
    podDetails.clusterName = pod.getMetadata().getClusterName();
    podDetails.nodeName = pod.getSpec().getNodeName();
    podDetails.namespace = pod.getMetadata().getNamespace();

    podDetails.createdAt = pod.getMetadata().getCreationTimestamp();
    podDetails.startedAt = pod.getStatus().getStartTime();

    podDetails.phase = pod.getStatus().getPhase();

    podDetails.podIP = pod.getStatus().getPodIP();
    podDetails.hostIP = pod.getStatus().getHostIP();

    podDetails.conditions = new ArrayList<>();
    for (PodCondition podCondition : pod.getStatus().getConditions()) {
        Condition condition = new Condition(podCondition.getType(),
                podCondition.getStatus());
        podDetails.conditions.add(condition);
    }

    return podDetails;
}
 
Example #4
Source File: KubeCloudInstanceImpl.java    From teamcity-kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public Date getStartedTime() {
    final PodStatus podStatus = myPod.getStatus();
    if(podStatus == null) return myCreationTime;
    try {
        final List<PodCondition> podConditions = podStatus.getConditions();
        if (podConditions != null && !podConditions.isEmpty()) {
            for (PodCondition podCondition : podConditions) {
                if (PodConditionType.valueOf(podCondition.getType()) == PodConditionType.Ready)
                    return myPodTransitionTimeFormat.parse(podCondition.getLastTransitionTime());
            }
        }
        String startTime = podStatus.getStartTime();
        return !StringUtil.isEmpty(startTime) ? myPodStartTimeFormat.parse(startTime) : myCreationTime;
    } catch (ParseException e) {
        throw new KubeCloudException("Failed to get instance start date", e);
    }
}
 
Example #5
Source File: KubernetesDiscoveryAgent.java    From activemq-k8s-discovery with Apache License 2.0 6 votes vote down vote up
private boolean isConditionOk(PodCondition c) {
    switch ( c.getType()) {
        case "OutOfDisk":
        case "NetworkUnavailable":
            return !c.getStatus().equals("True");
        case "MemoryPressure":
        case "DiskPressure":
        case "PodScheduled":
        case "Initialized":
            return true;
        case "Ready":
            return c.getStatus().equals("True");
        default:
            LOG.warn("Do not know what to make of status "+c.getType()+", just returning true.");
            return  true;
    }
}
 
Example #6
Source File: KubernetesDiscoveryAgent.java    From activemq-k8s-discovery with Apache License 2.0 5 votes vote down vote up
private boolean allConditionsOk(List<PodCondition> conditions) {
    Optional<PodCondition> notRunning = conditions
            .stream()
            .filter( c -> !isConditionOk( c))
            .findFirst();
    return ! notRunning.isPresent();
}
 
Example #7
Source File: ResourceManager.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Log actual status of custom resource with pods.
 * @param customResource - Kafka, KafkaConnect etc. - every resource that HasMetadata and HasStatus (Strimzi status)
 */
public static <T extends HasMetadata & HasStatus> void logCurrentResourceStatus(T customResource) {
    List<String> printWholeCR = Arrays.asList(KafkaConnector.RESOURCE_KIND, KafkaTopic.RESOURCE_KIND, KafkaUser.RESOURCE_KIND);

    String kind = customResource.getKind();
    String name = customResource.getMetadata().getName();

    if (printWholeCR.contains(kind)) {
        LOGGER.info(customResource);
    } else {
        List<String> log = new ArrayList<>(asList("\n", kind, " status:\n", "\nConditions:\n"));

        for (Condition condition : customResource.getStatus().getConditions()) {
            log.add("\tType: " + condition.getType() + "\n");
            log.add("\tMessage: " + condition.getMessage() + "\n");
        }

        log.add("\nPods with conditions and messages:\n\n");

        for (Pod pod : kubeClient().listPodsByPrefixInName(name)) {
            log.add(pod.getMetadata().getName() + ":");
            for (PodCondition podCondition : pod.getStatus().getConditions()) {
                if (podCondition.getMessage() != null) {
                    log.add("\n\tType: " + podCondition.getType() + "\n");
                    log.add("\tMessage: " + podCondition.getMessage() + "\n");
                }
            }
            log.add("\n\n");
        }
        LOGGER.info("{}", String.join("", log));
    }
}
 
Example #8
Source File: DeploymentUtils.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Log actual status of deployment with pods
 * @param deployment - every DoneableDeployment, that HasMetadata and has status (fabric8 status)
 **/
public static void logCurrentDeploymentStatus(Deployment deployment) {
    if (deployment != null) {
        String kind = deployment.getKind();
        String name = deployment.getMetadata().getName();

        List<String> log = new ArrayList<>(asList("\n", kind, " status:\n", "\nConditions:\n"));

        for (DeploymentCondition deploymentCondition : deployment.getStatus().getConditions()) {
            log.add("\tType: " + deploymentCondition.getType() + "\n");
            log.add("\tMessage: " + deploymentCondition.getMessage() + "\n");
        }

        if (kubeClient().listPodsByPrefixInName(name).size() != 0) {
            log.add("\nPods with conditions and messages:\n\n");

            for (Pod pod : kubeClient().listPodsByPrefixInName(name)) {
                log.add(pod.getMetadata().getName() + ":");
                for (PodCondition podCondition : pod.getStatus().getConditions()) {
                    if (podCondition.getMessage() != null) {
                        log.add("\n\tType: " + podCondition.getType() + "\n");
                        log.add("\tMessage: " + podCondition.getMessage() + "\n");
                    }
                }
                log.add("\n\n");
            }
            LOGGER.info("{}", String.join("", log));
        }

        LOGGER.info("{}", String.join("", log));
    }
}
 
Example #9
Source File: SystemtestsKubernetesApps.java    From enmasse with Apache License 2.0 5 votes vote down vote up
private static Predicate<Pod> conditionIsTrue(final String type) {
    return p -> Optional.ofNullable(p)
            .map(Pod::getStatus)
            .map(PodStatus::getConditions)
            .flatMap(o -> o.stream().filter(c -> type.equals(c.getType())).findFirst())
            .map(PodCondition::getStatus)
            .map("True"::equals)
            .orElse(false);
}
 
Example #10
Source File: RollingUpdater.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
/**
 * Lets wait until there are enough Ready pods of the given RC
 */
private void waitUntilPodsAreReady(final T obj, final String namespace, final int requiredPodCount) {
  final CountDownLatch countDownLatch = new CountDownLatch(1);
  final AtomicInteger podCount = new AtomicInteger(0);

  final Runnable readyPodsPoller = () -> {
    PodList podList = listSelectedPods(obj);
    int count = 0;
    List<Pod> items = podList.getItems();
    for (Pod item : items) {
      for (PodCondition c : item.getStatus().getConditions()) {
        if (c.getType().equals("Ready") && c.getStatus().equals("True")) {
          count++;
        }
      }
    }
    podCount.set(count);
    if (count == requiredPodCount) {
      countDownLatch.countDown();
    }
  };

  ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
  ScheduledFuture poller = executor.scheduleWithFixedDelay(readyPodsPoller, 0, 1, TimeUnit.SECONDS);
  ScheduledFuture logger = executor.scheduleWithFixedDelay(() -> LOG.debug("Only {}/{} pod(s) ready for {}: {} in namespace: {} seconds so waiting...",
      podCount.get(), requiredPodCount, obj.getKind(), obj.getMetadata().getName(), namespace), 0, loggingIntervalMillis, TimeUnit.MILLISECONDS);
  try {
    countDownLatch.await(rollingTimeoutMillis, TimeUnit.MILLISECONDS);
    executor.shutdown();
  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    poller.cancel(true);
    logger.cancel(true);
    executor.shutdown();
    LOG.warn("Only {}/{} pod(s) ready for {}: {} in namespace: {}  after waiting for {} seconds so giving up",
        podCount.get(), requiredPodCount, obj.getKind(), obj.getMetadata().getName(), namespace, TimeUnit.MILLISECONDS.toSeconds(rollingTimeoutMillis));
  }
}
 
Example #11
Source File: Readiness.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
public static boolean isPodReady(Pod pod) {
  Utils.checkNotNull(pod, "Pod can't be null.");
  PodCondition condition = getPodReadyCondition(pod);

  //Can be true in testing, so handle it to make test writing easier.
  if (condition == null  || condition.getStatus() == null) {
    return false;
  }
  return condition.getStatus().equalsIgnoreCase(TRUE);
}
 
Example #12
Source File: Readiness.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the ready condition of the pod.
 * @param pod   The target pod.
 * @return      The {@link PodCondition} or null if not found.
 */
private static PodCondition getPodReadyCondition(Pod pod) {
  Utils.checkNotNull(pod, "Pod can't be null.");

  if (pod.getStatus() == null || pod.getStatus().getConditions() == null) {
    return null;
  }

  for (PodCondition condition : pod.getStatus().getConditions()) {
    if (POD_READY.equals(condition.getType())) {
      return condition;
    }
  }
  return null;
}