use of io.fabric8.kubernetes.api.model.ContainerStatus in project halyard by spinnaker.
the class KubernetesV1DistributedService method getRunningServiceDetails.
@Override
default RunningServiceDetails getRunningServiceDetails(AccountDeploymentDetails<KubernetesAccount> details, SpinnakerRuntimeSettings runtimeSettings) {
ServiceSettings settings = runtimeSettings.getServiceSettings(getService());
RunningServiceDetails res = new RunningServiceDetails();
KubernetesClient client = KubernetesV1ProviderUtils.getClient(details);
String name = getServiceName();
String namespace = getNamespace(settings);
RunningServiceDetails.LoadBalancer lb = new RunningServiceDetails.LoadBalancer();
lb.setExists(client.services().inNamespace(namespace).withName(name).get() != null);
res.setLoadBalancer(lb);
List<Pod> pods = client.pods().inNamespace(namespace).withLabel("load-balancer-" + name, "true").list().getItems();
pods.addAll(client.pods().inNamespace(namespace).withLabel("load-balancer-" + name, "false").list().getItems());
Map<Integer, List<Instance>> instances = res.getInstances();
for (Pod pod : pods) {
String podName = pod.getMetadata().getName();
String serverGroupName = podName.substring(0, podName.lastIndexOf("-"));
Names parsedName = Names.parseName(serverGroupName);
Integer version = parsedName.getSequence();
if (version == null) {
throw new IllegalStateException("Server group for service " + getServiceName() + " has unknown sequence (" + serverGroupName + ")");
}
String location = pod.getMetadata().getNamespace();
String id = pod.getMetadata().getName();
Instance instance = new Instance().setId(id).setLocation(location);
List<ContainerStatus> containerStatuses = pod.getStatus().getContainerStatuses();
if (!containerStatuses.isEmpty() && containerStatuses.stream().allMatch(ContainerStatus::getReady)) {
instance.setHealthy(true);
}
if (!containerStatuses.isEmpty() && containerStatuses.stream().allMatch(s -> s.getState().getRunning() != null && s.getState().getTerminated() == null)) {
instance.setRunning(true);
}
List<Instance> knownInstances = instances.getOrDefault(version, new ArrayList<>());
knownInstances.add(instance);
instances.put(version, knownInstances);
}
List<ReplicaSet> replicaSets = client.extensions().replicaSets().inNamespace(settings.getLocation()).list().getItems();
for (ReplicaSet rs : replicaSets) {
String rsName = rs.getMetadata().getName();
Names parsedRsName = Names.parseName(rsName);
if (!parsedRsName.getCluster().equals(getServiceName())) {
continue;
}
instances.computeIfAbsent(parsedRsName.getSequence(), i -> new ArrayList<>());
}
return res;
}
use of io.fabric8.kubernetes.api.model.ContainerStatus in project flink by apache.
the class KubernetesPod method getTerminatedDiagnostics.
public String getTerminatedDiagnostics() {
final StringBuilder sb = new StringBuilder();
sb.append("Pod terminated, container termination statuses: [");
if (getInternalResource().getStatus() != null) {
sb.append(getInternalResource().getStatus().getContainerStatuses().stream().filter(containerStatus -> containerStatus.getState() != null && containerStatus.getState().getTerminated() != null).map((containerStatus) -> {
final ContainerStateTerminated containerStateTerminated = containerStatus.getState().getTerminated();
return String.format("%s(exitCode=%d, reason=%s, message=%s)", containerStatus.getName(), containerStateTerminated.getExitCode(), containerStateTerminated.getReason(), containerStateTerminated.getMessage());
}).collect(Collectors.joining(",")));
}
sb.append("]");
if (PodPhase.Failed.name().equals(getInternalResource().getStatus().getPhase())) {
sb.append(String.format(", pod status: %s(reason=%s, message=%s)", getInternalResource().getStatus().getPhase(), getInternalResource().getStatus().getReason(), getInternalResource().getStatus().getMessage()));
}
return sb.toString();
}
use of io.fabric8.kubernetes.api.model.ContainerStatus in project fabric8 by fabric8io.
the class SessionPodsAreReady method call.
@Override
public Boolean call() throws Exception {
boolean result = true;
List<Pod> pods = notNullList(kubernetesClient.pods().inNamespace(session.getNamespace()).list().getItems());
if (pods.isEmpty()) {
result = false;
session.getLogger().warn("No pods are available yet, waiting...");
}
for (Pod pod : pods) {
if (!KubernetesHelper.isPodReady(pod)) {
PodStatus podStatus = pod.getStatus();
int restartCount = 0;
if (podStatus != null) {
if ("Succeeded".equals(podStatus.getPhase())) {
// that have finished. see: OSFUSE-317
continue;
}
List<ContainerStatus> containerStatuses = podStatus.getContainerStatuses();
for (ContainerStatus containerStatus : containerStatuses) {
if (restartCount == 0) {
Integer restartCountValue = containerStatus.getRestartCount();
if (restartCountValue != null) {
restartCount = restartCountValue.intValue();
}
}
ContainerState state = containerStatus.getState();
if (state != null) {
ContainerStateWaiting waiting = state.getWaiting();
String containerName = containerStatus.getName();
if (waiting != null) {
session.getLogger().warn("Waiting for container:" + containerName + ". Reason:" + waiting.getReason());
} else {
session.getLogger().warn("Waiting for container:" + containerName + ".");
}
}
}
}
result = false;
String name = KubernetesHelper.getName(pod);
File yamlFile = new File(session.getBaseDir(), "target/test-pod-status/" + name + ".yml");
yamlFile.getParentFile().mkdirs();
try {
KubernetesHelper.saveYaml(pod, yamlFile);
} catch (IOException e) {
session.getLogger().warn("Failed to write " + yamlFile + ". " + e);
}
if (KubernetesHelper.isPodRunning(pod)) {
List<Container> containers = pod.getSpec().getContainers();
for (Container container : containers) {
File logFile = LogHelpers.getLogFileName(session.getBaseDir(), name, container, restartCount);
String log = kubernetesClient.pods().inNamespace(session.getNamespace()).withName(name).inContainer(container.getName()).getLog();
IOHelpers.writeFully(logFile, log);
}
}
}
}
return result;
}
Aggregations