-
Notifications
You must be signed in to change notification settings - Fork 153
/
charts.go
305 lines (251 loc) · 8.24 KB
/
charts.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
package helm
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
"sort"
sourcev1beta1 "github.com/fluxcd/source-controller/api/v1beta1"
"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/repo"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
pb "github.com/weaveworks/weave-gitops/pkg/api/profiles"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
//counterfeiter:generate . HelmRepoManager
type HelmRepoManager interface {
ListCharts(ctx context.Context, hr *sourcev1beta1.HelmRepository, pred ChartPredicate) ([]*pb.Profile, error)
GetValuesFile(ctx context.Context, helmRepo *sourcev1beta1.HelmRepository, c *ChartReference, filename string) ([]byte, error)
}
// ProfileAnnotation is the annotation that Helm charts must have to indicate
// that they provide a Profile.
const ProfileAnnotation = "weave.works/profile"
// LayerAnnotation specifies profile application order.
// Profiles are sorted by layer and those at a higher "layer" are only installed after
// lower layers have successfully installed and started.
const LayerAnnotation = "weave.works/layer"
// NewRepoManager creates and returns a new RepoManager.
func NewRepoManager(kc client.Client, cacheDir string) *RepoManager {
return &RepoManager{
Client: kc,
CacheDir: cacheDir,
envSettings: &cli.EnvSettings{
Debug: true,
RepositoryCache: cacheDir,
RepositoryConfig: path.Join(cacheDir, "/repository.yaml"),
},
}
}
// RepoManager implements HelmRepoManager interface using the Helm library packages.
type RepoManager struct {
client.Client
CacheDir string
envSettings *cli.EnvSettings
}
// ChartReference is a Helm chart reference
type ChartReference struct {
Chart string
Version string
}
// DefaultChartGetter provides default ways to get a chart index.yaml based on
// the URL scheme.
var defaultChartGetters = getter.Providers{
getter.Provider{
Schemes: []string{"http", "https"},
New: getter.NewHTTPGetter,
},
}
type ChartPredicate func(*repo.ChartVersion) bool
// Profiles is a predicate for scanning charts with the ProfileAnnotation.
var Profiles = func(v *repo.ChartVersion) bool {
return hasAnnotation(v.Metadata, ProfileAnnotation)
}
// ListCharts filters charts using the provided predicate.
func (h *RepoManager) ListCharts(ctx context.Context, hr *sourcev1beta1.HelmRepository, pred ChartPredicate) ([]*pb.Profile, error) {
chartRepo, err := fetchIndexFile(hr.Status.URL)
if err != nil {
return nil, fmt.Errorf("fetching profiles from HelmRepository %s/%s: %w",
hr.GetName(), hr.GetNamespace(), err)
}
ps := make(map[string]*pb.Profile)
for name, versions := range chartRepo.Entries {
for _, v := range versions {
if pred(v) {
// if already added, update the versions array
if p, ok := ps[name]; ok {
p.AvailableVersions = append(p.AvailableVersions, v.Version)
} else { // otherwise create a new profile and add to map
p = &pb.Profile{
Name: name,
Home: v.Home,
Sources: v.Sources,
Description: v.Description,
Keywords: v.Keywords,
Icon: v.Icon,
KubeVersion: v.KubeVersion,
HelmRepository: &pb.HelmRepository{
Name: hr.Name,
Namespace: hr.Namespace,
},
Layer: getLayer(v.Annotations),
}
for _, m := range v.Maintainers {
p.Maintainers = append(p.Maintainers, &pb.Maintainer{
Name: m.Name,
Email: m.Email,
Url: m.URL,
})
}
p.AvailableVersions = append(p.AvailableVersions, v.Version)
ps[name] = p
}
}
}
}
var profiles []*pb.Profile
for _, p := range ps {
sort.Strings(p.AvailableVersions)
profiles = append(profiles, p)
}
return profiles, nil
}
// GetValuesFile fetches the value file from a chart.
func (h *RepoManager) GetValuesFile(ctx context.Context, helmRepo *sourcev1beta1.HelmRepository, c *ChartReference, filename string) ([]byte, error) {
if err := h.updateCache(ctx, helmRepo); err != nil {
return nil, fmt.Errorf("updating cache: %w", err)
}
chart, err := h.loadChart(ctx, helmRepo, c)
if err != nil {
return nil, fmt.Errorf("loading %s from chart: %w", filename, err)
}
for _, v := range chart.Raw {
if v.Name == filename {
return v.Data, nil
}
}
return nil, fmt.Errorf("failed to find file: %s", filename)
}
func (h *RepoManager) updateCache(ctx context.Context, helmRepo *sourcev1beta1.HelmRepository) error {
entry, err := h.entryForRepository(ctx, helmRepo)
if err != nil {
return fmt.Errorf("failed to build repository entry: %w", err)
}
r, err := repo.NewChartRepository(entry, defaultChartGetters)
if err != nil {
return fmt.Errorf("error creating chart repository: %w", err)
}
r.CachePath = h.CacheDir
if _, err := r.DownloadIndexFile(); err != nil {
return fmt.Errorf("error downloading index file: %w", err)
}
return nil
}
func (h *RepoManager) loadChart(ctx context.Context, helmRepo *sourcev1beta1.HelmRepository, c *ChartReference) (*chart.Chart, error) {
o, err := h.chartPathOptionsFromRepository(ctx, helmRepo, c)
if err != nil {
return nil, fmt.Errorf("failed to configure client: %w", err)
}
chartLocation, err := o.LocateChart(c.Chart, h.envSettings)
if err != nil {
return nil, fmt.Errorf("locating chart %q: %w", c.Chart, err)
}
chart, err := loader.Load(chartLocation)
if err != nil {
return nil, fmt.Errorf("failed to load chart %q: %w", c.Chart, err)
}
return chart, nil
}
func (h *RepoManager) chartPathOptionsFromRepository(ctx context.Context, helmRepo *sourcev1beta1.HelmRepository, c *ChartReference) (*action.ChartPathOptions, error) {
// TODO: This should probably use Verify: true
co := &action.ChartPathOptions{
RepoURL: helmRepo.Spec.URL,
Version: c.Version,
PassCredentialsAll: helmRepo.Spec.PassCredentials,
}
if helmRepo.Spec.SecretRef != nil {
username, password, err := credsForRepository(ctx, h.Client, helmRepo)
if err != nil {
return nil, err
}
co.Username = username
co.Password = password
}
return co, nil
}
func (h *RepoManager) entryForRepository(ctx context.Context, helmRepo *sourcev1beta1.HelmRepository) (*repo.Entry, error) {
entry := &repo.Entry{
Name: helmRepo.GetName() + "-" + helmRepo.GetNamespace(),
URL: helmRepo.Spec.URL,
}
if helmRepo.Spec.SecretRef != nil {
username, password, err := credsForRepository(ctx, h.Client, helmRepo)
if err != nil {
return nil, err
}
entry.Username = username
entry.Password = password
}
return entry, nil
}
func credsForRepository(ctx context.Context, kc client.Client, hr *sourcev1beta1.HelmRepository) (string, string, error) {
var secret corev1.Secret
if err := kc.Get(ctx, types.NamespacedName{Name: hr.Spec.SecretRef.Name, Namespace: hr.Namespace}, &secret); err != nil {
return "", "", fmt.Errorf("repository authentication: %w", err)
}
return string(secret.Data["username"]), string(secret.Data["password"]), nil
}
func fetchIndexFile(chartURL string) (*repo.IndexFile, error) {
if hostname := os.Getenv("SOURCE_CONTROLLER_LOCALHOST"); hostname != "" {
u, err := url.Parse(chartURL)
if err != nil {
return nil, err
}
u.Host = hostname
chartURL = u.String()
}
u, err := url.Parse(chartURL)
if err != nil {
return nil, fmt.Errorf("error parsing URL %q: %w", chartURL, err)
}
c, err := defaultChartGetters.ByScheme(u.Scheme)
if err != nil {
return nil, fmt.Errorf("no provider for scheme %q: %w", u.Scheme, err)
}
res, err := c.Get(u.String())
if err != nil {
return nil, fmt.Errorf("error fetching index file: %w", err)
}
b, err := ioutil.ReadAll(res)
if err != nil {
return nil, fmt.Errorf("error reading response: %w", err)
}
i := &repo.IndexFile{}
if err := yaml.Unmarshal(b, i); err != nil {
return nil, fmt.Errorf("error unmarshaling chart response: %w", err)
}
if i.APIVersion == "" {
return nil, repo.ErrNoAPIVersion
}
i.SortEntries()
return i, nil
}
func getLayer(annotations map[string]string) string {
return annotations[LayerAnnotation]
}
func hasAnnotation(cm *chart.Metadata, name string) bool {
for k := range cm.Annotations {
if k == name {
return true
}
}
return false
}