/
helm.go
238 lines (198 loc) · 5.63 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package helm
import (
"fmt"
"log"
"net/url"
"os"
"strings"
"time"
"github.com/awslabs/eksdemo/pkg/printer"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/postrender"
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/repo"
"helm.sh/helm/v3/pkg/strvals"
"sigs.k8s.io/yaml"
)
var debug bool
type Helm struct {
AppVersion string
ChartName string
ChartVersion string
Namespace string
PostRenderer postrender.PostRenderer
ReleaseName string
RepositoryURL string
Wait bool
SetValues []string
ValuesFile string
}
func Init(helmDebug bool) {
debug = helmDebug
}
func initialize(kubeContext, namespace string) (*action.Configuration, error) {
// Hack to work around https://github.com/helm/helm/issues/7430
_ = os.Setenv("HELM_KUBECONTEXT", kubeContext)
_ = os.Setenv("HELM_NAMESPACE", namespace)
settings := cli.New()
// Initialize the action configuration
actionConfig := new(action.Configuration)
if err := actionConfig.Init(settings.RESTClientGetter(), namespace, "secret", log.Printf); err != nil {
return nil, fmt.Errorf("failed to initialize helm action config: %w", err)
}
return actionConfig, nil
}
func (h *Helm) DownloadChart() (*chart.Chart, error) {
getters := getter.All(&cli.EnvSettings{})
u, err := url.Parse(h.RepositoryURL)
if err != nil {
return nil, err
}
var chartPath string
if u.Scheme == "oci" {
chartPath = h.RepositoryURL + ":" + h.ChartVersion
} else {
// Find Chart
chartPath, err = repo.FindChartInRepoURL(h.RepositoryURL, h.ChartName, h.ChartVersion, "", "", "", getters)
if err != nil {
return nil, err
}
}
fmt.Printf("Downloading Chart: %s\n", chartPath)
g, err := getters.ByScheme(u.Scheme)
if err != nil {
return nil, err
}
// Download chart archive into memory
data, err := g.Get(chartPath)
// If ECR Public is returning a 403 Forbidden error, then log out and try again
// https://docs.aws.amazon.com/AmazonECR/latest/public/public-troubleshooting.html
if h.isECRPublicAuthError(err) {
fmt.Println("ECR Public is returning a 403 Forbidden error. Logging out and trying again...")
registryClient, e := registry.NewClient()
if e != nil {
return nil, fmt.Errorf("failed to create registry client: %w", e)
}
e = registryClient.Logout("public.ecr.aws")
if e != nil {
return nil, fmt.Errorf("failed to log out of ECR Public: %w", e)
}
ociGetter, e := getter.NewOCIGetter()
if e != nil {
return nil, e
}
data, err = ociGetter.Get(chartPath)
}
if err != nil {
return nil, err
}
// Decompress the archive
files, err := loader.LoadArchiveFiles(data)
if err != nil {
return nil, err
}
// Load the chart
chart, err := loader.LoadFiles(files)
if err != nil {
return nil, err
}
return chart, nil
}
func (h *Helm) Install(chart *chart.Chart, kubeContext string) error {
// Parse the values file
values := map[string]interface{}{}
if err := yaml.Unmarshal([]byte(h.ValuesFile), &values); err != nil {
return fmt.Errorf("failed to parse values file: %w", err)
}
for _, v := range h.SetValues {
if err := strvals.ParseInto(v, values); err != nil {
return fmt.Errorf("failed parsing --set data: %w", err)
}
}
if debug {
fmt.Println("\nHelm Debug, Final Values:")
fmt.Println("---")
err := printer.EncodeYAML(os.Stdout, values)
if err != nil {
return fmt.Errorf("failed to print final values: %w", err)
}
fmt.Println()
}
actionConfig, err := initialize(kubeContext, h.Namespace)
if err != nil {
return err
}
// Configure the install options
instAction := action.NewInstall(actionConfig)
instAction.Namespace = h.Namespace
instAction.ReleaseName = h.ReleaseName
instAction.CreateNamespace = true
instAction.IsUpgrade = true
instAction.PostRenderer = h.PostRenderer
instAction.Wait = h.Wait
instAction.Timeout = 300 * time.Second
chart.Metadata.AppVersion = h.AppVersion
// Install the chart
fmt.Println("Helm installing...")
rel, err := instAction.Run(chart, values)
if err != nil {
return fmt.Errorf("helm install failed: %s", err)
}
fmt.Printf("Using chart version %q, installed %q version %q in namespace %q\n",
rel.Chart.Metadata.Version, rel.Name, rel.Chart.Metadata.AppVersion, rel.Namespace)
// Print the Chart NOTES
if len(rel.Info.Notes) > 0 {
fmt.Printf("NOTES:\n%s\n", strings.TrimSpace(rel.Info.Notes))
}
return nil
}
func (h *Helm) isECRPublicAuthError(err error) bool {
return err != nil &&
strings.HasPrefix(h.RepositoryURL, "oci://public.ecr.aws") &&
strings.HasSuffix(err.Error(), "403 Forbidden")
}
func List(kubeContext string) ([]*release.Release, error) {
actionConfig, err := initialize(kubeContext, "")
if err != nil {
return nil, err
}
client := action.NewList(actionConfig)
client.AllNamespaces = true
releases, err := client.Run()
if (err) != nil {
return nil, err
}
return releases, nil
}
func Status(kubeContext, releaseName, namespace string) (string, error) {
actionConfig, err := initialize(kubeContext, namespace)
if err != nil {
return "", err
}
status := action.NewStatus(actionConfig)
rel, err := status.Run(releaseName)
if (err) != nil {
return "", err
}
// strip chart metadata from the output
rel.Chart = nil
return "", nil
}
func Uninstall(kubeContext, releaseName, namespace string) error {
actionConfig, err := initialize(kubeContext, namespace)
if err != nil {
return err
}
uninstall := action.NewUninstall(actionConfig)
// Uninstall the chart
_, err = uninstall.Run(releaseName)
if err != nil {
return fmt.Errorf("failed uninstalling chart: %w", err)
}
return nil
}