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

api: organize go_proto_libraries #8003

Merged
merged 21 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from 17 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
12 changes: 12 additions & 0 deletions api/bazel/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//proto:compiler.bzl", "go_proto_compiler")

licenses(["notice"]) # Apache 2

go_proto_compiler(
name = "pgv_plugin_go",
options = ["lang=go"],
plugin = "@com_envoyproxy_protoc_gen_validate//:protoc-gen-validate",
suffix = ".pb.validate.go",
valid_archive = False,
visibility = ["//visibility:public"],
)
115 changes: 63 additions & 52 deletions api/bazel/api_build_system.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,23 @@ _PY_SUFFIX = "_py"
_CC_SUFFIX = "_cc"
_CC_EXPORT_SUFFIX = "_export_cc"
_GO_PROTO_SUFFIX = "_go_proto"
_GO_GRPC_SUFFIX = "_go_grpc"
_GO_IMPORTPATH_PREFIX = "github.com/envoyproxy/data-plane-api/api/"

_COMMON_PROTO_DEPS = [
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:descriptor_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:empty_proto",
"@com_google_protobuf//:struct_proto",
"@com_google_protobuf//:timestamp_proto",
"@com_google_protobuf//:wrappers_proto",
"@com_google_googleapis//google/api:http_proto",
"@com_google_googleapis//google/api:annotations_proto",
"@com_google_googleapis//google/rpc:status_proto",
"@com_github_gogo_protobuf//:gogo_proto",
"@com_envoyproxy_protoc_gen_validate//validate:validate_proto",
]

def _Suffix(d, suffix):
return d + suffix

Expand Down Expand Up @@ -61,41 +75,6 @@ def py_proto_library(name, deps = []):
visibility = ["//visibility:public"],
)

def api_go_proto_library(name, proto, deps = []):
go_proto_library(
name = _Suffix(name, _GO_PROTO_SUFFIX),
importpath = _Suffix(_GO_IMPORTPATH_PREFIX, name),
proto = proto,
visibility = ["//visibility:public"],
deps = deps + [
"@com_github_gogo_protobuf//:gogo_proto_go",
"@io_bazel_rules_go//proto/wkt:any_go_proto",
"@io_bazel_rules_go//proto/wkt:duration_go_proto",
"@io_bazel_rules_go//proto/wkt:struct_go_proto",
"@io_bazel_rules_go//proto/wkt:timestamp_go_proto",
"@io_bazel_rules_go//proto/wkt:wrappers_go_proto",
"@com_envoyproxy_protoc_gen_validate//validate:go_default_library",
"@com_google_googleapis//google/rpc:status_go_proto",
],
)

def api_go_grpc_library(name, proto, deps = []):
go_grpc_library(
name = _Suffix(name, _GO_GRPC_SUFFIX),
importpath = _Suffix(_GO_IMPORTPATH_PREFIX, name),
proto = proto,
visibility = ["//visibility:public"],
deps = deps + [
"@com_github_gogo_protobuf//:gogo_proto_go",
"@io_bazel_rules_go//proto/wkt:any_go_proto",
"@io_bazel_rules_go//proto/wkt:duration_go_proto",
"@io_bazel_rules_go//proto/wkt:struct_go_proto",
"@io_bazel_rules_go//proto/wkt:wrappers_go_proto",
"@com_envoyproxy_protoc_gen_validate//validate:go_default_library",
"@com_google_googleapis//google/api:annotations_go_proto",
],
)

# This is api_proto_library plus some logic internal to //envoy/api.
def api_proto_library_internal(visibility = ["//visibility:private"], **kwargs):
# //envoy/docs/build.sh needs visibility in order to generate documents.
Expand All @@ -108,8 +87,6 @@ def api_proto_library_internal(visibility = ["//visibility:private"], **kwargs):

# 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,
visibility = ["//visibility:private"],
Expand All @@ -124,20 +101,7 @@ def api_proto_library(
native.proto_library(
name = name,
srcs = srcs,
deps = deps + external_proto_deps + [
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:descriptor_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:empty_proto",
"@com_google_protobuf//:struct_proto",
"@com_google_protobuf//:timestamp_proto",
"@com_google_protobuf//:wrappers_proto",
"@com_google_googleapis//google/api:http_proto",
"@com_google_googleapis//google/api:annotations_proto",
"@com_google_googleapis//google/rpc:status_proto",
"@com_github_gogo_protobuf//:gogo_proto",
"@com_envoyproxy_protoc_gen_validate//validate:validate_proto",
],
deps = deps + external_proto_deps + _COMMON_PROTO_DEPS,
visibility = visibility,
)
pgv_cc_proto_library(
Expand Down Expand Up @@ -181,3 +145,50 @@ def api_go_test(name, size, importpath, srcs = [], deps = []):
importpath = importpath,
deps = deps,
)

_GO_BAZEL_RULE_MAPPING = {
"@opencensus_proto//opencensus/proto/trace/v1:trace_proto": "@opencensus_proto//opencensus/proto/trace/v1:trace_proto_go",
htuch marked this conversation as resolved.
Show resolved Hide resolved
"@opencensus_proto//opencensus/proto/trace/v1:trace_config_proto": "@opencensus_proto//opencensus/proto/trace/v1:trace_and_config_proto_go",
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_proto": "@com_google_googleapis//google/api/expr/v1alpha1:cel_go_proto",
}

def go_proto_mapping(dep):
mapped = _GO_BAZEL_RULE_MAPPING.get(dep)
if mapped == None:
return _Suffix("@" + Label(dep).workspace_name + "//" + Label(dep).package + ":" + Label(dep).name, _GO_PROTO_SUFFIX)
return mapped

def api_proto_package(name = "pkg", srcs = [], deps = [], has_services = False, visibility = ["//visibility:public"]):
if srcs == []:
srcs = native.glob(["*.proto"])

native.proto_library(
name = name,
srcs = srcs,
deps = deps + _COMMON_PROTO_DEPS,
visibility = visibility,
)

compilers = ["@io_bazel_rules_go//proto:go_proto", "//bazel:pgv_plugin_go"]
if has_services:
compilers = ["@io_bazel_rules_go//proto:go_grpc", "//bazel:pgv_plugin_go"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that go_proto and go_grpc will not be separate targets anymore? So users no longer have to refer to *_go_grpc and can always just use *_go_proto?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is generally the same go package for proto and gRPC in OSS. Internal rules might still create two different rules if necessary.


go_proto_library(
htuch marked this conversation as resolved.
Show resolved Hide resolved
name = _Suffix(name, _GO_PROTO_SUFFIX),
compilers = compilers,
importpath = _Suffix(_GO_IMPORTPATH_PREFIX, native.package_name()),
proto = name,
visibility = ["//visibility:public"],
deps = [go_proto_mapping(dep) for dep in deps] + [
"@com_github_gogo_protobuf//:gogo_proto_go",
"@com_github_golang_protobuf//ptypes:go_default_library",
"@com_github_golang_protobuf//ptypes/any:go_default_library",
htuch marked this conversation as resolved.
Show resolved Hide resolved
"@com_github_golang_protobuf//ptypes/duration:go_default_library",
"@com_github_golang_protobuf//ptypes/struct:go_default_library",
"@com_github_golang_protobuf//ptypes/timestamp:go_default_library",
"@com_github_golang_protobuf//ptypes/wrappers:go_default_library",
"@com_envoyproxy_protoc_gen_validate//validate:go_default_library",
"@com_google_googleapis//google/api:annotations_go_proto",
"@com_google_googleapis//google/rpc:status_go_proto",
],
)
13 changes: 12 additions & 1 deletion api/envoy/admin/v2alpha/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
load("@envoy_api//bazel:api_build_system.bzl", "api_proto_library_internal")
load("@envoy_api//bazel:api_build_system.bzl", "api_proto_library_internal", "api_proto_package")

licenses(["notice"]) # Apache 2

api_proto_package(
deps = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When do you have default vs. explicit name = "v2",?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started naming them starting from envoy/api/v2/core, and then switched to using the default name. I'm fine making them all to be pkg. I thought envoy/api/v2:v2 looked better at first.

"//envoy/api/v2",
"//envoy/api/v2/auth",
"//envoy/api/v2/core",
"//envoy/config/bootstrap/v2:pkg",
"//envoy/service/tap/v2alpha:pkg",
"//envoy/type",
],
)

api_proto_library_internal(
name = "config_dump",
srcs = ["config_dump.proto"],
Expand Down
87 changes: 16 additions & 71 deletions api/envoy/api/v2/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@envoy_api//bazel:api_build_system.bzl", "api_go_grpc_library", "api_go_proto_library", "api_proto_library_internal")
load("@envoy_api//bazel:api_build_system.bzl", "api_proto_library_internal", "api_proto_package")

licenses(["notice"]) # Apache 2

Expand All @@ -16,19 +16,28 @@ package_group(
],
)

api_proto_package(
name = "v2",
has_services = True,
deps = [
"//envoy/api/v2/auth",
"//envoy/api/v2/cluster",
"//envoy/api/v2/core",
"//envoy/api/v2/endpoint:pkg",
"//envoy/api/v2/listener:pkg",
"//envoy/api/v2/ratelimit:pkg",
"//envoy/api/v2/route:pkg",
"//envoy/type",
],
)

api_proto_library_internal(
name = "discovery",
srcs = ["discovery.proto"],
visibility = [":friends"],
deps = ["//envoy/api/v2/core:base"],
)

api_go_proto_library(
name = "discovery",
proto = ":discovery",
deps = ["//envoy/api/v2/core:base_go_proto"],
)

api_proto_library_internal(
name = "eds",
srcs = ["eds.proto"],
Expand All @@ -44,19 +53,6 @@ api_proto_library_internal(
],
)

api_go_grpc_library(
name = "eds",
proto = ":eds",
deps = [
":discovery_go_proto",
"//envoy/api/v2/core:address_go_proto",
"//envoy/api/v2/core:base_go_proto",
"//envoy/api/v2/core:health_check_go_proto",
"//envoy/api/v2/endpoint:endpoint_go_proto",
"//envoy/type:percent_go_proto",
],
)

api_proto_library_internal(
name = "cds",
srcs = ["cds.proto"],
Expand All @@ -79,26 +75,6 @@ api_proto_library_internal(
],
)

api_go_grpc_library(
name = "cds",
proto = ":cds",
deps = [
":discovery_go_proto",
":eds_go_grpc",
"//envoy/api/v2/auth:cert_go_proto",
"//envoy/api/v2/cluster:circuit_breaker_go_proto",
"//envoy/api/v2/cluster:filter_go_proto",
"//envoy/api/v2/cluster:outlier_detection_go_proto",
"//envoy/api/v2/core:address_go_proto",
"//envoy/api/v2/core:base_go_proto",
"//envoy/api/v2/core:config_source_go_proto",
"//envoy/api/v2/core:health_check_go_proto",
"//envoy/api/v2/core:protocol_go_proto",
"//envoy/api/v2/endpoint:endpoint_go_proto",
"//envoy/type:percent_go_proto",
],
)

api_proto_library_internal(
name = "lds",
srcs = ["lds.proto"],
Expand All @@ -112,17 +88,6 @@ api_proto_library_internal(
],
)

api_go_grpc_library(
name = "lds",
proto = ":lds",
deps = [
":discovery_go_proto",
"//envoy/api/v2/core:address_go_proto",
"//envoy/api/v2/core:base_go_proto",
"//envoy/api/v2/listener:listener_go_proto",
],
)

api_proto_library_internal(
name = "rds",
srcs = ["rds.proto"],
Expand All @@ -136,17 +101,6 @@ api_proto_library_internal(
],
)

api_go_grpc_library(
name = "rds",
proto = ":rds",
deps = [
":discovery_go_proto",
"//envoy/api/v2/core:base_go_proto",
"//envoy/api/v2/core:config_source_go_proto",
"//envoy/api/v2/route:route_go_proto",
],
)

api_proto_library_internal(
name = "srds",
srcs = ["srds.proto"],
Expand All @@ -158,12 +112,3 @@ api_proto_library_internal(
"//envoy/api/v2/route",
],
)

api_go_grpc_library(
name = "srds",
proto = ":srds",
deps = [
":discovery_go_proto",
"//envoy/api/v2/core:base_go_proto",
],
)
18 changes: 8 additions & 10 deletions api/envoy/api/v2/auth/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@envoy_api//bazel:api_build_system.bzl", "api_go_proto_library", "api_proto_library_internal")
load("@envoy_api//bazel:api_build_system.bzl", "api_proto_library_internal", "api_proto_package")

licenses(["notice"]) # Apache 2

Expand All @@ -15,6 +15,13 @@ package_group(
],
)

api_proto_package(
name = "auth",
deps = [
"//envoy/api/v2/core",
],
)

api_proto_library_internal(
name = "cert",
srcs = ["cert.proto"],
Expand All @@ -24,12 +31,3 @@ api_proto_library_internal(
"//envoy/api/v2/core:config_source",
],
)

api_go_proto_library(
name = "cert",
proto = ":cert",
deps = [
"//envoy/api/v2/core:base_go_proto",
"//envoy/api/v2/core:config_source_go_proto",
],
)
1 change: 0 additions & 1 deletion api/envoy/api/v2/auth/cert.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package envoy.api.v2.auth;
option java_outer_classname = "CertProto";
option java_multiple_files = true;
option java_package = "io.envoyproxy.envoy.api.v2.auth";
option go_package = "auth";

import "envoy/api/v2/core/base.proto";
import "envoy/api/v2/core/config_source.proto";
Expand Down
Loading