Search in sources :

Example 51 with ApiException

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

the class LeaseLock method create.

@Override
public boolean create(LeaderElectionRecord record) {
    try {
        V1ObjectMeta objectMeta = new V1ObjectMeta();
        objectMeta.setName(name);
        objectMeta.setNamespace(namespace);
        if (record.getOwnerReference() != null) {
            objectMeta.setOwnerReferences(Collections.singletonList(record.getOwnerReference()));
        }
        V1Lease createdLease = coordinationV1Api.createNamespacedLease(namespace, new V1Lease().metadata(objectMeta).spec(getLeaseFromRecord(record)), null, null, null, null);
        leaseRefer.set(createdLease);
        return true;
    } catch (ApiException e) {
        if (e.getCode() == HttpURLConnection.HTTP_CONFLICT) {
            log.debug("received {} when creating lease lock", e.getCode(), e);
        } else {
            log.error("received {} when creating lease lock", e.getCode(), e);
        }
        return false;
    }
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Lease(io.kubernetes.client.openapi.models.V1Lease) ApiException(io.kubernetes.client.openapi.ApiException)

Example 52 with ApiException

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

the class LeaseLock method update.

@Override
public boolean update(LeaderElectionRecord record) {
    try {
        V1Lease latest = leaseRefer.get();
        latest.setSpec(getLeaseFromRecord(record));
        V1Lease updatedLease = coordinationV1Api.replaceNamespacedLease(name, namespace, latest, null, null, null, null);
        leaseRefer.set(updatedLease);
        return true;
    } catch (ApiException e) {
        if (e.getCode() == HttpURLConnection.HTTP_CONFLICT) {
            log.debug("received {} when updating lease lock", e.getCode(), e);
        } else {
            log.error("received {} when updating lease lock", e.getCode(), e);
        }
        return false;
    }
}
Also used : V1Lease(io.kubernetes.client.openapi.models.V1Lease) ApiException(io.kubernetes.client.openapi.ApiException)

Example 53 with ApiException

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

the class EndpointsLock method create.

@Override
public boolean create(LeaderElectionRecord record) {
    try {
        V1Endpoints endpoints = new V1Endpoints();
        V1ObjectMeta objectMeta = new V1ObjectMeta();
        objectMeta.setName(name);
        objectMeta.setNamespace(namespace);
        Map<String, String> annotations = new HashMap<>();
        annotations.put(LeaderElectionRecordAnnotationKey, coreV1Client.getApiClient().getJSON().serialize(record));
        objectMeta.setAnnotations(annotations);
        if (record.getOwnerReference() != null) {
            objectMeta.setOwnerReferences(Collections.singletonList(record.getOwnerReference()));
        }
        endpoints.setMetadata(objectMeta);
        V1Endpoints createdendpoints = coreV1Client.createNamespacedEndpoints(namespace, endpoints, null, null, null, null);
        endpointsRefer.set(createdendpoints);
        return true;
    } catch (ApiException e) {
        if (e.getCode() == HttpURLConnection.HTTP_CONFLICT) {
            log.debug("received {} when creating endpoints lock", e.getCode(), e);
        } else {
            log.error("received {} when creating endpoints lock", e.getCode(), e);
        }
        return false;
    }
}
Also used : V1Endpoints(io.kubernetes.client.openapi.models.V1Endpoints) HashMap(java.util.HashMap) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) ApiException(io.kubernetes.client.openapi.ApiException)

Example 54 with ApiException

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

the class CopyTest method testUrl.

@Test
public void testUrl() throws IOException, ApiException, InterruptedException {
    Copy copy = new Copy(client);
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(podName).namespace(namespace));
    wireMockRule.stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec")).willReturn(aResponse().withStatus(404).withHeader("Content-Type", "application/json").withBody("{}")));
    try {
        InputStream inputStream = copy.copyFileFromPod(pod, "container", "/some/path/to/file");
        // block until the connection is established
        inputStream.read();
        inputStream.close();
    } catch (IOException | ApiException | IllegalStateException e) {
        e.printStackTrace();
    }
    wireMockRule.verify(getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec")).withQueryParam("stdin", equalTo("false")).withQueryParam("stdout", equalTo("true")).withQueryParam("stderr", equalTo("true")).withQueryParam("tty", equalTo("false")).withQueryParam("command", new AnythingPattern()));
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) InputStream(java.io.InputStream) AnythingPattern(com.github.tomakehurst.wiremock.matching.AnythingPattern) V1Pod(io.kubernetes.client.openapi.models.V1Pod) IOException(java.io.IOException) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

Example 55 with ApiException

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

the class DefaultSharedIndexInformerWireMockTest method testAllNamespacedPodInformerTransformFailure.

@Test
public void testAllNamespacedPodInformerTransformFailure() throws InterruptedException {
    CoreV1Api coreV1Api = new CoreV1Api(client);
    String startRV = "1000";
    String endRV = "1001";
    V1PodList podList = new V1PodList().metadata(new V1ListMeta().resourceVersion(startRV)).items(Arrays.asList());
    stubFor(get(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(podList))));
    Watch.Response<V1Pod> watchResponse = new Watch.Response<>(EventType.ADDED.name(), new V1Pod().metadata(new V1ObjectMeta().namespace(namespace).name(podName).resourceVersion(endRV)));
    stubFor(get(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(watchResponse))));
    SharedInformerFactory factory = new SharedInformerFactory();
    SharedIndexInformer<V1Pod> podInformer = factory.sharedIndexInformerFor((CallGeneratorParams params) -> {
        try {
            return coreV1Api.listPodForAllNamespacesCall(null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
    }, V1Pod.class, V1PodList.class);
    podInformer.setTransform((obj) -> {
        throw new ObjectTransformException("test transform failure");
    });
    AtomicBoolean foundExistingPod = new AtomicBoolean(false);
    podInformer.addEventHandler(new ResourceEventHandler<V1Pod>() {

        @Override
        public void onAdd(V1Pod obj) {
            if (podName.equals(obj.getMetadata().getName()) && namespace.equals(obj.getMetadata().getNamespace())) {
                foundExistingPod.set(true);
            }
        }

        @Override
        public void onUpdate(V1Pod oldObj, V1Pod newObj) {
        }

        @Override
        public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {
        }
    });
    factory.startAllRegisteredInformers();
    Thread.sleep(1000);
    // cannot find the pod due to transform failure
    assertFalse(foundExistingPod.get());
    assertEquals(endRV, podInformer.lastSyncResourceVersion());
    verify(1, getRequestedFor(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("false")));
    verify(moreThan(1), getRequestedFor(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("true")));
    factory.stopAllRegisteredInformers();
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) ObjectTransformException(io.kubernetes.client.informer.exception.ObjectTransformException) JSON(io.kubernetes.client.openapi.JSON) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) V1PodList(io.kubernetes.client.openapi.models.V1PodList) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) Watch(io.kubernetes.client.util.Watch) V1Pod(io.kubernetes.client.openapi.models.V1Pod) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

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