Skip to content

Commit

Permalink
Merge branch 'buildpacks:main' into scafall
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthaksarthak9 authored Jan 30, 2024
2 parents 7c0627d + e3f8bc2 commit 9364a18
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 8 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chrismellard/docker-credential-acr-env v0.0.0-20230304212654-82a0ddb27589 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containerd/containerd v1.7.7 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
github.com/containerd/typeurl v1.0.2 // indirect
github.com/containerd/typeurl/v2 v2.1.1 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/distribution/reference v0.5.0 // indirect
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chrismellard/docker-credential-acr-env v0.0.0-20230304212654-82a0ddb27589 h1:krfRl01rzPzxSxyLyrChD+U+MzsBXbm0OwYYB67uF+4=
github.com/chrismellard/docker-credential-acr-env v0.0.0-20230304212654-82a0ddb27589/go.mod h1:OuDyvmLnMCwa2ep4Jkm6nyA0ocJuZlGyk2gGseVzERM=
github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/containerd/containerd v1.7.7 h1:QOC2K4A42RQpcrZyptP6z9EJZnlHfHJUfZrAAHe15q4=
github.com/containerd/containerd v1.7.7/go.mod h1:3c4XZv6VeT9qgf9GMTxNTMFxGJrGpI2vz1yk4ye+YY8=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
Expand All @@ -108,8 +109,6 @@ github.com/containerd/stargz-snapshotter/estargz v0.14.3 h1:OqlDCK3ZVUO6C3B/5FSk
github.com/containerd/stargz-snapshotter/estargz v0.14.3/go.mod h1:KY//uOCIkSuNAHhJogcZtrNHdKrA99/FCCRjE3HD36o=
github.com/containerd/typeurl v1.0.2 h1:Chlt8zIieDbzQFzXzAeBEF92KhExuE4p9p92/QmY7aY=
github.com/containerd/typeurl v1.0.2/go.mod h1:9trJWW2sRlGub4wZJRTW83VtbOLS6hwcDZXTn6oPz9s=
github.com/containerd/typeurl/v2 v2.1.1 h1:3Q4Pt7i8nYwy2KmQWIw2+1hTvwTE/6w9FqcttATPO/4=
github.com/containerd/typeurl/v2 v2.1.1/go.mod h1:IDp2JFvbwZ31H8dQbEIY7sDl2L3o3HZj1hsSQlywkQ0=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
Expand Down Expand Up @@ -266,8 +265,6 @@ github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e h1:Qa6dnn8Dla
github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e/go.mod h1:waEya8ee1Ro/lgxpVhkJI4BVASzkm3UZqkx/cFJiYHM=
github.com/moby/buildkit v0.11.6 h1:VYNdoKk5TVxN7k4RvZgdeM4GOyRvIi4Z8MXOY7xvyUs=
github.com/moby/buildkit v0.11.6/go.mod h1:GCqKfHhz+pddzfgaR7WmHVEE3nKKZMMDPpK8mh3ZLv4=
github.com/moby/buildkit v0.12.2 h1:B7guBgY6sfk4dBlv/ORUxyYlp0UojYaYyATgtNwSCXc=
github.com/moby/buildkit v0.12.2/go.mod h1:adB4y0SxxX8trnrY+oEulb48ODLqPO6pKMF0ppGcCoI=
github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk=
github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
github.com/moby/sys/sequential v0.5.0 h1:OPvI35Lzn9K04PBbCLW0g4LcFAJgHsvXsRyewg5lXtc=
Expand Down Expand Up @@ -506,7 +503,6 @@ google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAs
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
Expand Down
15 changes: 15 additions & 0 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type BuilderOption func(*options) error

type options struct {
toFlatten buildpack.FlattenModuleInfos
labels map[string]string
}

// FromImage constructs a builder from a builder image
Expand Down Expand Up @@ -139,6 +140,13 @@ func constructBuilder(img imgutil.Image, newName string, errOnMissingLabel bool,
return nil, err
}

for labelKey, labelValue := range opts.labels {
err = img.SetLabel(labelKey, labelValue)
if err != nil {
return nil, errors.Wrapf(err, "adding label %s=%s", labelKey, labelValue)
}
}

bldr := &Builder{
baseImageName: img.Name(),
image: img,
Expand Down Expand Up @@ -170,6 +178,13 @@ func WithFlattened(modules buildpack.FlattenModuleInfos) BuilderOption {
}
}

func WithLabels(labels map[string]string) BuilderOption {
return func(o *options) error {
o.labels = labels
return nil
}
}

func constructLifecycleDescriptor(metadata Metadata) LifecycleDescriptor {
return CompatDescriptor(LifecycleDescriptor{
Info: LifecycleInfo{
Expand Down
28 changes: 28 additions & 0 deletions internal/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,34 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
})
})
})

when("labels", func() {
var (
customLabels, imageLabels map[string]string
err error
)
it.Before(func() {
h.AssertNil(t, baseImage.SetEnv("CNB_USER_ID", "1234"))
h.AssertNil(t, baseImage.SetEnv("CNB_GROUP_ID", "4321"))
h.AssertNil(t, baseImage.SetLabel("io.buildpacks.stack.id", "some.stack.id"))
h.AssertNil(t, baseImage.SetLabel("io.buildpacks.stack.mixins", `["mixinX", "mixinY", "build:mixinA"]`))
})

it.After(func() {
h.AssertNilE(t, baseImage.Cleanup())
})

it("should set labels to the image", func() {
customLabels = map[string]string{"test.label.one": "1", "test.label.two": "2"}
subject, err = builder.New(baseImage, "some/builder", builder.WithLabels(customLabels))
h.AssertNil(t, err)

imageLabels, err = baseImage.Labels()
h.AssertNil(t, err)
h.AssertEq(t, imageLabels["test.label.one"], "1")
h.AssertEq(t, imageLabels["test.label.two"], "2")
})
})
}

func assertImageHasBPLayer(t *testing.T, image *fakes.Image, bp buildpack.BuildModule) {
Expand Down
3 changes: 3 additions & 0 deletions internal/commands/builder_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type BuilderCreateFlags struct {
Registry string
Policy string
Flatten []string
Label map[string]string
}

// CreateBuilder creates a builder image, based on a builder config
Expand Down Expand Up @@ -96,6 +97,7 @@ Creating a custom builder allows you to control what buildpacks are used and wha
Registry: flags.Registry,
PullPolicy: pullPolicy,
Flatten: toFlatten,
Labels: flags.Label,
}); err != nil {
return err
}
Expand All @@ -113,6 +115,7 @@ Creating a custom builder allows you to control what buildpacks are used and wha
cmd.Flags().BoolVar(&flags.Publish, "publish", false, "Publish the builder directly to the container registry specified in <image-name>, instead of the daemon.")
cmd.Flags().StringVar(&flags.Policy, "pull-policy", "", "Pull policy to use. Accepted values are always, never, and if-not-present. The default is always")
cmd.Flags().StringSliceVar(&flags.Flatten, "flatten", nil, "List of buildpacks to flatten together into a single layer (format: '<buildpack-id>@<buildpack-version>,<buildpack-id>@<buildpack-version>'")
cmd.Flags().StringToStringVarP(&flags.Label, "label", "l", nil, "Labels to add to the builder image, in the form of '<name>=<value>'")

AddHelpFlag(cmd, "create")
return cmd
Expand Down
17 changes: 17 additions & 0 deletions internal/commands/builder_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,5 +424,22 @@ func testCreateCommand(t *testing.T, when spec.G, it spec.S) {
})
})
})

when("--label", func() {
when("can not be parsed", func() {
it("errors with a descriptive message", func() {
cmd := packageCommand()
cmd.SetArgs([]string{
"some/builder",
"--config", builderConfigPath,
"--label", "name+value",
})

err := cmd.Execute()
h.AssertNotNil(t, err)
h.AssertError(t, err, "invalid argument \"name+value\" for \"-l, --label\" flag: name+value must be formatted as key=value")
})
})
})
})
}
7 changes: 7 additions & 0 deletions pkg/client/create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ type CreateBuilderOptions struct {
// BuildConfigEnv for Builder
BuildConfigEnv map[string]string

// Map of labels to add to the Buildpack
Labels map[string]string

// Configuration that defines the functionality a builder provides.
Config pubbldr.Config

Expand Down Expand Up @@ -154,6 +157,10 @@ func (c *Client) createBaseBuilder(ctx context.Context, opts CreateBuilderOption
if opts.Flatten != nil && len(opts.Flatten.FlattenModules()) > 0 {
builderOpts = append(builderOpts, builder.WithFlattened(opts.Flatten))
}
if opts.Labels != nil && len(opts.Labels) > 0 {
builderOpts = append(builderOpts, builder.WithLabels(opts.Labels))
}

bldr, err := builder.New(baseImage, opts.BuilderName, builderOpts...)
if err != nil {
return nil, errors.Wrap(err, "invalid build-image")
Expand Down
14 changes: 14 additions & 0 deletions pkg/client/create_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,20 @@ func testCreateBuilder(t *testing.T, when spec.G, it spec.S) {
h.AssertNotContains(t, out.String(), "is using deprecated Buildpacks API version")
})

it("should set labels", func() {
opts.Labels = map[string]string{"test.label.one": "1", "test.label.two": "2"}
prepareFetcherWithBuildImage()
prepareFetcherWithRunImages()

err := subject.CreateBuilder(context.TODO(), opts)
h.AssertNil(t, err)

imageLabels, err := fakeBuildImage.Labels()
h.AssertNil(t, err)
h.AssertEq(t, imageLabels["test.label.one"], "1")
h.AssertEq(t, imageLabels["test.label.two"], "2")
})

when("Buildpack dependencies are provided", func() {
var (
bp1v1 buildpack.BuildModule
Expand Down

0 comments on commit 9364a18

Please sign in to comment.