-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathhelm.go
165 lines (143 loc) · 4.91 KB
/
helm.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
package helm
import (
"bytes"
"errors"
"fmt"
"os/exec"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/thoas/go-funk"
"k8s.io/klog/v2"
)
// Client is a local helm client
type Client struct {
HelmExecutable string
}
// NewClient ensures a helm command exists
func NewClient() (*Client, error) {
path, err := exec.LookPath("helm")
if err != nil {
return nil, fmt.Errorf("helm must be installed and available in path: %s", err.Error())
}
klog.V(3).Infof("found helm at %s", path)
return &Client{path}, nil
}
// Exec returns the output and error of a helm command given several arguments
// Returns stdOut and stdErr as well as any error
func (h Client) Exec(arg ...string) (string, string, error) {
cmd := exec.Command(h.HelmExecutable, arg...)
klog.V(8).Infof("running helm command: %v", cmd)
var stdoutBuf, stderrBuf bytes.Buffer
cmd.Stdout = &stdoutBuf
cmd.Stderr = &stderrBuf
err := cmd.Run()
outStr, errStr := stdoutBuf.String(), stderrBuf.String()
if err != nil {
klog.V(8).Infof("stdout: %s", outStr)
klog.V(7).Infof("stderr: %s", errStr)
return "", errStr, fmt.Errorf("exit code %d running command %s", cmd.ProcessState.ExitCode(), cmd.String())
}
return outStr, errStr, nil
}
// Version returns the helm client version
func (h Client) Version() (*semver.Version, error) {
out, _, err := h.Exec("version", "--template={{.Version}}")
if err != nil {
return nil, err
}
version, err := semver.NewVersion(out)
if err != nil {
return nil, err
}
return version, nil
}
// AddRepository adds a Helm repository
func (h Client) AddRepository(repoName, url string) error {
_, _, err := h.Exec("repo", "add", repoName, url)
if err != nil {
return err
}
return nil
}
// Cache returns the local helm cache if defined
func (h Client) Cache() (string, error) {
stdOut, stdErr, _ := h.Exec("env")
if stdErr != "" {
return "", fmt.Errorf("error running helm env: %s", stdErr)
}
for _, line := range strings.Split(stdOut, "\n") {
if strings.Contains(line, "HELM_REPOSITORY_CACHE") {
value := strings.Split(line, "=")[1]
return strings.Trim(value, "\""), nil
}
}
return "", fmt.Errorf("could not find HELM_REPOSITORY_CACHE in helm env output")
}
// UpdateDependencies will update dependencies for a given release if it is stored locally (i.e. pulled from git)
func (h Client) UpdateDependencies(path string) error {
klog.V(5).Infof("updating chart dependencies for %s", path)
_, stdErr, _ := h.Exec("dependency", "update", path)
if stdErr != "" {
return fmt.Errorf("error running helm dependency update: %s", stdErr)
}
return nil
}
// GetManifestString will run 'helm get manifest' on a given namespace and release and return string output.
func (h Client) GetManifestString(namespace, release string) (string, error) {
out, err := h.get("manifest", namespace, release)
if err != nil {
return "", err
}
return out, err
}
// GetUserSuppliedValues will run 'helm get values' on a given namespace and release and return []byte output suitable for yaml Marshaling.
func (h Client) GetUserSuppliedValuesYAML(namespace, release string) ([]byte, error) {
out, err := h.get("values", namespace, release, "--output", "yaml")
if err != nil {
return nil, err
}
return []byte(out), err
}
// ListNamespaceReleasesYAML will run 'helm list' on a given namespace and return []byte output suitable for yaml Marshaling.
func (h Client) ListNamespaceReleasesYAML(namespace string) ([]byte, error) {
out, err := h.list(namespace, "--output", "yaml")
if err != nil {
return nil, err
}
return []byte(out), nil
}
// get can run any 'helm get' command
func (h Client) get(kind, namespace, release string, extraArgs ...string) (string, error) {
validKinds := []string{"all", "hooks", "manifest", "notes", "values"}
if !funk.Contains(validKinds, kind) {
return "", errors.New("invalid kind passed to helm: " + kind)
}
args := []string{"get", kind, "--namespace", namespace, release}
args = append(args, extraArgs...)
stdOut, stdErr, err := h.Exec(args...)
if err != nil && stdErr != "" {
return "", errors.New(stdErr)
}
return stdOut, nil
}
// list can run any 'helm list' command
func (h Client) list(namespace string, extraArgs ...string) (string, error) {
args := []string{"list", "--namespace", namespace}
args = append(args, extraArgs...)
stdOut, stdErr, err := h.Exec(args...)
if err != nil && stdErr != "" {
return "", errors.New(stdErr)
}
return stdOut, nil
}