Skip to content

Commit

Permalink
Allow builder.toml to specify packages under [[buildpacks]]
Browse files Browse the repository at this point in the history
Signed-off-by: Natalie Arellano <narellano@vmware.com>
Signed-off-by: Andrew Meyer <meyeran@vmware.com>
Signed-off-by: Simon Jones <sijones@pivotal.io>
  • Loading branch information
simonjjones authored and natalieparellano committed Feb 20, 2020
1 parent 349dd78 commit 222c6d2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 22 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
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

0 comments on commit 222c6d2

Please sign in to comment.