Repository: openshift/openshift-ansible

Issue:
RFE: put nodeSelector label in default namespace
When using labels for region and zones (as shown in https://github.com/openshift/openshift-ansible/blob/master/inventory/byo/hosts.origin.example#L156 ) the default namespace will never run a registry or router. 

Fixing it using https://lists.openshift.redhat.com/openshift-archives/users/2016-February/msg00036.html is easy and could be part of a playbook.



Discussion:

Resolving patch:
diff --git a/filter_plugins/oo_filters.py b/filter_plugins/oo_filters.py
--- a/filter_plugins/oo_filters.py
+++ b/filter_plugins/oo_filters.py
@@ -339,85 +339,6 @@ def oo_filter_list(data, filter_attr=None):
         # Gather up the values for the list of keys passed in
         return [x for x in data if filter_attr in x 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)
@@ -707,7 +628,8 @@ def oo_openshift_env(hostvars):
             if regex.match(key):
                 facts[key] = hostvars[key]
 
-        migrations = {'openshift_router_selector': 'openshift_hosted_router_selector'}
+        migrations = {'openshift_router_selector': 'openshift_hosted_router_selector',
+                      'openshift_registry_selector': 'openshift_hosted_registry_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]
@@ -771,7 +693,7 @@ def oo_persistent_volumes(hostvars, groups, persistent_volumes=None):
                                         fsType=filesystem,
                                         volumeID=volume_id)))
                             persistent_volumes.append(persistent_volume)
-                        else:
+                        elif kind != 'object':
                             msg = "|failed invalid storage kind '{0}' for component '{1}'".format(
                                 kind,
                                 component)
@@ -922,7 +844,5 @@ def filters(self):
             "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,
             "oo_merge_hostvars": self.oo_merge_hostvars,
         }
diff --git a/roles/openshift_facts/library/openshift_facts.py b/roles/openshift_facts/library/openshift_facts.py
--- a/roles/openshift_facts/library/openshift_facts.py
+++ b/roles/openshift_facts/library/openshift_facts.py
@@ -114,6 +114,12 @@ def migrate_hosted_facts(facts):
             if 'router' not in facts['hosted']:
                 facts['hosted']['router'] = {}
             facts['hosted']['router']['selector'] = facts['master'].pop('router_selector')
+        if 'registry_selector' in facts['master']:
+            if 'hosted' not in facts:
+                facts['hosted'] = {}
+            if 'registry' not in facts['hosted']:
+                facts['hosted']['registry'] = {}
+            facts['hosted']['registry']['selector'] = facts['master'].pop('registry_selector')
     return facts
 
 def first_ip(network):
@@ -466,11 +472,11 @@ def set_selectors(facts):
         facts['hosted']['router'] = {}
     if 'selector' not in facts['hosted']['router'] or facts['hosted']['router']['selector'] in [None, 'None']:
         facts['hosted']['router']['selector'] = selector
+    if 'registry' not in facts['hosted']:
+        facts['hosted']['registry'] = {}
+    if 'selector' not in facts['hosted']['registry'] or facts['hosted']['registry']['selector'] in [None, 'None']:
+        facts['hosted']['registry']['selector'] = selector
 
-    if 'master' in facts:
-        if 'infra_nodes' in facts['master']:
-            if 'registry_selector' not in facts['master']:
-                facts['master']['registry_selector'] = selector
     return facts
 
 def set_metrics_facts_if_unset(facts):


Test patch: