Skip to content

Commit

Permalink
Refactor DownloadBuildpack to use dependency injection strategy and a…
Browse files Browse the repository at this point in the history
…dd tests
  • Loading branch information
YousefHaggyHeroku committed Jul 14, 2021
1 parent 14af1f1 commit 3c83697
Show file tree
Hide file tree
Showing 31 changed files with 750 additions and 541 deletions.
8 changes: 4 additions & 4 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import (
ignore "github.com/sabhiram/go-gitignore"

"github.com/buildpacks/pack/config"
"github.com/buildpacks/pack/image"
"github.com/buildpacks/pack/internal/blob"
"github.com/buildpacks/pack/internal/build"
"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/buildpack"
"github.com/buildpacks/pack/internal/buildpackage"
internalConfig "github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/image"
"github.com/buildpacks/pack/internal/layer"
"github.com/buildpacks/pack/internal/paths"
"github.com/buildpacks/pack/internal/stack"
Expand Down Expand Up @@ -717,15 +717,15 @@ func (c *Client) processBuildpacks(ctx context.Context, builderImage imgutil.Ima
order = appendBuildpackToOrder(order, mainBP.Descriptor().Info)
case buildpack.PackageLocator:
imageName := buildpack.ParsePackageLocator(bp)
mainBP, depBPs, err := extractPackagedBuildpacks(ctx, imageName, c.imageFetcher, image.FetchOptions{Daemon: !publish, PullPolicy: pullPolicy})
mainBP, depBPs, err := extractPackagedBuildpacks(ctx, imageName, c.imageFetcher, !publish, pullPolicy)
if err != nil {
return fetchedBPs, order, errors.Wrapf(err, "creating from buildpackage %s", style.Symbol(bp))
}

fetchedBPs = append(append(fetchedBPs, mainBP), depBPs...)
order = appendBuildpackToOrder(order, mainBP.Descriptor().Info)
case buildpack.RegistryLocator:
registryCache, err := c.getRegistry(c.logger, registry)
registryCache, err := getRegistry(c.logger, registry)
if err != nil {
return fetchedBPs, order, errors.Wrapf(err, "invalid registry '%s'", registry)
}
Expand All @@ -735,7 +735,7 @@ func (c *Client) processBuildpacks(ctx context.Context, builderImage imgutil.Ima
return fetchedBPs, order, errors.Wrapf(err, "locating in registry %s", style.Symbol(bp))
}

mainBP, depBPs, err := extractPackagedBuildpacks(ctx, registryBp.Address, c.imageFetcher, image.FetchOptions{Daemon: !publish, PullPolicy: pullPolicy})
mainBP, depBPs, err := extractPackagedBuildpacks(ctx, registryBp.Address, c.imageFetcher, !publish, pullPolicy)
if err != nil {
return fetchedBPs, order, errors.Wrapf(err, "extracting from registry %s", style.Symbol(bp))
}
Expand Down
109 changes: 109 additions & 0 deletions buildpack_downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package pack

import (
"context"
"fmt"

"github.com/buildpacks/pack/config"
"github.com/buildpacks/pack/internal/buildpack"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/paths"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/logging"
"github.com/pkg/errors"
)

type buildpackDownloader struct {
logger logging.Logger
imageFetcher ImageFetcher
downloader Downloader
}

func NewBuildpackDownloader(logger logging.Logger, imageFetcher ImageFetcher, downloader Downloader) *buildpackDownloader { //nolint:golint,gosimple
return &buildpackDownloader{
logger: logger,
imageFetcher: imageFetcher,
downloader: downloader,
}
}

type BuildpackDownloadOptions struct {
// Buildpack registry name. Defines where all registry buildpacks will be pulled from.
RegistryName string

// The base directory to use to resolve relative assets
RelativeBaseDir string

// The OS of the builder image
ImageOS string

// Deprecated, the older alternative to buildpack URI
ImageName string

Daemon bool

PullPolicy config.PullPolicy
}

func (c *buildpackDownloader) Download(ctx context.Context, buildpackURI string, opts BuildpackDownloadOptions) (dist.Buildpack, []dist.Buildpack, error) {
var err error
var locatorType buildpack.LocatorType
if buildpackURI == "" && opts.ImageName != "" {
c.logger.Warn("The 'image' key is deprecated. Use 'uri=\"docker://...\"' instead.")
buildpackURI = opts.ImageName
locatorType = buildpack.PackageLocator
} else {
locatorType, err = buildpack.GetLocatorType(buildpackURI, opts.RelativeBaseDir, []dist.BuildpackInfo{})
if err != nil {
return nil, nil, err
}
}

var mainBP dist.Buildpack
var depBPs []dist.Buildpack
switch locatorType {
case buildpack.PackageLocator:
imageName := buildpack.ParsePackageLocator(buildpackURI)
c.logger.Debugf("Downloading buildpack from image: %s", style.Symbol(imageName))
mainBP, depBPs, err = extractPackagedBuildpacks(ctx, imageName, c.imageFetcher, opts.Daemon, opts.PullPolicy)
if err != nil {
return nil, nil, errors.Wrapf(err, "extracting from registry %s", style.Symbol(buildpackURI))
}
case buildpack.RegistryLocator:
c.logger.Debugf("Downloading buildpack from registry: %s", style.Symbol(buildpackURI))
registryCache, err := getRegistry(c.logger, opts.RegistryName)
if err != nil {
return nil, nil, errors.Wrapf(err, "invalid registry '%s'", opts.RegistryName)
}

registryBp, err := registryCache.LocateBuildpack(buildpackURI)
if err != nil {
return nil, nil, errors.Wrapf(err, "locating in registry %s", style.Symbol(buildpackURI))
}

mainBP, depBPs, err = extractPackagedBuildpacks(ctx, registryBp.Address, c.imageFetcher, opts.Daemon, opts.PullPolicy)
if err != nil {
return nil, nil, errors.Wrapf(err, "extracting from registry %s", style.Symbol(buildpackURI))
}
case buildpack.URILocator:
buildpackURI, err = paths.FilePathToURI(buildpackURI, opts.RelativeBaseDir)
if err != nil {
return nil, nil, errors.Wrapf(err, "making absolute: %s", style.Symbol(buildpackURI))
}

c.logger.Debugf("Downloading buildpack from URI: %s", style.Symbol(buildpackURI))

blob, err := c.downloader.Download(ctx, buildpackURI)
if err != nil {
return nil, nil, errors.Wrapf(err, "downloading buildpack from %s", style.Symbol(buildpackURI))
}

mainBP, depBPs, err = decomposeBuildpack(blob, opts.ImageOS)
if err != nil {
return nil, nil, errors.Wrapf(err, "extracting from %s", style.Symbol(buildpackURI))
}
default:
return nil, nil, fmt.Errorf("error reading %s: invalid locator: %s", buildpackURI, locatorType)
}
return mainBP, depBPs, nil
}
Loading

0 comments on commit 3c83697

Please sign in to comment.