This repository has been archived by the owner on Nov 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
kubectl.go
122 lines (95 loc) · 2.6 KB
/
kubectl.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package cmd
import (
"fmt"
)
type Kubectl struct {
cmd *Command
}
func (k *Kubectl) SwallowErrorLog(swallow bool) *Kubectl {
k.cmd.SwallowErrorLog = swallow
return k
}
func (k *Kubectl) Cmd() *Command {
return k.cmd
}
func (k *Kubectl) With(args ...string) *Kubectl {
k.cmd = k.cmd.With(args...)
return k
}
func (k *Kubectl) WithStdIn(stdIn string) *Kubectl {
k.cmd.StdIn = stdIn
return k
}
func (k *Kubectl) File(file string) *Kubectl {
return k.With("-f", file)
}
func (k *Kubectl) WithName(name string) *Kubectl {
return k.With(name)
}
func (k *Kubectl) Namespace(ns string) *Kubectl {
return k.With("-n", ns)
}
func (k *Kubectl) Context(context string) *Kubectl {
return k.With("--context", context)
}
func (k *Kubectl) DryRun() *Kubectl {
return k.With("--dry-run")
}
func (k *Kubectl) OutYaml() *Kubectl {
return k.With("-oyaml")
}
func (k *Kubectl) IgnoreNotFound() *Kubectl {
return k.With("--ignore-not-found")
}
func (k *Kubectl) Create(typeToCreate string) *Kubectl {
return k.With("create", typeToCreate)
}
func (k *Kubectl) Delete(typeToDelete string) *Kubectl {
return k.With("delete", typeToDelete)
}
func (k *Kubectl) Apply() *Kubectl {
return k.With("apply")
}
func (k *Kubectl) ApplyStdIn(stdIn string) *Kubectl {
return k.Apply().File("-").WithStdIn(stdIn)
}
func (k *Kubectl) ApplyFile(path string) *Kubectl {
return k.Apply().File(path)
}
func (k *Kubectl) Redact(unredacted, redacted string) *Kubectl {
if k.cmd.Redactions == nil {
k.cmd.Redactions = make(map[string]string)
}
k.cmd.Redactions[unredacted] = redacted
return k
}
func (k *Kubectl) UseContext(context string) *Kubectl {
return k.With("config", "use-context", context)
}
func (k *Kubectl) CurrentContext() *Kubectl {
return k.With("config", "current-context")
}
func (k *Kubectl) DryRunAndApply(runner Runner) error {
out, err := runner.Output(k.DryRun().OutYaml().Cmd())
if err != nil {
return err
}
return runner.Run(New().Kubectl().ApplyStdIn(out).Cmd())
}
func (k *Kubectl) JsonPatch(jsonPatch string) *Kubectl {
return k.With("--type=json", jsonPatch)
}
func (k *Kubectl) DeleteFile(path string) *Kubectl {
return k.With("delete", "-f", path)
}
func (k *Kubectl) DeleteStdIn(stdIn string) *Kubectl {
return k.DeleteFile("-").WithStdIn(stdIn)
}
func (k *Kubectl) OutJsonpath(jsonpath string) *Kubectl {
return k.With(fmt.Sprintf("-o=jsonpath=%s", jsonpath))
}
func (k *Kubectl) GetServiceIP(namespace, name string, runner Runner) (string, error) {
cmd := k.With("get", "svc", name).Namespace(namespace)
cmd = cmd.OutJsonpath("{ .status.loadBalancer.ingress[0].ip }")
return runner.Output(cmd.Cmd())
}