This topic describes how to use Enterprise Distributed Application Service (EDAS) SDK for Java to roll back an application deployed in a Kubernetes cluster to a historical version by calling an API operation.

Prerequisites

Before you roll back an application to a historical version, make sure that the following prerequisites are met:

Background information

To roll back an application to a historical version, you can call the DeployK8sApplication to deploy the historical version. This topic provides sample code on how to deploy a historical version of an application. For more information about how to deploy an application, see DeployK8sApplication.

Roll back an application to a historical version

The following code provides an example on how to roll back an application to a historical version:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.edas.model.v20170801.DeployK8sApplicationRequest;
import com.aliyuncs.edas.model.v20170801.DeployK8sApplicationResponse;

public class DeployK8sApplication {

    public static void main(String[] args)  {
        // The AccessKey ID of your Alibaba Cloud account or a RAM user within the Alibaba Cloud account. 
        String aliyun_user_ak = "<yourAccessKeyId>";
        // The AccessKey secret of your Alibaba Cloud account or a RAM user within the Alibaba Cloud account. 
        String aliyun_user_sk = "<yourAccessKeySecret>";
        // The ID of the region in which the application to be rolled back resides. 
        String region_id = "cn-hangzhou";

        DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
        DefaultAcsClient client = new DefaultAcsClient(defaultProfile);

        // Create an API request and set the parameters. 
        DeployK8sApplicationRequest request = new DeployK8sApplicationRequest();
        // The ID of the application. 
        request.setAppId("6bbc57a2-a017-4bec-b521-49a15bd3****");
        // The value of the PackageVersion.Id parameter that is queried by calling the ListHistoryDeployVersion operation. 
        request.setPackageVersionId("e4aeb2972cde64d5ff0618966102****");

        // The number of instances for the application. 
        request.setReplicas(4);
        // The CPU quota and memory quota for application instances. A value of 0 indicates that no quotas are set. 
        request.setCpuLimit(0);
        request.setMemoryLimit(0);
        request.setCpuRequest(0);
        request.setMemoryRequest(0);
        // The minimum number of CPU cores required. Unit: cores. A value of 0 indicates that no quota is set. 
        request.setMcpuRequest(0);
        // The maximum number of CPU cores allowed. Unit: cores. A value of 0 indicates that no quota is set. 
        request.setMcpuLimit(0);

        // The description of the change record. 
        request.setChangeOrderDesc("Rollback to a historical version");

        try {
            DeployK8sApplicationResponse response = client.getAcsResponse(request);
            System.out.println("ChangeOrderId=" + response.getChangeOrderId() + "\nMessage=" + response.getMessage());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
Sample response:
ChangeOrderId=1b0bec74-7ec3-43f7-b9c8-f8e746d2****
Message=success

Verify the result

After you roll back the application to a historical version, you can call the GetChangeOrderInfo operation to view the change details and obtain the change status of the application. For more information about the API operation, see GetChangeOrderInfo.

Sample response that is returned after you call the GetChangeOrderInfo operation:
{
  "Message": "success",
  "RequestId": "3E9C6941-3945-4D43-9CBE-12CAB45569AA",
  "Code": 200,
  "changeOrderInfo": {
    "Status": 2,
    "Desc": "Rollback to a historical version",
    "PipelineInfoList": {
      "PipelineInfo": [
        {
          "PipelineStatus": 2,
          "PipelineName": "Batch 1 Change",
           ...... 
           "TaskMessage": "Apply success. <br>application is ready at desired state, version: 22"
           ......
           // The sample response is incomplete and is only for reference. 
You can view the value of the changeOrderInfo.Status parameter to check whether the application is rolled back to a historical version. Valid values of the changeOrderInfo.Status parameter:
  • 0: ready
  • 1: in progress
  • 2: successful
  • 3: failed
  • 6: terminated
  • 8: wait for manual confirmation to trigger the next phase during a manual phased rollback
  • 9: wait to trigger the next phase during an automatic phased rollback
  • 10: failed due to a system exception
Note If a value of 3 is returned for the changeOrderInfo.Status parameter, the application fails to be rolled back to a historical version. To find the cause of the failure, check the value of the TaskMessage parameter. After the issue is fixed, roll back the application to a historical version again.