Search in sources :

Example 26 with CoreV1Api

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

the class KubectlDrain method doDrain.

private V1Node doDrain() throws KubectlException, ApiException, IOException {
    CoreV1Api api = new CoreV1Api(apiClient);
    V1Node node = performCordon();
    V1PodList allPods = api.listPodForAllNamespaces(null, null, "spec.nodeName=" + node.getMetadata().getName(), null, null, null, null, null, null, null);
    validatePods(allPods.getItems());
    for (V1Pod pod : allPods.getItems()) {
        deletePod(api, pod.getMetadata().getName(), pod.getMetadata().getNamespace());
    }
    return node;
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Node(io.kubernetes.client.openapi.models.V1Node) V1Pod(io.kubernetes.client.openapi.models.V1Pod) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Example 27 with CoreV1Api

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

the class KubectlTaint method executeInternal.

private V1Node executeInternal() throws KubectlException, ApiException, IOException {
    CoreV1Api v1 = new CoreV1Api(apiClient);
    V1Node node = v1.readNode(name, null);
    TaintsBuilder builder = Taints.taints(node);
    for (Map.Entry<String, Pair<String, String>> taint : addingTaints.entrySet()) {
        builder.addTaint(taint.getKey(), taint.getValue().getLeft(), makeEffect(taint.getValue().getRight()));
    }
    for (Map.Entry<String, String> taint : removeTaints.entrySet()) {
        if (taint.getValue() == null) {
            builder.removeTaint(taint.getKey());
        } else {
            builder.removeTaint(taint.getKey(), makeEffect(taint.getValue()));
        }
    }
    return v1.replaceNode(name, node, null, null, null, null);
}
Also used : V1Node(io.kubernetes.client.openapi.models.V1Node) TaintsBuilder(io.kubernetes.client.util.taints.Taints.TaintsBuilder) HashMap(java.util.HashMap) Map(java.util.Map) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Pair(org.apache.commons.lang3.tuple.Pair)

Example 28 with CoreV1Api

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

the class PatchUtilsTest method testMergePatchPod.

@Test
public void testMergePatchPod() throws ApiException {
    CoreV1Api coreV1Api = new CoreV1Api(client);
    stubFor(patch(urlPathEqualTo("/api/v1/namespaces/default/pods/foo")).withHeader("Content-Type", containing(V1Patch.PATCH_FORMAT_JSON_MERGE_PATCH)).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("{}")));
    PatchUtils.patch(V1Pod.class, () -> coreV1Api.patchNamespacedPodCall("foo", "default", new V1Patch("[]"), null, null, null, null, null, null), V1Patch.PATCH_FORMAT_JSON_MERGE_PATCH, client);
    verify(1, patchRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods/foo")));
}
Also used : V1Patch(io.kubernetes.client.custom.V1Patch) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Test(org.junit.Test)

Example 29 with CoreV1Api

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

the class PatchUtilsTest method testJsonPatchPod.

@Test
public void testJsonPatchPod() throws ApiException {
    CoreV1Api coreV1Api = new CoreV1Api(client);
    stubFor(patch(urlPathEqualTo("/api/v1/namespaces/default/pods/foo")).withHeader("Content-Type", containing(V1Patch.PATCH_FORMAT_JSON_PATCH)).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("{}")));
    PatchUtils.patch(V1Pod.class, () -> coreV1Api.patchNamespacedPodCall("foo", "default", new V1Patch("[]"), null, null, null, null, null, null), V1Patch.PATCH_FORMAT_JSON_PATCH, client);
    verify(1, patchRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods/foo")));
}
Also used : V1Patch(io.kubernetes.client.custom.V1Patch) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Test(org.junit.Test)

Example 30 with CoreV1Api

use of io.kubernetes.client.openapi.apis.CoreV1Api 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

CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)48 V1Pod (io.kubernetes.client.openapi.models.V1Pod)18 ApiClient (io.kubernetes.client.openapi.ApiClient)16 Test (org.junit.Test)16 ApiException (io.kubernetes.client.openapi.ApiException)13 V1PodList (io.kubernetes.client.openapi.models.V1PodList)13 SneakyThrows (lombok.SneakyThrows)12 IOException (java.io.IOException)11 V1Namespace (io.kubernetes.client.openapi.models.V1Namespace)10 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)10 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)9 Watch (io.kubernetes.client.util.Watch)9 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)8 JSON (io.kubernetes.client.openapi.JSON)6 V1Status (io.kubernetes.client.openapi.models.V1Status)6 OkHttpClient (okhttp3.OkHttpClient)6 V1Patch (io.kubernetes.client.custom.V1Patch)5 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)5 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5