Search in sources :

Example 46 with V1ObjectMeta

use of io.kubernetes.client.openapi.models.V1ObjectMeta 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 47 with V1ObjectMeta

use of io.kubernetes.client.openapi.models.V1ObjectMeta 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 48 with V1ObjectMeta

use of io.kubernetes.client.openapi.models.V1ObjectMeta 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 49 with V1ObjectMeta

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

the class PortForwardTest method testUrl.

@Test
public void testUrl() throws IOException, ApiException, InterruptedException {
    PortForward forward = new PortForward(client);
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(podName).namespace(namespace));
    wireMockRule.stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/portforward")).willReturn(aResponse().withStatus(404).withHeader("Content-Type", "application/json").withBody("{}")));
    int portNumber = 8080;
    List<Integer> ports = new ArrayList<>();
    ports.add(portNumber);
    assertThrows(ApiException.class, () -> {
        InputStream inputStream = forward.forward(pod, ports).getInputStream(portNumber);
        // block until the connection is established
        inputStream.read();
        inputStream.close();
    });
    wireMockRule.verify(getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/portforward")).withQueryParam("ports", equalTo(Integer.toString(portNumber))));
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Test(org.junit.Test)

Example 50 with V1ObjectMeta

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

the class CachesTest method testDefaultNamespaceIndex.

@Test
public void testDefaultNamespaceIndex() {
    String testName = "test-name";
    String testNamespace = "test-namespace";
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(testName).namespace(testNamespace));
    List<String> indices = Caches.metaNamespaceIndexFunc(pod);
    assertEquals(pod.getMetadata().getNamespace(), indices.get(0));
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Test(org.junit.Test)

Aggregations

V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)78 Test (org.junit.Test)49 V1Pod (io.kubernetes.client.openapi.models.V1Pod)37 HashMap (java.util.HashMap)14 ApiException (io.kubernetes.client.openapi.ApiException)11 V1PodList (io.kubernetes.client.openapi.models.V1PodList)11 V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)10 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)8 V1Container (io.kubernetes.client.openapi.models.V1Container)7 ApiClient (io.kubernetes.client.openapi.ApiClient)6 JSON (io.kubernetes.client.openapi.JSON)6 V1Job (io.kubernetes.client.openapi.models.V1Job)6 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)6 V1PodTemplateSpec (io.kubernetes.client.openapi.models.V1PodTemplateSpec)6 V1Patch (io.kubernetes.client.custom.V1Patch)5 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)5 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)5 V1Service (io.kubernetes.client.openapi.models.V1Service)5 V1ServiceSpec (io.kubernetes.client.openapi.models.V1ServiceSpec)5 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)5