Skip to content

Commit

Permalink
Support user defined kubeconfig, fix merging context (#266)
Browse files Browse the repository at this point in the history
* Support user defined kubeconfig, fix merging context

Signed-off-by: Derek Nola <[email protected]>
  • Loading branch information
dereknola authored Dec 6, 2023
1 parent 4d6e602 commit 9998f50
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,16 @@ It is assumed that the control node has access to the internet. The playbook wil

## Kubeconfig

After successful bringup, the kubeconfig of the cluster is copied to the control node and set as default (`~/.kube/config`).
Assuming you have [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl) installed, you to confirm access to your **Kubernetes** cluster use the following:
After successful bringup, the kubeconfig of the cluster is copied to the control node and merged with `~/.kube/config` under the `k3s-ansible` context.
Assuming you have [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl) installed, you can confirm access to your **Kubernetes** cluster with the following:

```bash
kubectl config use-context k3s-ansible
kubectl get nodes
```

If you wish for your kubeconfig to be copied elsewhere and not merged, you can set the `kubeconfig` variable in `inventory.yml` to the desired path.

## Local Testing

A Vagrantfile is provided that provision a 5 nodes cluster using Vagrant (LibVirt or Virtualbox as provider). To use it:
Expand Down
1 change: 1 addition & 0 deletions roles/k3s_server/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
k3s_server_location: "/var/lib/rancher/k3s"
systemd_dir: "/etc/systemd/system"

Check warning on line 3 in roles/k3s_server/defaults/main.yml

View workflow job for this annotation

GitHub Actions / Lint

var-naming[no-role-prefix]

Variables names from within roles should use k3s_server_ as a prefix. (vars: systemd_dir)
api_port: 6443

Check warning on line 4 in roles/k3s_server/defaults/main.yml

View workflow job for this annotation

GitHub Actions / Lint

var-naming[no-role-prefix]

Variables names from within roles should use k3s_server_ as a prefix. (vars: api_port)
kubeconfig: ~/.kube/config.new

Check warning on line 5 in roles/k3s_server/defaults/main.yml

View workflow job for this annotation

GitHub Actions / Lint

var-naming[no-role-prefix]

Variables names from within roles should use k3s_server_ as a prefix. (vars: kubeconfig)
24 changes: 22 additions & 2 deletions roles/k3s_server/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,33 @@
- name: Copy kubectl config to local machine
ansible.builtin.fetch:
src: ~{{ ansible_user }}/.kube/config
dest: ~/.kube/config.new
dest: "{{ kubeconfig }}"
flat: true

- name: Check whether kubectl is installed on control node
ansible.builtin.command: 'kubectl'
register: kubectl_installed
ignore_errors: yes

Check failure on line 100 in roles/k3s_server/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

yaml[truthy]

Truthy value should be one of \[false, true]

Check warning on line 100 in roles/k3s_server/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

100:22 [truthy] truthy value should be one of [false, true]
delegate_to: 127.0.0.1
become: false
changed_when: false

- name: Setup kubeconfig k3s-ansible context
when: kubeconfig == "~/.kube/config.new" && kubectl_installed.rc == 0
ansible.builtin.replace:
path: "{{ kubeconfig }}"
regexp: 'name: default'
replace: 'name: k3s-ansible'
delegate_to: 127.0.0.1
become: false

- name: Merge with any existing kube config
when: kubeconfig == "~/.kube/config.new" && kubectl_installed.rc == 0
ansible.builtin.shell: |
TFILE=$(mktemp)
KUBECONFIG=~/.kube/config:~/.kube/config.new kubectl config view --flatten > ${TFILE}
KUBECONFIG=~/.kube/config.new kubectl rename-context default k3s-ansible
KUBECONFIG=~/.kube/config.new kubectl config set-context k3s-ansible --user=k3s-ansible --cluster=k3s-ansible
KUBECONFIG=~/.kube/config.new:~/.kube/config kubectl config view --flatten > ${TFILE}
mv ${TFILE} ~/.kube/config
rm ~/.kube/config.new
delegate_to: 127.0.0.1
Expand Down

0 comments on commit 9998f50

Please sign in to comment.