|
| 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 io.kubernetes.client.custom.V1Patch; |
| 16 | +import io.kubernetes.client.openapi.ApiClient; |
| 17 | +import io.kubernetes.client.openapi.ApiException; |
| 18 | +import io.kubernetes.client.openapi.Configuration; |
| 19 | +import io.kubernetes.client.openapi.apis.AppsV1Api; |
| 20 | +import io.kubernetes.client.openapi.models.V1Container; |
| 21 | +import io.kubernetes.client.openapi.models.V1Deployment; |
| 22 | +import io.kubernetes.client.openapi.models.V1DeploymentBuilder; |
| 23 | +import io.kubernetes.client.openapi.models.V1DeploymentSpec; |
| 24 | +import io.kubernetes.client.openapi.models.V1LabelSelector; |
| 25 | +import io.kubernetes.client.openapi.models.V1ObjectMeta; |
| 26 | +import io.kubernetes.client.openapi.models.V1PodSpec; |
| 27 | +import io.kubernetes.client.openapi.models.V1PodTemplateSpec; |
| 28 | +import io.kubernetes.client.util.Config; |
| 29 | +import io.kubernetes.client.util.PatchUtils; |
| 30 | +import io.kubernetes.client.util.wait.Wait; |
| 31 | +import java.io.IOException; |
| 32 | +import java.time.Duration; |
| 33 | +import java.time.LocalDateTime; |
| 34 | +import java.util.Collections; |
| 35 | + |
| 36 | +public class DeployRolloutRestartExample { |
| 37 | + public static void main(String[] args) throws IOException, ApiException { |
| 38 | + ApiClient client = Config.defaultClient(); |
| 39 | + Configuration.setDefaultApiClient(client); |
| 40 | + AppsV1Api appsV1Api = new AppsV1Api(client); |
| 41 | + |
| 42 | + String deploymentName = "example-nginx"; |
| 43 | + String imageName = "nginx:1.21.6"; |
| 44 | + String namespace = "default"; |
| 45 | + |
| 46 | + // Create an example deployment |
| 47 | + V1DeploymentBuilder deploymentBuilder = |
| 48 | + new V1DeploymentBuilder() |
| 49 | + .withApiVersion("apps/v1") |
| 50 | + .withKind("Deployment") |
| 51 | + .withMetadata(new V1ObjectMeta().name(deploymentName).namespace(namespace)) |
| 52 | + .withSpec( |
| 53 | + new V1DeploymentSpec() |
| 54 | + .replicas(1) |
| 55 | + .selector(new V1LabelSelector().putMatchLabelsItem("name", deploymentName)) |
| 56 | + .template( |
| 57 | + new V1PodTemplateSpec() |
| 58 | + .metadata(new V1ObjectMeta().putLabelsItem("name", deploymentName)) |
| 59 | + .spec( |
| 60 | + new V1PodSpec() |
| 61 | + .containers( |
| 62 | + Collections.singletonList( |
| 63 | + new V1Container() |
| 64 | + .name(deploymentName) |
| 65 | + .image(imageName)))))); |
| 66 | + appsV1Api.createNamespacedDeployment( |
| 67 | + namespace, deploymentBuilder.build(), null, null, null, null); |
| 68 | + |
| 69 | + // Wait until example deployment is ready |
| 70 | + Wait.poll( |
| 71 | + Duration.ofSeconds(3), |
| 72 | + Duration.ofSeconds(60), |
| 73 | + () -> { |
| 74 | + try { |
| 75 | + System.out.println("Waiting until example deployment is ready..."); |
| 76 | + return appsV1Api |
| 77 | + .readNamespacedDeployment(deploymentName, namespace, null) |
| 78 | + .getStatus() |
| 79 | + .getReadyReplicas() |
| 80 | + > 0; |
| 81 | + } catch (ApiException e) { |
| 82 | + e.printStackTrace(); |
| 83 | + return false; |
| 84 | + } |
| 85 | + }); |
| 86 | + System.out.println("Created example deployment!"); |
| 87 | + |
| 88 | + // Trigger a rollout restart of the example deployment |
| 89 | + V1Deployment runningDeployment = |
| 90 | + appsV1Api.readNamespacedDeployment(deploymentName, namespace, null); |
| 91 | + |
| 92 | + // Explicitly set "restartedAt" annotation with current date/time to trigger rollout when patch |
| 93 | + // is applied |
| 94 | + runningDeployment |
| 95 | + .getSpec() |
| 96 | + .getTemplate() |
| 97 | + .getMetadata() |
| 98 | + .putAnnotationsItem("kubectl.kubernetes.io/restartedAt", LocalDateTime.now().toString()); |
| 99 | + try { |
| 100 | + String deploymentJson = client.getJSON().serialize(runningDeployment); |
| 101 | + |
| 102 | + PatchUtils.patch( |
| 103 | + V1Deployment.class, |
| 104 | + () -> |
| 105 | + appsV1Api.patchNamespacedDeploymentCall( |
| 106 | + deploymentName, |
| 107 | + namespace, |
| 108 | + new V1Patch(deploymentJson), |
| 109 | + null, |
| 110 | + null, |
| 111 | + "kubectl-rollout", |
| 112 | + null, |
| 113 | + null, |
| 114 | + null), |
| 115 | + V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, |
| 116 | + client); |
| 117 | + |
| 118 | + // Wait until deployment has stabilized after rollout restart |
| 119 | + Wait.poll( |
| 120 | + Duration.ofSeconds(3), |
| 121 | + Duration.ofSeconds(60), |
| 122 | + () -> { |
| 123 | + try { |
| 124 | + System.out.println("Waiting until example deployment restarted successfully..."); |
| 125 | + return appsV1Api |
| 126 | + .readNamespacedDeployment(deploymentName, namespace, null) |
| 127 | + .getStatus() |
| 128 | + .getReadyReplicas() |
| 129 | + > 0; |
| 130 | + } catch (ApiException e) { |
| 131 | + e.printStackTrace(); |
| 132 | + return false; |
| 133 | + } |
| 134 | + }); |
| 135 | + System.out.println("Example deployment restarted successfully!"); |
| 136 | + } catch (ApiException e) { |
| 137 | + e.printStackTrace(); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments