From efc1d847ef0006b800665461d77d635b6168e4b4 Mon Sep 17 00:00:00 2001 From: Lucian Petrut Date: Tue, 10 Dec 2024 12:00:29 +0000 Subject: [PATCH 1/2] Add worker bootstrap-node-taints setting The control-plane charm already allows defining node taints. We'll add the same for worker nodes. --- charms/worker/charmcraft.yaml | 13 +++++++++++++ charms/worker/k8s/src/config/extra_args.py | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/charms/worker/charmcraft.yaml b/charms/worker/charmcraft.yaml index 218a7a6c..b73516e0 100644 --- a/charms/worker/charmcraft.yaml +++ b/charms/worker/charmcraft.yaml @@ -60,6 +60,19 @@ bases: config: options: + bootstrap-node-taints: + type: string + default: "" + description: | + Space-separated list of taints to apply to this node at registration time. + + This config is only used at bootstrap time when Kubelet first registers the + node with Kubernetes. To change node taints after deploy time, use kubectl + instead. + + For more information, see the upstream Kubernetes documentation about + taints: + https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/ node-labels: default: "" type: string diff --git a/charms/worker/k8s/src/config/extra_args.py b/charms/worker/k8s/src/config/extra_args.py index b3be1d8f..f986edb0 100644 --- a/charms/worker/k8s/src/config/extra_args.py +++ b/charms/worker/k8s/src/config/extra_args.py @@ -70,3 +70,13 @@ def craft( cmd = _parse(src["kubelet-extra-args"]) dest.extra_node_kubelet_args = cmd + + is_worker_node = isinstance(dest, NodeJoinConfig) and not isinstance( + dest, ControlPlaneNodeJoinConfig + ) + if is_worker_node: + # We'll only do this for worker nodes, control plane nodes are handled + # separately. + taints = str(src["bootstrap-node-taints"] or "").strip().split() + if taints: + dest.extra_node_kubelet_args["--register-with-taints"] = ",".join(taints) From 13d33475c9af0c5c649f0dd84d6364ceea604dd1 Mon Sep 17 00:00:00 2001 From: Lucian Petrut Date: Wed, 11 Dec 2024 07:45:53 +0000 Subject: [PATCH 2/2] Add extra_args.taint_worker function We're adding a separate worker that applies a list of taints, cleaning up the code a little. --- charms/worker/k8s/src/charm.py | 3 +++ charms/worker/k8s/src/config/extra_args.py | 25 +++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/charms/worker/k8s/src/charm.py b/charms/worker/k8s/src/charm.py index fc438a8b..9f686f6d 100755 --- a/charms/worker/k8s/src/charm.py +++ b/charms/worker/k8s/src/charm.py @@ -815,6 +815,9 @@ def _join_with_token(self, relation: ops.Relation, token: str, cluster_name: str request.config = NodeJoinConfig() config.extra_args.craft(self.config, request.config, cluster_name) + bootstrap_node_taints = str(self.config["bootstrap-node-taints"] or "").strip().split() + config.extra_args.taint_worker(request.config, bootstrap_node_taints) + self.api_manager.join_cluster(request) log.info("Joined %s(%s)", self.unit, node_name) diff --git a/charms/worker/k8s/src/config/extra_args.py b/charms/worker/k8s/src/config/extra_args.py index f986edb0..0166a05e 100644 --- a/charms/worker/k8s/src/config/extra_args.py +++ b/charms/worker/k8s/src/config/extra_args.py @@ -4,7 +4,7 @@ # Learn more at: https://juju.is/docs/sdk """Parse extra arguments for Kubernetes components.""" -from typing import Dict, Union +from typing import Dict, List, Union import ops from charms.k8s.v0.k8sd_api_manager import ( @@ -71,12 +71,17 @@ def craft( cmd = _parse(src["kubelet-extra-args"]) dest.extra_node_kubelet_args = cmd - is_worker_node = isinstance(dest, NodeJoinConfig) and not isinstance( - dest, ControlPlaneNodeJoinConfig - ) - if is_worker_node: - # We'll only do this for worker nodes, control plane nodes are handled - # separately. - taints = str(src["bootstrap-node-taints"] or "").strip().split() - if taints: - dest.extra_node_kubelet_args["--register-with-taints"] = ",".join(taints) + +def taint_worker(dest: NodeJoinConfig, taints: List[str]): + """Apply the specified list of taints to the node join configuration. + + Updates the following attributes of the `config` object: + - extra_node_kubelet_args: arguments for kubelet. + + Args: + dest (NodeJoinConfig): + The configuration object to be updated with extra arguments. + taints (List[str]): + The list of taints to apply. + """ + dest.extra_node_kubelet_args["--register-with-taints"] = ",".join(taints)