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

Fix kubeconfig merge with existing config #351

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alankan-finocomp
Copy link

Changes

Since the existing config file was not in scope, the commands did not perform anything.

To fix it, I include the existing kubeconfig path in KUBECONFIG variable such that the YAML contents are merged when it prints out via kubectl config view --flatten

Also, replacing keyword "default" with ansible variable kube_config can also help replace the current-context in the kubeconfig file.

Linked Issues

#350

@dereknola
Copy link
Member

dereknola commented Jul 26, 2024

This function is not intended to merge with the default kubeconfig. That's why there is an explicit check
This seems to be an error with the if statement, it should be reversed.
https://github.com/k3s-io/k3s-ansible/blob/master/roles/k3s_server/tasks/main.yml#L160

The intention is that if supply a dedicated kubeconfig location for the k3s-ansible controlled clusters, we don't do any merging or overriding.

Copy link
Member

@dereknola dereknola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order for this to not affect user defined kubeconfigs, we need to check and see if the kubeconfig is still equal to our variable default.

regexp: 'name: default'
replace: 'name: {{ cluster_context }}'
regexp: 'default'
replace: '{{ cluster_context }}'
delegate_to: 127.0.0.1
become: false

- name: Merge with any existing kubeconfig on control node
when: kubeconfig != "~/.kube/config"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
when: kubeconfig != "~/.kube/config"
when: kubeconfig == "~/.kube/config.new"

@@ -151,17 +151,17 @@
when: kubeconfig != "~/.kube/config"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
when: kubeconfig != "~/.kube/config"
when: kubeconfig == "~/.kube/config.new"

Copy link
Member

@dereknola dereknola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to override the default kubeconfig at the end

KUBECONFIG={{ kubeconfig }} kubectl config set-context {{ cluster_context }} --user={{ cluster_context }} --cluster={{ cluster_context }}
KUBECONFIG={{ kubeconfig }} kubectl config view --flatten > ${TFILE}
KUBECONFIG={{ kubeconfig }}:~/.kube/config kubectl config set-context {{ cluster_context }} --user={{ cluster_context }} --cluster={{ cluster_context }}
KUBECONFIG={{ kubeconfig }}:~/.kube/config kubectl config view --flatten > ${TFILE}
mv ${TFILE} {{ kubeconfig }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mv ${TFILE} {{ kubeconfig }}
mv ${TFILE} ~/.kube/config

@joaogbcravo
Copy link

There is also the block which can cause a full overwrite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants