X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=test%2Fcommon%2Fkube_proxy_api_functions.sh;h=dcaaf80258d555a52b4b26d5a5c52e8d3e60c88f;hb=663566c28930429775ea9921f0e32ddf5253da28;hp=fc9764d321342863bbcdb85097a4c5175725ee20;hpb=edea18a8fda2e2201cb3ede7f7af13f610bf4acc;p=nonrtric.git diff --git a/test/common/kube_proxy_api_functions.sh b/test/common/kube_proxy_api_functions.sh index fc9764d3..dcaaf802 100644 --- a/test/common/kube_proxy_api_functions.sh +++ b/test/common/kube_proxy_api_functions.sh @@ -26,7 +26,7 @@ # arg: (selects staging, snapshot, release etc) # is present only for images with staging, snapshot,release tags __KUBEPROXY_imagesetup() { - __check_and_create_image_var KUBEPROXY "KUBE_PROXY_IMAGE" "KUBE_PROXY_IMAGE_BASE" "KUBE_PROXY_IMAGE_TAG" REMOTE_PROXY "$KUBE_PROXY_DISPLAY_NAME" + __check_and_create_image_var KUBEPROXY "KUBE_PROXY_IMAGE" "KUBE_PROXY_IMAGE_BASE" "KUBE_PROXY_IMAGE_TAG" LOCAL "$KUBE_PROXY_DISPLAY_NAME" } # Pull image from remote repo or use locally built image @@ -35,20 +35,39 @@ __KUBEPROXY_imagesetup() { # Shall be used for images that does not allow overriding # Both var may contain: 'remote', 'remote-remove' or 'local' __KUBEPROXY_imagepull() { - __check_and_pull_image $2 "$KUBE_PROXY_DISPLAY_NAME" $KUBE_PROXY_APP_NAME $KUBE_PROXY_IMAGE + echo -e $RED"Image for app KUBEPROXY shall never be pulled from remote repo"$ERED } # Build image (only for simulator or interfaces stubs owned by the test environment) # arg: (selects staging, snapshot, release etc) # is present only for images with staging, snapshot,release tags __KUBEPROXY_imagebuild() { - echo -e $RED"Image for app KUBEPROXY shall never be built"$ERED + cd ../http-https-proxy + echo " Building KUBEPROXY - $KUBE_PROXY_DISPLAY_NAME - image: $KUBE_PROXY_IMAGE" + docker build --build-arg NEXUS_PROXY_REPO=$NEXUS_PROXY_REPO -t $KUBE_PROXY_IMAGE . &> .dockererr + if [ $? -eq 0 ]; then + echo -e $GREEN" Build Ok"$EGREEN + __retag_and_push_image KUBE_PROXY_IMAGE + if [ $? -ne 0 ]; then + exit 1 + fi + else + echo -e $RED" Build Failed"$ERED + ((RES_CONF_FAIL++)) + cat .dockererr + echo -e $RED"Exiting...."$ERED + exit 1 + fi } # Generate a string for each included image using the app display name and a docker images format string +# If a custom image repo is used then also the source image from the local repo is listed # arg: __KUBEPROXY_image_data() { echo -e "$KUBE_PROXY_DISPLAY_NAME\t$(docker images --format $1 $KUBE_PROXY_IMAGE)" >> $2 + if [ ! -z "$KUBE_PROXY_IMAGE_SOURCE" ]; then + echo -e "-- source image --\t$(docker images --format $1 $KUBE_PROXY_IMAGE_SOURCE)" >> $2 + fi } # Scale kubernetes resources to zero @@ -61,7 +80,7 @@ __KUBEPROXY_kube_scale_zero() { # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action. # This function is called for prestarted apps not managed by the test script. __KUBEPROXY_kube_scale_zero_and_wait() { - echo -e $RED" Http proxy replicas kept as is"$ERED + echo -e $RED" KUBEPROXY app is not scaled in this state"$ERED } # Delete all kube resouces for the app @@ -74,23 +93,84 @@ __KUBEPROXY_kube_delete_all() { # This function is called for apps managed by the test script. # args: __KUBEPROXY_store_docker_logs() { - docker logs $KUBE_PROXY_APP_NAME > $1$2_kubeproxy.log 2>&1 + if [ $RUNMODE == "KUBE" ]; then + kubectl logs -l "autotest=KUBEPROXY" -n $KUBE_SIM_NAMESPACE --tail=-1 > $1$2_kubeproxy.log 2>&1 + else + docker logs $KUBE_PROXY_APP_NAME > $1$2_kubeproxy.log 2>&1 + fi } -####################################################### +# Initial setup of protocol, host and ports +# This function is called for apps managed by the test script. +# args: - +__KUBEPROXY_initial_setup() { + use_kube_proxy_http +} +####################################################### -## Access to Kube Http Proxy -# Host name may be changed if app started by kube -# Direct access from script +## Access to Kube http proxy +# Direct access KUBE_PROXY_HTTPX="http" -KUBE_PROXY_HOST_NAME=$LOCALHOST_NAME -KUBE_PROXY_PATH=$KUBE_PROXY_HTTPX"://"$KUBE_PROXY_HOST_NAME":"$KUBE_PROXY_WEB_EXTERNAL_PORT + + +# Set http as the protocol to use for all communication to the Kube http proxy +# args: - +# (Function for test scripts) +use_kube_proxy_http() { + echo -e $BOLD"$KUBE_PROXY_DISPLAY_NAME protocol setting"$EBOLD + echo -e " Using $BOLD http $EBOLD towards Kube http proxy" + + KUBE_PROXY_HTTPX="http" + + echo -e $YELLOW" This setting cannot be changed once the kube proxy is started"$EYELLOW + echo "" +} + +# Set https as the protocol to use for all communication to the Kube http proxy +# args: - +# (Function for test scripts) +use_kube_proxy_https() { + echo -e $BOLD"$KUBE_PROXY_DISPLAY_NAME protocol setting"$EBOLD + echo -e " Using $BOLD https $EBOLD towards Kube http proxy" + + KUBE_PROXY_HTTPX="https" + + echo -e $YELLOW" This setting cannot be changed once the kube proxy is started"$EYELLOW + echo "" +} ######################### -### Http Proxy functions +### Kube Http Proxy functions ######################### +# Export env vars for config files, docker compose and kube resources +# args: - +__kube_proxy_vars() { + + export KUBE_PROXY_WEB_EXTERNAL_PORT + export KUBE_PROXY_WEB_INTERNAL_PORT + export KUBE_PROXY_EXTERNAL_PORT + export KUBE_PROXY_INTERNAL_PORT + + export KUBE_PROXY_WEB_EXTERNAL_SECURE_PORT + export KUBE_PROXY_WEB_INTERNAL_SECURE_PORT + export KUBE_PROXY_EXTERNAL_SECURE_PORT + export KUBE_PROXY_INTERNAL_SECURE_PORT + + export KUBE_SIM_NAMESPACE + export KUBE_PROXY_IMAGE + + export KUBE_PROXY_APP_NAME + export KUBE_PROXY_DOCKER_EXTERNAL_PORT + export KUBE_PROXY_DOCKER_EXTERNAL_SECURE_PORT + export KUBE_PROXY_WEB_DOCKER_EXTERNAL_PORT + export KUBE_PROXY_WEB_DOCKER_EXTERNAL_SECURE_PORT + export DOCKER_SIM_NWNAME + + export KUBE_PROXY_DISPLAY_NAME +} + # Start the Kube Http Proxy in the simulator group # args: - # (Function for test scripts) @@ -128,13 +208,10 @@ start_kube_proxy() { if [ $retcode_i -eq 0 ]; then echo -e " Creating $KUBE_PROXY_APP_NAME deployment and service" + + __kube_proxy_vars + export KUBE_PROXY_APP_NAME - export KUBE_PROXY_WEB_EXTERNAL_PORT - export KUBE_PROXY_WEB_INTERNAL_PORT - export KUBE_PROXY_EXTERNAL_PORT - export KUBE_PROXY_INTERNAL_PORT - export KUBE_SIM_NAMESPACE - export KUBE_PROXY_IMAGE __kube_create_namespace $KUBE_SIM_NAMESPACE @@ -151,31 +228,96 @@ start_kube_proxy() { fi echo " Retrieving host and ports for service..." - KUBE_PROXY_HOST_NAME=$(__kube_get_service_host $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE) - KUBE_PROXY_WEB_EXTERNAL_PORT=$(__kube_get_service_port $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "web") - KUBE_PROXY_EXTERNAL_PORT=$(__kube_get_service_port $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "http") + + #Finding host of the proxy + echo " Trying to find svc hostname..." + CLUSTER_KUBE_PROXY_HOST=$(__kube_cmd_with_timeout "kubectl get svc $KUBE_PROXY_APP_NAME -n $KUBE_SIM_NAMESPACE -o jsonpath={.status.loadBalancer.ingress[0].hostname}") - minikube status > /dev/null - if [ $? -eq 0 ]; then - echo -e $GREEN" Running minikube inside a kubernetes cluster. No proxy needed for the test script to access services"$EGREEN - export CLUSTER_KUBE_PROXY_NODEPORT="" + if [ "$CLUSTER_KUBE_PROXY_HOST" == "localhost" ]; then + #Local host found + echo -e $YELLOW" The test environment svc $KUBE_PROXY_APP_NAME host is: $CLUSTER_KUBE_PROXY_HOST"$EYELLOW + CLUSTER_KUBE_PROXY_HOST="127.0.0.1" else - echo -e $YELLOW" Running outside the kubernetes cluster. Proxy is setup to access services from the test script"$EYELLOW - export CLUSTER_KUBE_PROXY_NODEPORT=$(__kube_get_service_nodeport $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "http") + if [ -z "$CLUSTER_KUBE_PROXY_HOST" ]; then + #Host of proxy not found, trying to find the ip.... + echo " Trying to find svc ip..." + CLUSTER_KUBE_PROXY_HOST=$(__kube_cmd_with_timeout "kubectl get svc $KUBE_PROXY_APP_NAME -n $KUBE_SIM_NAMESPACE -o jsonpath={.status.loadBalancer.ingress[0].ip}") + if [ ! -z "$CLUSTER_KUBE_PROXY_HOST" ]; then + #Host ip found + echo -e $YELLOW" The test environment svc $KUBE_PROXY_APP_NAME ip is: $CLUSTER_KUBE_PROXY_HOST."$EYELLOW + fi + else + #Host or ip of proxy found + echo -e $YELLOW" The test environment host/ip is: $CLUSTER_KUBE_PROXY_HOST."$EYELLOW + fi fi - KUBE_PROXY_PATH=$KUBE_PROXY_HTTPX"://"$KUBE_PROXY_HOST_NAME":"$KUBE_PROXY_WEB_EXTERNAL_PORT - KUBE_PROXY_CONFIG_PORT=$KUBE_PROXY_EXTERNAL_PORT - KUBE_PROXY_CONFIG_HOST_NAME=$KUBE_PROXY_APP_NAME"."$KUBE_SIM_NAMESPACE + PORT_KEY_PREFIX="" + if [ $KUBE_PROXY_HTTPX == "https" ]; then + PORT_KEY_PREFIX="s" #add suffix to port key name to get https ports + fi + if [ -z "$CLUSTER_KUBE_PROXY_HOST" ]; then + #Host/ip of proxy not found, try to use the cluster and the nodeports of the proxy + CLUSTER_KUBE_PROXY_HOST=$(kubectl config view -o jsonpath={.clusters[0].cluster.server} | awk -F[/:] '{print $4}') + echo -e $YELLOW" The test environment cluster ip is: $CLUSTER_KUBE_PROXY_HOST."$EYELLOW + CLUSTER_KUBE_PROXY_PORT=$(__kube_get_service_nodeport $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "http$PORT_KEY_PREFIX") # port for proxy access + 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 + 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" + else + #Find the service ports of the proxy + CLUSTER_KUBE_PROXY_PORT=$(__kube_get_service_port $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "http$PORT_KEY_PREFIX") # port for proxy access + 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 + 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" + fi + + KUBE_PROXY_WEB_PATH=$KUBE_PROXY_HTTPX"://"$CLUSTER_KUBE_PROXY_HOST":"$KUBE_PROXY_WEB_NODEPORT - echo " Host IP, http port, cluster http nodeport (may be empty): $KUBE_PROXY_HOST_NAME $KUBE_PROXY_WEB_EXTERNAL_PORT $CLUSTER_KUBE_PROXY_NODEPORT" + export KUBE_PROXY_PATH= # Make sure proxy is empty when checking the proxy itself + __check_service_start $KUBE_PROXY_APP_NAME $KUBE_PROXY_WEB_PATH$KUBE_PROXY_ALIVE_URL - __check_service_start $KUBE_PROXY_APP_NAME $KUBE_PROXY_PATH$KUBE_PROXY_ALIVE_URL + # Set proxy for all subsequent calls for all services etc + export KUBE_PROXY_PATH=$KUBE_PROXY_HTTPX"://"$CLUSTER_KUBE_PROXY_HOST":"$CLUSTER_KUBE_PROXY_PORT + export KUBE_PROXY_HTTPX + + KP_PORT1=$(__kube_get_service_nodeport $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "http") + KP_PORT2=$(__kube_get_service_nodeport $KUBE_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "https") + + echo " $KUBE_PROXY_DISPLAY_NAME node ports (http/https): $KP_PORT1 $KP_PORT2" else - echo $YELLOW" Kube http proxy not needed in docker test. App not started" + # Check if docker app shall be fully managed by the test script + __check_included_image 'KUBEPROXY' + if [ $? -eq 1 ]; then + echo -e $RED"The Kube Proxy app is not included in this test script"$ERED + echo -e $RED"The Kube Proxy will not be started"$ERED + exit + fi + + __kube_proxy_vars + + __start_container $KUBE_PROXY_COMPOSE_DIR "" NODOCKERARGS 1 $KUBE_PROXY_APP_NAME + + if [ $KUBE_PROXY_HTTPX == "http" ]; then + export KUBE_PROXY_WEB_PATH=$KUBE_PROXY_HTTPX"://"$LOCALHOST_NAME":"$KUBE_PROXY_WEB_DOCKER_EXTERNAL_PORT + else + export KUBE_PROXY_WEB_PATH=$KUBE_PROXY_HTTPX"://"$LOCALHOST_NAME":"$KUBE_PROXY_WEB_DOCKER_EXTERNAL_SECURE_PORT + fi + + export KUBE_PROXY_PATH= # Make sure proxy is empty when checking the proxy itself + __check_service_start $KUBE_PROXY_APP_NAME $KUBE_PROXY_WEB_PATH$KUBE_PROXY_ALIVE_URL + + if [ $KUBE_PROXY_HTTPX == "http" ]; then + export KUBE_PROXY_PATH=$KUBE_PROXY_HTTPX"://"$LOCALHOST_NAME":"$KUBE_PROXY_DOCKER_EXTERNAL_PORT + else + export KUBE_PROXY_PATH=$KUBE_PROXY_HTTPX"://"$LOCALHOST_NAME":"$KUBE_PROXY_DOCKER_EXTERNAL_SECURE_PORT + fi + + echo " $KUBE_PROXY_DISPLAY_NAME localhost ports (http/https): $KUBE_PROXY_DOCKER_EXTERNAL_PORT $KUBE_PROXY_DOCKER_EXTERNAL_SECURE_PORT" + + fi echo "" + }