Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

salt: Add some default topology labels on nodes #3897

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

- Add default `topology.kubernetes.io/region` and `topology.kubernetes.io/zone`
topology labels on nodes
(PR[#3889](https://github.com/scality/metalk8s/pull/3889))
(PR[#3897](https://github.com/scality/metalk8s/pull/3897))

- Add support for a `metalk8s.scality.com/force-lvcreate` annotation on `Volume`
objects of type `LVMLogicalVolume` to force the creation of their LV (use with
Expand Down
2 changes: 0 additions & 2 deletions docs/installation/expansion.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ following the template below:
metalk8s.scality.com/ssh-user: 'root'
labels:
metalk8s.scality.com/version: '|version|'
topology.kubernetes.io/region: 'default'
topology.kubernetes.io/zone: 'default'
<role labels>
spec:
taints: <taints>
Expand Down
2 changes: 0 additions & 2 deletions examples/new-node.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ metadata:
metalk8s.scality.com/version: '2.0'
node-role.kubernetes.io/master: ''
node-role.kubernetes.io/etcd: ''
topology.kubernetes.io/region: 'default'
topology.kubernetes.io/zone: 'default'
spec:
taints:
- effect: NoSchedule
Expand Down
2 changes: 0 additions & 2 deletions examples/new-node_vagrant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ metadata:
metalk8s.scality.com/version: '2.0'
node-role.kubernetes.io/master: ''
node-role.kubernetes.io/etcd: ''
topology.kubernetes.io/region: 'default'
topology.kubernetes.io/zone: 'default'
spec:
taints:
- effect: NoSchedule
Expand Down
50 changes: 50 additions & 0 deletions salt/_states/metalk8s_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,53 @@ def object_updated(name, manifest=None, **kwargs):
ret["comment"] = "The object was updated"

return ret


def labels_exist(name, labels, **kwargs):
"""Ensure that labels exist.

NOTE: This function do not update the labels if they
already exist

Arguments:
name (str): Name of the object
labels (dict): Labels that should exist
"""
ret = {"name": name, "changes": {}, "result": True, "comment": ""}

obj = __salt__["metalk8s_kubernetes.get_object"](
name=name, saltenv=__env__, **kwargs
)

if not obj:
ret["result"] = False
ret["comment"] = f"The object {name} does not exist"
return ret

labels_to_add = {
key: value
for key, value in labels.items()
if key not in obj["metadata"]["labels"]
}

if not labels_to_add:
ret["comment"] = f"All labels already exist on object {name}"
return ret

if __opts__["test"]:
ret["result"] = None
ret[
"comment"
] = f"Labels {', '.join(labels_to_add)} will be added to object {name}"
return ret

__salt__["metalk8s_kubernetes.update_object"](
name=name,
saltenv=__env__,
patch={"metadata": {"labels": labels_to_add}},
**kwargs,
)

ret["comment"] = f"Object {name} labels has been added"
ret["changes"] = {"labels added": labels_to_add}
return ret
5 changes: 5 additions & 0 deletions salt/metalk8s/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ metalk8s:
kubernetes:
cluster: kubernetes

nodes:
default_labels:
topology.kubernetes.io/region: default
topology.kubernetes.io/zone: default

kubeadm_preflight:
mandatory:
packages:
Expand Down
10 changes: 10 additions & 0 deletions salt/metalk8s/kubernetes/mark-control-plane/deployed.sls
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{%- from "metalk8s/map.jinja" import kubelet with context %}
{%- from "metalk8s/map.jinja" import kubernetes with context %}

{%- set node_name = pillar.bootstrap_id %}
{%- set cri_socket = kubelet.service.options['container-runtime-endpoint'] %}
Expand All @@ -20,3 +21,12 @@ Mark node {{ node_name }} as bootstrap:
cri_socket: {{ cri_socket }}
- require:
- test: Ensure node {{ node_name }} exists

Ensure node {{ node_name }} default labels exist:
metalk8s_kubernetes.labels_exist:
- name: {{ node_name }}
- apiVersion: v1
- kind: Node
- labels: {{ kubernetes.nodes.default_labels | tojson }}
- require:
- test: Ensure node {{ node_name }} exists
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
{%- endfor %}
{%- endif %}

{%- set topology_labels = ["topology.kubernetes.io/region", "topology.kubernetes.io/zone"] %}

kind: Node
apiVersion: v1
metadata:
Expand All @@ -43,11 +41,6 @@ metadata:
node-role.kubernetes.io/etcd: ""
node-role.kubernetes.io/bootstrap: ""
node-role.kubernetes.io/infra: ""
{%- for label in topology_labels %}
{%- if label not in node_obj["metadata"].get("labels", {}) %}
{{ label }}: default
{%- endif %}
{%- endfor %}
annotations:
kubeadm.alpha.kubernetes.io/cri-socket: {{ cri_socket }}
{%- if update_taints %}
Expand Down
8 changes: 8 additions & 0 deletions salt/metalk8s/orchestrate/deploy_node.sls
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{%- from "metalk8s/map.jinja" import defaults with context %}
{%- from "metalk8s/map.jinja" import kubernetes with context %}
{%- from "metalk8s/map.jinja" import networks with context %}
{%- from "metalk8s/map.jinja" import repo with context %}

Expand Down Expand Up @@ -120,6 +121,13 @@ Refresh the mine:
- name: mine.update
- tgt: '*'

Ensure node default labels exist:
metalk8s_kubernetes.labels_exist:
- name: {{ node_name }}
- apiVersion: v1
- kind: Node
- labels: {{ kubernetes.nodes.default_labels | tojson }}

Cordon the node:
metalk8s_cordon.node_cordoned:
- name: {{ node_name }}
Expand Down
2 changes: 0 additions & 2 deletions tests/install/steps/files/control-plane-node.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ metadata:
node-role.kubernetes.io/master: ''
node-role.kubernetes.io/etcd: ''
node-role.kubernetes.io/infra: ''
topology.kubernetes.io/region: 'default'
topology.kubernetes.io/zone: 'default'
annotations:
metalk8s.scality.com/ssh-user: centos
metalk8s.scality.com/ssh-port: "22"
Expand Down
2 changes: 0 additions & 2 deletions ui/src/ducks/app/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,6 @@ export function* createNode({ payload }) {
name: payload.name,
labels: {
'metalk8s.scality.com/version': clusterVersion,
'topology.kubernetes.io/region': 'default',
'topology.kubernetes.io/zone': 'default',
},
annotations: {
'metalk8s.scality.com/ssh-user': payload.ssh_user,
Expand Down