Updated test env documentation
[nonrtric.git] / test / common / kubeproxy_api_functions.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2020 Nordix Foundation. All rights reserved.
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 #  ============LICENSE_END=================================================
18 #
19
20 # This is a script that contains container/service management functions for Kube Http Proxy
21 # This http proxy is to provide full access for the test script to all addressable kube object in a cluster
22
23 ################ Test engine functions ################
24
25 # Create the image var used during the test
26 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
27 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
28 __KUBEPROXY_imagesetup() {
29         __check_and_create_image_var KUBEPROXY "KUBE_PROXY_IMAGE" "KUBE_PROXY_IMAGE_BASE" "KUBE_PROXY_IMAGE_TAG" LOCAL "$KUBE_PROXY_DISPLAY_NAME" $IMAGE_TARGET_PLATFORM_IMG_TAG
30 }
31
32 # Pull image from remote repo or use locally built image
33 # arg: <pull-policy-override> <pull-policy-original>
34 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
35 # <pull-policy-original> Shall be used for images that does not allow overriding
36 # Both var may contain: 'remote', 'remote-remove' or 'local'
37 __KUBEPROXY_imagepull() {
38         echo -e $RED"Image for app KUBEPROXY shall never be pulled from remote repo"$ERED
39 }
40
41 # Build image (only for simulator or interfaces stubs owned by the test environment)
42 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
43 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
44 __KUBEPROXY_imagebuild() {
45         cd ../http-https-proxy
46         echo " Building KUBEPROXY - $KUBE_PROXY_DISPLAY_NAME - image: $KUBE_PROXY_IMAGE"
47         docker build  $IMAGE_TARGET_PLATFORM_CMD_PARAM --build-arg NEXUS_PROXY_REPO=$NEXUS_PROXY_REPO -t $KUBE_PROXY_IMAGE . &> .dockererr
48         if [ $? -eq 0 ]; then
49                 echo -e  $GREEN"  Build Ok"$EGREEN
50                 __retag_and_push_image KUBE_PROXY_IMAGE
51                 if [ $? -ne 0 ]; then
52                         exit 1
53                 fi
54         else
55                 echo -e $RED"  Build Failed"$ERED
56                 ((RES_CONF_FAIL++))
57                 cat .dockererr
58                 echo -e $RED"Exiting...."$ERED
59                 exit 1
60         fi
61 }
62
63 # Generate a string for each included image using the app display name and a docker images format string
64 # If a custom image repo is used then also the source image from the local repo is listed
65 # arg: <docker-images-format-string> <file-to-append>
66 __KUBEPROXY_image_data() {
67         echo -e "$KUBE_PROXY_DISPLAY_NAME\t$(docker images --format $1 $KUBE_PROXY_IMAGE)" >>   $2
68         if [ ! -z "$KUBE_PROXY_IMAGE_SOURCE" ]; then
69                 echo -e "-- source image --\t$(docker images --format $1 $KUBE_PROXY_IMAGE_SOURCE)" >>   $2
70         fi
71 }
72
73 # Scale kubernetes resources to zero
74 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
75 # This function is called for apps fully managed by the test script
76 __KUBEPROXY_kube_scale_zero() {
77         __kube_scale_all_resources $KUBE_SIM_NAMESPACE autotest KUBEPROXY
78 }
79
80 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
81 # This function is called for pre-started apps not managed by the test script.
82 __KUBEPROXY_kube_scale_zero_and_wait() {
83         echo -e $RED" KUBEPROXY app is not scaled in this state"$ERED
84 }
85
86 # Delete all kube resources for the app
87 # This function is called for apps managed by the test script.
88 __KUBEPROXY_kube_delete_all() {
89         __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest KUBEPROXY
90 }
91
92 # Store docker logs
93 # This function is called for apps managed by the test script.
94 # args: <log-dir> <file-prefix>
95 __KUBEPROXY_store_docker_logs() {
96         if [ $RUNMODE == "KUBE" ]; then
97                 kubectl $KUBECONF  logs -l "autotest=KUBEPROXY" -n $KUBE_SIM_NAMESPACE --tail=-1 > $1$2_kubeproxy.log 2>&1
98         else
99                 docker logs $KUBE_PROXY_APP_NAME > $1$2_kubeproxy.log 2>&1
100         fi
101 }
102
103 # Initial setup of protocol, host and ports
104 # This function is called for apps managed by the test script.
105 # args: -
106 __KUBEPROXY_initial_setup() {
107         use_kube_proxy_http
108 }
109
110 # Set app short-name, app name and namespace for logging runtime statistics of kubernetes pods or docker containers
111 # For docker, the namespace shall be excluded
112 # This function is called for apps managed by the test script as well as for pre-started apps.
113 # args: -
114 __KUBEPROXY_statistics_setup() {
115         if [ $RUNMODE == "KUBE" ]; then
116                 echo "KUBEPROXXY $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE"
117         else
118                 echo "KUBEPROXXY $KUBE_PROXY_APP_NAME"
119         fi
120 }
121
122 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
123 # args: -
124 __KUBEPROXY_test_requirements() {
125         :
126 }
127
128 #######################################################
129
130 ## Access to Kube http proxy
131 # Direct access
132 KUBE_PROXY_HTTPX="http"
133
134
135 # Set http as the protocol to use for all communication to the Kube http proxy
136 # args: -
137 # (Function for test scripts)
138 use_kube_proxy_http() {
139         echo -e $BOLD"$KUBE_PROXY_DISPLAY_NAME protocol setting"$EBOLD
140         echo -e " Using $BOLD http $EBOLD towards Kube http proxy"
141
142         KUBE_PROXY_HTTPX="http"
143
144         echo -e $YELLOW" This setting cannot be changed once the kube proxy is started"$EYELLOW
145         echo ""
146 }
147
148 # Set https as the protocol to use for all communication to the Kube http proxy
149 # args: -
150 # (Function for test scripts)
151 use_kube_proxy_https() {
152         echo -e $BOLD"$KUBE_PROXY_DISPLAY_NAME protocol setting"$EBOLD
153         echo -e " Using $BOLD https $EBOLD towards Kube http proxy"
154
155         KUBE_PROXY_HTTPX="https"
156
157         echo -e $YELLOW" This setting cannot be changed once the kube proxy is started"$EYELLOW
158         echo ""
159 }
160
161 #########################
162 ### Kube Http Proxy functions
163 #########################
164
165 # Export env vars for config files, docker compose and kube resources
166 # args: -
167 __kube_proxy_vars() {
168
169         export KUBE_PROXY_WEB_EXTERNAL_PORT
170         export KUBE_PROXY_WEB_INTERNAL_PORT
171         export KUBE_PROXY_EXTERNAL_PORT
172         export KUBE_PROXY_INTERNAL_PORT
173
174         export KUBE_PROXY_WEB_EXTERNAL_SECURE_PORT
175         export KUBE_PROXY_WEB_INTERNAL_SECURE_PORT
176         export KUBE_PROXY_EXTERNAL_SECURE_PORT
177         export KUBE_PROXY_INTERNAL_SECURE_PORT
178
179         export KUBE_SIM_NAMESPACE
180         export KUBE_PROXY_IMAGE
181
182         export KUBE_PROXY_APP_NAME
183         export KUBE_PROXY_DOCKER_EXTERNAL_PORT
184         export KUBE_PROXY_DOCKER_EXTERNAL_SECURE_PORT
185         export KUBE_PROXY_WEB_DOCKER_EXTERNAL_PORT
186         export KUBE_PROXY_WEB_DOCKER_EXTERNAL_SECURE_PORT
187         export DOCKER_SIM_NWNAME
188
189         export KUBE_PROXY_DISPLAY_NAME
190 }
191
192 # Start the Kube Http Proxy in the simulator group
193 # args: -
194 # (Function for test scripts)
195 start_kube_proxy() {
196
197         echo -e $BOLD"Starting $KUBE_PROXY_DISPLAY_NAME"$EBOLD
198
199         if [ $RUNMODE == "KUBE" ]; then
200
201                 # Check if app shall be fully managed by the test script
202                 __check_included_image "KUBEPROXY"
203                 retcode_i=$?
204
205                 # Check if app shall only be used by the test script
206                 __check_prestarted_image "KUBEPROXY"
207                 retcode_p=$?
208
209                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
210                         echo -e $RED"The $KUBE_PROXY_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
211                         echo -e $RED"The $KUBE_PROXY_APP_NAME will not be started"$ERED
212                         exit
213                 fi
214                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
215                         echo -e $RED"The $KUBE_PROXY_APP_NAME app is included both as managed and prestarted in this test script"$ERED
216                         echo -e $RED"The $KUBE_PROXY_APP_NAME will not be started"$ERED
217                         exit
218                 fi
219
220                 # Check if app shall be used - not managed - by the test script
221                 if [ $retcode_p -eq 0 ]; then
222                         echo -e " Using existing $KUBE_PROXY_APP_NAME deployment and service"
223                         echo " Setting KUBEPROXY replicas=1"
224                         __kube_scale deployment $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE 1
225                 fi
226
227                 if [ $retcode_i -eq 0 ]; then
228                         echo -e " Creating $KUBE_PROXY_APP_NAME deployment and service"
229
230                         __kube_proxy_vars
231
232                         export KUBE_PROXY_APP_NAME
233
234                         __kube_create_namespace $KUBE_SIM_NAMESPACE
235
236                         # Create service
237                         input_yaml=$SIM_GROUP"/"$KUBE_PROXY_COMPOSE_DIR"/"svc.yaml
238                         output_yaml=$PWD/tmp/proxy_svc.yaml
239                         __kube_create_instance service $KUBE_PROXY_APP_NAME $input_yaml $output_yaml
240
241                         # Create app
242                         input_yaml=$SIM_GROUP"/"$KUBE_PROXY_COMPOSE_DIR"/"app.yaml
243                         output_yaml=$PWD/tmp/proxy_app.yaml
244                         __kube_create_instance app $KUBE_PROXY_APP_NAME $input_yaml $output_yaml
245
246                 fi
247
248                 echo " Retrieving host and ports for service..."
249
250                 # #Keeping this old code for reference
251                 # #Finding host of the proxy
252                 # echo "  Trying to find svc hostname..."
253                 # CLUSTER_KUBE_PROXY_HOST=$(__kube_cmd_with_timeout "kubectl $KUBECONF get svc $KUBE_PROXY_APP_NAME -n $KUBE_SIM_NAMESPACE  -o jsonpath={.status.loadBalancer.ingress[0].hostname}")
254
255
256                 # if [ "$CLUSTER_KUBE_PROXY_HOST" == "localhost" ]; then
257                 #       #Local host found
258                 #       echo -e $YELLOW" The test environment svc $KUBE_PROXY_APP_NAME host is: $CLUSTER_KUBE_PROXY_HOST"$EYELLOW
259                 #       CLUSTER_KUBE_PROXY_HOST="127.0.0.1"
260                 # else
261                 #       if [ -z "$CLUSTER_KUBE_PROXY_HOST" ]; then
262                 #               #Host of proxy not found, trying to find the ip....
263                 #               echo "  Trying to find svc ip..."
264                 #               CLUSTER_KUBE_PROXY_HOST=$(__kube_cmd_with_timeout "kubectl $KUBECONF get svc $KUBE_PROXY_APP_NAME -n $KUBE_SIM_NAMESPACE  -o jsonpath={.status.loadBalancer.ingress[0].ip}")
265                 #               if [ ! -z "$CLUSTER_KUBE_PROXY_HOST" ]; then
266                 #                       #Host ip found
267                 #                       echo -e $YELLOW" The test environment svc $KUBE_PROXY_APP_NAME ip is: $CLUSTER_KUBE_PROXY_HOST."$EYELLOW
268                 #               fi
269                 #       else
270                 #               #Host or ip of proxy found
271                 #               echo -e $YELLOW" The test environment host/ip is: $CLUSTER_KUBE_PROXY_HOST."$EYELLOW
272                 #       fi
273                 # fi
274
275                 # PORT_KEY_PREFIX=""
276                 # if [ $KUBE_PROXY_HTTPX == "https" ]; then
277                 #       PORT_KEY_PREFIX="s"  #add suffix to port key name to get https ports
278                 # fi
279                 # if [ -z "$CLUSTER_KUBE_PROXY_HOST" ]; then
280                 #       #Host/ip of proxy not found, try to use the cluster and the nodeports of the proxy
281                 #       CLUSTER_KUBE_PROXY_HOST=$(kubectl $KUBECONF config view -o jsonpath={.clusters[0].cluster.server} | awk -F[/:] '{print $4}')
282                 #       echo -e $YELLOW" The test environment cluster ip is: $CLUSTER_KUBE_PROXY_HOST."$EYELLOW
283                 #       CLUSTER_KUBE_PROXY_PORT=$(__kube_get_service_nodeport $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "http$PORT_KEY_PREFIX")  # port for proxy access
284                 #       KUBE_PROXY_WEB_NODEPORT=$(__kube_get_service_nodeport $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "web$PORT_KEY_PREFIX")  # web port, only for alive test
285                 #       echo " Cluster ip/host, cluster http$PORT_KEY_PREFIX nodeport, cluster web$PORT_KEY_PREFIX nodeport: $CLUSTER_KUBE_PROXY_HOST $CLUSTER_KUBE_PROXY_PORT $KUBE_PROXY_WEB_NODEPORT"
286                 # else
287                 #       #Find the service ports of the proxy
288                 #       CLUSTER_KUBE_PROXY_PORT=$(__kube_get_service_port $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "http$PORT_KEY_PREFIX")  # port for proxy access
289                 #       KUBE_PROXY_WEB_NODEPORT=$(__kube_get_service_port $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "web$PORT_KEY_PREFIX")  # web port, only for alive test
290                 #       echo " Proxy ip/host, proxy http$PORT_KEY_PREFIX port, proxy web$PORT_KEY_PREFIX port: $CLUSTER_KUBE_PROXY_HOST $CLUSTER_KUBE_PROXY_PORT $KUBE_PROXY_WEB_NODEPORT"
291                 # fi
292                 # #End of old code
293
294                 ########### New method to find host/ip to cluster/proxy
295                 # Basic principle if the ip/host of the svc for the proxy is found - use the proxy service ports towards that ip/host of the proxy.
296                 # If proxy ip/host is not found then find the cluster ip/host and use the proxy nodeports towards that ip/host of the cluster
297
298                 #Finding host/ip of the proxy
299                 echo "  Trying to find svc hostname..."
300                 CLUSTER_KUBE_PROXY_HOST=$(__kube_cmd_with_timeout "kubectl $KUBECONF get svc $KUBE_PROXY_APP_NAME -n $KUBE_SIM_NAMESPACE  -o jsonpath={.status.loadBalancer.ingress[0].hostname}")
301                 if [ -z "$CLUSTER_KUBE_PROXY_HOST" ]; then
302                         #Host of proxy not found, trying to find the ip....
303                         echo "  Svc hostname not found, trying to find svc ip..."
304                         CLUSTER_KUBE_PROXY_HOST=$(__kube_cmd_with_timeout "kubectl $KUBECONF get svc $KUBE_PROXY_APP_NAME -n $KUBE_SIM_NAMESPACE  -o jsonpath={.status.loadBalancer.ingress[0].ip}")
305                 fi
306                 PORT_KEY_PREFIX=""
307                 if [ $KUBE_PROXY_HTTPX == "https" ]; then
308                         PORT_KEY_PREFIX="s"  #add suffix to port key name to get https ports
309                 fi
310
311                 if [ -z "$CLUSTER_KUBE_PROXY_HOST" ]; then
312                         #Finding host/ip of the cluster
313                         echo "  Nor svc hostname or ip found, trying to find cluster host/ip from context..."
314                         __current_context=$(kubectl $KUBECONF config current-context)
315                         __cluster_name=$(kubectl $KUBECONF config view -o "jsonpath={.contexts[?(@.name=='"$__current_context"')].context.cluster}")
316                         __cluster_server=$(kubectl $KUBECONF config view -o "jsonpath={.clusters[?(@.name=='"$__cluster_name"')].cluster.server}")
317                         CLUSTER_KUBE_PROXY_HOST=$(echo $__cluster_server | awk -F[/:] '{print $4}')
318
319                         echo -e $YELLOW" The test environment cluster: $CLUSTER_KUBE_PROXY_HOST."$EYELLOW
320                         CLUSTER_KUBE_PROXY_PORT=$(__kube_get_service_nodeport $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "http$PORT_KEY_PREFIX")  # port for proxy access
321                         KUBE_PROXY_WEB_NODEPORT=$(__kube_get_service_nodeport $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "web$PORT_KEY_PREFIX")  # web port, only for alive test
322                         echo " Cluster ip/host, cluster http$PORT_KEY_PREFIX nodeport, cluster web$PORT_KEY_PREFIX nodeport: $CLUSTER_KUBE_PROXY_HOST $CLUSTER_KUBE_PROXY_PORT $KUBE_PROXY_WEB_NODEPORT"
323                 else
324                         echo -e $YELLOW" The test environment proxy: $CLUSTER_KUBE_PROXY_HOST."$EYELLOW
325                         CLUSTER_KUBE_PROXY_PORT=$(__kube_get_service_port $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "http$PORT_KEY_PREFIX")  # port for proxy access
326                         KUBE_PROXY_WEB_NODEPORT=$(__kube_get_service_port $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "web$PORT_KEY_PREFIX")  # web port, only for alive test
327                         echo " Proxy ip/host, proxy http$PORT_KEY_PREFIX port, proxy web$PORT_KEY_PREFIX port: $CLUSTER_KUBE_PROXY_HOST $CLUSTER_KUBE_PROXY_PORT $KUBE_PROXY_WEB_NODEPORT"
328                 fi
329                 ########### End of new method
330
331                 KUBE_PROXY_WEB_PATH=$KUBE_PROXY_HTTPX"://"$CLUSTER_KUBE_PROXY_HOST":"$KUBE_PROXY_WEB_NODEPORT
332
333                 export KUBE_PROXY_PATH=  # Make sure proxy is empty when checking the proxy itself
334                 __check_service_start $KUBE_PROXY_APP_NAME $KUBE_PROXY_WEB_PATH$KUBE_PROXY_ALIVE_URL
335
336                 # Set proxy for all subsequent calls for all services etc
337                 export KUBE_PROXY_PATH=$KUBE_PROXY_HTTPX"://"$CLUSTER_KUBE_PROXY_HOST":"$CLUSTER_KUBE_PROXY_PORT
338                 export KUBE_PROXY_HTTPX
339
340                 KP_PORT1=$(__kube_get_service_nodeport $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "http")
341                 KP_PORT2=$(__kube_get_service_nodeport $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "https")
342
343                 echo " $KUBE_PROXY_DISPLAY_NAME node ports (http/https): $KP_PORT1 $KP_PORT2"
344
345         else
346                 # Check if docker app shall be fully managed by the test script
347                 __check_included_image 'KUBEPROXY'
348                 if [ $? -eq 1 ]; then
349                         echo -e $RED"The Kube Proxy app is not included in this test script"$ERED
350                         echo -e $RED"The Kube Proxy will not be started"$ERED
351                         exit
352                 fi
353
354                 __kube_proxy_vars
355
356                 __start_container $KUBE_PROXY_COMPOSE_DIR "" NODOCKERARGS 1 $KUBE_PROXY_APP_NAME
357
358                 if [ $KUBE_PROXY_HTTPX == "http" ]; then
359                         export KUBE_PROXY_WEB_PATH=$KUBE_PROXY_HTTPX"://"$LOCALHOST_NAME":"$KUBE_PROXY_WEB_DOCKER_EXTERNAL_PORT
360                 else
361                         export KUBE_PROXY_WEB_PATH=$KUBE_PROXY_HTTPX"://"$LOCALHOST_NAME":"$KUBE_PROXY_WEB_DOCKER_EXTERNAL_SECURE_PORT
362                 fi
363
364                 export KUBE_PROXY_PATH=  # Make sure proxy is empty when checking the proxy itself
365         __check_service_start $KUBE_PROXY_APP_NAME $KUBE_PROXY_WEB_PATH$KUBE_PROXY_ALIVE_URL
366
367                 if [ $KUBE_PROXY_HTTPX == "http" ]; then
368                         export KUBE_PROXY_PATH=$KUBE_PROXY_HTTPX"://"$LOCALHOST_NAME":"$KUBE_PROXY_DOCKER_EXTERNAL_PORT
369                 else
370                         export KUBE_PROXY_PATH=$KUBE_PROXY_HTTPX"://"$LOCALHOST_NAME":"$KUBE_PROXY_DOCKER_EXTERNAL_SECURE_PORT
371                 fi
372
373                 echo " $KUBE_PROXY_DISPLAY_NAME localhost ports (http/https): $KUBE_PROXY_DOCKER_EXTERNAL_PORT $KUBE_PROXY_DOCKER_EXTERNAL_SECURE_PORT"
374
375
376         fi
377         echo ""
378
379 }
380
381 # Turn on debug logging in kubeproxy
382 # args: -
383 # (Function for test scripts)
384 set_kubeproxy_debug() {
385         echo -e $BOLD"Setting kubeproxy debug logging"$EBOLD
386         curlString="$KUBE_PROXY_WEB_PATH/debug -X PUT"
387         result=$(__do_curl_no_proxy "$curlString")
388         if [ $? -ne 0 ]; then
389                 __print_err "could not set debug logging" $@
390                 ((RES_CONF_FAIL++))
391                 return 1
392         fi
393         echo ""
394         return 0
395 }
396