Skip to content

Commit

Permalink
fix(kube-client): support kube config merge list option for KubeClien…
Browse files Browse the repository at this point in the history
…tGetter

Signed-off-by: Timofey Kirillov <[email protected]>
  • Loading branch information
distorhead committed Feb 22, 2022
1 parent 7c274f2 commit ae4dd95
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
18 changes: 10 additions & 8 deletions pkg/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ func makeOutOfClusterClientConfigError(configPath, context string, err error) er
return fmt.Errorf("%s: %s", baseErrMsg, err)
}

func setConfigPathMergeListEnvironment(configPathMergeList []string) error {
configPathEnvVar := strings.Join(configPathMergeList, string(filepath.ListSeparator))
if err := os.Setenv(clientcmd.RecommendedConfigPathEnvVar, configPathEnvVar); err != nil {
return fmt.Errorf("unable to set env var %q: %s", clientcmd.RecommendedConfigPathEnvVar, err)
}
return nil
}

func GetClientConfig(context string, configPath string, configData []byte, configPathMergeList []string) (clientcmd.ClientConfig, error) {
overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults}
if context != "" {
Expand All @@ -177,14 +185,8 @@ func GetClientConfig(context string, configPath string, configData []byte, confi
}

if len(configPathMergeList) > 0 {
oldEnvVar := os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
defer func() {
os.Setenv(clientcmd.RecommendedConfigPathEnvVar, oldEnvVar)
}()

configPathEnvVar := strings.Join(configPathMergeList, string(filepath.ListSeparator))
if err := os.Setenv(clientcmd.RecommendedConfigPathEnvVar, configPathEnvVar); err != nil {
return nil, fmt.Errorf("unable to set env var %q: %s", clientcmd.RecommendedConfigPathEnvVar, err)
if err := setConfigPathMergeListEnvironment(configPathMergeList); err != nil {
return nil, err
}
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/kube/kube_config_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func NewKubeConfigGetter(opts KubeConfigGetterOptions) (genericclioptions.RESTCl
} else {
configFlags := genericclioptions.NewConfigFlags(true)

if len(opts.ConfigPathMergeList) > 0 {
if err := setConfigPathMergeListEnvironment(opts.ConfigPathMergeList); err != nil {
return nil, err
}
}

configFlags.Context = new(string)
*configFlags.Context = opts.Context

Expand Down

0 comments on commit ae4dd95

Please sign in to comment.