java icon indicating copy to clipboard operation
java copied to clipboard

I want to use JDK to call the rollback function of deploy, but after checking the source code, I failed. I want to implement the k8s command as an example kubectl rollout history deployment example-nginx --revision=2 -n nss

Open dengyichao opened this issue 1 year ago • 4 comments

Hello, I have written a large number of test cases on my end, but none of them were able to successfully roll back using Kubernetes Java JDK. I hope that experts can provide help. Below is my test case code and environment K8s version is: 1.21 Example of Writing: ` @Test public void rollbackDeploymentTest() { ApiClient apiClient = kubeClient.getApiClient(null); Configuration.setDefaultApiClient(apiClient); AppsV1Api appsV1Api = new AppsV1Api(apiClient);

    String deploymentName = "example-nginx";
    String namespace = "xxx-test";
    String revision = "4";

    try {
        //  Deployment
        V1Deployment deployment = appsV1Api.readNamespacedDeployment(deploymentName, namespace).execute();

        if (deployment == null) {
            System.err.println("not Deployment:" + deploymentName);
        }

        // Update Annotation, trigger rollback
        deployment.getMetadata().getAnnotations().put("deployment.kubernetes.io/revision", revision);

        // Update Deployment
        appsV1Api.replaceNamespacedDeployment(deploymentName, namespace, deployment);
        System.out.println("Rollback successful!");
    } catch (ApiException e) {
        // 打印详细错误日志
        System.err.println(" Deployment : " + e.getResponseBody());
        e.printStackTrace();
    }
}

@Test
public void test(){
    ApiClient apiClient = kubeClient.getApiClient(null);
    Configuration.setDefaultApiClient(apiClient);
    AppsV1Api appsV1Api = new AppsV1Api(apiClient);

    String deploymentName = "example-nginx";
    String namespace = "xxx-test";
    String revision = "3";

    Map<String, Object> patch = new HashMap<>();
    Map<String, Object> spec = new HashMap<>();
    Map<String, Object> rollbackTo = new HashMap<>();
    rollbackTo.put("revision", revision);
    spec.put("rollbackTo", rollbackTo);
    patch.put("spec", spec);


    String jsonPatch = JsonUtils.toJson(patch);

    try {
        V1Patch v1Patch = new V1Patch(jsonPatch);
        // Use merge patch+json format
        appsV1Api.patchNamespacedDeployment(deploymentName, namespace, v1Patch);
        PatchUtils.patch(V1Deployment.class, () -> appsV1Api.patchNamespacedDeployment(deploymentName, namespace, v1Patch).fieldManager("kubectl-rollout").buildCall(null), V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, apiClient);

    } catch (ApiException e) {
        e.printStackTrace();
    }
}`

dengyichao avatar Feb 13 '25 08:02 dengyichao

Your best bet is to review the golang source for kubectl ... and port that code to Java.

Many of the functions in kubectl are quite complicated.

brendandburns avatar Feb 14 '25 04:02 brendandburns

Alternately, if you add --v=10 to the kubectl command you can see the API calls that it is making.

brendandburns avatar Feb 14 '25 04:02 brendandburns

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot avatar May 15 '25 05:05 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle rotten
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot avatar Jun 14 '25 06:06 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

k8s-triage-robot avatar Jul 14 '25 07:07 k8s-triage-robot

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

k8s-ci-robot avatar Jul 14 '25 07:07 k8s-ci-robot