/
helm_utils.go
111 lines (97 loc) · 3.22 KB
/
helm_utils.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
// 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
// Deprecated: this is the deprecated gardener testframework.
// Use gardener/test/framework instead
package framework
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/mholt/archiver"
"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"
)
const (
stableRepository = "stable"
)
// 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
}