Search in sources :

Example 6 with V1Patch

use of io.kubernetes.client.custom.V1Patch in project java by kubernetes-client.

the class EventLoggerTest method testPatchComputing.

@Test
public void testPatchComputing() {
    CoreV1Event event1 = new CoreV1EventBuilder().withSource(new V1EventSourceBuilder().build()).withMetadata(new V1ObjectMeta()).withInvolvedObject(new V1ObjectReferenceBuilder().build()).withCount(1).withMessage("foo1").build();
    CoreV1Event event2 = new CoreV1EventBuilder().withSource(new V1EventSourceBuilder().build()).withMetadata(new V1ObjectMeta()).withInvolvedObject(new V1ObjectReferenceBuilder().build()).withCount(2).withMessage("foo2").build();
    String aggregatedKey = EventUtils.getAggregatedAndLocalKeyByReason(event1).getRight();
    EventLogger eventLogger = new EventLogger(100, EventUtils::getEventKey);
    MutablePair<CoreV1Event, V1Patch> result1 = eventLogger.observe(event1, aggregatedKey);
    assertEquals(event1, result1.getLeft());
    assertNull(result1.getRight());
    MutablePair<CoreV1Event, V1Patch> result2 = eventLogger.observe(event2, aggregatedKey);
    assertEquals(event2, result2.getLeft());
    assertNotNull(result2.getRight());
}
Also used : V1EventSourceBuilder(io.kubernetes.client.openapi.models.V1EventSourceBuilder) V1ObjectReferenceBuilder(io.kubernetes.client.openapi.models.V1ObjectReferenceBuilder) EventLogger(io.kubernetes.client.extended.event.legacy.EventLogger) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) EventUtils(io.kubernetes.client.extended.event.legacy.EventUtils) V1Patch(io.kubernetes.client.custom.V1Patch) CoreV1EventBuilder(io.kubernetes.client.openapi.models.CoreV1EventBuilder) CoreV1Event(io.kubernetes.client.openapi.models.CoreV1Event) Test(org.junit.Test)

Example 7 with V1Patch

use of io.kubernetes.client.custom.V1Patch in project java by kubernetes-client.

the class KubectlApply method executeServerSideApply.

private ApiType executeServerSideApply() throws KubectlException {
    refreshDiscovery();
    GenericKubernetesApi<ApiType, KubernetesListObject> api = getGenericApi();
    PatchOptions patchOptions = new PatchOptions();
    patchOptions.setForce(this.forceConflict);
    patchOptions.setFieldManager(this.fieldManager);
    if (ModelMapper.isNamespaced(this.targetObj.getClass())) {
        String targetNamespace = namespace != null ? namespace : Strings.isNullOrEmpty(targetObj.getMetadata().getNamespace()) ? Namespaces.NAMESPACE_DEFAULT : targetObj.getMetadata().getNamespace();
        KubernetesApiResponse<KubernetesObject> response = null;
        try {
            return api.patch(targetNamespace, targetObj.getMetadata().getName(), V1Patch.PATCH_FORMAT_APPLY_YAML, new V1Patch(apiClient.getJSON().serialize(targetObj)), patchOptions).throwsApiException().getObject();
        } catch (ApiException e) {
            throw new KubectlException(e);
        }
    } else {
        try {
            return api.patch(targetObj.getMetadata().getName(), V1Patch.PATCH_FORMAT_APPLY_YAML, new V1Patch(apiClient.getJSON().serialize(targetObj)), patchOptions).throwsApiException().getObject();
        } catch (ApiException e) {
            throw new KubectlException(e);
        }
    }
}
Also used : KubernetesListObject(io.kubernetes.client.common.KubernetesListObject) PatchOptions(io.kubernetes.client.util.generic.options.PatchOptions) KubernetesObject(io.kubernetes.client.common.KubernetesObject) V1Patch(io.kubernetes.client.custom.V1Patch) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException) ApiException(io.kubernetes.client.openapi.ApiException)

Example 8 with V1Patch

use of io.kubernetes.client.custom.V1Patch in project java by kubernetes-client.

the class PatchHelper method dryRunStrategyMergePatch.

public static <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject> ApiType dryRunStrategyMergePatch(GenericKubernetesApi<ApiType, ApiListType> genericApi, String patch, String namespace, String name) throws ApiException {
    PatchOptions options = new PatchOptions();
    options.setDryRun("All");
    return genericApi.patch(namespace, name, V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, new V1Patch(patch), options).throwsApiException().getObject();
}
Also used : PatchOptions(io.kubernetes.client.util.generic.options.PatchOptions) V1Patch(io.kubernetes.client.custom.V1Patch)

Example 9 with V1Patch

use of io.kubernetes.client.custom.V1Patch in project java by kubernetes-client.

the class GenericKubernetesApiForCoreApiTest method patchNamespacedPodReturningObject.

@Test
public void patchNamespacedPodReturningObject() {
    V1Patch v1Patch = new V1Patch("{}");
    V1Pod foo1 = new V1Pod().kind("Pod").metadata(new V1ObjectMeta().namespace("default").name("foo1"));
    stubFor(patch(urlEqualTo("/api/v1/namespaces/default/pods/foo1")).withHeader("Content-Type", containing(V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH)).willReturn(aResponse().withStatus(200).withBody(json.serialize(foo1))));
    KubernetesApiResponse<V1Pod> podPatchResp = podClient.patch("default", "foo1", V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, v1Patch);
    assertTrue(podPatchResp.isSuccess());
    assertEquals(foo1, podPatchResp.getObject());
    assertNull(podPatchResp.getStatus());
    verify(1, patchRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods/foo1")));
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Patch(io.kubernetes.client.custom.V1Patch) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Test(org.junit.Test)

Example 10 with V1Patch

use of io.kubernetes.client.custom.V1Patch in project java by kubernetes-client.

the class GenericKubernetesApiForCoreApiTest method patchNamespacedPodWithApiPrefix.

@Test
public void patchNamespacedPodWithApiPrefix() {
    V1Patch v1Patch = new V1Patch("{}");
    V1Pod foo1 = new V1Pod().kind("Pod").metadata(new V1ObjectMeta().namespace("default").name("foo1"));
    // add api prefix
    String prefix = "/k8s/clusters/c-7q988";
    stubFor(patch(urlEqualTo(prefix + "/api/v1/namespaces/default/pods/foo1")).withHeader("Content-Type", containing(V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH)).willReturn(aResponse().withStatus(200).withBody(json.serialize(foo1))));
    GenericKubernetesApi<V1Pod, V1PodList> rancherPodClient = new GenericKubernetesApi<>(V1Pod.class, V1PodList.class, "", "v1", "pods", new ClientBuilder().setBasePath("http://localhost:" + 8181 + prefix).build());
    KubernetesApiResponse<V1Pod> podPatchResp = rancherPodClient.patch("default", "foo1", V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, v1Patch);
    assertTrue(podPatchResp.isSuccess());
    assertEquals(foo1, podPatchResp.getObject());
    assertNull(podPatchResp.getStatus());
    verify(1, patchRequestedFor(urlPathEqualTo(prefix + "/api/v1/namespaces/default/pods/foo1")));
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Patch(io.kubernetes.client.custom.V1Patch) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ClientBuilder(io.kubernetes.client.util.ClientBuilder) Test(org.junit.Test)

Aggregations

V1Patch (io.kubernetes.client.custom.V1Patch)19 Test (org.junit.Test)9 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)6 ApiException (io.kubernetes.client.openapi.ApiException)4 AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)3 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)3 CoreV1Event (io.kubernetes.client.openapi.models.CoreV1Event)3 V1Pod (io.kubernetes.client.openapi.models.V1Pod)3 KubectlException (io.kubernetes.client.extended.kubectl.exception.KubectlException)2 ApiClient (io.kubernetes.client.openapi.ApiClient)2 V1Container (io.kubernetes.client.openapi.models.V1Container)2 V1Deployment (io.kubernetes.client.openapi.models.V1Deployment)2 V1PodList (io.kubernetes.client.openapi.models.V1PodList)2 V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)2 PatchOptions (io.kubernetes.client.util.generic.options.PatchOptions)2 OffsetDateTime (java.time.OffsetDateTime)2 MutablePair (org.apache.commons.lang3.tuple.MutablePair)2 EqualToPattern (com.github.tomakehurst.wiremock.matching.EqualToPattern)1 KubernetesListObject (io.kubernetes.client.common.KubernetesListObject)1 KubernetesObject (io.kubernetes.client.common.KubernetesObject)1