-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathhelm.go
292 lines (244 loc) · 7.66 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.
package helm
import (
"bytes"
"context"
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"sigs.k8s.io/yaml"
"github.com/gitpod-io/gitpod/installer/pkg/common"
"github.com/gitpod-io/gitpod/installer/third_party/charts"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/downloader"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/release"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// TemplateConfig
type TemplateConfig struct {
Namespace string
}
func getContext(settings Settings) context.Context {
// Create context and prepare the handle of SIGTERM
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
// Handle SIGTERM
cSignal := make(chan os.Signal)
signal.Notify(cSignal, os.Interrupt, syscall.SIGTERM)
go func() {
<-cSignal
settings.Write("Release of Gitpod has been cancelled.")
cancel()
}()
return ctx
}
func installDependencies(settings Settings) error {
client := action.NewDependency()
man := &downloader.Manager{
Out: &bytes.Buffer{},
ChartPath: settings.Chart,
Keyring: client.Keyring,
SkipUpdate: client.SkipRefresh,
Verify: downloader.VerifyNever,
RegistryClient: settings.ActionConfig.RegistryClient,
Getters: getter.All(settings.Env),
RepositoryConfig: settings.Env.RepositoryConfig,
RepositoryCache: settings.Env.RepositoryCache,
Debug: false,
}
err := man.Update()
if err != nil {
return fmt.Errorf("error pulling Helm dependency for %s: %w", settings.Chart, err)
}
return nil
}
// runInstall emulates this function in Helm with simplified error handling
// https://github.com/helm/helm/blob/9fafb4ad6811afb017cc464b630be2ff8390ac63/cmd/helm/install.go#L177
func runInstall(settings Settings, client *action.Install) (*release.Release, error) {
name, _, err := client.NameAndChart([]string{
settings.Config.Name,
settings.Chart,
})
if err != nil {
return nil, err
}
client.ReleaseName = name
p := getter.All(settings.Env)
vals, err := settings.Values.MergeValues(p)
if err != nil {
return nil, err
}
chartRequested, err := loader.Load(settings.Chart)
if err != nil {
return nil, err
}
return client.RunWithContext(getContext(settings), chartRequested, vals)
}
func writeCharts(chart *charts.Chart) (string, error) {
dir, err := os.MkdirTemp("", chart.Name)
if err != nil {
return "", err
}
err = chart.Export(dir)
if err != nil {
return "", err
}
return dir, nil
}
// AffinityYaml convert an affinity into a YAML byte array
func AffinityYaml(orLabels ...string) ([]byte, error) {
affinities := nodeAffinity(orLabels...)
marshal, err := yaml.Marshal(affinities)
if err != nil {
return nil, err
}
return marshal, nil
}
func nodeAffinity(orLabels ...string) *corev1.Affinity {
var terms []corev1.NodeSelectorTerm
for _, lbl := range orLabels {
terms = append(terms, corev1.NodeSelectorTerm{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Key: lbl,
Operator: corev1.NodeSelectorOpExists,
},
},
})
}
return &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: terms,
},
},
}
}
func ImagePullSecrets(key string, ctx *common.RenderContext) string {
if len(ctx.Config.ImagePullSecrets) > 0 {
var pullSecrets []string
for _, i := range ctx.Config.ImagePullSecrets {
pullSecrets = append(pullSecrets, i.Name)
}
return KeyValueArray(key, pullSecrets)
}
// Nothing to be set
return ""
}
// ImportTemplate allows for Helm charts to be imported into the installer manifest
func ImportTemplate(chart *charts.Chart, templateCfg TemplateConfig, pkgConfig PkgConfig) common.HelmFunc {
return func(cfg *common.RenderContext) (r []string, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("cannot import template %s: %w", chart.Name, err)
}
}()
helmConfig, err := pkgConfig(cfg)
if err != nil {
return nil, err
}
if !helmConfig.Enabled {
return nil, nil
}
dir, err := writeCharts(chart)
if err != nil {
return nil, err
}
settings := SettingsFactory(
&Config{
Debug: false,
Name: chart.Name,
Namespace: cfg.Namespace,
},
dir,
helmConfig.Values,
)
if _, err := os.Stat(filepath.Join(dir, "charts")); err != nil {
err = installDependencies(settings)
if err != nil {
return nil, err
}
}
client := action.NewInstall(settings.ActionConfig)
client.DryRun = true
client.ReleaseName = "RELEASE-NAME"
client.Replace = true // Skip the name check
client.ClientOnly = true
if templateCfg.Namespace == "" {
client.Namespace = cfg.Namespace
} else {
client.Namespace = templateCfg.Namespace
}
rel, err := runInstall(settings, client)
if err != nil {
return nil, err
}
if rel == nil {
return nil, fmt.Errorf("release for %s generated an empty value", settings.Config.Name)
}
// Fetch any additional Kubernetes files that need applying
var templates []string
for _, obj := range chart.AdditionalFiles {
b, err := chart.Content.ReadFile(obj)
if err != nil {
return nil, err
}
templates = append(templates, string(b))
}
return append(templates, rel.Manifest), nil
}
}
// CustomizeAnnotation check for customized annotations and output in Helm format
func CustomizeAnnotation(registryValues []string, prefix string, ctx *common.RenderContext, component string, typeMeta metav1.TypeMeta, existingAnnotations ...func() map[string]string) []string {
annotations := common.CustomizeAnnotation(ctx, component, typeMeta, existingAnnotations...)
if len(annotations) > 0 {
for k, v := range annotations {
// Escape any dots in the keys so they're not expanded
key := strings.Replace(k, ".", "\\.", -1)
registryValues = append(registryValues, KeyValue(fmt.Sprintf("%s.%s", prefix, key), v))
}
}
return registryValues
}
// CustomizeLabel check for customized labels and output in Helm format - also removes the default labels, which conflict with Helm
func CustomizeLabel(registryValues []string, prefix string, ctx *common.RenderContext, component string, typeMeta metav1.TypeMeta, existingLabels ...func() map[string]string) []string {
labels := common.CustomizeLabel(ctx, component, typeMeta, existingLabels...)
// Remove the default labels
for k := range common.DefaultLabels(component) {
delete(labels, k)
}
if len(labels) > 0 {
for k, v := range labels {
// Escape any dots in the keys so they're not expanded
key := strings.Replace(k, ".", "\\.", -1)
registryValues = append(registryValues, KeyValue(fmt.Sprintf("%s.%s", prefix, key), v))
}
}
return registryValues
}
// CustomizeEnvvar check for customized envvars and output in Helm format - assumes name/value only
func CustomizeEnvvar(registryValues []string, prefix string, ctx *common.RenderContext, component string, existingEnvvars ...[]corev1.EnvVar) []string {
// Helm is unlikely to have any existing envvars, so treat them as optional
envvars := common.CustomizeEnvvar(ctx, component, func() []corev1.EnvVar {
envs := make([]corev1.EnvVar, 0)
for _, e := range existingEnvvars {
envs = append(envs, e...)
}
return envs
}())
if len(envvars) > 0 {
for k, v := range envvars {
registryValues = append(registryValues, KeyValue(fmt.Sprintf("%s[%d].name", prefix, k), v.Name))
registryValues = append(registryValues, KeyValue(fmt.Sprintf("%s[%d].value", prefix, k), v.Value))
}
}
return registryValues
}