Skip to content

Commit

Permalink
check recommended kubeconfig path (#206)
Browse files Browse the repository at this point in the history
* check recommended kubeconfig path
* gen code
  • Loading branch information
harveyxia authored Feb 1, 2021
1 parent 0e94d1b commit dff5f99
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
4 changes: 4 additions & 0 deletions changelog/v0.17.2/kubeconfig-loader-check-recommended.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changelog:
- type: FIX
issueLink: https://github.com/solo-io/skv2/issues/205
description: Add recommended kubeconfig directory path to kubeconfig loader loading rules.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/Masterminds/sprig/v3 v3.1.0
github.com/avast/retry-go v2.2.0+incompatible
github.com/aws/aws-sdk-go v1.30.15
github.com/envoyproxy/protoc-gen-validate v0.1.0
github.com/envoyproxy/protoc-gen-validate v0.3.0-java
github.com/gertd/go-pluralize v0.1.1
github.com/go-logr/logr v0.3.0
github.com/go-logr/zapr v0.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0 h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/envoyproxy/protoc-gen-validate v0.3.0-java h1:bV5JGEB1ouEzZa0hgVDFFiClrUEuGWRaAc/3mxR2QK0=
github.com/envoyproxy/protoc-gen-validate v0.3.0-java/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanphx/json-patch v0.0.0-20190203023257-5858425f7550/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch v4.5.0+incompatible h1:ouOWdg56aJriqS0huScTkVXPC5IcNrDCXZ6OoTAWu7M=
Expand Down
20 changes: 5 additions & 15 deletions pkg/multicluster/kubeconfig/loader.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
package kubeconfig

import (
"fmt"
"os"
"os/user"
"path"

"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

// Fetch ClientConfig. If kubeConfigPath is not specified, retrieve the kubeconfig from environment in which this is invoked.
// Override the API Server URL and current context if specified.
// Copied and modified from https://github.com/kubernetes-sigs/controller-runtime/blob/cb7f85860a8cde7259b35bb84af1fdcb02c098f2/pkg/client/config/config.go#L135
func GetClientConfigWithContext(kubeConfigPath, kubeContext, apiServerUrl string) (clientcmd.ClientConfig, error) {

// default loading rules checks for KUBECONFIG env var
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
// also check recommended default kubeconfig file locations
loadingRules.Precedence = append(loadingRules.Precedence, clientcmd.RecommendedHomeFile)

// explicit path overrides all loading rules, will error if not found
if kubeConfigPath != "" {
loadingRules.ExplicitPath = kubeConfigPath
} else {
// Fetch kubeconfig from environment in which this is invoked
if _, ok := os.LookupEnv("HOME"); !ok {
u, err := user.Current()
if err != nil {
return nil, fmt.Errorf("could not get current user: %v", err)
}
loadingRules.Precedence = append(loadingRules.Precedence, path.Join(u.HomeDir, clientcmd.RecommendedHomeDir, clientcmd.RecommendedFileName))
}
}

overrides := &clientcmd.ConfigOverrides{}
Expand Down

0 comments on commit dff5f99

Please sign in to comment.