Search in sources :

Example 1 with ContainerStatus

use of io.fabric8.kubernetes.api.model.ContainerStatus in project fabric8 by jboss-fuse.

the class Provision method provisioningSuccess.

/**
 * Wait for a container to provision and assert its status.
 *
 * @param containers
 * @param timeout
 * @param onFailed
 * @throws Exception
 */
public static void provisioningSuccess(Collection<? extends Container> containers, Long timeout, Callback<Container> onFailed) throws ProvisionException, InterruptedException {
    if (containers.isEmpty()) {
        return;
    }
    StringBuilder sb = new StringBuilder();
    sb.append(" ");
    for (Container c : containers) {
        sb.append(c.getId()).append(" ");
    }
    System.out.println("Waiting for containers: [" + sb.toString() + "] to successfully provision");
    for (int i = 0; i < 3; i++) {
        if (!containerStatus(containers, timeout)) {
            StringBuilder error = new StringBuilder();
            for (Container container : containers) {
                if (!container.isAliveAndOK()) {
                    onFailed.call(container);
                    error.append("Container " + container.getId() + " failed to provision. Status:" + container.getProvisionStatus() + " Exception:" + container.getProvisionException()).append("\n");
                }
            }
            throw new ProvisionException(error.toString());
        }
        Thread.sleep(1000);
    }
}
Also used : Container(io.fabric8.api.Container)

Example 2 with ContainerStatus

use of io.fabric8.kubernetes.api.model.ContainerStatus in project fabric8 by fabric8io.

the class Example method listPods.

protected static void listPods(KubernetesClient kube) {
    System.out.println("\n\nLooking up pods");
    System.out.println("=========================================================================");
    PodList pods = kube.pods().list();
    // System.out.println("Got pods: " + pods);
    List<Pod> items = pods.getItems();
    for (Pod item : items) {
        System.out.println("Pod " + KubernetesHelper.getName(item) + " with ip: " + item.getStatus().getPodIP() + " created: " + item.getMetadata().getCreationTimestamp());
        PodSpec spec = item.getSpec();
        if (spec != null) {
            List<Container> containers = spec.getContainers();
            if (containers != null) {
                for (Container container : containers) {
                    System.out.println("Container " + container.getImage() + " " + container.getCommand() + " ports: " + container.getPorts());
                }
            }
        }
        Map<String, ContainerStatus> currentContainers = KubernetesHelper.getCurrentContainers(item);
        System.out.println("Has " + currentContainers.size() + " container(s)");
        Set<Map.Entry<String, ContainerStatus>> entries = currentContainers.entrySet();
        for (Map.Entry<String, ContainerStatus> entry : entries) {
            String id = entry.getKey();
            ContainerStatus info = entry.getValue();
            System.out.println("Current container: " + id + " info: " + info);
        }
    }
    System.out.println();
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Container(io.fabric8.kubernetes.api.model.Container) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Map(java.util.Map)

Example 3 with ContainerStatus

use of io.fabric8.kubernetes.api.model.ContainerStatus in project fabric8 by fabric8io.

the class LogHelpers method getRestartCount.

public static int getRestartCount(Pod pod) {
    int restartCount = 0;
    PodStatus podStatus = pod.getStatus();
    if (podStatus != null) {
        List<ContainerStatus> containerStatuses = podStatus.getContainerStatuses();
        for (ContainerStatus containerStatus : containerStatuses) {
            if (restartCount == 0) {
                Integer restartCountValue = containerStatus.getRestartCount();
                if (restartCountValue != null) {
                    restartCount = restartCountValue.intValue();
                }
            }
        }
    }
    return restartCount;
}
Also used : PodStatus(io.fabric8.kubernetes.api.model.PodStatus) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus)

Example 4 with ContainerStatus

use of io.fabric8.kubernetes.api.model.ContainerStatus in project ephemerals by LivePersonInc.

the class KubernetesDeploymentStatusWaiter method getDeploymentStatus.

@Override
public DeploymentStatus getDeploymentStatus() {
    PodList list = client.pods().withLabel(KubernetesDeploymentHandler.DEPLOYMENT_LABEL_KEY, deployment.getId()).list();
    Pod pod = list.getItems().get(0);
    ContainerStatus containerStatus = pod.getStatus().getContainerStatuses().get(0);
    switch(pod.getStatus().getPhase()) {
        case "Pending":
            return DeploymentStatus.IN_PROGRESS;
        case "Running":
            if (containerStatus.getReady()) {
                return DeploymentStatus.FINISHED;
            } else // Out of memory / CPU issues
            if (containerStatus.getRestartCount() > deployment.getDeploymentConfiguration().getMaxDeploymentRetries() && (containerStatus.getLastState().getTerminated().getExitCode() == 137 || containerStatus.getLastState().getTerminated().getExitCode() == 143)) {
                return DeploymentStatus.FAILED;
            } else // General error
            if (containerStatus.getRestartCount() > deployment.getDeploymentConfiguration().getMaxDeploymentRetries() && containerStatus.getLastState().getTerminated().getReason().contains("Error") && containerStatus.getState().getTerminated().getReason().contains("Error") && containerStatus.getLastState().getTerminated().getExitCode().equals(containerStatus.getState().getTerminated().getExitCode())) {
                return DeploymentStatus.FAILED;
            } else // CrashLoopBackOff errors
            if (containerStatus.getRestartCount() > deployment.getDeploymentConfiguration().getMaxDeploymentRetries() && containerStatus.getLastState().getTerminated() != null && containerStatus.getState().getWaiting().getReason().contains("CrashLoopBackOff")) {
                return DeploymentStatus.FAILED;
            } else // Partial deployment if it was terminated and not restarted
            if (containerStatus.getRestartCount() == 0 && containerStatus.getState().getTerminated() != null) {
                return DeploymentStatus.PARTIALLY;
            } else {
                return DeploymentStatus.IN_PROGRESS;
            }
        case "Failed":
            return DeploymentStatus.FAILED;
        case "Unknown":
            return DeploymentStatus.UNKNOWN;
        default:
            return DeploymentStatus.UNKNOWN;
    }
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Pod(io.fabric8.kubernetes.api.model.Pod)

Example 5 with ContainerStatus

use of io.fabric8.kubernetes.api.model.ContainerStatus in project logging-log4j2 by apache.

the class KubernetesLookup method initialize.

private boolean initialize() {
    if (kubernetesInfo == null || (isSpringIncluded && !kubernetesInfo.isSpringActive)) {
        initLock.lock();
        try {
            boolean isSpringActive = isSpringActive();
            if (kubernetesInfo == null || (!kubernetesInfo.isSpringActive && isSpringActive)) {
                KubernetesInfo info = new KubernetesInfo();
                KubernetesClient client = null;
                info.isSpringActive = isSpringActive;
                if (pod == null) {
                    client = new KubernetesClientBuilder().createClient();
                    if (client != null) {
                        pod = getCurrentPod(System.getenv(HOSTNAME), client);
                        info.masterUrl = client.getMasterUrl();
                        if (pod != null) {
                            info.namespace = pod.getMetadata().getNamespace();
                            namespace = client.namespaces().withName(info.namespace).get();
                        }
                    } else {
                        LOGGER.warn("Kubernetes is not available for access");
                    }
                } else {
                    info.masterUrl = masterUrl;
                }
                if (pod != null) {
                    if (namespace != null) {
                        info.namespaceId = namespace.getMetadata().getUid();
                        info.namespaceAnnotations = namespace.getMetadata().getAnnotations();
                        info.namespaceLabels = namespace.getMetadata().getLabels();
                    }
                    info.app = pod.getMetadata().getLabels().get("app");
                    info.hostName = pod.getSpec().getNodeName();
                    info.annotations = pod.getMetadata().getAnnotations();
                    final String app = info.app != null ? info.app : "";
                    info.podTemplateHash = pod.getMetadata().getLabels().get("pod-template-hash");
                    info.accountName = pod.getSpec().getServiceAccountName();
                    info.clusterName = pod.getMetadata().getClusterName();
                    info.hostIp = pod.getStatus().getHostIP();
                    info.labels = pod.getMetadata().getLabels();
                    info.podId = pod.getMetadata().getUid();
                    info.podIp = pod.getStatus().getPodIP();
                    info.podName = pod.getMetadata().getName();
                    ContainerStatus containerStatus = null;
                    List<ContainerStatus> statuses = pod.getStatus().getContainerStatuses();
                    if (statuses.size() == 1) {
                        containerStatus = statuses.get(0);
                    } else if (statuses.size() > 1) {
                        String containerId = ContainerUtil.getContainerId();
                        if (containerId != null) {
                            containerStatus = statuses.stream().filter(cs -> cs.getContainerID().contains(containerId)).findFirst().orElse(null);
                        }
                    }
                    final String containerName;
                    if (containerStatus != null) {
                        info.containerId = containerStatus.getContainerID();
                        info.imageId = containerStatus.getImageID();
                        containerName = containerStatus.getName();
                    } else {
                        containerName = null;
                    }
                    Container container = null;
                    List<Container> containers = pod.getSpec().getContainers();
                    if (containers.size() == 1) {
                        container = containers.get(0);
                    } else if (containers.size() > 1 && containerName != null) {
                        container = containers.stream().filter(c -> c.getName().equals(containerName)).findFirst().orElse(null);
                    }
                    if (container != null) {
                        info.containerName = container.getName();
                        info.imageName = container.getImage();
                    }
                    kubernetesInfo = info;
                }
            }
        } finally {
            initLock.unlock();
        }
    }
    return kubernetesInfo != null;
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Container(io.fabric8.kubernetes.api.model.Container)

Aggregations

ContainerStatus (io.fabric8.kubernetes.api.model.ContainerStatus)8 Pod (io.fabric8.kubernetes.api.model.Pod)6 Container (io.fabric8.kubernetes.api.model.Container)4 Container (io.fabric8.api.Container)3 PodList (io.fabric8.kubernetes.api.model.PodList)3 PodStatus (io.fabric8.kubernetes.api.model.PodStatus)3 List (java.util.List)3 Map (java.util.Map)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Collectors (java.util.stream.Collectors)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonObject (com.google.gson.JsonObject)1 Names (com.netflix.frigga.Names)1 KubernetesUtil (com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.KubernetesUtil)1 KubernetesLoadBalancerDescription (com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.loadbalancer.KubernetesLoadBalancerDescription)1 KubernetesNamedServicePort (com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.loadbalancer.KubernetesNamedServicePort)1