8000 fix kubectl KUBECONFIG path by StrongMonkey · Pull Request #304 · rancher/rio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

fix kubectl KUBECONFIG path #304

Merged
merged 1 commit into from
Jun 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions cli/pkg/clicontext/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package clicontext

import (
"fmt"
"io/ioutil"
"os"
"os/exec"

"github.com/rancher/wrangler/pkg/kubeconfig"
"github.com/sirupsen/logrus"
"k8s.io/client-go/tools/clientcmd"
)

func (c *Config) KubectlCmd(namespace, command string, args ...string) (*exec.Cmd, error) {
func (c *Config) Kubectl(namespace, command string, args ...string) error {
var execArgs []string
if logrus.GetLevel() >= logrus.DebugLevel {
execArgs = append(execArgs, "--v=9")
Expand All @@ -21,19 +24,30 @@ func (c *Config) KubectlCmd(namespace, command string, args ...string) (*exec.Cm
}
execArgs = append(execArgs, args...)

if c.Kubeconfig == "" {
loader := kubeconfig.GetInteractiveClientConfig(c.Kubeconfig)
rawconfig, err := loader.RawConfig()
if err != nil {
return err
}
fp, err := ioutil.TempFile("", "kubeconfig-")
if err != nil {
return err
}
if err := fp.Close(); err != nil {
return err
}
defer os.Remove(fp.Name())
if err := clientcmd.WriteToFile(rawconfig, fp.Name()); err != nil {
return err
}
c.Kubeconfig = fp.Name()
}
logrus.Debugf("kubectl %v, KUBECONFIG=%s", execArgs, c.Kubeconfig)
cmd := exec.Command("kubectl", execArgs...)
cmd.Env = append(os.Environ(), fmt.Sprintf("KUBECONFIG=%s", c.Kubeconfig))
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
return cmd, nil
}

func (c *Config) Kubectl(namespace, command string, args ...string) error {
cmd, err := c.KubectlCmd(namespace, command, args...)
if err != nil {
return err
}
return cmd.Run()
}
0