Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
Update protobuf rules; upgrade to rules_go 0.16.1 (#203)
Browse files Browse the repository at this point in the history
Previous protobuf rules used by example gRPC server/client
implementations did not use proto_library.  This change updates
to these rules (compatible with newer rules_go)

Upgrade to rules_go 0.16.1.  Previously, the e2e_test.sh assumed
the name of the generated go_binary matched the rule name, but
with changes introduced for cross-compilation, that assumption no
longer held true.  A genrule has been introduced to fix that.
  • Loading branch information
pcj authored and nlopezgi committed Nov 20, 2018
1 parent 588652b commit c839dce
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 85 deletions.
96 changes: 61 additions & 35 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
workspace(name = "io_bazel_rules_k8s")

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

http_archive(
name = "base_images_docker",
Expand Down Expand Up @@ -45,7 +45,6 @@ container_pull(
repository = "google/bazel",
)

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

# Gcloud installer
http_file(
Expand Down Expand Up @@ -119,27 +118,57 @@ py_library(
url = "https://pypi.python.org/packages/source/m/mock/mock-1.0.1.tar.gz",
)

http_archive(
name = "io_bazel_rules_go",
urls = ["https://github.com/bazelbuild/rules_go/archive/0.16.1.tar.gz"],
sha256 = "ced2749527318abeddd9d91f5e1555ed86e2b6bfd08677b750396e0ec5462bec",
strip_prefix = "rules_go-0.16.1",
)

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

go_rules_dependencies()

go_register_toolchains()

# ================================================================
# Imports for examples/
# ================================================================

git_repository(
name = "org_pubref_rules_protobuf",
commit = "4cc90ab2b9f4d829b9706221d4167bc7fb3bd247", # patched v0.8.1 (Sep 27 2017)
remote = "https://github.com/pubref/rules_protobuf.git",
http_archive(
name = "build_stack_rules_proto",
urls = ["https://github.com/stackb/rules_proto/archive/32204c6ec15aea9c015774223252be09d4156234.tar.gz"],
sha256 = "49ed4e86938d6d8c080382e958158a01b3b7a7adc65b91f80a804e542cd43649",
strip_prefix = "rules_proto-32204c6ec15aea9c015774223252be09d4156234",
)

load("@org_pubref_rules_protobuf//protobuf:rules.bzl", "proto_repositories")
load("@build_stack_rules_proto//:deps.bzl", "io_grpc_grpc_java")

io_grpc_grpc_java()

proto_repositories()
load("@io_grpc_grpc_java//:repositories.bzl", "grpc_java_repositories")

load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cpp_proto_repositories")
grpc_java_repositories(omit_com_google_protobuf = True)

cpp_proto_repositories()
load("@build_stack_rules_proto//java:deps.bzl", "java_grpc_library")

load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_repositories")
java_grpc_library()

java_proto_repositories()
load("@build_stack_rules_proto//cpp:deps.bzl", "cpp_grpc_library")

cpp_grpc_library()

load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")

grpc_deps()

load("@build_stack_rules_proto//go:deps.bzl", "go_grpc_library")

go_grpc_library()

load("@build_stack_rules_proto//python:deps.bzl", "python_grpc_library")

python_grpc_library()

# We use cc_image to build a sample service
load(
Expand All @@ -157,19 +186,6 @@ load(

_java_image_repos()

git_repository(
name = "io_bazel_rules_go",
commit = "ae70411645c171b2056d38a6a959e491949f9afe",
remote = "https://github.com/bazelbuild/rules_go.git",
)

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

go_repositories()

# We use go_image to build a sample service
load(
"@io_bazel_rules_docker//go:image.bzl",
Expand All @@ -178,10 +194,6 @@ load(

_go_image_repos()

load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_repositories")

go_proto_repositories()

git_repository(
name = "io_bazel_rules_python",
commit = "3e167dcfb17356c68588715ed324c5e9b76f391d",
Expand All @@ -196,17 +208,35 @@ load(

pip_repositories()

pip_import(
name = "protobuf_py_deps",
requirements = "@build_stack_rules_proto//python/requirements:protobuf.txt",
)

load("@protobuf_py_deps//:requirements.bzl", protobuf_pip_install = "pip_install")

protobuf_pip_install()

pip_import(
name = "grpc_py_deps",
requirements = "@build_stack_rules_proto//python:requirements.txt",
)

load("@grpc_py_deps//:requirements.bzl", grpc_pip_install = "pip_install")

grpc_pip_install()

pip_import(
name = "examples_helloworld_pip",
requirements = "//examples/hellogrpc/py:requirements.txt",
)

load(
"@examples_helloworld_pip//:requirements.bzl",
grpcpip_install = "pip_install",
setuptools_pip_install = "pip_install",
)

grpcpip_install()
setuptools_pip_install()

pip_import(
name = "examples_hellohttp_pip",
Expand All @@ -228,10 +258,6 @@ load(

_py_image_repos()

load("@org_pubref_rules_protobuf//python:rules.bzl", "py_proto_repositories")

py_proto_repositories()

git_repository(
name = "io_bazel_rules_jsonnet",
commit = "09ec18db5b9ad3129810f5f0ccc86363a8bfb6be",
Expand Down
2 changes: 1 addition & 1 deletion examples/hellogrpc/cc/client/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ cc_library(
name = "simple_lib",
srcs = ["simple.cc"],
hdrs = ["simple.h"],
deps = ["//examples/hellogrpc/proto:cc"],
deps = ["//examples/hellogrpc/proto:simple_cc_grpc"],
)

cc_binary(
Expand Down
2 changes: 1 addition & 1 deletion examples/hellogrpc/cc/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ load("@io_bazel_rules_docker//cc:image.bzl", "cc_image")
cc_image(
name = "server",
srcs = ["main.cc"],
deps = ["//examples/hellogrpc/proto:cc"],
deps = ["//examples/hellogrpc/proto:simple_cc_grpc"],
)

load("@k8s_deploy//:defaults.bzl", "k8s_deploy")
Expand Down
19 changes: 15 additions & 4 deletions examples/hellogrpc/go/client/BUILD
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package(default_visibility = ["//visibility:public"])

load("@io_bazel_rules_go//go:def.bzl", "go_binary")
load("@org_pubref_rules_protobuf//go:rules.bzl", "GRPC_COMPILE_DEPS")

go_binary(
name = "client",
name = "client_bin",
srcs = ["main.go"],
importpath = "github.com/bazelbuild/rules_k8s/rules_k8s/examples/hellogrpc/go/client",
deps = [
"//examples/hellogrpc/proto:go",
] + GRPC_COMPILE_DEPS,
"//examples/hellogrpc/proto:simple_go_grpc",
"@org_golang_google_grpc//:go_default_library",
],
)

# Normalize the binary name per
# https://github.com/bazelbuild/rules_go/issues/1239#issuecomment-361296415
genrule(
name = "client_exe",
srcs = [":client_bin"],
outs = ["client"],
cmd = "cp $(SRCS) $@",
output_to_bindir = True,
)

6 changes: 3 additions & 3 deletions examples/hellogrpc/go/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
package(default_visibility = ["//visibility:public"])

load("@io_bazel_rules_docker//go:image.bzl", "go_image")
load("@org_pubref_rules_protobuf//go:rules.bzl", "GRPC_COMPILE_DEPS")

go_image(
name = "server",
srcs = ["main.go"],
importpath = "github.com/bazelbuild/not-my-project/examples/hellogrpc/go/server",
deps = [
"//examples/hellogrpc/proto:go",
] + GRPC_COMPILE_DEPS,
"//examples/hellogrpc/proto:simple_go_grpc",
"@org_golang_google_grpc//:go_default_library",
],
)

load("@k8s_deploy//:defaults.bzl", "k8s_deploy")
Expand Down
3 changes: 2 additions & 1 deletion examples/hellogrpc/go/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
package main

import (
"context"
"flag"
"fmt"
"log"
"net"

pb "github.com/bazelbuild/rules_k8s/examples/hellogrpc/proto/go"
"golang.org/x/net/context"
"google.golang.org/grpc"
)

Expand All @@ -32,6 +32,7 @@ func main() {
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
log.Printf("Server listening on :%s...", *port)
s := grpc.NewServer()
pb.RegisterSimpleServer(s, &server{})
if err := s.Serve(lis); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions examples/hellogrpc/java/client/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ java_binary(
srcs = ["HelloClient.java"],
main_class = "io.bazel.rules_k8s.examples.helloworld.java.client.HelloClient",
deps = [
"//examples/hellogrpc/proto:java",
"//examples/hellogrpc/proto:java_compile_imports",
"@org_pubref_rules_protobuf//java:netty_runtime_deps",
"//examples/hellogrpc/proto:simple_java_grpc",
"@build_stack_rules_proto//java:grpc_netty",
],
)
5 changes: 2 additions & 3 deletions examples/hellogrpc/java/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ java_image(
srcs = ["HelloServer.java"],
main_class = "io.bazel.rules_k8s.examples.helloworld.java.server.HelloServer",
deps = [
"//examples/hellogrpc/proto:java",
"//examples/hellogrpc/proto:java_compile_imports",
"@org_pubref_rules_protobuf//java:netty_runtime_deps",
"//examples/hellogrpc/proto:simple_java_grpc",
"@build_stack_rules_proto//java:grpc_netty",
],
)

Expand Down
50 changes: 25 additions & 25 deletions examples/hellogrpc/proto/BUILD
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
package(default_visibility = ["//visibility:public"])
proto_library(
name = "simple_proto",
srcs = ["simple.proto"],
visibility = ["//visibility:public"],
)

load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cc_proto_library")
load("@build_stack_rules_proto//cpp:cpp_grpc_library.bzl", "cpp_grpc_library")

cc_proto_library(
name = "cc",
protos = [
"simple.proto",
],
cpp_grpc_library(
name = "simple_cc_grpc",
deps = [":simple_proto"],
visibility = ["//examples/hellogrpc/cc:__subpackages__"],
)

load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_library")
load("@build_stack_rules_proto//java:java_grpc_library.bzl", "java_grpc_library")

java_proto_library(
name = "java",
protos = [
"simple.proto",
],
java_grpc_library(
name = "simple_java_grpc",
deps = [":simple_proto"],
visibility = ["//examples/hellogrpc/java:__subpackages__"],
)

load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_library")
load("@build_stack_rules_proto//go:go_grpc_library.bzl", "go_grpc_library")

go_proto_library(
name = "go",
go_grpc_library(
name = "simple_go_grpc",
importpath = "github.com/bazelbuild/rules_k8s/examples/hellogrpc/proto/go",
protos = [
"simple.proto",
],
deps = [":simple_proto"],
visibility = ["//examples/hellogrpc/go:__subpackages__"],
)

load("@org_pubref_rules_protobuf//python:rules.bzl", "py_proto_library")
load("@build_stack_rules_proto//python:python_grpc_library.bzl", "python_grpc_library")

py_proto_library(
name = "py",
protos = [
"simple.proto",
],
python_grpc_library(
name = "simple_python_grpc",
deps = [":simple_proto"],
visibility = ["//examples/hellogrpc/py:__subpackages__"],
)
5 changes: 1 addition & 4 deletions examples/hellogrpc/py/client/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ package(default_visibility = ["//visibility:public"])

licenses(["notice"]) # Apache 2.0

load("@examples_helloworld_pip//:requirements.bzl", "requirement")

py_binary(
name = "client",
srcs = ["client.py"],
deps = [
"//examples/hellogrpc/proto:py",
requirement("grpcio"),
"//examples/hellogrpc/proto:simple_python_grpc",
],
)
3 changes: 1 addition & 2 deletions examples/hellogrpc/py/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
grpcio==1.6.0
setuptools==39.2.0
setuptools==39.2.0
3 changes: 1 addition & 2 deletions examples/hellogrpc/py/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ py_image(
# ~400KB. By partitioning things this way, the large grpcio layer remains
# unchanging and we can reduce the amount of image data we repush by ~99%!
layers = [
requirement("grpcio"),
requirement("setuptools"),
"//examples/hellogrpc/proto:py",
"//examples/hellogrpc/proto:simple_python_grpc",
],
main = "server.py",
)
Expand Down
4 changes: 3 additions & 1 deletion examples/hellogrpc/py/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ def await_termination(self):


def main():
hello_server = _HelloServer(_SimpleService(), 50051)
port = 50051
hello_server = _HelloServer(_SimpleService(), port)
print("Server listening at :%d..." % port)
hello_server.start()
hello_server.await_termination()

Expand Down

0 comments on commit c839dce

Please sign in to comment.