Skip to content

Commit

Permalink
get traversing from 3->2
Browse files Browse the repository at this point in the history
Signed-off-by: edithwuly <[email protected]>
  • Loading branch information
edithwuly committed May 19, 2023
1 parent 1aa6461 commit cbdc207
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 60 deletions.
10 changes: 1 addition & 9 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 1 addition & 13 deletions internal/fakes/fake_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
Expand Down
19 changes: 2 additions & 17 deletions pkg/buildpack/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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()))
}
Expand Down
43 changes: 31 additions & 12 deletions pkg/buildpack/buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 0 additions & 9 deletions pkg/dist/layers.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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")
Expand Down

0 comments on commit cbdc207

Please sign in to comment.