blob: 248a494ff588b04b68e2a5805823324c96bef6ba [file] [log] [blame]
rameshiyer27a1954d42022-11-14 06:00:12 +00001#!/bin/bash
2#
3# ============LICENSE_START====================================================
rameshiyer2765fc3752023-04-05 13:47:15 +01004# Copyright (C) 2022-2023 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"
49SET_VALUES=""
rameshiyer27a1954d42022-11-14 06:00:12 +000050
rameshiyer2765fc3752023-04-05 13:47:15 +010051DISTRIBUTION_CSAR=${WORKSPACE}/csit/resources/tests/data/csar
52DIST_TEMP_FOLDER=/tmp/distribution
rameshiyer27a1954d42022-11-14 06:00:12 +000053
rameshiyer2765fc3752023-04-05 13:47:15 +010054export PROJECT=""
55export ROBOT_FILE=""
56export ROBOT_LOG_DIR=${WORKSPACE}/csit/archives
57export READINESS_CONTAINERS=()
58
59function spin_microk8s_cluster() {
60 echo "Verify if Microk8s cluster is running.."
61 microk8s version
62 exitcode="${?}"
63
64 if [ "$exitcode" -ne 0 ]; then
65 echo "Microk8s cluster not available, Spinning up the cluster.."
66 sudo snap install microk8s --classic --channel=1.26/stable
67
68 if [ "${?}" -ne 0 ]; then
69 echo "Failed to install kubernetes cluster. Aborting.."
70 return 1
71 fi
72 echo "Microk8s cluster installed successfully"
73 sudo usermod -a -G microk8s $USER
74 echo "Enabling DNS and helm3 plugins"
75 sudo microk8s.enable dns helm3 hostpath-storage
76 echo "Creating configuration file for Microk8s"
77 sudo mkdir -p $HOME/.kube
78 sudo chown -R $USER:$USER $HOME/.kube
79 sudo microk8s kubectl config view --raw >$HOME/.kube/config
80 sudo chmod 600 $HOME/.kube/config
81 echo "K8s installation completed"
82 echo "----------------------------------------"
83 else
84 echo "K8s cluster is already running"
85 echo "----------------------------------------"
86 return 0
87 fi
88
89}
90
91function uninstall_policy() {
92 echo "Removing the policy helm deployment"
93 sudo microk8s helm uninstall csit-policy
94 sudo microk8s helm uninstall prometheus
95 sudo microk8s helm uninstall csit-robot
96 rm -rf ${WORKSPACE}/helm/policy/Chart.lock
97 if [ "$PROJECT" == "clamp" ] || [ "$PROJECT" == "policy-clamp" ]; then
98 sudo microk8s helm uninstall policy-chartmuseum
99 sudo microk8s helm repo remove chartmuseum-git policy-chartmuseum
100 fi
101 sudo rm -rf /dockerdata-nfs/mariadb-galera/
102 sudo microk8s kubectl delete pvc --all
103 echo "Policy deployment deleted"
104 echo "Clean up docker"
105 docker image prune -f
106}
107
108function teardown_cluster() {
109 echo "Removing k8s cluster and k8s configuration file"
110 sudo snap remove microk8s;rm -rf $HOME/.kube/config
111 echo "MicroK8s Cluster removed"
112}
113
114function build_robot_image() {
115 echo "Build docker image for robot framework"
116 cd ${WORKSPACE}/csit/resources || exit
117 clone_models
118 if [ "${PROJECT}" == "distribution" ] || [ "${PROJECT}" == "policy-distribution" ]; then
119 copy_csar_file
120 fi
121 echo "Build robot framework docker image"
122 docker login -u docker -p docker nexus3.onap.org:10001
123 docker build . --file Dockerfile \
124 --build-arg CSIT_SCRIPT="$CSIT_SCRIPT" \
125 --build-arg ROBOT_FILE="$ROBOT_FILE" \
126 --tag "${ROBOT_DOCKER_IMAGE}" --no-cache
127 echo "---------------------------------------------"
128}
129
130function start_csit() {
131 build_robot_image
132 if [ "${?}" -eq 0 ]; then
133 echo "Importing robot image into microk8s registry"
134 docker save -o policy-csit-robot.tar ${ROBOT_DOCKER_IMAGE}:latest
135 sudo microk8s ctr image import policy-csit-robot.tar
136 rm -rf ${WORKSPACE}/csit/resources/policy-csit-robot.tar
137 rm -rf ${WORKSPACE}/csit/resources/tests/models/
138 echo "---------------------------------------------"
139 if [ "$PROJECT" == "clamp" ] || [ "$PROJECT" == "policy-clamp" ]; then
140 POD_READY_STATUS="0/1"
141 while [[ ${POD_READY_STATUS} != "1/1" ]]; do
142 echo "Waiting for chartmuseum pod to come up..."
143 sleep 5
144 POD_READY_STATUS=$(sudo microk8s kubectl get pods | grep -e "policy-chartmuseum" | awk '{print $2}')
145 done
146 push_acelement_chart
147 fi
148 echo "Installing Robot framework pod for running CSIT"
149 cd ${WORKSPACE}/helm
150 mkdir -p ${ROBOT_LOG_DIR}
151 sudo microk8s helm install csit-robot robot --set robot="$ROBOT_FILE" --set "readiness={${READINESS_CONTAINERS[*]}}" --set robotLogDir=$ROBOT_LOG_DIR
152 print_robot_log
rameshiyer2765fc3752023-04-05 13:47:15 +0100153 fi
154}
155
156function print_robot_log() {
157 count_pods=0
158 while [[ ${count_pods} -eq 0 ]]; do
159 echo "Waiting for pods to come up..."
160 sleep 5
161 count_pods=$(sudo microk8s kubectl get pods --output name | wc -l)
162 done
163 robotpod=$(sudo microk8s kubectl get po | grep policy-csit)
164 podName=$(echo "$robotpod" | awk '{print $1}')
165 echo "The robot tests will begin once the policy components {${READINESS_CONTAINERS[*]}} are up and running..."
166 sudo microk8s kubectl wait --for=jsonpath='{.status.phase}'=Running --timeout=10m pod/"$podName"
167 echo "Policy deployment status:"
168 sudo microk8s kubectl get po
169 sudo microk8s kubectl get all -A
170 echo "Robot Test logs:"
171 sudo microk8s kubectl logs -f "$podName"
172}
173
174function clone_models() {
175
176 # download models examples
177 git clone -b "${GERRIT_BRANCH}" --single-branch https://github.com/onap/policy-models.git "${WORKSPACE}"/csit/resources/tests/models
178
179 # create a couple of variations of the policy definitions
180 sed -e 's!Measurement_vGMUX!ADifferentValue!' \
181 tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.json \
182 >tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.v1_2.json
183
184 sed -e 's!"version": "1.0.0"!"version": "2.0.0"!' \
185 -e 's!"policy-version": 1!"policy-version": 2!' \
186 tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.json \
187 >tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.v2.json
188}
189
190function copy_csar_file() {
191 zip -F ${DISTRIBUTION_CSAR}/sample_csar_with_apex_policy.csar \
192 --out ${DISTRIBUTION_CSAR}/csar_temp.csar -q
193 # Remake temp directory
194 sudo rm -rf "${DIST_TEMP_FOLDER}"
195 sudo mkdir "${DIST_TEMP_FOLDER}"
196 sudo cp ${DISTRIBUTION_CSAR}/csar_temp.csar ${DISTRIBUTION_CSAR}/temp.csar
197 sudo mv ${DISTRIBUTION_CSAR}/temp.csar ${DIST_TEMP_FOLDER}/sample_csar_with_apex_policy.csar
198}
199
200function set_project_config() {
201 echo "Setting project configuration for: $PROJECT"
202 case $PROJECT in
203
204 clamp | policy-clamp)
205 export ROBOT_FILE=$POLICY_CLAMP_ROBOT
206 export READINESS_CONTAINERS=($POLICY_CLAMP_CONTAINER,$POLICY_APEX_CONTAINER,$POLICY_PF_PPNT_CONTAINER,$POLICY_K8S_PPNT_CONTAINER,
207 $POLICY_HTTP_PPNT_CONTAINER)
208 export SET_VALUES="--set $POLICY_CLAMP_CONTAINER.enabled=true --set $POLICY_APEX_CONTAINER.enabled=true
209 --set $POLICY_PF_PPNT_CONTAINER.enabled=true --set $POLICY_K8S_PPNT_CONTAINER.enabled=true --set $POLICY_HTTP_PPNT_CONTAINER.enabled=true"
210 install_chartmuseum
211 ;;
212
213 api | policy-api)
214 export ROBOT_FILE=$POLICY_API_ROBOT
215 export READINESS_CONTAINERS=($POLICY_API_CONTAINER)
216 ;;
217
218 pap | policy-pap)
219 export ROBOT_FILE=$POLICY_PAP_ROBOT
220 export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_PAP_CONTAINER,$POLICY_API_CONTAINER,$POLICY_XACML_CONTAINER)
221 export SET_VALUES="--set $POLICY_APEX_CONTAINER.enabled=true --set $POLICY_XACML_CONTAINER.enabled=true"
222 ;;
223
224 apex-pdp | policy-apex-pdp)
225 export ROBOT_FILE=$POLICY_APEX_PDP_ROBOT
226 export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER)
227 export SET_VALUES="--set $POLICY_APEX_CONTAINER.enabled=true"
228 ;;
229
230 xacml-pdp | policy-xacml-pdp)
231 export ROBOT_FILE=($POLICY_XACML_PDP_ROBOT)
232 export READINESS_CONTAINERS=($POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER,$POLICY_XACML_CONTAINER)
233 export SET_VALUES="--set $POLICY_XACML_CONTAINER.enabled=true"
234 ;;
235
236 drools-pdp | policy-drools-pdp)
237 export ROBOT_FILE=($POLICY_DROOLS_PDP_ROBOT)
238 export READINESS_CONTAINERS=($POLICY_DROOLS_CONTAINER)
239 export SET_VALUES="--set $POLICY_DROOLS_CONTAINER.enabled=true"
240 ;;
241
242 distribution | policy-distribution)
243 export ROBOT_FILE=($POLICY_DISTRIBUTION_ROBOT)
244 export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER,
245 $POLICY_DISTRIBUTION_CONTAINER)
246 export SET_VALUES="--set $POLICY_APEX_CONTAINER.enabled=true --set $POLICY_DISTRIBUTION_CONTAINER.enabled=true"
247 ;;
248
249 *)
250 echo "Unknown project supplied. Enabling all policy charts for the deployment"
251 export SET_VALUES="--set $POLICY_APEX_CONTAINER.enabled=true --set $POLICY_XACML_CONTAINER.enabled=true
252 --set $POLICY_DISTRIBUTION_CONTAINER.enabled=true --set $POLICY_POLICY_DROOLS_CONTAINER.enabled=true
253 --set $POLICY_CLAMP_CONTAINER.enabled=true --set $POLICY_PF_PPNT_CONTAINER.enabled=true
254 --set $POLICY_K8S_PPNT_CONTAINER.enabled=true --set $POLICY_HTTP_PPNT_CONTAINER.enabled=true"
255 ;;
256 esac
257
258}
259
260function install_chartmuseum () {
261 echo "Installing Chartmuseum helm repository..."
262 sudo microk8s helm repo add chartmuseum-git https://chartmuseum.github.io/charts
263 sudo microk8s helm repo update
264 sudo microk8s helm install policy-chartmuseum chartmuseum-git/chartmuseum --set env.open.DISABLE_API=false --set service.type=NodePort --set service.nodePort=30208
265 sudo microk8s helm plugin install https://github.com/chartmuseum/helm-push
266 echo "---------------------------------------------"
267}
268
269function push_acelement_chart() {
270 echo "Pushing acelement chart to the chartmuseum repo..."
271 sudo microk8s helm repo add policy-chartmuseum http://localhost:30208
272
273 # download clamp repo
274 git clone -b "${GERRIT_BRANCH}" --single-branch https://github.com/onap/policy-clamp.git "${WORKSPACE}"/csit/resources/tests/clamp
275 ACELEMENT_CHART=${WORKSPACE}/csit/resources/tests/clamp/examples/src/main/resources/clamp/acm/acelement-helm/acelement
276 sudo microk8s helm cm-push $ACELEMENT_CHART policy-chartmuseum
277 sudo microk8s helm repo update
278 rm -rf ${WORKSPACE}/csit/resources/tests/clamp/
279 echo "-------------------------------------------"
280}
rameshiyer27a1954d42022-11-14 06:00:12 +0000281
rameshiyer2707004862023-01-23 11:50:01 +0000282
rameshiyer2765fc3752023-04-05 13:47:15 +0100283OPERATION="$1"
284PROJECT="$2"
rameshiyer27bdced492023-06-28 12:15:14 +0100285if [ -z "$3" ]
286then
287 LOCALIMAGE="false"
288else
289 LOCALIMAGE="$3"
290fi
291
rameshiyer27a1954d42022-11-14 06:00:12 +0000292
rameshiyer2765fc3752023-04-05 13:47:15 +0100293if [ $OPERATION == "install" ]; then
294 spin_microk8s_cluster
295 if [ "${?}" -eq 0 ]; then
296 set_project_config
297 echo "Installing policy helm charts in the default namespace"
298 source ${WORKSPACE}/compose/get-k8s-versions.sh
rameshiyer27bdced492023-06-28 12:15:14 +0100299 if [ $LOCALIMAGE == "true" ]; then
300 echo "loading local image"
301 source ${WORKSPACE}/compose/get-versions.sh
302 ${WORKSPACE}/compose/loaddockerimage.sh
303 fi
rameshiyer2765fc3752023-04-05 13:47:15 +0100304 cd ${WORKSPACE}/helm || exit
305 sudo microk8s helm dependency build policy
306 sudo microk8s helm install csit-policy policy ${SET_VALUES}
307 sudo microk8s helm install prometheus prometheus
308 echo "Policy chart installation completed"
309 echo "-------------------------------------------"
310 fi
311
312 if [ "$PROJECT" ]; then
313 export ROBOT_LOG_DIR=${WORKSPACE}/csit/archives/${PROJECT}
314 echo "CSIT will be invoked from $ROBOT_FILE"
315 echo "Readiness containers: ${READINESS_CONTAINERS[*]}"
316 echo "-------------------------------------------"
317 start_csit
318 else
319 echo "No project supplied for running CSIT"
320 fi
321
322elif [ $OPERATION == "uninstall" ]; then
323 uninstall_policy
324
325elif [ $OPERATION == "clean" ]; then
326 teardown_cluster
327
328else
rameshiyer27bdced492023-06-28 12:15:14 +0100329 echo "Invalid arguments provided. Usage: $0 [options..] {install {project_name} | uninstall | clean} {uselocalimage = true/false}"
rameshiyer2765fc3752023-04-05 13:47:15 +0100330fi