From 1f3384830c68c5b752f826f5184b44a5f31a0b4a Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Thu, 4 Jul 2024 19:26:54 +0200 Subject: [PATCH] Set IP address in kubeconfig for install_k3s_cluster Change-Id: Ib525d6c441fcd187730e637a4e6be712d0777bb9 Signed-off-by: garciadeblas --- installers/full_install_osm.sh | 2 +- installers/install_k3s_cluster.sh | 24 +++++++++++++++++------- installers/install_kubeadm_cluster.sh | 25 ++++++++++++++++--------- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/installers/full_install_osm.sh b/installers/full_install_osm.sh index e75f2905..ba692c1c 100755 --- a/installers/full_install_osm.sh +++ b/installers/full_install_osm.sh @@ -423,7 +423,7 @@ function install_k8s_cluster() { $OSM_DEVOPS/installers/install_cluster_addons.sh ${K8SCLUSTER_ADDONS_INSTALL_OPTS} || \ FATAL_TRACK k8scluster "install_cluster_addons.sh failed for kubeadm cluster" elif [ "${K8S_CLUSTER_ENGINE}" == "k3s" ]; then - K3S_INSTALL_OPTS="-D ${OSM_DEVOPS} ${DEBUG_INSTALL}" + K3S_INSTALL_OPTS="-i ${OSM_DEFAULT_IP} -D ${OSM_DEVOPS} ${DEBUG_INSTALL}" # The K3s installation script will automatically take the HTTP_PROXY, HTTPS_PROXY and NO_PROXY, # as well as the CONTAINERD_HTTP_PROXY, CONTAINERD_HTTPS_PROXY and CONTAINERD_NO_PROXY variables # from the shell, if they are present, and write them to the environment file of k3s systemd service, diff --git a/installers/install_k3s_cluster.sh b/installers/install_k3s_cluster.sh index aa5d6f75..68f1b2b3 100755 --- a/installers/install_k3s_cluster.sh +++ b/installers/install_k3s_cluster.sh @@ -120,14 +120,16 @@ function check_for_readiness() { [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function } -# Retrieves and saves the credentials -function save_credentials() { +# Initializes kubeconfig file +function save_kubeconfig() { [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function KUBEDIR="${HOME}/.kube" KUBEFILE="$KUBEDIR/config" mkdir -p "${KUBEDIR}" - sudo cp /etc/rancher/k3s/k3s.yaml "${KUBEFILE}" + K3S_KUBECONFIG="/etc/rancher/k3s/k3s.yaml" + sudo cp "${K3S_KUBECONFIG}" "${KUBEFILE}" sudo chown $(id -u):$(id -g) "${KUBEFILE}" + sed -i "s#server: https://127.0.0.1#server: https://${DEFAULT_IP}#g" "${KUBEFILE}" chmod 700 "${KUBEFILE}" echo echo "Credentials saved at ${KUBEFILE}" @@ -136,8 +138,15 @@ function save_credentials() { } # main -while getopts ":D:-: " o; do +DEFAULT_IP="127.0.0.1" +OSM_DEVOPS="/usr/share/osm-devops" +DEBUG_INSTALL="" + +while getopts ":D:i:-: " o; do case "${o}" in + i) + DEFAULT_IP="${OPTARG}" + ;; D) OSM_DEVOPS="${OPTARG}" ;; @@ -163,8 +172,9 @@ done source $OSM_DEVOPS/common/logging source $OSM_DEVOPS/common/track -echo "DEBUG_INSTALL=$DEBUG_INSTALL" -echo "OSM_DEVOPS=$OSM_DEVOPS" +echo "DEBUG_INSTALL=${DEBUG_INSTALL}" +echo "DEFAULT_IP=${DEFAULT_IP}" +echo "OSM_DEVOPS=${OSM_DEVOPS}" echo "HOME=$HOME" install_k3s @@ -174,5 +184,5 @@ track k8scluster k3s_node_ready_ok # update_service_nodeport_range # check_for_readiness # track k8scluster k3s_update_nodeport_range_ok -save_credentials +save_kubeconfig track k8scluster k3s_creds_ok diff --git a/installers/install_kubeadm_cluster.sh b/installers/install_kubeadm_cluster.sh index c6072457..f0f49c2d 100755 --- a/installers/install_kubeadm_cluster.sh +++ b/installers/install_kubeadm_cluster.sh @@ -60,13 +60,19 @@ function init_kubeadm() { } # Initializes kubeconfig file -function kube_config_dir() { +function save_kubeconfig() { [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function K8S_MANIFEST_DIR="/etc/kubernetes/manifests" [ ! -d $K8S_MANIFEST_DIR ] && FATAL_TRACK k8scluster "Kubernetes folder $K8S_MANIFEST_DIR was not found" - mkdir -p $HOME/.kube - sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config - sudo chown $(id -u):$(id -g) $HOME/.kube/config + KUBEDIR="${HOME}/.kube" + KUBEFILE="$KUBEDIR/config" + mkdir -p "${KUBEDIR}" + KUBEADM_KUBECONFIG="/etc/kubernetes/admin.conf" + sudo cp "${KUBEADM_KUBECONFIG}" "${KUBEFILE}" + sudo chown $(id -u):$(id -g) "${KUBEFILE}" + echo + echo "Credentials saved at ${KUBEFILE}" + echo [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function } @@ -126,11 +132,12 @@ function remove_k8s_namespace() { [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function } # main -while getopts ":D:d:i:-: " o; do +OSM_CLUSTER_WORK_DIR="/etc/osm" +OSM_DEVOPS="/usr/share/osm-devops" +DEBUG_INSTALL="" + +while getopts ":D:d:-: " o; do case "${o}" in - i) - DEFAULT_IP="${OPTARG}" - ;; d) OSM_CLUSTER_WORK_DIR="${OPTARG}" ;; @@ -173,7 +180,7 @@ install_kube check_and_track_kube_install init_kubeadm $OSM_CLUSTER_WORK_DIR/kubeadm-config.yaml -kube_config_dir +save_kubeconfig check_and_track_init_k8s deploy_cni_provider -- 2.25.1