Skip to content

Commit

Permalink
WIP: bug fixes for pack builder create
Browse files Browse the repository at this point in the history
Signed-off-by: WYGIN <wygininc@gmail.com>
  • Loading branch information
WYGIN committed Mar 26, 2024
1 parent a129b74 commit 150d28c
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 50 deletions.
110 changes: 84 additions & 26 deletions builder/config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,25 @@ func (c *MultiArchConfig) BuilderConfigs(getIndexManifest func(ref name.Referenc
targets := c.Targets()
for _, target := range targets {
if len(target.Distributions) == 0 {
cfg, err := c.processTarget(target, dist.Distribution{}, "", getIndexManifest)
configCopy := *c
cfg, err := configCopy.processTarget(target, dist.Distribution{}, "", getIndexManifest)
if err != nil {
return configs, err
}
configs = append(configs, cfg)
} else {
for _, distro := range target.Distributions {
if len(distro.Versions) == 0 {
cfg, err := c.processTarget(target, distro, "", getIndexManifest)
configCopy := *c
cfg, err := configCopy.processTarget(target, distro, "", getIndexManifest)
if err != nil {
return configs, err
}
configs = append(configs, cfg)
} else {
for _, version := range distro.Versions {
cfg, err := c.processTarget(target, distro, version, getIndexManifest)
configCopy := *c
cfg, err := configCopy.processTarget(target, distro, version, getIndexManifest)
if err != nil {
return configs, err
}
Expand All @@ -187,67 +190,122 @@ func (c *MultiArchConfig) BuilderConfigs(getIndexManifest func(ref name.Referenc
return configs, nil
}

func (c *MultiArchConfig) processTarget(target dist.Target, distro dist.Distribution, version string, getIndexManifest func(ref name.Reference) (*v1.IndexManifest, error)) (config Config, err error) {
func (c MultiArchConfig) processTarget(target dist.Target, distro dist.Distribution, version string, getIndexManifest func(ref name.Reference) (*v1.IndexManifest, error)) (config Config, err error) {
config = Config{
Buildpacks: make(ModuleCollection, len(c.Config.Buildpacks)),
Extensions: make(ModuleCollection, len(c.Config.Extensions)),
Order: make(dist.Order, len(c.Config.Order)),
OrderExtensions: make(dist.Order, len(c.Config.OrderExtensions)),
WithTargets: make([]dist.Target, len(c.Config.WithTargets)),
Run: RunConfig{
Images: make([]RunImageConfig, len(c.Config.Run.Images)),
},
Stack: StackConfig{
RunImageMirrors: make([]string, len(c.Config.Stack.RunImageMirrors)),
},
Build: BuildConfig{
Env: make([]BuildConfigEnv, len(c.Config.Build.Env)),
},
}
processedTarget := buildpackage.ProcessTarget(target, distro, version)
for _, bp := range c.Config.Buildpacks {
for i, bp := range c.Config.Buildpacks {
if bp.URI != "" {
if bp.URI, err = buildpackage.GetRelativeURI(bp.URI, c.relativeBaseDir, &processedTarget, getIndexManifest); err != nil {
return c.Config, err
//TODO: Delete below line
fmt.Printf("MultiArchConfig processTarget BP from %s to %s \n", bp.URI, fmt.Sprintf("%s%s%s", style.Symbol(target.OS), "/" + style.Symbol(target.Arch), "/" + style.Symbol(target.ArchVariant)))
if config.Buildpacks[i].URI, err = buildpackage.GetRelativeURI(bp.URI, c.relativeBaseDir, &processedTarget, getIndexManifest); err != nil {
return config, err
}
}
}

for _, ext := range c.Config.Extensions {
for i, ext := range c.Config.Extensions {
if ext.URI != "" {
if ext.URI, err = buildpackage.GetRelativeURI(ext.URI, c.relativeBaseDir, &processedTarget, getIndexManifest); err != nil {
return c.Config, err
//TODO: Delete below line
fmt.Printf("MultiArchConfig processTarget Ext from %s to %s \n", ext.URI, fmt.Sprintf("%s%s%s", style.Symbol(target.OS), "/" + style.Symbol(target.Arch), "/" + style.Symbol(target.ArchVariant)))
if config.Extensions[i].URI, err = buildpackage.GetRelativeURI(ext.URI, c.relativeBaseDir, &processedTarget, getIndexManifest); err != nil {
return config, err
}
}
}

if img := c.Build.Image; img != "" {
if c.Build.Image, err = buildpackage.ParseURItoString(img, processedTarget, getIndexManifest); err != nil {
return c.Config, err
if img := c.Config.Build.Image; img != "" {
//TODO: Delete below line
fmt.Printf("MultiArchConfig processTarget Build.Image from %s to %s \n", img, fmt.Sprintf("%s%s%s", style.Symbol(target.OS), "/" + style.Symbol(target.Arch), "/" + style.Symbol(target.ArchVariant)))
if config.Build.Image, err = buildpackage.ParseURItoString(img, processedTarget, getIndexManifest); err != nil {
return config, err
}
}

for i, runImg := range c.Run.Images {
c.Run.Images[i].Image, err = buildpackage.ParseURItoString(runImg.Image, processedTarget, getIndexManifest)
for i, runImg := range c.Config.Run.Images {
//TODO: Delete below line
fmt.Printf("MultiArchConfig processTarget Run.Images[%d] from %s to %s \n", i, runImg, fmt.Sprintf("%s%s%s", style.Symbol(target.OS), "/" + style.Symbol(target.Arch), "/" + style.Symbol(target.ArchVariant)))
config.Run.Images[i].Image, err = buildpackage.ParseURItoString(runImg.Image, processedTarget, getIndexManifest)
if err != nil {
for j, mirror := range runImg.Mirrors {
if c.Run.Images[i].Mirrors[j], err = buildpackage.ParseURItoString(mirror, processedTarget, getIndexManifest); err == nil {
//TODO: Delete below line
fmt.Printf("MultiArchConfig processTarget Run.Images[%d].Mirrors[%d] from %s to %s \n",i, j, mirror, fmt.Sprintf("%s%s%s", style.Symbol(target.OS), "/" + style.Symbol(target.Arch), "/" + style.Symbol(target.ArchVariant)))
if config.Run.Images[i].Mirrors[j], err = buildpackage.ParseURItoString(mirror, processedTarget, getIndexManifest); err == nil {
break
}
}

if err != nil {
return c.Config, err
return config, err
}
}
}

if img := c.Stack.BuildImage; img != "" {
if c.Stack.BuildImage, err = buildpackage.ParseURItoString(img, processedTarget, getIndexManifest); err != nil {
return c.Config, err
if img := c.Config.Stack.BuildImage; img != "" {
//TODO: Delete below line
fmt.Printf("MultiArchConfig processTarget stack.BuildImage from %s to %s \n", img, fmt.Sprintf("%s%s%s", style.Symbol(target.OS), "/" + style.Symbol(target.Arch), "/" + style.Symbol(target.ArchVariant)))
if config.Stack.BuildImage, err = buildpackage.ParseURItoString(img, processedTarget, getIndexManifest); err != nil {
return config, err
}
}

if img := c.Stack.RunImage; img != "" {
if c.Stack.RunImage, err = buildpackage.ParseURItoString(img, processedTarget, getIndexManifest); err != nil {
for i, mirror := range c.Stack.RunImageMirrors {
if c.Stack.RunImageMirrors[i], err = buildpackage.ParseURItoString(mirror, processedTarget, getIndexManifest); err == nil {
if img := c.Config.Stack.RunImage; img != "" {
//TODO: Delete below line
fmt.Printf("MultiArchConfig processTarget stacks.RunImage from %s to %s \n", img, fmt.Sprintf("%s%s%s", style.Symbol(target.OS), "/" + style.Symbol(target.Arch), "/" + style.Symbol(target.ArchVariant)))
if config.Stack.RunImage, err = buildpackage.ParseURItoString(img, processedTarget, getIndexManifest); err != nil {
for i, mirror := range config.Stack.RunImageMirrors {
//TODO: Delete below line
fmt.Printf("MultiArchConfig processTarget stacks.RunImage Mirror at %d from %s to %s \n", i, mirror, fmt.Sprintf("%s%s%s", style.Symbol(target.OS), "/" + style.Symbol(target.Arch), "/" + style.Symbol(target.ArchVariant)))
if config.Stack.RunImageMirrors[i], err = buildpackage.ParseURItoString(mirror, processedTarget, getIndexManifest); err == nil {
break
}
}

if err != nil {
return c.Config, err
return config, err
}
}
}

config.Order = c.Config.Order
config.OrderExtensions = c.Config.OrderExtensions
config.WithTargets = []dist.Target{processedTarget}
return c.Config, nil
return config, nil
}

func (c *MultiArchConfig) MultiArch() bool {
targets := c.Targets()
if len(targets) > 1 {
return true
}

for _, target := range targets {
if len(target.Distributions) > 1 {
return true
}

for _, distro := range target.Distributions {
if len(distro.Versions) > 1 {
return true
}
}
}

return false
}

// ValidateConfig validates the config
Expand Down
2 changes: 1 addition & 1 deletion buildpackage/multi_arch_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ func ParseURItoString(uri string, target dist.Target, getIndexManifest func(ref
return "", imgutil.ErrManifestUndefined
}

fmt.Printf("fetching image from repo: %s", style.Symbol(uri))
fmt.Printf("fetching image from repo: %s \n", style.Symbol(uri))
digest, err := DigestFromIndex(idx, target)
if err != nil {
return "", err
Expand Down
18 changes: 16 additions & 2 deletions internal/commands/builder_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"fmt"
"path/filepath"
"runtime"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/buildpacks/pack/internal/target"
"github.com/buildpacks/pack/pkg/buildpack"
"github.com/buildpacks/pack/pkg/client"
"github.com/buildpacks/pack/pkg/dist"
"github.com/buildpacks/pack/pkg/image"
"github.com/buildpacks/pack/pkg/logging"
)
Expand Down Expand Up @@ -106,15 +108,27 @@ Creating a custom builder allows you to control what buildpacks are used and wha
Flatten: toFlatten,
Labels: flags.Label,
}
if len(builderConfig.WithTargets) > 1 {

switch multiArch, publish := builderConfig.MultiArch(), flags.Publish; {
case multiArch && !publish:
builderConfig.WithTargets = []dist.Target{{OS: runtime.GOOS, Arch: runtime.GOARCH}}
if err := pack.CreateBuilder(cmd.Context(), builderOpts); err != nil {
return err
}
case multiArch && publish:
if err := pack.CreateMultiArchBuilder(cmd.Context(), builderOpts); err != nil {
return err
}
} else {
default:
if len(builderConfig.Targets()) == 0 {
logger.Warnf("A new '--target' flag is available to set the platform for a builder, using '%s' as default", style.Symbol("---os---"))
}

if err := pack.CreateBuilder(cmd.Context(), builderOpts); err != nil {
return err
}
}

logger.Infof("Successfully created builder image %s", style.Symbol(imageName))
logging.Tip(logger, "Run %s to use this builder", style.Symbol(fmt.Sprintf("pack build <image-name> --builder %s", imageName)))
return nil
Expand Down
13 changes: 13 additions & 0 deletions internal/commands/buildpack_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -123,6 +124,16 @@ func BuildpackPackage(logger logging.Logger, cfg config.Config, packager Buildpa
return err
}

// if not publishing image and is a MultiArch image, only build the target with target's specific device's platform
if !flags.Publish && len(bpConfigs) > 1 {
targets = []dist.Target{{OS: runtime.GOOS, Arch: runtime.GOARCH}}
bpMultiArchConfig = pubbldpkg.NewMultiArchBuildpack(bpConfig, bpPath, flags.Flatten, cmd.Flags().Changed("flatten"), targets)
bpConfigs, err = bpMultiArchConfig.MultiArchConfigs()
if err != nil {
return err
}
}

bpName := args[0]
if flags.Format == client.FormatFile {
switch ext := filepath.Ext(bpName); ext {
Expand Down Expand Up @@ -185,6 +196,8 @@ func BuildpackPackage(logger logging.Logger, cfg config.Config, packager Buildpa
} else {
if len(bpConfigs) == 1 {
pkgBPOpts.IndexOptions.Target = bpConfigs[0].WithTargets[0]
} else {
logger.Warnf("A new '--target' flag is available to set the platform for the buildpack package, using '%s' as default", bpPackageCfg.Platform.OS)
}

if err := packager.PackageBuildpack(cmd.Context(), pkgBPOpts); err != nil {
Expand Down
18 changes: 16 additions & 2 deletions internal/commands/create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"fmt"
"path/filepath"
"runtime"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/internal/target"
"github.com/buildpacks/pack/pkg/client"
"github.com/buildpacks/pack/pkg/dist"
"github.com/buildpacks/pack/pkg/image"
"github.com/buildpacks/pack/pkg/logging"
)
Expand Down Expand Up @@ -82,15 +84,27 @@ Creating a custom builder allows you to control what buildpacks are used and wha
Registry: flags.Registry,
PullPolicy: pullPolicy,
}
if len(builderConfig.Targets()) > 1 {

switch multiArch, publish := builderConfig.MultiArch(), flags.Publish; {
case multiArch && !publish:
builderConfig.WithTargets = []dist.Target{{OS: runtime.GOOS, Arch: runtime.GOARCH}}
if err := pack.CreateBuilder(cmd.Context(), builderOpts); err != nil {
return err
}
case multiArch && publish:
if err := pack.CreateMultiArchBuilder(cmd.Context(), builderOpts); err != nil {
return err
}
} else {
default:
if len(builderConfig.Targets()) == 0 {
logger.Warnf("A new '--target' flag is available to set the platform for a builder, using '%s' as default", style.Symbol("---os---"))
}

if err := pack.CreateBuilder(cmd.Context(), builderOpts); err != nil {
return err
}
}

logger.Infof("Successfully created builder image %s", style.Symbol(imageName))
logging.Tip(logger, "Run %s to use this builder", style.Symbol(fmt.Sprintf("pack build <image-name> --builder %s", imageName)))
return nil
Expand Down
23 changes: 18 additions & 5 deletions internal/commands/extension_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/BurntSushi/toml"
"github.com/pkg/errors"
Expand Down Expand Up @@ -57,11 +58,6 @@ func ExtensionPackage(logger logging.Logger, cfg config.Config, packager Extensi
return errors.Wrap(err, "parsing pull policy")
}

targets, err := target.ParseTargets(flags.Targets, logger)
if err != nil {
return err
}

exPackageCfg := pubbldpkg.DefaultExtensionConfig()
relativeBaseDir := ""
if flags.PackageTomlPath != "" {
Expand Down Expand Up @@ -96,12 +92,27 @@ func ExtensionPackage(logger logging.Logger, cfg config.Config, packager Extensi
if err != nil {
return err
}

targets, err := target.ParseTargets(flags.Targets, logger)
if err != nil {
return err
}

extMultiArchConfig := pubbldpkg.NewMultiArchExtension(extConfig, extPath, targets)
extConfigs, err := extMultiArchConfig.MultiArchConfigs()
if err != nil {
return err
}

if !flags.Publish && len(extConfigs) > 1 {
targets = []dist.Target{{OS: runtime.GOOS, Arch: runtime.GOARCH}}
extMultiArchConfig = pubbldpkg.NewMultiArchExtension(extConfig, extPath, targets)
extConfigs, err = extMultiArchConfig.MultiArchConfigs()
if err != nil {
return err
}
}

name := args[0]
if flags.Format == client.FormatFile {
switch ext := filepath.Ext(name); ext {
Expand Down Expand Up @@ -150,6 +161,8 @@ func ExtensionPackage(logger logging.Logger, cfg config.Config, packager Extensi
} else {
if len(extConfigs) == 1 {
pkgExtOpts.IndexOptions.Target = extConfigs[0].WithTargets[0]
} else {
logger.Warnf("A new '--target' flag is available to set the platform for the extension package, using '%s' as default", exPackageCfg.Platform.OS)
}

if err := packager.PackageExtension(cmd.Context(), pkgExtOpts); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ func NewClient(opts ...Option) (*Client, error) {
)
}

if len(client.cachedIndexManifests) == 0 {
client.cachedIndexManifests = make(map[name.Reference]*v1.IndexManifest)
}

client.lifecycleExecutor = build.NewLifecycleExecutor(client.logger, client.docker)

return client, nil
Expand Down
Loading

0 comments on commit 150d28c

Please sign in to comment.