Browse Source

Retrieve node list from API when testing for nodes with selector.

Andrew Butcher 7 years ago
parent
commit
6b4e69dd7c

+ 8 - 43
roles/lib_utils/filter_plugins/oo_filters.py

@@ -23,7 +23,7 @@ from ansible import errors
 from ansible.parsing.yaml.dumper import AnsibleDumper
 
 # pylint: disable=import-error,no-name-in-module
-from ansible.module_utils.six import string_types, u
+from ansible.module_utils.six import iteritems, string_types, u
 # pylint: disable=import-error,no-name-in-module
 from ansible.module_utils.six.moves.urllib.parse import urlparse
 
@@ -660,48 +660,14 @@ def map_from_pairs(source, delim="="):
     return dict(item.split(delim) for item in source.split(","))
 
 
-def lib_utils_oo_get_node_labels(source, hostvars=None):
-    ''' Return a list of labels assigned to schedulable nodes '''
-    labels = list()
+def map_to_pairs(source, delim="="):
+    ''' Returns a comma separated str given the source as a dict '''
 
-    # Filter out the unschedulable nodes
-    for host in source:
-        if host not in hostvars:
-            return
-        node_vars = hostvars[host]
+    # Some default selectors are empty strings.
+    if source == {} or source == '':
+        return str()
 
-        # All nodes are considered schedulable,
-        # unless explicitly marked so
-        schedulable = node_vars.get('openshift_schedulable')
-        if schedulable is None:
-            schedulable = True
-        try:
-            if not strtobool(str(schedulable)):
-                # explicitly marked as unschedulable
-                continue
-        except ValueError:
-            # Incorrect value in openshift_schedulable, skip node
-            continue
-
-        # Get a list of labels from the node
-        node_labels = node_vars.get('openshift_node_labels')
-        if node_labels:
-            labels.append(node_labels)
-
-    return labels
-
-
-def lib_utils_oo_has_no_matching_selector(source, selector=None):
-    ''' Return True when selector cannot be placed
-        on nodes with labels from source '''
-    # Empty selector means any node
-    if not selector:
-        return False
-    for item in source:
-        if set(selector.items()).issubset(set(item.items())):
-            # Matching selector found
-            return False
-    return True
+    return ','.join(["{}{}{}".format(key, delim, value) for key, value in iteritems(source)])
 
 
 class FilterModule(object):
@@ -735,7 +701,6 @@ class FilterModule(object):
             "lib_utils_oo_selector_to_string_list": lib_utils_oo_selector_to_string_list,
             "lib_utils_oo_filter_sa_secrets": lib_utils_oo_filter_sa_secrets,
             "lib_utils_oo_l_of_d_to_csv": lib_utils_oo_l_of_d_to_csv,
-            "lib_utils_oo_has_no_matching_selector": lib_utils_oo_has_no_matching_selector,
-            "lib_utils_oo_get_node_labels": lib_utils_oo_get_node_labels,
             "map_from_pairs": map_from_pairs,
+            "map_to_pairs": map_to_pairs,
         }

+ 0 - 3
roles/openshift_facts/defaults/main.yml

@@ -100,6 +100,3 @@ openshift_service_type_dict:
   openshift-enterprise: atomic-openshift
 
 openshift_service_type: "{{ openshift_service_type_dict[openshift_deployment_type] }}"
-
-# Create a list of node labels (dict) for schedulable nodes
-openshift_schedulable_node_labels: "{{ groups['oo_nodes_to_config'] | lib_utils_oo_get_node_labels(hostvars) }}"

+ 7 - 6
roles/openshift_logging_curator/tasks/main.yaml

@@ -14,12 +14,13 @@
 
 - include_tasks: determine_version.yaml
 
-- name: Ensure that logging curator has nodes to run on
-  fail:
-    msg: |-
-      No schedulable nodes found matching node selector for logging curator - '{{ openshift_logging_curator_nodeselector }}'
-  when:
-    - openshift_schedulable_node_labels | lib_utils_oo_has_no_matching_selector(openshift_logging_curator_nodeselector)
+- name: Ensure that Logging Curator has nodes to run on
+  import_role:
+    name: openshift_master
+    tasks_from: ensure_nodes_matching_selector.yml
+  vars:
+    openshift_master_ensure_nodes_selector: "{{ openshift_logging_curator_nodeselector | map_to_pairs }}"
+    openshift_master_ensure_nodes_service: Logging Curator
 
 # allow passing in a tempdir
 - name: Create temp directory for doing work in

+ 6 - 5
roles/openshift_logging_elasticsearch/tasks/main.yaml

@@ -1,10 +1,11 @@
 ---
 - name: Ensure that ElasticSearch has nodes to run on
-  fail:
-    msg: |-
-      No schedulable nodes found matching node selector for Elasticsearch - '{{ openshift_logging_es_nodeselector }}'
-  when:
-  - openshift_schedulable_node_labels | lib_utils_oo_has_no_matching_selector(openshift_logging_es_nodeselector)
+  import_role:
+    name: openshift_master
+    tasks_from: ensure_nodes_matching_selector.yml
+  vars:
+    openshift_master_ensure_nodes_selector: "{{ openshift_logging_es_nodeselector | map_to_pairs }}"
+    openshift_master_ensure_nodes_service: Elasticsearch
 
 - name: Validate Elasticsearch cluster size
   fail: msg="The openshift_logging_es_cluster_size may only be scaled down manually. Please see official documentation on how to do this."

+ 7 - 6
roles/openshift_logging_eventrouter/tasks/install_eventrouter.yaml

@@ -4,12 +4,13 @@
     msg: Invalid sink type "{{openshift_logging_eventrouter_sink}}", only one of "{{__eventrouter_sinks}}" allowed
     that: openshift_logging_eventrouter_sink in __eventrouter_sinks
 
-- name: Ensure that logging eventrouter has nodes to run on
-  fail:
-    msg: |-
-      No schedulable nodes found matching node selector for logging EventRouter - '{{ openshift_logging_eventrouter_nodeselector }}'
-  when:
-  - openshift_schedulable_node_labels | lib_utils_oo_has_no_matching_selector(openshift_logging_eventrouter_nodeselector)
+- name: Ensure that Logging EventRouter has nodes to run on
+  import_role:
+    name: openshift_master
+    tasks_from: ensure_nodes_matching_selector.yml
+  vars:
+    openshift_master_ensure_nodes_selector: "{{ openshift_logging_eventrouter_nodeselector | map_to_pairs }}"
+    openshift_master_ensure_nodes_service: Logging EventRouter
 
 # allow passing in a tempdir
 - name: Create temp directory for doing work in

+ 6 - 5
roles/openshift_logging_kibana/tasks/main.yaml

@@ -9,11 +9,12 @@
     loop_var: var_file_name
 
 - name: Ensure that Kibana has nodes to run on
-  fail:
-    msg: |-
-      No schedulable nodes found matching node selector for Kibana - '{{ openshift_logging_kibana_nodeselector }}'
-  when:
-    - openshift_schedulable_node_labels | lib_utils_oo_has_no_matching_selector(openshift_logging_kibana_nodeselector)
+  import_role:
+    name: openshift_master
+    tasks_from: ensure_nodes_matching_selector.yml
+  vars:
+    openshift_master_ensure_nodes_selector: "{{ openshift_logging_kibana_nodeselector | map_to_pairs }}"
+    openshift_master_ensure_nodes_service: Kibana
 
 - name: Set kibana image facts
   set_fact:

+ 7 - 6
roles/openshift_logging_mux/tasks/main.yaml

@@ -7,12 +7,13 @@
     msg: Operations logs destination is required
   when: not openshift_logging_mux_ops_host or openshift_logging_mux_ops_host == ''
 
-- name: Ensure that logging mux has nodes to run on
-  fail:
-    msg: |-
-      No schedulable nodes found matching node selector for logging mux - '{{ openshift_logging_mux_nodeselector }}'
-  when:
-  - openshift_schedulable_node_labels | lib_utils_oo_has_no_matching_selector(openshift_logging_mux_nodeselector)
+- name: Ensure that Logging Mux has nodes to run on
+  import_role:
+    name: openshift_master
+    tasks_from: ensure_nodes_matching_selector.yml
+  vars:
+    openshift_master_ensure_nodes_selector: "{{ openshift_logging_mux_nodeselector | map_to_pairs }}"
+    openshift_master_ensure_nodes_service: Logging Mux
 
 - name: Set default image variables based on openshift_deployment_type
   include_vars: "{{ var_file_name }}"

+ 14 - 0
roles/openshift_master/tasks/ensure_nodes_matching_selector.yml

@@ -0,0 +1,14 @@
+---
+- name: Retrieve list of schedulable nodes matching selector
+  oc_obj:
+    state: list
+    kind: node
+    selector: "{{ openshift_master_ensure_nodes_selector }}"
+    field_selector: "spec.unschedulable!=true"
+  register: __schedulable_nodes_matching_selector
+
+- name: "Ensure that {{ openshift_master_ensure_nodes_service }} has nodes to run on"
+  assert:
+    that: "{{  __schedulable_nodes_matching_selector['results']['results'][0]['items'] | default([]) | length != 0 }}"
+    msg: |-
+      No schedulable nodes found matching node selector for {{ openshift_master_ensure_nodes_service }} - '{{ openshift_master_ensure_nodes_selector }}'

+ 6 - 5
roles/openshift_metrics/tasks/install_cassandra.yaml

@@ -1,10 +1,11 @@
 ---
 - name: Ensure that Cassandra has nodes to run on
-  fail:
-    msg: |-
-      No schedulable nodes found matching node selector for cassandra - '{{ openshift_metrics_cassandra_nodeselector }}'
-  when:
-  - openshift_schedulable_node_labels | lib_utils_oo_has_no_matching_selector(openshift_metrics_cassandra_nodeselector)
+  import_role:
+    name: openshift_master
+    tasks_from: ensure_nodes_matching_selector.yml
+  vars:
+    openshift_master_ensure_nodes_selector: "{{ openshift_metrics_cassandra_nodeselector | map_to_pairs }}"
+    openshift_master_ensure_nodes_service: Cassandra
 
 - shell: >
     {{ openshift_client_binary }} -n {{ openshift_metrics_project | quote }}

+ 6 - 5
roles/openshift_metrics/tasks/install_hawkular.yaml

@@ -1,10 +1,11 @@
 ---
 - name: Ensure that Hawkular has nodes to run on
-  fail:
-    msg: |-
-      No schedulable nodes found matching node selector for hawkular - '{{ openshift_metrics_hawkular_nodeselector }}'
-  when:
-  - openshift_schedulable_node_labels | lib_utils_oo_has_no_matching_selector(openshift_metrics_hawkular_nodeselector)
+  import_role:
+    name: openshift_master
+    tasks_from: ensure_nodes_matching_selector.yml
+  vars:
+    openshift_master_ensure_nodes_selector: "{{ openshift_metrics_hawkular_nodeselector | map_to_pairs }}"
+    openshift_master_ensure_nodes_service: Hawkular
 
 - command: >
     {{ openshift_client_binary }} -n {{ openshift_metrics_project | quote }}

+ 6 - 5
roles/openshift_metrics/tasks/install_heapster.yaml

@@ -1,10 +1,11 @@
 ---
 - name: Ensure that Heapster has nodes to run on
-  fail:
-    msg: |-
-      No schedulable nodes found matching node selector for heapster - '{{ openshift_metrics_heapster_nodeselector }}'
-  when:
-  - openshift_schedulable_node_labels | lib_utils_oo_has_no_matching_selector(openshift_metrics_heapster_nodeselector)
+  import_role:
+    name: openshift_master
+    tasks_from: ensure_nodes_matching_selector.yml
+  vars:
+    openshift_master_ensure_nodes_selector: "{{ openshift_metrics_heapster_nodeselector | map_to_pairs }}"
+    openshift_master_ensure_nodes_service: Heapster
 
 - command: >
     {{ openshift_client_binary }} -n {{ openshift_metrics_project | quote }}

+ 7 - 6
roles/openshift_metrics/tasks/install_hosa.yaml

@@ -1,10 +1,11 @@
 ---
-- name: Ensure that Hawkular agent has nodes to run on
-  fail:
-    msg: |-
-      No schedulable nodes found matching node selector for Hawkular agent - '{{ openshift_metrics_hawkular_agent_nodeselector }}'
-  when:
-  - openshift_schedulable_node_labels | lib_utils_oo_has_no_matching_selector(openshift_metrics_hawkular_agent_nodeselector)
+- name: Ensure that Hawkular Agent (HOSA) has nodes to run on
+  import_role:
+    name: openshift_master
+    tasks_from: ensure_nodes_matching_selector.yml
+  vars:
+    openshift_master_ensure_nodes_selector: "{{ openshift_metrics_hawkular_agent_nodeselector | map_to_pairs }}"
+    openshift_master_ensure_nodes_service: Hawkular Agent (HOSA)
 
 - name: Generate Hawkular Agent (HOSA) Cluster Role
   template:

+ 6 - 5
roles/openshift_prometheus/tasks/install_prometheus.yaml

@@ -3,11 +3,12 @@
 - include_tasks: facts.yaml
 
 - name: Ensure that Prometheus has nodes to run on
-  fail:
-    msg: |-
-      No schedulable nodes found matching node selector for Prometheus - '{{ openshift_prometheus_node_selector }}'
-  when:
-  - openshift_schedulable_node_labels | lib_utils_oo_has_no_matching_selector(openshift_prometheus_node_selector)
+  import_role:
+    name: openshift_master
+    tasks_from: ensure_nodes_matching_selector.yml
+  vars:
+    openshift_master_ensure_nodes_selector: "{{ openshift_prometheus_node_selector | map_to_pairs }}"
+    openshift_master_ensure_nodes_service: Prometheus
 
 # namespace
 - name: Add prometheus project

+ 7 - 6
roles/openshift_provisioners/tasks/install_provisioners.yaml

@@ -15,12 +15,13 @@
   fail: msg='the openshift_provisioners_efs_aws_secret_access_key variable is required'
   when: (openshift_provisioners_efs | bool) and openshift_provisioners_efs_aws_secret_access_key is not defined
 
-- name: Ensure that provisioners have nodes to run on
-  fail:
-    msg: |-
-      No schedulable nodes found matching node selector for Prometheus - '{{ openshift_provisioners_efs_nodeselector }}'
-  when:
-  - openshift_schedulable_node_labels | lib_utils_oo_has_no_matching_selector(openshift_provisioners_efs_nodeselector)
+- name: Ensure that Provisioners has nodes to run on
+  import_role:
+    name: openshift_master
+    tasks_from: ensure_nodes_matching_selector.yml
+  vars:
+    openshift_master_ensure_nodes_selector: "{{ openshift_provisioners_efs_nodeselector | map_to_pairs }}"
+    openshift_master_ensure_nodes_service: Provisioners
 
 - name: Install support
   include_tasks: install_support.yaml

+ 6 - 6
roles/template_service_broker/tasks/install.yml

@@ -1,11 +1,11 @@
 ---
-# Fact setting
 - name: Ensure that Template Service Broker has nodes to run on
-  fail:
-    msg: |-
-      No schedulable nodes found matching node selector for Template Service Broker - '{{ template_service_broker_selector }}'
-  when:
-  - openshift_schedulable_node_labels | lib_utils_oo_has_no_matching_selector(template_service_broker_selector)
+  import_role:
+    name: openshift_master
+    tasks_from: ensure_nodes_matching_selector.yml
+  vars:
+    openshift_master_ensure_nodes_selector: "{{ template_service_broker_selector | map_to_pairs }}"
+    openshift_master_ensure_nodes_service: Template Service Broker
 
 - name: Set default image variables based on openshift_deployment_type
   include_vars: "{{ item }}"