@@ -231,52 +231,6 @@ func (r *HelmChartReconciler) getSource(ctx context.Context, chart sourcev1.Helm
231231
232232func (r * HelmChartReconciler ) reconcileFromHelmRepository (ctx context.Context ,
233233 repository sourcev1.HelmRepository , chart sourcev1.HelmChart , force bool ) (sourcev1.HelmChart , error ) {
234- cv , err := helm .GetDownloadableChartVersionFromIndex (r .Storage .LocalPath (* repository .GetArtifact ()),
235- chart .Spec .Chart , chart .Spec .Version )
236- if err != nil {
237- return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPullFailedReason , err .Error ()), err
238- }
239-
240- // Return early if the revision is still the same as the current artifact
241- newArtifact := r .Storage .NewArtifactFor (chart .Kind , chart .GetObjectMeta (), cv .Version ,
242- fmt .Sprintf ("%s-%s.tgz" , cv .Name , cv .Version ))
243- if ! force && sourcev1 .InReadyCondition (chart .Status .Conditions ) && chart .GetArtifact ().HasRevision (newArtifact .Revision ) {
244- if newArtifact .URL != repository .GetArtifact ().URL {
245- r .Storage .SetArtifactURL (chart .GetArtifact ())
246- chart .Status .URL = r .Storage .SetHostname (chart .Status .URL )
247- }
248- return chart , nil
249- }
250-
251- // TODO(hidde): according to the Helm source the first item is not
252- // always the correct one to pick, check for updates once in awhile.
253- // Ref: https://github.com/helm/helm/blob/v3.3.0/pkg/downloader/chart_downloader.go#L241
254- ref := cv .URLs [0 ]
255- u , err := url .Parse (ref )
256- if err != nil {
257- err = fmt .Errorf ("invalid chart URL format '%s': %w" , ref , err )
258- }
259-
260- // Prepend the chart repository base URL if the URL is relative
261- if ! u .IsAbs () {
262- repoURL , err := url .Parse (repository .Spec .URL )
263- if err != nil {
264- err = fmt .Errorf ("invalid repository URL format '%s': %w" , repository .Spec .URL , err )
265- return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPullFailedReason , err .Error ()), err
266- }
267- q := repoURL .Query ()
268- // Trailing slash is required for ResolveReference to work
269- repoURL .Path = strings .TrimSuffix (repoURL .Path , "/" ) + "/"
270- u = repoURL .ResolveReference (u )
271- u .RawQuery = q .Encode ()
272- }
273-
274- // Get the getter for the protocol
275- c , err := r .Getters .ByScheme (u .Scheme )
276- if err != nil {
277- return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPullFailedReason , err .Error ()), err
278- }
279-
280234 var clientOpts []getter.Option
281235 if repository .Spec .SecretRef != nil {
282236 name := types.NamespacedName {
@@ -299,6 +253,46 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
299253 defer cleanup ()
300254 clientOpts = opts
301255 }
256+ clientOpts = append (clientOpts , getter .WithTimeout (repository .GetTimeout ()))
257+
258+ // Initialize the chart repository and load the index file
259+ chartRepo , err := helm .NewChartRepository (repository .Spec .URL , r .Getters , clientOpts )
260+ if err != nil {
261+ switch err .(type ) {
262+ case * url.Error :
263+ return sourcev1 .HelmChartNotReady (chart , sourcev1 .URLInvalidReason , err .Error ()), err
264+ default :
265+ return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPullFailedReason , err .Error ()), err
266+ }
267+ }
268+ indexFile , err := os .Open (r .Storage .LocalPath (* repository .GetArtifact ()))
269+ if err != nil {
270+ return sourcev1 .HelmChartNotReady (chart , sourcev1 .StorageOperationFailedReason , err .Error ()), err
271+ }
272+ b , err := ioutil .ReadAll (indexFile )
273+ if err != nil {
274+ return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPullFailedReason , err .Error ()), err
275+ }
276+ if err = chartRepo .LoadIndex (b ); err != nil {
277+ return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPullFailedReason , err .Error ()), err
278+ }
279+
280+ // Lookup the chart version in the chart repository index
281+ chartVer , err := chartRepo .Get (chart .Spec .Chart , chart .Spec .Version )
282+ if err != nil {
283+ return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPullFailedReason , err .Error ()), err
284+ }
285+
286+ // Return early if the revision is still the same as the current artifact
287+ newArtifact := r .Storage .NewArtifactFor (chart .Kind , chart .GetObjectMeta (), chartVer .Version ,
288+ fmt .Sprintf ("%s-%s.tgz" , chartVer .Name , chartVer .Version ))
289+ if ! force && repository .GetArtifact ().HasRevision (newArtifact .Revision ) {
290+ if newArtifact .URL != chart .GetArtifact ().URL {
291+ r .Storage .SetArtifactURL (chart .GetArtifact ())
292+ chart .Status .URL = r .Storage .SetHostname (chart .Status .URL )
293+ }
294+ return chart , nil
295+ }
302296
303297 // Ensure artifact directory exists
304298 err = r .Storage .MkdirAll (newArtifact )
@@ -315,9 +309,8 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
315309 }
316310 defer unlock ()
317311
318- // TODO(hidde): implement timeout from the HelmRepository
319- // https://github.com/helm/helm/pull/7950
320- res , err := c .Get (u .String (), clientOpts ... )
312+ // Attempt to download the chart
313+ res , err := chartRepo .DownloadChart (chartVer )
321314 if err != nil {
322315 return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPullFailedReason , err .Error ()), err
323316 }
@@ -345,7 +338,7 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
345338 }
346339
347340 // Overwrite values file
348- chartPath := path .Join (tmpDir , cv .Name )
341+ chartPath := path .Join (tmpDir , chartVer .Name )
349342 if err := helm .OverwriteChartDefaultValues (chartPath , chart .Spec .ValuesFile ); err != nil {
350343 return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPackageFailedReason , err .Error ()), err
351344 }
@@ -376,7 +369,7 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
376369 }
377370
378371 // Update symlink
379- chartUrl , err := r .Storage .Symlink (newArtifact , fmt .Sprintf ("%s-latest.tgz" , cv .Name ))
372+ chartUrl , err := r .Storage .Symlink (newArtifact , fmt .Sprintf ("%s-latest.tgz" , chartVer .Name ))
380373 if err != nil {
381374 err = fmt .Errorf ("storage error: %w" , err )
382375 return sourcev1 .HelmChartNotReady (chart , sourcev1 .StorageOperationFailedReason , err .Error ()), err
0 commit comments