-
Notifications
You must be signed in to change notification settings - Fork 832
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
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: alankan-finocomp <[email protected]>
The intention is that if supply a dedicated kubeconfig location for the k3s-ansible controlled clusters, we don't do any merging or overriding. |
There was a problem hiding this 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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when: kubeconfig != "~/.kube/config" | |
when: kubeconfig == "~/.kube/config.new" |
@@ -151,17 +151,17 @@ | |||
when: kubeconfig != "~/.kube/config" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when: kubeconfig != "~/.kube/config" | |
when: kubeconfig == "~/.kube/config.new" |
There was a problem hiding this 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 }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mv ${TFILE} {{ kubeconfig }} | |
mv ${TFILE} ~/.kube/config |
There is also the block which can cause a full overwrite. |
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 thecurrent-context
in the kubeconfig file.Linked Issues
#350