Skip to content

Commit

Permalink
fix join process to k8s cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
dean-taylor committed May 6, 2021
1 parent 6ed5057 commit 1ef1274
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
20 changes: 20 additions & 0 deletions files/KUBECONFIG.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tasks/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 5 additions & 17 deletions tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
classic: yes
name: microk8s
state: present
register: microk8s_install

- name: group membership
ansible.builtin.user:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1ef1274

Please sign in to comment.