From c3bfbc75d9e26435a2bdd99cd5a1e2a0b4114703 Mon Sep 17 00:00:00 2001 From: William Chang Date: Thu, 28 Dec 2017 12:44:42 -0800 Subject: [PATCH] Create bazel rules for Go gRPC client libraries (#340) Add Go bazel rules for protos and gRPC. Signed-off-by: misterwilliam --- .circleci/config.yml | 2 +- WORKSPACE | 8 +- api/BUILD | 229 ++++++++++++++++++++++++++++++++++++ api/base.proto | 1 - bazel/api_build_system.bzl | 2 + bazel/repositories.bzl | 48 ++++++++ ci/run_envoy_docker.sh | 2 +- test/build/BUILD | 19 +++ test/build/go_build_test.go | 18 +++ 9 files changed, 321 insertions(+), 8 deletions(-) create mode 100644 test/build/go_build_test.go diff --git a/.circleci/config.yml b/.circleci/config.yml index 7384318f..ac07f553 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -2,7 +2,7 @@ version: 2 jobs: test: docker: - - image: lyft/envoy-build:114e24c6fd05fc026492e9d2ca5608694e5ea59d + - image: envoyproxy/envoy-build:52f6880ffbf761c9b809fc3ac208900956ff16b4 resource_class: xlarge working_directory: /source steps: diff --git a/WORKSPACE b/WORKSPACE index 9410767d..007ec17e 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -30,15 +30,13 @@ new_http_archive( url = "https://pypi.python.org/packages/source/s/six/six-1.10.0.tar.gz#md5=34eed507548117b2ab523ab14b2f8b55", ) -git_repository( +http_archive( name = "io_bazel_rules_go", - remote = "https://github.com/bazelbuild/rules_go.git", - commit = "4374be38e9a75ff5957c3922adb155d32086fe14", + url = "https://github.com/bazelbuild/rules_go/releases/download/0.8.1/rules_go-0.8.1.tar.gz", + sha256 = "90bb270d0a92ed5c83558b2797346917c46547f6f7103e648941ecdb6b9d0e72", ) load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains") load("@com_lyft_protoc_gen_validate//bazel:go_proto_library.bzl", "go_proto_repositories") go_proto_repositories(shared=0) go_rules_dependencies() go_register_toolchains() -load("@io_bazel_rules_go//proto:def.bzl", "proto_register_toolchains") -proto_register_toolchains() diff --git a/api/BUILD b/api/BUILD index f5421459..c09ca79f 100644 --- a/api/BUILD +++ b/api/BUILD @@ -1,4 +1,5 @@ load("//bazel:api_build_system.bzl", "api_proto_library") +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library", "go_grpc_library") licenses(["notice"]) # Apache 2 @@ -7,12 +8,39 @@ api_proto_library( srcs = ["address.proto"], ) +go_proto_library( + name = "address_go_proto", + importpath = "github.com/envoyproxy/data-plane-api/api/address", + proto = ":address", + visibility = ["//visibility:public"], + deps = [ + "@com_github_gogo_protobuf//:gogo_proto_go", + "@com_github_golang_protobuf//ptypes/wrappers:go_default_library", + "@com_lyft_protoc_gen_validate//validate:go_default_library", + ], +) + api_proto_library( name = "base", srcs = ["base.proto"], deps = [":address"], ) +go_proto_library( + name = "base_go_proto", + importpath = "github.com/envoyproxy/data-plane-api/api/base", + proto = ":base", + visibility = ["//visibility:public"], + deps = [ + ":address_go_proto", + "@com_github_gogo_protobuf//:gogo_proto_go", + "@com_github_golang_protobuf//ptypes/duration:go_default_library", + "@com_github_golang_protobuf//ptypes/struct:go_default_library", + "@com_github_golang_protobuf//ptypes/wrappers:go_default_library", + "@com_lyft_protoc_gen_validate//validate:go_default_library", + ], +) + api_proto_library( name = "bootstrap", srcs = ["bootstrap.proto"], @@ -27,12 +55,44 @@ api_proto_library( ], ) +go_proto_library( + name = "bootstrap_go_proto", + importpath = "github.com/envoyproxy/data-plane-api/api/bootstrap", + proto = ":bootstrap", + visibility = ["//visibility:public"], + deps = [ + ":address_go_proto", + ":base_go_proto", + ":cds_go_grpc", + ":lds_go_grpc", + ":sds_go_grpc", + ":stats_go_proto", + ":trace_go_proto", + "@com_github_gogo_protobuf//:gogo_proto_go", + "@com_github_golang_protobuf//ptypes/duration:go_default_library", + "@com_lyft_protoc_gen_validate//validate:go_default_library", + ], +) + api_proto_library( name = "health_check", srcs = ["health_check.proto"], deps = [":base"], ) +go_proto_library( + name = "health_check_go_proto", + importpath = "github.com/envoyproxy/data-plane-api/api/health_check", + proto = ":health_check", + visibility = ["//visibility:public"], + deps = [ + ":base_go_proto", + "@com_github_golang_protobuf//ptypes/duration:go_default_library", + "@com_github_golang_protobuf//ptypes/wrappers:go_default_library", + "@com_lyft_protoc_gen_validate//validate:go_default_library", + ], +) + api_proto_library( name = "cds", srcs = ["cds.proto"], @@ -47,6 +107,27 @@ api_proto_library( ], ) +go_grpc_library( + name = "cds_go_grpc", + importpath = "github.com/envoyproxy/data-plane-api/api/cds", + proto = ":cds", + visibility = ["//visibility:public"], + deps = [ + ":address_go_proto", + ":base_go_proto", + ":discovery_go_grpc", + ":health_check_go_proto", + ":protocol_go_proto", + ":sds_go_grpc", + "@com_github_gogo_protobuf//:gogo_proto_go", + "@com_github_golang_protobuf//ptypes/duration:go_default_library", + "@com_github_golang_protobuf//ptypes/struct:go_default_library", + "@com_github_golang_protobuf//ptypes/wrappers:go_default_library", + "@com_lyft_protoc_gen_validate//validate:go_default_library", + "@googleapis//:http_api_go_proto", + ], +) + api_proto_library( name = "discovery", srcs = ["discovery.proto"], @@ -54,6 +135,17 @@ api_proto_library( deps = [":base"], ) +go_grpc_library( + name = "discovery_go_grpc", + importpath = "github.com/envoyproxy/data-plane-api/api/discovery", + proto = ":discovery", + visibility = ["//visibility:public"], + deps = [ + ":base_go_proto", + "@com_github_golang_protobuf//ptypes/any:go_default_library", + ], +) + api_proto_library( name = "eds", srcs = ["eds.proto"], @@ -66,6 +158,23 @@ api_proto_library( ], ) +go_grpc_library( + name = "eds_go_grpc", + importpath = "github.com/envoyproxy/data-plane-api/api/eds", + proto = ":eds", + visibility = ["//visibility:public"], + deps = [ + ":address_go_proto", + ":base_go_proto", + ":discovery_go_grpc", + ":health_check_go_proto", + "@com_github_golang_protobuf//ptypes/duration:go_default_library", + "@com_github_golang_protobuf//ptypes/wrappers:go_default_library", + "@com_lyft_protoc_gen_validate//validate:go_default_library", + "@googleapis//:http_api_go_proto", + ], +) + api_proto_library( name = "grpc_cluster", srcs = ["grpc_cluster.proto"], @@ -81,6 +190,19 @@ api_proto_library( ], ) +go_grpc_library( + name = "hds_go_grpc", + importpath = "github.com/envoyproxy/data-plane-api/api/hds", + proto = ":hds", + visibility = ["//visibility:public"], + deps = [ + ":base_go_proto", + ":health_check_go_proto", + "@com_github_golang_protobuf//ptypes/duration:go_default_library", + "@googleapis//:http_api_go_proto", + ], +) + api_proto_library( name = "lds", srcs = ["lds.proto"], @@ -93,6 +215,23 @@ api_proto_library( ], ) +go_grpc_library( + name = "lds_go_grpc", + importpath = "github.com/envoyproxy/data-plane-api/api/lds", + proto = ":lds", + visibility = ["//visibility:public"], + deps = [ + ":address_go_proto", + ":base_go_proto", + ":discovery_go_grpc", + ":sds_go_grpc", + "@com_github_golang_protobuf//ptypes/struct:go_default_library", + "@com_github_golang_protobuf//ptypes/wrappers:go_default_library", + "@com_lyft_protoc_gen_validate//validate:go_default_library", + "@googleapis//:http_api_go_proto", + ], +) + api_proto_library( name = "metrics", srcs = ["metrics_service.proto"], @@ -109,12 +248,33 @@ api_proto_library( srcs = ["protocol.proto"], ) +go_proto_library( + name = "protocol_go_proto", + importpath = "github.com/envoyproxy/data-plane-api/api/protocol", + proto = ":protocol", + visibility = ["//visibility:public"], + deps = [ + "@com_github_golang_protobuf//ptypes/wrappers:go_default_library", + "@com_lyft_protoc_gen_validate//validate:go_default_library", + ], +) + api_proto_library( name = "rls", srcs = ["rls.proto"], has_services = 1, ) +go_grpc_library( + name = "rls_go_grpc", + importpath = "github.com/envoyproxy/data-plane-api/api/rls", + proto = ":rls", + visibility = ["//visibility:public"], + deps = [ + "@com_lyft_protoc_gen_validate//validate:go_default_library", + ], +) + api_proto_library( name = "rds", srcs = ["rds.proto"], @@ -126,6 +286,23 @@ api_proto_library( ], ) +go_grpc_library( + name = "rds_go_grpc", + importpath = "github.com/envoyproxy/data-plane-api/api/rds", + proto = ":rds", + visibility = ["//visibility:public"], + deps = [ + ":auth_go_proto", + ":base_go_proto", + ":discovery_go_grpc", + "@com_github_gogo_protobuf//:gogo_proto_go", + "@com_github_golang_protobuf//ptypes/duration:go_default_library", + "@com_github_golang_protobuf//ptypes/wrappers:go_default_library", + "@com_lyft_protoc_gen_validate//validate:go_default_library", + "@googleapis//:http_api_go_proto", + ], +) + api_proto_library( name = "sds", srcs = ["sds.proto"], @@ -136,6 +313,20 @@ api_proto_library( ], ) +go_grpc_library( + name = "sds_go_grpc", + importpath = "github.com/envoyproxy/data-plane-api/api/sds", + proto = ":sds", + visibility = ["//visibility:public"], + deps = [ + ":base_go_proto", + ":discovery_go_grpc", + "@com_github_golang_protobuf//ptypes/wrappers:go_default_library", + "@com_lyft_protoc_gen_validate//validate:go_default_library", + "@googleapis//:http_api_go_proto", + ], +) + api_proto_library( name = "stats", srcs = ["stats.proto"], @@ -144,11 +335,35 @@ api_proto_library( ], ) +go_proto_library( + name = "stats_go_proto", + importpath = "github.com/envoyproxy/data-plane-api/api/stats", + proto = ":stats", + visibility = ["//visibility:public"], + deps = [ + ":address_go_proto", + "@com_github_golang_protobuf//ptypes/struct:go_default_library", + "@com_github_golang_protobuf//ptypes/wrappers:go_default_library", + "@com_lyft_protoc_gen_validate//validate:go_default_library", + ], +) + api_proto_library( name = "trace", srcs = ["trace.proto"], ) +go_proto_library( + name = "trace_go_proto", + importpath = "github.com/envoyproxy/data-plane-api/api/trace", + proto = ":trace", + visibility = ["//visibility:public"], + deps = [ + "@com_github_golang_protobuf//ptypes/struct:go_default_library", + "@com_lyft_protoc_gen_validate//validate:go_default_library", + ], +) + api_proto_library( name = "auth", srcs = ["auth.proto"], @@ -157,6 +372,20 @@ api_proto_library( ], ) +go_proto_library( + name = "auth_go_proto", + importpath = "github.com/envoyproxy/data-plane-api/auth", + proto = ":auth", + visibility = ["//visibility:public"], + deps = [ + "//api:address_go_proto", + "//api:sds_go_grpc", + "@com_github_golang_protobuf//ptypes/struct:go_default_library", + "@com_github_golang_protobuf//ptypes/timestamp:go_default_library", + "@googleapis//:rpc_status_go_proto", + ], +) + # TODO(htuch): Grow this to cover everything we want to generate docs for, so we can just invoke # bazel build //api --aspects tools/protodoc/protodoc.bzl%proto_doc_aspect --output_groups=rst proto_library( diff --git a/api/base.proto b/api/base.proto index 49b42892..2c4b5ff3 100644 --- a/api/base.proto +++ b/api/base.proto @@ -1,7 +1,6 @@ syntax = "proto3"; package envoy.api.v2; -option go_package = "api"; import "api/address.proto"; diff --git a/bazel/api_build_system.bzl b/bazel/api_build_system.bzl index 9323ba42..e3cde960 100644 --- a/bazel/api_build_system.bzl +++ b/bazel/api_build_system.bzl @@ -36,6 +36,8 @@ def api_py_proto_library(name, srcs = [], deps = [], has_services = 0): # TODO(htuch): has_services is currently ignored but will in future support # gRPC stub generation. +# TODO(htuch): Automatically generate go_proto_library and go_grpc_library +# from api_proto_library. def api_proto_library(name, srcs = [], deps = [], has_services = 0, require_py = 1): # This is now vestigial, since there are no direct consumers in # data-plane-api. However, we want to maintain native proto_library support diff --git a/bazel/repositories.bzl b/bazel/repositories.bzl index 1390c96b..4845aa93 100644 --- a/bazel/repositories.bzl +++ b/bazel/repositories.bzl @@ -16,6 +16,7 @@ def api_dependencies(): url = "https://github.com/googleapis/googleapis/archive/" + GOOGLEAPIS_SHA + ".tar.gz", build_file_content = """ load("@com_google_protobuf//:protobuf.bzl", "cc_proto_library", "py_proto_library") +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") filegroup( name = "http_api_protos_src", @@ -26,6 +27,13 @@ filegroup( visibility = ["//visibility:public"], ) +go_proto_library( + name = "descriptor_go_proto", + importpath = "github.com/golang/protobuf/protoc-gen-go/descriptor", + proto = "@com_google_protobuf//:descriptor_proto", + visibility = ["//visibility:public"], +) + proto_library( name = "http_api_protos_proto", srcs = [":http_api_protos_src"], @@ -57,6 +65,17 @@ py_proto_library( visibility = ["//visibility:public"], deps = ["@com_google_protobuf//:protobuf_python"], ) + +go_proto_library( + name = "http_api_go_proto", + importpath = "google.golang.org/genproto/googleapis/api/annotations", + proto = ":http_api_protos_proto", + visibility = ["//visibility:public"], + deps = [ + ":descriptor_go_proto", + ], +) + filegroup( name = "rpc_status_protos_src", srcs = [ @@ -81,6 +100,17 @@ cc_proto_library( ], visibility = ["//visibility:public"], ) + +go_proto_library( + name = "rpc_status_go_proto", + importpath = "google.golang.org/genproto/googleapis/rpc/status", + proto = ":rpc_status_protos_lib", + visibility = ["//visibility:public"], + deps = [ + "@com_github_golang_protobuf//ptypes/any:go_default_library", + ], +) + py_proto_library( name = "rpc_status_protos_py", srcs = [ @@ -101,6 +131,7 @@ py_proto_library( url = "https://github.com/gogo/protobuf/archive/" + GOGOPROTO_SHA + ".tar.gz", build_file_content = """ load("@com_google_protobuf//:protobuf.bzl", "cc_proto_library", "py_proto_library") +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") proto_library( name = "gogo_proto", @@ -113,6 +144,13 @@ proto_library( visibility = ["//visibility:public"], ) +go_proto_library( + name = "descriptor_go_proto", + importpath = "github.com/golang/protobuf/protoc-gen-go/descriptor", + proto = "@com_google_protobuf//:descriptor_proto", + visibility = ["//visibility:public"], +) + cc_proto_library( name = "gogo_proto_cc", srcs = [ @@ -124,6 +162,16 @@ cc_proto_library( visibility = ["//visibility:public"], ) +go_proto_library( + name = "gogo_proto_go", + importpath = "gogoproto", + proto = ":gogo_proto", + visibility = ["//visibility:public"], + deps = [ + ":descriptor_go_proto", + ], +) + py_proto_library( name = "gogo_proto_py", srcs = [ diff --git a/ci/run_envoy_docker.sh b/ci/run_envoy_docker.sh index 3f2a4b6e..c71f22d1 100755 --- a/ci/run_envoy_docker.sh +++ b/ci/run_envoy_docker.sh @@ -2,7 +2,7 @@ set -e -[[ -z "${IMAGE_NAME}" ]] && IMAGE_NAME="lyft/envoy-build-ubuntu" +[[ -z "${IMAGE_NAME}" ]] && IMAGE_NAME="envoyproxy/envoy-build" [[ -z "${IMAGE_ID}" ]] && IMAGE_ID="latest" [[ -z "${ENVOY_API_DOCKER_BUILD_DIR}" ]] && ENVOY_API_DOCKER_BUILD_DIR=/tmp/envoy-api-docker-build diff --git a/test/build/BUILD b/test/build/BUILD index b8b62fb6..39570930 100644 --- a/test/build/BUILD +++ b/test/build/BUILD @@ -1,4 +1,5 @@ load("//bazel:api_build_system.bzl", "api_cc_test") +load("@io_bazel_rules_go//go:def.bzl", "go_test") licenses(["notice"]) # Apache 2 @@ -17,3 +18,21 @@ api_cc_test( "//api/filter/accesslog", ], ) + +go_test( + name = "go_build_test", + size = "small", + srcs = ["go_build_test.go"], + importpath = "go_build_test", + deps = [ + "//api:bootstrap_go_proto", + "//api:cds_go_grpc", + "//api:discovery_go_grpc", + "//api:eds_go_grpc", + "//api:hds_go_grpc", + "//api:lds_go_grpc", + "//api:rds_go_grpc", + "//api:rls_go_grpc", + "//api:sds_go_grpc", + ], +) diff --git a/test/build/go_build_test.go b/test/build/go_build_test.go new file mode 100644 index 00000000..bd0daa7b --- /dev/null +++ b/test/build/go_build_test.go @@ -0,0 +1,18 @@ +package go_build_test + +import ( + "testing" + _ "github.com/envoyproxy/data-plane-api/api/bootstrap" + _ "github.com/envoyproxy/data-plane-api/api/discovery" + _ "github.com/envoyproxy/data-plane-api/api/cds" + _ "github.com/envoyproxy/data-plane-api/api/eds" + _ "github.com/envoyproxy/data-plane-api/api/hds" + _ "github.com/envoyproxy/data-plane-api/api/lds" + _ "github.com/envoyproxy/data-plane-api/api/rds" + _ "github.com/envoyproxy/data-plane-api/api/rls" + _ "github.com/envoyproxy/data-plane-api/api/sds" +) + +func TestNoop(t *testing.T) { + // Noop test that verifies the successful importation of Envoy V2 API protos +}