diff --git a/.travis.yml b/.travis.yml index e9e9f35..a8faab0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,7 @@ env: - MOLECULE_SCENARIO: default matrix: - MOLECULE_DISTRO: geerlingguy/docker-debian10-ansible:latest + - MOLECULE_DISTRO: geerlingguy/docker-ubuntu2004-ansible:latest # Test installing docker - MOLECULE_DISTRO: geerlingguy/docker-centos7-ansible:latest diff --git a/README.md b/README.md index 1c353ba..b6c2e31 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ consistency. | Variable | Description | Default Value | |------------------------------------------|-------------------------------------------------------------------------------------|--------------------------------------------| -| `k3s_cluster_state` | State of cluster: installed, started, stopped, restarted, downloaded, uninstalled. | installed | +| `k3s_state` | State of k3s: installed, started, stopped, restarted, downloaded, uninstalled, validated. | installed | | `k3s_release_version` | Use a specific version of k3s, eg. `v0.2.0`. Specify `false` for stable. | `false` | | `k3s_build_cluster` | When multiple `play_hosts` are available, attempt to cluster. Read notes below. | `true` | | `k3s_github_url` | Set the GitHub URL to install k3s from. | https://github.com/rancher/k3s | diff --git a/defaults/main.yml b/defaults/main.yml index 640696a..0029749 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,8 +1,8 @@ --- -# k3s cluster state, options: installed, started, stopped, restarted +# k3s state, options: installed, started, stopped, restarted, validated # (default: installed) -k3s_cluster_state: installed +k3s_state: installed # Use a specific k3s version, if set to "false" we will get the latest # k3s_release_version: v0.1.0 diff --git a/molecule/autodeploy/converge.yml b/molecule/autodeploy/converge.yml index 105d48b..a03f036 100644 --- a/molecule/autodeploy/converge.yml +++ b/molecule/autodeploy/converge.yml @@ -6,6 +6,6 @@ molecule_is_test: true k3s_build_cluster: false k3s_server_manifests_templates: - - "molecule/highavailability/templates/00-ns-monitoring.yml.j2" + - "molecule/autodeploy/templates/00-ns-monitoring.yml.j2" roles: - role: xanmanning.k3s diff --git a/molecule/default/templates/00-ns-monitoring.yml.j2 b/molecule/autodeploy/templates/00-ns-monitoring.yml.j2 similarity index 100% rename from molecule/default/templates/00-ns-monitoring.yml.j2 rename to molecule/autodeploy/templates/00-ns-monitoring.yml.j2 diff --git a/molecule/docker/converge.yml b/molecule/docker/converge.yml index a88a3c4..3700ddc 100644 --- a/molecule/docker/converge.yml +++ b/molecule/docker/converge.yml @@ -6,7 +6,6 @@ molecule_is_test: true k3s_use_docker: true k3s_https_port: 26443 - k3s_flannel_backend: wireguard k3s_cluster_domain: examplecluster.local roles: - role: xanmanning.k3s diff --git a/molecule/docker/molecule.yml b/molecule/docker/molecule.yml index af6a516..85427b3 100644 --- a/molecule/docker/molecule.yml +++ b/molecule/docker/molecule.yml @@ -10,7 +10,7 @@ lint: | ansible-lint platforms: - name: node1 - image: "${MOLECULE_DISTRO:-geerlingguy/docker-centos8-ansible:latest}" + image: "${MOLECULE_DISTRO:-geerlingguy/docker-ubuntu2004-ansible:latest}" command: ${MOLECULE_DOCKER_COMMAND:-""} volumes: - /sys/fs/cgroup:/sys/fs/cgroup:ro @@ -19,7 +19,7 @@ platforms: networks: - name: k3snet - name: node2 - image: "${MOLECULE_DISTRO:-geerlingguy/docker-centos8-ansible:latest}" + image: "${MOLECULE_DISTRO:-geerlingguy/docker-ubuntu2004-ansible:latest}" command: ${MOLECULE_DOCKER_COMMAND:-""} volumes: - /sys/fs/cgroup:/sys/fs/cgroup:ro @@ -28,7 +28,7 @@ platforms: networks: - name: k3snet - name: node3 - image: "${MOLECULE_DISTRO:-geerlingguy/docker-centos8-ansible:latest}" + image: "${MOLECULE_DISTRO:-geerlingguy/docker-ubuntu2004-ansible:latest}" command: ${MOLECULE_DOCKER_COMMAND:-""} volumes: - /sys/fs/cgroup:/sys/fs/cgroup:ro diff --git a/molecule/highavailabilitydb/templates/00-ns-monitoring.yml.j2 b/molecule/highavailabilitydb/templates/00-ns-monitoring.yml.j2 deleted file mode 100644 index d325236..0000000 --- a/molecule/highavailabilitydb/templates/00-ns-monitoring.yml.j2 +++ /dev/null @@ -1,4 +0,0 @@ -apiVersion: v1 -kind: Namespace -metadata: - name: monitoring diff --git a/tasks/build/configure-k3s-cluster.yml b/tasks/build/configure-k3s-cluster.yml index dfa0019..175a69c 100644 --- a/tasks/build/configure-k3s-cluster.yml +++ b/tasks/build/configure-k3s-cluster.yml @@ -59,25 +59,10 @@ when: k3s_control_node and not k3s_primary_control_node become: "{{ k3s_become_for_systemd | ternary(true, false, k3s_become_for_all) }}" -- name: Wait for control plane to be ready to accept connections - wait_for: - port: "{{ k3s_https_port }}" - host: "{{ k3s_bind_address | default('127.0.0.1') }}" - delay: 5 - sleep: 5 - timeout: 300 - when: k3s_control_node +- import_tasks: ../validate/check-control-plane.yml + when: not k3s_skip_validation - meta: flush_handlers -- 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.rc == 0 - and kubectl_get_nodes_result.stdout.find("NotReady") == -1 - retries: 30 - delay: 20 - when: k3s_control_node and not k3s_no_flannel and not ansible_check_mode - become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}" +- import_tasks: ../validate/check-cluster-nodes-ready.yml + when: not k3s_skip_validation diff --git a/tasks/main.yml b/tasks/main.yml index 10646b3..6ea8c4e 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,11 +1,11 @@ --- -- name: Check to see if k3s_cluster_state is a supported value +- name: Check to see if k3s_state is a supported value assert: that: - - k3s_cluster_state in ['installed', 'started', 'stopped', 'restarted', 'downloaded', 'uninstalled'] - fail_msg: "k3s_cluster_state not valid. Check README.md for details." - success_msg: "k3s_cluster_state is valid." - when: k3s_cluster_state is defined + - k3s_state in k3s_valid_states + fail_msg: "k3s_state not valid. Check README.md for details." + success_msg: "k3s_state is valid." + when: k3s_state is defined -- include_tasks: state-{{ (k3s_cluster_state | lower) | default('installed') }}.yml +- include_tasks: state-{{ (k3s_state | lower) | default('installed') }}.yml diff --git a/tasks/state-validated.yml b/tasks/state-validated.yml new file mode 100644 index 0000000..9cfe1d1 --- /dev/null +++ b/tasks/state-validated.yml @@ -0,0 +1,7 @@ +--- + +- import_tasks: validate/check-environment.yml + +- import_tasks: validate/main.yml + +- import_tasks: validate/post-install.yml diff --git a/tasks/validate/check-cluster-nodes-ready.yml b/tasks/validate/check-cluster-nodes-ready.yml new file mode 100644 index 0000000..5c81fd2 --- /dev/null +++ b/tasks/validate/check-cluster-nodes-ready.yml @@ -0,0 +1,14 @@ +--- + +- name: Check that all nodes to be ready + command: "{{ k3s_install_dir }}/kubectl get nodes" + changed_when: false + failed_when: kubectl_get_nodes_result.stdout.find("was refused") != -1 or + kubectl_get_nodes_result.stdout.find("ServiceUnavailable") != -1 + register: kubectl_get_nodes_result + until: kubectl_get_nodes_result.rc == 0 + and kubectl_get_nodes_result.stdout.find("NotReady") == -1 + retries: 30 + delay: 20 + when: k3s_control_node and not k3s_no_flannel and not ansible_check_mode + become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}" diff --git a/tasks/validate/check-control-plane.yml b/tasks/validate/check-control-plane.yml new file mode 100644 index 0000000..14e3260 --- /dev/null +++ b/tasks/validate/check-control-plane.yml @@ -0,0 +1,10 @@ +--- + +- name: Check that the control plane to is available to accept connections + wait_for: + port: "{{ k3s_https_port }}" + host: "{{ k3s_bind_address | default('127.0.0.1') }}" + delay: 5 + sleep: 5 + timeout: 300 + when: k3s_control_node diff --git a/tasks/validate/post-install.yml b/tasks/validate/post-install.yml new file mode 100644 index 0000000..728e247 --- /dev/null +++ b/tasks/validate/post-install.yml @@ -0,0 +1,4 @@ +--- + +- import_tasks: check-control-plane.yml +- import_tasks: check-clustr-nodes-ready.yml diff --git a/vars/main.yml b/vars/main.yml index df57d16..8da9a4e 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,5 +1,16 @@ --- +# Valid states for this role +k3s_valid_states: + - installed + - started + - stopped + - restarted + - downloaded + - uninstalled + - validated + +# Map ansible fact gathering architecture to a release name and suffix in github. k3s_arch_lookup: amd64: arch: amd64 @@ -26,13 +37,27 @@ k3s_arch_lookup: arch: arm suffix: "-armhf" +# Always default to stable channel, this will change with k3s_release_version k3s_release_channel: stable + +# Create the API address for the k3s_github_url that has been specified k3s_github_api: "{{ k3s_github_url | replace('github.com', 'api.github.com') }}" +# Create the latest release API link for github. Possibly deprecated with k3s.io updates API k3s_github_api_releases: "{{ k3s_github_api | replace('.com', '.com/repos') }}/releases/latest" +# K3s updates API k3s_api_releases: https://update.k3s.io/v1-release/channels +# Download location for releases k3s_github_download_url: "{{ k3s_github_url }}/releases/download" + +# Empty array for counting the number of control plane nodes k3s_controller_count: [] +# Default to the "system" systemd context, this will be "user" when running rootless k3s_systemd_context: system + +# Directory for systemd unit files to be installed. As this role doesn't use package +# management, this should live in /etc/systemd, not /lib/systemd k3s_systemd_unit_directory: "/etc/systemd/{{ k3s_systemd_context }}" + +# Directory for gathering the k3s token for clustering. I don't see this changing. k3s_token_location: "/etc/rancher"