Skip to content

Commit

Permalink
Add support for --docker_platform flag (#994)
Browse files Browse the repository at this point in the history
* Add support for --platform when executing docker create

Signed-off-by: Thomas Lam <thomaslam@canva.com>

* Undo redundant changes

Signed-off-by: Thomas Lam <thomaslam@canva.com>

---------

Signed-off-by: Thomas Lam <thomaslam@canva.com>
Co-authored-by: DaveGay <davegay@google.com>
  • Loading branch information
lamcw and DaveGay authored Aug 8, 2023
1 parent a91618c commit 059f56e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/rbe_configs_gen/rbe_configs_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
toolchainContainer = flag.String("toolchain_container", "", "Repository path to toolchain image to generate configs for. E.g., l.gcr.io/google/rbe-ubuntu16-04:latest")
execOS = flag.String("exec_os", "", "The OS (linux|windows) of the toolchain container image a.k.a, the execution platform in Bazel.")
targetOS = flag.String("target_os", "", "The OS (linux|windows) artifacts built will target a.k.a, the target platform in Bazel.")
dockerPlatform = flag.String("docker_platform", "", "(Optional) Set platform when creating container, if given the Docker server is multi-platform capable.")

// Optional input arguments.
bazelVersion = flag.String("bazel_version", "", "(Optional) Bazel release version to generate configs for. E.g., 4.0.0. If unspecified, the latest available Bazel release is picked.")
Expand Down Expand Up @@ -158,6 +159,7 @@ func main() {
BazelVersion: *bazelVersion,
BazelPath: *bazelPath,
ToolchainContainer: *toolchainContainer,
DockerPlatform: *dockerPlatform,
ExecOS: *execOS,
TargetOS: *targetOS,
OutputTarball: *outputTarball,
Expand Down
3 changes: 3 additions & 0 deletions pkg/rbeconfigsgen/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Options struct {
BazelPath string
// ToolchainContainer is the docker image of the toolchain container to generate configs for.
ToolchainContainer string
// Specify --platform when executing docker create.
DockerPlatform string
// ExecOS is the OS of the toolchain container image or the OS in which the build actions will
// execute.
ExecOS string
Expand Down Expand Up @@ -273,6 +275,7 @@ func (o *Options) Validate() error {
log.Printf("ToolchainContainer=%q", o.ToolchainContainer)
log.Printf("ExecOS=%q", o.ExecOS)
log.Printf("TargetOS=%q", o.TargetOS)
log.Printf("DockerPlatform=%q", o.DockerPlatform)
log.Printf("OutputTarball=%q", o.OutputTarball)
log.Printf("OutputSourceRoot=%q", o.OutputSourceRoot)
log.Printf("OutputConfigPath=%q", o.OutputConfigPath)
Expand Down
12 changes: 9 additions & 3 deletions pkg/rbeconfigsgen/rbeconfigsgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func BazeliskDownloadInfo(os string) (string, string, error) {
// newDockerRunner creates a new running container of the given containerImage. stopContainer
// determines if the cleanup function on the dockerRunner will stop the running container when
// called.
func newDockerRunner(containerImage string, stopContainer bool) (*dockerRunner, error) {
func newDockerRunner(containerImage string, dockerPlatform string, stopContainer bool) (*dockerRunner, error) {
if containerImage == "" {
return nil, fmt.Errorf("container image was not specified")
}
Expand All @@ -271,7 +271,13 @@ func newDockerRunner(containerImage string, stopContainer bool) (*dockerRunner,
log.Printf("Resolved toolchain image %q to fully qualified reference %q.", d.containerImage, resolvedImage)
d.resolvedImage = resolvedImage

cid, err := runCmd(d.dockerPath, "create", "--rm", d.resolvedImage, "sleep", "infinity")
args := []string{"create", "--rm"}
if dockerPlatform != "" {
args = append(args, "--platform", dockerPlatform)
}
args = append(args, d.resolvedImage, "sleep", "infinity")

cid, err := runCmd(d.dockerPath, args...)
if err != nil {
return nil, fmt.Errorf("failed to create a container with the toolchain container image: %w", err)
}
Expand Down Expand Up @@ -982,7 +988,7 @@ func Run(o Options) error {
if err := processTempDir(&o); err != nil {
return fmt.Errorf("unable to initialize a local temporary working directory to store intermediate files: %w", err)
}
d, err := newDockerRunner(o.ToolchainContainer, o.Cleanup)
d, err := newDockerRunner(o.ToolchainContainer, o.DockerPlatform, o.Cleanup)
if err != nil {
return fmt.Errorf("failed to initialize a docker container: %w", err)
}
Expand Down

0 comments on commit 059f56e

Please sign in to comment.