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

kubeconfig overhaul #1029

Merged
merged 11 commits into from
Oct 31, 2019
Merged

Conversation

BenTheElder
Copy link
Member

Implements most of #850, also #112, #113

This is smaller than it looks, a lot of the lines are boilerplate license etc. because I broke up the implementation into small portions and tried to comment it pretty strongly

There are a few additional not strictly necessary but nice to have features like ensuring that the formatting matches client-go generated kubeconfigs.

This does:

This is a mildly breaking change. We'll want a simple guide in the release notes for migration.

If you want kind to use a different config file, you can either set the --kubeconfig option to kind create cluster / kind delete cluster or you can export KUBECONFIG prior to using kind.

This is basically the same as before, just that you decide the path and you do it before kind create cluster instead of after.

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 30, 2019
@k8s-ci-robot k8s-ci-robot requested review from amwat and munnerz October 30, 2019 17:44
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: BenTheElder

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 size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Oct 30, 2019
@BenTheElder
Copy link
Member Author

heh our lints don't like client-go :+)

pkg/internal/util/kubeconfig/merge.go:34:18: Error return value of `unlockFile` is not checked (errcheck)
	defer unlockFile(configPath)
	                ^
pkg/internal/util/kubeconfig/remove.go:37:19: Error return value of `unlockFile` is not checked (errcheck)
		defer unlockFile(configPath)
		                ^
verifying generated ...

@BenTheElder
Copy link
Member Author

important reference for expected behavior: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands

in addition, from inspecting code (mainly client-go directly) and reading some other sources

  • for the case of KUBECONFIG being a list of paths, empty paths are discarded, duplicate paths are discarded, THEN the first existing one OR the last file is used
  • path+".lock" should be used to 'lock' modifications, we've lifted the exact little lockfile creation / deletion routines from client-go
  • merging clusters into kubeconfig universally sets the current context (kops, gcloud, etc. ...)
  • removing clusters from kubeconfig universally unsets the current context if the context refers to the cluster being removed

pkg/internal/util/kubeconfig/remove.go Outdated Show resolved Hide resolved
pkg/internal/util/kubeconfig/remove.go Outdated Show resolved Hide resolved
pkg/internal/util/kubeconfig/merge.go Show resolved Hide resolved
@BenTheElder BenTheElder changed the title [WIP] kubeconfig overhaul kubeconfig overhaul Oct 30, 2019
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 30, 2019
"sigs.k8s.io/kind/pkg/internal/util/assert"
)

func TestEncodeRoundtrip(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add a test failing?

Copy link
Member Author

Choose a reason for hiding this comment

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

not trivially, it only fails if it cannot marshal the type so ...

Copy link
Member Author

Choose a reason for hiding this comment

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

FYI we have 41.5% statements covered in this package, the rest are mostly error cases that aren't really triggerable like this, or file system interactions (and somewhat unit tests then, but I will probably still add some)

if err != nil {
return nil, err
}
// special case: don't write anything when empty
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we can detect this first?

Copy link
Member Author

Choose a reason for hiding this comment

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

not really, the input is fairly arbitrary.

this is a really simple nice to have thing to make the output more like the user would expect. it would be totally valid to not have this entire method.

@BenTheElder
Copy link
Member Author

brought coverage up to 59.4% of statements for the kubeconfig package by covering WriteMerged with writing in tempdirs.

@BenTheElder
Copy link
Member Author

ok covered removal. this is now at 64.7% statements covered.
the non-covered statements are largely:

  • the homedir implementation copied from client-go which has a large amount of windows specific code. /shrug we won't be modifying this, only keeping in sync with upstream
  • error branches that are technically possible but won't really happen (eg failing to marshal a kubeconfig)

@BenTheElder BenTheElder requested a review from amwat October 31, 2019 01:20

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands

- If the --kubeconfig flag is set, then only that file is loaded. The flag may only be set once and no merging takes place.
Copy link
Contributor

Choose a reason for hiding this comment

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

We should document this later in the quick start guide or the readme

Copy link
Contributor

Choose a reason for hiding this comment

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

done #1032

@aojea
Copy link
Contributor

aojea commented Oct 31, 2019

/lgtm
this is simply amazing, impressive work

Comment on lines -139 to -143
// matches kubeconfig server entry like:
// server: https://172.17.0.2:6443
// which we rewrite to:
// server: https://$ADDRESS:$PORT
var serverAddressRE = regexp.MustCompile(`^(\s+server:) https://.*:\d+$`)
Copy link
Contributor

@maelvls maelvls Nov 5, 2019

Choose a reason for hiding this comment

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

Since the 'rewrite' doesn't (seem to) happen anymore, the kind get kubeconfig prints the container's host and port instead of 127.0.0.1 + the forwarded port. Or am I wrong?

Or maybe I'm using kind get kubeconfig wrong 😁

The kind get kubeconfig --internal does the same as kind get kubeconfig --internal for now (as of aea9a47)

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the quick response 🙂
Issue created: #1051

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants