bazel: update openapi-gen to use new kazel-generated dictionaries

pull/564/head
Jeff Grafton 2018-11-06 16:08:28 -08:00
parent 06d5c50eb2
commit 3fd6f97f55
6 changed files with 152 additions and 61 deletions

View File

@ -2,6 +2,11 @@ package(default_visibility = ["//visibility:public"])
load("@io_bazel_rules_docker//container:container.bzl", "container_bundle", "container_image")
load("@io_kubernetes_build//defs:build.bzl", "release_filegroup")
load(":code_generation_test.bzl", "code_generation_test_suite")
code_generation_test_suite(
name = "code_generation_tests",
)
filegroup(
name = "package-srcs",

81
build/code_generation.bzl Normal file
View File

@ -0,0 +1,81 @@
# Copyright 2018 The Kubernetes 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.
load("//build:kazel_generated.bzl", "go_prefix", "tags_values_pkgs")
load("//build:openapi.bzl", "openapi_vendor_prefix")
load("@io_kubernetes_build//defs:go.bzl", "go_genrule")
def bazel_go_library(pkg):
"""Returns the Bazel label for the Go library for the provided package.
This is intended to be used with the //build:kazel_generated.bzl tag dictionaries; for example:
load("//build:kazel_generated.bzl", "tags_values_pkgs")
some_rule(
...
deps = [bazel_go_library(pkg) for pkg in tags_values_pkgs["openapi-gen"]["true"]],
...
)
"""
return "//%s:go_default_library" % pkg
def go_pkg(pkg):
"""Returns the full Go package name for the provided workspace-relative package.
This is suitable to pass to tools depending on the Go build library.
If any packages are in staging/src, they are remapped to their intended path in vendor/.
This is intended to be used with the //build:kazel_generated.bzl tag dictionaries.
For example:
load("//build:kazel_generated.bzl", "tags_values_pkgs")
genrule(
...
cmd = "do something --pkgs=%s" % ",".join([go_pkg(pkg) for pkg in tags_values_pkgs["openapi-gen"]["true"]]),
...
)
"""
return go_prefix + "/" + pkg.replace("staging/src/", "vendor/", maxsplit = 1)
def openapi_deps():
deps = [
"//vendor/github.com/go-openapi/spec:go_default_library",
"//vendor/k8s.io/kube-openapi/pkg/common:go_default_library",
]
deps.extend([bazel_go_library(pkg) for pkg in tags_values_pkgs["openapi-gen"]["true"]])
return deps
def gen_openapi(outs, output_pkg):
"""Calls openapi-gen to produce the zz_generated.openapi.go file,
which should be provided in outs.
output_pkg should be set to the full go package name for this generated file.
"""
go_genrule(
name = "zz_generated.openapi",
srcs = ["//" + openapi_vendor_prefix + "hack/boilerplate:boilerplate.generatego.txt"],
outs = outs,
# In order for vendored dependencies to be imported correctly,
# the generator must run from the repo root inside the generated GOPATH.
# All of bazel's $(location)s are relative to the original working directory, however.
cmd = " ".join([
"cd $$GOPATH/src/" + go_prefix + ";",
"$$GO_GENRULE_EXECROOT/$(location //vendor/k8s.io/kube-openapi/cmd/openapi-gen)",
"--v 1",
"--logtostderr",
"--go-header-file $$GO_GENRULE_EXECROOT/$(location //" + openapi_vendor_prefix + "hack/boilerplate:boilerplate.generatego.txt)",
"--output-file-base zz_generated.openapi",
"--output-package " + output_pkg,
"--report-filename tmp_api_violations.report",
"--input-dirs " + ",".join([go_pkg(pkg) for pkg in tags_values_pkgs["openapi-gen"]["true"]]),
"&& cp $$GOPATH/src/" + output_pkg + "/zz_generated.openapi.go $$GO_GENRULE_EXECROOT/$(location :zz_generated.openapi.go)",
"&& rm tmp_api_violations.report",
]),
go_deps = openapi_deps(),
tools = ["//vendor/k8s.io/kube-openapi/cmd/openapi-gen"],
)

View File

@ -0,0 +1,49 @@
# Copyright 2018 The Kubernetes 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.
load(":code_generation.bzl", "bazel_go_library", "go_pkg")
load("@bazel_skylib//:lib.bzl", "asserts", "unittest")
def _bazel_go_library_test_impl(ctx):
env = unittest.begin(ctx)
test_cases = [
("pkg/kubectl/util", "//pkg/kubectl/util:go_default_library"),
("vendor/some/third/party", "//vendor/some/third/party:go_default_library"),
("staging/src/k8s.io/apimachinery/api", "//staging/src/k8s.io/apimachinery/api:go_default_library"),
]
for input, expected in test_cases:
asserts.equals(env, expected, bazel_go_library(input))
unittest.end(env)
bazel_go_library_test = unittest.make(_bazel_go_library_test_impl)
def _go_pkg_test_impl(ctx):
env = unittest.begin(ctx)
test_cases = [
("pkg/kubectl/util", "k8s.io/kubernetes/pkg/kubectl/util"),
("vendor/some/third/party", "k8s.io/kubernetes/vendor/some/third/party"),
("staging/src/k8s.io/apimachinery/api", "k8s.io/kubernetes/vendor/k8s.io/apimachinery/api"),
]
for input, expected in test_cases:
asserts.equals(env, expected, go_pkg(input))
unittest.end(env)
go_pkg_test = unittest.make(_go_pkg_test_impl)
def code_generation_test_suite(name):
unittest.suite(
name,
bazel_go_library_test,
go_pkg_test,
)

View File

@ -16,9 +16,6 @@
# k8s.io/kubernetes will need to set the following variables in
# //build/openapi.bzl in their project and customize the go prefix:
#
# openapi_go_prefix = "k8s.io/myproject/"
# openapi_vendor_prefix = "vendor/k8s.io/kubernetes/"
openapi_go_prefix = "k8s.io/kubernetes/"
openapi_vendor_prefix = ""

View File

@ -1,10 +1,22 @@
# doc.go is managed by kazel, so gazelle should ignore it.
# gazelle:exclude doc.go
package(default_visibility = ["//visibility:public"])
load("//pkg/generated/openapi:def.bzl", "openapi_library")
load("//build:openapi.bzl", "openapi_go_prefix", "openapi_vendor_prefix")
load("//build:code_generation.bzl", "gen_openapi", "openapi_deps")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
gen_openapi(
outs = ["zz_generated.openapi.go"],
output_pkg = "k8s.io/kubernetes/pkg/generated/openapi",
)
go_library(
name = "go_default_library",
srcs = [
"doc.go",
"zz_generated.openapi.go",
],
importpath = "k8s.io/kubernetes/pkg/generated/openapi",
deps = openapi_deps(), # keep
)
filegroup(
name = "package-srcs",

View File

@ -1,53 +0,0 @@
# Copyright 2017 The Kubernetes 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.
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_kubernetes_build//defs:go.bzl", "go_genrule")
def openapi_library(name, tags, srcs, go_prefix, vendor_prefix = "", openapi_targets = [], vendor_targets = []):
deps = [
"//vendor/github.com/go-openapi/spec:go_default_library",
"//vendor/k8s.io/kube-openapi/pkg/common:go_default_library",
] + ["//%s:go_default_library" % target for target in openapi_targets] + ["//staging/src/%s:go_default_library" % target for target in vendor_targets]
go_library(
name = name,
srcs = srcs + [":zz_generated.openapi"],
importpath = go_prefix + "pkg/generated/openapi",
tags = tags,
deps = deps,
)
go_genrule(
name = "zz_generated.openapi",
srcs = ["//" + vendor_prefix + "hack/boilerplate:boilerplate.go.txt"],
outs = ["zz_generated.openapi.go"],
# In order for vendored dependencies to be imported correctly,
# the generator must run from the repo root inside the generated GOPATH.
# All of bazel's $(location)s are relative to the original working directory, however,
# so we must save it first.
cmd = " ".join([
"cd $$GOPATH/src/" + go_prefix + ";",
"$$GO_GENRULE_EXECROOT/$(location //vendor/k8s.io/kube-openapi/cmd/openapi-gen)",
"--v 1",
"--logtostderr",
"--go-header-file $$GO_GENRULE_EXECROOT/$(location //" + vendor_prefix + "hack/boilerplate:boilerplate.go.txt)",
"--output-file-base zz_generated.openapi",
"--output-package " + go_prefix + "pkg/generated/openapi",
"--report-filename tmp_api_violations.report",
"--input-dirs " + ",".join([go_prefix + target for target in openapi_targets] + [go_prefix + "vendor/" + target for target in vendor_targets]),
"&& cp $$GOPATH/src/" + go_prefix + "pkg/generated/openapi/zz_generated.openapi.go $$GO_GENRULE_EXECROOT/$(location :zz_generated.openapi.go)",
"&& rm tmp_api_violations.report",
]),
go_deps = deps,
tools = ["//vendor/k8s.io/kube-openapi/cmd/openapi-gen"],
)