@@ -19,6 +19,7 @@ package controllers
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "io"
22
23
"io/ioutil"
23
24
"net/url"
24
25
"os"
@@ -374,23 +375,30 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
374
375
if err != nil {
375
376
return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPullFailedReason , err .Error ()), err
376
377
}
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 ()
377
388
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.
380
390
var (
391
+ pkgPath = tmpFile .Name ()
381
392
readyReason = sourcev1 .ChartPullSucceededReason
382
393
readyMessage = fmt .Sprintf ("Fetched revision: %s" , newArtifact .Revision )
383
394
)
395
+
384
396
switch {
385
397
case len (chart .GetValuesFiles ()) > 0 :
386
- var (
387
- tmpDir string
388
- pkgPath string
389
- )
390
398
valuesMap := make (map [string ]interface {})
391
399
392
400
// Load the chart
393
- helmChart , err := loader .LoadArchive ( res )
401
+ helmChart , err := loader .LoadFile ( pkgPath )
394
402
if err != nil {
395
403
err = fmt .Errorf ("load chart error: %w" , err )
396
404
return sourcev1 .HelmChartNotReady (chart , sourcev1 .StorageOperationFailedReason , err .Error ()), err
@@ -435,12 +443,11 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
435
443
if changed , err := helm .OverwriteChartDefaultValues (helmChart , yamlBytes ); err != nil {
436
444
return sourcev1 .HelmChartNotReady (chart , sourcev1 .ChartPackageFailedReason , err .Error ()), err
437
445
} else if ! changed {
438
- // No changes, skip to write original package to storage
439
- goto skipToDefault
446
+ break
440
447
}
441
448
442
449
// 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 ))
444
451
if err != nil {
445
452
err = fmt .Errorf ("tmp dir error: %w" , err )
446
453
return sourcev1 .HelmChartNotReady (chart , sourcev1 .StorageOperationFailedReason , err .Error ()), err
@@ -462,15 +469,12 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
462
469
463
470
readyMessage = fmt .Sprintf ("Fetched and packaged revision: %s" , newArtifact .Revision )
464
471
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
474
478
}
475
479
476
480
// Update symlink
0 commit comments