mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
kubernetes: add node labels support
Support management of node labels through salt states. Signed-off-by: Flavio Castelli <fcastelli@suse.com>
This commit is contained in:
parent
ea02960d0d
commit
f87fea0245
@ -157,6 +157,89 @@ def node(name, **kwargs):
|
||||
return None
|
||||
|
||||
|
||||
def node_labels(name, **kwargs):
|
||||
'''
|
||||
Return the labels of the node identified by the specified name
|
||||
|
||||
CLI Examples::
|
||||
|
||||
salt '*' kubernetes.node_labels name="minikube"
|
||||
'''
|
||||
match = node(name, **kwargs)
|
||||
|
||||
if match is not None:
|
||||
return match.metadata.labels
|
||||
|
||||
return {}
|
||||
|
||||
|
||||
def node_add_label(node_name, label_name, label_value, **kwargs):
|
||||
'''
|
||||
Set the value of the label identified by `label_name` to `label_value` on
|
||||
the node identified by the name `node_name`.
|
||||
Creates the lable if not present.
|
||||
|
||||
CLI Examples::
|
||||
|
||||
salt '*' kubernetes.node_add_label node_name="minikube" \
|
||||
label_name="foo" label_value="bar"
|
||||
'''
|
||||
_setup_conn(**kwargs)
|
||||
try:
|
||||
api_instance = kubernetes.client.CoreV1Api()
|
||||
body = {
|
||||
'metadata': {
|
||||
'labels': {
|
||||
label_name: label_value}
|
||||
}
|
||||
}
|
||||
api_response = api_instance.patch_node(node_name, body)
|
||||
return api_response
|
||||
except (ApiException, HTTPError) as exc:
|
||||
if isinstance(exc, ApiException) and exc.status == 404:
|
||||
return None
|
||||
else:
|
||||
log.exception(
|
||||
'Exception when calling CoreV1Api->patch_node: {0}'.format(exc)
|
||||
)
|
||||
raise CommandExecutionError(exc)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def node_remove_label(node_name, label_name, **kwargs):
|
||||
'''
|
||||
Removes the label identified by `label_name` from
|
||||
the node identified by the name `node_name`.
|
||||
|
||||
CLI Examples::
|
||||
|
||||
salt '*' kubernetes.node_remove_label node_name="minikube" \
|
||||
label_name="foo"
|
||||
'''
|
||||
_setup_conn(**kwargs)
|
||||
try:
|
||||
api_instance = kubernetes.client.CoreV1Api()
|
||||
body = {
|
||||
'metadata': {
|
||||
'labels': {
|
||||
label_name: None}
|
||||
}
|
||||
}
|
||||
api_response = api_instance.patch_node(node_name, body)
|
||||
return api_response
|
||||
except (ApiException, HTTPError) as exc:
|
||||
if isinstance(exc, ApiException) and exc.status == 404:
|
||||
return None
|
||||
else:
|
||||
log.exception(
|
||||
'Exception when calling CoreV1Api->patch_node: {0}'.format(exc)
|
||||
)
|
||||
raise CommandExecutionError(exc)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def namespaces(**kwargs):
|
||||
'''
|
||||
Return the names of the available namespaces
|
||||
|
@ -76,6 +76,7 @@ The kubernetes module is used to manage different kubernetes resources.
|
||||
'''
|
||||
from __future__ import absolute_import
|
||||
|
||||
import copy
|
||||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@ -831,3 +832,107 @@ def pod_present(
|
||||
}
|
||||
ret['result'] = True
|
||||
return ret
|
||||
|
||||
|
||||
def node_label_absent(name, node, **kwargs):
|
||||
'''
|
||||
Ensures that the named label is absent from the node.
|
||||
|
||||
name
|
||||
The name of the label
|
||||
|
||||
node
|
||||
The name of the node
|
||||
'''
|
||||
|
||||
ret = {'name': name,
|
||||
'changes': {},
|
||||
'result': False,
|
||||
'comment': ''}
|
||||
|
||||
labels = __salt__['kubernetes.node_labels'](node, **kwargs)
|
||||
|
||||
if name not in labels:
|
||||
ret['result'] = True if not __opts__['test'] else None
|
||||
ret['comment'] = 'The label does not exist'
|
||||
return ret
|
||||
|
||||
if __opts__['test']:
|
||||
ret['comment'] = 'The label is going to be deleted'
|
||||
ret['result'] = None
|
||||
return ret
|
||||
|
||||
__salt__['kubernetes.node_remove_label'](
|
||||
node_name=node,
|
||||
label_name=name,
|
||||
**kwargs)
|
||||
|
||||
ret['result'] = True
|
||||
ret['changes'] = {
|
||||
'kubernetes.node_label': {
|
||||
'new': 'absent', 'old': 'present'}}
|
||||
ret['comment'] = 'Label removed from node'
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def node_label_present(
|
||||
name,
|
||||
node,
|
||||
value,
|
||||
**kwargs):
|
||||
'''
|
||||
Ensures that the named label is set on the named node
|
||||
with the given value.
|
||||
If the label exists it will be replaced.
|
||||
|
||||
name
|
||||
The name of the label.
|
||||
|
||||
value
|
||||
Value of the label.
|
||||
|
||||
node
|
||||
Node to change.
|
||||
'''
|
||||
ret = {'name': name,
|
||||
'changes': {},
|
||||
'result': False,
|
||||
'comment': ''}
|
||||
|
||||
labels = __salt__['kubernetes.node_labels'](node, **kwargs)
|
||||
|
||||
if name not in labels:
|
||||
if __opts__['test']:
|
||||
ret['result'] = None
|
||||
ret['comment'] = 'The label is going to be set'
|
||||
return ret
|
||||
__salt__['kubernetes.node_add_label'](label_name=name,
|
||||
label_value=value,
|
||||
node_name=node,
|
||||
**kwargs)
|
||||
elif labels[name] == value:
|
||||
ret['result'] = True
|
||||
ret['comment'] = 'The label is already set and has the specified value'
|
||||
return ret
|
||||
else:
|
||||
if __opts__['test']:
|
||||
ret['result'] = None
|
||||
return ret
|
||||
|
||||
ret['comment'] = 'The label is already set, changing the value'
|
||||
__salt__['kubernetes.node_add_label'](
|
||||
node_name=node,
|
||||
label_name=name,
|
||||
label_value=value,
|
||||
**kwargs)
|
||||
|
||||
old_labels = copy.copy(labels)
|
||||
labels[name] = value
|
||||
|
||||
ret['changes']['{0}.{1}'.format(node, name)] = {
|
||||
'old': old_labels,
|
||||
'new': labels}
|
||||
ret['result'] = True
|
||||
|
||||
return ret
|
||||
|
Loading…
Reference in New Issue
Block a user