4462829b8526bc6af6a07f1806f97a4858846b29
[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 /**
52  *
53  * @author Waqas Ikram (waqas.ikram@est.tech)
54  *
55  */
56 @Component
57 public class MonitorHelmInstallStatusTask extends AbstractServiceTask {
58
59
60     private static final int MAX_RETRIES = 10;
61     private static final String RETRY_COUNTER_PARAM_NAME = "retryCounter";
62     private static final String IS_RESOURCE_READY_PARAM_NAME = "isResourceReady";
63     private static final Logger logger = LoggerFactory.getLogger(MonitorHelmInstallStatusTask.class);
64     private final KubernetesClientProvider kubernetesClientProvider;
65     private final KubernetesClient kubernetesClient;
66
67     @Autowired
68     protected MonitorHelmInstallStatusTask(final DatabaseServiceProvider databaseServiceProvider,
69             final KubernetesClientProvider kubernetesClientProvider, final KubernetesClient kubernetesClient) {
70         super(databaseServiceProvider);
71         this.kubernetesClientProvider = kubernetesClientProvider;
72         this.kubernetesClient = kubernetesClient;
73     }
74
75     public void updateJobStatus(final DelegateExecution execution) {
76         logger.info("Executing updateJobStatus ");
77         final String kind = (String) execution.getVariable(KIND_PARAM_NAME);
78         final String asDeploymentItemInstId = (String) execution.getVariable(AS_DEPLOYMENT_ITEM_INST_ID_PARAM_NAME);
79
80         addJobStatus(execution, JobStatusEnum.IN_PROGRESS,
81                 "Checking if resource " + kind + " is ready for asDeploymentItemInstId: " + asDeploymentItemInstId);
82
83         execution.setVariable(RETRY_COUNTER_PARAM_NAME, 0);
84
85         logger.info("Finished updateJobStatus ...");
86     }
87
88     public void isResourceReady(final DelegateExecution execution) {
89         logger.info("Executing isResourceReady ");
90         final String kind = (String) execution.getVariable(KIND_PARAM_NAME);
91         final String releaseName = (String) execution.getVariable(RELEASE_NAME_PARAM_NAME);
92         final String kubeConfigFile = (String) execution.getVariable(KUBE_CONFIG_FILE_PATH_PARAM_NAME);
93         final String labelSelector = "app.kubernetes.io/instance=" + releaseName;
94         try {
95             final ApiClient apiClient = kubernetesClientProvider.getApiClient(kubeConfigFile);
96             boolean isReady = false;
97             logger.debug("Will check if resource type: {} is ready using labelSelector: {}", kind, labelSelector);
98             switch (kind) {
99                 case KIND_JOB:
100                     isReady = kubernetesClient.isJobReady(apiClient, labelSelector);
101                     break;
102                 case KIND_POD:
103                     isReady = kubernetesClient.isPodReady(apiClient, labelSelector);
104                     break;
105                 case KIND_SERVICE:
106                     isReady = kubernetesClient.isServiceReady(apiClient, labelSelector);
107                     break;
108                 case KIND_DEPLOYMENT:
109                     isReady = kubernetesClient.isDeploymentReady(apiClient, labelSelector);
110                     break;
111                 case KIND_REPLICA_SET:
112                     isReady = kubernetesClient.isReplicaSetReady(apiClient, labelSelector);
113                     break;
114                 case KIND_DAEMON_SET:
115                     isReady = kubernetesClient.isDaemonSetReady(apiClient, labelSelector);
116                     break;
117                 case KIND_STATEFUL_SET:
118                     isReady = kubernetesClient.isStatefulSetReady(apiClient, labelSelector);
119                     break;
120
121                 default:
122                     logger.warn("Unknown resource type {} setting {} flag to true", kind, IS_RESOURCE_READY_PARAM_NAME);
123                     isReady = true;
124                     break;
125             }
126
127             logger.debug("isReady: {}", isReady);
128             execution.setVariable(IS_RESOURCE_READY_PARAM_NAME, isReady);
129
130         } catch (final KubernetesRequestTimeOut kubernetesRequestTimeOut) {
131             final Integer counter = (Integer) execution.getVariable(RETRY_COUNTER_PARAM_NAME);
132             if (counter > MAX_RETRIES) {
133                 final String message = "Retries max out for resource: " + kind;
134                 logger.error(message);
135                 abortOperation(execution, message);
136             }
137             logger.debug("Current retries counter: {} will increament and try again", counter);
138             execution.setVariable(RETRY_COUNTER_PARAM_NAME, counter + 1);
139             execution.setVariable(IS_RESOURCE_READY_PARAM_NAME, false);
140
141         } catch (final Exception exception) {
142             final String message = "Unable to preform status check for resource " + kind;
143             logger.error(message, exception);
144             abortOperation(execution, message);
145         }
146         logger.info("Finished isResourceReady ...");
147
148     }
149
150     public void checkIfOperationWasSuccessful(final DelegateExecution execution) {
151         logger.info("Executing checkIfOperationWasSuccessful ");
152
153         final String kind = (String) execution.getVariable(KIND_PARAM_NAME);
154
155         @SuppressWarnings("unchecked")
156         final Map<String, Boolean> kubeKindResult =
157                 (Map<String, Boolean>) execution.getVariable(KUBE_KINDS_RESULT_PARAM_NAME);
158
159         final boolean isReady = (boolean) execution.getVariable(IS_RESOURCE_READY_PARAM_NAME);
160         logger.debug("{} status {}", kind, isReady ? "Successful" : "failed");
161         kubeKindResult.put(kind, isReady);
162
163         execution.setVariable(KUBE_KINDS_RESULT_PARAM_NAME, kubeKindResult);
164
165         if (!isReady) {
166             final String message = "Status check failed for resource: {}" + kind;
167             logger.error(message);
168             abortOperation(execution, message);
169         }
170
171         final String asDeploymentItemInstId = (String) execution.getVariable(AS_DEPLOYMENT_ITEM_INST_ID_PARAM_NAME);
172         addJobStatus(execution, JobStatusEnum.IN_PROGRESS,
173                 "Resource " + kind + " is ready for asDeploymentItemInstId: " + asDeploymentItemInstId);
174         logger.info("Finished checkIfOperationWasSuccessful ...");
175     }
176
177     public void timeOutLogFailue(final DelegateExecution execution) {
178         logger.info("Executing timeOutLogFailue ");
179         final String message = "Is Resource ready operation timed out";
180         logger.error(message);
181         abortOperation(execution, message);
182         logger.info("Finished timeOutLogFailue ...");
183     }
184
185 }