Skip to content
Permalink
Browse files
fix(bundles): bundle copy from archive to remote incorrect values
* Refactor bundle archive writer/reader to gzip/unzip image archives in write/read operations.
* Improve tests to check validity of chart to catch incorrect-values-bug.

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Sep 12, 2022
1 parent eaa6d58 commit e9a2c53887547a7b8136e808653263abacb05dad
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 122 deletions.
@@ -2,7 +2,6 @@ package bundles

import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
@@ -149,16 +148,11 @@ func (bundle *BundleArchive) CopyFromRemote(ctx context.Context, fromRemote *Rem

// TODO: maybe save into tmp file archive OR read resulting image size from the registry before pulling
imageBytes := bytes.NewBuffer(nil)
zipper := gzip.NewWriter(imageBytes)

if err := fromRemote.RegistryClient.PullImageArchive(ctx, zipper, imageRef); err != nil {
if err := fromRemote.RegistryClient.PullImageArchive(ctx, imageBytes, imageRef); err != nil {
return fmt.Errorf("error pulling image %q archive: %w", imageRef, err)
}

if err := zipper.Close(); err != nil {
return fmt.Errorf("unable to close gzip writer: %w", err)
}

if err := bundle.Writer.WriteImageArchive(tag, imageBytes.Bytes()); err != nil {
return fmt.Errorf("error writing image %q into bundle archive: %w", imageRef, err)
}
@@ -2,8 +2,10 @@ package bundles

import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"io"
"os"
"time"

@@ -117,22 +119,32 @@ func (writer *BundleArchiveFileWriter) WriteChartArchive(data []byte) error {

func (writer *BundleArchiveFileWriter) WriteImageArchive(imageTag string, data []byte) error {
now := time.Now()
buf := bytes.NewBuffer(nil)
zipper := gzip.NewWriter(buf)

if _, err := io.Copy(zipper, bytes.NewReader(data)); err != nil {
return fmt.Errorf("unable to gzip image archive data: %w", err)
}

if err := zipper.Close(); err != nil {
return fmt.Errorf("unable to close gzip image archive: %w", err)
}

header := &tar.Header{
Name: fmt.Sprintf("images/%s.tar.gz", imageTag),
Typeflag: tar.TypeReg,
Mode: 0o777,
Size: int64(len(data)),
Size: int64(len(buf.Bytes())),
ModTime: now,
AccessTime: now,
ChangeTime: now,
}

if err := writer.tmpArchiveWriter.WriteHeader(header); err != nil {
return fmt.Errorf("unable to write chart.tar.gz header: %w", err)
return fmt.Errorf("unable to write image %q header: %w", imageTag, err)
}

if _, err := writer.tmpArchiveWriter.Write(data); err != nil {
if _, err := writer.tmpArchiveWriter.Write(buf.Bytes()); err != nil {
return fmt.Errorf("unable to write chart.tar.gz data: %w", err)
}

@@ -4,11 +4,15 @@ import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"fmt"

"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"sigs.k8s.io/yaml"

"github.com/werf/logboek"
)

func ChartToBytes(ch *chart.Chart) ([]byte, error) {
@@ -36,3 +40,20 @@ func BytesToChart(data []byte) (*chart.Chart, error) {
dataReader := bytes.NewBuffer(data)
return loader.LoadArchiveWithOptions(dataReader, loader.LoadOptions{})
}

func SaveChartValues(ctx context.Context, ch *chart.Chart) error {
valuesRaw, err := yaml.Marshal(ch.Values)
if err != nil {
return fmt.Errorf("unable to marshal chart values: %w", err)
}
logboek.Context(ctx).Debug().LogF("Values after change (%v):\n%s\n---\n", err, valuesRaw)

for _, f := range ch.Raw {
if f.Name == chartutil.ValuesfileName {
f.Data = valuesRaw
break
}
}

return nil
}

0 comments on commit e9a2c53

Please sign in to comment.