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

Add `kubectl config merge` command #46381

Open
henriquetruta opened this Issue May 24, 2017 · 21 comments

Comments

Projects
None yet
@henriquetruta
Copy link
Contributor

henriquetruta commented May 24, 2017

gcloud/GKE has an efficient way of getting credentials from somewhere by using the get-credentials command that automatically merges the content of a remote cluster in a local kubeconfig file. We want to have something similar in k8s. A first approach would be having two kubeconfig files locally and being able to merge the content of one into the other without needing to do manual copy/paste.

We could have a command that looks like:
kubectl config merge <source> <target>

Where the target file defaults to ~/.kube/config and source would be some kubeconfig file that the user wants to have its entries (users, contexts and clusters) copied to the target file.

If there is no name conflict, i.e., the entries in source don’t exist in target, the output is quite straightforward. However, if there is some conflict, we need to decide on how to approach. Do anything and inform the user? Rename them in target? Copy only those with no conflict? Have this options as parameters (--ignore, --rename, --abort-if-conflict, etc)
I suggest having for now, for the sake of simplicity doing anything and informing the user. The others can me made in follow-up issues

@henriquetruta

This comment has been minimized.

Copy link
Contributor Author

henriquetruta commented May 24, 2017

@nikhiljindal @madhusudancs can you take a look?

@superbrothers

This comment has been minimized.

Copy link
Member

superbrothers commented May 25, 2017

Can kubectl config view meet your requirements?

# use multiple kubeconfig files at the same time and view merged config
$ KUBECONFIG=~/.kube/config:~/.kube/kubconfig2 kubectl config view

https://kubernetes.io/docs/user-guide/kubectl-cheatsheet/

@madhusudancs

This comment has been minimized.

Copy link
Contributor

madhusudancs commented May 25, 2017

kubectl config view might not work as is as it redacts certificate data. kubectl config view --flatten might, but I have never tried that with multiple kubeconfigs.

cc @kubernetes/sig-cli-feature-requests

@shiywang

This comment has been minimized.

Copy link
Member

shiywang commented May 25, 2017

@madhusudancs @superbrothers @henriquetruta
KUBECONFIG=~/.kube/config:~/config kubectl config view --flatten works, but I think adding this kubectl config merge is also ok, I can implement this since I'm working on another kubectl config subcommand
cc @kubernetes/sig-cli-feature-requests

@madhusudancs

This comment has been minimized.

Copy link
Contributor

madhusudancs commented May 25, 2017

@shiywang it is up to SIG-CLI to decide.

@shiywang

This comment has been minimized.

Copy link
Member

shiywang commented May 25, 2017

@madhusudancs sure, we'll wait for @kubernetes/sig-cli-maintainers to decide.

@henriquetruta

This comment has been minimized.

Copy link
Contributor Author

henriquetruta commented May 25, 2017

@shiywang thanks! I've already started to implement it here

@bgrant0607

This comment has been minimized.

Copy link
Member

bgrant0607 commented Jun 21, 2017

See also #9298, #10693, #20605, #30395

@caesarxuchao

This comment has been minimized.

Copy link
Member

caesarxuchao commented Aug 9, 2017

/sub

@fejta-bot

This comment has been minimized.

Copy link

fejta-bot commented Jan 2, 2018

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

Prevent issues from auto-closing with an /lifecycle frozen comment.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or @fejta.
/lifecycle stale

@PaulCharlton

This comment has been minimized.

Copy link

PaulCharlton commented Mar 7, 2018

/remove-lifecycle stale

@epcim

This comment has been minimized.

Copy link

epcim commented Mar 16, 2018

@superbrothers commented on May 25, 2017, 7:27 AM GMT+2:

Can kubectl config view meet your requirements?

# use multiple kubeconfig files at the same time and view merged config
$ KUBECONFIG=~/.kube/config:~/.kube/kubconfig2 kubectl config view

https://kubernetes.io/docs/user-guide/kubectl-cheatsheet/

works, except the fact your yaml config starts with --- 👎

@HighwayofLife

This comment has been minimized.

Copy link

HighwayofLife commented Mar 16, 2018

@epcim

KUBECONFIG=~/.kube/config:~/.kube/kubconfig2 kubectl config view --flatten > ~/.kube/config

Does not have a yaml config starting with ---, however, it still works even when yaml starts with that separator.

@anthonydahanne

This comment has been minimized.

Copy link

anthonydahanne commented Apr 9, 2018

KUBECONFIG=/.kube/config:/.kube/kubconfig2 kubectl config view --flatten > ~/.kube/config

well, that almost worked for me, except that... only partial content was copied ! (writing while reading seemed to have messed up the final output !)
I had better luck with a temporary file :

KUBECONFIG=~/.kube/config:~/.kube/kubconfig2 kubectl config view --flatten > mergedkub && mv mergedkub ~/.kube/config

@fejta-bot

This comment has been minimized.

Copy link

fejta-bot commented Jul 8, 2018

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@anthonydahanne

This comment has been minimized.

Copy link

anthonydahanne commented Jul 9, 2018

/remove-lifecycle stale

@fejta-bot

This comment has been minimized.

Copy link

fejta-bot commented Oct 7, 2018

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@sieben

This comment has been minimized.

Copy link

sieben commented Oct 7, 2018

/remove-lifecycle stale

@omerfsen

This comment has been minimized.

Copy link

omerfsen commented Dec 18, 2018

There is a problem that KUBECONFIG does not understands special chars like ~ (home dir path) so use full path. for example:

KUBECONFIG=/home/serra/.kube/config:/home/serra/.kube/config.baremetal kubectl config view --flatten > /home/serra/.kube/config

@liggitt

This comment has been minimized.

Copy link
Member

liggitt commented Dec 18, 2018

There is a problem that KUBECONFIG does not understands special chars like ~ (home dir path) so use full path. for example:

KUBECONFIG=/home/serra/.kube/config:/home/serra/.kube/config.baremetal kubectl config view --flatten > /home/serra/.kube/config

Most shells expand that for you:

$ more ~/k1
apiVersion: v1
clusters:
- cluster:
    server: https://1.1.1.1
  name: cluster1
contexts:
- context:
    cluster: cluster1
    user: user1
  name: context1
current-context: context1
kind: Config
preferences: {}
users:
- name: user1

$ more ~/k2
apiVersion: v1
clusters:
- cluster:
    server: https://2.2.2.2
  name: cluster2
contexts:
- context:
    cluster: cluster2
    user: user2
  name: context2
current-context: context2
kind: Config
preferences: {}
users:
- name: user2

$ KUBECONFIG=~/k1:~/k2 kubectl config view
apiVersion: v1
clusters:
- cluster:
    server: https://1.1.1.1
  name: cluster1
- cluster:
    server: https://2.2.2.2
  name: cluster2
contexts:
- context:
    cluster: cluster1
    user: user1
  name: context1
- context:
    cluster: cluster2
    user: user2
  name: context2
current-context: context1
kind: Config
preferences: {}
users:
- name: user1
  user: {}
- name: user2
  user: {}
@MichaelSp

This comment has been minimized.

Copy link

MichaelSp commented Feb 7, 2019

My little bash script based on #46381 (comment)

# ~/.bashrc

function kmerge() {
  KUBECONFIG=~/.kube/config:$1 kubectl config view --flatten > ~/.kube/mergedkub && mv ~/.kube/mergedkub ~/.kube/config
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment