Skip to content

Commit

Permalink
Merge branch 'master' into volume-mount
Browse files Browse the repository at this point in the history
  • Loading branch information
jromero authored Feb 25, 2020
2 parents e9e3360 + 3738e94 commit 9367dc1
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 216 deletions.
2 changes: 1 addition & 1 deletion acceptance/testdata/pack_fixtures/builder.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# noop-buildpack-2 has the same id but a different version compared to noop-buildpack
uri = "noop-buildpack-2.tgz"

[[packages]]
[[buildpacks]]
image = "{{ .package_name }}"

[[order]]
Expand Down
59 changes: 51 additions & 8 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1407,16 +1407,59 @@ func testBuild(t *testing.T, when spec.G, it spec.S) {

when("Lifecycle option", func() {
when("Platform API", func() {
when("lifecycle platform API is compatible", func() {
it("should succeed", func() {
err := subject.Build(context.TODO(), BuildOptions{
Image: "some/app",
Builder: builderName,
for _, supportedPlatformAPI := range []string{"0.2", "0.3"} {
var (
supportedPlatformAPI = supportedPlatformAPI
compatibleBuilder *fakes.Image
)

when(fmt.Sprintf("lifecycle platform API is compatible (%s)", supportedPlatformAPI), func() {
it.Before(func() {
compatibleBuilder = ifakes.NewFakeBuilderImage(t,
tmpDir,
"compatible-"+builderName,
defaultBuilderStackID,
"1234",
"5678",
builder.Metadata{
Stack: builder.StackMetadata{
RunImage: builder.RunImageMetadata{
Image: "default/run",
Mirrors: []string{
"registry1.example.com/run/mirror",
"registry2.example.com/run/mirror",
},
},
},
Lifecycle: builder.LifecycleMetadata{
LifecycleInfo: builder.LifecycleInfo{
Version: &builder.Version{
Version: *semver.MustParse("0.3.0"),
},
},
API: builder.LifecycleAPI{
BuildpackVersion: api.MustParse("0.3"),
PlatformVersion: api.MustParse(supportedPlatformAPI),
},
},
},
nil,
nil,
)

fakeImageFetcher.LocalImages[compatibleBuilder.Name()] = compatibleBuilder
})

h.AssertNil(t, err)
it("should succeed", func() {
err := subject.Build(context.TODO(), BuildOptions{
Image: "some/app",
Builder: compatibleBuilder.Name(),
})

h.AssertNil(t, err)
})
})
})
}

when("lifecycle platform API is not compatible", func() {
var incompatibleBuilderImage *fakes.Image
Expand Down Expand Up @@ -1445,7 +1488,7 @@ func testBuild(t *testing.T, when spec.G, it spec.S) {
},
API: builder.LifecycleAPI{
BuildpackVersion: api.MustParse("0.3"),
PlatformVersion: api.MustParse("0.9"),
PlatformVersion: api.MustParse("0.1"),
},
},
},
Expand Down
41 changes: 29 additions & 12 deletions builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,39 @@ import (
"github.com/buildpacks/pack/internal/style"
)

type BuildpackCollection []BuildpackConfig

func (c BuildpackCollection) Packages() []BuildpackConfig {
var bps []BuildpackConfig
for _, bp := range c {
if bp.ImageName != "" && bp.URI == "" {
bps = append(bps, bp)
}
}
return bps
}

func (c BuildpackCollection) Buildpacks() []BuildpackConfig {
var bps []BuildpackConfig
for _, bp := range c {
if bp.URI != "" && bp.ImageName == "" {
bps = append(bps, bp)
}
}
return bps
}

type Config struct {
Description string `toml:"description"`
Buildpacks []BuildpackConfig `toml:"buildpacks"`
Packages []PackageConfig `toml:"packages"`
Order dist.Order `toml:"order"`
Stack StackConfig `toml:"stack"`
Lifecycle LifecycleConfig `toml:"lifecycle"`
Description string `toml:"description"`
Buildpacks BuildpackCollection `toml:"buildpacks"`
Order dist.Order `toml:"order"`
Stack StackConfig `toml:"stack"`
Lifecycle LifecycleConfig `toml:"lifecycle"`
}

type BuildpackConfig struct {
dist.BuildpackInfo
URI string `toml:"uri"`
}

type PackageConfig struct {
Image string `toml:"image"`
dist.ImageOrURI
}

type StackConfig struct {
Expand Down Expand Up @@ -103,7 +120,7 @@ func parseConfig(reader io.Reader, relativeToDir string) (Config, error) {
return Config{}, errors.Wrap(err, "decoding toml contents")
}

for i, bp := range builderConfig.Buildpacks {
for i, bp := range builderConfig.Buildpacks.Buildpacks() {
uri, err := paths.ToAbsolute(bp.URI, relativeToDir)
if err != nil {
return Config{}, errors.Wrap(err, "transforming buildpack URI")
Expand Down
6 changes: 3 additions & 3 deletions create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (c *Client) CreateBuilder(ctx context.Context, opts CreateBuilderOptions) e
return errors.Wrap(err, "setting lifecycle")
}

for _, b := range opts.Config.Buildpacks {
for _, b := range opts.Config.Buildpacks.Buildpacks() {
err := ensureBPSupport(b.URI)
if err != nil {
return err
Expand All @@ -85,8 +85,8 @@ func (c *Client) CreateBuilder(ctx context.Context, opts CreateBuilderOptions) e
bldr.AddBuildpack(fetchedBp)
}

for _, pkg := range opts.Config.Packages {
mainBP, depBPs, err := extractPackagedBuildpacks(ctx, pkg.Image, c.imageFetcher, opts.Publish, opts.NoPull)
for _, pkg := range opts.Config.Buildpacks.Packages() {
mainBP, depBPs, err := extractPackagedBuildpacks(ctx, pkg.ImageName, c.imageFetcher, opts.Publish, opts.NoPull)
if err != nil {
return err
}
Expand Down
53 changes: 47 additions & 6 deletions create_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {
Buildpacks: []pubbldr.BuildpackConfig{
{
BuildpackInfo: dist.BuildpackInfo{ID: "bp.one", Version: "1.2.3"},
URI: "https://example.fake/bp-one.tgz",
ImageOrURI: dist.ImageOrURI{
BuildpackURI: dist.BuildpackURI{
URI: "https://example.fake/bp-one.tgz",
},
},
},
},
Order: []dist.OrderEntry{{
Expand Down Expand Up @@ -525,7 +529,14 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {

opts.Publish = false
opts.NoPull = false
opts.Config.Packages = []pubbldr.PackageConfig{{Image: packageImage.Name()}}
opts.Config.Buildpacks = append(
opts.Config.Buildpacks,
pubbldr.BuildpackConfig{
ImageOrURI: dist.ImageOrURI{
ImageRef: dist.ImageRef{ImageName: packageImage.Name()},
},
},
)

shouldFetchPackageImageWith(true, true)
h.AssertNil(t, subject.CreateBuilder(context.TODO(), opts))
Expand All @@ -540,7 +551,14 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {

opts.Publish = true
opts.NoPull = false
opts.Config.Packages = []pubbldr.PackageConfig{{Image: packageImage.Name()}}
opts.Config.Buildpacks = append(
opts.Config.Buildpacks,
pubbldr.BuildpackConfig{
ImageOrURI: dist.ImageOrURI{
ImageRef: dist.ImageRef{ImageName: packageImage.Name()},
},
},
)

shouldFetchPackageImageWith(false, true)
h.AssertNil(t, subject.CreateBuilder(context.TODO(), opts))
Expand All @@ -555,7 +573,14 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {

opts.Publish = true
opts.NoPull = true
opts.Config.Packages = []pubbldr.PackageConfig{{Image: packageImage.Name()}}
opts.Config.Buildpacks = append(
opts.Config.Buildpacks,
pubbldr.BuildpackConfig{
ImageOrURI: dist.ImageOrURI{
ImageRef: dist.ImageRef{ImageName: packageImage.Name()},
},
},
)

shouldFetchPackageImageWith(false, false)
h.AssertNil(t, subject.CreateBuilder(context.TODO(), opts))
Expand All @@ -570,7 +595,15 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {

opts.Publish = false
opts.NoPull = true
opts.Config.Packages = []pubbldr.PackageConfig{{Image: packageImage.Name()}}
opts.Config.Buildpacks = append(
opts.Config.Buildpacks,
pubbldr.BuildpackConfig{
ImageOrURI: dist.ImageOrURI{
ImageRef: dist.ImageRef{ImageName: packageImage.Name()},
},
},
)

prepareFetcherWithMissingPackageImage()

h.AssertError(t, subject.CreateBuilder(context.TODO(), opts), "not found")
Expand All @@ -585,7 +618,15 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {
opts.BuilderName = "some/builder"

notPackageImage := fakes.NewImage("not/package", "", nil)
opts.Config.Packages = []pubbldr.PackageConfig{{Image: notPackageImage.Name()}}
opts.Config.Buildpacks = append(
opts.Config.Buildpacks,
pubbldr.BuildpackConfig{
ImageOrURI: dist.ImageOrURI{
ImageRef: dist.ImageRef{ImageName: notPackageImage.Name()},
},
},
)

mockImageFetcher.EXPECT().Fetch(gomock.Any(), notPackageImage.Name(), gomock.Any(), gomock.Any()).Return(notPackageImage, nil)
h.AssertNil(t, notPackageImage.SetLabel("io.buildpacks.buildpack.layers", ""))

Expand Down
47 changes: 10 additions & 37 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"

"github.com/buildpacks/pack/internal/api"
"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/cache"
"github.com/buildpacks/pack/internal/style"
Expand All @@ -19,7 +18,7 @@ import (

var (
// SupportedPlatformAPIVersions lists the Platform API versions pack supports.
SupportedPlatformAPIVersions = []string{"0.1", "0.2"}
SupportedPlatformAPIVersions = []string{"0.2", "0.3"}
)

type Lifecycle struct {
Expand Down Expand Up @@ -65,11 +64,6 @@ type LifecycleOptions struct {
Volumes []string
}

// CombinedExporterCacher returns true if the lifecycle contains combined exporter/cacher phases and reversed analyzer/restorer phases.
func (l *Lifecycle) CombinedExporterCacher() bool {
return api.MustParse(l.platformAPIVersion).Compare(api.MustParse("0.2")) >= 0
}

func (l *Lifecycle) Execute(ctx context.Context, opts LifecycleOptions) error {
l.Setup(opts)
defer l.Cleanup()
Expand All @@ -90,30 +84,16 @@ func (l *Lifecycle) Execute(ctx context.Context, opts LifecycleOptions) error {
return err
}

if l.CombinedExporterCacher() {
l.logger.Info(style.Step("ANALYZING"))
if err := l.Analyze(ctx, opts.Image.Name(), buildCache.Name(), opts.Publish, opts.ClearCache); err != nil {
return err
}

l.logger.Info(style.Step("RESTORING"))
if opts.ClearCache {
l.logger.Info("Skipping 'restore' due to clearing cache")
} else if err := l.Restore(ctx, buildCache.Name()); err != nil {
return err
}
} else {
l.logger.Info(style.Step("RESTORING"))
if opts.ClearCache {
l.logger.Info("Skipping 'restore' due to clearing cache")
} else if err := l.Restore(ctx, buildCache.Name()); err != nil {
return err
}
l.logger.Info(style.Step("ANALYZING"))
if err := l.Analyze(ctx, opts.Image.Name(), buildCache.Name(), opts.Publish, opts.ClearCache); err != nil {
return err
}

l.logger.Info(style.Step("ANALYZING"))
if err := l.Analyze(ctx, opts.Image.Name(), buildCache.Name(), opts.Publish, opts.ClearCache); err != nil {
return err
}
l.logger.Info(style.Step("RESTORING"))
if opts.ClearCache {
l.logger.Info("Skipping 'restore' due to clearing cache")
} else if err := l.Restore(ctx, buildCache.Name()); err != nil {
return err
}

l.logger.Info(style.Step("BUILDING"))
Expand All @@ -127,13 +107,6 @@ func (l *Lifecycle) Execute(ctx context.Context, opts LifecycleOptions) error {
return err
}

if !l.CombinedExporterCacher() {
l.logger.Info(style.Step("CACHING"))
if err := l.Cache(ctx, buildCache.Name()); err != nil {
return err
}
}

return nil
}

Expand Down
Loading

0 comments on commit 9367dc1

Please sign in to comment.