This topic describes how to use Enterprise Distributed Application Service (EDAS) SDK for Java to call an API operation to roll back an application that is deployed in a Kubernetes cluster to a historical version.
Prerequisites
The application is updated. You can use one of the following methods to update an application:
The ID of the historical version is obtained. You can call the ListHistoryDeployVersion operation to query the historical versions (PackageVersion.Id) of an application. Sample version ID: e4aeb2972cde64d5ff0618966102****.
Background information
To roll back an application to a specified historical version, you can call the DeployK8sApplication operation. The sample code that is provided in this topic is simple. If your business requires more advanced code to deploy applications, see DeployK8sApplication.
Roll back an application to a historical version
The following sample 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 pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in EDAS is a high-risk operation. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
// In this sample code, the AccessKey ID and AccessKey secret are included in the environment variables. You can save your AccessKey ID and AccessKey secret to your configuration file.
// To prevent key leaks, we recommend that you do not include your AccessKey ID and AccessKey secret in the code.
String aliyun_user_ak = System.getenv("ACCESS_KEY_ID");
String aliyun_user_sk = System.getenv("ACCESS_KEY_SECRET");
// The ID of the region where the application resides.
String region_id = "cn-hangzhou";
DefaultProfile defaultProfile = DefaultProfile.getProfile(region_id, aliyun_user_ak, aliyun_user_sk);
DefaultAcsClient client = new DefaultAcsClient(defaultProfile);
// Initiate an API request and configure the related 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 returned after you call the ListHistoryDeployVersion operation.
request.setPackageVersionId("e4aeb2972cde64d5ff0618966102****");
// The number of pods.
request.setReplicas(4);
// The CPU quota and the memory quota of the pods. The value 0 indicates that no limits are specified.
request.setCpuLimit(0);
request.setMemoryLimit(0);
request.setCpuRequest(0);
request.setMemoryRequest(0);
// The minimum number of CPU cores that are required. Unit: cores. The value 0 indicates that the minimum number of CPU cores has no limit.
request.setMcpuRequest(0);
// The maximum number of CPU cores that are allowed. Unit: cores. The value 0 indicates that the maximum number of CPU cores has no limit.
request.setMcpuLimit(0);
// The description of the change record.
request.setChangeOrderDesc("Roll back 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
Check the result
After you roll back the application to a historical version, you can call the GetChangeOrderInfo operation to view the change details and change status of the application. For more information about specific API parameters, see GetChangeOrderInfo.
Sample returned response after you call the GetChangeOrderInfo operation:
{
"Message": "success",
"RequestId": "3E9C6941-3945-4D43-9CBE-12CAB45569AA",
"Code": 200,
"changeOrderInfo": {
"Status": 2,
"Desc": "Roll back 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.
To check whether the application is rolled back to the specified historical version, you can view the value of the changeOrderInfo.Status parameter. Valid values of the changeOrderInfo.Status parameter:
0: Ready
1: In progress
2: Successful
3: Failed
6: Terminated
8: Waiting to manually confirm the next phase during a manual phased-release
9: Waiting to automatically confirm the next phase during an automatic phased-release
10: Failed due to a system exception
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.