Skip to content

Commit

Permalink
Fix: pack build --buildpack doesn't overwrite existing buildpacks in …
Browse files Browse the repository at this point in the history
…the builder correctly

Signed-off-by: fatih <fatiiates@gmail.com>
  • Loading branch information
fatiiates authored and dfreilich committed Jan 24, 2022
1 parent f046f79 commit fb90077
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,37 @@ func addBuildpacks(logger logging.Logger, tmpDir string, image imgutil.Image, ad
if existingBPInfo.LayerDiffID == diffID.String() {
logger.Debugf("Buildpack %s already exists on builder with same contents, skipping...", style.Symbol(bpInfo.FullName()))
continue
} else {

bpWhiteoutsTmpDir := filepath.Join(tmpDir, strconv.Itoa(i)+"_whiteouts")
if err := os.MkdirAll(bpWhiteoutsTmpDir, os.ModePerm); err != nil {
return errors.Wrap(err, "creating buildpack whiteouts temp dir")
}

deletedMaps := map[string][]byte{
filepath.Join(
cnbDir,
"buildpacks",
strings.ReplaceAll(bpInfo.ID, "/", "_"),
".wh."+bpInfo.Version,
): {},
}
whiteoutsTarFile := filepath.Join(bpWhiteoutsTmpDir, "whiteouts.tar")

if err := createTarball(whiteoutsTarFile, deletedMaps); err != nil {
return errors.Wrapf(err,
"creating whiteout layers' tarfile for buildpack %s",
style.Symbol(bp.Descriptor().Info.FullName()),
)
}

if err := image.AddLayer(whiteoutsTarFile); err != nil {
return errors.Wrapf(err,
"adding whiteout layer tar for buildpack %s",
style.Symbol(bpInfo.FullName()),
)
}

}

logger.Debugf(BuildpackOnBuilderMessage, style.Symbol(bpInfo.FullName()), style.Symbol(existingBPInfo.LayerDiffID), style.Symbol(diffID.String()))
Expand Down Expand Up @@ -455,6 +486,34 @@ func addBuildpacks(logger logging.Logger, tmpDir string, image imgutil.Image, ad
return nil
}

func createTarball(tarPath string, data map[string][]byte) error {

tarFile, err := os.Create(tarPath)
if err != nil {
return err
}

defer tarFile.Close()

tw := tar.NewWriter(tarFile)
defer tw.Close()

for name, content := range data {
hdr := &tar.Header{
Name: name,
Mode: 0600,
Size: int64(len(content)),
}
if err := tw.WriteHeader(hdr); err != nil {
return err
}
if _, err := tw.Write(content); err != nil {
return err
}
}
return nil
}

func processOrder(buildpacks []dist.BuildpackInfo, order dist.Order) (dist.Order, error) {
resolvedOrder := dist.Order{}

Expand Down

0 comments on commit fb90077

Please sign in to comment.