From b58cb69b708e5ae5dd15b6e021341fe4da83dc3d Mon Sep 17 00:00:00 2001 From: Anatoli Tsikhamirau Date: Mon, 14 Mar 2022 11:32:26 +0100 Subject: [PATCH] Preflight check of single node-exporter installation (#3011) --- .../install-node-exporter-as-daemonset.yml | 11 ++++----- ...nstall-node-exporter-as-system-service.yml | 3 +-- .../uninstall-node-exporter-as-daemonset.yml | 3 --- .../roles/node_exporter/tasks/main.yml | 5 ++-- .../node_exporter/tasks/preflight/upgrade.yml | 23 +++++++++++++++++++ .../tasks/upgrade/main.yml} | 9 +++++--- .../roles/preflight/tasks/upgrade.yml | 8 +++++++ ansible/playbooks/upgrade.yml | 6 ++--- 8 files changed, 47 insertions(+), 21 deletions(-) rename ansible/playbooks/roles/node_exporter/tasks/{ => apply}/install-node-exporter-as-daemonset.yml (96%) rename ansible/playbooks/roles/node_exporter/tasks/{ => apply}/install-node-exporter-as-system-service.yml (98%) rename ansible/playbooks/roles/node_exporter/tasks/{ => apply}/uninstall-node-exporter-as-daemonset.yml (98%) create mode 100644 ansible/playbooks/roles/node_exporter/tasks/preflight/upgrade.yml rename ansible/playbooks/roles/{upgrade/tasks/node-exporter.yml => node_exporter/tasks/upgrade/main.yml} (95%) diff --git a/ansible/playbooks/roles/node_exporter/tasks/install-node-exporter-as-daemonset.yml b/ansible/playbooks/roles/node_exporter/tasks/apply/install-node-exporter-as-daemonset.yml similarity index 96% rename from ansible/playbooks/roles/node_exporter/tasks/install-node-exporter-as-daemonset.yml rename to ansible/playbooks/roles/node_exporter/tasks/apply/install-node-exporter-as-daemonset.yml index 2c14941c91..55f71425c2 100644 --- a/ansible/playbooks/roles/node_exporter/tasks/install-node-exporter-as-daemonset.yml +++ b/ansible/playbooks/roles/node_exporter/tasks/apply/install-node-exporter-as-daemonset.yml @@ -1,13 +1,10 @@ --- - - name: Prepare configuration and upgrade/install Node-Exporter's Helm chart vars: # Handling "undefined", "null", "empty" and "boolean" values all at once. disable_helm_chart_bool: "{{ specification.disable_helm_chart | default(false, true) | bool }}" - # Handling "undefined", "null" and "empty" values all at once. helm_chart_values_bool: "{{ specification.helm_chart_values | default(false) | ternary(true, false) }}" - delegate_to: localhost become: false run_once: true @@ -26,7 +23,7 @@ repository_url: "{{ custom_repository_url | default('http://localhost/epirepo', true) }}" - when: helm_chart_values_bool - # IF `helm_chart_values` + # IF `helm_chart_values` block: - name: Copy Node-Exporter's Helm chart's custom configuration to file copy: @@ -35,17 +32,17 @@ - name: Install Node-Exporter's Helm chart (with custom values.yaml) delegate_to: localhost - shell: | + command: | helm -n {{ monitoring_chart_namespace }} upgrade --install \ -f {{ download_directory }}/{{ node_exporter_helm_chart_name }}_values.yaml \ {{ node_exporter_helm_chart_name }} \ {{ download_directory }}/{{ exporter_chart_file_name }} --create-namespace - when: not helm_chart_values_bool - # ELSE + # ELSE block: - name: Install Node-Exporter's Helm chart (with default values.yaml) - shell: | + command: | helm -n {{ monitoring_chart_namespace }} upgrade --install \ {{ node_exporter_helm_chart_name }} \ {{ download_directory }}/{{ exporter_chart_file_name }} --create-namespace diff --git a/ansible/playbooks/roles/node_exporter/tasks/install-node-exporter-as-system-service.yml b/ansible/playbooks/roles/node_exporter/tasks/apply/install-node-exporter-as-system-service.yml similarity index 98% rename from ansible/playbooks/roles/node_exporter/tasks/install-node-exporter-as-system-service.yml rename to ansible/playbooks/roles/node_exporter/tasks/apply/install-node-exporter-as-system-service.yml index 5f601bb943..86682f957e 100644 --- a/ansible/playbooks/roles/node_exporter/tasks/install-node-exporter-as-system-service.yml +++ b/ansible/playbooks/roles/node_exporter/tasks/apply/install-node-exporter-as-system-service.yml @@ -1,5 +1,4 @@ --- - - name: Create node_exporter system group group: name: node_exporter @@ -43,7 +42,7 @@ src: "{{ download_directory }}/{{ exporter_file_name }}" dest: "/opt/node_exporter" creates: "/opt/node_exporter/node_exporter" - extra_opts: [--strip-components=1] + extra_opts: [ --strip-components=1 ] mode: u=rwx,g=rx,o=rx owner: root group: node_exporter diff --git a/ansible/playbooks/roles/node_exporter/tasks/uninstall-node-exporter-as-daemonset.yml b/ansible/playbooks/roles/node_exporter/tasks/apply/uninstall-node-exporter-as-daemonset.yml similarity index 98% rename from ansible/playbooks/roles/node_exporter/tasks/uninstall-node-exporter-as-daemonset.yml rename to ansible/playbooks/roles/node_exporter/tasks/apply/uninstall-node-exporter-as-daemonset.yml index 1d3b4dbc49..587769ed2a 100644 --- a/ansible/playbooks/roles/node_exporter/tasks/uninstall-node-exporter-as-daemonset.yml +++ b/ansible/playbooks/roles/node_exporter/tasks/apply/uninstall-node-exporter-as-daemonset.yml @@ -1,9 +1,6 @@ --- - - name: "Uninstall Helm release {{ node_exporter_helm_chart_name }} from {{ node_exporter_release_ns }} ns" - delegate_to: localhost become: false run_once: true - command: "helm uninstall -n {{ node_exporter_release_ns }} {{ node_exporter_helm_chart_name }}" diff --git a/ansible/playbooks/roles/node_exporter/tasks/main.yml b/ansible/playbooks/roles/node_exporter/tasks/main.yml index 86601df110..11138567d3 100644 --- a/ansible/playbooks/roles/node_exporter/tasks/main.yml +++ b/ansible/playbooks/roles/node_exporter/tasks/main.yml @@ -1,8 +1,7 @@ --- - - name: Include installation tasks for Node Exporter as System Service - include_tasks: install-node-exporter-as-system-service.yml + include_tasks: apply/install-node-exporter-as-system-service.yml - name: Include installation tasks for Node Exporter as DaemonSet for "k8s as cloud service" when: k8s_as_cloud_service is defined and k8s_as_cloud_service - include_tasks: install-node-exporter-as-daemonset.yml + include_tasks: apply/install-node-exporter-as-daemonset.yml diff --git a/ansible/playbooks/roles/node_exporter/tasks/preflight/upgrade.yml b/ansible/playbooks/roles/node_exporter/tasks/preflight/upgrade.yml new file mode 100644 index 0000000000..32b71b8d6f --- /dev/null +++ b/ansible/playbooks/roles/node_exporter/tasks/preflight/upgrade.yml @@ -0,0 +1,23 @@ +--- +- name: Check that only one Node Exporter Helm release is installed + run_once: true + block: + - name: Include defaults from node_exporter role + include_vars: + file: roles/node_exporter/defaults/main.yml + name: exporter_defaults + + - name: Get information about node-exporter helm releases + become: true + command: "helm list --all-namespaces --output json --filter {{ exporter_defaults.node_exporter_helm_chart_name }}" + register: node_exporter_helm_list + changed_when: false + + - name: Assert node-exported installed only in one namespace + assert: + that: node_exporter_helm_list.stdout_lines|length == 1 + fail_msg: | + Multiple Helm installations of Node Exporter are not supported or there is nothing to upgrade. + `helm list` output: + {{ node_exporter_helm_list.stdout_lines }} + success_msg: There is only one Node Exporter release. diff --git a/ansible/playbooks/roles/upgrade/tasks/node-exporter.yml b/ansible/playbooks/roles/node_exporter/tasks/upgrade/main.yml similarity index 95% rename from ansible/playbooks/roles/upgrade/tasks/node-exporter.yml rename to ansible/playbooks/roles/node_exporter/tasks/upgrade/main.yml index 58b5bdf914..a7661f8d47 100644 --- a/ansible/playbooks/roles/upgrade/tasks/node-exporter.yml +++ b/ansible/playbooks/roles/node_exporter/tasks/upgrade/main.yml @@ -1,14 +1,16 @@ --- - name: Node Exporter | Include defaults from node_exporter role include_vars: - file: roles/node_exporter/defaults/main.yml + file: defaults/main.yml name: exporter_defaults - name: Node Exporter | Include specification vars from node_exporter role include_vars: - file: roles/node_exporter/vars/main.yml + file: vars/main.yml name: exporter_vars +# Re-installation in a different namespace was added in Epiphany v1.3 +# Everything related to that topic can be removed when versions before 1.3 are unsupported, including preflight checks - name: Node Exporter as DaemonSet | Prepare fact about helm release and reinstall daemonset if release exists delegate_to: localhost become: false @@ -86,6 +88,7 @@ - name: Node Exporter as System Service | Collect version shell: >- /opt/node_exporter/node_exporter --version 2>&1 + changed_when: false register: exporter_current_version_out - name: Node Exporter as System Service | Set exporter_current_version fact @@ -140,7 +143,7 @@ src: "{{ download_directory }}/{{ exporter_file_name }}" dest: /opt/node_exporter creates: /opt/node_exporter/node_exporter - extra_opts: [--strip-components=1] + extra_opts: [ --strip-components=1 ] mode: u=rwx,go=rx owner: root group: node_exporter diff --git a/ansible/playbooks/roles/preflight/tasks/upgrade.yml b/ansible/playbooks/roles/preflight/tasks/upgrade.yml index 65016a5c1a..91e2309e94 100644 --- a/ansible/playbooks/roles/preflight/tasks/upgrade.yml +++ b/ansible/playbooks/roles/preflight/tasks/upgrade.yml @@ -21,3 +21,11 @@ - groups.postgresql is defined - groups.postgresql | length > 0 - "'postgresql' in group_names" # run only on PostgreSQL hosts + +- name: Node Exporter in K8s checks + include_tasks: roles/node_exporter/tasks/preflight/upgrade.yml + when: + - "'node_exporter' in upgrade_components or upgrade_components|length == 0" + - k8s_as_cloud_service is defined and k8s_as_cloud_service + - "'node_exporter' in group_names" + - "'kubernetes_master' in group_names" diff --git a/ansible/playbooks/upgrade.yml b/ansible/playbooks/upgrade.yml index 6639327668..81cb260ea1 100644 --- a/ansible/playbooks/upgrade.yml +++ b/ansible/playbooks/upgrade.yml @@ -280,9 +280,9 @@ become: true become_method: sudo tasks: - - import_role: - name: upgrade - tasks_from: node-exporter + - include_role: + name: node_exporter + tasks_from: upgrade/main when: "'node_exporter' in upgrade_components or upgrade_components|length == 0" environment: KUBECONFIG: "{{ kubeconfig.local }}"