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;
}
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);
}
}
}
}
}
}
}
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();
}
}
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);
}
}
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;
}
Aggregations