From 1ef12747aff32c8841a74c350e85378dcfbfd9fe Mon Sep 17 00:00:00 2001 From: Dean Taylor Date: Fri, 7 May 2021 07:11:59 +0800 Subject: [PATCH] fix join process to k8s cluster --- files/KUBECONFIG.sh | 20 ++++++++++++++++++++ tasks/config.yml | 2 +- tasks/install.yml | 22 +++++----------------- tasks/main.yml | 8 ++++++-- 4 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 files/KUBECONFIG.sh diff --git a/files/KUBECONFIG.sh b/files/KUBECONFIG.sh new file mode 100644 index 0000000..e0fc9ed --- /dev/null +++ b/files/KUBECONFIG.sh @@ -0,0 +1,20 @@ +DIR="${HOME}/.kube" + +# if kubectl is in PATH / installed +if which kubectl &>/dev/null; then + [ -d "${DIR}" ] || mkdir -p "${DIR}" + + # Configure kubectl for local microk8s + if ! [ -f "${DIR}/microk8s.config" ]; then + # if microk8s is in PATH / installed + if which microk8s &>/dev/null; then + umask 0077 + microk8s config | sed 's/\(user\|name\): admin/\1: microk8s-admin/' > "${DIR}/microk8s.config" + fi + fi + + # setup kubectl + KUBECONFIG="$(find $DIR \( -name 'config' -o -name '*.config' \) \( -type f -o -type l \) -print0 | tr '\0' ':')" + KUBECONFIG="${KUBECONFIG%:}" + export KUBECONFIG +fi diff --git a/tasks/config.yml b/tasks/config.yml index de5f7a7..263aab2 100644 --- a/tasks/config.yml +++ b/tasks/config.yml @@ -4,5 +4,5 @@ with_items: - "{{ microk8s_features }}" when: - - ansible_local.microk8s.addons is defined - (ansible_local.microk8s.addons | selectattr('name','contains', item ) | first).status == "disabled" + register: microk8s_enable diff --git a/tasks/install.yml b/tasks/install.yml index 9c45e1d..7571dd6 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -6,6 +6,7 @@ classic: yes name: microk8s state: present + register: microk8s_install - name: group membership ansible.builtin.user: @@ -18,21 +19,8 @@ - name: KUBECONFIG kubectl setup ansible.builtin.copy: - content: | - DIR="${HOME}/.kube" - if which kubectl &>/dev/null; then - [ -d "${DIR}" ] || mkdir -p "${DIR}" - if ! [ -f "${DIR}/microk8s.config" ]; then - if which microk8s &>/dev/null; then - umask 0077 - microk8s config | sed 's/\(user\|name\): admin/\1: microk8s-admin/' > "${DIR}/microk8s.config" - fi - fi - KUBECONFIG="$(find $DIR \( -name 'config' -o -name '*.config' \) \( -type f -o -type l \) -print0 | tr '\0' ':')" - KUBECONFIG="${KUBECONFIG%:}" - export KUBECONFIG - fi dest: /etc/profile.d/KUBECONFIG.sh + src: files/KUBECONFIG.sh - name: create ansible local facts directory ansible.builtin.file: @@ -47,10 +35,10 @@ register: microk8s_facts - name: wait for microk8s to start - ansible.builtin.shell: - cmd: microk8s status --format yaml --wait-ready --timeout 60 + ansible.builtin.command: + cmd: /snap/bin/microk8s status --format yaml --wait-ready --timeout 60 changed_when: false - register: microk8s_status + when: microk8s_install.changed - name: reload ansible local facts ansible.builtin.setup: diff --git a/tasks/main.yml b/tasks/main.yml index 7236af9..86df2ba 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -7,10 +7,14 @@ ansible.builtin.include_tasks: config.yml when: - inventory_hostname == groups['microk8s'] | first + - ansible_local.microk8s is defined + - ansible_local.microk8s.addons is defined - name: join cluster ansible.builtin.include_tasks: join.yml when: - inventory_hostname != groups['microk8s'] | first - - ansible_local.microk8s - - hostvar['ansible_local']['microk8s']['high-availability'] is not defined + - ansible_local.microk8s is defined + - ansible_local['microk8s']['high-availability'] is defined + - ansible_local['microk8s']['high-availability']['enabled'] == true + - ansible_local['microk8s']['high-availability']['nodes'] | length <= 1