From 41cb71e364a18b2667b91f9438408d707ec98a7c Mon Sep 17 00:00:00 2001 From: NaeiKinDus Date: Sun, 7 Jul 2024 00:00:00 +0000 Subject: [PATCH] feat(role): add kubectl role --- .../infrastructure/roles/kubectl/README.md | 29 ++++++++++++ .../roles/kubectl/defaults/main.yml | 3 ++ .../roles/kubectl/handlers/main.yml | 2 + .../roles/kubectl/meta/main.yml | 15 ++++++ .../roles/kubectl/tasks/main.yml | 47 +++++++++++++++++++ .../roles/kubectl/tests/inventory | 2 + .../roles/kubectl/tests/test.yml | 5 ++ .../roles/kubectl/vars/main.yml | 2 + 8 files changed, 105 insertions(+) create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/kubectl/README.md create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/kubectl/defaults/main.yml create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/kubectl/handlers/main.yml create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/kubectl/meta/main.yml create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/kubectl/tasks/main.yml create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/kubectl/tests/inventory create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/kubectl/tests/test.yml create mode 100644 collections/ansible_collections/nullified/infrastructure/roles/kubectl/vars/main.yml diff --git a/collections/ansible_collections/nullified/infrastructure/roles/kubectl/README.md b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/README.md new file mode 100644 index 0000000..5190d65 --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/README.md @@ -0,0 +1,29 @@ +Kubectl +========= + +This role handles the installation of the kubectl binary and optionally supports version pining or unconditional upgrade. + +Requirements +------------ + +Only tested and used on Debian (uses the `ansible.builtin.apt` module). + +Role Variables +-------------- +### `kubectl_binary_path` +Path where the kubectl binary is installed (no discovery is performed so pre-existing binaries will remain untouched). + +**Default value**: `kubectl_binary_path: /usr/local/bin/kubectl` + +### `kubectl_install_version` +Version of kubectl to install. It allows multiple values: +- empty: will install the binary if not already present, else nothing is changed, +- 'vX.Y.Z': will install the specified version (e.g. `v3.15.2`), upgrading / downgrading the local binary if required, +- latest: will install the latest available version unless the local binary is already up-to-date. + +**Default value**: `kubectl_install_version: latest` + +License +------- + +MIT diff --git a/collections/ansible_collections/nullified/infrastructure/roles/kubectl/defaults/main.yml b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/defaults/main.yml new file mode 100644 index 0000000..521b8d0 --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/defaults/main.yml @@ -0,0 +1,3 @@ +--- +kubectl_binary_path: '/usr/local/bin/kubectl' +kubectl_install_version: latest diff --git a/collections/ansible_collections/nullified/infrastructure/roles/kubectl/handlers/main.yml b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/handlers/main.yml new file mode 100644 index 0000000..a524cd9 --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for kubectl diff --git a/collections/ansible_collections/nullified/infrastructure/roles/kubectl/meta/main.yml b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/meta/main.yml new file mode 100644 index 0000000..772ed81 --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/meta/main.yml @@ -0,0 +1,15 @@ +galaxy_info: + author: Florian L. + description: Install kubectl binary + issue_tracker_url: https://gitlab.0x2a.ninja/infrastructure/configuration + license: MIT + min_ansible_version: 2.15 + platforms: + - name: Debian + versions: + - 11 + - 12 + galaxy_tags: + - kubectl + - kubernetes +dependencies: [] diff --git a/collections/ansible_collections/nullified/infrastructure/roles/kubectl/tasks/main.yml b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/tasks/main.yml new file mode 100644 index 0000000..331deec --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/tasks/main.yml @@ -0,0 +1,47 @@ +--- +- name: install required packages + become: true + ansible.builtin.apt: + update_cache: true + force_apt_get: true + cache_valid_time: 3600 + pkg: + - curl + - jq + +- name: find if binary is already installed + ansible.builtin.file: + path: '{{ kubectl_binary_path }}' + register: kubectl_stat + changed_when: false + failed_when: false + +- name: find current installed version + when: kubectl_stat.state != "absent" + ansible.builtin.shell: "{{ kubectl_binary_path }} version --client=true --output=json | jq --raw-output '.clientVersion.gitVersion'" + changed_when: false + register: kubectl_local_version_exec + +- name: find latest available version + connection: local + ansible.builtin.command: curl -L -s https://dl.k8s.io/release/stable.txt + register: latest_kubectl_version_exec + when: kubectl_install_version is falsy or kubectl_install_version == "latest" + changed_when: false + +- name: set kubectl facts + ansible.builtin.set_fact: + kubectl_latest_version_available: '{{ latest_kubectl_version_exec.get("stdout", "") if latest_kubectl_version_exec is defined }}' + kubectl_local_version: '{{ kubectl_local_version_exec.get("stdout", "") if kubectl_local_version_exec is defined }}' + kubectl_target_install_version: '{{ kubectl_install_version if kubectl_install_version != "latest" else latest_kubectl_version_exec.get("stdout", "") }}' + +- name: install binary + become: true + when: kubectl_stat.state == "absent" or (kubectl_local_version != kubectl_target_install_version and kubectl_install_version is not falsy) + ansible.builtin.get_url: + url: "https://dl.k8s.io/release/{{ kubectl_target_install_version }}/bin/linux/amd64/kubectl" + dest: /usr/local/bin/kubectl + owner: root + group: root + mode: '0755' + force: true diff --git a/collections/ansible_collections/nullified/infrastructure/roles/kubectl/tests/inventory b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/collections/ansible_collections/nullified/infrastructure/roles/kubectl/tests/test.yml b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/tests/test.yml new file mode 100644 index 0000000..7709e70 --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - kubectl diff --git a/collections/ansible_collections/nullified/infrastructure/roles/kubectl/vars/main.yml b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/vars/main.yml new file mode 100644 index 0000000..292e8d9 --- /dev/null +++ b/collections/ansible_collections/nullified/infrastructure/roles/kubectl/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for kubectl