-
Notifications
You must be signed in to change notification settings - Fork 432
/
kubernetes.go
71 lines (54 loc) · 1.37 KB
/
kubernetes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package install
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"github.com/solo-io/gloo/pkg/cliutil"
)
// Deprecated: Prefer kubectl.Cli
type KubeCli interface {
Kubectl(stdin io.Reader, args ...string) error
KubectlOut(stdin io.Reader, args ...string) ([]byte, error)
}
type CmdKubectl struct{}
var _ KubeCli = &CmdKubectl{}
func (k *CmdKubectl) Kubectl(stdin io.Reader, args ...string) error {
return Kubectl(stdin, args...)
}
func (k *CmdKubectl) KubectlOut(stdin io.Reader, args ...string) ([]byte, error) {
return KubectlOut(stdin, args...)
}
var verbose bool
func SetVerbose(b bool) {
verbose = b
}
// Deprecated: Prefer kubectl.Cli
func Kubectl(stdin io.Reader, args ...string) error {
_, err := KubectlOut(stdin, args...)
return err
}
// Deprecated: Prefer kubectl.Cli
func KubectlOut(stdin io.Reader, args ...string) ([]byte, error) {
kubectl := exec.Command("kubectl", args...)
if stdin != nil {
kubectl.Stdin = stdin
}
var stdout, stderr io.Writer
if verbose {
fmt.Fprintf(os.Stderr, "running kubectl command: %v\n", kubectl.Args)
stdout = os.Stdout
stderr = os.Stderr
} else {
// use logfile
cliutil.Initialize()
stdout = cliutil.GetLogger()
stderr = cliutil.GetLogger()
}
buf := &bytes.Buffer{}
kubectl.Stdout = io.MultiWriter(stdout, buf)
kubectl.Stderr = io.MultiWriter(stderr, buf)
err := kubectl.Run()
return buf.Bytes(), err
}