-
Notifications
You must be signed in to change notification settings - Fork 52
/
helm.go
306 lines (268 loc) · 8.36 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2020 IBM Corp.
// SPDX-License-Identifier: Apache-2.0
package helm
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/kube"
"helm.sh/helm/v3/pkg/release"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
var (
debugOption = os.Getenv("HELM_DEBUG") == "true"
)
const chartsMountPath = "/opt/fybrik/charts/"
func getConfig(kubeNamespace string) (*action.Configuration, error) {
actionConfig := new(action.Configuration)
if kubeNamespace == "" {
kubeNamespace = "default"
}
config := &genericclioptions.ConfigFlags{
Namespace: &kubeNamespace,
}
err := actionConfig.Init(config, kubeNamespace, os.Getenv("HELM_DRIVER"), debugf)
if err != nil {
return nil, err
}
return actionConfig, err
}
func debugf(format string, v ...interface{}) {
if debugOption {
format = fmt.Sprintf("[debug] %s\n", format)
_ = log.Output(2, fmt.Sprintf(format, v...))
}
}
func ChartRef(hostname, namespace, name, tagname string) string {
return fmt.Sprintf("%s/%s/%s:%s", hostname, namespace, name, tagname)
}
// Interface of a helm chart
type Interface interface {
Uninstall(kubeNamespace string, releaseName string) (*release.UninstallReleaseResponse, error)
Install(chart *chart.Chart, kubeNamespace string, releaseName string, vals map[string]interface{}) (*release.Release, error)
Upgrade(chart *chart.Chart, kubeNamespace string, releaseName string, vals map[string]interface{}) (*release.Release, error)
Status(kubeNamespace string, releaseName string) (*release.Release, error)
RegistryLogin(hostname string, username string, password string, insecure bool) error
RegistryLogout(hostname string) error
Pull(ref string, destination string) error
Load(ref string, chartPath string) (*chart.Chart, error)
Package(chartPath string, destinationPath string, version string) error
GetResources(kubeNamespace string, releaseName string) ([]*unstructured.Unstructured, error)
}
// Fake implementation
type Fake struct {
release *release.Release
resources []*unstructured.Unstructured
}
// Uninstall helm release
func (r *Fake) Uninstall(kubeNamespace, releaseName string) (*release.UninstallReleaseResponse, error) {
res := &release.UninstallReleaseResponse{}
r.release = nil
return res, nil
}
// Install helm release
func (r *Fake) Install(chrt *chart.Chart, kubeNamespace, releaseName string, vals map[string]interface{}) (*release.Release, error) {
r.release = &release.Release{
Name: releaseName,
Info: &release.Info{Status: release.StatusDeployed},
}
return r.release, nil
}
// Upgrade helm release
func (r *Fake) Upgrade(chrt *chart.Chart, kubeNamespace, releaseName string, vals map[string]interface{}) (*release.Release, error) {
r.release = &release.Release{
Name: releaseName,
Info: &release.Info{Status: release.StatusDeployed},
}
return r.release, nil
}
// Status of helm release
func (r *Fake) Status(kubeNamespace, releaseName string) (*release.Release, error) {
return r.release, nil
}
// RegistryLogin to docker registry v2
func (r *Fake) RegistryLogin(hostname, username, password string, insecure bool) error {
return nil
}
// RegistryLogout to docker registry v2
func (r *Fake) RegistryLogout(hostname string) error {
return nil
}
// ChartPull helm chart from repo
func (r *Fake) Pull(ref, destination string) error {
return nil
}
// Load helm chart
func (r *Fake) Load(ref, chartPath string) (*chart.Chart, error) {
return nil, nil
}
// GetResources returns allocated resources for the specified release (their current state)
func (r *Fake) GetResources(kubeNamespace, releaseName string) ([]*unstructured.Unstructured, error) {
return r.resources, nil
}
// Package helm chart from repo
func (r *Fake) Package(chartPath, destinationPath, version string) error {
return nil
}
func NewEmptyFake() *Fake {
return &Fake{
release: &release.Release{Info: &release.Info{}},
resources: make([]*unstructured.Unstructured, 0),
}
}
func NewFake(rls *release.Release, resources []*unstructured.Unstructured) *Fake {
return &Fake{
release: rls,
resources: resources,
}
}
// Impl implementation
type Impl struct {
}
// Uninstall helm release
func (r *Impl) Uninstall(kubeNamespace, releaseName string) (*release.UninstallReleaseResponse, error) {
cfg, err := getConfig(kubeNamespace)
if err != nil {
return nil, err
}
uninstall := action.NewUninstall(cfg)
return uninstall.Run(releaseName)
}
// Load helm chart
func (r *Impl) Load(ref, chartPath string) (*chart.Chart, error) {
// check for chart mounted in container
chrt, err := loader.Load(chartsMountPath + ref)
if err == nil {
return chrt, nil
}
// Construct the packed chart path
chartRef, err := action.ParseReference(ref)
if err != nil {
return nil, err
}
_, chartName := filepath.Split(chartRef.Repo)
packedChartPath := chartPath + "/" + chartName + "-" + chartRef.Tag + ".tgz"
return loader.Load(packedChartPath)
}
// Install helm release from packaged chart
func (r *Impl) Install(chrt *chart.Chart, kubeNamespace, releaseName string, vals map[string]interface{}) (*release.Release, error) {
cfg, err := getConfig(kubeNamespace)
if err != nil {
return nil, err
}
install := action.NewInstall(cfg)
install.ReleaseName = releaseName
install.Namespace = kubeNamespace
install.DisableOpenAPIValidation = true
return install.Run(chrt, vals)
}
// Upgrade helm release
func (r *Impl) Upgrade(chrt *chart.Chart, kubeNamespace, releaseName string, vals map[string]interface{}) (*release.Release, error) {
cfg, err := getConfig(kubeNamespace)
if err != nil {
return nil, err
}
upgrade := action.NewUpgrade(cfg)
upgrade.Namespace = kubeNamespace
upgrade.DisableOpenAPIValidation = true
return upgrade.Run(releaseName, chrt, vals)
}
// Status of helm release
func (r *Impl) Status(kubeNamespace, releaseName string) (*release.Release, error) {
cfg, err := getConfig(kubeNamespace)
if err != nil {
return nil, err
}
status := action.NewStatus(cfg)
return status.Run(releaseName)
}
// RegistryLogin to docker registry v2
func (r *Impl) RegistryLogin(hostname, username, password string, insecure bool) error {
cfg, err := getConfig("")
if err != nil {
return err
}
login := action.NewRegistryLogin(cfg)
var buf bytes.Buffer
return login.Run(&buf, hostname, username, password, insecure)
}
// RegistryLogout to docker registry v2
func (r *Impl) RegistryLogout(hostname string) error {
cfg, err := getConfig("")
if err != nil {
return err
}
logout := action.NewRegistryLogout(cfg)
var buf bytes.Buffer
return logout.Run(&buf, hostname)
}
// Package helm chart from repo
func (r *Impl) Package(chartPath, destinationPath, version string) error {
client := action.NewPackage()
if version != "" {
client.Version = version
}
client.Destination = destinationPath
_, err := client.Run(chartPath, nil)
return err
}
// Pull helm chart from repo
func (r *Impl) Pull(ref, destination string) error {
// if chart mounted in container, no need to pull
if _, err := os.Stat(chartsMountPath + ref); err == nil {
return nil
}
chartRef, err := action.ParseReference(ref)
if err != nil {
return err
}
cfg, err := getConfig("")
if err != nil {
return err
}
client := action.NewPullWithOpts(action.WithConfig(cfg))
client.Version = chartRef.Tag
client.DestDir = destination
_, err = client.Run("oci://" + chartRef.Repo)
return err
}
// GetResources returns allocated resources for the specified release (their current state)
func (r *Impl) GetResources(kubeNamespace, releaseName string) ([]*unstructured.Unstructured, error) {
resources := make([]*unstructured.Unstructured, 0)
var rel *release.Release
var config *action.Configuration
var err error
var resourceList kube.ResourceList
config, err = getConfig(kubeNamespace)
if err != nil || config == nil {
return resources, err
}
status := action.NewStatus(config)
rel, err = status.Run(releaseName)
if err != nil {
return resources, err
}
resourceList, err = config.KubeClient.Build(bytes.NewBufferString(rel.Manifest), false)
if err != nil {
return resources, err
}
for _, res := range resourceList {
if err := res.Get(); err != nil {
return resources, err
}
obj := res.Object
if unstr, ok := obj.(*unstructured.Unstructured); ok {
resources = append(resources, unstr)
} else {
return resources, errors.New("invalid runtime object")
}
}
return resources, nil
}