Merge "Allow KuD installer to specify Kubespray 2.14.1 or 2.16.0"
[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=$(control_plane_ip)
60             shift
61             ;;
62         * )
63             ARGS+=("$1")
64             shift
65             ;;
66     esac
67 done
68 set -- "${ARGS[@]}" # restore positional parameters
69
70 service_host=${service_host:-"localhost"}
71
72 CSAR_DIR="/opt/csar"
73 csar_id="4bf66240-a0be-4ce2-aebd-a01df7725f16"
74
75 function populate_CSAR_compositevfw_helm {
76     _checks_args "$1"
77     pushd "${CSAR_DIR}/$1"
78     print_msg "Create Helm Chart Archives for compositevfw"
79     rm -f *.tar.gz
80     tar -czf packetgen.tar.gz -C $DEMO_FOLDER/composite-firewall packetgen
81     tar -czf firewall.tar.gz -C $DEMO_FOLDER/composite-firewall firewall
82     tar -czf sink.tar.gz -C $DEMO_FOLDER/composite-firewall sink
83     tar -czf profile.tar.gz -C $DEMO_FOLDER/composite-firewall manifest.yaml override_values.yaml
84     popd
85 }
86
87 project="testvfw"
88 composite_app="compositevfw"
89 version="v1"
90 deployment_intent_group="vfw_deployment_intent_group"
91
92 function setup {
93     install_deps
94     populate_CSAR_compositevfw_helm "$csar_id"
95     cat <<EOF >plugin_fw_v2_config.yaml
96 orchestrator:
97   host: ${service_host}
98   port: 30415
99 clm:
100   host: ${service_host}
101   port: 30461
102 ncm:
103   host: ${service_host}
104   port: 30431
105 ovnaction:
106   host: ${service_host}
107   port: 30471
108 dcm:
109   host: ${service_host}
110   port: 30477
111 gac:
112   host: ${service_host}
113   port: 30491
114 dtc:
115  host: ${service_host}
116  port: 30481
117 EOF
118     cat <<EOF >plugin_fw_v2_values.yaml
119 ClusterProvider: vfw-cluster-provider
120 ClusterLabel: LabelA
121 Clusters:
122 EOF
123     echo $clusters | jq -r '.[] | "- Name: \(.metadata.name)\n  KubeConfig: \(.file)"' >>plugin_fw_v2_values.yaml
124     cat <<EOF >>plugin_fw_v2_values.yaml
125 EmcoProviderNetwork: emco-private-net
126 UnprotectedProviderNetwork: unprotected-private-net
127 ProtectedNetwork: protected-private-net
128 Project: ${project}
129 LogicalCloud: lcadmin
130 CompositeApp: ${composite_app}
131 Version: ${version}
132 PackagesPath: ${CSAR_DIR}/${csar_id}
133 CompositeProfile: vfw_composite-profile
134 DeploymentIntentGroup: ${deployment_intent_group}
135 Release: fw0
136 DeploymentIntentsInGroup: vfw_deploy_intents
137 GenericPlacementIntent: generic-placement-intent
138 OvnActionIntent: vfw_ovnaction_intent
139 EOF
140 }
141
142 function call_emcoctl {
143     rc=$1
144     shift
145     # retry due to known issue with emcoctl and instantiating/terminating multiple resources
146     try=0
147     until [[ $(emcoctl $@ | awk '/Response Code:/ {code=$3} END{print code}') =~ $rc ]]; do
148         if [[ $try -lt 10 ]]; then
149             sleep 1s
150         else
151             return 1
152         fi
153         try=$((try + 1))
154     done
155     return 0
156 }
157
158 function createData {
159     call_emcoctl 2.. --config plugin_fw_v2_config.yaml apply -f plugin_fw_v2.yaml -v plugin_fw_v2_values.yaml
160 }
161
162 function getData {
163     emcoctl --config plugin_fw_v2_config.yaml get -f plugin_fw_v2.yaml -v plugin_fw_v2_values.yaml
164 }
165
166 function deleteData {
167     call_emcoctl 4.. --config plugin_fw_v2_config.yaml delete -f plugin_fw_v2.yaml -v plugin_fw_v2_values.yaml
168 }
169
170 function statusVfw {
171     emcoctl --config plugin_fw_v2_config.yaml get projects/${project}/composite-apps/${composite_app}/${version}/deployment-intent-groups/${deployment_intent_group}/status
172 }
173
174 function waitForVfw {
175     for try in {0..59}; do
176         sleep 1
177         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)"
178         echo "$(date +%H:%M:%S) - Filter=[$*] : $new_phase"
179         if [[ "$new_phase" == "$1" ]]; then
180             return 0
181         fi
182     done
183 }
184
185 function usage {
186     echo "Usage: $0 setup|create|get|destroy|status"
187     echo "    setup - creates the emcoctl files and packages needed for vfw"
188     echo "    create - creates all ncm, ovnaction, clm resources needed for vfw"
189     echo "    get - queries all resources in ncm, ovnaction, clm resources created for vfw"
190     echo "    destroy - deletes all resources in ncm, ovnaction, clm resources created for vfw"
191     echo "    status - get status of deployed resources"
192     echo ""
193     echo "    a reasonable test sequence:"
194     echo "    1.  setup"
195     echo "    2.  create"
196     echo "    3.  destroy"
197
198     exit
199 }
200
201 if [[ "$#" -gt 0 ]] ; then
202     case "$1" in
203         "setup" ) setup ;;
204         "create" ) createData ;;
205         "get" ) getData ;;
206         "status" ) statusVfw ;;
207         "wait" ) waitForVfw "Instantiated" ;;
208         "delete" ) deleteData ;;
209         *) usage ;;
210     esac
211 else
212     setup
213     createData
214
215     print_msg "[BEGIN] Basic checks for instantiated resource"
216     print_msg "Wait for deployment to be instantiated"
217     waitForVfw "Instantiated"
218     for name in $(cluster_names); do
219         print_msg "Check that networks were created on cluster $name"
220         file=$(cluster_file "$name")
221         KUBECONFIG=$file kubectl get network protected-private-net -o name
222         KUBECONFIG=$file kubectl get providernetwork emco-private-net -o name
223         KUBECONFIG=$file kubectl get providernetwork unprotected-private-net -o name
224     done
225     # Give some time for the Pods to show up on the clusters.  kubectl
226     # wait may return with "error: no matching resources found" if the
227     # Pods have not started yet.
228     sleep 30s
229     for name in $(cluster_names); do
230         print_msg "Wait for all pods to start on cluster $name"
231         file=$(cluster_file "$name")
232         KUBECONFIG=$file kubectl wait pod -l release=fw0 --for=condition=Ready --timeout=5m
233     done
234     # TODO: Provide some health check to verify vFW work
235     print_msg "Not waiting for vFW to fully install as no further checks are implemented in testcase"
236     #print_msg "Waiting 8minutes for vFW installation"
237     #sleep 8m
238     print_msg "[END] Basic checks for instantiated resource"
239
240     print_msg "Delete deployment"
241     deleteData
242 fi