Search in sources :

Example 11 with ContainerStatus

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;
}
Also used : KubernetesResourceDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesResourceDescription) KubernetesUtil(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.KubernetesUtil) SpinnakerMonitoringDaemonService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerMonitoringDaemonService) KubernetesAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount) LogCollector(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.LogCollector) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) DaemonTaskHandler(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskHandler) SidecarService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.SidecarService) Pair(org.apache.commons.lang3.tuple.Pair) Names(com.netflix.frigga.Names) Map(java.util.Map) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Provider(com.netflix.spinnaker.halyard.config.model.v1.node.Provider) TypeReference(com.fasterxml.jackson.core.type.TypeReference) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Strings(io.fabric8.utils.Strings) DeploymentEnvironment(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentEnvironment) SecretVolumeSourceBuilder(io.fabric8.kubernetes.api.model.SecretVolumeSourceBuilder) ServiceInterfaceFactory(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceInterfaceFactory) KubernetesVolumeMount(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesVolumeMount) Set(java.util.Set) KubernetesProbe(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesProbe) GenerateService(com.netflix.spinnaker.halyard.deploy.services.v1.GenerateService) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) Collectors(java.util.stream.Collectors) List(java.util.List) KubernetesVolumeSource(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesVolumeSource) SocketUtils(org.springframework.util.SocketUtils) Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) KubernetesContainerDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesContainerDescription) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) Container(io.fabric8.kubernetes.api.model.Container) DeployKubernetesAtomicOperationDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.DeployKubernetesAtomicOperationDescription) CustomSizing(com.netflix.spinnaker.halyard.config.model.v1.node.CustomSizing) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) HashMap(java.util.HashMap) ArtifactService(com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) KubernetesHandler(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesHandler) AccountDeploymentDetails(com.netflix.spinnaker.halyard.deploy.deployment.v1.AccountDeploymentDetails) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) JobExecutor(com.netflix.spinnaker.halyard.core.job.v1.JobExecutor) KubernetesImageDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesImageDescription) KubernetesContainerPort(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesContainerPort) Instance(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails.Instance) KubernetesNamedServicePort(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.loadbalancer.KubernetesNamedServicePort) Volume(io.fabric8.kubernetes.api.model.Volume) ReplicaSetBuilder(io.fabric8.kubernetes.api.model.extensions.ReplicaSetBuilder) KubernetesLoadBalancerDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.loadbalancer.KubernetesLoadBalancerDescription) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) KubernetesEnvVar(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesEnvVar) KubernetesSecretVolumeSource(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesSecretVolumeSource) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) Pod(io.fabric8.kubernetes.api.model.Pod) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Profile(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile) Paths(java.nio.file.Paths) KubernetesHttpGetAction(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesHttpGetAction) KubernetesVolumeSourceType(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesVolumeSourceType) KubernetesTcpSocketAction(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesTcpSocketAction) DistributedService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.DistributedService) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ConfigSource(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ConfigSource) Collections(java.util.Collections) KubernetesHandlerType(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesHandlerType) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Pod(io.fabric8.kubernetes.api.model.Pod) Instance(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails.Instance) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) Names(com.netflix.frigga.Names) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) List(java.util.List) ArrayList(java.util.ArrayList) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet)

Example 12 with ContainerStatus

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();
}
Also used : VisibleForTesting(org.apache.flink.annotation.VisibleForTesting) Objects(java.util.Objects) ContainerStateTerminated(io.fabric8.kubernetes.api.model.ContainerStateTerminated) Pod(io.fabric8.kubernetes.api.model.Pod) Collectors(java.util.stream.Collectors) ContainerStateTerminated(io.fabric8.kubernetes.api.model.ContainerStateTerminated)

Example 13 with ContainerStatus

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;
}
Also used : ContainerStateWaiting(io.fabric8.kubernetes.api.model.ContainerStateWaiting) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) ContainerState(io.fabric8.kubernetes.api.model.ContainerState) PodStatus(io.fabric8.kubernetes.api.model.PodStatus) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Container(io.fabric8.kubernetes.api.model.Container) File(java.io.File)

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