Search in sources :

Example 36 with ApiException

use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.

the class PodLogsTest method testNotFound.

@Test
public void testNotFound() throws ApiException, IOException {
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(podName).namespace(namespace)).spec(new V1PodSpec().containers(Arrays.asList(new V1Container().name(container).image("nginx"))));
    wireMockRule.stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log")).willReturn(aResponse().withStatus(404).withHeader("Content-Type", "text/plain").withBody("Not Found")));
    PodLogs logs = new PodLogs(client);
    boolean thrown = false;
    try {
        logs.streamNamespacedPodLog(pod);
    } catch (ApiException ex) {
        assertEquals(404, ex.getCode());
        thrown = true;
    }
    assertEquals(thrown, true);
    wireMockRule.verify(getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log")).withQueryParam("container", equalTo(container)).withQueryParam("follow", equalTo("true")).withQueryParam("pretty", equalTo("false")).withQueryParam("previous", equalTo("false")).withQueryParam("timestamps", equalTo("false")));
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

Example 37 with ApiException

use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.

the class GenericKubernetesApi method executeCall.

private <DataType extends KubernetesType> KubernetesApiResponse<DataType> executeCall(ApiClient apiClient, Class<DataType> dataClass, CallBuilder callBuilder) {
    try {
        Call call = callBuilder.build();
        call = tweakCallForCoreV1Group(call);
        JsonElement element = apiClient.<JsonElement>execute(call, JsonElement.class).getData();
        return getKubernetesApiResponse(dataClass, element, apiClient.getJSON().getGson());
    } catch (ApiException e) {
        if (e.getCause() instanceof IOException) {
            // make this a checked exception?
            throw new IllegalStateException(e.getCause());
        }
        final V1Status status;
        try {
            status = apiClient.getJSON().deserialize(e.getResponseBody(), V1Status.class);
        } catch (JsonSyntaxException jsonEx) {
            // craft a status object
            return new KubernetesApiResponse<>(new V1Status().code(e.getCode()).message(e.getResponseBody()), e.getCode());
        }
        if (null == status) {
            // this line should never reach?
            throw new RuntimeException(e);
        }
        return new KubernetesApiResponse<>(status, e.getCode());
    }
}
Also used : Call(okhttp3.Call) JsonSyntaxException(com.google.gson.JsonSyntaxException) V1Status(io.kubernetes.client.openapi.models.V1Status) JsonElement(com.google.gson.JsonElement) IOException(java.io.IOException) ApiException(io.kubernetes.client.openapi.ApiException)

Example 38 with ApiException

use of io.kubernetes.client.openapi.ApiException in project twister2 by DSC-SPIDAL.

the class WorkerLogger method run.

@Override
public void run() {
    Path logfile = createLogFile();
    LOG.info("Starting to log for " + id + " to: " + logFileName);
    try {
        logStream = streamContainerLog();
        Files.copy(logStream, logfile, StandardCopyOption.REPLACE_EXISTING);
        if (!stop) {
            logStream.close();
            response.close();
            LOG.info("Logging completed for " + id + " to: " + logFileName);
        }
    } catch (ApiException e) {
        if (!stop) {
            LOG.log(Level.SEVERE, "Cannot get the log stream for " + id, e);
        }
    } catch (IOException e) {
        if (!stop) {
            LOG.log(Level.SEVERE, "Cannot get the log stream for " + id, e);
        }
    }
    jobLogger.workerLoggerCompleted(id);
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) ApiException(io.kubernetes.client.openapi.ApiException)

Example 39 with ApiException

use of io.kubernetes.client.openapi.ApiException in project twister2 by DSC-SPIDAL.

the class KubernetesController method getUploaderWebServerPods.

public List<String> getUploaderWebServerPods(String uploaderLabel) {
    V1PodList podList = null;
    try {
        podList = coreApi.listNamespacedPod(namespace, null, null, null, null, uploaderLabel, null, null, null, null);
    } catch (ApiException e) {
        LOG.log(Level.SEVERE, "Exception when getting uploader pod list.", e);
        throw new RuntimeException(e);
    }
    List<String> podNames = podList.getItems().stream().map(v1pod -> v1pod.getMetadata().getName()).collect(Collectors.toList());
    return podNames;
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1NodeList(io.kubernetes.client.openapi.models.V1NodeList) V1ConfigMapList(io.kubernetes.client.openapi.models.V1ConfigMapList) V1NodeAddress(io.kubernetes.client.openapi.models.V1NodeAddress) V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Node(io.kubernetes.client.openapi.models.V1Node) V1StatefulSetList(io.kubernetes.client.openapi.models.V1StatefulSetList) Twister2RuntimeException(edu.iu.dsc.tws.api.exceptions.Twister2RuntimeException) AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ApiClient(io.kubernetes.client.openapi.ApiClient) JobMasterAPI(edu.iu.dsc.tws.proto.jobmaster.JobMasterAPI) ApiException(io.kubernetes.client.openapi.ApiException) V1Patch(io.kubernetes.client.custom.V1Patch) Configuration(io.kubernetes.client.openapi.Configuration) V1PersistentVolume(io.kubernetes.client.openapi.models.V1PersistentVolume) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) Map(java.util.Map) V1PersistentVolumeClaimList(io.kubernetes.client.openapi.models.V1PersistentVolumeClaimList) V1StatefulSet(io.kubernetes.client.openapi.models.V1StatefulSet) V1ServiceList(io.kubernetes.client.openapi.models.V1ServiceList) NodeInfoUtils(edu.iu.dsc.tws.proto.utils.NodeInfoUtils) Exec(io.kubernetes.client.Exec) V1Secret(io.kubernetes.client.openapi.models.V1Secret) JobAPI(edu.iu.dsc.tws.proto.system.job.JobAPI) IOException(java.io.IOException) ProcessUtils(edu.iu.dsc.tws.rsched.utils.ProcessUtils) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) ClientBuilder(io.kubernetes.client.util.ClientBuilder) Base64(java.util.Base64) List(java.util.List) V1PersistentVolumeList(io.kubernetes.client.openapi.models.V1PersistentVolumeList) OkHttpClient(okhttp3.OkHttpClient) V1SecretList(io.kubernetes.client.openapi.models.V1SecretList) V1PersistentVolumeClaim(io.kubernetes.client.openapi.models.V1PersistentVolumeClaim) V1Service(io.kubernetes.client.openapi.models.V1Service) Twister2RuntimeException(edu.iu.dsc.tws.api.exceptions.Twister2RuntimeException) ApiException(io.kubernetes.client.openapi.ApiException)

Example 40 with ApiException

use of io.kubernetes.client.openapi.ApiException in project twister2 by DSC-SPIDAL.

the class PodWatchUtils method getNodeIP.

/**
 * get the IP of the node where the pod with that name is running
 */
public static String getNodeIP(String namespace, String jobID, String podIP) {
    if (apiClient == null || coreApi == null) {
        createApiInstances();
    }
    // this is better but it does not work with another installation
    // String podNameLabel = "statefulset.kubernetes.io/pod-name=" + podName;
    String workerRoleLabel = KubernetesUtils.workerPodLabelSelector(jobID);
    V1PodList podList = null;
    try {
        podList = coreApi.listNamespacedPod(namespace, null, null, null, null, workerRoleLabel, null, null, null, null);
    } catch (ApiException e) {
        LOG.log(Level.SEVERE, "Exception when getting PodList.", e);
        throw new RuntimeException(e);
    }
    for (V1Pod pod : podList.getItems()) {
        if (podIP.equals(pod.getStatus().getPodIP())) {
            return pod.getStatus().getHostIP();
        }
    }
    return null;
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ApiException(io.kubernetes.client.openapi.ApiException)

Aggregations

ApiException (io.kubernetes.client.openapi.ApiException)82 V1Pod (io.kubernetes.client.openapi.models.V1Pod)21 IOException (java.io.IOException)21 V1PodList (io.kubernetes.client.openapi.models.V1PodList)18 Test (org.junit.Test)18 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)17 KubectlException (io.kubernetes.client.extended.kubectl.exception.KubectlException)14 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)13 ApiClient (io.kubernetes.client.openapi.ApiClient)10 AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)10 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)8 V1Deployment (io.kubernetes.client.openapi.models.V1Deployment)8 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)8 V1Status (io.kubernetes.client.openapi.models.V1Status)8 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)8 Watch (io.kubernetes.client.util.Watch)8 List (java.util.List)8 Configuration (io.kubernetes.client.openapi.Configuration)6 HashMap (java.util.HashMap)6 V1Patch (io.kubernetes.client.custom.V1Patch)5