Update OSM installer to allow an external IP address for the cluster

Change-Id: Ic760f427dddbaf50532c87074834585b4674cd7e
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
Signed-off-by: ramonsalguer <javier.ramon@telefonica.com>
diff --git a/installers/install_k3s_cluster.sh b/installers/install_k3s_cluster.sh
index e117ab4..3fc0b4a 100755
--- a/installers/install_k3s_cluster.sh
+++ b/installers/install_k3s_cluster.sh
@@ -46,7 +46,34 @@
 function install_k3s() {
     [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
     export INSTALL_K3S_EXEC="--disable traefik"
+
+    # Regular installation of K3s
     curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=${K8S_VERSION} sh -s -
+
+    # If specified a public IP, K3s service is updated accordingly and restarted
+    if [ -n "${K3S_PUBLIC_IP}" ]; then
+        # Back-up service config file to home
+        cp /etc/systemd/system/k3s.service ~/BASE-k3s.service
+
+        # Generate new service config file with additions for using a public IP
+        (
+            cat ~/BASE-k3s.service | sed '${/^$/d}'
+            echo -e "\t'--node-external-ip' \\"
+            echo -e "\t'${K3S_PUBLIC_IP}' \\"
+            echo
+        )| \
+        tee ~/PUBLIC-k3s.service
+
+        # Replace service config and apply
+        sudo cp ~/PUBLIC-k3s.service /etc/systemd/system/k3s.service
+        sudo systemctl daemon-reload
+        sudo systemctl restart k3s
+
+        # Cleanup
+        rm ~/BASE-k3s.service ~/PUBLIC-k3s.service
+    fi
+
+    # Make kubeconfig permissions less restrictive
     sudo chmod 644 /etc/rancher/k3s/k3s.yaml
     [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
 }
@@ -171,11 +198,14 @@
 }
 
 # main
-while getopts ":D:i:p:d:u:P:-: " o; do
+while getopts ":D:i:e:p:d:u:P:-: " o; do
     case "${o}" in
         i)
             DEFAULT_IP="${OPTARG}"
             ;;
+        e)
+            K3S_PUBLIC_IP="${OPTARG}"
+            ;;
         D)
             OSM_DEVOPS="${OPTARG}"
             ;;
@@ -217,12 +247,14 @@
 DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL=-}
 DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER=-}
 DOCKER_REGISTRY_PASSWORD=${DOCKER_REGISTRY_PASSWORD=-}
+K3S_PUBLIC_IP=${K3S_PUBLIC_IP:-}
 echo "DEBUG_INSTALL=${DEBUG_INSTALL}"
 echo "DEFAULT_IP=${DEFAULT_IP}"
 echo "OSM_DEVOPS=${OSM_DEVOPS}"
 echo "DOCKER_PROXY_URL=${DOCKER_PROXY_URL}"
 echo "DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL}"
 echo "DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER}"
+echo "K3S_PUBLIC_IP=${K3S_PUBLIC_IP}"
 echo "HOME=$HOME"
 
 source $OSM_DEVOPS/common/logging