@@ -4,16 +4,15 @@ import (
4
4
"bytes"
5
5
"encoding/json"
6
6
"fmt"
7
- "io/ioutil"
8
- "net/url"
9
- "os"
10
- "path"
11
-
12
7
"github.com/juju/errors"
13
8
"github.com/mkmik/multierror"
14
9
"helm.sh/helm/v3/pkg/chart"
15
10
"helm.sh/helm/v3/pkg/provenance"
11
+ "io/ioutil"
16
12
"k8s.io/klog"
13
+ "net/url"
14
+ "os"
15
+ "path"
17
16
"sigs.k8s.io/yaml"
18
17
19
18
"github.com/bitnami-labs/charts-syncer/api"
@@ -65,7 +64,7 @@ func GetChartLock(chartPath string) (*chart.Lock, error) {
65
64
return lock , nil
66
65
}
67
66
68
- // GetChartDependencies returns the chart chart.Dependencies from a chart in tgz format.
67
+ // GetChartDependencies returns the chart dependencies from a chart in tgz format.
69
68
func GetChartDependencies (filepath string , name string ) ([]* chart.Dependency , error ) {
70
69
// Create temporary working directory
71
70
chartPath , err := ioutil .TempDir ("" , "charts-syncer" )
@@ -112,9 +111,9 @@ func GetLockAPIVersion(chartPath string) (string, error) {
112
111
113
112
// BuildDependencies updates the chart dependencies and their repository references in the provided chart path
114
113
//
115
- // It reads the lock file to download the versions from the target
116
- // chart repository (it assumes all charts are stored in a single repo).
117
- func BuildDependencies ( chartPath string , r client. ChartsReader , sourceRepo , targetRepo * api. Repo , replaceDependencyRepo bool ) error {
114
+ // It reads the lock file to download the versions from the target chart repository
115
+ func BuildDependencies ( chartPath string , r client. ChartsReader , sourceRepo , targetRepo * api. Repo , t map [ uint32 ]client. ChartsReaderWriter , syncTrusted , ignoreTrusted [] * api. Repo ) error {
116
+
118
117
// Build deps manually for OCI as helm does not support it yet
119
118
if err := os .RemoveAll (path .Join (chartPath , "charts" )); err != nil {
120
119
return errors .Trace (err )
@@ -138,13 +137,14 @@ func BuildDependencies(chartPath string, r client.ChartsReader, sourceRepo, targ
138
137
if apiVersion == "" {
139
138
return nil
140
139
}
140
+
141
141
switch apiVersion {
142
142
case APIV1 :
143
- if err := updateRequirementsFile (chartPath , lock , sourceRepo , targetRepo , replaceDependencyRepo ); err != nil {
143
+ if err := updateRequirementsFile (chartPath , lock , sourceRepo , targetRepo , syncTrusted , ignoreTrusted ); err != nil {
144
144
return errors .Trace (err )
145
145
}
146
146
case APIV2 :
147
- if err := updateChartMetadataFile (chartPath , lock , sourceRepo , targetRepo , replaceDependencyRepo ); err != nil {
147
+ if err := updateChartMetadataFile (chartPath , lock , sourceRepo , targetRepo , syncTrusted , ignoreTrusted ); err != nil {
148
148
return errors .Trace (err )
149
149
}
150
150
default :
@@ -158,7 +158,22 @@ func BuildDependencies(chartPath string, r client.ChartsReader, sourceRepo, targ
158
158
id := fmt .Sprintf ("%s-%s" , dep .Name , dep .Version )
159
159
klog .V (4 ).Infof ("Building %q chart dependency" , id )
160
160
161
- depTgz , err := r .Fetch (dep .Name , dep .Version )
161
+ var repoClient client.ChartsReader = nil
162
+
163
+ depRepo := api.Repo {
164
+ Url : dep .Repository ,
165
+ }
166
+
167
+ //if the repo is trusted and won't be synced - we download the dependency from it (source)
168
+ if utils .ShouldIgnoreRepo (depRepo , syncTrusted , ignoreTrusted ) {
169
+ repoClient = t [utils .GetRepoLocationId (dep .Repository )]
170
+ } else {
171
+ //otherwise we download it from the destination repo
172
+ repoClient = r
173
+ }
174
+
175
+ depTgz , err := repoClient .Fetch (dep .Name , dep .Version )
176
+
162
177
if err != nil {
163
178
klog .Warningf ("Failed fetching %q chart. The dependencies processing will remain incomplete." , id )
164
179
errs = multierror .Append (errs , errors .Annotatef (err , "fetching %q chart" , id ))
@@ -179,7 +194,7 @@ func BuildDependencies(chartPath string, r client.ChartsReader, sourceRepo, targ
179
194
180
195
// updateChartMetadataFile updates the dependencies in Chart.yaml
181
196
// For helm v3 dependency management
182
- func updateChartMetadataFile (chartPath string , lock * chart.Lock , sourceRepo , targetRepo * api.Repo , replaceDependencyRepo bool ) error {
197
+ func updateChartMetadataFile (chartPath string , lock * chart.Lock , sourceRepo , targetRepo * api.Repo , syncTrusted , ignoreTrusted [] * api. Repo ) error {
183
198
chartFile := path .Join (chartPath , ChartFilename )
184
199
chartYamlContent , err := ioutil .ReadFile (chartFile )
185
200
if err != nil {
@@ -191,8 +206,15 @@ func updateChartMetadataFile(chartPath string, lock *chart.Lock, sourceRepo, tar
191
206
return errors .Annotatef (err , "error unmarshaling %s file" , chartFile )
192
207
}
193
208
for _ , dep := range chartMetadata .Dependencies {
194
- // Maybe there are dependencies from other chart repos. In this case we don't want to replace
195
- // the repository.
209
+ // Maybe there are dependencies from other chart repos. We replace them or not depending on what we have in
210
+ // source.ignoreTrustedRepos and target.syncTrustedRepos (the logic can be found in utils.ShouldIgnoreRepo)
211
+ r := api.Repo {
212
+ Url : dep .Repository ,
213
+ }
214
+
215
+ //ignore repo means don't replace it, don't ignore - means "replace it" - use negation to achieve it
216
+ replaceDependencyRepo := ! utils .ShouldIgnoreRepo (r , syncTrusted , ignoreTrusted )
217
+
196
218
if dep .Repository == sourceRepo .GetUrl () || replaceDependencyRepo {
197
219
repoUrl , err := getDependencyRepoURL (targetRepo )
198
220
if err != nil {
@@ -206,15 +228,15 @@ func updateChartMetadataFile(chartPath string, lock *chart.Lock, sourceRepo, tar
206
228
if err := writeChartFile (dest , chartMetadata ); err != nil {
207
229
return errors .Trace (err )
208
230
}
209
- if err := updateLockFile (chartPath , lock , chartMetadata .Dependencies , sourceRepo , targetRepo , false , replaceDependencyRepo ); err != nil {
231
+ if err := updateLockFile (chartPath , lock , chartMetadata .Dependencies , sourceRepo , targetRepo , false , syncTrusted , ignoreTrusted ); err != nil {
210
232
return errors .Trace (err )
211
233
}
212
234
return nil
213
235
}
214
236
215
237
// updateRequirementsFile returns the full list of dependencies and the list of missing dependencies.
216
238
// For helm v2 dependency management
217
- func updateRequirementsFile (chartPath string , lock * chart.Lock , sourceRepo , targetRepo * api.Repo , replaceDependencyRepo bool ) error {
239
+ func updateRequirementsFile (chartPath string , lock * chart.Lock , sourceRepo , targetRepo * api.Repo , syncTrusted , ignoreTrusted [] * api. Repo ) error {
218
240
requirementsFile := path .Join (chartPath , RequirementsFilename )
219
241
requirements , err := ioutil .ReadFile (requirementsFile )
220
242
if err != nil {
@@ -227,8 +249,15 @@ func updateRequirementsFile(chartPath string, lock *chart.Lock, sourceRepo, targ
227
249
return errors .Annotatef (err , "error unmarshaling %s file" , requirementsFile )
228
250
}
229
251
for _ , dep := range deps .Dependencies {
230
- // Maybe there are dependencies from other chart repos. In this case we don't want to replace
231
- // the repository.
252
+ // Maybe there are dependencies from other chart repos. We replace them or not depending on what we have in
253
+ // source.ignoreTrustedRepos and target.syncTrustedRepos (the logic can be found in utils.ShouldIgnoreRepo)
254
+ r := api.Repo {
255
+ Url : dep .Repository ,
256
+ }
257
+
258
+ //ignore repo means don't replace it, don't ignore - means "replace it" - use negation to achieve it
259
+ replaceDependencyRepo := ! utils .ShouldIgnoreRepo (r , syncTrusted , ignoreTrusted )
260
+
232
261
// For example, old charts pointing to helm/charts repo
233
262
if dep .Repository == sourceRepo .GetUrl () || replaceDependencyRepo {
234
263
repoUrl , err := getDependencyRepoURL (targetRepo )
@@ -243,15 +272,25 @@ func updateRequirementsFile(chartPath string, lock *chart.Lock, sourceRepo, targ
243
272
if err := writeChartFile (dest , deps ); err != nil {
244
273
return errors .Trace (err )
245
274
}
246
- if err := updateLockFile (chartPath , lock , deps .Dependencies , sourceRepo , targetRepo , true , replaceDependencyRepo ); err != nil {
275
+ if err := updateLockFile (chartPath , lock , deps .Dependencies , sourceRepo , targetRepo , true , syncTrusted , ignoreTrusted ); err != nil {
247
276
return errors .Trace (err )
248
277
}
249
278
return nil
250
279
}
251
280
252
281
// updateLockFile updates the lock file with the new registry
253
- func updateLockFile (chartPath string , lock * chart.Lock , deps []* chart.Dependency , sourceRepo * api.Repo , targetRepo * api.Repo , legacyLockfile , replaceDependencyRepo bool ) error {
282
+ func updateLockFile (chartPath string , lock * chart.Lock , deps []* chart.Dependency , sourceRepo * api.Repo , targetRepo * api.Repo , legacyLockfile bool , syncTrusted , ignoreTrusted [] * api. Repo ) error {
254
283
for _ , dep := range lock .Dependencies {
284
+
285
+ // Maybe there are dependencies from other chart repos. We replace them or not depending on what we have in
286
+ // source.ignoreTrustedRepos and target.syncTrustedRepos (the logic can be found in utils.ShouldIgnoreRepo)
287
+ r := api.Repo {
288
+ Url : dep .Repository ,
289
+ }
290
+
291
+ //ignore repo means don't replace it, don't ignore - means "replace it" - use negation to achieve it
292
+ replaceDependencyRepo := ! utils .ShouldIgnoreRepo (r , syncTrusted , ignoreTrusted )
293
+
255
294
if dep .Repository == sourceRepo .GetUrl () || replaceDependencyRepo {
256
295
repoUrl , err := getDependencyRepoURL (targetRepo )
257
296
if err != nil {
0 commit comments