|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | +package io.kubernetes.client.examples; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.time.Duration; |
| 17 | +import java.time.LocalDateTime; |
| 18 | +import java.util.Collections; |
| 19 | + |
| 20 | +import io.kubernetes.client.custom.V1Patch; |
| 21 | +import io.kubernetes.client.openapi.ApiClient; |
| 22 | +import io.kubernetes.client.openapi.ApiException; |
| 23 | +import io.kubernetes.client.openapi.Configuration; |
| 24 | +import io.kubernetes.client.openapi.apis.AppsV1Api; |
| 25 | +import io.kubernetes.client.openapi.models.V1Container; |
| 26 | +import io.kubernetes.client.openapi.models.V1Deployment; |
| 27 | +import io.kubernetes.client.openapi.models.V1DeploymentBuilder; |
| 28 | +import io.kubernetes.client.openapi.models.V1DeploymentSpec; |
| 29 | +import io.kubernetes.client.openapi.models.V1LabelSelector; |
| 30 | +import io.kubernetes.client.openapi.models.V1ObjectMeta; |
| 31 | +import io.kubernetes.client.openapi.models.V1PodSpec; |
| 32 | +import io.kubernetes.client.openapi.models.V1PodTemplateSpec; |
| 33 | +import io.kubernetes.client.util.Config; |
| 34 | +import io.kubernetes.client.util.PatchUtils; |
| 35 | +import io.kubernetes.client.util.wait.Wait; |
| 36 | + |
| 37 | +public class DeployRolloutRestartExample { |
| 38 | + public static void main(String[] args) throws IOException, ApiException { |
| 39 | + ApiClient client = Config.defaultClient(); |
| 40 | + Configuration.setDefaultApiClient(client); |
| 41 | + AppsV1Api appsV1Api = new AppsV1Api(client); |
| 42 | + |
| 43 | + String deploymentName = "example-nginx"; |
| 44 | + String imageName = "nginx:latest"; |
| 45 | + String namespace = "default"; |
| 46 | + |
| 47 | + // Create an example deployment |
| 48 | + V1DeploymentBuilder deploymentBuilder = new V1DeploymentBuilder().withApiVersion("apps/v1") |
| 49 | + .withKind("Deployment").withMetadata(new V1ObjectMeta().name(deploymentName) |
| 50 | + .namespace(namespace)).withSpec(new V1DeploymentSpec() |
| 51 | + .replicas(1).selector(new V1LabelSelector().putMatchLabelsItem("name", deploymentName)) |
| 52 | + .template(new V1PodTemplateSpec().metadata( |
| 53 | + new V1ObjectMeta().putLabelsItem("name", deploymentName)) |
| 54 | + .spec(new V1PodSpec().containers(Collections.singletonList(new V1Container() |
| 55 | + .name(deploymentName).image(imageName)))))); |
| 56 | + appsV1Api.createNamespacedDeployment(namespace, deploymentBuilder.build(), null, null, null, null); |
| 57 | + |
| 58 | + // Wait until example deployment is ready |
| 59 | + Wait.poll(Duration.ofSeconds(3), Duration.ofSeconds(60), () -> { |
| 60 | + try { |
| 61 | + System.out.println("Waiting until example deployment is ready..."); |
| 62 | + return appsV1Api.readNamespacedDeployment(deploymentName, namespace, null).getStatus().getReadyReplicas() > 0; |
| 63 | + } catch (ApiException e) { |
| 64 | + e.printStackTrace(); |
| 65 | + return false; |
| 66 | + } |
| 67 | + }); |
| 68 | + System.out.println("Created example deployment!"); |
| 69 | + |
| 70 | + // Trigger a rollout restart of the example deployment |
| 71 | + V1Deployment runningDeployment = appsV1Api.readNamespacedDeployment(deploymentName, namespace, null); |
| 72 | + |
| 73 | + // Explicitly set "restartedAt" annotation with current date/time to trigger rollout when patch is applied |
| 74 | + runningDeployment.getSpec().getTemplate().getMetadata().putAnnotationsItem("kubectl.kubernetes.io/restartedAt", LocalDateTime.now().toString()); |
| 75 | + try { |
| 76 | + String deploymentJson = client.getJSON().serialize(runningDeployment); |
| 77 | + |
| 78 | + PatchUtils.patch(V1Deployment.class, () -> appsV1Api.patchNamespacedDeploymentCall(deploymentName, |
| 79 | + namespace, new V1Patch(deploymentJson), null, null, "kubectl-rollout", null, null, null), |
| 80 | + V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, client); |
| 81 | + |
| 82 | + // Wait until deployment has stabilized after rollout restart |
| 83 | + Wait.poll(Duration.ofSeconds(3), Duration.ofSeconds(60), () -> { |
| 84 | + try { |
| 85 | + System.out.println("Waiting until example deployment restarted successfully..."); |
| 86 | + return appsV1Api.readNamespacedDeployment(deploymentName, namespace, null).getStatus().getReadyReplicas() > 0; |
| 87 | + } catch (ApiException e) { |
| 88 | + e.printStackTrace(); |
| 89 | + return false; |
| 90 | + } |
| 91 | + }); |
| 92 | + System.out.println("Example deployment restarted successfully!"); |
| 93 | + } catch (ApiException e) { |
| 94 | + e.printStackTrace(); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments