forked from techno-tim/k3s-ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test playbook using molecule (techno-tim#67)
* Test cluster using molecule * Fix detection of first control node * Include --flannel-iface and --node-ip as k3s arguments * Store logs of k3s-init.service as GitHub job artifacts
- Loading branch information
Showing
25 changed files
with
406 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
--- | ||
exclude_paths: | ||
# default paths | ||
- '.cache/' | ||
- '.github/' | ||
- 'test/fixtures/formatting-before/' | ||
- 'test/fixtures/formatting-prettier/' | ||
|
||
# The "converge" and "reset" playbooks use import_playbook in | ||
# conjunction with the "env" lookup plugin, which lets the | ||
# syntax check of ansible-lint fail. | ||
- '**/converge.yml' | ||
- '**/reset.yml' | ||
|
||
skip_list: | ||
- 'fqcn-builtins' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
.vagrant | ||
.env/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
collections: | ||
- name: community.general | ||
- name: ansible.posix | ||
- name: kubernetes.core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Test suites for `k3s-ansible` | ||
|
||
This folder contains the [molecule](https://molecule.rtfd.io/)-based test setup for this playbook. | ||
|
||
## Scenarios | ||
|
||
We have these scenarios: | ||
|
||
- **default**: | ||
A 3 control + 2 worker node cluster based very closely on the [sample inventory](../inventory/sample/). | ||
|
||
## How to execute | ||
|
||
To test on your local machine, follow these steps: | ||
|
||
### System requirements | ||
|
||
Make sure that the following software packages are available on your system: | ||
|
||
- [Python 3](https://www.python.org/downloads) | ||
- [Vagrant](https://www.vagrantup.com/downloads) | ||
- [VirtualBox](https://www.virtualbox.org/wiki/Downloads) | ||
|
||
### Set up VirtualBox networking on Linux and macOS | ||
|
||
_You can safely skip this if you are working on Windows._ | ||
|
||
Furthermore, the test cluster uses the `192.168.30.0/24` subnet which is [not set up by VirtualBox automatically](https://www.virtualbox.org/manual/ch06.html#network_hostonly). | ||
To set the subnet up for use with VirtualBox, please make sure that `/etc/vbox/networks.conf` exists and that it contains this line: | ||
|
||
``` | ||
* 192.168.30.0/24` | ||
``` | ||
|
||
### Install Python dependencies | ||
|
||
You will get [Molecule, Ansible and a few extra dependencies](../requirements.txt) via [pip](https://pip.pypa.io/). | ||
Usually, it is advisable to work in a [virtual environment](https://docs.python.org/3/tutorial/venv.html) for this: | ||
|
||
```bash | ||
cd /path/to/k3s-ansible | ||
|
||
# Create a virtualenv at ".env". You only need to do this once. | ||
python3 -m venv .env | ||
|
||
# Activate the virtualenv for your current shell session. | ||
# If you start a new session, you will have to repeat this. | ||
source .env/bin/activate | ||
|
||
# Install the required packages into the virtualenv. | ||
# These remain installed across shell sessions. | ||
python3 -m pip install -r requirements.txt | ||
``` | ||
|
||
### Run molecule | ||
|
||
With the virtual environment from the previous step active in your shell session, you can now use molecule to test the playbook. | ||
Interesting commands are: | ||
|
||
- `molecule create`: Create virtual machines for the test cluster nodes. | ||
- `molecule destroy`: Delete the virtual machines for the test cluster nodes. | ||
- `molecule converge`: Run the `site` playbook on the nodes of the test cluster. | ||
- `molecule side_effect`: Run the `reset` playbook on the nodes of the test cluster. | ||
- `molecule verify`: Verify that the cluster works correctly. | ||
- `molecule test`: The "all-in-one" sequence of steps that is executed in CI. | ||
This includes the `create`, `converge`, `verify`, `side_effect` and `destroy` steps. | ||
See [`molecule.yml`](default/molecule.yml) for more details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- | ||
dependency: | ||
name: galaxy | ||
driver: | ||
name: vagrant | ||
platforms: | ||
- &control | ||
name: control1 | ||
box: generic/ubuntu2204 | ||
memory: 2048 | ||
cpus: 2 | ||
config_options: | ||
# We currently can not use public-key based authentication on Ubuntu 22.04, | ||
# see: https://github.com/chef/bento/issues/1405 | ||
ssh.username: "vagrant" | ||
ssh.password: "vagrant" | ||
groups: | ||
- k3s_cluster | ||
- master | ||
interfaces: | ||
- network_name: private_network | ||
ip: 192.168.30.38 | ||
- <<: *control | ||
name: control2 | ||
interfaces: | ||
- network_name: private_network | ||
ip: 192.168.30.39 | ||
- <<: *control | ||
name: control3 | ||
interfaces: | ||
- network_name: private_network | ||
ip: 192.168.30.40 | ||
- &node | ||
<<: *control | ||
name: node1 | ||
groups: | ||
- k3s_cluster | ||
- node | ||
interfaces: | ||
- network_name: private_network | ||
ip: 192.168.30.41 | ||
- <<: *node | ||
name: node2 | ||
interfaces: | ||
- network_name: private_network | ||
ip: 192.168.30.42 | ||
provisioner: | ||
name: ansible | ||
playbooks: | ||
converge: ../resources/converge.yml | ||
side_effect: ../resources/reset.yml | ||
verify: ../resources/verify.yml | ||
inventory: | ||
links: | ||
group_vars: ../../inventory/sample/group_vars | ||
env: | ||
OVERRIDES_FILE: ../default/overrides.yml | ||
scenario: | ||
test_sequence: | ||
- dependency | ||
- lint | ||
- cleanup | ||
- destroy | ||
- syntax | ||
- create | ||
- prepare | ||
- converge | ||
# idempotence is not possible with the playbook in its current form. | ||
- verify | ||
# We are repurposing side_effect here to test the reset playbook. | ||
# This is why we do not run it before verify (which tests the cluster), | ||
# but after the verify step. | ||
- side_effect | ||
- cleanup | ||
- destroy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
- name: Apply overrides | ||
hosts: all | ||
tasks: | ||
- name: Override host variables | ||
ansible.builtin.set_fact: | ||
# See: https://github.com/flannel-io/flannel/blob/67d603aaf45ef80f5dd39f43714fc5e6f8a637eb/Documentation/troubleshooting.md#Vagrant # noqa yaml[line-length] | ||
flannel_iface: eth1 | ||
|
||
# The test VMs might be a bit slow, so we give them more time to join the cluster: | ||
retry_count: 45 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
- name: Apply overrides | ||
ansible.builtin.import_playbook: >- | ||
{{ lookup("ansible.builtin.env", "OVERRIDES_FILE") }} | ||
- name: Converge | ||
ansible.builtin.import_playbook: ../../site.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
- name: Apply overrides | ||
ansible.builtin.import_playbook: >- | ||
{{ lookup("ansible.builtin.env", "OVERRIDES_FILE") }} | ||
- name: Reset | ||
ansible.builtin.import_playbook: ../../reset.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
- name: Verify | ||
hosts: all | ||
roles: | ||
- verify/from_outside |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
# A host outside of the cluster from which the checks shall be performed | ||
outside_host: localhost | ||
|
||
# This kubernetes namespace will be used for testing | ||
testing_namespace: molecule-verify-from-outside | ||
|
||
# The directory in which the example manifests reside | ||
example_manifests_path: ../../../../example |
5 changes: 5 additions & 0 deletions
5
molecule/resources/verify/from_outside/tasks/kubecfg-cleanup.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
- name: Clean up kubecfg | ||
ansible.builtin.file: | ||
path: "{{ kubecfg.path }}" | ||
state: absent |
19 changes: 19 additions & 0 deletions
19
molecule/resources/verify/from_outside/tasks/kubecfg-fetch.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
- name: Create temporary directory for kubecfg | ||
ansible.builtin.tempfile: | ||
state: directory | ||
suffix: kubecfg | ||
register: kubecfg | ||
- name: Gathering facts | ||
delegate_to: "{{ groups['master'][0] }}" | ||
ansible.builtin.gather_facts: | ||
- name: Download kubecfg | ||
ansible.builtin.fetch: | ||
src: "{{ ansible_env.HOME }}/.kube/config" | ||
dest: "{{ kubecfg.path }}/" | ||
flat: true | ||
delegate_to: "{{ groups['master'][0] }}" | ||
delegate_facts: true | ||
- name: Store path to kubecfg | ||
ansible.builtin.set_fact: | ||
kubecfg_path: "{{ kubecfg.path }}/config" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
- name: Verify | ||
run_once: true | ||
delegate_to: "{{ outside_host }}" | ||
block: | ||
- ansible.builtin.import_tasks: kubecfg-fetch.yml | ||
- name: "TEST CASE: Get nodes" | ||
ansible.builtin.include_tasks: test/get-nodes.yml | ||
- name: "TEST CASE: Deploy example" | ||
ansible.builtin.include_tasks: test/deploy-example.yml | ||
always: | ||
- ansible.builtin.import_tasks: kubecfg-cleanup.yml |
Oops, something went wrong.