Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support user defined kubeconfig, fix merging context #266

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,4 +1,5 @@
---
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
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
Loading