Skip to content

Commit

Permalink
Add targets when creating buildpackpage
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Kutner <jpkutner@gmail.com>
  • Loading branch information
jkutner committed Apr 8, 2023
1 parent daedf35 commit c6cc3ac
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 44 deletions.
2 changes: 1 addition & 1 deletion internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ func (b *Builder) validateBuildpacks() error {
return err
}

if len(bpd.Stacks()) == 0 && len(bpd.Targets()) == 0 { // order buildpack
if len(bpd.Order()) > 0 { // order buildpack
for _, g := range bpd.Order() {
for _, r := range g.Group {
if _, ok := bpLookup[r.FullName()]; !ok {
Expand Down
6 changes: 3 additions & 3 deletions internal/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,8 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
// - 1 from stackLayer
// - 1 from runImageLayer
h.AssertEq(t, baseImage.NumberOfAddedLayers(), 6)
oldSha256 := "4dc0072c61fc2bd7118bbc93a432eae0012082de094455cf0a9fed20e3c44789"
newSha256 := "29cb2bce4c2350f0e86f3dd30fa3810beb409b910126a18651de750f457fedfb"
oldSha256 := "2ba2e8563f7f43533ba26047a44f3e8bb7dd009043bd73a0e6aadb02c084955c"
newSha256 := "719faea06424d01bb5788ce63c1167e8d382b2d9df8fcf3a0a54ea9b2e3b4045"
if runtime.GOOS == "windows" {
newSha256 = "eaed4a1617bba5738ae5672f6aefda8add7abb2f8630c75dc97a6232879d4ae4"
}
Expand All @@ -794,7 +794,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
when("adding buildpack that already exists on the image", func() {
it("skips adding buildpack that already exists", func() {
logger := logging.NewLogWithWriters(&outBuf, &outBuf, logging.WithVerbose())
diffID := "4dc0072c61fc2bd7118bbc93a432eae0012082de094455cf0a9fed20e3c44789"
diffID := "2ba2e8563f7f43533ba26047a44f3e8bb7dd009043bd73a0e6aadb02c084955c"
bpLayer := dist.ModuleLayers{
"buildpack-1-id": map[string]dist.ModuleLayerInfo{
"buildpack-1-version-1": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/buildpack/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ func testPackageBuilder(t *testing.T, when spec.G, it spec.S) {
// buildpackage metadata
h.ContentContains(`"io.buildpacks.buildpackage.metadata":"{\"id\":\"bp.1.id\",\"version\":\"bp.1.version\",\"stacks\":[{\"id\":\"stack.id.1\"},{\"id\":\"stack.id.2\"}]}"`),
// buildpack layers metadata
h.ContentContains(`"io.buildpacks.buildpack.layers":"{\"bp.1.id\":{\"bp.1.version\":{\"api\":\"0.2\",\"stacks\":[{\"id\":\"stack.id.1\"},{\"id\":\"stack.id.2\"}],\"layerDiffID\":\"sha256:9fa0bb03eebdd0f8e4b6d6f50471b44be83dba750624dfce15dac45975c5707b\"}}`),
h.ContentContains(`"io.buildpacks.buildpack.layers":"{\"bp.1.id\":{\"bp.1.version\":{\"api\":\"0.2\",\"stacks\":[{\"id\":\"stack.id.1\"},{\"id\":\"stack.id.2\"}],\"layerDiffID\":\"sha256:44447e95b06b73496d1891de5afb01936e9999b97ea03dad6337d9f5610807a7\"}}`),
// image os
h.ContentContains(`"os":"linux"`),
)
Expand Down
44 changes: 16 additions & 28 deletions pkg/buildpack/buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,15 @@ func FromBlob(descriptor Descriptor, blob Blob) BuildModule {
func FromBuildpackRootBlob(blob Blob, layerWriterFactory archive.TarWriterFactory) (BuildModule, error) {
descriptor := dist.BuildpackDescriptor{}
descriptor.WithAPI = api.MustParse(dist.AssumedBuildpackAPIVersion)
if err := readDescriptor(KindBuildpack, &descriptor, blob); err != nil {
rc, err := blob.Open()
if err != nil {
return nil, errors.Wrapf(err, "open %s", KindBuildpack)
}
defer rc.Close()
if err := readDescriptor(KindBuildpack, &descriptor, rc); err != nil {
return nil, err
}
if err := detectPlatformSpecificValues(&descriptor, blob); err != nil {
if err := detectPlatformSpecificValues(&descriptor, rc); err != nil {
return nil, err
}
if err := validateBuildpackDescriptor(descriptor); err != nil {
Expand All @@ -91,7 +96,12 @@ func FromBuildpackRootBlob(blob Blob, layerWriterFactory archive.TarWriterFactor
func FromExtensionRootBlob(blob Blob, layerWriterFactory archive.TarWriterFactory) (BuildModule, error) {
descriptor := dist.ExtensionDescriptor{}
descriptor.WithAPI = api.MustParse(dist.AssumedBuildpackAPIVersion)
if err := readDescriptor(KindExtension, &descriptor, blob); err != nil {
rc, err := blob.Open()
if err != nil {
return nil, errors.Wrapf(err, "open %s", KindExtension)
}
defer rc.Close()
if err := readDescriptor(KindExtension, &descriptor, rc); err != nil {
return nil, err
}
if err := validateExtensionDescriptor(descriptor); err != nil {
Expand All @@ -100,13 +110,7 @@ func FromExtensionRootBlob(blob Blob, layerWriterFactory archive.TarWriterFactor
return buildpackFrom(&descriptor, blob, layerWriterFactory)
}

func readDescriptor(kind string, descriptor interface{}, blob Blob) error {
rc, err := blob.Open()
if err != nil {
return errors.Wrapf(err, "open %s", kind)
}
defer rc.Close()

func readDescriptor(kind string, descriptor interface{}, rc io.ReadCloser) error {
descriptorFile := kind + ".toml"

_, buf, err := archive.ReadTarEntry(rc, descriptorFile)
Expand All @@ -122,14 +126,8 @@ func readDescriptor(kind string, descriptor interface{}, blob Blob) error {
return nil
}

func detectPlatformSpecificValues(descriptor *dist.BuildpackDescriptor, blob Blob) error {
rc, err := blob.Open()
if err != nil {
return errors.Wrapf(err, "open %s", KindBuildpack)
}
defer rc.Close()

_, _, err = archive.ReadTarEntry(rc, path.Join("bin", "build"))
func detectPlatformSpecificValues(descriptor *dist.BuildpackDescriptor, rc io.ReadCloser) error {
_, _, err := archive.ReadTarEntry(rc, path.Join("bin", "build"))
if err == nil {
descriptor.WithLinuxBuild = true
}
Expand Down Expand Up @@ -272,16 +270,6 @@ func validateBuildpackDescriptor(bpd dist.BuildpackDescriptor) error {
return errors.Errorf("%s is required", style.Symbol("buildpack.version"))
}

if len(bpd.Order()) == 0 && len(bpd.Stacks()) == 0 && len(bpd.Targets()) == 0 {
return errors.Errorf(
"buildpack %s: must have either %s/%s or an %s defined",
style.Symbol(bpd.Info().FullName()),
style.Symbol("targets"),
style.Symbol("stacks"),
style.Symbol("order"),
)
}

if len(bpd.Order()) >= 1 && (len(bpd.Stacks()) >= 1 || len(bpd.Targets()) >= 1) {
return errors.Errorf(
"buildpack %s: cannot have both %s/%s and an %s defined",
Expand Down
6 changes: 3 additions & 3 deletions pkg/buildpack/buildpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,12 @@ id = "some.stack.id"
},
archive.DefaultTarWriterFactory(),
)
h.AssertError(t, err, "cannot have both 'stacks' and an 'order' defined")
h.AssertError(t, err, "cannot have both 'targets'/'stacks' and an 'order' defined")
})
})

when("missing stacks and order", func() {
it("returns error", func() {
it("does not return an error", func() {
_, err := buildpack.FromBuildpackRootBlob(
&readerBlob{
openFn: func() io.ReadCloser {
Expand All @@ -421,7 +421,7 @@ version = "1.2.3"
},
archive.DefaultTarWriterFactory(),
)
h.AssertError(t, err, "must have either 'stacks' or an 'order' defined")
h.AssertNil(t, err)
})
})
})
Expand Down
5 changes: 3 additions & 2 deletions pkg/buildpack/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ func extractBuildpacks(pkg Package) (mainBP BuildModule, depBPs []BuildModule, e
Homepage: bpInfo.Homepage,
Name: bpInfo.Name,
},
WithStacks: bpInfo.Stacks,
WithOrder: bpInfo.Order,
WithStacks: bpInfo.Stacks,
WithTargets: bpInfo.Targets,
WithOrder: bpInfo.Order,
}

diffID := bpInfo.LayerDiffID // Allow use in closure
Expand Down
5 changes: 3 additions & 2 deletions pkg/client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,8 +709,9 @@ func allBuildpacks(builderImage imgutil.Image, additionalBuildpacks []buildpack.
ID: id,
Version: ver,
},
WithStacks: bp.Stacks,
WithOrder: bp.Order,
WithStacks: bp.Stacks,
WithTargets: bp.Targets,
WithOrder: bp.Order,
}
all = append(all, &desc)
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/client/new_buildpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client_test

import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -132,6 +131,5 @@ func assertBuildpackToml(t *testing.T, path string, id string) {
h.AssertNil(t, err)
defer f.Close()

fmt.Printf("%s\n", buildpackDescriptor)
h.AssertEq(t, buildpackDescriptor.Info().ID, "example/my-cnb")
}
4 changes: 2 additions & 2 deletions pkg/dist/buildpack_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type BuildpackDescriptor struct {
WithAPI *api.Version `toml:"api"`
WithInfo ModuleInfo `toml:"buildpack"`
WithStacks []Stack `toml:"stacks"`
WithTargets []Target `toml:"targets"`
WithTargets []Target `toml:"targets,omitempty"`
WithOrder Order `toml:"order"`
WithWindowsBuild bool
WithLinuxBuild bool
Expand Down Expand Up @@ -55,7 +55,7 @@ func (b *BuildpackDescriptor) EnsureStackSupport(stackID string, providedMixins

func (b *BuildpackDescriptor) EnsureTargetSupport(os, arch, distroName, distroVersion string) error {
if len(b.Targets()) == 0 {
if (!b.WithLinuxBuild && !b.WithWindowsBuild) || len(b.Stacks()) > 0 {
if (!b.WithLinuxBuild && !b.WithWindowsBuild) || len(b.Stacks()) > 0 { // nolint
return nil // Order buildpack or stack buildpack, no validation required
} else if b.WithLinuxBuild && os == DefaultTargetOSLinux && arch == DefaultTargetArch {
return nil
Expand Down
1 change: 1 addition & 0 deletions pkg/dist/dist.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type ModuleLayers map[string]map[string]ModuleLayerInfo
type ModuleLayerInfo struct {
API *api.Version `json:"api"`
Stacks []Stack `json:"stacks,omitempty"`
Targets []Target `json:"targets,omitempty"`
Order Order `json:"order,omitempty"`
LayerDiffID string `json:"layerDiffID"`
Homepage string `json:"homepage,omitempty"`
Expand Down
2 changes: 2 additions & 0 deletions pkg/dist/layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Descriptor interface {
Info() ModuleInfo
Order() Order
Stacks() []Stack
Targets() []Target
}

func LayerDiffID(layerTarPath string) (v1.Hash, error) {
Expand Down Expand Up @@ -45,6 +46,7 @@ func AddToLayersMD(layerMD ModuleLayers, descriptor Descriptor, diffID string) {
layerMD[info.ID][info.Version] = ModuleLayerInfo{
API: descriptor.API(),
Stacks: descriptor.Stacks(),
Targets: descriptor.Targets(),
Order: descriptor.Order(),
LayerDiffID: diffID,
Homepage: info.Homepage,
Expand Down

0 comments on commit c6cc3ac

Please sign in to comment.