blob: 04f0a836cd6df35d32d93ce630c9ec71f609e528 [file] [log] [blame]
rameshiyer27a1954d42022-11-14 06:00:12 +00001#!/bin/bash
2#
3# ============LICENSE_START====================================================
rameshiyer27f2e4da72024-01-13 21:26:09 +00004# Copyright (C) 2022-2024 Nordix Foundation.
rameshiyer27a1954d42022-11-14 06:00:12 +00005# =============================================================================
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18# SPDX-License-Identifier: Apache-2.0
19# ============LICENSE_END======================================================
20
rameshiyer2765fc3752023-04-05 13:47:15 +010021# This script spins up kubernetes cluster in Microk8s for deploying policy helm charts.
22# Runs CSITs in kubernetes.
rameshiyer2707004862023-01-23 11:50:01 +000023
rameshiyer27f575f922023-10-12 15:11:02 +010024WORKSPACE=$(git rev-parse --show-toplevel)
25export WORKSPACE
adheli.tavares1153b6d2023-02-28 12:59:37 +000026
rameshiyer2765fc3752023-04-05 13:47:15 +010027export GERRIT_BRANCH=$(awk -F= '$1 == "defaultbranch" { print $2 }' "${WORKSPACE}"/.gitreview)
rameshiyer27d8f91802023-03-07 01:32:28 +000028
rameshiyer2765fc3752023-04-05 13:47:15 +010029CSIT_SCRIPT="scripts/run-test.sh"
30ROBOT_DOCKER_IMAGE="policy-csit-robot"
31POLICY_CLAMP_ROBOT="policy-clamp-test.robot"
32POLICY_API_ROBOT="api-test.robot api-slas.robot"
33POLICY_PAP_ROBOT="pap-test.robot pap-slas.robot"
34POLICY_APEX_PDP_ROBOT="apex-pdp-test.robot apex-slas.robot"
35POLICY_XACML_PDP_ROBOT="xacml-pdp-test.robot"
36POLICY_DROOLS_PDP_ROBOT="drools-pdp-test.robot"
37POLICY_DISTRIBUTION_ROBOT="distribution-test.robot"
rameshiyer2707004862023-01-23 11:50:01 +000038
rameshiyer2765fc3752023-04-05 13:47:15 +010039POLICY_API_CONTAINER="policy-api"
40POLICY_PAP_CONTAINER="policy-pap"
41POLICY_CLAMP_CONTAINER="policy-clamp-runtime-acm"
42POLICY_APEX_CONTAINER="policy-apex-pdp"
43POLICY_DROOLS_CONTAINER="policy-drools-pdp"
44POLICY_XACML_CONTAINER="policy-xacml-pdp"
45POLICY_DISTRIBUTION_CONTAINER="policy-distribution"
46POLICY_K8S_PPNT_CONTAINER="policy-clamp-ac-k8s-ppnt"
47POLICY_HTTP_PPNT_CONTAINER="policy-clamp-ac-http-ppnt"
48POLICY_PF_PPNT_CONTAINER="policy-clamp-ac-pf-ppnt"
rameshiyer2731c61d42024-01-21 14:24:03 +000049KAFKA_CONTAINER="kafka-deployment"
50ZK_CONTAINER="zookeeper-deployment"
51KAFKA_DIR=${WORKSPACE}/helm/cp-kafka
rameshiyer2765fc3752023-04-05 13:47:15 +010052SET_VALUES=""
rameshiyer27a1954d42022-11-14 06:00:12 +000053
rameshiyer2765fc3752023-04-05 13:47:15 +010054DISTRIBUTION_CSAR=${WORKSPACE}/csit/resources/tests/data/csar
55DIST_TEMP_FOLDER=/tmp/distribution
rameshiyer27a1954d42022-11-14 06:00:12 +000056
rameshiyer2765fc3752023-04-05 13:47:15 +010057export PROJECT=""
58export ROBOT_FILE=""
59export ROBOT_LOG_DIR=${WORKSPACE}/csit/archives
60export READINESS_CONTAINERS=()
61
rameshiyer2731c61d42024-01-21 14:24:03 +000062
rameshiyer2765fc3752023-04-05 13:47:15 +010063function spin_microk8s_cluster() {
64 echo "Verify if Microk8s cluster is running.."
65 microk8s version
66 exitcode="${?}"
67
68 if [ "$exitcode" -ne 0 ]; then
69 echo "Microk8s cluster not available, Spinning up the cluster.."
70 sudo snap install microk8s --classic --channel=1.26/stable
71
72 if [ "${?}" -ne 0 ]; then
73 echo "Failed to install kubernetes cluster. Aborting.."
74 return 1
75 fi
76 echo "Microk8s cluster installed successfully"
77 sudo usermod -a -G microk8s $USER
78 echo "Enabling DNS and helm3 plugins"
79 sudo microk8s.enable dns helm3 hostpath-storage
80 echo "Creating configuration file for Microk8s"
81 sudo mkdir -p $HOME/.kube
82 sudo chown -R $USER:$USER $HOME/.kube
83 sudo microk8s kubectl config view --raw >$HOME/.kube/config
84 sudo chmod 600 $HOME/.kube/config
85 echo "K8s installation completed"
86 echo "----------------------------------------"
87 else
88 echo "K8s cluster is already running"
89 echo "----------------------------------------"
90 return 0
91 fi
92
93}
94
rameshiyer2731c61d42024-01-21 14:24:03 +000095function install_kafka() {
96 echo "Installing Confluent kafka"
97 kubectl apply -f $KAFKA_DIR/zookeeper.yaml
98 kubectl apply -f $KAFKA_DIR/kafka.yaml
99 echo "----------------------------------------"
100}
101
rameshiyer2765fc3752023-04-05 13:47:15 +0100102function uninstall_policy() {
103 echo "Removing the policy helm deployment"
104 sudo microk8s helm uninstall csit-policy
105 sudo microk8s helm uninstall prometheus
106 sudo microk8s helm uninstall csit-robot
rameshiyer2731c61d42024-01-21 14:24:03 +0000107 sudo kubectl delete deploy $ZK_CONTAINER $KAFKA_CONTAINER
rameshiyer2765fc3752023-04-05 13:47:15 +0100108 rm -rf ${WORKSPACE}/helm/policy/Chart.lock
109 if [ "$PROJECT" == "clamp" ] || [ "$PROJECT" == "policy-clamp" ]; then
110 sudo microk8s helm uninstall policy-chartmuseum
111 sudo microk8s helm repo remove chartmuseum-git policy-chartmuseum
112 fi
113 sudo rm -rf /dockerdata-nfs/mariadb-galera/
114 sudo microk8s kubectl delete pvc --all
115 echo "Policy deployment deleted"
116 echo "Clean up docker"
117 docker image prune -f
118}
119
120function teardown_cluster() {
121 echo "Removing k8s cluster and k8s configuration file"
122 sudo snap remove microk8s;rm -rf $HOME/.kube/config
123 echo "MicroK8s Cluster removed"
124}
125
126function build_robot_image() {
127 echo "Build docker image for robot framework"
128 cd ${WORKSPACE}/csit/resources || exit
129 clone_models
130 if [ "${PROJECT}" == "distribution" ] || [ "${PROJECT}" == "policy-distribution" ]; then
131 copy_csar_file
132 fi
133 echo "Build robot framework docker image"
134 docker login -u docker -p docker nexus3.onap.org:10001
135 docker build . --file Dockerfile \
136 --build-arg CSIT_SCRIPT="$CSIT_SCRIPT" \
137 --build-arg ROBOT_FILE="$ROBOT_FILE" \
138 --tag "${ROBOT_DOCKER_IMAGE}" --no-cache
139 echo "---------------------------------------------"
140}
141
142function start_csit() {
143 build_robot_image
144 if [ "${?}" -eq 0 ]; then
145 echo "Importing robot image into microk8s registry"
146 docker save -o policy-csit-robot.tar ${ROBOT_DOCKER_IMAGE}:latest
147 sudo microk8s ctr image import policy-csit-robot.tar
148 rm -rf ${WORKSPACE}/csit/resources/policy-csit-robot.tar
149 rm -rf ${WORKSPACE}/csit/resources/tests/models/
150 echo "---------------------------------------------"
151 if [ "$PROJECT" == "clamp" ] || [ "$PROJECT" == "policy-clamp" ]; then
152 POD_READY_STATUS="0/1"
153 while [[ ${POD_READY_STATUS} != "1/1" ]]; do
154 echo "Waiting for chartmuseum pod to come up..."
155 sleep 5
156 POD_READY_STATUS=$(sudo microk8s kubectl get pods | grep -e "policy-chartmuseum" | awk '{print $2}')
157 done
158 push_acelement_chart
159 fi
160 echo "Installing Robot framework pod for running CSIT"
161 cd ${WORKSPACE}/helm
162 mkdir -p ${ROBOT_LOG_DIR}
163 sudo microk8s helm install csit-robot robot --set robot="$ROBOT_FILE" --set "readiness={${READINESS_CONTAINERS[*]}}" --set robotLogDir=$ROBOT_LOG_DIR
164 print_robot_log
rameshiyer2765fc3752023-04-05 13:47:15 +0100165 fi
166}
167
168function print_robot_log() {
169 count_pods=0
170 while [[ ${count_pods} -eq 0 ]]; do
171 echo "Waiting for pods to come up..."
172 sleep 5
173 count_pods=$(sudo microk8s kubectl get pods --output name | wc -l)
174 done
175 robotpod=$(sudo microk8s kubectl get po | grep policy-csit)
176 podName=$(echo "$robotpod" | awk '{print $1}')
177 echo "The robot tests will begin once the policy components {${READINESS_CONTAINERS[*]}} are up and running..."
178 sudo microk8s kubectl wait --for=jsonpath='{.status.phase}'=Running --timeout=10m pod/"$podName"
179 echo "Policy deployment status:"
180 sudo microk8s kubectl get po
181 sudo microk8s kubectl get all -A
182 echo "Robot Test logs:"
183 sudo microk8s kubectl logs -f "$podName"
184}
185
186function clone_models() {
187
188 # download models examples
189 git clone -b "${GERRIT_BRANCH}" --single-branch https://github.com/onap/policy-models.git "${WORKSPACE}"/csit/resources/tests/models
190
191 # create a couple of variations of the policy definitions
192 sed -e 's!Measurement_vGMUX!ADifferentValue!' \
193 tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.json \
194 >tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.v1_2.json
195
196 sed -e 's!"version": "1.0.0"!"version": "2.0.0"!' \
197 -e 's!"policy-version": 1!"policy-version": 2!' \
198 tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.json \
199 >tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.v2.json
200}
201
202function copy_csar_file() {
203 zip -F ${DISTRIBUTION_CSAR}/sample_csar_with_apex_policy.csar \
204 --out ${DISTRIBUTION_CSAR}/csar_temp.csar -q
205 # Remake temp directory
206 sudo rm -rf "${DIST_TEMP_FOLDER}"
207 sudo mkdir "${DIST_TEMP_FOLDER}"
208 sudo cp ${DISTRIBUTION_CSAR}/csar_temp.csar ${DISTRIBUTION_CSAR}/temp.csar
209 sudo mv ${DISTRIBUTION_CSAR}/temp.csar ${DIST_TEMP_FOLDER}/sample_csar_with_apex_policy.csar
210}
211
212function set_project_config() {
213 echo "Setting project configuration for: $PROJECT"
214 case $PROJECT in
215
216 clamp | policy-clamp)
217 export ROBOT_FILE=$POLICY_CLAMP_ROBOT
218 export READINESS_CONTAINERS=($POLICY_CLAMP_CONTAINER,$POLICY_APEX_CONTAINER,$POLICY_PF_PPNT_CONTAINER,$POLICY_K8S_PPNT_CONTAINER,
219 $POLICY_HTTP_PPNT_CONTAINER)
220 export SET_VALUES="--set $POLICY_CLAMP_CONTAINER.enabled=true --set $POLICY_APEX_CONTAINER.enabled=true
221 --set $POLICY_PF_PPNT_CONTAINER.enabled=true --set $POLICY_K8S_PPNT_CONTAINER.enabled=true --set $POLICY_HTTP_PPNT_CONTAINER.enabled=true"
222 install_chartmuseum
223 ;;
224
225 api | policy-api)
226 export ROBOT_FILE=$POLICY_API_ROBOT
227 export READINESS_CONTAINERS=($POLICY_API_CONTAINER)
228 ;;
229
230 pap | policy-pap)
231 export ROBOT_FILE=$POLICY_PAP_ROBOT
232 export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_PAP_CONTAINER,$POLICY_API_CONTAINER,$POLICY_XACML_CONTAINER)
233 export SET_VALUES="--set $POLICY_APEX_CONTAINER.enabled=true --set $POLICY_XACML_CONTAINER.enabled=true"
234 ;;
235
236 apex-pdp | policy-apex-pdp)
237 export ROBOT_FILE=$POLICY_APEX_PDP_ROBOT
238 export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER)
239 export SET_VALUES="--set $POLICY_APEX_CONTAINER.enabled=true"
240 ;;
241
242 xacml-pdp | policy-xacml-pdp)
243 export ROBOT_FILE=($POLICY_XACML_PDP_ROBOT)
244 export READINESS_CONTAINERS=($POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER,$POLICY_XACML_CONTAINER)
245 export SET_VALUES="--set $POLICY_XACML_CONTAINER.enabled=true"
246 ;;
247
248 drools-pdp | policy-drools-pdp)
249 export ROBOT_FILE=($POLICY_DROOLS_PDP_ROBOT)
250 export READINESS_CONTAINERS=($POLICY_DROOLS_CONTAINER)
251 export SET_VALUES="--set $POLICY_DROOLS_CONTAINER.enabled=true"
252 ;;
253
254 distribution | policy-distribution)
255 export ROBOT_FILE=($POLICY_DISTRIBUTION_ROBOT)
256 export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER,
257 $POLICY_DISTRIBUTION_CONTAINER)
258 export SET_VALUES="--set $POLICY_APEX_CONTAINER.enabled=true --set $POLICY_DISTRIBUTION_CONTAINER.enabled=true"
259 ;;
260
261 *)
262 echo "Unknown project supplied. Enabling all policy charts for the deployment"
saul.gilld589fbb2023-10-20 16:32:57 +0100263 export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER,
264 $POLICY_DISTRIBUTION_CONTAINER,$POLICY_DROOLS_CONTAINER,$POLICY_XACML_CONTAINER,
265 $POLICY_CLAMP_CONTAINER,$POLICY_PF_PPNT_CONTAINER,$POLICY_K8S_PPNT_CONTAINER,
266 $POLICY_HTTP_PPNT_CONTAINER)
rameshiyer2765fc3752023-04-05 13:47:15 +0100267 export SET_VALUES="--set $POLICY_APEX_CONTAINER.enabled=true --set $POLICY_XACML_CONTAINER.enabled=true
rameshiyer27f2e4da72024-01-13 21:26:09 +0000268 --set $POLICY_DISTRIBUTION_CONTAINER.enabled=true --set $POLICY_DROOLS_CONTAINER.enabled=true
rameshiyer2765fc3752023-04-05 13:47:15 +0100269 --set $POLICY_CLAMP_CONTAINER.enabled=true --set $POLICY_PF_PPNT_CONTAINER.enabled=true
270 --set $POLICY_K8S_PPNT_CONTAINER.enabled=true --set $POLICY_HTTP_PPNT_CONTAINER.enabled=true"
271 ;;
272 esac
273
274}
275
276function install_chartmuseum () {
277 echo "Installing Chartmuseum helm repository..."
278 sudo microk8s helm repo add chartmuseum-git https://chartmuseum.github.io/charts
279 sudo microk8s helm repo update
280 sudo microk8s helm install policy-chartmuseum chartmuseum-git/chartmuseum --set env.open.DISABLE_API=false --set service.type=NodePort --set service.nodePort=30208
281 sudo microk8s helm plugin install https://github.com/chartmuseum/helm-push
282 echo "---------------------------------------------"
283}
284
285function push_acelement_chart() {
286 echo "Pushing acelement chart to the chartmuseum repo..."
287 sudo microk8s helm repo add policy-chartmuseum http://localhost:30208
288
289 # download clamp repo
290 git clone -b "${GERRIT_BRANCH}" --single-branch https://github.com/onap/policy-clamp.git "${WORKSPACE}"/csit/resources/tests/clamp
291 ACELEMENT_CHART=${WORKSPACE}/csit/resources/tests/clamp/examples/src/main/resources/clamp/acm/acelement-helm/acelement
292 sudo microk8s helm cm-push $ACELEMENT_CHART policy-chartmuseum
293 sudo microk8s helm repo update
294 rm -rf ${WORKSPACE}/csit/resources/tests/clamp/
295 echo "-------------------------------------------"
296}
rameshiyer27a1954d42022-11-14 06:00:12 +0000297
saul.gilld589fbb2023-10-20 16:32:57 +0100298function get_pod_name() {
299 microk8s kubectl get pods --no-headers -o custom-columns=':metadata.name' | grep $1
300}
301
302wait_for_pods_running() {
303 local namespace="$1"
304 shift
305 local timeout_seconds="$1"
306 shift
307
308 IFS=',' read -ra pod_names <<< "$1"
309 shift
310
311 local pending_pods=("${pod_names[@]}")
312
313 local start_time
314 start_time=$(date +%s)
315
316 while [ ${#pending_pods[@]} -gt 0 ]; do
317 local current_time
318 current_time=$(date +%s)
319 local elapsed_time
320 elapsed_time=$((current_time - start_time))
321
322 if [ "$elapsed_time" -ge "$timeout_seconds" ]; then
323 echo "Timed out waiting for all pods to reach 'Running' state."
324 exit 1
325 fi
326
327 local newly_running_pods=()
328
329 for pod_name_prefix in "${pending_pods[@]}"; do
330 local pod_name=$(get_pod_name "$pod_name_prefix")
331 local pod_status
332 pod_status=$(kubectl get pod "$pod_name" -n "$namespace" --no-headers -o custom-columns=STATUS:.status.phase 2>/dev/null)
333
334 if [ "$pod_status" == "Running" ]; then
335 echo "Pod '$pod_name' in namespace '$namespace' is now in 'Running' state."
336 else
337 newly_running_pods+=("$pod_name")
338 echo "Waiting for pod '$pod_name' in namespace '$namespace' to reach 'Running' state..."
339 fi
340 done
341
342 pending_pods=("${newly_running_pods[@]}")
343
344 sleep 5
345 done
346
347 echo "All specified pods are in the 'Running' state. Exiting the function."
348}
349
rameshiyer2707004862023-01-23 11:50:01 +0000350
rameshiyer2765fc3752023-04-05 13:47:15 +0100351OPERATION="$1"
352PROJECT="$2"
rameshiyer27bdced492023-06-28 12:15:14 +0100353if [ -z "$3" ]
354then
355 LOCALIMAGE="false"
356else
357 LOCALIMAGE="$3"
358fi
359
rameshiyer27a1954d42022-11-14 06:00:12 +0000360
rameshiyer2765fc3752023-04-05 13:47:15 +0100361if [ $OPERATION == "install" ]; then
362 spin_microk8s_cluster
363 if [ "${?}" -eq 0 ]; then
rameshiyer2731c61d42024-01-21 14:24:03 +0000364 export KAFKA_CONTAINERS=($KAFKA_CONTAINER,$ZK_CONTAINER)
365 install_kafka
366 wait_for_pods_running default 300 $KAFKA_CONTAINERS
rameshiyer2765fc3752023-04-05 13:47:15 +0100367 set_project_config
368 echo "Installing policy helm charts in the default namespace"
369 source ${WORKSPACE}/compose/get-k8s-versions.sh
rameshiyer27bdced492023-06-28 12:15:14 +0100370 if [ $LOCALIMAGE == "true" ]; then
371 echo "loading local image"
372 source ${WORKSPACE}/compose/get-versions.sh
373 ${WORKSPACE}/compose/loaddockerimage.sh
374 fi
rameshiyer2765fc3752023-04-05 13:47:15 +0100375 cd ${WORKSPACE}/helm || exit
376 sudo microk8s helm dependency build policy
377 sudo microk8s helm install csit-policy policy ${SET_VALUES}
378 sudo microk8s helm install prometheus prometheus
saul.gilld589fbb2023-10-20 16:32:57 +0100379 wait_for_pods_running default 300 $READINESS_CONTAINERS
rameshiyer2765fc3752023-04-05 13:47:15 +0100380 echo "Policy chart installation completed"
381 echo "-------------------------------------------"
382 fi
383
384 if [ "$PROJECT" ]; then
385 export ROBOT_LOG_DIR=${WORKSPACE}/csit/archives/${PROJECT}
386 echo "CSIT will be invoked from $ROBOT_FILE"
387 echo "Readiness containers: ${READINESS_CONTAINERS[*]}"
388 echo "-------------------------------------------"
389 start_csit
390 else
391 echo "No project supplied for running CSIT"
392 fi
393
394elif [ $OPERATION == "uninstall" ]; then
395 uninstall_policy
396
397elif [ $OPERATION == "clean" ]; then
398 teardown_cluster
399
400else
rameshiyer27bdced492023-06-28 12:15:14 +0100401 echo "Invalid arguments provided. Usage: $0 [options..] {install {project_name} | uninstall | clean} {uselocalimage = true/false}"
rameshiyer2765fc3752023-04-05 13:47:15 +0100402fi