Search in sources :

Example 1 with PodContainers

use of org.apache.skywalking.oap.query.graphql.type.PodContainers in project skywalking by apache.

the class OndemandLogQuery method listContainers.

public PodContainers listContainers(final String namespace, final String podName) throws IOException {
    try {
        if (Strings.isNullOrEmpty(namespace) || Strings.isNullOrEmpty(podName)) {
            return new PodContainers().setErrorReason("namespace and podName can't be null or empty");
        }
        final V1Pod pod = kApi().readNamespacedPod(podName, namespace, null);
        final V1PodSpec spec = pod.getSpec();
        if (isNull(spec)) {
            return new PodContainers().setErrorReason("No pod spec can be found");
        }
        final List<String> containers = spec.getContainers().stream().map(V1Container::getName).collect(Collectors.toList());
        if (nonNull(spec.getInitContainers())) {
            final List<String> init = spec.getInitContainers().stream().map(V1Container::getName).collect(Collectors.toList());
            containers.addAll(init);
        }
        return new PodContainers().setContainers(containers);
    } catch (ApiException e) {
        log.error("Failed to list containers from Kubernetes, {}", e.getResponseBody(), e);
        if (!Strings.isNullOrEmpty(e.getResponseBody())) {
            Map<String, Object> responseBody = gson.fromJson(e.getResponseBody(), responseType);
            String message = responseBody.getOrDefault("message", e.getCode()).toString();
            return new PodContainers().setErrorReason(message);
        }
        return new PodContainers().setErrorReason(e.getMessage() + ": " + e.getCode());
    }
}
Also used : V1Pod(io.kubernetes.client.openapi.models.V1Pod) Map(java.util.Map) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) PodContainers(org.apache.skywalking.oap.query.graphql.type.PodContainers) ApiException(io.kubernetes.client.openapi.ApiException)

Aggregations

ApiException (io.kubernetes.client.openapi.ApiException)1 V1Pod (io.kubernetes.client.openapi.models.V1Pod)1 V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)1 Map (java.util.Map)1 PodContainers (org.apache.skywalking.oap.query.graphql.type.PodContainers)1