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

Feat/kubeconfig multi cluster #512

Conversation

marckhouzam
Copy link

Modify kubeconfig for multiple clusters ( #112 )

kubeadm does not currently allow to configure a user reference id,
and instead always uses kubernetes-admin. This causes a problem
when we create multiple clusters with Kind and want to use
each corresponding kubeconfig file at the same time in KUBECONFIG.

Until kubeadm supports configuring a user reference id, the only
option to fix this for Kind is to modify the kubeconfig file that
kubeadm provides. As Kind already did this with the server name,
it made sense to take the logic further and also make the user
reference id unique to a cluster.

Three approaches were considered:

1- continue with the current approach of parsing each line of
the admin.conf kubeconfig file, and make the modifications
necessary. After implementing this approach, the solution
seemed quite brittle as it uses regex but no yaml structure.

2- use the go package yaml.v2 to -fully- parse the yaml of the
admin.conf kubeconfig file, make the modifications, and then
output the new yaml kubeconfig file. This solution requires
to define a detailed struct of every field contained in the
original yaml file. Having to map every field in advance is
brittle as any modification that kubeadm may make to the file
in the future would require adaptation in Kind.

3- use the go package yaml.v2 to -generically- parse the yaml of
the admin.conf kubeconfig file, make the modifications, and then
output the new yaml kubeconfig file. This solution only
accesses the yaml fields that are required to be modified.
Although any future changes from kubeadm to those fields would
require modifications in Kind, modifications to all other fields
would not.

This PR implements solution #3 which was felt to be the most
future-proof and least brittle of the three.

Signed-off-by: Marc Khouzam <marc.khouzam@ville.montreal.qc.ca>
kubeadm does not currently allow to configure a user reference id,
and instead always uses kubernetes-admin.  This causes a problem
when we create multiple clusters with Kind and want to use
each corresponding kubeconfig file at the same time in KUBECONFIG.

Until kubeadm supports configuring a user reference id, the only
option to fix this for Kind is to modify the kubeconfig file that
kubeadm provides.  As Kind already did this with the server name,
it made sense to take the logic further and also make the user
reference id unique to a cluster.

Three approaches were considered:

1- continue with the current approach of parsing each line of
   the admin.conf kubeconfig file, and make the modifications
   necessary.  After implementing this approach, the solution
   seemed quite brittle as it uses regex but no yaml structure.

2- use the go package yaml.v2 to -fully- parse the yaml of the
   admin.conf kubeconfig file, make the modifications, and then
   output the new yaml kubeconfig file.  This solution requires
   to define a detailed struct of every field contained in the
   original yaml file. Having to map every field in advance is
   brittle as any modification that kubeadm may make to the file
   in the future would require adaptation in Kind.

3- use the go package yaml.v2 to -generically- parse the yaml of
   the admin.conf kubeconfig file, make the modifications, and then
   output the new yaml kubeconfig file.  This solution only
   accesses the yaml fields that are required to be modified.
   Although any future changes from kubeadm to those fields would
   require modifications in Kind, modifications to all other fields
   would not.

This commit implements solution #3 which was felt to be the most
future-proof and least brittle of the three.

Signed-off-by: Marc Khouzam <marc.khouzam@ville.montreal.qc.ca>
@k8s-ci-robot
Copy link
Contributor

Welcome @marckhouzam!

It looks like this is your first PR to kubernetes-sigs/kind 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kind has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 9, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: marckhouzam
To fully approve this pull request, please assign additional approvers.
We suggest the following additional approver: munnerz

If they are not already assigned, you can assign the PR to them by writing /assign @munnerz in a comment when ready.

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 9, 2019
@k8s-ci-robot
Copy link
Contributor

Hi @marckhouzam. Thanks for your PR.

I'm waiting for a kubernetes-sigs or kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label May 9, 2019
Copy link
Member

@tao12345666333 tao12345666333 left a comment

Choose a reason for hiding this comment

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

Thanks. 👍 Have a bit confused.

for k := range user {
if k == "name" {
user[k] = newUserName
} else if k == "user" {
Copy link
Member

Choose a reason for hiding this comment

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

Is this for backward compatibility? The current config looks like this:

(MoeLove) ➜  kind git:(using-network) ✗ kubectl config view 
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://localhost:45965
  name: moe
contexts:
- context:
    cluster: moe
    user: kubernetes-admin
  name: kubernetes-admin@moe
current-context: kubernetes-admin@moe
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

Copy link
Author

Choose a reason for hiding this comment

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

Which part?

As I understand it, the field contexts[0].context.user refers to a user defined in this file in the users[] section. So, this PR makes the user unique and changes it in the two sections: contexts[0].context.user and users[].name.

Finally, the username within the cluster is taken by default to be users[].name but since we changed it, we need to tell what the real username is. That is why we must insert the field username.

There is not backwards-compatibility issue to worry about, as far as I can tell, although I am no expert in kubeconfig definitions.
However, the real username in the cluster is still kubernetes-admin, which used to be the

@BenTheElder
Copy link
Member

/cc @neolit123

@BenTheElder
Copy link
Member

Thanks for the PR!

/ok-to-test

I wonder if the KUBECONFIG format is fully stable by now, this change seems a bit brittle still.

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels May 9, 2019
@marckhouzam
Copy link
Author

@BenTheElder Your comment made me realize I may be able to make these kubeconfig modifications by calling kubectl config. That should be less brittle.

I'll try it out and report back.

@neolit123
Copy link
Member

@BenTheElder

I wonder if the KUBECONFIG format is fully stable by now, this change seems a bit brittle still.

yes the format is stable.
ideally kind should use client-go utils to load the kubeconfig, modify Config.Clusters.Server and write the modified object.

https://github.com/kubernetes/client-go/blob/master/tools/clientcmd/api/types.go
https://github.com/kubernetes/client-go/blob/master/tools/clientcmd/loader.go#L350
https://github.com/kubernetes/client-go/blob/master/tools/clientcmd/loader.go#L405

the alternative from kubectl is:

KUBECONFIG=/etc/kubernetes/admin.conf kubectl config set clusters.kubernetes.server <addr:port>

@marckhouzam

kubeadm does not currently allow to configure a user reference id,
and instead always uses kubernetes-admin. This causes a problem
when we create multiple clusters with Kind and want to use
each corresponding kubeconfig file at the same time in KUBECONFIG.

kubeadm has the following command:
kubeadm alpha kubeconfig user --client-name <user>

if you call this on a control-plane node it will output to STDOUT a kubeconfig for a separate user for the current cluster.

Until kubeadm supports configuring a user reference id, the only
option to fix this for Kind is to modify the kubeconfig file that
kubeadm provides. As Kind already did this with the server name,
it made sense to take the logic further and also make the user
reference id unique to a cluster.

kubeadm indeed does not allow configuring the default user name, but by configuring separate users for multiple clusters you can technically merge the resulted kubeconfig files.

Three approaches were considered:

see my comments above about using client-go. the library can also be used to merge kubeconfig files.

alternatively see how it can be done using KUBECONFIG and kubectl config:
https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#merging-kubeconfig-files

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 9, 2019
@marckhouzam
Copy link
Author

@neolit123 Thanks for the information. I will look into the different options and re-work this PR.

@neolit123
Copy link
Member

neolit123 commented May 10, 2019

np, i guess part of my point was that this should be already doable without changes in kind.

@marckhouzam
Copy link
Author

marckhouzam commented May 12, 2019

I did a new PR #520 to use client-go to modify the server in the kubeconfig file, as suggested by @neolit123

I've also written the modification needed to make the user unique using client-go, but I haven't posted it. I'll try @neolit123 suggestion about using kubeadm directly for this and see if I can get that to work nicely.

Ultimately, I think it would be nice if kubeadm would automatically generate the userid with the cluster name suffix, as this problem is not limited to Kind but to any time multiple clusters are created using kubeadm.

@BenTheElder BenTheElder added this to the 0.4 milestone May 15, 2019
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 15, 2019
@k8s-ci-robot
Copy link
Contributor

@marckhouzam: PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot
Copy link
Contributor

@marckhouzam: The following tests failed, say /retest to rerun them all:

Test name Commit Details Rerun command
pull-kind-verify e03994e link /test pull-kind-verify
pull-kind-conformance-parallel-1-15 e03994e link /test pull-kind-conformance-parallel-1-15
pull-kind-unit e03994e link /test pull-kind-unit
pull-kind-conformance-parallel-1-16 e03994e link /test pull-kind-conformance-parallel-1-16
pull-kind-e2e-kubernetes e03994e link /test pull-kind-e2e-kubernetes

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@BenTheElder
Copy link
Member

This is done now as part of #850. Thanks again for the PR 🙃

stg-0 added a commit to stg-0/kind that referenced this pull request Mar 7, 2024
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants