From efc703541c2af9450816565e00bfed8bdd835d6c Mon Sep 17 00:00:00 2001 From: Xan Manning Date: Tue, 1 Oct 2019 21:31:35 +0100 Subject: [PATCH] Updated for 0.10.0, adding molecule testing with Travis-CI --- .gitignore | 5 +-- .travis.yml | 39 +++++++++++++++++++++++ .yamllint | 11 +++++++ README.md | 3 ++ handlers/main.yml | 10 +++--- meta/main.yml | 6 ++++ molecule/default/Dockerfile.j2 | 26 +++++++++++++++ molecule/default/INSTALL.rst | 22 +++++++++++++ molecule/default/molecule.yml | 40 ++++++++++++++++++++++++ molecule/default/playbook-docker.yml | 9 ++++++ molecule/default/playbook.yml | 8 +++++ molecule/default/tests/test_default.py | 14 +++++++++ molecule/default/tests/test_default.pyc | Bin 0 -> 1025 bytes tasks/configure-k3s-cluster.yml | 25 ++++++++++++--- tasks/install-k3s.yml | 4 +-- templates/k3s.service.j2 | 5 ++- {tests => vagrant}/Vagrantfile | 0 {tests => vagrant}/ansible.cfg | 0 {tests => vagrant}/inventory.yml | 2 ++ {tests => vagrant}/test-single-node.yml | 0 {tests => vagrant}/test.yml | 0 21 files changed, 215 insertions(+), 14 deletions(-) create mode 100644 .travis.yml create mode 100644 .yamllint create mode 100644 molecule/default/Dockerfile.j2 create mode 100644 molecule/default/INSTALL.rst create mode 100644 molecule/default/molecule.yml create mode 100644 molecule/default/playbook-docker.yml create mode 100644 molecule/default/playbook.yml create mode 100644 molecule/default/tests/test_default.py create mode 100644 molecule/default/tests/test_default.pyc rename {tests => vagrant}/Vagrantfile (100%) rename {tests => vagrant}/ansible.cfg (100%) rename {tests => vagrant}/inventory.yml (99%) rename {tests => vagrant}/test-single-node.yml (100%) rename {tests => vagrant}/test.yml (100%) diff --git a/.gitignore b/.gitignore index e5ff4e8..c833899 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ VAULT_PASSWORD VAULT_PASS .vault_pass .vault_pass.asc -tests/fetch -tests/ubuntu-*.log +vagramt/fetch +vagrant/ubuntu-*.log +__pycache__ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8eb0096 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,39 @@ +--- + +# Thanks to geerlingguy! + +language: python +services: docker + +env: + global: + - ROLE_NAME: k3s + matrix: + - MOLECULE_DISTRO: centos8 + - MOLECULE_DISTRO: centos7 + - MOLECULE_DISTRO: ubuntu1804 + - MOLECULE_DISTRO: debian10 + - MOLECULE_DISTRO: fedora29 + - MOLECULE_DISTRO: fedora30 + - MOLECULE_DISTRO: amazonlinux2 + + # Test other role features. + - MOLECULE_DISTRO: centos7 + MOLECULE_PLAYBOOK: playbook-docker.yml + +install: + # Install test dependencies. + - pip install molecule docker jmespath + +before_script: + # Use actual Ansible Galaxy role name for the project directory. + - cd ../ + - mv ansible-role-$ROLE_NAME xanmanning.$ROLE_NAME + - cd xanmanning.$ROLE_NAME + +script: + # Run tests. + - molecule test + +notifications: + webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..ad0be76 --- /dev/null +++ b/.yamllint @@ -0,0 +1,11 @@ +extends: default + +rules: + braces: + max-spaces-inside: 1 + level: error + brackets: + max-spaces-inside: 1 + level: error + line-length: disable + truthy: disable diff --git a/README.md b/README.md index 1ccedc8..ff637ae 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ Ansible role for installing [Racher Labs k3s](https://k3s.io/) ("Lightweight Kubernetes") as either a standalone server or cluster. +[![Build Status](https://www.travis-ci.org/PyratLabs/ansible-role-k3s.svg?branch=master)](https://www.travis-ci.org/PyratLabs/ansible-role-k3s) + ## Requirements This role has been tested on Ansible 2.6.0+ against the following Linux Distributions: @@ -12,6 +14,7 @@ This role has been tested on Ansible 2.6.0+ against the following Linux Distribu - Debian 10 - openSUSE Leap 15 - Ubuntu 18.04 LTS + - Amazon Linux 2 ## Disclaimer diff --git a/handlers/main.yml b/handlers/main.yml index d7c04a2..69adce3 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,18 +1,20 @@ --- -- name: reload systemctl - command: systemctl daemon-reload - args: - warn: false +- name: reload systemd + systemd: + daemon_reload: true + # when: molecule_is_test is not defined - name: restart k3s service: name: k3s state: restarted enabled: true + # when: molecule_is_test is not defined - name: restart docker service: name: docker state: restarted enabled: true + # when: molecule_is_test is not defined diff --git a/meta/main.yml b/meta/main.yml index 415a4da..8ddd98b 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,6 +1,7 @@ --- galaxy_info: + role_name: k3s author: Xan Manning description: Ansible role for installing k3s as either a standalone server or cluster company: Pyrat Ltd. @@ -38,6 +39,11 @@ galaxy_info: - name: EL versions: - 7 + - name: Amazon + - name: Fedora + versions: + - 29 + - 30 - name: Debian versions: - buster diff --git a/molecule/default/Dockerfile.j2 b/molecule/default/Dockerfile.j2 new file mode 100644 index 0000000..1aeeaa5 --- /dev/null +++ b/molecule/default/Dockerfile.j2 @@ -0,0 +1,26 @@ +# Molecule managed + +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python systemd sudo bash ca-certificates && apt-get clean; \ + elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python systemd sudo python-devel python*-dnf bash && dnf clean all; \ + elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python systemd sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ + elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python systemd sudo bash python-xml && zypper clean -a; \ + elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo systemd bash ca-certificates; \ + elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python systemd sudo bash ca-certificates && xbps-remove -O; fi + +RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \ + rm -f /lib/systemd/system/multi-user.target.wants/*; \ + rm -f /etc/systemd/system/*.wants/*; \ + rm -f /lib/systemd/system/local-fs.target.wants/*; \ + rm -f /lib/systemd/system/sockets.target.wants/*udev*; \ + rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \ + rm -f /lib/systemd/system/basic.target.wants/*; \ + rm -f /lib/systemd/system/anaconda.target.wants/*; + +VOLUME [“/sys/fs/cgroup”] +CMD [“/usr/sbin/init”] diff --git a/molecule/default/INSTALL.rst b/molecule/default/INSTALL.rst new file mode 100644 index 0000000..6a44bde --- /dev/null +++ b/molecule/default/INSTALL.rst @@ -0,0 +1,22 @@ +******* +Docker driver installation guide +******* + +Requirements +============ + +* Docker Engine + +Install +======= + +Please refer to the `Virtual environment`_ documentation for installation best +practices. If not using a virtual environment, please consider passing the +widely recommended `'--user' flag`_ when invoking ``pip``. + +.. _Virtual environment: https://virtualenv.pypa.io/en/latest/ +.. _'--user' flag: https://packaging.python.org/tutorials/installing-packages/#installing-to-the-user-site + +.. code-block:: bash + + $ pip install 'molecule[docker]' diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..7df57f1 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,40 @@ +--- + +dependency: + name: galaxy +driver: + name: docker +lint: + name: yamllint +platforms: + - name: node1 + image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos8}-ansible:latest" + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + privileged: true + pre_build_image: true + - name: node2 + image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos8}-ansible:latest" + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + privileged: true + pre_build_image: true + - name: node3 + image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos8}-ansible:latest" + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + privileged: true + pre_build_image: true +provisioner: + name: ansible + lint: + name: ansible-lint + playbooks: + converge: ${MOLECULE_PLAYBOOK:-playbook.yml} +verifier: + name: testinfra + lint: + name: flake8 diff --git a/molecule/default/playbook-docker.yml b/molecule/default/playbook-docker.yml new file mode 100644 index 0000000..412eddc --- /dev/null +++ b/molecule/default/playbook-docker.yml @@ -0,0 +1,9 @@ +--- +- name: Converge + hosts: all + become: true + vars: + molecule_is_test: true + k3s_ensure_docker_installed: true + roles: + - role: xanmanning.k3s diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml new file mode 100644 index 0000000..394481e --- /dev/null +++ b/molecule/default/playbook.yml @@ -0,0 +1,8 @@ +--- +- name: Converge + hosts: all + become: true + vars: + molecule_is_test: true + roles: + - role: xanmanning.k3s diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py new file mode 100644 index 0000000..eedd64a --- /dev/null +++ b/molecule/default/tests/test_default.py @@ -0,0 +1,14 @@ +import os + +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_hosts_file(host): + f = host.file('/etc/hosts') + + assert f.exists + assert f.user == 'root' + assert f.group == 'root' diff --git a/molecule/default/tests/test_default.pyc b/molecule/default/tests/test_default.pyc new file mode 100644 index 0000000000000000000000000000000000000000..28c2e2d42688dc5ec97e4aa0c0223353ad18569d GIT binary patch literal 1025 zcmdr~&2H2%5FRJnUs3jiP%k~_(#@gbh7eViszu9I+5!?Qp(st9)>*F|*^?DjyBFZ$ z;3aqo9-tjNoBqB5`5BMDjK7)rCgH!W&A-3HZ#gWlkk zB^iA>8GlU=kB14m^qG`7qcvE*2ejHDnQKbrE20Do2G^92f!zTN;77X+T_Mu|?A`;g z*MUb%klH$pT&9uTBpY+g;|p!kVvn{)YxL#%T`45TRf73<;UZp=yKgNwD6~q9(Z+{0 z4sku68{O2AE8XLc7F{!CL!rdcZIS@9q&%%g_ysIXIk>Sk^J7twVn#X z5V?tM&Q)fFj_2HnQV@dNqEMyD;;XBNp@WVoErj7YYIC_*JdNq-QkBj4^Uqz9M-(n$ zTJSP!Bo69J>{B-$^Ae^`unmIjIvHU7pwC{h$mvXD0w6thoZnSFXi!Ky$dnallBcFo zij;H&cOhO^ALiA661qj9`|as*dw7iyuI9pM<%!NYrtNsUQoUX+Hg=WW&*py|kQ>C? UnJ(zBdgp#cKbaQ+-E2F&1#loHZU6uP literal 0 HcmV?d00001 diff --git a/tasks/configure-k3s-cluster.yml b/tasks/configure-k3s-cluster.yml index 1ef930c..4732432 100644 --- a/tasks/configure-k3s-cluster.yml +++ b/tasks/configure-k3s-cluster.yml @@ -3,7 +3,12 @@ - name: Ensure ansible_host is mapped to inventory_hostname lineinfile: path: /tmp/inventory.txt - line: "{{ item }}@@@{{ hostvars[item].ansible_host }}@@@{{ hostvars[item].k3s_control_node }}" + line: >- + {{ item }} + @@@ + {{ hostvars[item].ansible_host | default(hostvars[item].ansible_fqdn) }} + @@@ + {{ hostvars[item].k3s_control_node }} create: true loop: "{{ play_hosts }}" @@ -14,11 +19,11 @@ - name: Ensure control node is delegated to for obtaining a token set_fact: - k3s_control_delegate: "{{ k3s_control_delegate_raw.stdout.split('@@@')[0] }}" + k3s_control_delegate: "{{ k3s_control_delegate_raw.stdout.split(' @@@ ')[0] }}" - name: Ensure the control node address is registered in Ansible set_fact: - k3s_control_node_address: "{{ hostvars[k3s_control_delegate].ansible_host }}" + k3s_control_node_address: "{{ hostvars[k3s_control_delegate].ansible_host | default(hostvars[k3s_control_delegate].ansible_fqdn) }}" - name: Ensure NODE_TOKEN is captured from control node slurp: @@ -31,16 +36,26 @@ src: k3s.service.j2 dest: /etc/systemd/system/k3s.service notify: - - reload systemctl + - reload systemd - restart k3s - meta: flush_handlers +- name: Wait for control plane to be ready to accept connections + wait_for: + port: 6443 + delay: 5 + sleep: 5 + timeout: 300 + when: k3s_control_node + - name: Wait for all nodes to be ready command: "{{ k3s_install_dir }}/kubectl get nodes" changed_when: false + failed_when: false register: kubectl_get_nodes_result - until: kubectl_get_nodes_result.stdout.find("NotReady") == -1 + until: kubectl_get_nodes_result.rc == 0 + and kubectl_get_nodes_result.stdout.find("NotReady") == -1 retries: 30 delay: 20 when: k3s_control_node diff --git a/tasks/install-k3s.yml b/tasks/install-k3s.yml index 5ed0931..9455e8d 100644 --- a/tasks/install-k3s.yml +++ b/tasks/install-k3s.yml @@ -6,7 +6,7 @@ dest: /etc/systemd/system/k3s.service when: k3s_control_node notify: - - reload systemctl + - reload systemd - meta: flush_handlers @@ -27,4 +27,4 @@ name: k3s state: started enabled: true - when: k3s_control_node + when: k3s_control_node # and molecule_is_test is not defined diff --git a/templates/k3s.service.j2 b/templates/k3s.service.j2 index 049014b..137b0fc 100644 --- a/templates/k3s.service.j2 +++ b/templates/k3s.service.j2 @@ -4,7 +4,7 @@ Documentation=https://k3s.io After=network.target [Service] -Type=notify +Type={{ 'notify' if k3s_control_node else 'exec' }} ExecStartPre=-/sbin/modprobe br_netfilter ExecStartPre=-/sbin/modprobe overlay {% if k3s_control_node %} @@ -18,6 +18,9 @@ LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity TasksMax=infinity +TimeoutStartSec=0 +Restart=always +RestartSec=5s [Install] WantedBy=multi-user.target diff --git a/tests/Vagrantfile b/vagrant/Vagrantfile similarity index 100% rename from tests/Vagrantfile rename to vagrant/Vagrantfile diff --git a/tests/ansible.cfg b/vagrant/ansible.cfg similarity index 100% rename from tests/ansible.cfg rename to vagrant/ansible.cfg diff --git a/tests/inventory.yml b/vagrant/inventory.yml similarity index 99% rename from tests/inventory.yml rename to vagrant/inventory.yml index 30ca4f1..cfe98f7 100644 --- a/tests/inventory.yml +++ b/vagrant/inventory.yml @@ -1,3 +1,5 @@ +--- + all: vars: ansible_become: true diff --git a/tests/test-single-node.yml b/vagrant/test-single-node.yml similarity index 100% rename from tests/test-single-node.yml rename to vagrant/test-single-node.yml diff --git a/tests/test.yml b/vagrant/test.yml similarity index 100% rename from tests/test.yml rename to vagrant/test.yml