-
Notifications
You must be signed in to change notification settings - Fork 444
/
kubernetes.go
93 lines (73 loc) · 2.1 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package install
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"github.com/solo-io/gloo/pkg/cliutil"
)
func KubectlApply(manifest []byte, extraArgs ...string) error {
return Kubectl(bytes.NewBuffer(manifest), append([]string{"apply", "-f", "-"}, extraArgs...)...)
}
func KubectlApplyOut(manifest []byte, extraArgs ...string) ([]byte, error) {
return KubectlOut(bytes.NewBuffer(manifest), append([]string{"apply", "-f", "-"}, extraArgs...)...)
}
func KubectlDelete(manifest []byte, extraArgs ...string) error {
return Kubectl(bytes.NewBuffer(manifest), append([]string{"delete", "-f", "-"}, extraArgs...)...)
}
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
}
func Kubectl(stdin io.Reader, args ...string) error {
kubectl := exec.Command("kubectl", args...)
if stdin != nil {
kubectl.Stdin = stdin
}
if verbose {
fmt.Fprintf(os.Stderr, "running kubectl command: %v\n", kubectl.Args)
kubectl.Stdout = os.Stdout
kubectl.Stderr = os.Stderr
} else {
// use logfile
cliutil.Initialize()
kubectl.Stdout = cliutil.GetLogger()
kubectl.Stderr = cliutil.GetLogger()
}
return kubectl.Run()
}
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
}