Unverified Commit 555c63ff authored by Kubernetes Prow Robot's avatar Kubernetes Prow Robot Committed by GitHub

Merge pull request #71529 from ixdy/bazel-refactor-openapi-gen-new-kazel

bazel: refactor openapi-gen logic for new kazel
parents 45b54f5c e34a0619
...@@ -9,8 +9,8 @@ ...@@ -9,8 +9,8 @@
"github.com/cloudflare/cfssl/cmd/cfssl", "github.com/cloudflare/cfssl/cmd/cfssl",
"github.com/cloudflare/cfssl/cmd/cfssljson", "github.com/cloudflare/cfssl/cmd/cfssljson",
"github.com/bazelbuild/bazel-gazelle/cmd/gazelle", "github.com/bazelbuild/bazel-gazelle/cmd/gazelle",
"github.com/kubernetes/repo-infra/kazel",
"k8s.io/kube-openapi/cmd/openapi-gen", "k8s.io/kube-openapi/cmd/openapi-gen",
"k8s.io/repo-infra/kazel",
"golang.org/x/lint/golint", "golang.org/x/lint/golint",
"./..." "./..."
], ],
...@@ -2406,10 +2406,6 @@ ...@@ -2406,10 +2406,6 @@
"Rev": "6807e777504f54ad073ecef66747de158294b639" "Rev": "6807e777504f54ad073ecef66747de158294b639"
}, },
{ {
"ImportPath": "github.com/kubernetes/repo-infra/kazel",
"Rev": "f2459dc75fc429b813d92c0622b408fd7f0d4cac"
},
{
"ImportPath": "github.com/lib/pq", "ImportPath": "github.com/lib/pq",
"Rev": "88edab0803230a3898347e77b474f8c1820a1f20" "Rev": "88edab0803230a3898347e77b474f8c1820a1f20"
}, },
...@@ -4099,6 +4095,10 @@ ...@@ -4099,6 +4095,10 @@
"Rev": "c59034cc13d587f5ef4e85ca0ade0c1866ae8e1d" "Rev": "c59034cc13d587f5ef4e85ca0ade0c1866ae8e1d"
}, },
{ {
"ImportPath": "k8s.io/repo-infra/kazel",
"Rev": "00fe14e3d1a3f9a73c4cea62d9c33b29c1e03ac4"
},
{
"ImportPath": "k8s.io/utils/clock", "ImportPath": "k8s.io/utils/clock",
"Rev": "8e7ff06bf0e2d3289061230af203e430a15b6dcc" "Rev": "8e7ff06bf0e2d3289061230af203e430a15b6dcc"
}, },
......
...@@ -2,6 +2,11 @@ package(default_visibility = ["//visibility:public"]) ...@@ -2,6 +2,11 @@ package(default_visibility = ["//visibility:public"])
load("@io_bazel_rules_docker//container:container.bzl", "container_bundle", "container_image") load("@io_bazel_rules_docker//container:container.bzl", "container_bundle", "container_image")
load("@io_kubernetes_build//defs:build.bzl", "release_filegroup") 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( filegroup(
name = "package-srcs", name = "package-srcs",
......
# 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"],
)
# 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,
)
# Copyright 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.
# #################################################
# # # # # # # # # # # # # # # # # # # # # # # # # #
# This file is autogenerated by kazel. DO NOT EDIT.
# # # # # # # # # # # # # # # # # # # # # # # # # #
# #################################################
#
# The go prefix passed to kazel
go_prefix = "k8s.io/kubernetes"
# The list of codegen tags kazel is configured to find
kazel_configured_tags = ["openapi-gen"]
# tags_values_pkgs is a dictionary mapping {k8s build tag: {tag value: [pkgs including that tag:value]}}
tags_values_pkgs = {"openapi-gen": {
"false": [
"staging/src/k8s.io/api/admission/v1beta1",
"staging/src/k8s.io/api/core/v1",
"staging/src/k8s.io/apimachinery/pkg/apis/testapigroup/v1",
"staging/src/k8s.io/apiserver/pkg/apis/example/v1",
"staging/src/k8s.io/apiserver/pkg/apis/example2/v1",
],
"true": [
"cmd/cloud-controller-manager/app/apis/config/v1alpha1",
"pkg/apis/abac/v0",
"pkg/apis/abac/v1beta1",
"pkg/apis/auditregistration",
"pkg/version",
"staging/src/k8s.io/api/admissionregistration/v1alpha1",
"staging/src/k8s.io/api/admissionregistration/v1beta1",
"staging/src/k8s.io/api/apps/v1",
"staging/src/k8s.io/api/apps/v1beta1",
"staging/src/k8s.io/api/apps/v1beta2",
"staging/src/k8s.io/api/auditregistration/v1alpha1",
"staging/src/k8s.io/api/authentication/v1",
"staging/src/k8s.io/api/authentication/v1beta1",
"staging/src/k8s.io/api/authorization/v1",
"staging/src/k8s.io/api/authorization/v1beta1",
"staging/src/k8s.io/api/autoscaling/v1",
"staging/src/k8s.io/api/autoscaling/v2beta1",
"staging/src/k8s.io/api/autoscaling/v2beta2",
"staging/src/k8s.io/api/batch/v1",
"staging/src/k8s.io/api/batch/v1beta1",
"staging/src/k8s.io/api/batch/v2alpha1",
"staging/src/k8s.io/api/certificates/v1beta1",
"staging/src/k8s.io/api/coordination/v1",
"staging/src/k8s.io/api/coordination/v1beta1",
"staging/src/k8s.io/api/core/v1",
"staging/src/k8s.io/api/events/v1beta1",
"staging/src/k8s.io/api/extensions/v1beta1",
"staging/src/k8s.io/api/imagepolicy/v1alpha1",
"staging/src/k8s.io/api/networking/v1",
"staging/src/k8s.io/api/policy/v1beta1",
"staging/src/k8s.io/api/rbac/v1",
"staging/src/k8s.io/api/rbac/v1alpha1",
"staging/src/k8s.io/api/rbac/v1beta1",
"staging/src/k8s.io/api/scheduling/v1alpha1",
"staging/src/k8s.io/api/scheduling/v1beta1",
"staging/src/k8s.io/api/settings/v1alpha1",
"staging/src/k8s.io/api/storage/v1",
"staging/src/k8s.io/api/storage/v1alpha1",
"staging/src/k8s.io/api/storage/v1beta1",
"staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1",
"staging/src/k8s.io/apimachinery/pkg/api/resource",
"staging/src/k8s.io/apimachinery/pkg/apis/meta/v1",
"staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1",
"staging/src/k8s.io/apimachinery/pkg/runtime",
"staging/src/k8s.io/apimachinery/pkg/util/intstr",
"staging/src/k8s.io/apimachinery/pkg/version",
"staging/src/k8s.io/apiserver/pkg/apis/audit/v1",
"staging/src/k8s.io/apiserver/pkg/apis/audit/v1alpha1",
"staging/src/k8s.io/apiserver/pkg/apis/audit/v1beta1",
"staging/src/k8s.io/client-go/pkg/apis/clientauthentication/v1alpha1",
"staging/src/k8s.io/client-go/pkg/apis/clientauthentication/v1beta1",
"staging/src/k8s.io/client-go/pkg/version",
"staging/src/k8s.io/csi-api/pkg/apis/csi/v1alpha1",
"staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1",
"staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1",
"staging/src/k8s.io/kube-controller-manager/config/v1alpha1",
"staging/src/k8s.io/kube-proxy/config/v1alpha1",
"staging/src/k8s.io/kube-scheduler/config/v1alpha1",
"staging/src/k8s.io/kubelet/config/v1beta1",
"staging/src/k8s.io/metrics/pkg/apis/custom_metrics/v1beta1",
"staging/src/k8s.io/metrics/pkg/apis/custom_metrics/v1beta2",
"staging/src/k8s.io/metrics/pkg/apis/external_metrics/v1beta1",
"staging/src/k8s.io/metrics/pkg/apis/metrics/v1alpha1",
"staging/src/k8s.io/metrics/pkg/apis/metrics/v1beta1",
"staging/src/k8s.io/node-api/pkg/apis/node/v1alpha1",
],
}}
# tags_pkgs_values is a dictionary mapping {k8s build tag: {pkg: [tag values in pkg]}}
tags_pkgs_values = {"openapi-gen": {
"cmd/cloud-controller-manager/app/apis/config/v1alpha1": ["true"],
"pkg/apis/abac/v0": ["true"],
"pkg/apis/abac/v1beta1": ["true"],
"pkg/apis/auditregistration": ["true"],
"pkg/version": ["true"],
"staging/src/k8s.io/api/admission/v1beta1": ["false"],
"staging/src/k8s.io/api/admissionregistration/v1alpha1": ["true"],
"staging/src/k8s.io/api/admissionregistration/v1beta1": ["true"],
"staging/src/k8s.io/api/apps/v1": ["true"],
"staging/src/k8s.io/api/apps/v1beta1": ["true"],
"staging/src/k8s.io/api/apps/v1beta2": ["true"],
"staging/src/k8s.io/api/auditregistration/v1alpha1": ["true"],
"staging/src/k8s.io/api/authentication/v1": ["true"],
"staging/src/k8s.io/api/authentication/v1beta1": ["true"],
"staging/src/k8s.io/api/authorization/v1": ["true"],
"staging/src/k8s.io/api/authorization/v1beta1": ["true"],
"staging/src/k8s.io/api/autoscaling/v1": ["true"],
"staging/src/k8s.io/api/autoscaling/v2beta1": ["true"],
"staging/src/k8s.io/api/autoscaling/v2beta2": ["true"],
"staging/src/k8s.io/api/batch/v1": ["true"],
"staging/src/k8s.io/api/batch/v1beta1": ["true"],
"staging/src/k8s.io/api/batch/v2alpha1": ["true"],
"staging/src/k8s.io/api/certificates/v1beta1": ["true"],
"staging/src/k8s.io/api/coordination/v1": ["true"],
"staging/src/k8s.io/api/coordination/v1beta1": ["true"],
"staging/src/k8s.io/api/core/v1": [
"false",
"true",
],
"staging/src/k8s.io/api/events/v1beta1": ["true"],
"staging/src/k8s.io/api/extensions/v1beta1": ["true"],
"staging/src/k8s.io/api/imagepolicy/v1alpha1": ["true"],
"staging/src/k8s.io/api/networking/v1": ["true"],
"staging/src/k8s.io/api/policy/v1beta1": ["true"],
"staging/src/k8s.io/api/rbac/v1": ["true"],
"staging/src/k8s.io/api/rbac/v1alpha1": ["true"],
"staging/src/k8s.io/api/rbac/v1beta1": ["true"],
"staging/src/k8s.io/api/scheduling/v1alpha1": ["true"],
"staging/src/k8s.io/api/scheduling/v1beta1": ["true"],
"staging/src/k8s.io/api/settings/v1alpha1": ["true"],
"staging/src/k8s.io/api/storage/v1": ["true"],
"staging/src/k8s.io/api/storage/v1alpha1": ["true"],
"staging/src/k8s.io/api/storage/v1beta1": ["true"],
"staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1": ["true"],
"staging/src/k8s.io/apimachinery/pkg/api/resource": ["true"],
"staging/src/k8s.io/apimachinery/pkg/apis/meta/v1": ["true"],
"staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1": ["true"],
"staging/src/k8s.io/apimachinery/pkg/apis/testapigroup/v1": ["false"],
"staging/src/k8s.io/apimachinery/pkg/runtime": ["true"],
"staging/src/k8s.io/apimachinery/pkg/util/intstr": ["true"],
"staging/src/k8s.io/apimachinery/pkg/version": ["true"],
"staging/src/k8s.io/apiserver/pkg/apis/audit/v1": ["true"],
"staging/src/k8s.io/apiserver/pkg/apis/audit/v1alpha1": ["true"],
"staging/src/k8s.io/apiserver/pkg/apis/audit/v1beta1": ["true"],
"staging/src/k8s.io/apiserver/pkg/apis/example/v1": ["false"],
"staging/src/k8s.io/apiserver/pkg/apis/example2/v1": ["false"],
"staging/src/k8s.io/client-go/pkg/apis/clientauthentication/v1alpha1": ["true"],
"staging/src/k8s.io/client-go/pkg/apis/clientauthentication/v1beta1": ["true"],
"staging/src/k8s.io/client-go/pkg/version": ["true"],
"staging/src/k8s.io/csi-api/pkg/apis/csi/v1alpha1": ["true"],
"staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1": ["true"],
"staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1": ["true"],
"staging/src/k8s.io/kube-controller-manager/config/v1alpha1": ["true"],
"staging/src/k8s.io/kube-proxy/config/v1alpha1": ["true"],
"staging/src/k8s.io/kube-scheduler/config/v1alpha1": ["true"],
"staging/src/k8s.io/kubelet/config/v1beta1": ["true"],
"staging/src/k8s.io/metrics/pkg/apis/custom_metrics/v1beta1": ["true"],
"staging/src/k8s.io/metrics/pkg/apis/custom_metrics/v1beta2": ["true"],
"staging/src/k8s.io/metrics/pkg/apis/external_metrics/v1beta1": ["true"],
"staging/src/k8s.io/metrics/pkg/apis/metrics/v1alpha1": ["true"],
"staging/src/k8s.io/metrics/pkg/apis/metrics/v1beta1": ["true"],
"staging/src/k8s.io/node-api/pkg/apis/node/v1alpha1": ["true"],
}}
...@@ -16,9 +16,6 @@ ...@@ -16,9 +16,6 @@
# k8s.io/kubernetes will need to set the following variables in # k8s.io/kubernetes will need to set the following variables in
# //build/openapi.bzl in their project and customize the go prefix: # //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_vendor_prefix = "vendor/k8s.io/kubernetes/"
openapi_go_prefix = "k8s.io/kubernetes/"
openapi_vendor_prefix = "" openapi_vendor_prefix = ""
...@@ -17,6 +17,8 @@ build --sandbox_fake_username ...@@ -17,6 +17,8 @@ build --sandbox_fake_username
# Enable go race detection. # Enable go race detection.
build:unit --features=race build:unit --features=race
test:unit --features=race test:unit --features=race
test:unit --build_tests_only
test:unit --test_tag_filters=-e2e,-integration test:unit --test_tag_filters=-e2e,-integration
test:unit --flaky_test_attempts=3 test:unit --flaky_test_attempts=3
......
...@@ -6,5 +6,9 @@ ...@@ -6,5 +6,9 @@
"^third_party/etcd.*" "^third_party/etcd.*"
], ],
"AddSourcesRules": true, "AddSourcesRules": true,
"K8sOpenAPIGen": true "K8sCodegenBzlFile": "build/kazel_generated.bzl",
"K8sCodegenBoilerplateFile": "hack/boilerplate/boilerplate.generatebzl.txt",
"K8sCodegenTags": [
"openapi-gen"
]
} }
...@@ -568,13 +568,11 @@ bazel-test: ...@@ -568,13 +568,11 @@ bazel-test:
@echo "$$BAZEL_TEST_HELP_INFO" @echo "$$BAZEL_TEST_HELP_INFO"
else else
# //hack:verify-all is a manual target. # //hack:verify-all is a manual target.
# We don't want to build any of the release artifacts when running tests.
# Some things in vendor don't build due to empty target lists for cross-platform rules. # Some things in vendor don't build due to empty target lists for cross-platform rules.
bazel-test: bazel-test:
bazel test --config=unit -- \ bazel test --config=unit -- \
//... \ //... \
//hack:verify-all \ //hack:verify-all \
-//build/... \
-//vendor/... -//vendor/...
endif endif
......
...@@ -74,7 +74,6 @@ go_test( ...@@ -74,7 +74,6 @@ go_test(
name = "go_default_test", name = "go_default_test",
srcs = ["options_test.go"], srcs = ["options_test.go"],
embed = [":go_default_library"], embed = [":go_default_library"],
tags = ["automanaged"],
deps = [ deps = [
"//cmd/controller-manager/app/options:go_default_library", "//cmd/controller-manager/app/options:go_default_library",
"//pkg/controller/apis/config:go_default_library", "//pkg/controller/apis/config:go_default_library",
......
# Copyright 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.
...@@ -83,10 +83,12 @@ def file_passes(filename, refs, regexs): ...@@ -83,10 +83,12 @@ def file_passes(filename, refs, regexs):
generated = is_generated_file(filename, data, regexs) generated = is_generated_file(filename, data, regexs)
basename = os.path.basename(filename) basename = os.path.basename(filename)
extension = file_extension(filename)
if generated: if generated:
extension = "generatego" if extension == "go":
else: extension = "generatego"
extension = file_extension(filename) elif extension == "bzl":
extension = "generatebzl"
if extension != "": if extension != "":
ref = refs[extension] ref = refs[extension]
......
...@@ -61,8 +61,8 @@ REQUIRED_BINS=( ...@@ -61,8 +61,8 @@ REQUIRED_BINS=(
"github.com/cloudflare/cfssl/cmd/cfssl" "github.com/cloudflare/cfssl/cmd/cfssl"
"github.com/cloudflare/cfssl/cmd/cfssljson" "github.com/cloudflare/cfssl/cmd/cfssljson"
"github.com/bazelbuild/bazel-gazelle/cmd/gazelle" "github.com/bazelbuild/bazel-gazelle/cmd/gazelle"
"github.com/kubernetes/repo-infra/kazel"
"k8s.io/kube-openapi/cmd/openapi-gen" "k8s.io/kube-openapi/cmd/openapi-gen"
"k8s.io/repo-infra/kazel"
"golang.org/x/lint/golint" "golang.org/x/lint/golint"
"./..." "./..."
) )
......
...@@ -20,17 +20,13 @@ set -o pipefail ...@@ -20,17 +20,13 @@ set -o pipefail
export KUBE_ROOT=$(dirname "${BASH_SOURCE}")/.. export KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${KUBE_ROOT}/hack/lib/init.sh" source "${KUBE_ROOT}/hack/lib/init.sh"
# Remove generated files prior to running kazel.
# TODO(spxtr): Remove this line once Bazel is the only way to build.
rm -f "${KUBE_ROOT}/pkg/generated/openapi/zz_generated.openapi.go"
# Ensure that we find the binaries we build before anything else. # Ensure that we find the binaries we build before anything else.
export GOBIN="${KUBE_OUTPUT_BINPATH}" export GOBIN="${KUBE_OUTPUT_BINPATH}"
PATH="${GOBIN}:${PATH}" PATH="${GOBIN}:${PATH}"
# Install tools we need, but only from vendor/... # Install tools we need, but only from vendor/...
go install k8s.io/kubernetes/vendor/github.com/bazelbuild/bazel-gazelle/cmd/gazelle go install k8s.io/kubernetes/vendor/github.com/bazelbuild/bazel-gazelle/cmd/gazelle
go install k8s.io/kubernetes/vendor/github.com/kubernetes/repo-infra/kazel go install k8s.io/kubernetes/vendor/k8s.io/repo-infra/kazel
touch "${KUBE_ROOT}/vendor/BUILD" touch "${KUBE_ROOT}/vendor/BUILD"
# Ensure that we use the correct importmap for all vendored dependencies. # Ensure that we use the correct importmap for all vendored dependencies.
......
# doc.go is managed by kazel, so gazelle should ignore it.
# gazelle:exclude doc.go
package(default_visibility = ["//visibility:public"]) package(default_visibility = ["//visibility:public"])
load("//pkg/generated/openapi:def.bzl", "openapi_library") load("//build:code_generation.bzl", "gen_openapi", "openapi_deps")
load("//build:openapi.bzl", "openapi_go_prefix", "openapi_vendor_prefix") 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",
)
openapi_library( go_library(
name = "go_default_library", name = "go_default_library",
srcs = ["doc.go"], srcs = [
go_prefix = openapi_go_prefix, "doc.go",
openapi_targets = [ "zz_generated.openapi.go",
"cmd/cloud-controller-manager/app/apis/config/v1alpha1",
"pkg/apis/abac/v0",
"pkg/apis/abac/v1beta1",
"pkg/apis/auditregistration",
"pkg/version",
],
tags = ["automanaged"],
vendor_prefix = openapi_vendor_prefix,
vendor_targets = [
"k8s.io/api/admission/v1beta1",
"k8s.io/api/admissionregistration/v1alpha1",
"k8s.io/api/admissionregistration/v1beta1",
"k8s.io/api/apps/v1",
"k8s.io/api/apps/v1beta1",
"k8s.io/api/apps/v1beta2",
"k8s.io/api/auditregistration/v1alpha1",
"k8s.io/api/authentication/v1",
"k8s.io/api/authentication/v1beta1",
"k8s.io/api/authorization/v1",
"k8s.io/api/authorization/v1beta1",
"k8s.io/api/autoscaling/v1",
"k8s.io/api/autoscaling/v2beta1",
"k8s.io/api/autoscaling/v2beta2",
"k8s.io/api/batch/v1",
"k8s.io/api/batch/v1beta1",
"k8s.io/api/batch/v2alpha1",
"k8s.io/api/certificates/v1beta1",
"k8s.io/api/coordination/v1",
"k8s.io/api/coordination/v1beta1",
"k8s.io/api/core/v1",
"k8s.io/api/events/v1beta1",
"k8s.io/api/extensions/v1beta1",
"k8s.io/api/imagepolicy/v1alpha1",
"k8s.io/api/networking/v1",
"k8s.io/api/policy/v1beta1",
"k8s.io/api/rbac/v1",
"k8s.io/api/rbac/v1alpha1",
"k8s.io/api/rbac/v1beta1",
"k8s.io/api/scheduling/v1alpha1",
"k8s.io/api/scheduling/v1beta1",
"k8s.io/api/settings/v1alpha1",
"k8s.io/api/storage/v1",
"k8s.io/api/storage/v1alpha1",
"k8s.io/api/storage/v1beta1",
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1",
"k8s.io/apimachinery/pkg/api/resource",
"k8s.io/apimachinery/pkg/apis/meta/v1",
"k8s.io/apimachinery/pkg/apis/meta/v1beta1",
"k8s.io/apimachinery/pkg/apis/testapigroup/v1",
"k8s.io/apimachinery/pkg/runtime",
"k8s.io/apimachinery/pkg/util/intstr",
"k8s.io/apimachinery/pkg/version",
"k8s.io/apiserver/pkg/apis/audit/v1",
"k8s.io/apiserver/pkg/apis/audit/v1alpha1",
"k8s.io/apiserver/pkg/apis/audit/v1beta1",
"k8s.io/apiserver/pkg/apis/example/v1",
"k8s.io/apiserver/pkg/apis/example2/v1",
"k8s.io/client-go/pkg/apis/clientauthentication/v1alpha1",
"k8s.io/client-go/pkg/apis/clientauthentication/v1beta1",
"k8s.io/client-go/pkg/version",
"k8s.io/csi-api/pkg/apis/csi/v1alpha1",
"k8s.io/kube-aggregator/pkg/apis/apiregistration/v1",
"k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1",
"k8s.io/kube-controller-manager/config/v1alpha1",
"k8s.io/kube-proxy/config/v1alpha1",
"k8s.io/kube-scheduler/config/v1alpha1",
"k8s.io/kubelet/config/v1beta1",
"k8s.io/metrics/pkg/apis/custom_metrics/v1beta1",
"k8s.io/metrics/pkg/apis/custom_metrics/v1beta2",
"k8s.io/metrics/pkg/apis/external_metrics/v1beta1",
"k8s.io/metrics/pkg/apis/metrics/v1alpha1",
"k8s.io/metrics/pkg/apis/metrics/v1beta1",
"k8s.io/node-api/pkg/apis/node/v1alpha1",
], ],
importpath = "k8s.io/kubernetes/pkg/generated/openapi",
deps = openapi_deps(), # keep
) )
filegroup( filegroup(
......
# 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"],
)
...@@ -45,7 +45,6 @@ go_test( ...@@ -45,7 +45,6 @@ go_test(
name = "go_default_test", name = "go_default_test",
srcs = ["remote_runtime_test.go"], srcs = ["remote_runtime_test.go"],
embed = [":go_default_library"], embed = [":go_default_library"],
tags = ["automanaged"],
deps = [ deps = [
"//pkg/kubelet/apis/cri:go_default_library", "//pkg/kubelet/apis/cri:go_default_library",
"//pkg/kubelet/apis/cri/testing:go_default_library", "//pkg/kubelet/apis/cri/testing:go_default_library",
......
...@@ -17,7 +17,6 @@ go_library( ...@@ -17,7 +17,6 @@ go_library(
"fake_runtime.go", "fake_runtime.go",
], ],
importpath = "k8s.io/kubernetes/pkg/kubelet/remote/fake", importpath = "k8s.io/kubernetes/pkg/kubelet/remote/fake",
tags = ["automanaged"],
deps = [ deps = [
"//pkg/kubelet/apis/cri/runtime/v1alpha2:go_default_library", "//pkg/kubelet/apis/cri/runtime/v1alpha2:go_default_library",
"//pkg/kubelet/apis/cri/testing:go_default_library", "//pkg/kubelet/apis/cri/testing:go_default_library",
......
...@@ -15,7 +15,6 @@ go_library( ...@@ -15,7 +15,6 @@ go_library(
"util.go", "util.go",
], ],
importpath = "k8s.io/kubernetes/pkg/proxy/ipvs/testing", importpath = "k8s.io/kubernetes/pkg/proxy/ipvs/testing",
tags = ["automanaged"],
deps = [ deps = [
"//pkg/util/ipset:go_default_library", "//pkg/util/ipset:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
......
...@@ -15,7 +15,6 @@ go_test( ...@@ -15,7 +15,6 @@ go_test(
"certificate_store_test.go", "certificate_store_test.go",
], ],
embed = [":go_default_library"], embed = [":go_default_library"],
tags = ["automanaged"],
deps = [ deps = [
"//staging/src/k8s.io/api/certificates/v1beta1:go_default_library", "//staging/src/k8s.io/api/certificates/v1beta1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
...@@ -35,7 +34,6 @@ go_library( ...@@ -35,7 +34,6 @@ go_library(
], ],
importmap = "k8s.io/kubernetes/vendor/k8s.io/client-go/util/certificate", importmap = "k8s.io/kubernetes/vendor/k8s.io/client-go/util/certificate",
importpath = "k8s.io/client-go/util/certificate", importpath = "k8s.io/client-go/util/certificate",
tags = ["automanaged"],
deps = [ deps = [
"//staging/src/k8s.io/api/certificates/v1beta1:go_default_library", "//staging/src/k8s.io/api/certificates/v1beta1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
......
...@@ -293,7 +293,6 @@ filegroup( ...@@ -293,7 +293,6 @@ filegroup(
"//vendor/github.com/kr/fs:all-srcs", "//vendor/github.com/kr/fs:all-srcs",
"//vendor/github.com/kr/pretty:all-srcs", "//vendor/github.com/kr/pretty:all-srcs",
"//vendor/github.com/kr/text:all-srcs", "//vendor/github.com/kr/text:all-srcs",
"//vendor/github.com/kubernetes/repo-infra/kazel:all-srcs",
"//vendor/github.com/lib/pq:all-srcs", "//vendor/github.com/lib/pq:all-srcs",
"//vendor/github.com/libopenstorage/openstorage/api:all-srcs", "//vendor/github.com/libopenstorage/openstorage/api:all-srcs",
"//vendor/github.com/libopenstorage/openstorage/pkg/parser:all-srcs", "//vendor/github.com/libopenstorage/openstorage/pkg/parser:all-srcs",
...@@ -469,6 +468,7 @@ filegroup( ...@@ -469,6 +468,7 @@ filegroup(
"//vendor/k8s.io/kube-openapi/pkg/generators:all-srcs", "//vendor/k8s.io/kube-openapi/pkg/generators:all-srcs",
"//vendor/k8s.io/kube-openapi/pkg/handler:all-srcs", "//vendor/k8s.io/kube-openapi/pkg/handler:all-srcs",
"//vendor/k8s.io/kube-openapi/pkg/util:all-srcs", "//vendor/k8s.io/kube-openapi/pkg/util:all-srcs",
"//vendor/k8s.io/repo-infra/kazel:all-srcs",
"//vendor/k8s.io/utils/clock:all-srcs", "//vendor/k8s.io/utils/clock:all-srcs",
"//vendor/k8s.io/utils/exec:all-srcs", "//vendor/k8s.io/utils/exec:all-srcs",
"//vendor/k8s.io/utils/pointer:all-srcs", "//vendor/k8s.io/utils/pointer:all-srcs",
......
/*
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.
*/
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
)
const (
openAPITagName = "openapi-gen"
staging = "staging/src/"
)
var (
// Generator tags are specified using the format "// +k8s:name=value"
genTagRe = regexp.MustCompile(`//\s*\+k8s:([^\s=]+)(?:=(\S+))\s*\n`)
)
// walkGenerated updates the rule for kubernetes' OpenAPI generated file.
// This involves reading all go files in the source tree and looking for the
// "+k8s:openapi-gen" tag. If present, then that package must be supplied to
// the genrule.
func (v *Vendorer) walkGenerated() error {
if !v.cfg.K8sOpenAPIGen {
return nil
}
v.managedAttrs = append(v.managedAttrs, "openapi_targets", "vendor_targets")
paths, err := v.findOpenAPI(".")
if err != nil {
return err
}
return v.addGeneratedOpenAPIRule(paths)
}
// findOpenAPI searches for all packages under root that request OpenAPI. It
// returns the go import paths. It does not follow symlinks.
func (v *Vendorer) findOpenAPI(root string) ([]string, error) {
for _, r := range v.skippedOpenAPIPaths {
if r.MatchString(root) {
return nil, nil
}
}
finfos, err := ioutil.ReadDir(root)
if err != nil {
return nil, err
}
var res []string
var includeMe bool
for _, finfo := range finfos {
path := filepath.Join(root, finfo.Name())
if finfo.IsDir() && (finfo.Mode()&os.ModeSymlink == 0) {
children, err := v.findOpenAPI(path)
if err != nil {
return nil, err
}
res = append(res, children...)
} else if strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, "_test.go") {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
matches := genTagRe.FindAllSubmatch(b, -1)
for _, m := range matches {
if len(m) >= 2 && string(m[1]) == openAPITagName {
includeMe = true
break
}
}
}
}
if includeMe {
pkg, err := v.ctx.ImportDir(filepath.Join(v.root, root), 0)
if err != nil {
return nil, err
}
res = append(res, pkg.ImportPath)
}
return res, nil
}
// addGeneratedOpenAPIRule updates the pkg/generated/openapi go_default_library
// rule with the automanaged openapi_targets and vendor_targets.
func (v *Vendorer) addGeneratedOpenAPIRule(paths []string) error {
var openAPITargets []string
var vendorTargets []string
baseImport := v.cfg.GoPrefix + "/"
for _, p := range paths {
if !strings.HasPrefix(p, baseImport) {
return fmt.Errorf("openapi-gen path outside of %s: %s", v.cfg.GoPrefix, p)
}
np := p[len(baseImport):]
if strings.HasPrefix(np, staging) {
vendorTargets = append(vendorTargets, np[len(staging):])
} else {
openAPITargets = append(openAPITargets, np)
}
}
sort.Strings(openAPITargets)
sort.Strings(vendorTargets)
pkgPath := filepath.Join("pkg", "generated", "openapi")
// If we haven't walked this package yet, walk it so there is a go_library rule to modify
if len(v.newRules[pkgPath]) == 0 {
if err := v.updateSinglePkg(pkgPath); err != nil {
return err
}
}
for _, r := range v.newRules[pkgPath] {
if r.Name() == "go_default_library" {
r.SetAttr("openapi_targets", asExpr(openAPITargets))
r.SetAttr("vendor_targets", asExpr(vendorTargets))
break
}
}
return nil
}
...@@ -9,8 +9,8 @@ go_library( ...@@ -9,8 +9,8 @@ go_library(
"kazel.go", "kazel.go",
"sourcerer.go", "sourcerer.go",
], ],
importmap = "k8s.io/kubernetes/vendor/github.com/kubernetes/repo-infra/kazel", importmap = "k8s.io/kubernetes/vendor/k8s.io/repo-infra/kazel",
importpath = "github.com/kubernetes/repo-infra/kazel", importpath = "k8s.io/repo-infra/kazel",
visibility = ["//visibility:private"], visibility = ["//visibility:private"],
deps = [ deps = [
"//vendor/github.com/bazelbuild/buildtools/build:go_default_library", "//vendor/github.com/bazelbuild/buildtools/build:go_default_library",
......
...@@ -28,19 +28,25 @@ type Cfg struct { ...@@ -28,19 +28,25 @@ type Cfg struct {
SrcDirs []string SrcDirs []string
// regexps that match packages to skip // regexps that match packages to skip
SkippedPaths []string SkippedPaths []string
// regexps that match packages to skip for K8SOpenAPIGen. // regexps that match packages to skip for k8s codegen.
// note that this skips anything matched by SkippedPaths as well. // note that this skips anything matched by SkippedPaths as well.
SkippedOpenAPIGenPaths []string SkippedK8sCodegenPaths []string
// whether to add "pkg-srcs" and "all-srcs" filegroups // whether to add "pkg-srcs" and "all-srcs" filegroups
// note that this operates on the entire tree (not just SrcsDirs) but skips anything matching SkippedPaths // note that this operates on the entire tree (not just SrcsDirs) but skips anything matching SkippedPaths
AddSourcesRules bool AddSourcesRules bool
// whether to have multiple build files in vendor/ or just one. // whether to have multiple build files in vendor/ or just one.
VendorMultipleBuildFiles bool VendorMultipleBuildFiles bool
// whether to manage kubernetes' pkg/generated/openapi.
K8sOpenAPIGen bool
// Whether to manage the upstream Go rules provided by bazelbuild/rules_go. // Whether to manage the upstream Go rules provided by bazelbuild/rules_go.
// If using gazelle, set this to false (or omit). // If using gazelle, set this to false (or omit).
ManageGoRules bool ManageGoRules bool
// If defined, metadata parsed from "+k8s:" codegen build tags will be saved into this file.
K8sCodegenBzlFile string
// If defined, contains the boilerplate text to be included in the header of the generated bzl file.
K8sCodegenBoilerplateFile string
// Which tags to include in the codegen bzl file.
// Include only the name of the tag.
// For example, to include +k8s:foo=bar, list "foo" here.
K8sCodegenTags []string
} }
// ReadCfg reads and unmarshals the specified json file into a Cfg struct. // ReadCfg reads and unmarshals the specified json file into a Cfg struct.
......
/*
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.
*/
package main
import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/bazelbuild/buildtools/build"
)
var (
// Generator tags are specified using the format "// +k8s:name=value"
genTagRe = regexp.MustCompile(`//\s*\+k8s:([^\s=]+)(?:=(\S+))\s*\n`)
)
// {tagName: {value: {pkgs}}} or {tagName: {pkg: {values}}}
type generatorTagsMap map[string]map[string]map[string]bool
// extractTags finds k8s codegen tags found in b listed in requestedTags.
// It returns a map of {tag name: slice of values for that tag}.
func extractTags(b []byte, requestedTags map[string]bool) map[string][]string {
tags := make(map[string][]string)
matches := genTagRe.FindAllSubmatch(b, -1)
for _, m := range matches {
if len(m) >= 3 {
tag, values := string(m[1]), string(m[2])
if _, requested := requestedTags[tag]; !requested {
continue
}
tags[tag] = append(tags[tag], strings.Split(values, ",")...)
}
}
return tags
}
// findGeneratorTags searches for all packages under root that include a kubernetes generator
// tag comment. It does not follow symlinks, and any path in the configured skippedPaths
// or codegen skipped paths is skipped.
func (v *Vendorer) findGeneratorTags(root string, requestedTags map[string]bool) (tagsValuesPkgs, tagsPkgsValues generatorTagsMap, err error) {
tagsValuesPkgs = make(generatorTagsMap)
tagsPkgsValues = make(generatorTagsMap)
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
pkg := filepath.Dir(path)
for _, r := range v.skippedK8sCodegenPaths {
if r.MatchString(pkg) {
return filepath.SkipDir
}
}
if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
return nil
}
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
for tag, values := range extractTags(b, requestedTags) {
if _, present := tagsValuesPkgs[tag]; !present {
tagsValuesPkgs[tag] = make(map[string]map[string]bool)
}
if _, present := tagsPkgsValues[tag]; !present {
tagsPkgsValues[tag] = make(map[string]map[string]bool)
}
if _, present := tagsPkgsValues[tag][pkg]; !present {
tagsPkgsValues[tag][pkg] = make(map[string]bool)
}
for _, v := range values {
if _, present := tagsValuesPkgs[tag][v]; !present {
tagsValuesPkgs[tag][v] = make(map[string]bool)
}
// Since multiple files in the same package may list a given tag/value, use a set to deduplicate.
tagsValuesPkgs[tag][v][pkg] = true
tagsPkgsValues[tag][pkg][v] = true
}
}
return nil
})
if err != nil {
return nil, nil, err
}
return
}
// flattened returns a copy of the map with the final stringSet flattened into a sorted slice.
func flattened(m generatorTagsMap) map[string]map[string][]string {
flattened := make(map[string]map[string][]string)
for tag, subMap := range m {
flattened[tag] = make(map[string][]string)
for k, subSet := range subMap {
for v := range subSet {
flattened[tag][k] = append(flattened[tag][k], v)
}
sort.Strings(flattened[tag][k])
}
}
return flattened
}
// walkGenerated generates a k8s codegen bzl file that can be parsed by Starlark
// rules and macros to find packages needed k8s code generation.
// This involves reading all non-test go sources in the tree and looking for
// "+k8s:name=value" tags. Only those tags listed in K8sCodegenTags will be
// included.
// If a K8sCodegenBoilerplateFile was configured, the contents of this file
// will be included as the header of the generated bzl file.
// Returns true if there are diffs against the existing generated bzl file.
func (v *Vendorer) walkGenerated() (bool, error) {
if v.cfg.K8sCodegenBzlFile == "" {
return false, nil
}
// only include the specified tags
requestedTags := make(map[string]bool)
for _, tag := range v.cfg.K8sCodegenTags {
requestedTags[tag] = true
}
tagsValuesPkgs, tagsPkgsValues, err := v.findGeneratorTags(".", requestedTags)
if err != nil {
return false, err
}
f := &build.File{
Path: v.cfg.K8sCodegenBzlFile,
}
addCommentBefore(f, "#################################################")
addCommentBefore(f, "# # # # # # # # # # # # # # # # # # # # # # # # #")
addCommentBefore(f, "This file is autogenerated by kazel. DO NOT EDIT.")
addCommentBefore(f, "# # # # # # # # # # # # # # # # # # # # # # # # #")
addCommentBefore(f, "#################################################")
addCommentBefore(f, "")
f.Stmt = append(f.Stmt, varExpr("go_prefix", "The go prefix passed to kazel", v.cfg.GoPrefix))
f.Stmt = append(f.Stmt, varExpr("kazel_configured_tags", "The list of codegen tags kazel is configured to find", v.cfg.K8sCodegenTags))
f.Stmt = append(f.Stmt, varExpr("tags_values_pkgs", "tags_values_pkgs is a dictionary mapping {k8s build tag: {tag value: [pkgs including that tag:value]}}", flattened(tagsValuesPkgs)))
f.Stmt = append(f.Stmt, varExpr("tags_pkgs_values", "tags_pkgs_values is a dictionary mapping {k8s build tag: {pkg: [tag values in pkg]}}", flattened(tagsPkgsValues)))
var boilerplate []byte
if v.cfg.K8sCodegenBoilerplateFile != "" {
boilerplate, err = ioutil.ReadFile(v.cfg.K8sCodegenBoilerplateFile)
if err != nil {
return false, err
}
}
// Open existing file to use in diff mode.
_, err = os.Stat(f.Path)
if err != nil && !os.IsNotExist(err) {
return false, err
}
return writeFile(f.Path, f, boilerplate, !os.IsNotExist(err), v.dryRun)
}
...@@ -21,7 +21,7 @@ import ( ...@@ -21,7 +21,7 @@ import (
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
bzl "github.com/bazelbuild/buildtools/build" "github.com/bazelbuild/buildtools/build"
) )
const ( const (
...@@ -85,21 +85,21 @@ func (v *Vendorer) walkSource(pkgPath string) ([]string, error) { ...@@ -85,21 +85,21 @@ func (v *Vendorer) walkSource(pkgPath string) ([]string, error) {
return nil, nil return nil, nil
} }
pkgSrcsExpr := &bzl.LiteralExpr{Token: `glob(["**"])`} pkgSrcsExpr := &build.LiteralExpr{Token: `glob(["**"])`}
if pkgPath == "." { if pkgPath == "." {
pkgSrcsExpr = &bzl.LiteralExpr{Token: `glob(["**"], exclude=["bazel-*/**", ".git/**"])`} pkgSrcsExpr = &build.LiteralExpr{Token: `glob(["**"], exclude=["bazel-*/**", ".git/**"])`}
} }
v.addRules(pkgPath, []*bzl.Rule{ v.addRules(pkgPath, []*build.Rule{
newRule(RuleTypeFileGroup, newRule("filegroup",
func(_ ruleType) string { return pkgSrcsTarget }, pkgSrcsTarget,
map[string]bzl.Expr{ map[string]build.Expr{
"srcs": pkgSrcsExpr, "srcs": pkgSrcsExpr,
"visibility": asExpr([]string{"//visibility:private"}), "visibility": asExpr([]string{"//visibility:private"}),
}), }),
newRule(RuleTypeFileGroup, newRule("filegroup",
func(_ ruleType) string { return allSrcsTarget }, allSrcsTarget,
map[string]bzl.Expr{ map[string]build.Expr{
"srcs": asExpr(append(children, fmt.Sprintf(":%s", pkgSrcsTarget))), "srcs": asExpr(append(children, fmt.Sprintf(":%s", pkgSrcsTarget))),
// TODO: should this be more restricted? // TODO: should this be more restricted?
"visibility": asExpr([]string{"//visibility:public"}), "visibility": asExpr([]string{"//visibility:public"}),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment