Update OSM installer to allow an external IP address for the cluster 05/14605/12
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 27 Sep 2024 09:34:55 +0000 (11:34 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 9 Oct 2024 09:04:09 +0000 (11:04 +0200)
Change-Id: Ic760f427dddbaf50532c87074834585b4674cd7e
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
Signed-off-by: ramonsalguer <javier.ramon@telefonica.com>
installers/full_install_osm.sh
installers/install_k3s_cluster.sh
installers/install_osm.sh

index c3fbb8d..f6f30e0 100755 (executable)
@@ -187,9 +187,10 @@ function install_osmclient(){
         python3 -m pip install -r /usr/lib/python3/dist-packages/osmclient/requirements.txt
     fi
     echo -e "\nOSM client installed"
-    echo -e "OSM client assumes that OSM host is running in localhost (127.0.0.1)."
-    echo -e "In case you want to interact with a different OSM host, you will have to configure this env variable in your .bashrc file:"
-    echo "     export OSM_HOSTNAME=nbi.${OSM_DEFAULT_IP}.nip.io"
+    echo "You can get the OSM NBI endpoint using the following command"
+    echo '     kubectl -n osm get ingress nbi-ingress -o jsonpath="{.spec.rules[0].host}"'
+    echo "You will have to configure this env variable in your .bashrc file:"
+    echo "     export OSM_HOSTNAME=nbi.${OSM_K8S_EXTERNAL_IP}.nip.io"
     [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
     return 0
 }
@@ -222,10 +223,10 @@ EOF"
     [ ! "$OSM_DOCKER_TAG" == "testing-daily" ] && OSM_HELM_OPTS="${OSM_HELM_OPTS} --set-string global.image.tag=${OSM_DOCKER_TAG}"
     [ ! "$OSM_DOCKER_TAG" == "testing-daily" ] && OSM_HELM_OPTS="${OSM_HELM_OPTS} --set prometheus.server.sidecarContainers.prometheus-config-sidecar.image=${DOCKER_REGISTRY_URL}${DOCKER_USER}/prometheus:${OSM_DOCKER_TAG}"
 
-    OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.hostname=${OSM_DEFAULT_IP}.nip.io"
-    OSM_HELM_OPTS="${OSM_HELM_OPTS} --set grafana.ingress.hosts={grafana.${OSM_DEFAULT_IP}.nip.io}"
-    OSM_HELM_OPTS="${OSM_HELM_OPTS} --set prometheus.server.ingress.hosts={prometheus.${OSM_DEFAULT_IP}.nip.io}"
-    # OSM_HELM_OPTS="${OSM_HELM_OPTS} --set prometheus.alertmanager.ingress.hosts={alertmanager.${OSM_DEFAULT_IP}.nip.io}"
+    OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.hostname=${OSM_K8S_EXTERNAL_IP}.nip.io"
+    OSM_HELM_OPTS="${OSM_HELM_OPTS} --set grafana.ingress.hosts={grafana.${OSM_K8S_EXTERNAL_IP}.nip.io}"
+    OSM_HELM_OPTS="${OSM_HELM_OPTS} --set prometheus.server.ingress.hosts={prometheus.${OSM_K8S_EXTERNAL_IP}.nip.io}"
+    # OSM_HELM_OPTS="${OSM_HELM_OPTS} --set prometheus.alertmanager.ingress.hosts={alertmanager.${OSM_K8S_EXTERNAL_IP}.nip.io}"
     if [ -z "${INSTALL_MGMT_CLUSTER}" ]; then
         OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.gitops.enabled=false"
     else
@@ -290,7 +291,7 @@ function deploy_mongodb() {
 
 function install_osm_ngsa_service() {
     [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
-    NGSA_OPTS="-i ${OSM_DEFAULT_IP} -d ${OSM_HELM_WORK_DIR} -D ${OSM_DEVOPS} -s ${OSM_NAMESPACE} -t ${OSM_DOCKER_TAG} -U ${DOCKER_USER} ${DEBUG_INSTALL}"
+    NGSA_OPTS="-i ${OSM_K8S_EXTERNAL_IP} -d ${OSM_HELM_WORK_DIR} -D ${OSM_DEVOPS} -s ${OSM_NAMESPACE} -t ${OSM_DOCKER_TAG} -U ${DOCKER_USER} ${DEBUG_INSTALL}"
     [ -n "${DOCKER_REGISTRY_URL}" ] && NGSA_OPTS="${NGSA_OPTS} -r ${DOCKER_REGISTRY_URL}"
     $OSM_DEVOPS/installers/install_ngsa.sh ${NGSA_OPTS} || \
     FATAL_TRACK install_osm_ngsa_service "install_ngsa.sh failed"
@@ -300,7 +301,7 @@ function install_osm_ngsa_service() {
 function add_local_k8scluster() {
     [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
     # OSM_HOSTNAME=$(kubectl get --namespace osm -o jsonpath="{.spec.rules[0].host}" ingress nbi-ingress)
-    OSM_HOSTNAME="nbi.${OSM_DEFAULT_IP}.nip.io:443"
+    OSM_HOSTNAME="nbi.${OSM_K8S_EXTERNAL_IP}.nip.io:443"
     /usr/bin/osm --hostname ${OSM_HOSTNAME} --all-projects vim-create \
       --name _system-osm-vim \
       --account_type dummy \
@@ -437,6 +438,7 @@ function install_k8s_cluster() {
         FATAL_TRACK k8scluster "install_cluster_addons.sh failed for kubeadm cluster"
     elif [ "${K8S_CLUSTER_ENGINE}" == "k3s" ]; then
         K3S_INSTALL_OPTS="-i ${OSM_DEFAULT_IP} -D ${OSM_DEVOPS} ${DEBUG_INSTALL}"
+        [ "${OSM_K8S_EXTERNAL_IP}" != "${OSM_DEFAULT_IP}" ] && K3S_INSTALL_OPTS="${K3S_INSTALL_OPTS} -e ${OSM_K8S_EXTERNAL_IP}"
         [ -n "${DOCKER_PROXY_URL}" ] && K3S_INSTALL_OPTS="${K3S_INSTALL_OPTS} -p ${DOCKER_PROXY_URL}"
         [ -n "${DOCKER_REGISTRY_URL}" ] && K3S_INSTALL_OPTS="${K3S_INSTALL_OPTS} -d ${DOCKER_REGISTRY_URL}"
         [ -n "${DOCKER_REGISTRY_USER}" ] && K3S_INSTALL_OPTS="${K3S_INSTALL_OPTS} -u ${DOCKER_REGISTRY_USER}"
@@ -501,6 +503,7 @@ function install_osm() {
     [ -z "$OSM_DEFAULT_IF" ] && FATAL "Not possible to determine the interface with the default route 0.0.0.0"
     OSM_DEFAULT_IP=`ip -o -4 a s ${OSM_DEFAULT_IF} |awk '{split($4,a,"/"); print a[1]; exit}'`
     [ -z "$OSM_DEFAULT_IP" ] && FATAL "Not possible to determine the IP address of the interface with the default route"
+    OSM_K8S_EXTERNAL_IP=${OSM_K8S_EXTERNAL_IP:-${OSM_DEFAULT_IP}}
 
     # configure apt proxy
     [ -n "$APT_PROXY_URL" ] && configure_apt_proxy $APT_PROXY_URL
@@ -697,6 +700,7 @@ function dump_vars(){
     echo "OPENSTACK_VM_NAME"="$OPENSTACK_VM_NAME"
     echo "OSM_DEVOPS=$OSM_DEVOPS"
     echo "OSM_DOCKER_TAG=$OSM_DOCKER_TAG"
+    echo "OSM_K8S_EXTERNAL_IP=$OSM_K8S_EXTERNAL_IP"
     echo "OSM_HELM_WORK_DIR=$OSM_HELM_WORK_DIR"
     echo "OSM_NAMESPACE=$OSM_NAMESPACE"
     echo "OSM_VCA_HOST=$OSM_VCA_HOST"
@@ -802,7 +806,7 @@ DOCKER_PROXY_URL=
 MODULE_DOCKER_TAG=
 OSM_INSTALLATION_TYPE="Default"
 
-while getopts ":a:c:r:n:k:u:R:D:o:O:N:H:S:s:t:U:P:A:l:L:K:d:p:T:f:F:G:M:-: hy" o; do
+while getopts ":a:c:e:r:n:k:u:R:D:o:O:N:H:S:s:t:U:P:A:l:L:K:d:p:T:f:F:G:M:-: hy" o; do
     case "${o}" in
         a)
             APT_PROXY_URL=${OPTARG}
@@ -814,6 +818,9 @@ while getopts ":a:c:r:n:k:u:R:D:o:O:N:H:S:s:t:U:P:A:l:L:K:d:p:T:f:F:G:M:-: hy" o
             echo -e "Invalid argument for -c : ' ${K8S_CLUSTER_ENGINE}'\n" >&2
             usage && exit 1
             ;;
+        e)
+            OSM_K8S_EXTERNAL_IP="${OPTARG}"
+            ;;
         r)
             REPOSITORY="${OPTARG}"
             REPO_ARGS+=(-r "$REPOSITORY")
index e117ab4..3fc0b4a 100755 (executable)
@@ -46,7 +46,34 @@ EOF
 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 @@ function save_kubeconfig() {
 }
 
 # 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_PROXY_URL=${DOCKER_PROXY_URL=-}
 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
index ba116cc..622ff36 100755 (executable)
@@ -135,7 +135,7 @@ EOF"
     [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
 }
 
-while getopts ":a:c:r:n:k:u:R:D:o:O:N:H:S:s:t:U:P:A:l:L:K:d:p:T:f:F:G:M:-: hy" o; do
+while getopts ":a:c:e:r:n:k:u:R:D:o:O:N:H:S:s:t:U:P:A:l:L:K:d:p:T:f:F:G:M:-: hy" o; do
 
     case "${o}" in
         D)