Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pack build support --extension #1651

Merged
merged 33 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
31f03d9
build(deps): bump golang.org/x/crypto from 0.5.0 to 0.6.0
dependabot[bot] Feb 9, 2023
82460d0
build(deps): bump golang.org/x/oauth2 from 0.4.0 to 0.5.0
dependabot[bot] Feb 9, 2023
4682cdc
add --extension
edithwuly Mar 1, 2023
b18e1d0
build(deps): bump github.com/containerd/containerd from 1.6.15 to 1.6.18
dependabot[bot] Feb 16, 2023
daaf58a
build(deps): bump github.com/prometheus/client_golang in /tools
dependabot[bot] Feb 16, 2023
9b604e4
build(deps): bump github.com/docker/cli
dependabot[bot] Feb 16, 2023
c230ae3
build(deps): bump github.com/buildpacks/lifecycle from 0.15.3 to 0.16.0
dependabot[bot] Feb 16, 2023
cb819ec
build(deps): bump github.com/onsi/gomega from 1.26.0 to 1.27.0
dependabot[bot] Feb 16, 2023
eba8581
perf: parallelize builder's module processing
matejvasek Feb 15, 2023
8f65364
Update rebaser for lifecycle 0.16.0
jkutner Feb 17, 2023
ca59103
build(deps): bump golang.org/x/net from 0.6.0 to 0.7.0
dependabot[bot] Feb 17, 2023
c10621c
build(deps): bump github.com/gdamore/tcell/v2 from 2.5.4 to 2.6.0
dependabot[bot] Feb 21, 2023
f556bb3
build(deps): bump github.com/onsi/gomega from 1.27.0 to 1.27.1
dependabot[bot] Feb 17, 2023
97025d0
build(deps): bump golang.org/x/sys in /tools
dependabot[bot] Feb 25, 2023
57af208
build(deps): bump crazy-max/ghaction-chocolatey from 2.0.0 to 2.1.0
dependabot[bot] Feb 24, 2023
92954c0
Fix typo in pack stack suggest
AidanDelaney Feb 27, 2023
fdcd17d
format
edithwuly Mar 1, 2023
7ea08cd
Merge branch 'main' into main
edithwuly Mar 1, 2023
6469c33
Merge branch 'buildpacks:main' into main
edithwuly Mar 13, 2023
f4b709b
unit test: overwritten and resolves version
edithwuly Mar 13, 2023
7dfa145
format
edithwuly Mar 13, 2023
c6953fa
resolve version
edithwuly Mar 13, 2023
6ffe547
add extension to fakeimage
edithwuly Mar 13, 2023
439078f
format
edithwuly Mar 13, 2023
c57c2d2
add TODO
edithwuly Mar 13, 2023
39454cf
Merge branch 'main' into main
edithwuly Mar 29, 2023
4affdc4
Update internal/commands/build.go
edithwuly Apr 7, 2023
8bb1a2b
Update pkg/client/build.go
edithwuly Apr 7, 2023
aee4dba
Update pkg/client/build.go
edithwuly Apr 7, 2023
909c502
Update pkg/client/build.go
edithwuly Apr 7, 2023
5e8633a
Merge branch 'main' into main
edithwuly Apr 7, 2023
6bb8cb0
remove handler for buildpack.FromBuilderLocator of extension
edithwuly Apr 7, 2023
48ed612
nit
edithwuly Apr 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type BuildFlags struct {
Env []string
EnvFiles []string
Buildpacks []string
Extensions []string
Volumes []string
AdditionalTags []string
Workspace string
Expand Down Expand Up @@ -96,6 +97,7 @@ func Build(logger logging.Logger, cfg config.Config, packClient PackClient) *cob
}

buildpacks := flags.Buildpacks
extensions := flags.Extensions

env, err := parseEnv(flags.EnvFiles, flags.Env)
if err != nil {
Expand Down Expand Up @@ -159,6 +161,7 @@ func Build(logger logging.Logger, cfg config.Config, packClient PackClient) *cob
return trustBuilder
},
Buildpacks: buildpacks,
Extensions: extensions,
ContainerConfig: client.ContainerConfig{
Network: flags.Network,
Volumes: flags.Volumes,
Expand Down Expand Up @@ -210,6 +213,7 @@ func parseTime(providedTime string) (*time.Time, error) {
func buildCommandFlags(cmd *cobra.Command, buildFlags *BuildFlags, cfg config.Config) {
cmd.Flags().StringVarP(&buildFlags.AppPath, "path", "p", "", "Path to app dir or zip-formatted file (defaults to current working directory)")
cmd.Flags().StringSliceVarP(&buildFlags.Buildpacks, "buildpack", "b", nil, "Buildpack to use. One of:\n a buildpack by id and version in the form of '<buildpack>@<version>',\n path to a buildpack directory (not supported on Windows),\n path/URL to a buildpack .tar or .tgz file, or\n a packaged buildpack image name in the form of '<hostname>/<repo>[:<tag>]'"+stringSliceHelp("buildpack"))
cmd.Flags().StringSliceVarP(&buildFlags.Extensions, "extension", "", nil, "Extension to use. One of:\n a extension by id and version in the form of '<extension>@<version>',\n path to a extension directory (not supported on Windows),\n path/URL to a extension .tar or .tgz file, or\n a packaged extension image name in the form of '<hostname>/<repo>[:<tag>]'"+stringSliceHelp("extension"))
edithwuly marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().StringVarP(&buildFlags.Builder, "builder", "B", cfg.DefaultBuilder, "Builder image")
cmd.Flags().Var(&buildFlags.Cache, "cache",
`Cache options used to define cache techniques for build process.
Expand Down
67 changes: 59 additions & 8 deletions pkg/client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ type BuildOptions struct {
// share both an ID and Version with a buildpack on the builder.
Buildpacks []string

// List of extension images or archives to add to a builder.
// These extensions may overwrite those on the builder if they
// share both an ID and Version with a extension on the builder.
edithwuly marked this conversation as resolved.
Show resolved Hide resolved
Extensions []string

// Additional image tags to push to, each will contain contents identical to Image
AdditionalTags []string

Expand Down Expand Up @@ -278,6 +283,11 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
return err
}

fetchedExs, orderExtensions, err := c.processExtensions(ctx, bldr.Image(), bldr.Extensions(), bldr.OrderExtensions(), bldr.StackID, opts)
if err != nil {
return err
}

if err := c.validateMixins(fetchedBPs, bldr, runImageName, runMixins); err != nil {
return errors.Wrap(err, "validating stack mixins")
}
Expand All @@ -291,7 +301,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
buildEnvs[k] = v
}

ephemeralBuilder, err := c.createEphemeralBuilder(rawBuilderImage, buildEnvs, order, fetchedBPs)
ephemeralBuilder, err := c.createEphemeralBuilder(rawBuilderImage, buildEnvs, order, fetchedBPs, orderExtensions, fetchedExs)
if err != nil {
return err
}
Expand Down Expand Up @@ -791,7 +801,7 @@ func (c *Client) processBuildpacks(ctx context.Context, builderImage imgutil.Ima
order = newOrder
}
default:
newFetchedBPs, moduleInfo, err := c.fetchBuildpack(ctx, bp, relativeBaseDir, builderImage, builderBPs, opts)
newFetchedBPs, moduleInfo, err := c.fetchBuildpack(ctx, bp, relativeBaseDir, builderImage, builderBPs, opts, buildpack.KindBuildpack)
if err != nil {
return fetchedBPs, order, err
}
Expand Down Expand Up @@ -825,7 +835,7 @@ func (c *Client) processBuildpacks(ctx context.Context, builderImage imgutil.Ima
if len(preBuildpacks) > 0 || len(postBuildpacks) > 0 {
order = builderOrder
for _, bp := range preBuildpacks {
newFetchedBPs, moduleInfo, err := c.fetchBuildpack(ctx, bp, relativeBaseDir, builderImage, builderBPs, opts)
newFetchedBPs, moduleInfo, err := c.fetchBuildpack(ctx, bp, relativeBaseDir, builderImage, builderBPs, opts, buildpack.KindBuildpack)
if err != nil {
return fetchedBPs, order, err
}
Expand All @@ -834,7 +844,7 @@ func (c *Client) processBuildpacks(ctx context.Context, builderImage imgutil.Ima
}

for _, bp := range postBuildpacks {
newFetchedBPs, moduleInfo, err := c.fetchBuildpack(ctx, bp, relativeBaseDir, builderImage, builderBPs, opts)
newFetchedBPs, moduleInfo, err := c.fetchBuildpack(ctx, bp, relativeBaseDir, builderImage, builderBPs, opts, buildpack.KindBuildpack)
if err != nil {
return fetchedBPs, order, err
}
Expand All @@ -847,7 +857,7 @@ func (c *Client) processBuildpacks(ctx context.Context, builderImage imgutil.Ima
return fetchedBPs, order, nil
}

func (c *Client) fetchBuildpack(ctx context.Context, bp string, relativeBaseDir string, builderImage imgutil.Image, builderBPs []dist.ModuleInfo, opts BuildOptions) ([]buildpack.BuildModule, *dist.ModuleInfo, error) {
func (c *Client) fetchBuildpack(ctx context.Context, bp string, relativeBaseDir string, builderImage imgutil.Image, builderBPs []dist.ModuleInfo, opts BuildOptions, kind string) ([]buildpack.BuildModule, *dist.ModuleInfo, error) {
pullPolicy := opts.PullPolicy
publish := opts.Publish
registry := opts.Registry
Expand All @@ -871,13 +881,17 @@ func (c *Client) fetchBuildpack(ctx context.Context, bp string, relativeBaseDir
if err != nil {
return nil, nil, errors.Wrapf(err, "getting OS from %s", style.Symbol(builderImage.Name()))
}
mainBP, depBPs, err := c.buildpackDownloader.Download(ctx, bp, buildpack.DownloadOptions{
downloadOptions := buildpack.DownloadOptions{
RegistryName: registry,
ImageOS: imageOS,
RelativeBaseDir: relativeBaseDir,
Daemon: !publish,
PullPolicy: pullPolicy,
})
}
if kind == buildpack.KindExtension {
downloadOptions.ModuleKind = kind
}
mainBP, depBPs, err := c.buildpackDownloader.Download(ctx, bp, downloadOptions)
if err != nil {
return nil, nil, errors.Wrap(err, "downloading buildpack")
}
Expand Down Expand Up @@ -939,7 +953,34 @@ func prependBuildpackToOrder(order dist.Order, bpInfo dist.ModuleInfo) (newOrder
return newOrder
}

func (c *Client) createEphemeralBuilder(rawBuilderImage imgutil.Image, env map[string]string, order dist.Order, buildpacks []buildpack.BuildModule) (*builder.Builder, error) {
func (c *Client) processExtensions(ctx context.Context, builderImage imgutil.Image, builderExs []dist.ModuleInfo, builderOrder dist.Order, stackID string, opts BuildOptions) (fetchedExs []buildpack.BuildModule, orderExtensions dist.Order, err error) {
relativeBaseDir := opts.RelativeBaseDir
declaredExs := opts.Extensions

orderExtensions = dist.Order{{Group: []dist.ModuleRef{}}}
for _, ex := range declaredExs {
locatorType, err := buildpack.GetLocatorType(ex, relativeBaseDir, builderExs)
if err != nil {
return nil, nil, err
}

switch locatorType {
case buildpack.RegistryLocator:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A note, in case it helps others, buildpack.FromBuilderLocator is deprecated now that we have pre- and post- buildpacks. We may want --pre-extension and --post-extension (like exist for buildpacks) but that should be considered out-of-scope for this issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added case buildpack.FromBuilderLocator in processExtensions()` funtion. Please help me review, thanks!

return nil, nil, errors.New("RegistryLocator type is not valid for extensions")
default:
newFetchedExs, moduleInfo, err := c.fetchBuildpack(ctx, ex, relativeBaseDir, builderImage, builderExs, opts, buildpack.KindExtension)
if err != nil {
return fetchedExs, orderExtensions, err
}
fetchedExs = append(fetchedExs, newFetchedExs...)
orderExtensions = prependBuildpackToOrder(orderExtensions, *moduleInfo)
}
}

return fetchedExs, orderExtensions, nil
}

func (c *Client) createEphemeralBuilder(rawBuilderImage imgutil.Image, env map[string]string, order dist.Order, buildpacks []buildpack.BuildModule, orderExtensions dist.Order, extensions []buildpack.BuildModule) (*builder.Builder, error) {
origBuilderName := rawBuilderImage.Name()
bldr, err := builder.New(rawBuilderImage, fmt.Sprintf("pack.local/builder/%x:latest", randString(10)))
if err != nil {
Expand All @@ -957,6 +998,16 @@ func (c *Client) createEphemeralBuilder(rawBuilderImage imgutil.Image, env map[s
bldr.SetOrder(order)
}

for _, ex := range extensions {
exInfo := ex.Descriptor().Info()
c.logger.Debugf("Adding extension %s version %s to builder", style.Symbol(exInfo.ID), style.Symbol(exInfo.Version))
bldr.AddExtension(ex)
}
if len(orderExtensions) > 0 && len(orderExtensions[0].Group) > 0 {
c.logger.Debug("Setting custom order")
edithwuly marked this conversation as resolved.
Show resolved Hide resolved
bldr.SetOrderExtensions(orderExtensions)
}

if err := bldr.Save(c.logger, builder.CreatorMetadata{Version: c.version}); err != nil {
return nil, err
}
Expand Down