Configure docker proxy URL for K3s-based installation to overcome docker pull rate... 25/14525/1
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 21 Aug 2024 16:18:14 +0000 (18:18 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 21 Aug 2024 16:18:23 +0000 (18:18 +0200)
Change-Id: I94dde54e4f124369daaf40ff6863b77e2fd25d23
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
installers/full_install_osm.sh
installers/install_k3s_cluster.sh

index 1b18334..6cde383 100755 (executable)
@@ -429,6 +429,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}"
+        [ -n "${DOCKER_PROXY_URL}" ] && K3S_INSTALL_OPTS="${K3S_INSTALL_OPTS} -p ${DOCKER_PROXY_URL}"
         # 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,
index a604ef1..fea91f4 100755 (executable)
@@ -18,6 +18,19 @@ set +eux
 # K3s releases: https://github.com/k3s-io/k3s/releases/
 K8S_VERSION="v1.29.3+k3s1"
 
+# configure registry
+function configure_registry() {
+    if [ -n "${DOCKER_PROXY_URL}" ]; then
+        echo "Configuring docker proxy URL in /etc/rancher/k3s/registries.yaml"
+        cat << EOF | sudo tee /etc/rancher/k3s/registries.yaml > /dev/null
+mirrors:
+  docker.io:
+    endpoint:
+      - "${DOCKER_PROXY_URL}"
+EOF
+    fi
+}
+
 # installs k3s
 function install_k3s() {
     [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
@@ -138,7 +151,7 @@ function save_kubeconfig() {
 }
 
 # main
-while getopts ":D:i:-: " o; do
+while getopts ":D:i:p:-: " o; do
     case "${o}" in
         i)
             DEFAULT_IP="${OPTARG}"
@@ -146,6 +159,9 @@ while getopts ":D:i:-: " o; do
         D)
             OSM_DEVOPS="${OPTARG}"
             ;;
+        p)
+            DOCKER_PROXY_URL="${OPTARG}"
+            ;;
         -)
             [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
             echo -e "Invalid option: '--$OPTARG'\n" >&2
@@ -168,14 +184,17 @@ done
 DEBUG_INSTALL=${DEBUG_INSTALL:-}
 DEFAULT_IP=${DEFAULT_IP:-"127.0.0.1"}
 OSM_DEVOPS=${OSM_DEVOPS:-"/usr/share/osm-devops"}
+DOCKER_PROXY_URL=${DOCKER_PROXY_URL=-}
 echo "DEBUG_INSTALL=${DEBUG_INSTALL}"
 echo "DEFAULT_IP=${DEFAULT_IP}"
 echo "OSM_DEVOPS=${OSM_DEVOPS}"
+echo "DOCKER_PROXY_URL=${DOCKER_PROXY_URL}"
 echo "HOME=$HOME"
 
 source $OSM_DEVOPS/common/logging
 source $OSM_DEVOPS/common/track
 
+configure_registry
 install_k3s
 track k8scluster k3s_install_ok
 check_for_readiness