summaryrefslogtreecommitdiffstats
path: root/filter_plugins/oo_filters.py
diff options
context:
space:
mode:
Diffstat (limited to 'filter_plugins/oo_filters.py')
-rw-r--r--filter_plugins/oo_filters.py202
1 files changed, 157 insertions, 45 deletions
diff --git a/filter_plugins/oo_filters.py b/filter_plugins/oo_filters.py
index cd67b69a5..3da4562ac 100644
--- a/filter_plugins/oo_filters.py
+++ b/filter_plugins/oo_filters.py
@@ -57,6 +57,19 @@ class FilterModule(object):
return [item for sublist in data for item in sublist]
@staticmethod
+ def oo_merge_dicts(first_dict, second_dict):
+ """ Merge two dictionaries where second_dict values take precedence.
+ Ex: first_dict={'a': 1, 'b': 2}
+ second_dict={'b': 3, 'c': 4}
+ returns {'a': 1, 'b': 3, 'c': 4}
+ """
+ if not isinstance(first_dict, dict) or not isinstance(second_dict, dict):
+ raise errors.AnsibleFilterError("|failed expects to merge two dicts")
+ merged = first_dict.copy()
+ merged.update(second_dict)
+ return merged
+
+ @staticmethod
def oo_collect(data, attribute=None, filters=None):
""" This takes a list of dict and collects all attributes specified into a
list. If filter is specified then we will include all items that
@@ -246,8 +259,11 @@ class FilterModule(object):
@staticmethod
def oo_split(string, separator=','):
- """ This splits the input string into a list
+ """ This splits the input string into a list. If the input string is
+ already a list we will return it as is.
"""
+ if isinstance(string, list):
+ return string
return string.split(separator)
@staticmethod
@@ -286,6 +302,85 @@ class FilterModule(object):
return [x for x in data if x.has_key(filter_attr) and x[filter_attr]]
@staticmethod
+ def oo_oc_nodes_matching_selector(nodes, selector):
+ """ Filters a list of nodes by selector.
+
+ Examples:
+ nodes = [{"kind": "Node", "metadata": {"name": "node1.example.com",
+ "labels": {"kubernetes.io/hostname": "node1.example.com",
+ "color": "green"}}},
+ {"kind": "Node", "metadata": {"name": "node2.example.com",
+ "labels": {"kubernetes.io/hostname": "node2.example.com",
+ "color": "red"}}}]
+ selector = 'color=green'
+ returns = ['node1.example.com']
+
+ nodes = [{"kind": "Node", "metadata": {"name": "node1.example.com",
+ "labels": {"kubernetes.io/hostname": "node1.example.com",
+ "color": "green"}}},
+ {"kind": "Node", "metadata": {"name": "node2.example.com",
+ "labels": {"kubernetes.io/hostname": "node2.example.com",
+ "color": "red"}}}]
+ selector = 'color=green,color=red'
+ returns = ['node1.example.com','node2.example.com']
+
+ Args:
+ nodes (list[dict]): list of node definitions
+ selector (str): "label=value" node selector to filter `nodes` by
+ Returns:
+ list[str]: nodes filtered by selector
+ """
+ if not isinstance(nodes, list):
+ raise errors.AnsibleFilterError("failed expects nodes to be a list, got {0}".format(type(nodes)))
+ if not isinstance(selector, basestring):
+ raise errors.AnsibleFilterError("failed expects selector to be a string")
+ if not re.match('.*=.*', selector):
+ raise errors.AnsibleFilterError("failed selector does not match \"label=value\" format")
+ node_lists = []
+ for node_selector in ''.join(selector.split()).split(','):
+ label = node_selector.split('=')[0]
+ value = node_selector.split('=')[1]
+ node_lists.append(FilterModule.oo_oc_nodes_with_label(nodes, label, value))
+ nodes = set(node_lists[0])
+ for node_list in node_lists[1:]:
+ nodes.intersection_update(node_list)
+ return list(nodes)
+
+ @staticmethod
+ def oo_oc_nodes_with_label(nodes, label, value):
+ """ Filters a list of nodes by label, value.
+
+ Examples:
+ nodes = [{"kind": "Node", "metadata": {"name": "node1.example.com",
+ "labels": {"kubernetes.io/hostname": "node1.example.com",
+ "color": "green"}}},
+ {"kind": "Node", "metadata": {"name": "node2.example.com",
+ "labels": {"kubernetes.io/hostname": "node2.example.com",
+ "color": "red"}}}]
+ label = 'color'
+ value = 'green'
+ returns = ['node1.example.com']
+ Args:
+ nodes (list[dict]): list of node definitions
+ label (str): label to filter `nodes` by
+ value (str): value of `label` to filter `nodes` by
+ Returns:
+ list[str]: nodes filtered by selector
+ """
+ if not isinstance(nodes, list):
+ raise errors.AnsibleFilterError("failed expects nodes to be a list")
+ if not isinstance(label, basestring):
+ raise errors.AnsibleFilterError("failed expects label to be a string")
+ if not isinstance(value, basestring):
+ raise errors.AnsibleFilterError("failed expects value to be a string")
+ matching_nodes = []
+ for node in nodes:
+ if label in node['metadata']['labels']:
+ if node['metadata']['labels'][label] == value:
+ matching_nodes.append(node['metadata']['name'])
+ return matching_nodes
+
+ @staticmethod
def oo_nodes_with_label(nodes, label, value=None):
""" Filters a list of nodes by label and value (if provided)
@@ -558,7 +653,9 @@ class FilterModule(object):
@staticmethod
def oo_openshift_env(hostvars):
- ''' Return facts which begin with "openshift_"
+ ''' Return facts which begin with "openshift_" and translate
+ legacy facts to their openshift_env counterparts.
+
Ex: hostvars = {'openshift_fact': 42,
'theyre_taking_the_hobbits_to': 'isengard'}
returns = {'openshift_fact': 42}
@@ -571,6 +668,11 @@ class FilterModule(object):
for key in hostvars:
if regex.match(key):
facts[key] = hostvars[key]
+
+ migrations = {'openshift_router_selector': 'openshift_hosted_router_selector'}
+ for old_fact, new_fact in migrations.iteritems():
+ if old_fact in facts and new_fact not in facts:
+ facts[new_fact] = facts[old_fact]
return facts
@staticmethod
@@ -588,36 +690,38 @@ class FilterModule(object):
if persistent_volumes == None:
persistent_volumes = []
- for component in hostvars['openshift']['hosted']:
- kind = hostvars['openshift']['hosted'][component]['storage']['kind']
- create_pv = hostvars['openshift']['hosted'][component]['storage']['create_pv']
- if kind != None and create_pv:
- if kind == 'nfs':
- host = hostvars['openshift']['hosted'][component]['storage']['host']
- if host == None:
- if len(groups['oo_nfs_to_config']) > 0:
- host = groups['oo_nfs_to_config'][0]
+ if 'hosted' in hostvars['openshift']:
+ for component in hostvars['openshift']['hosted']:
+ if 'storage' in hostvars['openshift']['hosted'][component]:
+ kind = hostvars['openshift']['hosted'][component]['storage']['kind']
+ create_pv = hostvars['openshift']['hosted'][component]['storage']['create_pv']
+ if kind != None and create_pv:
+ if kind == 'nfs':
+ host = hostvars['openshift']['hosted'][component]['storage']['host']
+ if host == None:
+ if len(groups['oo_nfs_to_config']) > 0:
+ host = groups['oo_nfs_to_config'][0]
+ else:
+ raise errors.AnsibleFilterError("|failed no storage host detected")
+ directory = hostvars['openshift']['hosted'][component]['storage']['nfs']['directory']
+ volume = hostvars['openshift']['hosted'][component]['storage']['volume']['name']
+ path = directory + '/' + volume
+ size = hostvars['openshift']['hosted'][component]['storage']['volume']['size']
+ access_modes = hostvars['openshift']['hosted'][component]['storage']['access_modes']
+ persistent_volume = dict(
+ name="{0}-volume".format(volume),
+ capacity=size,
+ access_modes=access_modes,
+ storage=dict(
+ nfs=dict(
+ server=host,
+ path=path)))
+ persistent_volumes.append(persistent_volume)
else:
- raise errors.AnsibleFilterError("|failed no storage host detected")
- directory = hostvars['openshift']['hosted'][component]['storage']['nfs']['directory']
- volume = hostvars['openshift']['hosted'][component]['storage']['volume']['name']
- path = directory + '/' + volume
- size = hostvars['openshift']['hosted'][component]['storage']['volume']['size']
- access_modes = hostvars['openshift']['hosted'][component]['storage']['access_modes']
- persistent_volume = dict(
- name="{0}-volume".format(volume),
- capacity=size,
- access_modes=access_modes,
- storage=dict(
- nfs=dict(
- server=host,
- path=path)))
- persistent_volumes.append(persistent_volume)
- else:
- msg = "|failed invalid storage kind '{0}' for component '{1}'".format(
- kind,
- component)
- raise errors.AnsibleFilterError(msg)
+ msg = "|failed invalid storage kind '{0}' for component '{1}'".format(
+ kind,
+ component)
+ raise errors.AnsibleFilterError(msg)
return persistent_volumes
@staticmethod
@@ -632,18 +736,20 @@ class FilterModule(object):
if persistent_volume_claims == None:
persistent_volume_claims = []
- for component in hostvars['openshift']['hosted']:
- kind = hostvars['openshift']['hosted'][component]['storage']['kind']
- create_pv = hostvars['openshift']['hosted'][component]['storage']['create_pv']
- if kind != None and create_pv:
- volume = hostvars['openshift']['hosted'][component]['storage']['volume']['name']
- size = hostvars['openshift']['hosted'][component]['storage']['volume']['size']
- access_modes = hostvars['openshift']['hosted'][component]['storage']['access_modes']
- persistent_volume_claim = dict(
- name="{0}-claim".format(volume),
- capacity=size,
- access_modes=access_modes)
- persistent_volume_claims.append(persistent_volume_claim)
+ if 'hosted' in hostvars['openshift']:
+ for component in hostvars['openshift']['hosted']:
+ if 'storage' in hostvars['openshift']['hosted'][component]:
+ kind = hostvars['openshift']['hosted'][component]['storage']['kind']
+ create_pv = hostvars['openshift']['hosted'][component]['storage']['create_pv']
+ if kind != None and create_pv:
+ volume = hostvars['openshift']['hosted'][component]['storage']['volume']['name']
+ size = hostvars['openshift']['hosted'][component]['storage']['volume']['size']
+ access_modes = hostvars['openshift']['hosted'][component]['storage']['access_modes']
+ persistent_volume_claim = dict(
+ name="{0}-claim".format(volume),
+ capacity=size,
+ access_modes=access_modes)
+ persistent_volume_claims.append(persistent_volume_claim)
return persistent_volume_claims
@staticmethod
@@ -711,7 +817,7 @@ class FilterModule(object):
return retval
@staticmethod
- def oo_image_tag_to_rpm_version(version):
+ def oo_image_tag_to_rpm_version(version, include_dash=False):
""" Convert an image tag string to an RPM version if necessary
Empty strings and strings that are already in rpm version format
are ignored.
@@ -722,7 +828,10 @@ class FilterModule(object):
raise errors.AnsibleFilterError("|failed expects a string or unicode")
if version.startswith("v"):
- version = "-" + version.replace("v", "")
+ version = version.replace("v", "")
+
+ if include_dash:
+ version = "-" + version
return version
@@ -755,4 +864,7 @@ class FilterModule(object):
"oo_pods_match_component": self.oo_pods_match_component,
"oo_get_hosts_from_hostvars": self.oo_get_hosts_from_hostvars,
"oo_image_tag_to_rpm_version": self.oo_image_tag_to_rpm_version,
+ "oo_merge_dicts": self.oo_merge_dicts,
+ "oo_oc_nodes_matching_selector": self.oo_oc_nodes_matching_selector,
+ "oo_oc_nodes_with_label": self.oo_oc_nodes_with_label
}