/
sync.go
296 lines (263 loc) · 9.93 KB
/
sync.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
package syncer
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/bitnami-labs/charts-syncer/api"
"github.com/bitnami-labs/charts-syncer/internal/chart"
"github.com/bitnami-labs/charts-syncer/internal/utils"
"github.com/juju/errors"
"github.com/mkmik/multierror"
"github.com/vmware-tanzu/asset-relocation-tool-for-kubernetes/pkg/mover"
"gopkg.in/yaml.v2"
helm "helm.sh/helm/v3/pkg/action"
helmchart "helm.sh/helm/v3/pkg/chart"
"k8s.io/klog"
)
// SyncPendingCharts syncs the charts not found in the target
//
// It uses topological sort to sync dependencies first.
func (s *Syncer) SyncPendingCharts(names ...string) error {
var errs error
// There might be problems loading all the charts due to missing dependencies,
// invalid/wrong charts in the repository, etc. Therefore, let's warn about
// them instead of blocking the whole sync.
err := s.loadCharts(names...)
if err != nil {
klog.Warningf("There were some problems loading the information of the requested charts: %v", err)
errs = multierror.Append(errs, errors.Trace(err))
}
// NOTE: We are not checking `errs` in purpose. See the comment above.
charts, err := s.topologicalSortCharts()
if err != nil {
return errors.Trace(err)
}
if len(charts) > 1 {
klog.Infof("There are %d charts out of sync!", len(charts))
} else if len(charts) == 1 {
klog.Infof("There is %d chart out of sync!", len(charts))
} else {
klog.Info("There are no charts out of sync!")
return nil
}
for _, ch := range charts {
id := fmt.Sprintf("%s-%s", ch.Name, ch.Version)
klog.Infof("Syncing %q chart...", id)
klog.V(3).Infof("Processing %q chart...", id)
outdir, err := ioutil.TempDir("", "charts-syncer")
if err != nil {
klog.Errorf("unable to create output directory for %q chart: %+v", id, err)
errs = multierror.Append(errs, errors.Trace(err))
continue
}
defer os.RemoveAll(outdir)
hasDeps := len(ch.Dependencies) > 0
workdir, err := ioutil.TempDir("", "charts-syncer")
if err != nil {
klog.Errorf("unable to create work directory for %q chart: %+v", id, err)
errs = multierror.Append(errs, errors.Trace(err))
continue
}
defer os.RemoveAll(workdir)
// Some client Upload() methods needs this info
metadata := &helmchart.Metadata{
Name: ch.Name,
Version: ch.Version,
}
var packagedChartPath string
// If any of the source or target objects contains an intermediate bundles path it means we are running a partial
// sync. Either from a repo to an intermediate dir, or from an intermediate dir to a repo.
intermediateScenario := s.source.GetIntermediateBundlesPath() != "" || s.target.GetIntermediateBundlesPath() != ""
if s.relocateContainerImages || intermediateScenario {
packagedChartPath, err = s.SyncWithRelok8s(ch, outdir)
if err != nil {
errs = multierror.Append(errs, errors.Annotatef(err, "unable to move chart %q with relok8s", id))
continue
}
} else {
packagedChartPath, err = s.SyncWithChartsSyncer(ch, id, workdir, outdir, hasDeps)
if err != nil {
errs = multierror.Append(errs, errors.Annotatef(err, "unable to move chart %q with charts-syncer", id))
continue
}
}
if s.dryRun {
klog.Infof("dry-run: Uploading %q chart", id)
continue
}
klog.V(3).Infof("Uploading %q chart...", id)
if err := s.cli.dst.Upload(packagedChartPath, metadata); err != nil {
klog.Errorf("unable to upload %q chart: %+v", id, err)
errs = multierror.Append(errs, errors.Trace(err))
continue
}
}
return errors.Trace(errs)
}
// SyncWithRelok8s will take a local packaged chart, a container registry and a container repository and will rewrite the chart
// updating the images in values.yaml. The local chart must include an image hints file so relok8s library knows how to
// update the images
func (s *Syncer) SyncWithRelok8s(chart *Chart, outdir string) (string, error) {
req, packagedChartPath := getRelok8sMoveRequest(s.source, s.target, chart, outdir)
chartMover, err := mover.NewChartMover(req)
if err != nil {
klog.Errorf("unable to create chart mover: %+v", err)
return "", errors.Trace(err)
}
if err = chartMover.Move(); err != nil {
klog.Errorf("unable to move chart %s:%s: %+v", chart.Name, chart.Version, err)
return "", errors.Trace(err)
}
return packagedChartPath, nil
}
func (s *Syncer) SyncWithChartsSyncer(ch *Chart, id, workdir, outdir string, hasDeps bool) (string, error) {
if err := utils.Untar(ch.TgzPath, workdir); err != nil {
klog.Errorf("unable to uncompress %q chart: %+v", id, err)
return "", errors.Trace(errors.Annotatef(err, "uncompressing %q chart", id))
}
chartPath := path.Join(workdir, ch.Name)
if err := chart.ChangeReferences(chartPath, ch.Name, ch.Version, s.source, s.target); err != nil {
klog.Errorf("unable to process %q chart: %+v", id, err)
return "", errors.Trace(err)
}
// Update deps
if hasDeps {
klog.V(3).Infof("Building %q dependencies", id)
if err := chart.BuildDependencies(chartPath, s.cli.dst, s.source.GetRepo(), s.target.GetRepo()); err != nil {
klog.Errorf("unable to build %q chart dependencies: %+v", id, err)
return "", errors.Trace(err)
}
}
// Read final chart metadata
configFilePath := fmt.Sprintf("%s/Chart.yaml", chartPath)
chartConfig, err := ioutil.ReadFile(configFilePath)
if err != nil {
klog.Errorf("unable to read %q metadata: %+v", id, err)
return "", errors.Trace(err)
}
metadata := &helmchart.Metadata{}
if err = yaml.Unmarshal(chartConfig, metadata); err != nil {
klog.Errorf("unable to decode %q metadata: %+v", id, err)
return "", errors.Trace(err)
}
// Package chart again
klog.V(3).Infof("Packaging %q", id)
pkgCli := helm.NewPackage()
pkgCli.Destination = outdir
packagedChartPath, err := pkgCli.Run(chartPath, nil)
if err != nil {
klog.Errorf("unable to package %q chart: %+v", id, err)
return "", errors.Trace(err)
}
return packagedChartPath, nil
}
func getRelok8sMoveRequest(source *api.Source, target *api.Target, chart *Chart, outdir string) (*mover.ChartMoveRequest, string) {
if target.GetIntermediateBundlesPath() != "" {
// airgap scenario step 1: SOURCE REPO => Intermediate bundles path
packagedChartPath := filepath.Join(outdir, fmt.Sprintf("%s-%s.bundle.tar", chart.Name, chart.Version))
return relok8sBundleSaveReq(chart.TgzPath, packagedChartPath, source.GetContainers().GetAuth()), packagedChartPath
} else if source.GetIntermediateBundlesPath() != "" {
// airgap scenario step 2: Intermediate bundles path => TARGET REPO
// Second step of intermediate process
// Once https://github.com/vmware-tanzu/asset-relocation-tool-for-kubernetes/issues/94 is solved, we could
// specify the name we want for the output file. Until then, we should keep using this template thing
outputChartPath := filepath.Join(outdir, "%s-%s.relocated.tgz")
packagedChartPath := filepath.Join(outdir, fmt.Sprintf("%s-%s.relocated.tgz", chart.Name, chart.Version))
return relok8sBundleLoadReq(
chart.TgzPath, outputChartPath,
target.GetContainerRegistry(), target.GetContainerRepository(),
target.GetContainers().GetAuth()), packagedChartPath
} else {
// Direct syncing, SOURCE_REPO => TARGET_REPO
// Once https://github.com/vmware-tanzu/asset-relocation-tool-for-kubernetes/issues/94 is solved, we could
// specify the name we want for the output file. Until then, we should keep using this template thing
outputChartPath := filepath.Join(outdir, "%s-%s.tgz")
packagedChartPath := filepath.Join(outdir, fmt.Sprintf("%s-%s.tgz", chart.Name, chart.Version))
return relok8sMoveReq(chart.TgzPath, outputChartPath, target.GetContainerRegistry(), target.GetContainerRepository(),
source.GetContainers().GetAuth(), target.GetContainers().GetAuth()), packagedChartPath
}
}
func relok8sMoveReq(sourcePath, targetPath, containerRegistry, containerRepository string, sourceAuth, targetAuth *api.Containers_ContainerAuth) *mover.ChartMoveRequest {
req := &mover.ChartMoveRequest{
Source: mover.Source{
Chart: mover.ChartSpec{
Local: &mover.LocalChart{
Path: sourcePath,
},
},
ContainersAuth: chartsSyncerToRelok8sAuth(sourceAuth),
},
Target: mover.Target{
Rules: mover.RewriteRules{
Registry: containerRegistry,
RepositoryPrefix: containerRepository,
ForcePush: true,
},
Chart: mover.ChartSpec{
Local: &mover.LocalChart{
Path: targetPath,
},
},
ContainersAuth: chartsSyncerToRelok8sAuth(targetAuth),
},
}
return req
}
func relok8sBundleSaveReq(sourcePath, targetPath string, containerSourceAuth *api.Containers_ContainerAuth) *mover.ChartMoveRequest {
req := &mover.ChartMoveRequest{
Source: mover.Source{
Chart: mover.ChartSpec{
Local: &mover.LocalChart{
Path: sourcePath,
},
},
ContainersAuth: chartsSyncerToRelok8sAuth(containerSourceAuth),
},
Target: mover.Target{
Chart: mover.ChartSpec{
IntermediateBundle: &mover.IntermediateBundle{
Path: targetPath,
},
},
},
}
return req
}
func relok8sBundleLoadReq(sourcePath, targetPath, containerRegistry, containerRepository string, containerTargetAuth *api.Containers_ContainerAuth) *mover.ChartMoveRequest {
req := &mover.ChartMoveRequest{
Source: mover.Source{
Chart: mover.ChartSpec{
IntermediateBundle: &mover.IntermediateBundle{
Path: sourcePath,
},
},
},
Target: mover.Target{
Rules: mover.RewriteRules{
Registry: containerRegistry,
RepositoryPrefix: containerRepository,
ForcePush: true,
},
Chart: mover.ChartSpec{
Local: &mover.LocalChart{
Path: targetPath,
},
},
ContainersAuth: chartsSyncerToRelok8sAuth(containerTargetAuth),
},
}
return req
}
// Translates charts syncer authentication settings into relok8s authentication
func chartsSyncerToRelok8sAuth(containerAuth *api.Containers_ContainerAuth) (relok8sAuth *mover.ContainersAuth) {
if containerAuth == nil {
return nil
}
return &mover.ContainersAuth{
Credentials: &mover.OCICredentials{
Username: containerAuth.Username, Password: containerAuth.Password, Server: containerAuth.Registry,
},
}
}