-
Notifications
You must be signed in to change notification settings - Fork 88
/
command.go
52 lines (43 loc) · 1.2 KB
/
command.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
package kubectl
import (
"context"
"fmt"
"os/exec"
//lint:ignore ST1001 since Ginkgo and Gomega are DSLs this makes the tests more natural to read
. "github.com/onsi/ginkgo/v2"
)
type CLI struct {
workspace string
kubeconfig string
}
func NewCLI(workspace, kubeconfig string) *CLI {
return &CLI{
workspace: workspace,
kubeconfig: kubeconfig,
}
}
func (c *CLI) Command(ctx context.Context, args ...string) *exec.Cmd {
args = append(
[]string{
fmt.Sprintf("--cache-dir=%s/.kube/cache", c.workspace),
fmt.Sprintf("--kubeconfig=%s", c.kubeconfig),
},
args...,
)
return exec.CommandContext(ctx, "kubectl", args...)
}
func (c *CLI) RunCommand(ctx context.Context, args ...string) ([]byte, error) {
return c.Command(ctx, args...).CombinedOutput()
}
func (c *CLI) GetPods(ctx context.Context, namespace string) {
out, _ := c.RunCommand(ctx, "get", "pods", fmt.Sprintf("--namespace=%s", namespace))
GinkgoWriter.Write(out)
}
func (c *CLI) GetAllPods(ctx context.Context) {
out, _ := c.RunCommand(ctx, "get", "pods", "--all-namespaces")
GinkgoWriter.Write(out)
}
func (c *CLI) DescribeNodes(ctx context.Context) {
out, _ := c.RunCommand(ctx, "describe", "nodes")
GinkgoWriter.Write(out)
}