diff --git a/platform/services/installer/app/commands/install.py b/platform/services/installer/app/commands/install.py index 271b9a693a..b9509ff6f4 100755 --- a/platform/services/installer/app/commands/install.py +++ b/platform/services/installer/app/commands/install.py @@ -210,7 +210,7 @@ def monitor_installation_progress(config: InstallationConfig) -> tuple[str, str] return status, message -def execute_installation(config: InstallationConfig) -> None: # noqa: C901, RUF100 +def execute_installation(config: InstallationConfig) -> None: # noqa: C901, RUF100, PLR0915 """ Execute platform installation with passed configuration. """ diff --git a/platform/services/installer/app/constants/paths.py b/platform/services/installer/app/constants/paths.py index 3967817e5e..bf2afe6377 100644 --- a/platform/services/installer/app/constants/paths.py +++ b/platform/services/installer/app/constants/paths.py @@ -43,6 +43,7 @@ K3S_BACKUP_RESTORE_PATH = f"{K3S_VAR_LIB_RANCHER}/backup" K3S_KILLALL_SCRIPT_PATH = "/usr/local/bin/k3s-killall.sh" K3S_OFFLINE_INSTALLATION_FILES_PATH = f"{OFFLINE_TOOLS_DIR}/k3s" +K3S_REGISTRIES_FILE_PATH = "/etc/rancher/k3s/registries.yaml" ### # logs related diff --git a/platform/services/installer/app/k3s/config.py b/platform/services/installer/app/k3s/config.py index 02af87fdef..0ea9c70552 100644 --- a/platform/services/installer/app/k3s/config.py +++ b/platform/services/installer/app/k3s/config.py @@ -98,7 +98,6 @@ def to_env_var_dict(self) -> dict: install_k3s_exec_big_str += f"{flag}={arg_name}={arg_value} " return { - "INSTALL_K3S_SKIP_DOWNLOAD": "true", "INSTALL_K3S_VERSION": self.version, "INSTALL_K3S_EXEC": install_k3s_exec_big_str, } diff --git a/platform/services/installer/app/k3s/install.py b/platform/services/installer/app/k3s/install.py index fd13ff98ed..5288e2cb73 100644 --- a/platform/services/installer/app/k3s/install.py +++ b/platform/services/installer/app/k3s/install.py @@ -21,8 +21,10 @@ import shutil import stat import subprocess +import tempfile import time from typing import IO +from urllib.parse import urlparse import requests import yaml @@ -36,11 +38,12 @@ K3S_INSTALLATION_MARK_FILEPATH, K3S_KUBECONFIG_PATH, K3S_OFFLINE_INSTALLATION_FILES_PATH, + K3S_REGISTRIES_FILE_PATH, K3S_REMOTE_KUBECONFIG_PATH, K3S_SELINUX_OFFLINE_INSTALLATION_FILES_PATH, USR_LOCAL_BIN_PATH, ) -from constants.platform import PLATFORM_NAMESPACE +from constants.platform import EXTERNAL_REGISTRY_ADDRESS, PLATFORM_NAMESPACE from k3s.config import k3s_configuration from k3s.detect_ip import get_first_public_ip from k3s.detect_selinux import is_selinux_installed @@ -69,6 +72,41 @@ def _download_script(target_file: IO[bytes]): os.chmod(target_file.name, target_file_stat.st_mode | stat.S_IEXEC) +def _set_local_registry(external_registry_address: str): + """ + Configures local docker registry - as a replacement for docker.io registry to avoid docker pull limit + Function called only when the EXTERNAL_REGISTRY_ADDRESS env variable is set. + :param external_registry_address: Address of the external registry to be used as a replacement for docker.io + """ + if not external_registry_address.startswith("http"): + external_registry_address = "https://" + external_registry_address + parsed_url = urlparse(external_registry_address) + + content = f"""mirrors: + docker.io: + endpoint: + - "{parsed_url.scheme}://{parsed_url.netloc}" """ + + if parsed_url.path.strip('/'): + content += f""" + rewrite: + "(.*)": "{parsed_url.path.strip('/')}/$1" """ + + content += f""" +configs: + "{parsed_url.scheme}://{parsed_url.netloc}": + tls: + insecure_skip_verify: true """ + + os.makedirs(os.path.dirname(K3S_REGISTRIES_FILE_PATH), exist_ok=True) + + try: + with open(K3S_REGISTRIES_FILE_PATH, "w", encoding="utf-8") as file: + file.write(content) + except (OSError) as error: + logger.exception(f"Error occurred while setting up local registry: {str(error)}") + + def _update_containerd_config(logs_file_path: str): """ Update containerd config.toml.tmpl to point to 'current' location. @@ -233,16 +271,21 @@ def install_k3s( # noqa: ANN201 Install K3S to current system. Write installation logs to 'logs_dir'. Use optionally 'external_address' to adjust produced kubeconfig. """ - try: - k3s_script_path = f"{K3S_OFFLINE_INSTALLATION_FILES_PATH}/install.sh" - _prepare_k3s_files_structure() - _install_k3s_selinux_rpm() - _run_installer(k3s_script_path=k3s_script_path, logs_file_path=logs_file_path) - _update_containerd_config(logs_file_path=logs_file_path) - _mark_k3s_installation() - except subprocess.CalledProcessError as ex: - raise K3SInstallationError from ex - - if setup_remote_kubeconfig: - _adjust_k3s_kubeconfig_server_address() - _set_default_namespace() + with tempfile.NamedTemporaryFile(delete=False) as tmp: + try: + if EXTERNAL_REGISTRY_ADDRESS: + _set_local_registry(EXTERNAL_REGISTRY_ADDRESS) + _download_script(tmp) + tmp.close() + _install_k3s_selinux_rpm() + _run_installer(k3s_script_path=tmp.name, logs_file_path=logs_file_path) + _update_containerd_config(logs_file_path=logs_file_path) + _mark_k3s_installation() + except subprocess.CalledProcessError as ex: + raise K3SInstallationError from ex + finally: + os.remove(tmp.name) + + if setup_remote_kubeconfig: + _adjust_k3s_kubeconfig_server_address() + _set_default_namespace() diff --git a/platform/services/installer/app/system-packages.yaml b/platform/services/installer/app/system-packages.yaml index 9e5d7e54a7..47fba77991 100644 --- a/platform/services/installer/app/system-packages.yaml +++ b/platform/services/installer/app/system-packages.yaml @@ -7,8 +7,6 @@ shared: - name: k3s destination: tools/k3s urls: - - https://github.com/k3s-io/k3s/releases/download/v1.33.1+k3s1/k3s - - https://github.com/k3s-io/k3s/releases/download/v1.33.1+k3s1/k3s-airgap-images-amd64.tar.gz - https://raw.githubusercontent.com/k3s-io/k3s/refs/tags/v1.33.1+k3s1/install.sh ubuntu: packages: