rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # ============LICENSE_START==================================================== |
rameshiyer27 | 65fc375 | 2023-04-05 13:47:15 +0100 | [diff] [blame] | 4 | # Copyright (C) 2022-2023 Nordix Foundation. |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 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 | |
rameshiyer27 | 65fc375 | 2023-04-05 13:47:15 +0100 | [diff] [blame] | 21 | # This script spins up kubernetes cluster in Microk8s for deploying policy helm charts. |
| 22 | # Runs CSITs in kubernetes. |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 23 | |
rameshiyer27 | f575f92 | 2023-10-12 15:11:02 +0100 | [diff] [blame] | 24 | WORKSPACE=$(git rev-parse --show-toplevel) |
| 25 | export WORKSPACE |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame] | 26 | |
rameshiyer27 | 65fc375 | 2023-04-05 13:47:15 +0100 | [diff] [blame] | 27 | export GERRIT_BRANCH=$(awk -F= '$1 == "defaultbranch" { print $2 }' "${WORKSPACE}"/.gitreview) |
rameshiyer27 | d8f9180 | 2023-03-07 01:32:28 +0000 | [diff] [blame] | 28 | |
rameshiyer27 | 65fc375 | 2023-04-05 13:47:15 +0100 | [diff] [blame] | 29 | CSIT_SCRIPT="scripts/run-test.sh" |
| 30 | ROBOT_DOCKER_IMAGE="policy-csit-robot" |
| 31 | POLICY_CLAMP_ROBOT="policy-clamp-test.robot" |
| 32 | POLICY_API_ROBOT="api-test.robot api-slas.robot" |
| 33 | POLICY_PAP_ROBOT="pap-test.robot pap-slas.robot" |
| 34 | POLICY_APEX_PDP_ROBOT="apex-pdp-test.robot apex-slas.robot" |
| 35 | POLICY_XACML_PDP_ROBOT="xacml-pdp-test.robot" |
| 36 | POLICY_DROOLS_PDP_ROBOT="drools-pdp-test.robot" |
| 37 | POLICY_DISTRIBUTION_ROBOT="distribution-test.robot" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 38 | |
rameshiyer27 | 65fc375 | 2023-04-05 13:47:15 +0100 | [diff] [blame] | 39 | POLICY_API_CONTAINER="policy-api" |
| 40 | POLICY_PAP_CONTAINER="policy-pap" |
| 41 | POLICY_CLAMP_CONTAINER="policy-clamp-runtime-acm" |
| 42 | POLICY_APEX_CONTAINER="policy-apex-pdp" |
| 43 | POLICY_DROOLS_CONTAINER="policy-drools-pdp" |
| 44 | POLICY_XACML_CONTAINER="policy-xacml-pdp" |
| 45 | POLICY_DISTRIBUTION_CONTAINER="policy-distribution" |
| 46 | POLICY_K8S_PPNT_CONTAINER="policy-clamp-ac-k8s-ppnt" |
| 47 | POLICY_HTTP_PPNT_CONTAINER="policy-clamp-ac-http-ppnt" |
| 48 | POLICY_PF_PPNT_CONTAINER="policy-clamp-ac-pf-ppnt" |
| 49 | SET_VALUES="" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 50 | |
rameshiyer27 | 65fc375 | 2023-04-05 13:47:15 +0100 | [diff] [blame] | 51 | DISTRIBUTION_CSAR=${WORKSPACE}/csit/resources/tests/data/csar |
| 52 | DIST_TEMP_FOLDER=/tmp/distribution |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 53 | |
rameshiyer27 | 65fc375 | 2023-04-05 13:47:15 +0100 | [diff] [blame] | 54 | export PROJECT="" |
| 55 | export ROBOT_FILE="" |
| 56 | export ROBOT_LOG_DIR=${WORKSPACE}/csit/archives |
| 57 | export READINESS_CONTAINERS=() |
| 58 | |
| 59 | function 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 | |
| 91 | function 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 | |
| 108 | function 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 | |
| 114 | function 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 | |
| 130 | function 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 |
rameshiyer27 | 65fc375 | 2023-04-05 13:47:15 +0100 | [diff] [blame] | 153 | fi |
| 154 | } |
| 155 | |
| 156 | function 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 | |
| 174 | function 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 | |
| 190 | function 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 | |
| 200 | function 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 | |
| 260 | function 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 | |
| 269 | function 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 | } |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 281 | |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 282 | |
rameshiyer27 | 65fc375 | 2023-04-05 13:47:15 +0100 | [diff] [blame] | 283 | OPERATION="$1" |
| 284 | PROJECT="$2" |
rameshiyer27 | bdced49 | 2023-06-28 12:15:14 +0100 | [diff] [blame] | 285 | if [ -z "$3" ] |
| 286 | then |
| 287 | LOCALIMAGE="false" |
| 288 | else |
| 289 | LOCALIMAGE="$3" |
| 290 | fi |
| 291 | |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 292 | |
rameshiyer27 | 65fc375 | 2023-04-05 13:47:15 +0100 | [diff] [blame] | 293 | if [ $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 |
rameshiyer27 | bdced49 | 2023-06-28 12:15:14 +0100 | [diff] [blame] | 299 | if [ $LOCALIMAGE == "true" ]; then |
| 300 | echo "loading local image" |
| 301 | source ${WORKSPACE}/compose/get-versions.sh |
| 302 | ${WORKSPACE}/compose/loaddockerimage.sh |
| 303 | fi |
rameshiyer27 | 65fc375 | 2023-04-05 13:47:15 +0100 | [diff] [blame] | 304 | 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 | |
| 322 | elif [ $OPERATION == "uninstall" ]; then |
| 323 | uninstall_policy |
| 324 | |
| 325 | elif [ $OPERATION == "clean" ]; then |
| 326 | teardown_cluster |
| 327 | |
| 328 | else |
rameshiyer27 | bdced49 | 2023-06-28 12:15:14 +0100 | [diff] [blame] | 329 | echo "Invalid arguments provided. Usage: $0 [options..] {install {project_name} | uninstall | clean} {uselocalimage = true/false}" |
rameshiyer27 | 65fc375 | 2023-04-05 13:47:15 +0100 | [diff] [blame] | 330 | fi |