3f2a1bdd2170184a4fe0f8fdca33d081aae72b2b
[so/adapters/so-cnf-adapter.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.cnfm.lcm.bpmn.flows.tasks;
22
23 import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.AS_DEPLOYMENT_ITEM_INST_ID_PARAM_NAME;
24 import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.KIND_PARAM_NAME;
25 import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.KUBE_CONFIG_FILE_PATH_PARAM_NAME;
26 import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.KUBE_KINDS_RESULT_PARAM_NAME;
27 import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.RELEASE_NAME_PARAM_NAME;
28
29 import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_DAEMON_SET;
30 import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_DEPLOYMENT;
31 import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_JOB;
32 import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_POD;
33 import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_REPLICA_SET;
34 import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_SERVICE;
35 import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_STATEFUL_SET;
36
37 import io.kubernetes.client.openapi.ApiClient;
38 import java.util.Map;
39 import org.camunda.bpm.engine.delegate.DelegateExecution;
40 import org.onap.so.cnfm.lcm.bpmn.flows.exceptions.KubernetesRequestTimeOut;
41 import org.onap.so.cnfm.lcm.bpmn.flows.extclients.kubernetes.KubernetesClient;
42 import org.onap.so.cnfm.lcm.bpmn.flows.extclients.kubernetes.KubernetesClientProvider;
43 import org.onap.so.cnfm.lcm.database.beans.JobStatusEnum;
44 import org.onap.so.cnfm.lcm.database.service.DatabaseServiceProvider;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.stereotype.Component;
49
50 /**
51  * @author Raviteja Karumuri (raviteja.karumuri@est.tech)
52  */
53 @Component
54 public class MonitorHelmUnInstallStatusTask extends AbstractServiceTask {
55
56     private static final Logger logger = LoggerFactory.getLogger(MonitorHelmUnInstallStatusTask.class);
57     private static final int MAX_RETRIES = 10;
58     private static final String IS_RESOURCE_DELETED_PARAM_NAME = "isResourceDeleted";
59     private static final String RETRY_COUNTER_PARAM_NAME = "retryCounter";
60     private final KubernetesClientProvider kubernetesClientProvider;
61     private final KubernetesClient kubernetesClient;
62
63     @Autowired
64     protected MonitorHelmUnInstallStatusTask(final DatabaseServiceProvider databaseServiceProvider,
65             final KubernetesClientProvider kubernetesClientProvider, final KubernetesClient kubernetesClient) {
66         super(databaseServiceProvider);
67         this.kubernetesClientProvider = kubernetesClientProvider;
68         this.kubernetesClient = kubernetesClient;
69     }
70
71     public void updateJobStatus(final DelegateExecution execution) {
72         logger.info("Executing updateJobStatus ");
73         final String kind = (String) execution.getVariable(KIND_PARAM_NAME);
74         final String asDeploymentItemInstId = (String) execution.getVariable(AS_DEPLOYMENT_ITEM_INST_ID_PARAM_NAME);
75
76         addJobStatus(execution, JobStatusEnum.IN_PROGRESS, "Checking if resource: " + kind
77                 + " is terminated for asDeploymentItemInstId: " + asDeploymentItemInstId);
78
79         execution.setVariable(RETRY_COUNTER_PARAM_NAME, 0);
80
81         logger.info("Finished updateJobStatus ...");
82     }
83
84     public void isResourceDeleted(final DelegateExecution execution) {
85         logger.info("Executing isResourceDeleted ");
86         final String kind = (String) execution.getVariable(KIND_PARAM_NAME);
87         final String releaseName = (String) execution.getVariable(RELEASE_NAME_PARAM_NAME);
88         final String kubeConfigFile = (String) execution.getVariable(KUBE_CONFIG_FILE_PATH_PARAM_NAME);
89         final String labelSelector = "app.kubernetes.io/instance=" + releaseName;
90         try {
91             final ApiClient apiClient = kubernetesClientProvider.getApiClient(kubeConfigFile);
92             boolean isDeleted = false;
93             logger.debug("Will check if resource type: {} is Deleted using labelSelector: {}", kind, labelSelector);
94             switch (kind) {
95                 case KIND_JOB:
96                     isDeleted = kubernetesClient.isJobDeleted(apiClient, labelSelector);
97                     break;
98                 case KIND_POD:
99                     isDeleted = kubernetesClient.isPodDeleted(apiClient, labelSelector);
100                     break;
101                 case KIND_SERVICE:
102                     isDeleted = kubernetesClient.isServiceDeleted(apiClient, labelSelector);
103                     break;
104                 case KIND_DEPLOYMENT:
105                     isDeleted = kubernetesClient.isDeploymentDeleted(apiClient, labelSelector);
106                     break;
107                 case KIND_REPLICA_SET:
108                     isDeleted = kubernetesClient.isReplicaSetDeleted(apiClient, labelSelector);
109                     break;
110                 case KIND_DAEMON_SET:
111                     isDeleted = kubernetesClient.isDaemonSetDeleted(apiClient, labelSelector);
112                     break;
113                 case KIND_STATEFUL_SET:
114                     isDeleted = kubernetesClient.isStatefulSetDeleted(apiClient, labelSelector);
115                     break;
116
117                 default:
118                     logger.warn("Unknown resource type {} setting {} flag to true", kind,
119                             IS_RESOURCE_DELETED_PARAM_NAME);
120                     isDeleted = true;
121                     break;
122             }
123
124             logger.debug("Resource '{}' isDeleted: {}", kind, isDeleted);
125             execution.setVariable(IS_RESOURCE_DELETED_PARAM_NAME, isDeleted);
126
127         } catch (final KubernetesRequestTimeOut kubernetesRequestTimeOut) {
128             final Integer counter = (Integer) execution.getVariable(RETRY_COUNTER_PARAM_NAME);
129             if (counter > MAX_RETRIES) {
130                 final String message = "Retries max out for resource: " + kind;
131                 logger.error(message);
132                 abortOperation(execution, message);
133             }
134             logger.debug("Current retries counter: {} will increament and try again", counter);
135             execution.setVariable(RETRY_COUNTER_PARAM_NAME, counter + 1);
136             execution.setVariable(IS_RESOURCE_DELETED_PARAM_NAME, false);
137
138         } catch (final Exception exception) {
139             final String message = "Unable to preform delete status check for resource " + kind;
140             logger.error(message, exception);
141             abortOperation(execution, message);
142         }
143         logger.info("Finished isResourceDeleted ...");
144
145     }
146
147     public void checkIfOperationWasSuccessful(final DelegateExecution execution) {
148         logger.info("Executing checkIfOperationWasSuccessful ");
149
150         final String kind = (String) execution.getVariable(KIND_PARAM_NAME);
151
152         @SuppressWarnings("unchecked")
153         final Map<String, Boolean> kubeKindResult =
154                 (Map<String, Boolean>) execution.getVariable(KUBE_KINDS_RESULT_PARAM_NAME);
155
156         final boolean isDeleted = (boolean) execution.getVariable(IS_RESOURCE_DELETED_PARAM_NAME);
157         logger.debug("{} delete status {}", kind, isDeleted ? "Successful" : "failed");
158         kubeKindResult.put(kind, isDeleted);
159
160         execution.setVariable(KUBE_KINDS_RESULT_PARAM_NAME, kubeKindResult);
161
162         if (!isDeleted) {
163             final String message = "Status check failed for resource: {}" + kind;
164             logger.error(message);
165             abortOperation(execution, message);
166         }
167
168         final String asDeploymentItemInstId = (String) execution.getVariable(AS_DEPLOYMENT_ITEM_INST_ID_PARAM_NAME);
169         addJobStatus(execution, JobStatusEnum.IN_PROGRESS,
170                 "Resource " + kind + " is deleting for asDeploymentItemInstId: " + asDeploymentItemInstId);
171         logger.info("Finished checkIfOperationWasSuccessful ...");
172     }
173
174     public void timeOutLogFailue(final DelegateExecution execution) {
175         logger.info("Executing timeOutLogFailue ");
176         final String message = "Is Resource Deleted operation timed out";
177         logger.error(message);
178         abortOperation(execution, message);
179         logger.info("Finished timeOutLogFailue ...");
180     }
181
182 }