Configure docker private registry for K3s-based installation 26/14526/1
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 21 Aug 2024 19:19:36 +0000 (21:19 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 21 Aug 2024 19:19:50 +0000 (21:19 +0200)
Change-Id: Iaf3a678f337b0241160b57c4876ffc671a4a3cbf
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
installers/full_install_osm.sh
installers/install_k3s_cluster.sh

index 6cde383..ee51e2f 100755 (executable)
@@ -430,6 +430,9 @@ function install_k8s_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}"
+        [ -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}"
+        [ -n "${DOCKER_REGISTRY_PASSWORD}" ] && K3S_INSTALL_OPTS="${K3S_INSTALL_OPTS} -P ${DOCKER_REGISTRY_PASSWORD}"
         # 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,
@@ -500,6 +503,7 @@ function install_osm() {
             echo "Kubeadm requires docker, so docker will be installed."
         fi
         install_docker_ce
+        [ -n "${DOCKER_REGISTRY_URL}" ] && docker_login
     fi
     track docker_ce docker_ce_ok
 
index fea91f4..86bc135 100755 (executable)
@@ -27,6 +27,16 @@ mirrors:
   docker.io:
     endpoint:
       - "${DOCKER_PROXY_URL}"
+EOF
+    fi
+    if [ -n "${DOCKER_REGISTRY_URL}" ]; then
+        echo "Configuring docker private registry in /etc/rancher/k3s/registries.yaml"
+        cat << EOF | sudo tee -a /etc/rancher/k3s/registries.yaml > /dev/null
+configs:
+  ${DOCKER_REGISTRY_URL}:
+    auth:
+      username: ${DOCKER_REGISTRY_USER}
+      password: ${DOCKER_REGISTRY_PASSWORD}
 EOF
     fi
 }
@@ -151,7 +161,7 @@ function save_kubeconfig() {
 }
 
 # main
-while getopts ":D:i:p:-: " o; do
+while getopts ":D:i:p:d:u:P:-: " o; do
     case "${o}" in
         i)
             DEFAULT_IP="${OPTARG}"
@@ -162,6 +172,15 @@ while getopts ":D:i:p:-: " o; do
         p)
             DOCKER_PROXY_URL="${OPTARG}"
             ;;
+        d)
+            DOCKER_REGISTRY_URL="${OPTARG}"
+            ;;
+        u)
+            DOCKER_REGISTRY_USER="${OPTARG}"
+            ;;
+        P)
+            DOCKER_REGISTRY_PASSWORD="${OPTARG}"
+            ;;
         -)
             [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
             echo -e "Invalid option: '--$OPTARG'\n" >&2
@@ -185,10 +204,15 @@ 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=-}
+DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL=-}
+DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER=-}
+DOCKER_REGISTRY_PASSWORD=${DOCKER_REGISTRY_PASSWORD=-}
 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 "HOME=$HOME"
 
 source $OSM_DEVOPS/common/logging