forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubectl_apply.go
75 lines (66 loc) · 1.81 KB
/
kubectl_apply.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
package utils
import (
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
"github.com/solo-io/go-utils/errors"
)
func IstioInject(istioNamespace, input string) (string, error) {
cmd := exec.Command("istioctl", "kube-inject", "-i", istioNamespace, "-f", "-")
cmd.Stdin = bytes.NewBuffer([]byte(input))
output := &bytes.Buffer{}
cmd.Stdout = output
cmd.Stderr = output
err := cmd.Run()
if err != nil {
return "", errors.Wrapf(err, "kube inject failed: %v", output.String())
}
return output.String(), nil
}
func KubectlApply(namespace, yamlStr string) error {
return Kubectl(bytes.NewBuffer([]byte(yamlStr)), "apply", "-n", namespace, "-f", "-")
}
func KubectlDelete(namespace, yamlStr string) error {
return Kubectl(bytes.NewBuffer([]byte(yamlStr)), "delete", "-n", namespace, "--ignore-not-found=true", "-f", "-")
}
func Kubectl(stdin io.Reader, args ...string) error {
return KubectlCtx(nil, stdin, args...)
}
func KubectlPortForward(ctx context.Context, namespace, deployment string, port int) error {
log.Printf("starting port forward on %v.%v:%v", namespace, deployment, port)
return KubectlCtx(ctx, nil, "port-forward", "-n", namespace, "deployment/"+deployment, fmt.Sprintf("%v", port))
}
func KubectlCtx(ctx context.Context, stdin io.Reader, args ...string) error {
kubectl := exec.Command("kubectl", args...)
if stdin != nil {
kubectl.Stdin = stdin
}
kubectl.Stdout = os.Stdout
kubectl.Stderr = os.Stderr
if ctx != nil {
go func() {
defer ginkgo.GinkgoRecover()
err := kubectl.Run()
select {
case <-ctx.Done():
return
default:
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}
}()
go func() {
<-ctx.Done()
if kubectl.Process != nil {
kubectl.Process.Kill()
}
}()
return nil
}
return kubectl.Run()
}