Skip to content

Commit

Permalink
Merge pull request #110 from estroz/feat/buildx-feature-gate
Browse files Browse the repository at this point in the history
feat(publish): optionally use 'docker buildx build' for multi-arch
  • Loading branch information
Alex Flom authored Oct 7, 2021
2 parents 2a018ce + 82bb527 commit 11d38e4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 25 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ Bundle management is a two part process:
1. Bundle Creation (Internet Connected)
1. Bundle Publishing (Disconnected)

## Requirements
## Required dependencies

- [`docker buildx`][docker-buildx] (called by `publish`)
- [`podman`][podman] (only required if not building multi-arch images, see below)

## Multi-arch catalog images

[`docker buildx`][docker-buildx] is required to build multi-arch catalog images;
`docker buildx build` is invoked by `publish` when `--buildx-platforms` is set.

## Usage

Expand Down Expand Up @@ -73,3 +78,4 @@ TODO: link to the following once a release is cut.
[config-spec]:pkg/config/v1alpha1/config_types.go
[go]:https://golang.org/dl/
[docker-buildx]:https://docs.docker.com/buildx/working-with-buildx/
[podman]:https://podman.io/getting-started/
60 changes: 48 additions & 12 deletions pkg/bundle/publish/catalog_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"

"github.com/RedHatGov/bundle/pkg/operator"
Expand Down Expand Up @@ -164,32 +163,69 @@ func (o *Options) buildCatalogImage(ctx context.Context, ref reference.DockerIma

logrus.Infof("Building rendered catalog image: %s", ref.Exact())

if len(o.BuildxPlatforms) == 0 {
err = o.buildPodman(ctx, ref, dir, dockerfile)
} else {
err = o.buildDockerBuildx(ctx, ref, dir, dockerfile)
}
return err
}

func (o *Options) buildDockerBuildx(ctx context.Context, ref reference.DockerImageReference, dir, dockerfile string) error {
exactRef := ref.Exact()

args := []string{
"build", "buildx",
"-t", exactRef,
"-f", dockerfile,
"--platform", strings.Join(o.BuildxPlatforms, ","),
"--push",
dir,
}
cmd := exec.CommandContext(ctx, "docker", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := runDebug(cmd); err != nil {
return err
}

return nil
}

func (o *Options) buildPodman(ctx context.Context, ref reference.DockerImageReference, dir, dockerfile string) error {
exactRef := ref.Exact()

bargs := []string{
"build",
"-t", ref.Exact(),
"-t", exactRef,
"-f", dockerfile,
// TODO: NO MULTIARCH SUPPORT... YET.
//"--platform", strings.Join(o.CatalogPlatforms, ","),
dir,
}
bcmd := exec.CommandContext(ctx, "podman", bargs...)
bcmd.Stdout = os.Stdout
bcmd.Stderr = os.Stderr
err = bcmd.Run()
if err != nil {
logrus.Error(bcmd.Stderr)
if err := runDebug(bcmd); err != nil {
return err
}
logrus.Debugf("command: %s", strings.Join(bcmd.Args, " "))

pargs := []string{
"push",
ref.Exact(),
fmt.Sprintf("--tls-verify=%s", strconv.FormatBool(!o.SkipTLS)),
exactRef,
}
if o.SkipTLS {
pargs = append(pargs, "--tls-verify=false")
}
pcmd := exec.CommandContext(ctx, "podman", pargs...)
logrus.Info(pcmd)
pcmd.Stdout = os.Stdout
pcmd.Stderr = os.Stderr
return pcmd.Run()
if err := runDebug(pcmd); err != nil {
return err
}

return nil
}

func runDebug(cmd *exec.Cmd) error {
logrus.Debugf("command: %s", strings.Join(cmd.Args, " "))
return cmd.Run()
}
4 changes: 0 additions & 4 deletions pkg/bundle/publish/catalog_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ func Test_buildCatalogImage(t *testing.T) {
SkipTLS: true,
},
ArchivePath: tt.fields.archivePath,
CatalogPlatforms: []string{
"linux/amd64",
"linux/arm64",
},
}

ref := reference.DockerImageReference{
Expand Down
16 changes: 9 additions & 7 deletions pkg/bundle/publish/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ import (
type Options struct {
*cli.RootOptions

ArchivePath string
ToMirror string
CatalogPlatforms []string
}
ArchivePath string
ToMirror string

var defaultPlatforms = []string{"linux/amd64"}
BuildxPlatforms []string
}

func (o *Options) BindFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.ArchivePath, "archive", "", "The archive file path.")
fs.StringVar(&o.ToMirror, "to-mirror", "", "The URL to the destination mirror registry")
fs.StringSliceVar(&o.CatalogPlatforms, "catalog-platforms", defaultPlatforms, "Platforms to build a catalog manifest list for. "+
"This list does NOT filter operator bundle manifest list platforms within the catalog")
fs.StringSliceVar(&o.BuildxPlatforms, "buildx-platforms", nil,
"If set, the command will invoke 'docker buildx build' to build a catalog manifest list "+
"for the specified platforms, ex. linux/amd64, instead of 'podman build' for the host platform. "+
"The 'buildx' plugin and accompanying configuration MUST be installed on the build host. "+
"This list does NOT filter operator bundle manifest list platforms within the catalog")
}

// ValidatePaths validate the existence of paths from user flags
Expand Down

0 comments on commit 11d38e4

Please sign in to comment.