Skip to content

Commit bdfd884

Browse files
authored
Allowing definition of k3s node name (#1517)
1 parent 5aadd98 commit bdfd884

File tree

6 files changed

+46
-6
lines changed

6 files changed

+46
-6
lines changed

platform/services/installer/app/commands/upgrade.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
from platform_stages.steps.install_system_packages import install_system_packages
5353
from platform_stages.upgrade import upgrade_platform
5454
from platform_utils.grafana import flush_ingesters as flush_lgtm_stack
55-
from platform_utils.k8s import check_if_telemetry_stack_is_installed
55+
from platform_utils.k8s import check_if_telemetry_stack_is_installed, get_node_name
5656
from platform_utils.management.data_folder import backup_data_folder, restore_data_folder
5757
from platform_utils.management.management import restore_platform, stop_platform
5858
from platform_utils.management.state import (
@@ -408,9 +408,16 @@ def perform_k3s_upgrade(config: UpgradeConfig) -> None:
408408
logger.info("Upgrading k3s.")
409409
click.echo(UpgradeCmdTexts.k3s_upgrade)
410410
logger.info(UpgradeCmdTexts.k3s_upgrade)
411+
411412
try:
413+
node_name = get_node_name()
414+
415+
if not node_name:
416+
logger.error("Node name could not be determined.")
417+
raise K3SInstallationError("Node name could not be determined.")
418+
412419
with click_spinner.spinner():
413-
install_k3s(setup_remote_kubeconfig=True)
420+
install_k3s(setup_remote_kubeconfig=True, node_name=node_name)
414421
except K3SInstallationError:
415422
logger.exception("Error during k3s upgrade.")
416423
click.echo(UpgradeCmdTexts.k3s_upgrade_failed)

platform/services/installer/app/configuration_models/install_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ def custom_certificate(self) -> bool:
4444
"""
4545
return bool(self.cert_file.value) or bool(self.key_file.value)
4646

47-
def __init__(self, interactive_mode: bool, install_telemetry_stack: bool) -> None:
47+
def __init__(self, interactive_mode: bool, install_telemetry_stack: bool, node_name: str | None = None) -> None:
4848
self._interactive_mode = interactive_mode
4949

5050
self.install_telemetry_stack = ConfigurationField(type=bool, required=False, value=install_telemetry_stack)
51+
self.node_name = ConfigurationField(type=str, required=False, value=node_name)
5152
self.offer_k8s_option = ConfigurationField(
5253
type=bool, required=True, value=os.getenv("PLATFORM_K8S_OPTION") == "true"
5354
)

platform/services/installer/app/k3s/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ def __init__( # noqa: PLR0913
2525
kubelet_args: dict[str, str] | None = None,
2626
service_node_port_range: str | None = None,
2727
node_ip: str | None = None,
28+
node_name: str | None = None,
2829
):
2930
self.version = version
3031
self.service_node_port_range = service_node_port_range
3132
self.node_ip = node_ip
33+
self.node_name = node_name
3234
if disable_components is None:
3335
self.disable_components = []
3436
else:
@@ -68,6 +70,11 @@ def to_env_var_dict(self) -> dict:
6870
if self.node_ip:
6971
install_k3s_exec_big_str += f"--node-ip={self.node_ip} "
7072

73+
if self.node_name:
74+
install_k3s_exec_big_str += f"--node-name={self.node_name} "
75+
else:
76+
install_k3s_exec_big_str += "--node-name=geti "
77+
7178
flags_mapping = {
7279
"--kube-apiserver-arg": self.kube_apiserver_args,
7380
"--kube-controller-manager-arg": self.kube_controller_manager_args,

platform/services/installer/app/k3s/install.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,16 @@ def _k3s_ready(time_wait: int = 300, delay: int = 10) -> bool:
117117
return False
118118

119119

120-
def _run_installer(k3s_script_path: str, logs_file_path: str):
120+
def _run_installer(k3s_script_path: str, logs_file_path: str, node_name: str | None = None):
121121
"""
122122
Run K3S installer by running downloaded script in 'k3s_script_path' and writing
123-
execution logs to 'logs_dir'.
123+
execution logs to 'logs_dir'. node_name contains optional name of a node of k3s cluster
124124
"""
125+
if node_name:
126+
k3s_configuration.node_name = node_name
125127
k3s_env_vars = k3s_configuration.to_env_var_dict()
128+
129+
logger.info(f"envs for k3s installation: {k3s_env_vars}")
126130
env = os.environ.copy()
127131
env.update(k3s_env_vars)
128132

@@ -221,6 +225,7 @@ def _install_k3s_selinux_rpm() -> None:
221225
def install_k3s( # noqa: ANN201
222226
logs_file_path: str = K3S_INSTALL_LOG_FILE_PATH,
223227
setup_remote_kubeconfig: bool = True,
228+
node_name: str | None = None,
224229
):
225230
"""
226231
Install K3S to current system. Write installation logs to 'logs_dir'. Use optionally 'external_address' to adjust
@@ -230,7 +235,7 @@ def install_k3s( # noqa: ANN201
230235
k3s_script_path = f"{K3S_OFFLINE_INSTALLATION_FILES_PATH}/install.sh"
231236
_prepare_k3s_files_structure()
232237
_install_k3s_selinux_rpm()
233-
_run_installer(k3s_script_path=k3s_script_path, logs_file_path=logs_file_path)
238+
_run_installer(k3s_script_path=k3s_script_path, logs_file_path=logs_file_path, node_name=node_name)
234239
_update_containerd_config(logs_file_path=logs_file_path)
235240
_mark_k3s_installation()
236241
except subprocess.CalledProcessError as ex:

platform/services/installer/app/platform_utils/k8s.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,22 @@ def _deployment_exists(name: str, namespace: str) -> bool:
280280
if ex.status == 404:
281281
return False
282282
raise
283+
284+
285+
def get_node_name() -> str | None:
286+
"""
287+
Get the name of the first node in the cluster.
288+
"""
289+
KubernetesConfigHandler(kube_config=K3S_KUBECONFIG_PATH)
290+
v1 = client.CoreV1Api()
291+
try:
292+
nodes = v1.list_node()
293+
294+
if not nodes.items:
295+
logger.error(f"Missing node {nodes}")
296+
return None
297+
except ApiException as ex:
298+
logger.error(f"Exception while getting the node name {ex}")
299+
return None
300+
301+
return nodes.items[0].metadata.name

platform/services/installer/tests/unit/k3s/test_install.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ def test_k3s_configuration():
166166
"INSTALL_K3S_SKIP_DOWNLOAD": "true",
167167
"INSTALL_K3S_EXEC": "--disable traefik "
168168
"--disable kube-server "
169+
"--node-name=geti "
169170
"--kube-apiserver-arg=enable-admission-plugins=NodeRestriction,"
170171
"PodSecurityPolicy,ServiceAccount "
171172
"--kube-controller-manager-arg=leader-elect-lease-duration=30s "

0 commit comments

Comments
 (0)