@@ -19,6 +19,7 @@ package controllers
1919import (
2020 "context"
2121 "fmt"
22+ "io"
2223 "io/ioutil"
2324 "net/url"
2425 "os"
@@ -374,23 +375,30 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
374375 if err != nil {
375376 return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPullFailedReason , err .Error ()), err
376377 }
378+ tmpFile , err := ioutil .TempFile ("" , fmt .Sprintf ("%s-%s-" , chart .Namespace , chart .Name ))
379+ if err != nil {
380+ return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPullFailedReason , err .Error ()), err
381+ }
382+ defer os .RemoveAll (tmpFile .Name ())
383+ if _ , err = io .Copy (tmpFile , res ); err != nil {
384+ tmpFile .Close ()
385+ return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPullFailedReason , err .Error ()), err
386+ }
387+ tmpFile .Close ()
377388
378- // Either repackage the chart with the declared default values file,
379- // or write the chart directly to storage.
389+ // Check if we need to repackage the chart with the declared defaults files.
380390 var (
391+ pkgPath = tmpFile .Name ()
381392 readyReason = sourcev1 .ChartPullSucceededReason
382393 readyMessage = fmt .Sprintf ("Fetched revision: %s" , newArtifact .Revision )
383394 )
395+
384396 switch {
385397 case len (chart .GetValuesFiles ()) > 0 :
386- var (
387- tmpDir string
388- pkgPath string
389- )
390398 valuesMap := make (map [string ]interface {})
391399
392400 // Load the chart
393- helmChart , err := loader .LoadArchive ( res )
401+ helmChart , err := loader .LoadFile ( pkgPath )
394402 if err != nil {
395403 err = fmt .Errorf ("load chart error: %w" , err )
396404 return sourcev1 .HelmChartNotReady (chart , sourcev1 .StorageOperationFailedReason , err .Error ()), err
@@ -435,12 +443,11 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
435443 if changed , err := helm .OverwriteChartDefaultValues (helmChart , yamlBytes ); err != nil {
436444 return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPackageFailedReason , err .Error ()), err
437445 } else if ! changed {
438- // No changes, skip to write original package to storage
439- goto skipToDefault
446+ break
440447 }
441448
442449 // Create temporary working directory
443- tmpDir , err = ioutil .TempDir ("" , fmt .Sprintf ("%s-%s-" , chart .Namespace , chart .Name ))
450+ tmpDir , err : = ioutil .TempDir ("" , fmt .Sprintf ("%s-%s-" , chart .Namespace , chart .Name ))
444451 if err != nil {
445452 err = fmt .Errorf ("tmp dir error: %w" , err )
446453 return sourcev1 .HelmChartNotReady (chart , sourcev1 .StorageOperationFailedReason , err .Error ()), err
@@ -462,15 +469,12 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
462469
463470 readyMessage = fmt .Sprintf ("Fetched and packaged revision: %s" , newArtifact .Revision )
464471 readyReason = sourcev1 .ChartPackageSucceededReason
465- break
466- skipToDefault:
467- fallthrough
468- default :
469- // Write artifact to storage
470- if err := r .Storage .AtomicWriteFile (& newArtifact , res , 0644 ); err != nil {
471- err = fmt .Errorf ("unable to write chart file: %w" , err )
472- return sourcev1 .HelmChartNotReady (chart , sourcev1 .StorageOperationFailedReason , err .Error ()), err
473- }
472+ }
473+
474+ // Write artifact to storage
475+ if err := r .Storage .CopyFromPath (& newArtifact , pkgPath ); err != nil {
476+ err = fmt .Errorf ("unable to write chart file: %w" , err )
477+ return sourcev1 .HelmChartNotReady (chart , sourcev1 .StorageOperationFailedReason , err .Error ()), err
474478 }
475479
476480 // Update symlink
0 commit comments