-
Notifications
You must be signed in to change notification settings - Fork 474
/
helm.go
257 lines (223 loc) · 7.52 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
// Copyright 2019 Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file.
//
// 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 framework
import (
"context"
"fmt"
"github.com/gardener/gardener/pkg/chartrenderer"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/mholt/archiver"
"github.com/onsi/ginkgo"
"github.com/pkg/errors"
"k8s.io/helm/pkg/downloader"
"k8s.io/helm/pkg/getter"
"k8s.io/helm/pkg/helm/environment"
"k8s.io/helm/pkg/helm/helmpath"
"k8s.io/helm/pkg/repo"
"os"
"path/filepath"
"strings"
)
const (
stableRepository = "stable"
)
// RenderAndDeployChart downloads a helm chart from helm stable repo url available in resources/repositories and deploys it on the test cluster
func (f *CommonFramework) RenderAndDeployChart(ctx context.Context, k8sClient kubernetes.Interface, c Chart, values map[string]interface{}) error {
helmRepo := Helm(f.ResourcesDir)
err := EnsureRepositoryDirectories(helmRepo)
if err != nil {
return err
}
ginkgo.By("Downloading chart artifacts")
err = f.DownloadChartArtifacts(ctx, helmRepo, f.ChartDir, c.Name, c.Version)
if err != nil {
return errors.Wrapf(err, "unable to download chart artifacts for chart %s with version %s", c.Name, c.Version)
}
return f.DeployChart(ctx, k8sClient, c.Namespace, f.ChartDir, c.ReleaseName, values)
}
// DeployChart deploys it on the test cluster
func (f *CommonFramework) DeployChart(ctx context.Context, k8sClient kubernetes.Interface, namespace, chartRepoDestination, chartNameToDeploy string, values map[string]interface{}) error {
renderer, err := chartrenderer.NewForConfig(k8sClient.RESTConfig())
if err != nil {
return err
}
applier, err := kubernetes.NewApplierForConfig(k8sClient.RESTConfig())
if err != nil {
return err
}
chartApplier := kubernetes.NewChartApplier(renderer, applier)
chartPathToRender := filepath.Join(chartRepoDestination, chartNameToDeploy)
return chartApplier.Apply(ctx, chartPathToRender, namespace, chartNameToDeploy, kubernetes.Values(values), kubernetes.ForceNamespace)
}
// DownloadChartArtifacts downloads a helm chart from helm stable repo url available in resources/repositories
func (f *CommonFramework) DownloadChartArtifacts(ctx context.Context, helm Helm, chartRepoDestination, chartNameToDownload, chartVersionToDownload string) error {
exists, err := Exists(chartRepoDestination)
if err != nil {
return err
}
if !exists {
if err := os.MkdirAll(chartRepoDestination, 0755); err != nil {
return err
}
}
rf, err := repo.LoadRepositoriesFile(helm.RepositoryFile())
if err != nil {
return err
}
if len(rf.Repositories) == 0 {
return ErrNoRepositoriesFound
}
stableRepo := rf.Repositories[0]
var chartPath string
chartDownloaded, err := Exists(filepath.Join(chartRepoDestination, strings.Split(chartNameToDownload, "/")[1]))
if err != nil {
return err
}
if !chartDownloaded {
chartPath, err = downloadChart(ctx, chartNameToDownload, chartVersionToDownload, chartRepoDestination, stableRepo.URL, HelmAccess{
HelmPath: helm,
})
if err != nil {
return err
}
f.Logger.Infof("Chart downloaded to %s", chartPath)
}
return nil
}
// Chart represents a external helm chart with a specific version and namespace
type Chart struct {
Name string
ReleaseName string
Namespace string
Version string
}
// Helm is the home for the HELM repo
type Helm string
// Path returns Helm path with elements appended.
func (h Helm) Path(elem ...string) string {
p := []string{h.String()}
p = append(p, elem...)
return filepath.Join(p...)
}
// Path returns the home for the helm repo with.
func (h Helm) String(elem ...string) string {
return string(h)
}
// Repository returns the path to the local repository.
func (h Helm) Repository() string {
return h.Path("repository")
}
// RepositoryFile returns the path to the repositories.yaml file.
func (h Helm) RepositoryFile() string {
return h.Path("repository", "repositories.yaml")
}
// CacheIndex returns the path to an index for the given named repository.
func (h Helm) CacheIndex(name string) string {
target := fmt.Sprintf("%s-index.yaml", name)
return h.Path("repository", "cache", target)
}
// HelmAccess is a struct that holds the helm home
type HelmAccess struct {
HelmPath Helm
}
// EnsureRepositoryDirectories creates the repository directory which holds the repositories.yaml config file
func EnsureRepositoryDirectories(helm Helm) error {
configDirectories := []string{
helm.String(),
helm.Repository(),
}
for _, p := range configDirectories {
fi, err := os.Stat(p)
if err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(p, os.ModePerm); err != nil {
return errors.Wrapf(err, "unable to create %s", p)
}
continue
}
return err
}
if !fi.IsDir() {
return fmt.Errorf("%s must be a directory", p)
}
}
return nil
}
// downloadChart downloads a native chart with <name> to <downloadDestination> from <stableRepoURL>
func downloadChart(ctx context.Context, name, version, downloadDestination, stableRepoURL string, helmSettings HelmAccess) (string, error) {
providers := getter.All(environment.EnvSettings{})
dl := downloader.ChartDownloader{
Getters: providers,
HelmHome: helmpath.Home(helmSettings.HelmPath),
Out: os.Stdout,
}
err := ensureCacheIndex(ctx, helmSettings, stableRepoURL, providers)
if err != nil {
return "", err
}
// Download the chart
filename, _, err := dl.DownloadTo(name, version, downloadDestination)
if err != nil {
return "", err
}
lname, err := filepath.Abs(filename)
if err != nil {
return "", err
}
err = archiver.Unarchive(lname, downloadDestination)
if err != nil {
return "", err
}
err = os.Remove(lname)
if err != nil {
return "", err
}
return lname, nil
}
func ensureCacheIndex(ctx context.Context, helmSettings HelmAccess, stableRepoURL string, providers getter.Providers) error {
// This will download the cache index file only if it does not exist
stableRepoCacheIndexPath := helmSettings.HelmPath.CacheIndex(stableRepository)
if _, err := os.Stat(stableRepoCacheIndexPath); err != nil {
if os.IsNotExist(err) {
directory := filepath.Dir(stableRepoCacheIndexPath)
err := os.MkdirAll(directory, os.ModePerm)
if err != nil {
return err
}
_, err = downloadCacheIndex(ctx, stableRepoCacheIndexPath, stableRepoURL, providers)
if err != nil {
return err
}
return nil
}
return err
}
return nil
}
// downloadCacheIndex downloads the cache index for repository
func downloadCacheIndex(ctx context.Context, cacheFile, stableRepositoryURL string, providers getter.Providers) (*repo.Entry, error) {
c := repo.Entry{
Name: stableRepository,
URL: stableRepositoryURL,
Cache: cacheFile,
}
r, err := repo.NewChartRepository(&c, providers)
if err != nil {
return nil, err
}
if err := r.DownloadIndexFile(""); err != nil {
return nil, fmt.Errorf("looks like %q is not a valid chart repository or cannot be reached: %s", stableRepositoryURL, err.Error())
}
return &c, nil
}