-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1662 from priyawadhwa/bazel
Added bazel in local execution environment
- Loading branch information
Showing
39 changed files
with
664 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
8
integration/examples/test-plugin/local/bazel/k8s/k8s-pod.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
integration/examples/test-plugin/local/bazel/skaffold.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.