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

feat: add new build.local.bazel entry for local, build-level Bazel configuration #9230

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 29 additions & 0 deletions docs-v2/content/en/schemas/v4beta9.json
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,29 @@
"description": "describes an artifact built with [Bazel](https://bazel.build/).",
"x-intellij-html-description": "describes an artifact built with <a href=\"https://bazel.build/\">Bazel</a>."
},
"BazelConfig": {
"properties": {
"args": {
"items": {
"type": "string"
},
"type": "array",
"description": "args to pass to `bazel build` for all the `bazel` targets in the build step.",
"x-intellij-html-description": "args to pass to <code>bazel build</code> for all the <code>bazel</code> targets in the build step.",
"default": "[]",
"examples": [
"[\"-flag\", \"--otherflag\"]"
]
}
},
"preferredOrder": [
"args"
],
"additionalProperties": false,
"type": "object",
"description": "contains some configuration to apply to `bazel` commands when building `BazelArtifact` targets locally.",
"x-intellij-html-description": "contains some configuration to apply to <code>bazel</code> commands when building <code>BazelArtifact</code> targets locally."
},
"BuildConfig": {
"type": "object",
"anyOf": [
Expand Down Expand Up @@ -3198,6 +3221,11 @@
},
"LocalBuild": {
"properties": {
"bazel": {
"$ref": "#/definitions/BazelConfig",
"description": "contains some configuration to apply to `bazel` commands when building `BazelArtifact` targets locally.",
"x-intellij-html-description": "contains some configuration to apply to <code>bazel</code> commands when building <code>BazelArtifact</code> targets locally."
},
"concurrency": {
"type": "integer",
"description": "how many artifacts can be built concurrently. 0 means \"no-limit\".",
Expand Down Expand Up @@ -3229,6 +3257,7 @@
},
"preferredOrder": [
"push",
"bazel",
"tryImportMissing",
"useDockerCLI",
"useBuildkit",
Expand Down
15 changes: 11 additions & 4 deletions pkg/skaffold/build/bazel/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (b *Builder) Build(ctx context.Context, out io.Writer, artifact *latest.Art
}

if b.pushImages {
return docker.Push(tarPath, tag, b.cfg, nil)
return docker.Push(tarPath, tag, b.dockerCfg, nil)
}
return b.loadImage(ctx, out, tarPath, tag)
}
Expand All @@ -67,6 +67,10 @@ func (b *Builder) buildTar(ctx context.Context, out io.Writer, workspace string,
args = append(args, a.BuildArgs...)
args = append(args, a.BuildTarget)

if b.cfg != nil {
args = append(args, b.cfg.BuildArgs...)
}

if output.IsColorable(out) {
args = append(args, "--color=yes")
} else {
Expand All @@ -82,7 +86,7 @@ func (b *Builder) buildTar(ctx context.Context, out io.Writer, workspace string,
return "", fmt.Errorf("running command: %w", err)
}

tarPath, err := bazelTarPath(ctx, workspace, a)
tarPath, err := b.bazelTarPath(ctx, workspace, a)
if err != nil {
return "", fmt.Errorf("getting bazel tar path: %w", err)
}
Expand All @@ -94,7 +98,6 @@ func (b *Builder) loadImage(ctx context.Context, out io.Writer, tarPath string,
manifest, err := tarball.LoadManifest(func() (io.ReadCloser, error) {
return os.Open(tarPath)
})

if err != nil {
return "", fmt.Errorf("loading manifest from tarball failed: %w", err)
}
Expand All @@ -118,7 +121,7 @@ func (b *Builder) loadImage(ctx context.Context, out io.Writer, tarPath string,
return imageID, nil
}

func bazelTarPath(ctx context.Context, workspace string, a *latest.BazelArtifact) (string, error) {
func (b *Builder) bazelTarPath(ctx context.Context, workspace string, a *latest.BazelArtifact) (string, error) {
args := []string{
"cquery",
a.BuildTarget,
Expand All @@ -131,6 +134,10 @@ func bazelTarPath(ctx context.Context, workspace string, a *latest.BazelArtifact
}
args = append(args, a.BuildArgs...)

if b.cfg != nil {
args = append(args, b.cfg.BuildArgs...)
}

cmd := exec.CommandContext(ctx, "bazel", args...)
cmd.Dir = workspace

Expand Down
16 changes: 9 additions & 7 deletions pkg/skaffold/build/bazel/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestBuildBazel(t *testing.T) {
t.Override(&util.DefaultExecCommand, testutil.CmdRun("bazel build //:app.tar --color=no").AndRunOut(
"bazel cquery //:app.tar --output starlark --starlark:expr target.files.to_list()[0].path",
"bin/app.tar").AndRunOut("bazel info execution_root", ""))
testutil.CreateFakeImageTar("bazel:app", "bin/app.tar")
t.RequireNoError(testutil.CreateFakeImageTar("bazel:app", "bin/app.tar"))

artifact := &latest.Artifact{
Workspace: ".",
Expand All @@ -47,7 +47,7 @@ func TestBuildBazel(t *testing.T) {
},
}

builder := NewArtifactBuilder(fakeLocalDaemon(), &mockConfig{}, false)
builder := NewArtifactBuilder(&latest.BazelConfig{}, fakeLocalDaemon(), &mockConfig{}, false)
_, err := builder.Build(context.Background(), io.Discard, artifact, "img:tag", platform.Matcher{})

t.CheckNoError(err)
Expand All @@ -59,7 +59,7 @@ func TestBazelTarPathPrependExecutionRoot(t *testing.T) {
t.Override(&util.DefaultExecCommand, testutil.CmdRun("bazel build //:app.tar --color=no").AndRunOut(
"bazel cquery //:app.tar --output starlark --starlark:expr target.files.to_list()[0].path",
"app.tar").AndRunOut("bazel info execution_root", ".."))
testutil.CreateFakeImageTar("bazel:app", "../app.tar")
t.RequireNoError(testutil.CreateFakeImageTar("bazel:app", "../app.tar"))

artifact := &latest.Artifact{
Workspace: "..",
Expand All @@ -70,7 +70,7 @@ func TestBazelTarPathPrependExecutionRoot(t *testing.T) {
},
}

builder := NewArtifactBuilder(fakeLocalDaemon(), &mockConfig{}, false)
builder := NewArtifactBuilder(&latest.BazelConfig{}, fakeLocalDaemon(), &mockConfig{}, false)
_, err := builder.Build(context.Background(), io.Discard, artifact, "img:tag", platform.Matcher{})

t.CheckNoError(err)
Expand All @@ -87,22 +87,24 @@ func TestBuildBazelFailInvalidTarget(t *testing.T) {
},
}

builder := NewArtifactBuilder(nil, &mockConfig{}, false)
builder := NewArtifactBuilder(&latest.BazelConfig{}, nil, &mockConfig{}, false)
_, err := builder.Build(context.Background(), io.Discard, artifact, "img:tag", platform.Matcher{})

t.CheckErrorContains("the bazel build target should end with .tar", err)
})
}

func TestBazelTarPath(t *testing.T) {
builder := NewArtifactBuilder(&latest.BazelConfig{}, nil, &mockConfig{}, false)

testutil.Run(t, "EmptyExecutionRoot", func(t *testutil.T) {
osSpecificPath := filepath.Join("absolute", "path", "bin")
t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
"bazel cquery //:skaffold_example.tar --output starlark --starlark:expr target.files.to_list()[0].path --arg1 --arg2",
fmt.Sprintf("%s\n", osSpecificPath),
).AndRunOut("bazel info execution_root", ""))

bazelBin, err := bazelTarPath(context.Background(), ".", &latest.BazelArtifact{
bazelBin, err := builder.bazelTarPath(context.Background(), ".", &latest.BazelArtifact{
BuildArgs: []string{"--arg1", "--arg2"},
BuildTarget: "//:skaffold_example.tar",
})
Expand All @@ -117,7 +119,7 @@ func TestBazelTarPath(t *testing.T) {
"bazel-bin/darwin-fastbuild-ST-confighash/path/to/bin\n",
).AndRunOut("bazel info execution_root", osSpecificPath))

bazelBin, err := bazelTarPath(context.Background(), ".", &latest.BazelArtifact{
bazelBin, err := builder.bazelTarPath(context.Background(), ".", &latest.BazelArtifact{
BuildArgs: []string{"--arg1", "--arg2"},
BuildTarget: "//:skaffold_example.tar",
})
Expand Down
13 changes: 9 additions & 4 deletions pkg/skaffold/build/bazel/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@ limitations under the License.

package bazel

import "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/docker"
import (
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
)

// Builder is an artifact builder that uses Bazel
type Builder struct {
cfg *latest.BazelConfig
localDocker docker.LocalDaemon
cfg docker.Config
dockerCfg docker.Config
pushImages bool
}

// NewArtifactBuilder returns a new bazel artifact builder
func NewArtifactBuilder(localDocker docker.LocalDaemon, cfg docker.Config, pushImages bool) *Builder {
func NewArtifactBuilder(cfg *latest.BazelConfig, localDocker docker.LocalDaemon, dockerCfg docker.Config, pushImages bool) *Builder {
return &Builder{
localDocker: localDocker,
cfg: cfg,
localDocker: localDocker,
dockerCfg: dockerCfg,
pushImages: pushImages,
}
}
2 changes: 1 addition & 1 deletion pkg/skaffold/build/local/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func newPerArtifactBuilder(b *Builder, a *latest.Artifact) (artifactBuilder, err
return dockerbuilder.NewArtifactBuilder(b.localDocker, b.cfg, b.local.UseDockerCLI, b.local.UseBuildkit, b.pushImages, b.artifactStore, b.sourceDependencies), nil

case a.BazelArtifact != nil:
return bazel.NewArtifactBuilder(b.localDocker, b.cfg, b.pushImages), nil
return bazel.NewArtifactBuilder(b.local.BazelConfig, b.localDocker, b.cfg, b.pushImages), nil

case a.JibArtifact != nil:
return jib.NewArtifactBuilder(b.localDocker, b.cfg, b.pushImages, b.skipTests, b.artifactStore), nil
Expand Down
14 changes: 14 additions & 0 deletions pkg/skaffold/schema/latest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ type LocalBuild struct {
// connects to a remote cluster.
Push *bool `yaml:"push,omitempty"`

// BazelConfig contains some configuration to apply to `bazel` commands
// when building `BazelArtifact` targets locally.
BazelConfig *BazelConfig `yaml:"bazel,omitempty"`

// TryImportMissing whether to attempt to import artifacts from
// Docker (either a local or remote registry) if not in the cache.
TryImportMissing bool `yaml:"tryImportMissing,omitempty"`
Expand All @@ -342,6 +346,16 @@ type LocalBuild struct {
Concurrency *int `yaml:"concurrency,omitempty"`
}

// BazelConfig contains some configuration to apply to `bazel` commands
// when building `BazelArtifact` targets locally.
type BazelConfig struct {
// BuildArgs are args to pass to `bazel build` for all the `bazel` targets
// in the build step.
//
// For example: `["-flag", "--otherflag"]`.
BuildArgs []string `yaml:"args,omitempty"`
}

// GoogleCloudBuild *beta* describes how to do a remote build on
// [Google Cloud Build](https://cloud.google.com/cloud-build/docs/).
// Docker and Jib artifacts can be built on Cloud Build. The `projectId` needs
Expand Down
Loading