Skip to content

Commit 80ed810

Browse files
authored
Get Bazel building all of the APIs (istio#133)
* update bazel and go versions, add field_behavior_proto to the set of standard proto deps BUT NOT THE GO_PROTO_DEPS 🚨 * everything but install compiling with bazel * add buildifier target * run buildifier for the first time * stashing state to explore making common-protos a sub-repo in bazel to fix path problems revert unneeded changes * get k8s.io building * revert prefix strip change that broke the build, just pass it as kwargs for now * run buildifier to clean up * got istio.io building * got install building - though currently the overall build fails as our protobuf validation is wrong. that'll be a follow-up * rever the import change - this does break the bazel build here but I can't figure out how to keep it working in the old build too * finally get the build working with both bazel and make! * update operator build to get it _really really_ working - unfortunately this means basically an entirely hand-written build file for just this one spot * buildifier
1 parent 15581b2 commit 80ed810

File tree

95 files changed

+730
-25
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+730
-25
lines changed

.bazelversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.2.0
1+
3.2.0

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ gen.warnings
1010
private/iam/v1/*.pb.html
1111
tsb/private/cli/v2/*.pb.html
1212
tsb/private/v2/*.pb.html
13+
bazel-*

BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
2+
3+
buildifier(
4+
name = "buildifier",
5+
)

WORKSPACE

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ rules_proto_toolchains()
3939

4040
http_archive(
4141
name = "io_bazel_rules_go",
42-
sha256 = "142dd33e38b563605f0d20e89d9ef9eda0fc3cb539a14be1bdb1350de2eda659",
42+
sha256 = "a8d6b1b354d371a646d2f7927319974e0f9e52f73a2452d2b3877118169eb6bb",
4343
urls = [
44-
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.22.2/rules_go-v0.22.2.tar.gz",
45-
"https://github.com/bazelbuild/rules_go/releases/download/v0.22.2/rules_go-v0.22.2.tar.gz",
44+
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.3/rules_go-v0.23.3.tar.gz",
45+
"https://github.com/bazelbuild/rules_go/releases/download/v0.23.3/rules_go-v0.23.3.tar.gz",
4646
],
4747
)
4848

@@ -51,3 +51,11 @@ load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_depe
5151
go_rules_dependencies()
5252

5353
go_register_toolchains()
54+
55+
# buildifier
56+
http_archive(
57+
name = "com_github_bazelbuild_buildtools",
58+
sha256 = "21e3f4cc034f01ca74643133fc9986cbf0bf43b7a0a0b150f0967e72b22b9132",
59+
strip_prefix = "buildtools-3.2.1",
60+
urls = ["https://github.com/bazelbuild/buildtools/archive/3.2.1.tar.gz"],
61+
)

audit/v1/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package()

authz/v1/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package()

bazel/api_build_system.bzl

Lines changed: 88 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,31 @@ _COMMON_PROTO_DEPS = [
1616
"@com_google_protobuf//:struct_proto",
1717
"@com_google_protobuf//:timestamp_proto",
1818
"@com_google_protobuf//:wrappers_proto",
19+
"@com_google_googleapis//google/api:annotations_proto",
1920
"@com_google_googleapis//google/api:http_proto",
2021
"@com_google_googleapis//google/api:httpbody_proto",
21-
"@com_google_googleapis//google/api:annotations_proto",
22+
"@com_google_googleapis//google/api:field_behavior_proto",
2223
"@com_google_googleapis//google/rpc:status_proto",
2324
"@com_envoyproxy_protoc_gen_validate//validate:validate_proto",
2425
]
26+
_COMMON_GO_DEPS = [
27+
"@com_github_golang_protobuf//ptypes:go_default_library",
28+
"@com_github_golang_protobuf//ptypes/any:go_default_library",
29+
"@com_github_golang_protobuf//ptypes/duration:go_default_library",
30+
"@com_github_golang_protobuf//ptypes/struct:go_default_library",
31+
"@com_github_golang_protobuf//ptypes/timestamp:go_default_library",
32+
"@com_github_golang_protobuf//ptypes/wrappers:go_default_library",
33+
"@com_envoyproxy_protoc_gen_validate//validate:go_default_library",
34+
"@com_google_googleapis//google/api:annotations_go_proto",
35+
# Do _not_ include :field_behavior_go_proto dep even though we include it as a
36+
# dep in _COMMON_PROTO_DEPS: it breaks the build with an invalid import, which
37+
# we get from :annotations_go_proto anyway.
38+
# "@com_google_googleapis//google/api:field_behavior_go_proto",
39+
"@com_google_googleapis//google/rpc:status_go_proto",
40+
]
41+
42+
EXTERNAL_PROTO_GO_DEP_MAP = {
43+
}
2544

2645
def _proto_mapping(dep, proto_dep_map, proto_suffix):
2746
mapped = proto_dep_map.get(dep)
@@ -31,15 +50,19 @@ def _proto_mapping(dep, proto_dep_map, proto_suffix):
3150
return mapped
3251

3352
def _go_proto_mapping(dep):
34-
return _proto_mapping(dep, EXTERNAL_PROTO_GO_BAZEL_DEP_MAP, _GO_PROTO_SUFFIX)
53+
# merge our local map and the imported map from @envoy_api//bazel
54+
full_map = dict(EXTERNAL_PROTO_GO_DEP_MAP.items() + EXTERNAL_PROTO_GO_BAZEL_DEP_MAP.items())
55+
return _proto_mapping(dep, full_map, _GO_PROTO_SUFFIX)
3556

3657
# API proto package
37-
def api_proto_package(
38-
name = "pkg",
39-
srcs = [],
40-
deps = [],
41-
has_services = False,
42-
visibility = ["//visibility:public"]):
58+
def _api_proto_package(
59+
name,
60+
srcs,
61+
proto_deps,
62+
go_deps,
63+
has_services,
64+
visibility,
65+
**kwargs):
4366
"""Defines a API proto package.
4467
4568
Args:
@@ -48,15 +71,19 @@ def api_proto_package(
4871
deps: dependencies
4972
has_services: whether .proto file has services
5073
visibility: Bazel visibility
74+
kwargs: other arguments to pass to proto_library (they are not passed to go_proto_library)
5175
"""
5276
if srcs == []:
5377
srcs = native.glob(["*.proto"])
5478

79+
# print("%s using sources %s" % (name, srcs))
80+
5581
proto_library(
5682
name = name,
5783
srcs = srcs,
58-
deps = deps + _COMMON_PROTO_DEPS,
84+
deps = proto_deps,
5985
visibility = visibility,
86+
**kwargs
6087
)
6188

6289
compilers = ["@io_bazel_rules_go//proto:go_proto", "@envoy_api//bazel:pgv_plugin_go"]
@@ -66,17 +93,57 @@ def api_proto_package(
6693
name = name + _GO_PROTO_SUFFIX,
6794
compilers = compilers,
6895
importpath = _GO_IMPORTPATH_PREFIX + native.package_name(),
69-
proto = name,
96+
protos = [":" + name],
7097
visibility = ["//visibility:public"],
71-
deps = [_go_proto_mapping(dep) for dep in deps] + [
72-
"@com_github_golang_protobuf//ptypes:go_default_library",
73-
"@com_github_golang_protobuf//ptypes/any:go_default_library",
74-
"@com_github_golang_protobuf//ptypes/duration:go_default_library",
75-
"@com_github_golang_protobuf//ptypes/struct:go_default_library",
76-
"@com_github_golang_protobuf//ptypes/timestamp:go_default_library",
77-
"@com_github_golang_protobuf//ptypes/wrappers:go_default_library",
78-
"@com_envoyproxy_protoc_gen_validate//validate:go_default_library",
79-
"@com_google_googleapis//google/api:annotations_go_proto",
80-
"@com_google_googleapis//google/rpc:status_go_proto",
81-
],
98+
deps = go_deps,
8299
)
100+
101+
def api_proto_package(
102+
name = "pkg",
103+
srcs = [],
104+
deps = [],
105+
has_services = False,
106+
visibility = ["//visibility:public"],
107+
**kwargs):
108+
"""Defines a API proto package.
109+
110+
Args:
111+
name: package name
112+
srcs: source files if not specified all .proto files in the directory
113+
deps: dependencies
114+
has_services: whether .proto file has services
115+
visibility: Bazel visibility
116+
strip_import_prefix: prefix to remove for protobufs in this target (only applies to proto_library rules)
117+
"""
118+
proto_deps = deps + _COMMON_PROTO_DEPS
119+
go_deps = [_go_proto_mapping(dep) for dep in deps] + _COMMON_GO_DEPS
120+
return _api_proto_package(name, srcs, proto_deps, go_deps, has_services, visibility, **kwargs)
121+
122+
def k8s_api_proto_package(
123+
name = "pkg",
124+
srcs = [],
125+
deps = [],
126+
has_services = False,
127+
visibility = ["//visibility:public"],
128+
strip_import_prefix = "//common-protos"):
129+
"""Defines a API proto package for Kubernetes API resources. This assumes a few extra dependencies
130+
compared to api_proto_package, as well as stripping of the `//common-protos` prefix for import.
131+
132+
Args:
133+
name: package name
134+
srcs: source files if not specified all .proto files in the directory
135+
deps: dependencies
136+
has_services: whether .proto file has services
137+
visibility: Bazel visibility
138+
"""
139+
_common_api_k8s_deps = [
140+
"//common-protos/k8s.io/apimachinery/pkg/apis/meta/v1:pkg",
141+
"//common-protos/k8s.io/apimachinery/pkg/runtime:pkg",
142+
"//common-protos/k8s.io/apimachinery/pkg/runtime/schema:pkg",
143+
]
144+
145+
proto_deps = deps + _COMMON_PROTO_DEPS + _common_api_k8s_deps
146+
147+
# note we run the _common_api_k8s_deps through _go_proto_mapping too (but not _COMMON_GO_DEPS)
148+
go_deps = [_go_proto_mapping(dep) for dep in deps + _common_api_k8s_deps] + _COMMON_GO_DEPS
149+
return _api_proto_package(name, srcs, proto_deps, go_deps, has_services, visibility, strip_import_prefix = strip_import_prefix)

common-protos/gogoproto/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package(
4+
strip_import_prefix = "//common-protos",
5+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package(
4+
strip_import_prefix = "//common-protos/istio.io/api",
5+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package(
4+
strip_import_prefix = "//common-protos/istio.io/api",
5+
deps = [
6+
"//common-protos/istio.io/api/authentication/v1alpha1:pkg",
7+
],
8+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package(
4+
strip_import_prefix = "//common-protos/istio.io/api",
5+
deps = [
6+
"//common-protos/gogoproto:pkg",
7+
],
8+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package(
4+
strip_import_prefix = "//common-protos/istio.io/api",
5+
deps = [
6+
"//common-protos/istio.io/api/networking/v1alpha3:pkg",
7+
],
8+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package(
4+
strip_import_prefix = "//common-protos/istio.io/api",
5+
deps = [
6+
"//common-protos/gogoproto:pkg",
7+
],
8+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package(
4+
strip_import_prefix = "//common-protos/istio.io/api",
5+
deps = [
6+
"//common-protos/gogoproto:pkg",
7+
],
8+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package(
4+
strip_import_prefix = "//common-protos/istio.io/api",
5+
deps = [
6+
"//common-protos/gogoproto:pkg",
7+
"//common-protos/istio.io/api/mixer/v1:pkg",
8+
],
9+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package(
4+
strip_import_prefix = "//common-protos/istio.io/api",
5+
deps = [
6+
"//common-protos/gogoproto:pkg",
7+
],
8+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package(
4+
strip_import_prefix = "//common-protos/istio.io/api",
5+
deps = [
6+
"//common-protos/gogoproto:pkg",
7+
],
8+
)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
load("@rules_proto//proto:defs.bzl", "proto_library")
2+
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
3+
4+
# Beware, this file is written by hand and does not use api_proto_package.
5+
# We have to do that in this one place because these protos alone in the
6+
# entire codebase are imported with 2 names:
7+
# internally, Istio API protos refer to eachother as `import "operator/compnent.proto"`
8+
# externally, we reference those same protos as `import "istio.io/api/operator/component.proto"`
9+
# to support both of these at the same time, we need some refined targets to make the build work.
10+
# More notes inline, but the gist is that for the golang part of the build to succeed, we have
11+
# to have only a single go_proto_library rule (or we wind up with problems around go packages).
12+
13+
proto_library(
14+
name = "component_proto",
15+
srcs = ["component.proto"],
16+
strip_import_prefix = "//common-protos", # enables import as "istio.io/api/operator/v1alpha1"
17+
visibility = ["//visibility:public"],
18+
deps = [
19+
"//common-protos/istio.io/api/operator/v1alpha1:kubernetes_proto",
20+
"//common-protos/k8s.io/api/core/v1:pkg",
21+
"@com_google_protobuf//:any_proto",
22+
],
23+
)
24+
25+
# this rule is the hand-written version of the go_proto_library that `api_proto_package`
26+
# generated for `component_proto` above, with the exception that we include
27+
# `:kubernetes_proto` here. This is because we can only have a single go_proto_library.
28+
go_proto_library(
29+
name = "component_proto_go_proto",
30+
compilers = [
31+
"@io_bazel_rules_go//proto:go_proto",
32+
"@envoy_api//bazel:pgv_plugin_go",
33+
],
34+
importpath = "github.com/tetrateio/api-gocommon-protos/istio.io/api/operator/v1alpha1",
35+
# this is the dirty part: we include ":kubernetes_proto" here for the golang build to work;
36+
# we can do this because go_proto_library ignores `strip_import_prefix`. If we tried to
37+
# pull in operator.proto, we'll have to rework how we attempt this (and probably just change
38+
# the import in our proto to remove the need to do all this craziness).
39+
protos = [
40+
":component_proto",
41+
":kubernetes_proto",
42+
],
43+
visibility = ["//visibility:public"],
44+
deps = [
45+
"//common-protos/k8s.io/api/core/v1:pkg_go_proto",
46+
"//common-protos/k8s.io/apimachinery/pkg/api/resource:pkg_go_proto",
47+
"//common-protos/k8s.io/apimachinery/pkg/apis/meta/v1:pkg_go_proto",
48+
"//common-protos/k8s.io/apimachinery/pkg/util/intstr:pkg_go_proto",
49+
"@com_github_golang_protobuf//ptypes:go_default_library",
50+
"@com_github_golang_protobuf//ptypes/any:go_default_library",
51+
],
52+
)
53+
54+
proto_library(
55+
name = "kubernetes_proto",
56+
srcs = ["kubernetes.proto"],
57+
strip_import_prefix = "//common-protos/istio.io/api", # enables import as "istio.io/api/operator/v1alpha1"
58+
visibility = ["//visibility:private"],
59+
deps = [
60+
"//common-protos/k8s.io/apimachinery/pkg/api/resource:pkg",
61+
"//common-protos/k8s.io/apimachinery/pkg/apis/meta/v1:pkg",
62+
"//common-protos/k8s.io/apimachinery/pkg/util/intstr:pkg",
63+
],
64+
)
65+
66+
proto_library(
67+
name = "operator_proto",
68+
srcs = ["operator.proto"],
69+
strip_import_prefix = "//common-protos/istio.io/api",
70+
visibility = ["//visibility:private"],
71+
deps = [
72+
"//common-protos/istio.io/api/operator/v1alpha1:component_proto_internal",
73+
],
74+
)
75+
76+
proto_library(
77+
name = "component_proto_internal",
78+
srcs = ["component.proto"],
79+
strip_import_prefix = "//common-protos/istio.io/api",
80+
visibility = ["//visibility:private"],
81+
deps = [
82+
"//common-protos/istio.io/api/operator/v1alpha1:kubernetes_proto",
83+
"//common-protos/k8s.io/api/core/v1:pkg",
84+
"@com_google_protobuf//:any_proto",
85+
],
86+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package(
4+
strip_import_prefix = "//common-protos/istio.io/api",
5+
deps = [
6+
"//common-protos/gogoproto:pkg",
7+
],
8+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
load("//bazel:api_build_system.bzl", "api_proto_package")
2+
3+
api_proto_package(
4+
strip_import_prefix = "//common-protos/istio.io/api",
5+
)

0 commit comments

Comments
 (0)