Search in sources :

Example 11 with V1Deployment

use of io.kubernetes.client.openapi.models.V1Deployment in project java by kubernetes-client.

the class AppsV1Api method readNamespacedDeploymentStatusAsync.

/**
 * (asynchronously) read status of the specified Deployment
 *
 * @param name name of the Deployment (required)
 * @param namespace object name and auth scope, such as for teams and projects (required)
 * @param pretty If 'true', then the output is pretty printed. (optional)
 * @param _callback The callback to be executed when the API call finishes
 * @return The request call
 * @throws ApiException If fail to process the API call, e.g. serializing the request body object
 * @http.response.details
 *     <table summary="Response Details" border="1">
 * <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
 * <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
 * <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
 * </table>
 */
public okhttp3.Call readNamespacedDeploymentStatusAsync(String name, String namespace, String pretty, final ApiCallback<V1Deployment> _callback) throws ApiException {
    okhttp3.Call localVarCall = readNamespacedDeploymentStatusValidateBeforeCall(name, namespace, pretty, _callback);
    Type localVarReturnType = new TypeToken<V1Deployment>() {
    }.getType();
    localVarApiClient.executeAsync(localVarCall, localVarReturnType, _callback);
    return localVarCall;
}
Also used : V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) Type(java.lang.reflect.Type)

Example 12 with V1Deployment

use of io.kubernetes.client.openapi.models.V1Deployment in project java by kubernetes-client.

the class AppsV1Api method replaceNamespacedDeploymentStatusAsync.

/**
 * (asynchronously) replace status of the specified Deployment
 *
 * @param name name of the Deployment (required)
 * @param namespace object name and auth scope, such as for teams and projects (required)
 * @param body (required)
 * @param pretty If &#39;true&#39;, then the output is pretty printed. (optional)
 * @param dryRun When present, indicates that modifications should not be persisted. An invalid or
 *     unrecognized dryRun directive will result in an error response and no further processing of
 *     the request. Valid values are: - All: all dry run stages will be processed (optional)
 * @param fieldManager fieldManager is a name associated with the actor or entity that is making
 *     these changes. The value must be less than or 128 characters long, and only contain
 *     printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. (optional)
 * @param fieldValidation fieldValidation determines how the server should respond to
 *     unknown/duplicate fields in the object in the request. Introduced as alpha in 1.23, older
 *     servers or servers with the &#x60;ServerSideFieldValidation&#x60; feature disabled will
 *     discard valid values specified in this param and not perform any server side field
 *     validation. Valid values are: - Ignore: ignores unknown/duplicate fields. - Warn: responds
 *     with a warning for each unknown/duplicate field, but successfully serves the request. -
 *     Strict: fails the request on unknown/duplicate fields. (optional)
 * @param _callback The callback to be executed when the API call finishes
 * @return The request call
 * @throws ApiException If fail to process the API call, e.g. serializing the request body object
 * @http.response.details
 *     <table summary="Response Details" border="1">
 * <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
 * <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
 * <tr><td> 201 </td><td> Created </td><td>  -  </td></tr>
 * <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
 * </table>
 */
public okhttp3.Call replaceNamespacedDeploymentStatusAsync(String name, String namespace, V1Deployment body, String pretty, String dryRun, String fieldManager, String fieldValidation, final ApiCallback<V1Deployment> _callback) throws ApiException {
    okhttp3.Call localVarCall = replaceNamespacedDeploymentStatusValidateBeforeCall(name, namespace, body, pretty, dryRun, fieldManager, fieldValidation, _callback);
    Type localVarReturnType = new TypeToken<V1Deployment>() {
    }.getType();
    localVarApiClient.executeAsync(localVarCall, localVarReturnType, _callback);
    return localVarCall;
}
Also used : V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) Type(java.lang.reflect.Type)

Example 13 with V1Deployment

use of io.kubernetes.client.openapi.models.V1Deployment 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 14 with V1Deployment

use of io.kubernetes.client.openapi.models.V1Deployment 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 15 with V1Deployment

use of io.kubernetes.client.openapi.models.V1Deployment in project java by kubernetes-client.

the class ExpandedExample method scaleDeployment.

/**
 * Scale up/down the number of pod in Deployment
 *
 * @param deploymentName
 * @param numberOfReplicas
 * @throws ApiException
 */
public static void scaleDeployment(String deploymentName, int numberOfReplicas) throws ApiException {
    AppsV1Api appsV1Api = new AppsV1Api();
    appsV1Api.setApiClient(COREV1_API.getApiClient());
    V1DeploymentList listNamespacedDeployment = appsV1Api.listNamespacedDeployment(DEFAULT_NAME_SPACE, null, null, null, null, null, null, null, null, null, Boolean.FALSE);
    List<V1Deployment> appsV1DeploymentItems = listNamespacedDeployment.getItems();
    Optional<V1Deployment> findedDeployment = appsV1DeploymentItems.stream().filter((V1Deployment deployment) -> deployment.getMetadata().getName().equals(deploymentName)).findFirst();
    findedDeployment.ifPresent((V1Deployment deploy) -> {
        try {
            V1DeploymentSpec newSpec = deploy.getSpec().replicas(numberOfReplicas);
            V1Deployment newDeploy = deploy.spec(newSpec);
            appsV1Api.replaceNamespacedDeployment(deploymentName, DEFAULT_NAME_SPACE, newDeploy, null, null, null, null);
        } catch (ApiException ex) {
            LOGGER.warn("Scale the pod failed for Deployment:" + deploymentName, ex);
        }
    });
}
Also used : V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) V1DeploymentSpec(io.kubernetes.client.openapi.models.V1DeploymentSpec) V1DeploymentList(io.kubernetes.client.openapi.models.V1DeploymentList) ApiException(io.kubernetes.client.openapi.ApiException)

Aggregations

V1Deployment (io.kubernetes.client.openapi.models.V1Deployment)23 Type (java.lang.reflect.Type)14 AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)7 ApiException (io.kubernetes.client.openapi.ApiException)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 V1Patch (io.kubernetes.client.custom.V1Patch)2 V1DeploymentSpec (io.kubernetes.client.openapi.models.V1DeploymentSpec)2 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)2 V1PodTemplateSpec (io.kubernetes.client.openapi.models.V1PodTemplateSpec)2 V1ReplicaSet (io.kubernetes.client.openapi.models.V1ReplicaSet)2 SneakyThrows (lombok.SneakyThrows)2 EqualToPattern (com.github.tomakehurst.wiremock.matching.EqualToPattern)1 KubernetesType (io.kubernetes.client.common.KubernetesType)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1 JSON (io.kubernetes.client.openapi.JSON)1 V1Container (io.kubernetes.client.openapi.models.V1Container)1 V1DeploymentBuilder (io.kubernetes.client.openapi.models.V1DeploymentBuilder)1 V1DeploymentList (io.kubernetes.client.openapi.models.V1DeploymentList)1 V1LabelSelector (io.kubernetes.client.openapi.models.V1LabelSelector)1