This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
kubeyaml.go
52 lines (45 loc) · 1.53 KB
/
kubeyaml.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 kubernetes
import (
"bytes"
"errors"
"os/exec"
"strings"
)
// KubeYAML is a placeholder value for calling the helper executable
// `kubeyaml`.
type KubeYAML struct {
}
// Image calls the kubeyaml subcommand `image` with the arguments given.
func (k KubeYAML) Image(in []byte, ns, kind, name, container, image string) ([]byte, error) {
args := []string{"image", "--namespace", ns, "--kind", kind, "--name", name}
args = append(args, "--container", container, "--image", image)
return execKubeyaml(in, args)
}
// Annotate calls the kubeyaml subcommand `annotate` with the arguments as given.
func (k KubeYAML) Annotate(in []byte, ns, kind, name string, policies ...string) ([]byte, error) {
args := []string{"annotate", "--namespace", ns, "--kind", kind, "--name", name}
args = append(args, policies...)
return execKubeyaml(in, args)
}
// Set calls the kubeyaml subcommand `set` with the arguments given.
func (k KubeYAML) Set(in []byte, ns, kind, name string, values ...string) ([]byte, error) {
args := []string{"set", "--namespace", ns, "--kind", kind, "--name", name}
args = append(args, values...)
return execKubeyaml(in, args)
}
func execKubeyaml(in []byte, args []string) ([]byte, error) {
cmd := exec.Command("kubeyaml", args...)
out := &bytes.Buffer{}
errOut := &bytes.Buffer{}
cmd.Stdin = bytes.NewBuffer(in)
cmd.Stdout = out
cmd.Stderr = errOut
err := cmd.Run()
if err != nil {
if errOut.Len() == 0 {
return nil, err
}
return nil, errors.New(strings.TrimSpace(errOut.String()))
}
return out.Bytes(), nil
}