Merge "Adding apex-pdp metrics to SLAs dashboard."
[policy/docker.git] / csit / run-k8s-csit.sh
1 #!/bin/bash
2 #
3 # ============LICENSE_START====================================================
4 #  Copyright (C) 2022 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
23 function spin_microk8s_cluster () {
24     echo "Verify if Microk8s cluster is running.."
25     microk8s version
26     exitcode="${?}"
27
28     if [ "$exitcode" -ne 0 ];  then
29         echo "Microk8s cluster not available, Spinning up the cluster.."
30         sudo snap install microk8s --classic --channel=1.25/stable
31
32               if [ "${?}" -ne 0 ];  then
33                   echo "Failed to install kubernetes cluster. Aborting.."
34                         return 1
35         fi
36         echo "Microk8s cluster installed successfully"
37         sudo usermod -a -G microk8s $USER
38         echo "Enabling DNS and helm3"
39         microk8s.enable dns helm3
40         echo "Creating configuration file for Microk8s"
41         microk8s kubectl config view --raw > $HOME/.kube/config
42         chmod 600 $HOME/.kube/config
43         echo "K8s installation completed"
44     else
45         echo "K8s cluster is already running"
46               return 0
47     fi
48
49 }
50
51 function teardown_cluster () {
52     echo "Removing k8s cluster and k8s configuration file"
53     sudo snap remove microk8s;rm -rf $HOME/.kube/config
54     echo "K8s Cluster removed"
55 }
56
57
58 if [ $1 == "install" ];  then
59     spin_microk8s_cluster
60     if [ "${?}" -eq 0 ];  then
61         echo "Installing policy helm charts in the default namespace"
62         cd ../helm/;helm dependency build policy;microk8s helm install dev-policy policy;
63         echo "Policy chart installation completed"
64     fi
65
66 elif [ $1 == "uninstall" ];  then
67     teardown_cluster
68 else
69     echo "Invalid arguments provided. Usage: $0 [option..] {install | uninstall}"
70 fi
71