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

🐛 When loading client config, check os/user.HomeDir if $HOME is unset #753

Merged

Conversation

joelanford
Copy link
Member

@joelanford joelanford commented Jan 8, 2020

Following up on the regression introduced in #642 and discussed in #748, this PR detects if $HOME is empty and, if so, automatically sets $HOME to user.Current before using client-go's default loading rules, which only checks $HOME.

As a follow-on, we could submit an issue upstream to see if it makes sense for client-go to use user.Current as a fallback so that we don't have to do it here in controller-runtime.

Closes #748

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jan 8, 2020
@gerred
Copy link
Contributor

gerred commented Jan 10, 2020

/lgtm

This should be upstream but makes sense here for now.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 10, 2020
// os/user.HomeDir when $HOME is unset.
//
// TODO(jlanford): could this be done upstream?
if _, ok := os.LookupEnv("HOME"); !ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

does this actually work? It looks like homedir is called from a static variable initializer -- https://github.com/kubernetes/client-go/blob/master/tools/clientcmd/loader.go#L51

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh good catch. Yeah this won't work. It looks like changing RecommendedHomeFile will work though since that's what actually ends up getting used: https://github.com/kubernetes/client-go/blob/7ec8a74ae980246e9d1a5734f45bd44fde93a5c1/tools/clientcmd/loader.go#L153

I'll update to just set that directly instead of the HOME env var.

Copy link
Contributor

Choose a reason for hiding this comment

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

is that thread-safe?

I'm worried that we may have to do a more invasive solution here, like customizing the result of (or not using at all) the NewDefaultBlahBlahBlah method.

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe this is now thread-safe. Now, we no longer change any global variables. Instead we create an instance of loadingRules and then append the extra file derived from u.HomeDir to the precedence list.

I manually tested this and it still works as expected.

Suggestions on adding a unit test for this? I'm not opposed, but the use of user.Current() and u.HomeDir makes it a little awkward. I don't love the idea of creating temporary files and directories in the home directories of anyone that runs the unit tests.

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 14, 2020
@joelanford
Copy link
Member Author

/test pull-controller-runtime-test

@gerred
Copy link
Contributor

gerred commented Jan 15, 2020

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 15, 2020
@DirectXMan12
Copy link
Contributor

/hold

thread safety

@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 Jan 22, 2020
@kensipe
Copy link

kensipe commented Jan 23, 2020

@DirectXMan12 could you elaborate on the thread safety concern? I'm not seeing it. There is the concern that $HOME does not exist by default on windows.

since this is a bug fix... it would also be good to have a test

DirectXMan12 pushed a commit that referenced this pull request Jan 31, 2020
🐛 added multigroup check while creating resource
@joelanford
Copy link
Member Author

@kensipe I think the thead-safety concern is that this would change a global variable clientcmd.RecommendedHomeFile without a lock.

If multiple callers call any of the methods that use loadConfig concurrently without HOME set, it is possible that we would not correctly reset clientcmd.RecommendedHomeFile back to its original value:

  1. caller A sets"
    • oldRecommendedHomeFile = <original>
    • clientcmd.RecommendedHomeFile = <foobarA>
  2. caller B sets
    • oldRecommendedHomeFile = <foobarA>
    • clientcmd.RecommendedHomeFile = <foobarB>
  3. caller A resets clientcmd.RecommendedHomeFile = <original>
  4. caller B resets clientcmd.RecommendedHomeFile = <foobarB>

Final value of clientcmd.RecommendedHomeFile is <foobarB> when it should be <original>.

@DirectXMan12 I'll look into alternatives.

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 4, 2020
@DirectXMan12
Copy link
Contributor

/approve
/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 12, 2020
@vincepri
Copy link
Member

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Feb 12, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: DirectXMan12, joelanford

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 approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 12, 2020
@k8s-ci-robot k8s-ci-robot merged commit 0fcf28e into kubernetes-sigs:master Feb 12, 2020
@joelanford joelanford deleted the home-client-config-fix branch March 16, 2020 17:32
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/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

config.loadConfig Breaking Change introduced in 0.4.0
6 participants