blob: f8c566787e3320ebd363b6b2743eee22f124d3e3 [file] [log] [blame]
rameshiyer27181e6c52023-03-30 10:50:18 +01001#!/bin/bash
2#
3# ============LICENSE_START====================================================
4# Copyright (C) 2022-2023 Nordix Foundation.
5# =============================================================================
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
21# This script spins up kubernetes cluster in Microk8s for deploying policy helm charts.
22# Runs CSITs in kubernetes.
23
24if [ -z "${WORKSPACE}" ]; then
25 WORKSPACE=$(git rev-parse --show-toplevel)
26 export WORKSPACE
27fi
28
rameshiyer27e9098662023-04-03 00:57:58 +010029export GERRIT_BRANCH=$(awk -F= '$1 == "defaultbranch" { print $2 }' "${WORKSPACE}"/.gitreview)
30
rameshiyer27181e6c52023-03-30 10:50:18 +010031CSIT_SCRIPT="scripts/run-test.sh"
32ROBOT_DOCKER_IMAGE="policy-csit-robot"
33POLICY_CLAMP_ROBOT="policy-clamp-test.robot"
waynedunicane4ff7e52023-03-01 09:07:31 +000034POLICY_API_ROBOT="api-test.robot api-slas.robot"
rameshiyer27181e6c52023-03-30 10:50:18 +010035POLICY_PAP_ROBOT="pap-test.robot pap-slas.robot"
waynedunicane4ff7e52023-03-01 09:07:31 +000036POLICY_APEX_PDP_ROBOT="apex-pdp-test.robot apex-slas.robot"
rameshiyer27181e6c52023-03-30 10:50:18 +010037POLICY_XACML_PDP_ROBOT="xacml-pdp-test.robot"
38POLICY_DROOLS_PDP_ROBOT="drools-pdp-test.robot"
39POLICY_DISTRIBUTION_ROBOT="distribution-test.robot"
rameshiyer27e9098662023-04-03 00:57:58 +010040
rameshiyer27181e6c52023-03-30 10:50:18 +010041POLICY_API_CONTAINER="policy-api"
42POLICY_PAP_CONTAINER="policy-pap"
43POLICY_CLAMP_CONTAINER="policy-clamp-runtime-acm"
44POLICY_APEX_CONTAINER="policy-apex-pdp"
45POLICY_DROOLS_CONTAINER="policy-drools-pdp"
46POLICY_XACML_CONTAINER="policy-xacml-pdp"
47POLICY_DISTRIBUTION_CONTAINER="policy-distribution"
rameshiyer27e9098662023-04-03 00:57:58 +010048POLICY_K8S_PPNT_CONTAINER="policy-clamp-ac-k8s-ppnt"
49POLICY_HTTP_PPNT_CONTAINER="policy-clamp-ac-http-ppnt"
50POLICY_PF_PPNT_CONTAINER="policy-clamp-ac-pf-ppnt"
adheli.tavares5980fb42023-03-16 16:31:11 +000051SET_VALUES=""
rameshiyer27181e6c52023-03-30 10:50:18 +010052
53DISTRIBUTION_CSAR=${WORKSPACE}/csit/resources/tests/data/csar
54DIST_TEMP_FOLDER=/tmp/distribution
55
56export PROJECT=""
57export ROBOT_FILE=""
58export ROBOT_LOG_DIR=${WORKSPACE}/csit/archives
59export READINESS_CONTAINERS=()
60
adheli.tavares5980fb42023-03-16 16:31:11 +000061function spin_microk8s_cluster() {
rameshiyer27181e6c52023-03-30 10:50:18 +010062 echo "Verify if Microk8s cluster is running.."
63 microk8s version
64 exitcode="${?}"
65
adheli.tavares5980fb42023-03-16 16:31:11 +000066 if [ "$exitcode" -ne 0 ]; then
rameshiyer27181e6c52023-03-30 10:50:18 +010067 echo "Microk8s cluster not available, Spinning up the cluster.."
68 sudo snap install microk8s --classic --channel=1.25/stable
69
adheli.tavares5980fb42023-03-16 16:31:11 +000070 if [ "${?}" -ne 0 ]; then
71 echo "Failed to install kubernetes cluster. Aborting.."
72 return 1
rameshiyer27181e6c52023-03-30 10:50:18 +010073 fi
74 echo "Microk8s cluster installed successfully"
75 sudo usermod -a -G microk8s $USER
76 echo "Enabling DNS and helm3 plugins"
77 sudo microk8s.enable dns helm3 hostpath-storage
78 echo "Creating configuration file for Microk8s"
adheli.tavares5980fb42023-03-16 16:31:11 +000079 sudo mkdir -p $HOME/.kube
rameshiyer27181e6c52023-03-30 10:50:18 +010080 sudo chown -R $USER:$USER $HOME/.kube
adheli.tavares5980fb42023-03-16 16:31:11 +000081 sudo microk8s kubectl config view --raw >$HOME/.kube/config
rameshiyer27181e6c52023-03-30 10:50:18 +010082 sudo chmod 600 $HOME/.kube/config
83 echo "K8s installation completed"
84 echo "----------------------------------------"
85 else
86 echo "K8s cluster is already running"
87 echo "----------------------------------------"
adheli.tavares5980fb42023-03-16 16:31:11 +000088 return 0
rameshiyer27181e6c52023-03-30 10:50:18 +010089 fi
90
91}
92
rameshiyer27e9098662023-04-03 00:57:58 +010093function uninstall_policy() {
94 echo "Removing the policy helm deployment"
adheli.tavares5980fb42023-03-16 16:31:11 +000095 sudo microk8s helm uninstall csit-policy
96 sudo microk8s helm uninstall prometheus
97 sudo microk8s helm uninstall csit-robot
98 rm -rf ${WORKSPACE}/helm/policy/Chart.lock
rameshiyer27181e6c52023-03-30 10:50:18 +010099 sudo rm -rf /dockerdata-nfs/mariadb-galera/
rameshiyer27e9098662023-04-03 00:57:58 +0100100 echo "Policy deployment deleted"
rameshiyer27181e6c52023-03-30 10:50:18 +0100101 echo "Clean up docker"
adheli.tavares5980fb42023-03-16 16:31:11 +0000102 docker image prune -f
rameshiyer27181e6c52023-03-30 10:50:18 +0100103}
104
rameshiyer27e9098662023-04-03 00:57:58 +0100105function teardown_cluster() {
106 echo "Removing k8s cluster and k8s configuration file"
107 sudo snap remove microk8s;rm -rf $HOME/.kube/config
108 echo "MicroK8s Cluster removed"
109}
110
adheli.tavares5980fb42023-03-16 16:31:11 +0000111function build_robot_image() {
rameshiyer27181e6c52023-03-30 10:50:18 +0100112 echo "Build docker image for robot framework"
adheli.tavares5980fb42023-03-16 16:31:11 +0000113 cd ${WORKSPACE}/csit/resources || exit
rameshiyer27181e6c52023-03-30 10:50:18 +0100114 clone_models
115 if [ "${PROJECT}" == "distribution" ] || [ "${PROJECT}" == "policy-distribution" ]; then
adheli.tavares5980fb42023-03-16 16:31:11 +0000116 copy_csar_file
rameshiyer27181e6c52023-03-30 10:50:18 +0100117 fi
118 echo "Build robot framework docker image"
119 docker login -u docker -p docker nexus3.onap.org:10001
120 docker build . --file Dockerfile \
121 --build-arg CSIT_SCRIPT="$CSIT_SCRIPT" \
adheli.tavares5980fb42023-03-16 16:31:11 +0000122 --build-arg ROBOT_FILE="$ROBOT_FILE" \
rameshiyer27181e6c52023-03-30 10:50:18 +0100123 --tag "${ROBOT_DOCKER_IMAGE}" --no-cache
124 echo "---------------------------------------------"
rameshiyer27181e6c52023-03-30 10:50:18 +0100125}
126
adheli.tavares5980fb42023-03-16 16:31:11 +0000127function start_csit() {
rameshiyer27181e6c52023-03-30 10:50:18 +0100128 build_robot_image
129 if [ "${?}" -eq 0 ]; then
adheli.tavares5980fb42023-03-16 16:31:11 +0000130 echo "Importing robot image into microk8s registry"
131 docker save -o policy-csit-robot.tar ${ROBOT_DOCKER_IMAGE}:latest
132 sudo microk8s ctr image import policy-csit-robot.tar
rameshiyer27181e6c52023-03-30 10:50:18 +0100133 rm -rf ${WORKSPACE}/csit/resources/policy-csit-robot.tar
134 rm -rf ${WORKSPACE}/csit/resources/tests/models/
135 echo "---------------------------------------------"
rameshiyer27e9098662023-04-03 00:57:58 +0100136 if [ $PROJECT == "clamp" ] || [ $PROJECT == "policy-clamp" ]; then
137 POD_READY_STATUS="0/1"
138 while [[ ${POD_READY_STATUS} != "1/1" ]]; do
139 echo "Waiting for chartmuseum pod to come up..."
140 sleep 5
141 POD_READY_STATUS=$(microk8s kubectl get pods | grep -e "policy-chartmuseum" | awk '{print $2}')
142 done
143 push_acelement_chart
144 fi
rameshiyer27181e6c52023-03-30 10:50:18 +0100145 echo "Installing Robot framework pod for running CSIT"
146 cd ${WORKSPACE}/helm
147 mkdir -p ${ROBOT_LOG_DIR}
adheli.tavares5980fb42023-03-16 16:31:11 +0000148 sudo microk8s helm install csit-robot robot --set robot="$ROBOT_FILE" --set "readiness={${READINESS_CONTAINERS[*]}}" --set robotLogDir=$ROBOT_LOG_DIR
rameshiyer27181e6c52023-03-30 10:50:18 +0100149 print_robot_log
adheli.tavares5980fb42023-03-16 16:31:11 +0000150 teardown_cluster
151 fi
rameshiyer27181e6c52023-03-30 10:50:18 +0100152}
153
adheli.tavares5980fb42023-03-16 16:31:11 +0000154function print_robot_log() {
rameshiyer27181e6c52023-03-30 10:50:18 +0100155 count_pods=0
156 while [[ ${count_pods} -eq 0 ]]; do
157 echo "Waiting for pods to come up..."
158 sleep 5
159 count_pods=$(sudo microk8s kubectl get pods --output name | wc -l)
160 done
adheli.tavares5980fb42023-03-16 16:31:11 +0000161 sudo microk8s kubectl get po
rameshiyer27181e6c52023-03-30 10:50:18 +0100162 robotpod=$(sudo microk8s kubectl get po | grep policy-csit)
163 podName=$(echo "$robotpod" | awk '{print $1}')
164 echo "The robot tests will begin once the policy components {${READINESS_CONTAINERS[*]}} are up and running..."
adheli.tavares5980fb42023-03-16 16:31:11 +0000165 sudo microk8s kubectl wait --for=jsonpath='{.status.phase}'=Running --timeout=10m pod/"$podName"
rameshiyer27181e6c52023-03-30 10:50:18 +0100166 sudo microk8s kubectl logs -f "$podName"
167 echo "Please check the logs of policy-csit-robot pod for the test execution results"
168}
169
rameshiyer27e9098662023-04-03 00:57:58 +0100170
171function clone_models () {
rameshiyer27181e6c52023-03-30 10:50:18 +0100172 # download models examples
173 git clone -b "${GERRIT_BRANCH}" --single-branch https://github.com/onap/policy-models.git "${WORKSPACE}"/csit/resources/tests/models
174
175 # create a couple of variations of the policy definitions
176 sed -e 's!Measurement_vGMUX!ADifferentValue!' \
adheli.tavares5980fb42023-03-16 16:31:11 +0000177 tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.json \
rameshiyer27181e6c52023-03-30 10:50:18 +0100178 >tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.v1_2.json
179
180 sed -e 's!"version": "1.0.0"!"version": "2.0.0"!' \
adheli.tavares5980fb42023-03-16 16:31:11 +0000181 -e 's!"policy-version": 1!"policy-version": 2!' \
182 tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.json \
rameshiyer27181e6c52023-03-30 10:50:18 +0100183 >tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.v2.json
184}
185
adheli.tavares5980fb42023-03-16 16:31:11 +0000186function copy_csar_file() {
187 zip -F ${DISTRIBUTION_CSAR}/sample_csar_with_apex_policy.csar \
188 --out ${DISTRIBUTION_CSAR}/csar_temp.csar -q
189 # Remake temp directory
190 sudo rm -rf "${DIST_TEMP_FOLDER}"
191 sudo mkdir "${DIST_TEMP_FOLDER}"
192 sudo cp ${DISTRIBUTION_CSAR}/csar_temp.csar ${DISTRIBUTION_CSAR}/temp.csar
193 sudo mv ${DISTRIBUTION_CSAR}/temp.csar ${DIST_TEMP_FOLDER}/sample_csar_with_apex_policy.csar
rameshiyer27181e6c52023-03-30 10:50:18 +0100194}
195
rameshiyer27e9098662023-04-03 00:57:58 +0100196
197function set_project_config() {
198 echo "Setting project configuration for: $PROJECT"
adheli.tavares5980fb42023-03-16 16:31:11 +0000199 case $PROJECT in
rameshiyer27181e6c52023-03-30 10:50:18 +0100200
adheli.tavares5980fb42023-03-16 16:31:11 +0000201 clamp | policy-clamp)
202 export ROBOT_FILE=$POLICY_CLAMP_ROBOT
rameshiyer27e9098662023-04-03 00:57:58 +0100203 export READINESS_CONTAINERS=($POLICY_CLAMP_CONTAINER,$POLICY_APEX_CONTAINER,$POLICY_PF_PPNT_CONTAINER,$POLICY_K8S_PPNT_CONTAINER,
204 $POLICY_HTTP_PPNT_CONTAINER)
205 export SET_VALUES="--set $POLICY_CLAMP_CONTAINER.enabled=true --set $POLICY_APEX_CONTAINER.enabled=true
206 --set $POLICY_PF_PPNT_CONTAINER.enabled=true --set $POLICY_K8S_PPNT_CONTAINER.enabled=true --set $POLICY_HTTP_PPNT_CONTAINER.enabled=true"
adheli.tavares5980fb42023-03-16 16:31:11 +0000207 ;;
rameshiyer27181e6c52023-03-30 10:50:18 +0100208
adheli.tavares5980fb42023-03-16 16:31:11 +0000209 api | policy-api)
210 export ROBOT_FILE=$POLICY_API_ROBOT
211 export READINESS_CONTAINERS=($POLICY_API_CONTAINER)
212 ;;
rameshiyer27181e6c52023-03-30 10:50:18 +0100213
adheli.tavares5980fb42023-03-16 16:31:11 +0000214 pap | policy-pap)
215 export ROBOT_FILE=$POLICY_PAP_ROBOT
216 export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_PAP_CONTAINER,$POLICY_API_CONTAINER,$POLICY_DROOLS_CONTAINER,
217 $POLICY_XACML_CONTAINER)
rameshiyer27e9098662023-04-03 00:57:58 +0100218 export SET_VALUES="--set $POLICY_APEX_CONTAINER.enabled=true --set $POLICY_DROOLS_CONTAINER.enabled=true --set $POLICY_XACML_CONTAINER.enabled=true"
adheli.tavares5980fb42023-03-16 16:31:11 +0000219 ;;
rameshiyer27181e6c52023-03-30 10:50:18 +0100220
adheli.tavares5980fb42023-03-16 16:31:11 +0000221 apex-pdp | policy-apex-pdp)
222 export ROBOT_FILE=$POLICY_APEX_PDP_ROBOT
223 export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER)
rameshiyer27e9098662023-04-03 00:57:58 +0100224 export SET_VALUES="--set $POLICY_APEX_CONTAINER.enabled=true"
adheli.tavares5980fb42023-03-16 16:31:11 +0000225 ;;
rameshiyer27181e6c52023-03-30 10:50:18 +0100226
adheli.tavares5980fb42023-03-16 16:31:11 +0000227 xacml-pdp | policy-xacml-pdp)
228 export ROBOT_FILE=($POLICY_XACML_PDP_ROBOT)
229 export READINESS_CONTAINERS=($POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER,$POLICY_XACML_CONTAINER)
rameshiyer27e9098662023-04-03 00:57:58 +0100230 export SET_VALUES="--set $POLICY_XACML_CONTAINER.enabled=true"
adheli.tavares5980fb42023-03-16 16:31:11 +0000231 ;;
rameshiyer27181e6c52023-03-30 10:50:18 +0100232
adheli.tavares5980fb42023-03-16 16:31:11 +0000233 drools-pdp | policy-drools-pdp)
234 export ROBOT_FILE=($POLICY_DROOLS_PDP_ROBOT)
235 export READINESS_CONTAINERS=($POLICY_DROOLS_CONTAINER)
rameshiyer27e9098662023-04-03 00:57:58 +0100236 export SET_VALUES="--set $POLICY_DROOLS_CONTAINER.enabled=true"
adheli.tavares5980fb42023-03-16 16:31:11 +0000237 ;;
rameshiyer27181e6c52023-03-30 10:50:18 +0100238
adheli.tavares5980fb42023-03-16 16:31:11 +0000239 distribution | policy-distribution)
240 export ROBOT_FILE=($POLICY_DISTRIBUTION_ROBOT)
241 export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER,
242 $POLICY_DISTRIBUTION_CONTAINER)
rameshiyer27e9098662023-04-03 00:57:58 +0100243 export SET_VALUES="--set $POLICY_APEX_CONTAINER.enabled=true --set $POLICY_DISTRIBUTION_CONTAINER.enabled=true"
adheli.tavares5980fb42023-03-16 16:31:11 +0000244 ;;
rameshiyer27181e6c52023-03-30 10:50:18 +0100245
adheli.tavares5980fb42023-03-16 16:31:11 +0000246 *)
rameshiyer27e9098662023-04-03 00:57:58 +0100247 echo "Unknown project supplied. Enabling all policy charts for the deployment"
248 export SET_VALUES="--set $POLICY_APEX_CONTAINER.enabled=true --set $POLICY_XACML_CONTAINER.enabled=true
249 --set $POLICY_DISTRIBUTION_CONTAINER.enabled=true --set $POLICY_POLICY_DROOLS_CONTAINER.enabled=true
250 --set $POLICY_CLAMP_CONTAINER.enabled=true --set $POLICY_PF_PPNT_CONTAINER.enabled=true
251 --set $POLICY_K8S_PPNT_CONTAINER.enabled=true --set $POLICY_HTTP_PPNT_CONTAINER.enabled=true"
adheli.tavares5980fb42023-03-16 16:31:11 +0000252 ;;
253 esac
rameshiyer27181e6c52023-03-30 10:50:18 +0100254
255}
256
rameshiyer27e9098662023-04-03 00:57:58 +0100257function install_chartmuseum () {
258 echo "Installing Chartmuseum helm repository..."
259 sudo microk8s helm repo add chartmuseum-git https://chartmuseum.github.io/charts
260 sudo microk8s helm repo update
261 sudo microk8s helm install policy-chartmuseum chartmuseum-git/chartmuseum --set env.open.DISABLE_API=false --set service.type=NodePort --set service.nodePort=30208
262 sudo microk8s helm plugin install https://github.com/chartmuseum/helm-push
adheli.tavares5980fb42023-03-16 16:31:11 +0000263}
264
rameshiyer27e9098662023-04-03 00:57:58 +0100265function push_acelement_chart() {
266 echo "Pushing acelement chart to the chartmuseum repo..."
267 sudo microk8s helm repo add policy-chartmuseum http://localhost:30208
268
269 # download clamp repo
270 git clone -b "${GERRIT_BRANCH}" --single-branch https://github.com/onap/policy-clamp.git "${WORKSPACE}"/csit/resources/tests/clamp
271 ACELEMENT_CHART=${WORKSPACE}/csit/resources/tests/clamp/examples/src/main/resources/clamp/acm/acelement-helm/acelement
272 sudo microk8s helm cm-push $ACELEMENT_CHART policy-chartmuseum
273 sudo microk8s helm repo update
274 echo "-------------------------------------------"
275}
276
277
adheli.tavares5980fb42023-03-16 16:31:11 +0000278OPERATION="$1"
279PROJECT="$2"
280
rameshiyer27e9098662023-04-03 00:57:58 +0100281if [ $OPERATION == "install" ]; then
rameshiyer27181e6c52023-03-30 10:50:18 +0100282 spin_microk8s_cluster
adheli.tavares5980fb42023-03-16 16:31:11 +0000283 if [ "${?}" -eq 0 ]; then
rameshiyer27e9098662023-04-03 00:57:58 +0100284 set_project_config
rameshiyer27181e6c52023-03-30 10:50:18 +0100285 echo "Installing policy helm charts in the default namespace"
adheli.tavares5980fb42023-03-16 16:31:11 +0000286 cd ${WORKSPACE}/helm || exit
rameshiyer27181e6c52023-03-30 10:50:18 +0100287 sudo microk8s helm dependency build policy
adheli.tavares5980fb42023-03-16 16:31:11 +0000288 sudo microk8s helm install csit-policy policy ${SET_VALUES}
rameshiyer27181e6c52023-03-30 10:50:18 +0100289 sudo microk8s helm install prometheus prometheus
290 echo "Policy chart installation completed"
adheli.tavares5980fb42023-03-16 16:31:11 +0000291 echo "-------------------------------------------"
rameshiyer27181e6c52023-03-30 10:50:18 +0100292 fi
293
adheli.tavares5980fb42023-03-16 16:31:11 +0000294 if [ "$PROJECT" ]; then
295 export $PROJECT
rameshiyer27181e6c52023-03-30 10:50:18 +0100296 export ROBOT_LOG_DIR=${WORKSPACE}/csit/archives/${PROJECT}
297 get_robot_file
298 echo "CSIT will be invoked from $ROBOT_FILE"
299 echo "Readiness containers: ${READINESS_CONTAINERS[*]}"
300 echo "-------------------------------------------"
301 start_csit
302 else
303 echo "No project supplied for running CSIT"
304 fi
305
adheli.tavares5980fb42023-03-16 16:31:11 +0000306elif [ $OPERATION == "uninstall" ]; then
rameshiyer27e9098662023-04-03 00:57:58 +0100307 uninstall_policy
308
309elif [ $OPERATION == "clean" ]; then
rameshiyer27181e6c52023-03-30 10:50:18 +0100310 teardown_cluster
311else
rameshiyer27e9098662023-04-03 00:57:58 +0100312 echo "Invalid arguments provided. Usage: $0 [option..] {install {project} | uninstall} | {clean}"
rameshiyer27181e6c52023-03-30 10:50:18 +0100313fi