Search in sources :

Example 6 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)

Example 7 with ContainerStatus

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

the class MQManager method addMasterSlaveStatus.

protected void addMasterSlaveStatus(List<MQBrokerStatusDTO> answer) throws Exception {
    Map<String, Map<String, MQBrokerStatusDTO>> groupMap = new HashMap<String, Map<String, MQBrokerStatusDTO>>();
    for (MQBrokerStatusDTO status : answer) {
        String key = status.getGroup();
        Map<String, MQBrokerStatusDTO> list = groupMap.get(key);
        if (list == null) {
            list = new HashMap<String, MQBrokerStatusDTO>();
            groupMap.put(key, list);
        }
        String statusPath = String.format("%s/%s", status.getContainer(), status.getBrokerName());
        list.put(statusPath, status);
    }
    CuratorFramework curator = getCurator();
    // now lets check the cluster status for each group
    Set<Map.Entry<String, Map<String, MQBrokerStatusDTO>>> entries = groupMap.entrySet();
    for (Map.Entry<String, Map<String, MQBrokerStatusDTO>> entry : entries) {
        String group = entry.getKey();
        Map<String, MQBrokerStatusDTO> containerMap = entry.getValue();
        String groupPath = ZkPath.MQ_CLUSTER.getPath(group);
        List<String> children = getChildrenSafe(curator, groupPath);
        for (String child : children) {
            String childPath = groupPath + "/" + child;
            byte[] data = curator.getData().forPath(childPath);
            if (data != null && data.length > 0) {
                String text = new String(data).trim();
                if (!text.isEmpty()) {
                    ObjectMapper mapper = new ObjectMapper();
                    Map<String, Object> map = mapper.readValue(data, HashMap.class);
                    String id = stringValue(map, "id", "container");
                    if (id != null) {
                        String container = stringValue(map, "container", "agent");
                        String statusPath = String.format("%s/%s", container, id);
                        MQBrokerStatusDTO containerStatus = containerMap.get(statusPath);
                        if (containerStatus != null) {
                            Boolean master = null;
                            List services = listValue(map, "services");
                            if (services != null) {
                                if (!services.isEmpty()) {
                                    List<String> serviceTexts = new ArrayList<String>();
                                    for (Object service : services) {
                                        String serviceText = getSubstitutedData(curator, service.toString());
                                        if (Strings.isNotBlank(serviceText)) {
                                            serviceTexts.add(serviceText);
                                        }
                                        containerStatus.setServices(serviceTexts);
                                    }
                                    master = Boolean.TRUE;
                                } else {
                                    master = Boolean.FALSE;
                                }
                            } else {
                                master = Boolean.FALSE;
                            }
                            containerStatus.setMaster(master);
                        }
                    }
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MQBrokerStatusDTO(io.fabric8.api.jmx.MQBrokerStatusDTO) CuratorFramework(org.apache.curator.framework.CuratorFramework) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 8 with ContainerStatus

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

the class ProvisionSupport method provisioningSuccess.

/**
 * Wait for a container to provision and assert its status.
 */
public static void provisioningSuccess(Collection<Container> containers, Long timeout) throws Exception {
    Set<Container> cntset = new HashSet<>(containers);
    long startedAt = System.currentTimeMillis();
    long remaining = timeout;
    while (!cntset.isEmpty() && remaining > 0 && !Thread.interrupted()) {
        containerStatus(containers, "success", remaining);
        remaining = startedAt + timeout - System.currentTimeMillis();
        for (Container container : containers) {
            if (!container.isAliveAndOK()) {
                if (container.getProvisionException() != null) {
                    throw new Exception(container.getProvisionException());
                } else if (remaining < 0) {
                    throw new Exception("Container " + container.getId() + " failed to provision. Status:" + container.getProvisionStatus() + " Exception:" + container.getProvisionException());
                }
            } else {
                cntset.remove(container);
            }
        }
        Thread.sleep(1000);
        remaining = startedAt + timeout - System.currentTimeMillis();
    }
}
Also used : Container(io.fabric8.api.Container) HashSet(java.util.HashSet)

Example 9 with ContainerStatus

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

the class ProvisionSupport method containerStatus.

/**
 * Waits for all container to provision and reach the specified status.
 */
public static void containerStatus(Collection<Container> containers, String status, Long timeout) throws Exception {
    CompletionService<Boolean> completionService = new ExecutorCompletionService<Boolean>(EXECUTOR);
    List<Future<Boolean>> waitForProvisionTasks = new LinkedList<Future<Boolean>>();
    StringBuilder sb = new StringBuilder();
    sb.append(" ");
    for (Container c : containers) {
        waitForProvisionTasks.add(completionService.submit(new WaitForProvisionTask(c, status, timeout)));
        sb.append(c.getId()).append(" ");
    }
    System.out.println("Waiting for containers: [" + sb.toString() + "] to reach status: " + status);
    for (int i = 0; i < containers.size(); i++) {
        completionService.poll(timeout, TimeUnit.MILLISECONDS);
    }
}
Also used : Container(io.fabric8.api.Container) Future(java.util.concurrent.Future) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) LinkedList(java.util.LinkedList)

Example 10 with ContainerStatus

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

the class KubernetesResourceUtil method getDockerContainerID.

public static String getDockerContainerID(Pod pod) {
    PodStatus status = pod.getStatus();
    if (status != null) {
        List<ContainerStatus> containerStatuses = status.getContainerStatuses();
        if (containerStatuses != null) {
            for (ContainerStatus containerStatus : containerStatuses) {
                String containerID = containerStatus.getContainerID();
                if (Strings.isNotBlank(containerID)) {
                    String prefix = "://";
                    int idx = containerID.indexOf(prefix);
                    if (idx > 0) {
                        return containerID.substring(idx + prefix.length());
                    }
                    return containerID;
                }
            }
        }
    }
    return null;
}
Also used : PodStatus(io.fabric8.kubernetes.api.model.PodStatus) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus)

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