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

[FEATURE]: One kubeconfig.yaml for more than one cluster #90

Closed
deminngi opened this issue Jul 9, 2019 · 9 comments
Closed

[FEATURE]: One kubeconfig.yaml for more than one cluster #90

deminngi opened this issue Jul 9, 2019 · 9 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@deminngi
Copy link

deminngi commented Jul 9, 2019

Currently if I create two k3d cluster with different names, k3d creates two kubeconfig.yaml files under two different directories.

I wanna have one kubeconfig.yaml file and use kubectx and kubens to switch between them easily.

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: 
<certificate-cluster-dev>
    server: https://localhost:6551
  name: cluster-dev
- cluster:
    certificate-authority-data: 
<certificate-cluster-prod>
    server: https://localhost:6550
  name: cluster-prod
contexts:
- context:
    cluster: cluster-dev
    namespace: development
    user: user-dev
  name: default-dev
- context:
    cluster: cluster-prod
    namespace: production
    user: user-prod
  name: default-prod
current-context: default-prod
kind: Config
preferences: {}
users:
- name: user-dev
  user:
    password: <user-dev password>
    username: admin
- name: user-prod
  user:
    password: <user-prod password>
    username: admin

Implementation perhaps with:
k3d create -a1 6550 -n1 cluster-prod -w1 5 -a2 6551 -n2 cluster-dev -w2 1 --image rancher/k3s:v0.7.0-rc4

@deminngi deminngi added the enhancement New feature or request label Jul 9, 2019
@iwilltry42
Copy link
Member

iwilltry42 commented Jul 10, 2019

Hey @giminni , thanks for submitting this feature request.
What you describe there is creating two clusters at the same time, which is way more complicated than simply merging the configs (because of re-using the same flag for different references multiple times).
If it's a big benefit, I'd rather suggest implementing something like k3d get-kubeconfig --merge cluster-prod cluster-dev which would grab the kubeconfigs for both clusters, rename the cluster in the config from default to the actual k3d cluster name and then run kubectl config view --merge --flatten to merge them.
But that could also be achieved by a simple shell script for now I guess 😬

@deminngi
Copy link
Author

@iwilltry42 your proposal sounds good

@iwilltry42 iwilltry42 added Hacktoberfest help wanted Extra attention is needed labels Sep 24, 2019
@tanelmae
Copy link

tanelmae commented Nov 1, 2019

If it helps someone here is my shell based workaround: https://gist.github.com/tanelmae/b0dc522ab3984f290b0b25766128f61a

k3ctx <cluster>
or

k3ctx
<pick cluster from what it can find>

@kristoferlundgren
Copy link

export KUBECONFIG=file1:file2:file3

... already merges config files.
https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#set-the-kubeconfig-environment-variable

Thus this one-liner would have worked with kubectx:

export KUBECONFIG=${HOME}/.kube/config:$(k3d get-kubeconfig --all | tr '\n' ':')

(ie, the default config, plus all k3d generated ones, colon separated)

... if k3d did not re-use the name "default" in the config files, regardless of the configured cluster name.
Therefore, "kubectx", when using the one-liner above, sees all k3d clusters as one "default".

I'm quite new to Kubernetes, but my understanding is that if k3d changed the generated config-files from using "default", to the asked-for cluster name. It would perhaps solve the issue and the one-liner above would work for kubectx?

@iwilltry42
Copy link
Member

@kristoferlundgren , changing the name from default to the actual k3d cluster name in the config generated by k3s would probably be the best and easiest solution for this combined with your one-liner.
I think it would make working with multiple k3d clusters easier in general.

It's on the list already 👍

@kristoferlundgren
Copy link

@iwilltry42 , Thanks for acknowledging that my idea is feasible!
Do you have a reference to "the list"?
Timeplan / rough estimate?

@iwilltry42
Copy link
Member

iwilltry42 commented Nov 11, 2019

@kristoferlundgren , I just pushed the change to master (469b56c) and so it will land in the next release 👍
EDIT: the change here is very simple and simply replaces every occurrence of 'default' in the kubeconfig with the cluster name. So it also replaces the username, etc. This can be improved later on by parsing the kubeconfig file completely and modifying it as a yaml struct.

@kristoferlundgren
Copy link

@iwilltry42 Hero of the day!

@iwilltry42
Copy link
Member

Landed in k3d v3 (see e.g. #226)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants