Replace virtlet with kubevirt in plugin_fw_v2
[multicloud/k8s.git] / kud / tests / plugin_fw_v2.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2020
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 set -o errexit
12 set -o nounset
13 set -o pipefail
14
15 source _common_test.sh
16 source _functions.sh
17
18 # TODO KUBECONFIG may be a list of paths
19 KUBECONFIG_PATH="${KUBECONFIG:-$HOME/.kube/config}"
20 DEMO_FOLDER="${DEMO_FOLDER:-$test_folder/../demo}"
21
22 clusters="${KUD_PLUGIN_FW_CLUSTERS:-$(cat <<EOF
23 [
24   {
25     "metadata": {
26       "name": "edge01",
27       "description": "description of edge01",
28       "userData1": "edge01 user data 1",
29       "userData2": "edge01 user data 2"
30     },
31     "file": "$KUBECONFIG_PATH"
32   }
33 ]
34 EOF
35 )}"
36
37 function cluster_names {
38     echo $clusters | jq -e -r '.[].metadata.name'
39 }
40
41 function cluster_metadata {
42     cat<<EOF | jq .
43 {
44   "metadata": $(echo $clusters | jq -e -r --arg name "$1" '.[]|select(.metadata.name==$name)|.metadata')
45 }
46 EOF
47 }
48
49 function cluster_file {
50     echo $clusters | jq -e -r --arg name "$1" '.[]|select(.metadata.name==$name)|.file'
51 }
52
53 ARGS=()
54 while [[ $# -gt 0 ]]; do
55     arg="$1"
56
57     case $arg in
58         "--external" )
59             service_host=$(kubectl cluster-info | grep "Kubernetes master" | \
60                 awk -F ":" '{print $2}' | awk -F "//" '{print $2}')
61             shift
62             ;;
63         * )
64             ARGS+=("$1")
65             shift
66             ;;
67     esac
68 done
69 set -- "${ARGS[@]}" # restore positional parameters
70
71 service_host=${service_host:-"localhost"}
72
73 CSAR_DIR="/opt/csar"
74 csar_id="4bf66240-a0be-4ce2-aebd-a01df7725f16"
75
76 function populate_CSAR_compositevfw_helm {
77     _checks_args "$1"
78     pushd "${CSAR_DIR}/$1"
79     print_msg "Create Helm Chart Archives for compositevfw"
80     rm -f *.tar.gz
81     tar -czf packetgen.tar.gz -C $DEMO_FOLDER/composite-firewall packetgen
82     tar -czf firewall.tar.gz -C $DEMO_FOLDER/composite-firewall firewall
83     tar -czf sink.tar.gz -C $DEMO_FOLDER/composite-firewall sink
84     tar -czf profile.tar.gz -C $DEMO_FOLDER/composite-firewall manifest.yaml override_values.yaml
85     popd
86 }
87
88 project="testvfw"
89 composite_app="compositevfw"
90 version="v1"
91 deployment_intent_group="vfw_deployment_intent_group"
92
93 function setup {
94     install_deps
95     populate_CSAR_compositevfw_helm "$csar_id"
96     cat <<EOF >plugin_fw_v2_config.yaml
97 orchestrator:
98   host: ${service_host}
99   port: 30415
100 clm:
101   host: ${service_host}
102   port: 30461
103 ncm:
104   host: ${service_host}
105   port: 30431
106 ovnaction:
107   host: ${service_host}
108   port: 30471
109 dcm:
110   host: ${service_host}
111   port: 30477
112 gac:
113   host: ${service_host}
114   port: 30491
115 dtc:
116  host: ${service_host}
117  port: 30481
118 EOF
119     cat <<EOF >plugin_fw_v2_values.yaml
120 ClusterProvider: vfw-cluster-provider
121 ClusterLabel: LabelA
122 Clusters:
123 EOF
124     echo $clusters | jq -r '.[] | "- Name: \(.metadata.name)\n  KubeConfig: \(.file)"' >>plugin_fw_v2_values.yaml
125     cat <<EOF >>plugin_fw_v2_values.yaml
126 EmcoProviderNetwork: emco-private-net
127 UnprotectedProviderNetwork: unprotected-private-net
128 ProtectedNetwork: protected-private-net
129 Project: ${project}
130 LogicalCloud: lcadmin
131 CompositeApp: ${composite_app}
132 Version: ${version}
133 PackagesPath: ${CSAR_DIR}/${csar_id}
134 CompositeProfile: vfw_composite-profile
135 DeploymentIntentGroup: ${deployment_intent_group}
136 Release: fw0
137 DeploymentIntentsInGroup: vfw_deploy_intents
138 GenericPlacementIntent: generic-placement-intent
139 OvnActionIntent: vfw_ovnaction_intent
140 EOF
141 }
142
143 function call_emcoctl {
144     rc=$1
145     shift
146     # retry due to known issue with emcoctl and instantiating/terminating multiple resources
147     try=0
148     until [[ $(emcoctl $@ | awk '/Response Code:/ {code=$3} END{print code}') =~ $rc ]]; do
149         if [[ $try -lt 10 ]]; then
150             sleep 1s
151         else
152             return 1
153         fi
154         try=$((try + 1))
155     done
156     return 0
157 }
158
159 function createData {
160     call_emcoctl 2.. --config plugin_fw_v2_config.yaml apply -f plugin_fw_v2.yaml -v plugin_fw_v2_values.yaml
161 }
162
163 function getData {
164     emcoctl --config plugin_fw_v2_config.yaml get -f plugin_fw_v2.yaml -v plugin_fw_v2_values.yaml
165 }
166
167 function deleteData {
168     call_emcoctl 4.. --config plugin_fw_v2_config.yaml delete -f plugin_fw_v2.yaml -v plugin_fw_v2_values.yaml
169 }
170
171 function statusVfw {
172     emcoctl --config plugin_fw_v2_config.yaml get projects/${project}/composite-apps/${composite_app}/${version}/deployment-intent-groups/${deployment_intent_group}/status
173 }
174
175 function waitForVfw {
176     for try in {0..59}; do
177         sleep 1
178         new_phase="$(emcoctl --config plugin_fw_v2_config.yaml get projects/${project}/composite-apps/${composite_app}/${version}/deployment-intent-groups/${deployment_intent_group}/status | awk '/Response: / {print $2}' | jq -r .status)"
179         echo "$(date +%H:%M:%S) - Filter=[$*] : $new_phase"
180         if [[ "$new_phase" == "$1" ]]; then
181             return 0
182         fi
183     done
184 }
185
186 function usage {
187     echo "Usage: $0 setup|create|get|destroy|status"
188     echo "    setup - creates the emcoctl files and packages needed for vfw"
189     echo "    create - creates all ncm, ovnaction, clm resources needed for vfw"
190     echo "    get - queries all resources in ncm, ovnaction, clm resources created for vfw"
191     echo "    destroy - deletes all resources in ncm, ovnaction, clm resources created for vfw"
192     echo "    status - get status of deployed resources"
193     echo ""
194     echo "    a reasonable test sequence:"
195     echo "    1.  setup"
196     echo "    2.  create"
197     echo "    3.  destroy"
198
199     exit
200 }
201
202 if [[ "$#" -gt 0 ]] ; then
203     case "$1" in
204         "setup" ) setup ;;
205         "create" ) createData ;;
206         "get" ) getData ;;
207         "status" ) statusVfw ;;
208         "wait" ) waitForVfw "Instantiated" ;;
209         "delete" ) deleteData ;;
210         *) usage ;;
211     esac
212 else
213     setup
214     createData
215
216     print_msg "[BEGIN] Basic checks for instantiated resource"
217     print_msg "Wait for deployment to be instantiated"
218     waitForVfw "Instantiated"
219     for name in $(cluster_names); do
220         print_msg "Check that networks were created on cluster $name"
221         file=$(cluster_file "$name")
222         KUBECONFIG=$file kubectl get network protected-private-net -o name
223         KUBECONFIG=$file kubectl get providernetwork emco-private-net -o name
224         KUBECONFIG=$file kubectl get providernetwork unprotected-private-net -o name
225     done
226     for name in $(cluster_names); do
227         print_msg "Wait for all pods to start on cluster $name"
228         file=$(cluster_file "$name")
229         KUBECONFIG=$file kubectl wait pod -l release=fw0 --for=condition=Ready --timeout=5m
230     done
231     # TODO: Provide some health check to verify vFW work
232     print_msg "Not waiting for vFW to fully install as no further checks are implemented in testcase"
233     #print_msg "Waiting 8minutes for vFW installation"
234     #sleep 8m
235     print_msg "[END] Basic checks for instantiated resource"
236
237     print_msg "Delete deployment"
238     deleteData
239 fi