Skip to content

Commit

Permalink
Merge pull request #1662 from priyawadhwa/bazel
Browse files Browse the repository at this point in the history
Added bazel in local execution environment
  • Loading branch information
priyawadhwa committed Feb 21, 2019
2 parents a98ad52 + ff280e7 commit cff3918
Show file tree
Hide file tree
Showing 39 changed files with 664 additions and 114 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
out/
examples/bazel/bazel-*
integration/examples/bazel/bazel-*
integration/examples/test-plugin/local/bazel/bazel-*
*.new
.idea/
docs/.firebase
Expand Down
21 changes: 8 additions & 13 deletions cmd/skaffold/app/cmd/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewCmdDiagnose(out io.Writer) *cobra.Command {
}

func doDiagnose(out io.Writer) error {
_, config, err := newRunner(opts)
runner, config, err := newRunner(opts)
if err != nil {
return errors.Wrap(err, "creating runner")
}
Expand All @@ -58,14 +58,14 @@ func doDiagnose(out io.Writer) error {
fmt.Fprintln(out, "Configuration version:", config.APIVersion)
fmt.Fprintln(out, "Number of artifacts:", len(config.Build.Artifacts))

if err := diagnoseArtifacts(out, config.Build.Artifacts); err != nil {
if err := diagnoseArtifacts(out, runner.Builder, config.Build.Artifacts); err != nil {
return errors.Wrap(err, "running diagnostic on artifacts")
}

return nil
}

func diagnoseArtifacts(out io.Writer, artifacts []*latest.Artifact) error {
func diagnoseArtifacts(out io.Writer, builder build.Builder, artifacts []*latest.Artifact) error {
ctx := context.Background()

for _, artifact := range artifacts {
Expand All @@ -80,11 +80,11 @@ func diagnoseArtifacts(out io.Writer, artifacts []*latest.Artifact) error {
fmt.Fprintf(out, " - Size of the context: %vbytes\n", size)
}

timeDeps1, deps, err := timeToListDependencies(ctx, artifact)
timeDeps1, deps, err := timeToListDependencies(ctx, builder, artifact)
if err != nil {
return errors.Wrap(err, "listing artifact dependencies")
}
timeDeps2, _, err := timeToListDependencies(ctx, artifact)
timeDeps2, _, err := timeToListDependencies(ctx, builder, artifact)
if err != nil {
return errors.Wrap(err, "listing artifact dependencies")
}
Expand All @@ -107,15 +107,10 @@ func diagnoseArtifacts(out io.Writer, artifacts []*latest.Artifact) error {
return nil
}

func timeToListDependencies(ctx context.Context, a *latest.Artifact) (time.Duration, []string, error) {
func timeToListDependencies(ctx context.Context, builder build.Builder, a *latest.Artifact) (time.Duration, []string, error) {
start := time.Now()

deps, err := build.DependenciesForArtifact(ctx, a)
if err != nil {
return 0, nil, errors.Wrap(err, "listing artifact dependencies")
}

return time.Since(start), deps, nil
paths, err := builder.DependenciesForArtifact(ctx, a)
return time.Since(start), paths, err
}

func timeToComputeMTimes(deps []string) (time.Duration, error) {
Expand Down
4 changes: 3 additions & 1 deletion integration/examples/test-plugin/gcb/leeroy-app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ FROM golang:1.10.1-alpine3.7 as builder
COPY app.go .
RUN go build -o /app .

FROM alpine:3.7
FROM alpine:3.7 as targetStage
CMD ["./app"]
COPY --from=builder /app .

FROM busybox as unusedStage
4 changes: 2 additions & 2 deletions integration/examples/test-plugin/gcb/skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ build:
context: ./leeroy-app/
plugin:
name: docker
properties:
target: targetStage
executionEnvironment:
name: googleCloudBuild
properties:
projectId: k8s-skaffold
deploy:
kubectl:
manifests:
Expand Down
9 changes: 9 additions & 0 deletions integration/examples/test-plugin/local/bazel/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@io_bazel_rules_docker//go:image.bzl", "go_image")

go_image(
name = "skaffold_example",
srcs = ["main.go"],
goos = "linux",
goarch = "amd64",
static = "on",
)
30 changes: 30 additions & 0 deletions integration/examples/test-plugin/local/bazel/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
workspace(name = "skaffold")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_docker",
strip_prefix = "rules_docker-0.7.0",
urls = ["https://github.com/bazelbuild/rules_docker/archive/v0.7.0.tar.gz"],
sha256 = "aed1c249d4ec8f703edddf35cbe9dfaca0b5f5ea6e4cd9e83e99f3b0d1136c3d",
)

http_archive(
name = "io_bazel_rules_go",
urls = ["https://github.com/bazelbuild/rules_go/releases/download/0.16.5/rules_go-0.16.5.tar.gz"],
sha256 = "7be7dc01f1e0afdba6c8eb2b43d2fa01c743be1b9273ab1eaf6c233df078d705",
)

load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")

go_rules_dependencies()
go_register_toolchains(
go_version = "1.10.1",
)

load(
"@io_bazel_rules_docker//go:image.bzl",
_go_image_repos = "repositories",
)

_go_image_repos()
8 changes: 8 additions & 0 deletions integration/examples/test-plugin/local/bazel/k8s/k8s-pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Pod
metadata:
name: bazel
spec:
containers:
- name: bazel
image: gcr.io/k8s-skaffold/skaffold-bazel
13 changes: 13 additions & 0 deletions integration/examples/test-plugin/local/bazel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"fmt"
"time"
)

func main() {
for {
fmt.Println("Hello bazel!!!!")
time.Sleep(time.Second * 1)
}
}
10 changes: 10 additions & 0 deletions integration/examples/test-plugin/local/bazel/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: skaffold/v1beta5
kind: Config
build:
artifacts:
- image: gcr.io/k8s-skaffold/skaffold-bazel
context: .
plugin:
name: bazel
properties:
target: //:skaffold_example.tar
6 changes: 5 additions & 1 deletion integration/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,13 @@ func TestRun(t *testing.T) {
deployments: []string{"skaffold-helm"},
remoteOnly: true,
}, {
description: "docker in gcb plugin",
description: "docker plugin in gcb exec environment",
dir: "examples/test-plugin/gcb",
deployments: []string{"leeroy-app", "leeroy-web"},
}, {
description: "bazel plugin in local exec environment",
dir: "examples/test-plugin/local/bazel",
pods: []string{"bazel"},
},
}

Expand Down
127 changes: 127 additions & 0 deletions pkg/skaffold/build/bazel/bazel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
Copyright 2019 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bazel

import (
"context"
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/bazel"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/local"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)

// Builder builds artifacts with Bazel.
type Builder struct {
opts *config.SkaffoldOptions
env *latest.ExecutionEnvironment
}

// NewBuilder creates a new Builder that builds artifacts with Bazel.
func NewBuilder() *Builder {
return &Builder{}
}

// Init stores skaffold options and the execution environment
func (b *Builder) Init(opts *config.SkaffoldOptions, env *latest.ExecutionEnvironment) {
b.opts = opts
b.env = env
}

// Labels are labels specific to Bazel.
func (b *Builder) Labels() map[string]string {
return map[string]string{
constants.Labels.Builder: "bazel",
}
}

// DependenciesForArtifact returns the dependencies for this bazel artifact
func (b *Builder) DependenciesForArtifact(ctx context.Context, artifact *latest.Artifact) ([]string, error) {
if err := setArtifact(artifact); err != nil {
return nil, err
}
if artifact.BazelArtifact == nil {
return nil, errors.New("bazel artifact is nil")
}
paths, err := bazel.GetDependencies(ctx, artifact.Workspace, artifact.BazelArtifact)
if err != nil {
return nil, errors.Wrap(err, "getting bazel dependencies")
}
return util.AbsolutePaths(artifact.Workspace, paths), nil
}

// Build is responsible for building artifacts in their respective execution environments
// The builder plugin is also responsible for setting any necessary defaults
func (b *Builder) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, artifacts []*latest.Artifact) ([]build.Artifact, error) {
switch b.env.Name {
case constants.Local:
return b.local(ctx, out, tags, artifacts)
default:
return nil, errors.Errorf("%s is not a supported environment for builder bazel", b.env.Name)
}
}

// local sets any necessary defaults and then builds artifacts with bazel locally
func (b *Builder) local(ctx context.Context, out io.Writer, tags tag.ImageTags, artifacts []*latest.Artifact) ([]build.Artifact, error) {
var l *latest.LocalBuild
if err := util.CloneThroughJSON(b.env.Properties, &l); err != nil {
return nil, errors.Wrap(err, "converting execution env to localBuild struct")
}
if l == nil {
l = &latest.LocalBuild{}
}
kubeContext, err := kubectx.CurrentContext()
if err != nil {
return nil, errors.Wrap(err, "getting current cluster context")
}
builder, err := local.NewBuilder(l, kubeContext, b.opts.SkipTests)
if err != nil {
return nil, errors.Wrap(err, "getting local builder")
}
for _, a := range artifacts {
if err := setArtifact(a); err != nil {
return nil, errors.Wrapf(err, "setting artifact %s", a.ImageName)
}
}
return builder.Build(ctx, out, tags, artifacts)
}

func setArtifact(artifact *latest.Artifact) error {
if artifact.ArtifactType.BazelArtifact != nil {
return nil
}
var a *latest.BazelArtifact
if err := yaml.UnmarshalStrict(artifact.BuilderPlugin.Contents, &a); err != nil {
return errors.Wrap(err, "unmarshalling bazel artifact")
}
if a == nil {
return errors.New("artifact is nil")
}
if a.BuildTarget == "" {
return errors.Errorf("%s must have an associated build target", artifact.ImageName)
}
artifact.ArtifactType.BazelArtifact = a
return nil
}

0 comments on commit cff3918

Please sign in to comment.