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;
51 * @author Raviteja Karumuri (raviteja.karumuri@est.tech)
54 public class MonitorHelmUnInstallStatusTask extends AbstractServiceTask {
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;
64 protected MonitorHelmUnInstallStatusTask(final DatabaseServiceProvider databaseServiceProvider,
65 final KubernetesClientProvider kubernetesClientProvider, final KubernetesClient kubernetesClient) {
66 super(databaseServiceProvider);
67 this.kubernetesClientProvider = kubernetesClientProvider;
68 this.kubernetesClient = kubernetesClient;
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);
76 addJobStatus(execution, JobStatusEnum.IN_PROGRESS, "Checking if resource: " + kind
77 + " is terminated for asDeploymentItemInstId: " + asDeploymentItemInstId);
79 execution.setVariable(RETRY_COUNTER_PARAM_NAME, 0);
81 logger.info("Finished updateJobStatus ...");
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;
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);
96 isDeleted = kubernetesClient.isJobDeleted(apiClient, labelSelector);
99 isDeleted = kubernetesClient.isPodDeleted(apiClient, labelSelector);
102 isDeleted = kubernetesClient.isServiceDeleted(apiClient, labelSelector);
104 case KIND_DEPLOYMENT:
105 isDeleted = kubernetesClient.isDeploymentDeleted(apiClient, labelSelector);
107 case KIND_REPLICA_SET:
108 isDeleted = kubernetesClient.isReplicaSetDeleted(apiClient, labelSelector);
110 case KIND_DAEMON_SET:
111 isDeleted = kubernetesClient.isDaemonSetDeleted(apiClient, labelSelector);
113 case KIND_STATEFUL_SET:
114 isDeleted = kubernetesClient.isStatefulSetDeleted(apiClient, labelSelector);
118 logger.warn("Unknown resource type {} setting {} flag to true", kind,
119 IS_RESOURCE_DELETED_PARAM_NAME);
124 logger.debug("Resource '{}' isDeleted: {}", kind, isDeleted);
125 execution.setVariable(IS_RESOURCE_DELETED_PARAM_NAME, isDeleted);
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);
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);
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);
143 logger.info("Finished isResourceDeleted ...");
147 public void checkIfOperationWasSuccessful(final DelegateExecution execution) {
148 logger.info("Executing checkIfOperationWasSuccessful ");
150 final String kind = (String) execution.getVariable(KIND_PARAM_NAME);
152 @SuppressWarnings("unchecked")
153 final Map<String, Boolean> kubeKindResult =
154 (Map<String, Boolean>) execution.getVariable(KUBE_KINDS_RESULT_PARAM_NAME);
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);
160 execution.setVariable(KUBE_KINDS_RESULT_PARAM_NAME, kubeKindResult);
163 final String message = "Status check failed for resource: {}" + kind;
164 logger.error(message);
165 abortOperation(execution, message);
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 ...");
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 ...");