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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.cnfm.lcm.bpmn.flows.tasks;
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;
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;
37 import io.kubernetes.client.openapi.ApiClient;
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;
53 * @author Waqas Ikram (waqas.ikram@est.tech)
57 public class MonitorHelmInstallStatusTask extends AbstractServiceTask {
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;
68 protected MonitorHelmInstallStatusTask(final DatabaseServiceProvider databaseServiceProvider,
69 final KubernetesClientProvider kubernetesClientProvider, final KubernetesClient kubernetesClient) {
70 super(databaseServiceProvider);
71 this.kubernetesClientProvider = kubernetesClientProvider;
72 this.kubernetesClient = kubernetesClient;
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);
80 addJobStatus(execution, JobStatusEnum.IN_PROGRESS,
81 "Checking if resource " + kind + " is ready for asDeploymentItemInstId: " + asDeploymentItemInstId);
83 execution.setVariable(RETRY_COUNTER_PARAM_NAME, 0);
85 logger.info("Finished updateJobStatus ...");
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;
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);
100 isReady = kubernetesClient.isJobReady(apiClient, labelSelector);
103 isReady = kubernetesClient.isPodReady(apiClient, labelSelector);
106 isReady = kubernetesClient.isServiceReady(apiClient, labelSelector);
108 case KIND_DEPLOYMENT:
109 isReady = kubernetesClient.isDeploymentReady(apiClient, labelSelector);
111 case KIND_REPLICA_SET:
112 isReady = kubernetesClient.isReplicaSetReady(apiClient, labelSelector);
114 case KIND_DAEMON_SET:
115 isReady = kubernetesClient.isDaemonSetReady(apiClient, labelSelector);
117 case KIND_STATEFUL_SET:
118 isReady = kubernetesClient.isStatefulSetReady(apiClient, labelSelector);
122 logger.warn("Unknown resource type {} setting {} flag to true", kind, IS_RESOURCE_READY_PARAM_NAME);
127 logger.debug("isReady: {}", isReady);
128 execution.setVariable(IS_RESOURCE_READY_PARAM_NAME, isReady);
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);
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);
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);
146 logger.info("Finished isResourceReady ...");
150 public void checkIfOperationWasSuccessful(final DelegateExecution execution) {
151 logger.info("Executing checkIfOperationWasSuccessful ");
153 final String kind = (String) execution.getVariable(KIND_PARAM_NAME);
155 @SuppressWarnings("unchecked")
156 final Map<String, Boolean> kubeKindResult =
157 (Map<String, Boolean>) execution.getVariable(KUBE_KINDS_RESULT_PARAM_NAME);
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);
163 execution.setVariable(KUBE_KINDS_RESULT_PARAM_NAME, kubeKindResult);
166 final String message = "Status check failed for resource: {}" + kind;
167 logger.error(message);
168 abortOperation(execution, message);
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 ...");
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 ...");