/
index.go
273 lines (240 loc) · 7.12 KB
/
index.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
package syncer
import (
"fmt"
"sort"
"time"
"github.com/Masterminds/semver/v3"
"github.com/juju/errors"
"github.com/mkmik/multierror"
"github.com/philopon/go-toposort"
"k8s.io/klog"
"github.com/bitnami/charts-syncer/internal/chart"
"github.com/bitnami/charts-syncer/internal/utils"
)
// Chart describes a chart, including dependencies
type Chart struct {
Name string
Version string
Dependencies []string
TgzPath string
}
// ChartIndex is a map linking a chart reference with its Chart
type ChartIndex map[string]*Chart
// func (i ChartIndex) Equal(ii ChartIndex) bool {
// var missing []string
// for ni, ich := range i {
// if iich, ok := ii[ni]; !ok {
// return false
// }
// missing = append(missing, ni)
// }
// }
// getIndex returns the chart index
func (s *Syncer) getIndex() ChartIndex {
if s.index == nil {
s.index = make(ChartIndex)
}
return s.index
}
// Add adds a chart in the index
func (i ChartIndex) Add(id string, chart *Chart) error {
if _, ok := i[id]; ok {
return errors.Errorf("%q is already indexed", id)
}
i[id] = chart
return nil
}
// Get returns an index chart
func (i ChartIndex) Get(id string) *Chart {
if c, ok := i[id]; ok {
return c
}
return nil
}
// loadCharts loads the charts map into the index from the source repo
func (s *Syncer) loadCharts(charts ...string) error {
if len(charts) == 0 {
if !s.autoDiscovery {
return errors.Errorf("unable to discover charts to sync")
}
srcCharts, err := s.cli.src.List()
if err != nil {
return errors.Trace(err)
}
if len(srcCharts) == 0 {
return errors.Errorf("not found charts to sync")
}
charts = srcCharts
}
// Sort chart names
sort.Strings(charts)
// Create basic layout for date and parse flag to time type
publishingThreshold, err := utils.GetDateThreshold(s.fromDate)
if err != nil {
return errors.Trace(err)
}
klog.V(4).Infof("Publishing threshold set to %q", publishingThreshold.String())
// Iterate over charts in source index
var errs error
for _, name := range charts {
if shouldSkipChart(name, s.skipCharts) {
klog.V(3).Infof("Indexing %q charts SKIPPED...", name)
continue
}
versions, err := s.cli.src.ListChartVersions(name)
if err != nil {
errs = multierror.Append(errs, errors.Trace(err))
continue
}
klog.V(5).Infof("Found %d versions for %q chart: %v", len(versions), name, versions)
klog.V(3).Infof("Indexing %q charts...", name)
if s.latestVersionOnly {
vs := make([]*semver.Version, len(versions))
for i, r := range versions {
v, err := semver.NewVersion(r)
if err != nil {
return errors.Trace(err)
}
vs[i] = v
}
sort.Sort(semver.Collection(vs))
// The last element of the array is the latest version
version := vs[len(vs)-1].String()
if err := s.processVersion(name, version, publishingThreshold); err != nil {
klog.Warningf("Failed processing %s:%s chart. The index will remain incomplete.", name, version)
errs = multierror.Append(errs, errors.Trace(err))
continue
}
} else {
for _, version := range versions {
if err := s.processVersion(name, version, publishingThreshold); err != nil {
klog.Warningf("Failed processing %s:%s chart. The index will remain incomplete.", name, version)
errs = multierror.Append(errs, errors.Trace(err))
continue
}
}
}
}
return errors.Trace(errs)
}
// processVersion takes care of loading a specific version of the chart into the index
func (s *Syncer) processVersion(name, version string, publishingThreshold time.Time) error {
details, err := s.cli.src.GetChartDetails(name, version)
if err != nil {
return err
}
id := fmt.Sprintf("%s-%s", name, version)
klog.V(5).Infof("Details for %q chart: %+v", id, details)
if details.PublishedAt.Before(publishingThreshold) {
klog.V(5).Infof("Skipping %q chart: Published before %q", id, publishingThreshold.String())
return nil
}
if ok, err := s.cli.dst.Has(name, version); err != nil {
klog.Errorf("unable to explore target repo to check %q chart: %v", id, err)
return err
} else if ok {
klog.V(5).Infof("Skipping %q chart: Already synced", id)
return nil
}
if ch := s.getIndex().Get(id); ch != nil {
klog.V(5).Infof("Skipping %q chart: Already indexed", id)
return nil
}
if err := s.loadChart(name, version); err != nil {
klog.Errorf("unable to load %q chart: %v", id, err)
return err
}
return nil
}
// loadChart loads a chart in the chart index map
func (s *Syncer) loadChart(name string, version string) error {
id := fmt.Sprintf("%s-%s", name, version)
// loadChart is a recursive function and it will be invoked again for each
// dependency.
//
// It makes sense that different "tier1" charts use the same "tier2" chart
// dependencies. This check will make the method to skip already indexed
// charts.
//
// Example:
// `wordpress` is a "tier1" chart that depends on the "tier2" charts `mariadb`
// and `common`. `magento` is a "tier1" chart that depends on the "tier2"
// charts `mariadb` and `elasticsearch`.
//
// If we run charts-syncer for `wordpress` and `magento`, this check will
// avoid re-indexing `mariadb` twice.
if ch := s.getIndex().Get(id); ch != nil {
klog.V(5).Infof("Skipping %q chart: Already indexed", id)
return nil
}
// In the same way, dependencies may already exist in the target chart
// repository.
if ok, err := s.cli.dst.Has(name, version); err != nil {
return errors.Errorf("unable to explore target repo to check %q chart: %v", id, err)
} else if ok {
klog.V(5).Infof("Skipping %q chart: Already synced", id)
return nil
}
tgz, err := s.cli.src.Fetch(name, version)
if err != nil {
return errors.Trace(err)
}
ch := &Chart{
Name: name,
Version: version,
TgzPath: tgz,
}
if !s.skipDependencies {
deps, err := chart.GetChartDependencies(tgz, name)
if err != nil {
return errors.Trace(err)
}
if len(deps) == 0 {
klog.V(4).Infof("Indexing %q chart", id)
return errors.Trace(s.getIndex().Add(id, ch))
}
var errs error
for _, dep := range deps {
depID := fmt.Sprintf("%s-%s", dep.Name, dep.Version)
if err := s.loadChart(dep.Name, dep.Version); err != nil {
errs = multierror.Append(errs, errors.Annotatef(err, "invalid %q chart dependency", depID))
continue
}
ch.Dependencies = append(ch.Dependencies, depID)
}
if errs != nil {
return errors.Trace(errs)
}
}
klog.V(4).Infof("Indexing %q chart", id)
return errors.Trace(s.getIndex().Add(id, ch))
}
// topologicalSortCharts returns the indexed charts, topologically sorted.
func (s *Syncer) topologicalSortCharts() ([]*Chart, error) {
graph := toposort.NewGraph(len(s.getIndex()))
for name := range s.getIndex() {
graph.AddNode(name)
}
for name, ch := range s.getIndex() {
for _, dep := range ch.Dependencies {
graph.AddEdge(dep, name)
}
}
result, ok := graph.Toposort()
if !ok {
return nil, errors.Errorf("dependency cycle detected in charts")
}
charts := make([]*Chart, len(result))
for i, id := range result {
charts[i] = s.getIndex().Get(id)
}
return charts, nil
}
func shouldSkipChart(chartName string, skippedCharts []string) bool {
for _, s := range skippedCharts {
if s == chartName {
return true
}
}
return false
}