Add custom label metadata to packaged buildpacks#1877
Add custom label metadata to packaged buildpacks#1877jkutner merged 4 commits intobuildpacks:mainfrom
Conversation
Introduces a new flag `--label` to the `pack buildpack package` command which: * can be used to add labels one at a time (e.g.; `--label a=1 --label b=2`) * can be used to add multiple labels in CSV format (e.g.; `--label a=1,b=2`) The labels are parsed into a `map[string]string` and added as label metadata to the packaged OCI or `.cnb` file. Signed-off-by: Colin Casey <casey.colin@gmail.com>
|
@colincasey Thanks for this PR!! ❤️ |
|
Thank you for this PR @colincasey ! |
|
I don't mind documenting the flag @dlion but I'm not sure how. When I looked at buildpacks/docs it seems like the Also, I wanted to address the failing codecov/patch check but I couldn't figure out how to simulate an error while setting a label on the |
|
Hi @colincasey My suggestion will be the following: Yo can extend the type imageLabelError struct {
*fakes.Image
error bool
}
func (i *imageLabelError) SetLabel(k string, v string) error {
if i.error {
return errors.New("Put some fancy error here...")
}
return i.Image.SetLabel(k, v)
}Then we have the function on line 49 that is mocking the image return by the factory, you can create a new fake instance configure to fail when it.Before(func() {
mockController = gomock.NewController(t)
mockImageFactory = func(expectedImageOS string) *testmocks.MockImageFactory {
imageFactory := testmocks.NewMockImageFactory(mockController)
if expectedImageOS != "" {
fakePackageImage := fakes.NewImage("some/package", "", nil)
// You could add the logic to return a
// &imageLabelError{Image: fakePackageImage, error: true}
// whenever you want the setLabel to fail
imageFactory.EXPECT().NewImage("some/package", true, expectedImageOS).Return(fakePackageImage, nil).MaxTimes(1)
}
return imageFactory
}
}) |
You are totally correct! I was thinking of adding it to sections like https://buildpacks.io/docs/buildpack-author-guide/package-a-buildpack/ but then yes, I recalled that the label would be automatically documented here https://buildpacks.io/docs/tools/pack/cli/pack_buildpack_package/, thanks for checking it out!
As @jjbustamante mentioned you are passing to the Builder a mockImageFactory, for example: |
Signed-off-by: Colin Casey <casey.colin@gmail.com>
Signed-off-by: Colin Casey <casey.colin@gmail.com>
|
Thanks @jjbustamante & @dlion. Coding in Go is still very new to me. TIL I learned about struct embedding 😄 |
| for labelKey, labelValue := range labels { | ||
| err = layoutImage.SetLabel(labelKey, labelValue) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "adding label %s=%s", labelKey, labelValue) |
There was a problem hiding this comment.
I think it is possible to also add the test case for this one.
There was a problem hiding this comment.
I downloaded the branch, and ran a quick test against our sample repo. I ran pack buildpack package create and added a custom label. I saved the package as image on my docker daemon and as file.
For the OCI image on disk, after checking the manifest and config file, my custom label was there.
> cat blobs/sha256/3467d7313119104e71d494587e9966b6f26d8a093b2b8ed4a98d495891207e8b | jq '.config.Labels | ."my.label" '
"my.value"Also inspecting the image saved in the daemon
> docker inspect my-buildpack-label | jq '.[] | .Config.Labels | ."my.label" '
"my.value"Show my label correctly.
This looks good to me! Thank you @colincasey for this awesome contribution!! it is going to be very helpful for the community
Summary
Introduces a new flag
--labelto thepack buildpack packagecommand which:--label a=1 --label b=2)--label a=1,b=2)The labels are parsed into a
map[string]stringand added as label metadata to the packaged OCI or.cnbfile.Output
The before/after from
pack buildpack package --helpis shown below.Before
After
Documentation
Related
Resolves #1867