Skip to content

Commit

Permalink
Fix(tests): Resolve Ansible Lint warnings and fix Molecule tests on G…
Browse files Browse the repository at this point in the history
…itHub Actions (#202)

* fix(ansible-lint): FQDN and `name`

* fix(ansible-lint): add `name` and FQDN for module call

* fix(ansible-lint): add `name` to tasks and FQDN for module

* fix(ansible-lint): add task `name` and FQDN for module calls

* fix(ansible-lint): last `include_tasks`

* fix(ansible-lint): add task names and FQDN

* refactor: `Ensure` to `Run`

* [skip ci]refactor: add exist and seperate ensure installed node task, mention build cluster

* [skip ci]refactor: Pipe seperator

* [skip ci]refactor: run

* refactor: remove quotes as other files don't use them

For templated vars in task name

* [skip ci]refactor: task names, use `Run`

* [skip ci]refactor: use variable name in task name

* [skip ci]refactor: task names

* [skip ci]refactor: add service mgr in task name

* [skip ci]refactor: add task names and module FQDNs

* [skip ci]refactor: fix task name

* [skip ci]refactor: add -

* [skip ci]refactor: include task names and FQDNs

* [skip ci]refactor: add task names and FQDNs

* [skip ci]: ignore `name[template]`

* refactor: `when` clause for `block` should be before `block`

* fix: ansible/molecule#3883

* refactor: molecule lint command was removed in version `5.0.0`

Use separate CI job step to run linting instead.

* [skip ci]refactor: noqa for command tasks

Subject to change

* refactor: use Ubuntu 22.04

Suspect issues with Molecule tests are related to cgroups v2.
  • Loading branch information
dbrennand committed May 13, 2023
1 parent 0cc1e48 commit de1bd09
Show file tree
Hide file tree
Showing 25 changed files with 127 additions and 96 deletions.
1 change: 1 addition & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

skip_list:
- role-name
- name[template]
25 changes: 24 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,32 @@ defaults:
working-directory: "xanmanning.k3s"

jobs:
ansible-lint:
name: Ansible Lint
runs-on: ubuntu-latest
steps:
- name: Checkout codebase
uses: actions/checkout@v2
with:
path: "xanmanning.k3s"

- name: Set up Python 3
uses: actions/setup-python@v2
with:
python-version: "3.x"

- name: Install test dependencies
run: pip3 install -r molecule/lint-requirements.txt

- name: Run yamllint
run: yamllint -s .

- name: Run ansible-lint
run: ansible-lint --exclude molecule/ --exclude meta/

molecule:
name: Molecule
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
Expand Down
5 changes: 0 additions & 5 deletions molecule/autodeploy/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ driver:
scenario:
test_sequence:
- dependency
- lint
- cleanup
- destroy
- syntax
Expand All @@ -20,10 +19,6 @@ scenario:
- verify
- cleanup
- destroy
lint: |
set -e
yamllint -s .
ansible-lint --exclude molecule/ --exclude meta/
platforms:
- name: node1
image: ${MOLECULE_DISTRO:-"geerlingguy/docker-rockylinux8-ansible:latest"}
Expand Down
5 changes: 0 additions & 5 deletions molecule/debug/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ driver:
scenario:
test_sequence:
- dependency
- lint
- cleanup
- destroy
- syntax
Expand All @@ -20,10 +19,6 @@ scenario:
- verify
- cleanup
- destroy
lint: |
set -e
yamllint -s .
ansible-lint --exclude molecule/ --exclude meta/
platforms:
- name: node1
image: ${MOLECULE_DISTRO:-"geerlingguy/docker-rockylinux8-ansible:latest"}
Expand Down
5 changes: 0 additions & 5 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ driver:
scenario:
test_sequence:
- dependency
- lint
- cleanup
- destroy
- syntax
Expand All @@ -20,10 +19,6 @@ scenario:
- verify
- cleanup
- destroy
lint: |
set -e
yamllint -s .
ansible-lint --exclude molecule/ --exclude meta/
platforms:
- name: node1
image: ${MOLECULE_DISTRO:-"geerlingguy/docker-rockylinux8-ansible:latest"}
Expand Down
5 changes: 0 additions & 5 deletions molecule/highavailabilitydb/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ driver:
scenario:
test_sequence:
- dependency
- lint
- cleanup
- destroy
- syntax
Expand All @@ -20,10 +19,6 @@ scenario:
- verify
- cleanup
- destroy
lint: |
set -e
yamllint -s .
ansible-lint --exclude molecule/ --exclude meta/
platforms:
- name: node1
image: ${MOLECULE_DISTRO:-"geerlingguy/docker-rockylinux8-ansible:latest"}
Expand Down
5 changes: 0 additions & 5 deletions molecule/highavailabilityetcd/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ driver:
scenario:
test_sequence:
- dependency
- lint
- cleanup
- destroy
- syntax
Expand All @@ -20,10 +19,6 @@ scenario:
- verify
- cleanup
- destroy
lint: |
set -e
yamllint -s .
ansible-lint --exclude molecule/ --exclude meta/
platforms:
- name: node1
image: ${MOLECULE_DISTRO:-"geerlingguy/docker-rockylinux8-ansible:latest"}
Expand Down
4 changes: 4 additions & 0 deletions molecule/lint-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-r ../requirements.txt

yamllint>=1.25.0
ansible-lint>=4.3.5
5 changes: 0 additions & 5 deletions molecule/nodeploy/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ driver:
scenario:
test_sequence:
- dependency
- lint
- cleanup
- destroy
- syntax
Expand All @@ -20,10 +19,6 @@ scenario:
- verify
- cleanup
- destroy
lint: |
set -e
yamllint -s .
ansible-lint --exclude molecule/ --exclude meta/
platforms:
- name: node1
image: ${MOLECULE_DISTRO:-"geerlingguy/docker-rockylinux8-ansible:latest"}
Expand Down
4 changes: 1 addition & 3 deletions molecule/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
-r ../requirements.txt

molecule[docker]>=3.2
molecule-plugins[docker]
docker>=4.3.1
yamllint>=1.25.0
ansible-lint>=4.3.5
8 changes: 5 additions & 3 deletions tasks/ensure_cluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,22 @@
become: "{{ k3s_become }}"

- name: Ensure secondary controllers are started
include_tasks: ensure_control_plane_started_{{ ansible_service_mgr }}.yml
ansible.builtin.include_tasks: ensure_control_plane_started_{{ ansible_service_mgr }}.yml
when:
- k3s_control_node
- not k3s_primary_control_node

- import_tasks: post_checks_control_plane.yml
- name: Run control plane post checks
ansible.builtin.import_tasks: post_checks_control_plane.yml
when:
- not k3s_skip_validation
- not k3s_skip_post_checks

- name: Flush Handlers
ansible.builtin.meta: flush_handlers

- import_tasks: post_checks_nodes.yml
- name: Run node post checks
ansible.builtin.import_tasks: post_checks_nodes.yml
when:
- not k3s_skip_validation
- not k3s_skip_post_checks
16 changes: 7 additions & 9 deletions tasks/ensure_drain_and_remove_nodes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
become: "{{ k3s_become }}"

- name: Clean up nodes that are in an uninstalled state
when:
- k3s_check_kubectl.stat.exists is defined
- k3s_check_kubectl.stat.exists
- k3s_control_delegate is defined
- not ansible_check_mode
block:

- name: Gather a list of nodes
ansible.builtin.command:
cmd: "{{ k3s_install_dir }}/kubectl get nodes"
Expand All @@ -19,7 +23,7 @@
register: kubectl_get_nodes_result
become: "{{ k3s_become }}"

- name: Ensure uninstalled nodes are drained
- name: Ensure uninstalled nodes are drained # noqa no-changed-when
ansible.builtin.command:
cmd: >-
{{ k3s_install_dir }}/kubectl drain {{ item }}
Expand All @@ -36,7 +40,7 @@
loop: "{{ ansible_play_hosts }}"
become: "{{ k3s_become }}"

- name: Ensure uninstalled nodes are removed
- name: Ensure uninstalled nodes are removed # noqa no-changed-when
ansible.builtin.command:
cmd: "{{ k3s_install_dir }}/kubectl delete node {{ item }}"
delegate_to: "{{ k3s_control_delegate }}"
Expand All @@ -48,9 +52,3 @@
- hostvars[item].k3s_state == 'uninstalled'
loop: "{{ ansible_play_hosts }}"
become: "{{ k3s_become }}"

when:
- k3s_check_kubectl.stat.exists is defined
- k3s_check_kubectl.stat.exists
- k3s_control_delegate is defined
- not ansible_check_mode
12 changes: 8 additions & 4 deletions tasks/ensure_installed.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
---

- include_tasks: ensure_directories.yml
- name: Ensure directories exist
ansible.builtin.include_tasks: ensure_directories.yml
loop: "{{ k3s_ensure_directories_exist }}"
loop_control:
loop_var: directory

- include_tasks: ensure_installed_node.yml
- name: Ensure installed node
ansible.builtin.include_tasks: ensure_installed_node.yml
when:
- ((k3s_control_node and k3s_controller_list | length == 1)
or (k3s_primary_control_node and k3s_controller_list | length > 1))
Expand All @@ -14,15 +16,17 @@
- name: Flush Handlers
ansible.builtin.meta: flush_handlers

- include_tasks: ensure_installed_node.yml
- name: Ensure installed node | k3s_build_cluster
ansible.builtin.include_tasks: ensure_installed_node.yml
when: k3s_build_cluster

- name: Determine if the systems are already clustered
ansible.builtin.stat:
path: "{{ k3s_token_location }}"
register: k3s_token_cluster_check

- include_tasks: ensure_control_plane_started_{{ ansible_service_mgr }}.yml
- name: Ensure control plane started with {{ ansible_service_mgr }}
ansible.builtin.include_tasks: ensure_control_plane_started_{{ ansible_service_mgr }}.yml
when: (k3s_control_node and k3s_controller_list | length == 1)
or (k3s_primary_control_node and k3s_controller_list | length > 1)
or k3s_token_cluster_check.stat.exists
2 changes: 1 addition & 1 deletion tasks/ensure_installed_node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
become: "{{ k3s_become }}"

- name: Ensure cluster token is present when pre-defined
when: k3s_control_token is defined
block:
- name: Ensure the cluster token file location exists
ansible.builtin.file:
Expand All @@ -44,7 +45,6 @@
become: "{{ k3s_become }}"
notify:
- "Restart k3s {{ k3s_service_handler[ansible_service_mgr] }}"
when: k3s_control_token is defined

- name: Ensure k3s service unit file is present
ansible.builtin.template:
Expand Down
15 changes: 6 additions & 9 deletions tasks/ensure_pre_configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@
loop: "{{ ansible_play_hosts }}"

- name: Ensure a k3s control node is defined if none are found in ansible_play_hosts
when:
- k3s_controller_list | length < 1
- k3s_build_cluster is defined
- k3s_build_cluster
block:

- name: Set the control host
ansible.builtin.set_fact:
k3s_control_node: true
Expand All @@ -52,11 +55,6 @@
- hostvars[item].k3s_control_node
loop: "{{ ansible_play_hosts }}"

when:
- k3s_controller_list | length < 1
- k3s_build_cluster is defined
- k3s_build_cluster

- name: Ensure a primary k3s control node is defined if multiple are found in ansible_play_hosts
ansible.builtin.set_fact:
k3s_primary_control_node: true
Expand Down Expand Up @@ -88,6 +86,8 @@
when: k3s_control_node is defined

- name: Delegate an initializing control plane node
when: k3s_registration_address is not defined
or k3s_control_delegate is not defined
block:
- name: Lookup control node from file
ansible.builtin.command:
Expand Down Expand Up @@ -124,6 +124,3 @@
when:
- k3s_registration_address is not defined
- k3s_control_node_address is not defined

when: k3s_registration_address is not defined
or k3s_control_delegate is not defined
2 changes: 1 addition & 1 deletion tasks/ensure_stopped.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
when: k3s_non_root is not defined or not k3s_non_root
become: "{{ k3s_become }}"

- name: Ensure k3s service is started
- name: Ensure k3s service is stopped
ansible.builtin.systemd:
name: k3s
state: stopped
Expand Down
6 changes: 4 additions & 2 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---

- import_tasks: pre_checks.yml
- name: Run pre-checks
ansible.builtin.import_tasks: pre_checks.yml

- include_tasks: state_{{ (k3s_state | lower) | default('installed') }}.yml
- name: Ensure state {{ (k3s_state | lower) | default('installed') }}
ansible.builtin.include_tasks: state_{{ (k3s_state | lower) | default('installed') }}.yml
Loading

0 comments on commit de1bd09

Please sign in to comment.