Skip to content

Commit

Permalink
[RFC] Support Bazel platform mappings
Browse files Browse the repository at this point in the history
This is a proof of concept/RFC to support Bazel platforms in skaffold.

Skaffold uses github.com/opencontainers/image-spec/specs-go/v1 and
containerd strings like "linux/amd64" or "linux/arm64/v7" to represent
platforms.

Bazel's platform system uses bazel targets to define platforms
(https://bazel.build/extending/platforms). While there are common
cpu and os definitions available, each Bazel project defines its own
target platforms (!)

Currently, when skaffold invokes bazel, skaffold's platform information
is not passed to Bazel. Bazel defaults to the host platform, unless the
target skaffold is building explicitly specifies a platform.

This change allows a build-level setting for configuring Bazel and
defines a platform mapping from Skaffold platforms to Bazel platforms.
The Bazel artifact builder then passes the --platforms flag to Bazel if applicable.

This allows a Bazel + Skaffold project to flexibly target multiple platforms.

This PR is intended to start a discussion w/ the Skaffold team on whether
this should be supported and if so, how it should be implemented. If there's
interest we can discuss pushing forward from there. This doesn't include
any tests or documentation, and is not intended to be merged as-is.
  • Loading branch information
aran committed Mar 14, 2024
1 parent eeaf001 commit 82af6fa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/skaffold/build/bazel/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (b *Builder) Build(ctx context.Context, out io.Writer, artifact *latest.Art

a := artifact.ArtifactType.BazelArtifact

tarPath, err := b.buildTar(ctx, out, artifact.Workspace, a)
tarPath, err := b.buildTar(ctx, out, artifact.Workspace, a, matcher)
if err != nil {
return "", err
}
Expand All @@ -58,7 +58,7 @@ func (b *Builder) Build(ctx context.Context, out io.Writer, artifact *latest.Art

func (b *Builder) SupportedPlatforms() platform.Matcher { return platform.All }

func (b *Builder) buildTar(ctx context.Context, out io.Writer, workspace string, a *latest.BazelArtifact) (string, error) {
func (b *Builder) buildTar(ctx context.Context, out io.Writer, workspace string, a *latest.BazelArtifact, matcher platform.Matcher) (string, error) {
if !strings.HasSuffix(a.BuildTarget, ".tar") {
return "", errors.New("the bazel build target should end with .tar, see https://github.com/bazelbuild/rules_docker#using-with-docker-locally")
}
Expand All @@ -67,6 +67,16 @@ func (b *Builder) buildTar(ctx context.Context, out io.Writer, workspace string,
args = append(args, a.BuildArgs...)
args = append(args, a.BuildTarget)

platformMappings := a.PlatformMappings
for _, mapping := range platformMappings {
m, err := platform.Parse([]string{mapping.Platform})
if err == nil {
if matcher.Intersect(m).IsNotEmpty() {
args = append(args, fmt.Sprintf("--platforms=%s", mapping.BazelPlatformTarget))
}
}
}

if output.IsColorable(out) {
args = append(args, "--color=yes")
} else {
Expand Down
17 changes: 17 additions & 0 deletions pkg/skaffold/schema/latest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ type TaggerComponent struct {
Component TagPolicy `yaml:",inline" yamltags:"skipTrim"`
}

type BazelPlatformMapping struct {
Platform string `yaml:"platform,omitempty"`
BazelPlatformTarget string `yaml:"target,omitempty"`
}

// BuildType contains the specific implementation and parameters needed
// for the build step. Only one field should be populated.
type BuildType struct {
Expand Down Expand Up @@ -1597,6 +1602,18 @@ type BazelArtifact struct {
// BuildArgs are additional args to pass to `bazel build`.
// For example: `["-flag", "--otherflag"]`.
BuildArgs []string `yaml:"args,omitempty"`

// PlatformMappings configure the --platforms flag for `bazel build`
// based on the configured skaffold target platform.
// For example:
// ```yaml
// platforms:
// - platform: linux/amd64
// target: //platforms:linux-x86_64
// - platform: linux/arm64
// target: //platforms:linux-arm64
// ```
PlatformMappings []BazelPlatformMapping `yaml:"platforms,omitempty"`
}

// KoArtifact builds images using [ko](https://github.com/google/ko).
Expand Down

0 comments on commit 82af6fa

Please sign in to comment.