Search in sources :

Example 1 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 2 with V1Patch

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

the class PatchExample method main.

public static void main(String[] args) throws IOException {
    try {
        AppsV1Api api = new AppsV1Api(ClientBuilder.standard().build());
        V1Deployment body = Configuration.getDefaultApiClient().getJSON().deserialize(jsonDeploymentStr, V1Deployment.class);
        // create a deployment
        V1Deployment deploy1 = api.createNamespacedDeployment("default", body, null, null, null, null);
        System.out.println("original deployment" + deploy1);
        // json-patch a deployment
        V1Deployment deploy2 = PatchUtils.patch(V1Deployment.class, () -> api.patchNamespacedDeploymentCall("hello-node", "default", new V1Patch(jsonPatchStr), null, null, null, // field-manager is optional
        null, null, null), V1Patch.PATCH_FORMAT_JSON_PATCH, api.getApiClient());
        System.out.println("json-patched deployment" + deploy2);
        // strategic-merge-patch a deployment
        V1Deployment deploy3 = PatchUtils.patch(V1Deployment.class, () -> api.patchNamespacedDeploymentCall("hello-node", "default", new V1Patch(strategicMergePatchStr), null, null, // field-manager is optional
        null, null, null, null), V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, api.getApiClient());
        System.out.println("strategic-merge-patched deployment" + deploy3);
        // apply-yaml a deployment, server side apply is available by default after kubernetes v1.16
        // or opt-in by turning on the feature gate for v1.14 or v1.15.
        // https://kubernetes.io/docs/reference/using-api/api-concepts/#server-side-apply
        V1Deployment deploy4 = PatchUtils.patch(V1Deployment.class, () -> api.patchNamespacedDeploymentCall("hello-node", "default", new V1Patch(applyYamlStr), null, null, // field-manager is required for server-side apply
        "example-field-manager", null, true, null), V1Patch.PATCH_FORMAT_APPLY_YAML, api.getApiClient());
        System.out.println("application/apply-patch+yaml deployment" + deploy4);
    } catch (ApiException e) {
        System.out.println(e.getResponseBody());
        e.printStackTrace();
    }
}
Also used : V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) V1Patch(io.kubernetes.client.custom.V1Patch) ApiException(io.kubernetes.client.openapi.ApiException)

Example 3 with V1Patch

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

the class DeployRolloutRestartExample method main.

public static void main(String[] args) throws IOException, ApiException {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    AppsV1Api appsV1Api = new AppsV1Api(client);
    String deploymentName = "example-nginx";
    String imageName = "nginx:1.21.6";
    String namespace = "default";
    // Create an example deployment
    V1DeploymentBuilder deploymentBuilder = new V1DeploymentBuilder().withApiVersion("apps/v1").withKind("Deployment").withMetadata(new V1ObjectMeta().name(deploymentName).namespace(namespace)).withSpec(new V1DeploymentSpec().replicas(1).selector(new V1LabelSelector().putMatchLabelsItem("name", deploymentName)).template(new V1PodTemplateSpec().metadata(new V1ObjectMeta().putLabelsItem("name", deploymentName)).spec(new V1PodSpec().containers(Collections.singletonList(new V1Container().name(deploymentName).image(imageName))))));
    appsV1Api.createNamespacedDeployment(namespace, deploymentBuilder.build(), null, null, null, null);
    // Wait until example deployment is ready
    Wait.poll(Duration.ofSeconds(3), Duration.ofSeconds(60), () -> {
        try {
            System.out.println("Waiting until example deployment is ready...");
            return appsV1Api.readNamespacedDeployment(deploymentName, namespace, null).getStatus().getReadyReplicas() > 0;
        } catch (ApiException e) {
            e.printStackTrace();
            return false;
        }
    });
    System.out.println("Created example deployment!");
    // Trigger a rollout restart of the example deployment
    V1Deployment runningDeployment = appsV1Api.readNamespacedDeployment(deploymentName, namespace, null);
    // Explicitly set "restartedAt" annotation with current date/time to trigger rollout when patch
    // is applied
    runningDeployment.getSpec().getTemplate().getMetadata().putAnnotationsItem("kubectl.kubernetes.io/restartedAt", LocalDateTime.now().toString());
    try {
        String deploymentJson = client.getJSON().serialize(runningDeployment);
        PatchUtils.patch(V1Deployment.class, () -> appsV1Api.patchNamespacedDeploymentCall(deploymentName, namespace, new V1Patch(deploymentJson), null, null, "kubectl-rollout", null, null, null), V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, client);
        // Wait until deployment has stabilized after rollout restart
        Wait.poll(Duration.ofSeconds(3), Duration.ofSeconds(60), () -> {
            try {
                System.out.println("Waiting until example deployment restarted successfully...");
                return appsV1Api.readNamespacedDeployment(deploymentName, namespace, null).getStatus().getReadyReplicas() > 0;
            } catch (ApiException e) {
                e.printStackTrace();
                return false;
            }
        });
        System.out.println("Example deployment restarted successfully!");
    } catch (ApiException e) {
        e.printStackTrace();
    }
}
Also used : V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Patch(io.kubernetes.client.custom.V1Patch) V1DeploymentSpec(io.kubernetes.client.openapi.models.V1DeploymentSpec) V1DeploymentBuilder(io.kubernetes.client.openapi.models.V1DeploymentBuilder) ApiClient(io.kubernetes.client.openapi.ApiClient) V1Container(io.kubernetes.client.openapi.models.V1Container) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) V1LabelSelector(io.kubernetes.client.openapi.models.V1LabelSelector) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) ApiException(io.kubernetes.client.openapi.ApiException)

Example 4 with V1Patch

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

the class GenericClientExample method main.

public static void main(String[] args) throws Exception {
    // The following codes demonstrates using generic client to manipulate pods
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name("foo").namespace("default")).spec(new V1PodSpec().containers(Arrays.asList(new V1Container().name("c").image("test"))));
    ApiClient apiClient = ClientBuilder.standard().build();
    GenericKubernetesApi<V1Pod, V1PodList> podClient = new GenericKubernetesApi<>(V1Pod.class, V1PodList.class, "", "v1", "pods", apiClient);
    V1Pod latestPod = podClient.create(pod).throwsApiException().getObject();
    System.out.println("Created!");
    V1Pod patchedPod = podClient.patch("default", "foo", V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, new V1Patch("{\"metadata\":{\"finalizers\":[\"example.io/foo\"]}}")).throwsApiException().getObject();
    System.out.println("Patched!");
    V1Pod deletedPod = podClient.delete("default", "foo").throwsApiException().getObject();
    if (deletedPod != null) {
        System.out.println("Received after-deletion status of the requested object, will be deleting in background!");
    }
    System.out.println("Deleted!");
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) 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) GenericKubernetesApi(io.kubernetes.client.util.generic.GenericKubernetesApi) ApiClient(io.kubernetes.client.openapi.ApiClient) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec)

Example 5 with V1Patch

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

the class EventLogger method observe.

public MutablePair<CoreV1Event, V1Patch> observe(CoreV1Event event, String key) {
    OffsetDateTime now = OffsetDateTime.now();
    EventLog lastObserved = this.eventCache.getIfPresent(key);
    V1Patch patch = null;
    if (lastObserved != null && lastObserved.count != null && lastObserved.count > 0) {
        event.setCount(lastObserved.count + 1);
        event.setFirstTimestamp(lastObserved.firstTimestamp);
        event.getMetadata().setName(lastObserved.name);
        event.getMetadata().setResourceVersion(lastObserved.resourceVersion);
        patch = buildEventPatch(event.getCount(), event.getMessage(), now);
    }
    EventLog log = new EventLog();
    log.count = event.getCount();
    log.firstTimestamp = event.getFirstTimestamp();
    log.name = event.getMetadata().getName();
    log.resourceVersion = event.getMetadata().getResourceVersion();
    this.eventCache.put(key, log);
    return new MutablePair<>(event, patch);
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) OffsetDateTime(java.time.OffsetDateTime) V1Patch(io.kubernetes.client.custom.V1Patch)

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