From cbdc20761c718506e779a0cbf3ecebfc7ad4e55c Mon Sep 17 00:00:00 2001 From: edithwuly Date: Fri, 19 May 2023 17:16:50 +0800 Subject: [PATCH] get traversing from 3->2 Signed-off-by: edithwuly --- internal/builder/builder.go | 10 +------- internal/fakes/fake_package.go | 14 +---------- pkg/buildpack/builder.go | 19 ++------------- pkg/buildpack/buildpack.go | 43 ++++++++++++++++++++++++---------- pkg/dist/layers.go | 9 ------- 5 files changed, 35 insertions(+), 60 deletions(-) diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 7b66ad891..d163755a5 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -582,21 +582,13 @@ func (b *Builder) addExplodedModules(kind string, logger logging.Logger, tmpDir } // create tar file - layerTar, err := buildpack.ToLayerTar(modTmpDir, module) + diffID, layerTar, err := buildpack.ToLayerTar(modTmpDir, module) if err != nil { lids[i] <- modInfo{err: err} } // generate diff id - diffID, err := dist.LayerDiffID(layerTar) info := module.Descriptor().Info() - if err != nil { - lids[i] <- modInfo{err: errors.Wrapf(err, - "getting content hashes for %s %s", - kind, - style.Symbol(info.FullName()), - )} - } lids[i] <- modInfo{ info: info, layerTar: layerTar, diff --git a/internal/fakes/fake_package.go b/internal/fakes/fake_package.go index 4713d2ffd..a55491771 100644 --- a/internal/fakes/fake_package.go +++ b/internal/fakes/fake_package.go @@ -6,8 +6,6 @@ import ( "os" "path/filepath" - "github.com/google/go-containerregistry/pkg/v1/tarball" - "github.com/buildpacks/pack/pkg/buildpack" "github.com/buildpacks/pack/pkg/dist" ) @@ -28,17 +26,7 @@ type fakePackage struct { func NewPackage(tmpDir string, name string, buildpacks []buildpack.BuildModule) (Package, error) { processBuildpack := func(bp buildpack.BuildModule) (tarFile string, diffID string, err error) { - tarFile, err = buildpack.ToLayerTar(tmpDir, bp) - if err != nil { - return "", "", err - } - - layer, err := tarball.LayerFromFile(tarFile) - if err != nil { - return "", "", err - } - - hash, err := layer.DiffID() + hash, tarFile, err := buildpack.ToLayerTar(tmpDir, bp) if err != nil { return "", "", err } diff --git a/pkg/buildpack/builder.go b/pkg/buildpack/builder.go index 222f1e577..2f7731b33 100644 --- a/pkg/buildpack/builder.go +++ b/pkg/buildpack/builder.go @@ -221,18 +221,11 @@ func (b *PackageBuilder) finalizeImage(image WorkableImage, tmpDir string) error // Let's create the tarball for each individual module for _, bp := range append(b.dependencies.ExplodedModules(), individualBuildModules...) { - bpLayerTar, err := ToLayerTar(tmpDir, bp) + diffID, bpLayerTar, err := ToLayerTar(tmpDir, bp) if err != nil { return err } - diffID, err := dist.LayerDiffID(bpLayerTar) - if err != nil { - return errors.Wrapf(err, - "getting content hashes for buildpack %s", - style.Symbol(bp.Descriptor().Info().FullName()), - ) - } collectionToAdd[bp.Descriptor().Info().FullName()] = toAdd{ tarPath: bpLayerTar, diffID: diffID.String(), @@ -278,19 +271,11 @@ func (b *PackageBuilder) finalizeExtensionImage(image WorkableImage, tmpDir stri } exLayers := dist.ModuleLayers{} - exLayerTar, err := ToLayerTar(tmpDir, b.extension) + diffID, exLayerTar, err := ToLayerTar(tmpDir, b.extension) if err != nil { return err } - diffID, err := dist.LayerDiffID(exLayerTar) - if err != nil { - return errors.Wrapf(err, - "getting content hashes for extension %s", - style.Symbol(b.extension.Descriptor().Info().FullName()), - ) - } - if err := image.AddLayerWithDiffID(exLayerTar, diffID.String()); err != nil { return errors.Wrapf(err, "adding layer tar for extension %s", style.Symbol(b.extension.Descriptor().Info().FullName())) } diff --git a/pkg/buildpack/buildpack.go b/pkg/buildpack/buildpack.go index 80879c10e..6a2c67af2 100644 --- a/pkg/buildpack/buildpack.go +++ b/pkg/buildpack/buildpack.go @@ -2,12 +2,16 @@ package buildpack import ( "archive/tar" + "crypto/sha256" + "encoding/hex" "fmt" "io" "os" "path" "path/filepath" + v1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/BurntSushi/toml" "github.com/buildpacks/lifecycle/api" "github.com/pkg/errors" @@ -304,26 +308,41 @@ func validateExtensionDescriptor(extd dist.ExtensionDescriptor) error { return nil } -func ToLayerTar(dest string, module BuildModule) (string, error) { - descriptor := module.Descriptor() +func ToLayerTar(dest string, module BuildModule) (v1.Hash, string, error) { modReader, err := module.Open() if err != nil { - return "", errors.Wrap(err, "opening blob") + return v1.Hash{}, "", errors.Wrap(err, "opening blob") } defer modReader.Close() - layerTar := filepath.Join(dest, fmt.Sprintf("%s.%s.tar", descriptor.EscapedID(), descriptor.Info().Version)) - fh, err := os.Create(layerTar) - if err != nil { - return "", errors.Wrap(err, "create file for tar") - } - defer fh.Close() + hasher := sha256.New() + var layerTar string + file, ok := modReader.(*os.File) + if ok { + layerTar = file.Name() + if _, err = io.Copy(hasher, modReader); err != nil { + return v1.Hash{}, "", errors.Wrap(err, "writing diffID") + } + } else { + descriptor := module.Descriptor() + layerTar = filepath.Join(dest, fmt.Sprintf("%s.%s.tar", descriptor.EscapedID(), descriptor.Info().Version)) + fh, err := os.Create(layerTar) + if err != nil { + return v1.Hash{}, "", errors.Wrap(err, "create file for tar") + } + defer fh.Close() - if _, err := io.Copy(fh, modReader); err != nil { - return "", errors.Wrap(err, "writing blob to tar") + writer := io.MultiWriter(fh, hasher) + + if _, err = io.Copy(writer, modReader); err != nil { + return v1.Hash{}, "", errors.Wrap(err, "writing blob to tar") + } } - return layerTar, nil + return v1.Hash{ + Algorithm: "sha256", + Hex: hex.EncodeToString(hasher.Sum(make([]byte, 0, hasher.Size()))), + }, layerTar, nil } // Set returns a set of the given string slice. diff --git a/pkg/dist/layers.go b/pkg/dist/layers.go index 590454980..3191f500b 100644 --- a/pkg/dist/layers.go +++ b/pkg/dist/layers.go @@ -1,9 +1,6 @@ package dist import ( - "os" - "path/filepath" - "github.com/buildpacks/lifecycle/api" v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/tarball" @@ -19,12 +16,6 @@ type Descriptor interface { } func LayerDiffID(layerTarPath string) (v1.Hash, error) { - fh, err := os.Open(filepath.Clean(layerTarPath)) - if err != nil { - return v1.Hash{}, errors.Wrap(err, "opening tar file") - } - defer fh.Close() - layer, err := tarball.LayerFromFile(layerTarPath) if err != nil { return v1.Hash{}, errors.Wrap(err, "reading layer tar")