[vFW_CNF_CDS] Add workflow health-check and K8sHealthCheck.kt script
[demo.git] / heat / vFW_CNF_CDS / templates / cba / Scripts / kotlin / SimpleStatusCheck.kt
1 /*
2  * Copyright © 2021 Orange
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts
18
19 import com.fasterxml.jackson.databind.node.ObjectNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertiesService
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.K8sPluginInstanceApi
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.K8sRbInstanceStatus
25 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
26 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
27 import org.slf4j.LoggerFactory
28
29 open class SimpleStatusCheck : AbstractScriptComponentFunction() {
30
31     private val log = LoggerFactory.getLogger(SimpleStatusCheck::class.java)!!
32
33     override fun getName(): String {
34         return "SimpleStatusCheck"
35     }
36
37     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
38         log.info("SIMPLE STATUS CHECK - START")
39
40         val configValueSetup: ObjectNode = getDynamicProperties("config-deploy-setup") as ObjectNode
41
42         val bluePrintPropertiesService: BlueprintPropertiesService =
43             this.functionDependencyInstanceAsType("blueprintPropertiesService")
44
45         val k8sConfiguration = K8sConnectionPluginConfiguration(bluePrintPropertiesService)
46
47         val instanceApi = K8sPluginInstanceApi(k8sConfiguration)
48
49         var checkCount: Int = 30 // in the future to be read in from the input
50         while (checkCount > 0) {
51             var continueCheck = false
52             configValueSetup.fields().forEach { it ->
53                 val vfModuleName = it.key
54                 val instanceName = it.value.get("k8s-instance-id").asText()
55
56                 val instanceStatus: K8sRbInstanceStatus? = instanceApi.getInstanceStatus(instanceName)
57                 log.debug("Get status for $instanceName")
58                 var status = ""
59                 instanceStatus?.resourcesStatus?.forEach {
60                     log.debug("Resource: name=$it.name kind=$it.gvk.kind group=$it.gvk.group version=$it.gvk.version")
61                     if (it.gvk?.kind == "Pod") {
62                         var version = it.gvk?.version!!
63                         if (it.gvk?.group!! != "")
64                             version = "${it.gvk?.group}/$version"
65                         // val podStatus = instanceApi.queryInstanceStatus(instanceName, it.gvk?.kind!!, version, it.name, null)
66                         // log.info(podStatus.toString())
67                         val podState = it.status?.get("status") as Map<String, Object>
68                         status = podState["phase"] as String
69                         if (status != "Running") {
70                             continueCheck = true
71                             log.info("Pod ${it.name} [$vfModuleName] has INVALID state ${(podState["phase"])}")
72                         } else {
73                             log.info("Pod ${it.name} [$vfModuleName] has VALID state ${(podState["phase"])}")
74                         }
75                     }
76                 }
77             }
78             if (continueCheck) {
79                 checkCount--
80                 if (checkCount == 0)
81                     throw BlueprintException("Pods State verification failed")
82                 Thread.sleep(10000L)
83             } else
84                 checkCount = 0
85         }
86
87         log.info("SIMPLE STATUS CHECK - END SUCCESS")
88     }
89
90     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
91         log.info("Executing Recovery")
92         this.addError("${runtimeException.message}")
93     }
94 }