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

bazel: refactor openapi-gen logic for new kazel
pull/564/head
Kubernetes Prow Robot 2019-01-08 20:58:07 -08:00 committed by GitHub
commit 555c63fff5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 1159 additions and 1326 deletions

10
Godeps/Godeps.json generated
View File

@ -9,8 +9,8 @@
"github.com/cloudflare/cfssl/cmd/cfssl",
"github.com/cloudflare/cfssl/cmd/cfssljson",
"github.com/bazelbuild/bazel-gazelle/cmd/gazelle",
"github.com/kubernetes/repo-infra/kazel",
"k8s.io/kube-openapi/cmd/openapi-gen",
"k8s.io/repo-infra/kazel",
"golang.org/x/lint/golint",
"./..."
],
@ -2405,10 +2405,6 @@
"ImportPath": "github.com/kr/text",
"Rev": "6807e777504f54ad073ecef66747de158294b639"
},
{
"ImportPath": "github.com/kubernetes/repo-infra/kazel",
"Rev": "f2459dc75fc429b813d92c0622b408fd7f0d4cac"
},
{
"ImportPath": "github.com/lib/pq",
"Rev": "88edab0803230a3898347e77b474f8c1820a1f20"
@ -4098,6 +4094,10 @@
"ImportPath": "k8s.io/kube-openapi/pkg/util/sets",
"Rev": "c59034cc13d587f5ef4e85ca0ade0c1866ae8e1d"
},
{
"ImportPath": "k8s.io/repo-infra/kazel",
"Rev": "00fe14e3d1a3f9a73c4cea62d9c33b29c1e03ac4"
},
{
"ImportPath": "k8s.io/utils/clock",
"Rev": "8e7ff06bf0e2d3289061230af203e430a15b6dcc"

418
Godeps/LICENSES generated
View File

@ -73319,215 +73319,6 @@ THE SOFTWARE.
================================================================================
================================================================================
= vendor/github.com/kubernetes/repo-infra/kazel licensed under: =
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
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.
= vendor/github.com/kubernetes/repo-infra/LICENSE e3fc50a88d0a364313df4b21ef20c29e
================================================================================
================================================================================
= vendor/github.com/lib/pq licensed under: =
@ -115013,6 +114804,215 @@ third-party archives.
================================================================================
================================================================================
= vendor/k8s.io/repo-infra/kazel licensed under: =
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
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.
= vendor/k8s.io/repo-infra/LICENSE e3fc50a88d0a364313df4b21ef20c29e
================================================================================
================================================================================
= vendor/k8s.io/utils/clock licensed under: =

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,
)

179
build/kazel_generated.bzl Normal file
View File

@ -0,0 +1,179 @@
# 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"],
}}

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

@ -17,6 +17,8 @@ build --sandbox_fake_username
# Enable go race detection.
build:unit --features=race
test:unit --features=race
test:unit --build_tests_only
test:unit --test_tag_filters=-e2e,-integration
test:unit --flaky_test_attempts=3

View File

@ -6,5 +6,9 @@
"^third_party/etcd.*"
],
"AddSourcesRules": true,
"K8sOpenAPIGen": true
"K8sCodegenBzlFile": "build/kazel_generated.bzl",
"K8sCodegenBoilerplateFile": "hack/boilerplate/boilerplate.generatebzl.txt",
"K8sCodegenTags": [
"openapi-gen"
]
}

View File

@ -568,13 +568,11 @@ bazel-test:
@echo "$$BAZEL_TEST_HELP_INFO"
else
# //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.
bazel-test:
bazel test --config=unit -- \
//... \
//hack:verify-all \
-//build/... \
-//vendor/...
endif

View File

@ -74,7 +74,6 @@ go_test(
name = "go_default_test",
srcs = ["options_test.go"],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//cmd/controller-manager/app/options:go_default_library",
"//pkg/controller/apis/config:go_default_library",

View File

@ -0,0 +1,14 @@
# 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.

View File

@ -83,10 +83,12 @@ def file_passes(filename, refs, regexs):
generated = is_generated_file(filename, data, regexs)
basename = os.path.basename(filename)
extension = file_extension(filename)
if generated:
extension = "generatego"
else:
extension = file_extension(filename)
if extension == "go":
extension = "generatego"
elif extension == "bzl":
extension = "generatebzl"
if extension != "":
ref = refs[extension]

View File

@ -61,8 +61,8 @@ REQUIRED_BINS=(
"github.com/cloudflare/cfssl/cmd/cfssl"
"github.com/cloudflare/cfssl/cmd/cfssljson"
"github.com/bazelbuild/bazel-gazelle/cmd/gazelle"
"github.com/kubernetes/repo-infra/kazel"
"k8s.io/kube-openapi/cmd/openapi-gen"
"k8s.io/repo-infra/kazel"
"golang.org/x/lint/golint"
"./..."
)

View File

@ -20,17 +20,13 @@ set -o pipefail
export KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
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.
export GOBIN="${KUBE_OUTPUT_BINPATH}"
PATH="${GOBIN}:${PATH}"
# 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/kubernetes/repo-infra/kazel
go install k8s.io/kubernetes/vendor/k8s.io/repo-infra/kazel
touch "${KUBE_ROOT}/vendor/BUILD"
# Ensure that we use the correct importmap for all vendored dependencies.

View File

@ -1,90 +1,21 @@
# 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")
openapi_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"],
go_prefix = openapi_go_prefix,
openapi_targets = [
"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",
srcs = [
"doc.go",
"zz_generated.openapi.go",
],
importpath = "k8s.io/kubernetes/pkg/generated/openapi",
deps = openapi_deps(), # keep
)
filegroup(

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"],
)

View File

@ -45,7 +45,6 @@ go_test(
name = "go_default_test",
srcs = ["remote_runtime_test.go"],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//pkg/kubelet/apis/cri:go_default_library",
"//pkg/kubelet/apis/cri/testing:go_default_library",

View File

@ -17,7 +17,6 @@ go_library(
"fake_runtime.go",
],
importpath = "k8s.io/kubernetes/pkg/kubelet/remote/fake",
tags = ["automanaged"],
deps = [
"//pkg/kubelet/apis/cri/runtime/v1alpha2:go_default_library",
"//pkg/kubelet/apis/cri/testing:go_default_library",

View File

@ -15,7 +15,6 @@ go_library(
"util.go",
],
importpath = "k8s.io/kubernetes/pkg/proxy/ipvs/testing",
tags = ["automanaged"],
deps = [
"//pkg/util/ipset:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",

View File

@ -15,7 +15,6 @@ go_test(
"certificate_store_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//staging/src/k8s.io/api/certificates/v1beta1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
@ -35,7 +34,6 @@ go_library(
],
importmap = "k8s.io/kubernetes/vendor/k8s.io/client-go/util/certificate",
importpath = "k8s.io/client-go/util/certificate",
tags = ["automanaged"],
deps = [
"//staging/src/k8s.io/api/certificates/v1beta1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",

2
vendor/BUILD vendored
View File

@ -293,7 +293,6 @@ filegroup(
"//vendor/github.com/kr/fs:all-srcs",
"//vendor/github.com/kr/pretty: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/libopenstorage/openstorage/api:all-srcs",
"//vendor/github.com/libopenstorage/openstorage/pkg/parser:all-srcs",
@ -469,6 +468,7 @@ filegroup(
"//vendor/k8s.io/kube-openapi/pkg/generators:all-srcs",
"//vendor/k8s.io/kube-openapi/pkg/handler: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/exec:all-srcs",
"//vendor/k8s.io/utils/pointer:all-srcs",

View File

@ -1,136 +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.
*/
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
}

View File

@ -1,803 +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.
*/
package main
import (
"bytes"
"flag"
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"regexp"
"runtime"
"sort"
"strings"
bzl "github.com/bazelbuild/buildtools/build"
"k8s.io/klog"
)
const (
vendorPath = "vendor/"
automanagedTag = "automanaged"
)
var (
root = flag.String("root", ".", "root of go source")
dryRun = flag.Bool("dry-run", false, "run in dry mode")
printDiff = flag.Bool("print-diff", false, "print diff to stdout")
validate = flag.Bool("validate", false, "run in dry mode and exit nonzero if any BUILD files need to be updated")
cfgPath = flag.String("cfg-path", ".kazelcfg.json", "path to kazel config (relative paths interpreted relative to -repo.")
)
func main() {
flag.Parse()
flag.Set("alsologtostderr", "true")
if *root == "" {
klog.Fatalf("-root argument is required")
}
if *validate {
*dryRun = true
}
v, err := newVendorer(*root, *cfgPath, *dryRun)
if err != nil {
klog.Fatalf("unable to build vendorer: %v", err)
}
if err = os.Chdir(v.root); err != nil {
klog.Fatalf("cannot chdir into root %q: %v", v.root, err)
}
if v.cfg.ManageGoRules {
if err = v.walkVendor(); err != nil {
klog.Fatalf("err walking vendor: %v", err)
}
if err = v.walkRepo(); err != nil {
klog.Fatalf("err walking repo: %v", err)
}
}
if err = v.walkGenerated(); err != nil {
klog.Fatalf("err walking generated: %v", err)
}
if _, err = v.walkSource("."); err != nil {
klog.Fatalf("err walking source: %v", err)
}
written := 0
if written, err = v.reconcileAllRules(); err != nil {
klog.Fatalf("err reconciling rules: %v", err)
}
if *validate && written > 0 {
fmt.Fprintf(os.Stderr, "\n%d BUILD files not up-to-date.\n", written)
os.Exit(1)
}
}
// Vendorer collects context, configuration, and cache while walking the tree.
type Vendorer struct {
ctx *build.Context
icache map[icacheKey]icacheVal
skippedPaths []*regexp.Regexp
skippedOpenAPIPaths []*regexp.Regexp
dryRun bool
root string
cfg *Cfg
newRules map[string][]*bzl.Rule // package path -> list of rules to add or update
managedAttrs []string
}
func newVendorer(root, cfgPath string, dryRun bool) (*Vendorer, error) {
absRoot, err := filepath.Abs(root)
if err != nil {
return nil, fmt.Errorf("could not get absolute path: %v", err)
}
if !filepath.IsAbs(cfgPath) {
cfgPath = filepath.Join(absRoot, cfgPath)
}
cfg, err := ReadCfg(cfgPath)
if err != nil {
return nil, err
}
v := Vendorer{
ctx: context(),
dryRun: dryRun,
root: absRoot,
icache: map[icacheKey]icacheVal{},
cfg: cfg,
newRules: make(map[string][]*bzl.Rule),
managedAttrs: []string{"srcs", "deps", "library"},
}
builtIn, err := compileSkippedPaths([]string{"^\\.git", "^bazel-*"})
if err != nil {
return nil, err
}
sp, err := compileSkippedPaths(cfg.SkippedPaths)
if err != nil {
return nil, err
}
sp = append(builtIn, sp...)
v.skippedPaths = sp
sop, err := compileSkippedPaths(cfg.SkippedOpenAPIGenPaths)
if err != nil {
return nil, err
}
v.skippedOpenAPIPaths = append(sop, sp...)
return &v, nil
}
type icacheKey struct {
path, srcDir string
}
type icacheVal struct {
pkg *build.Package
err error
}
func (v *Vendorer) importPkg(path string, srcDir string) (*build.Package, error) {
k := icacheKey{path: path, srcDir: srcDir}
if val, ok := v.icache[k]; ok {
return val.pkg, val.err
}
// cache miss
pkg, err := v.ctx.Import(path, srcDir, build.ImportComment)
v.icache[k] = icacheVal{pkg: pkg, err: err}
return pkg, err
}
func writeHeaders(file *bzl.File) {
pkgRule := bzl.Rule{
Call: &bzl.CallExpr{
X: &bzl.LiteralExpr{Token: "package"},
},
}
pkgRule.SetAttr("default_visibility", asExpr([]string{"//visibility:public"}))
file.Stmt = append(file.Stmt,
[]bzl.Expr{
pkgRule.Call,
&bzl.CallExpr{
X: &bzl.LiteralExpr{Token: "load"},
List: asExpr([]string{
"@io_bazel_rules_go//go:def.bzl",
}).(*bzl.ListExpr).List,
},
}...,
)
}
func writeRules(file *bzl.File, rules []*bzl.Rule) {
for _, rule := range rules {
file.Stmt = append(file.Stmt, rule.Call)
}
}
func (v *Vendorer) resolve(ipath string) Label {
if ipath == v.cfg.GoPrefix {
return Label{
tag: "go_default_library",
}
} else if strings.HasPrefix(ipath, v.cfg.GoPrefix) {
return Label{
pkg: strings.TrimPrefix(ipath, v.cfg.GoPrefix+"/"),
tag: "go_default_library",
}
}
if v.cfg.VendorMultipleBuildFiles {
return Label{
pkg: "vendor/" + ipath,
tag: "go_default_library",
}
}
return Label{
pkg: "vendor",
tag: ipath,
}
}
func (v *Vendorer) walk(root string, f func(path, ipath string, pkg *build.Package) error) error {
skipVendor := true
if root == vendorPath {
skipVendor = false
}
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
if skipVendor && strings.HasPrefix(path, vendorPath) {
return filepath.SkipDir
}
for _, r := range v.skippedPaths {
if r.MatchString(path) {
return filepath.SkipDir
}
}
ipath, err := filepath.Rel(root, path)
if err != nil {
return err
}
pkg, err := v.importPkg(".", filepath.Join(v.root, path))
if err != nil {
if _, ok := err.(*build.NoGoError); err != nil && ok {
return nil
}
return err
}
return f(path, ipath, pkg)
})
}
func (v *Vendorer) walkRepo() error {
for _, root := range v.cfg.SrcDirs {
if err := v.walk(root, v.updatePkg); err != nil {
return err
}
}
return nil
}
func (v *Vendorer) updateSinglePkg(path string) error {
pkg, err := v.importPkg(".", "./"+path)
if err != nil {
if _, ok := err.(*build.NoGoError); err != nil && ok {
return nil
}
return err
}
return v.updatePkg(path, "", pkg)
}
type ruleType int
// The RuleType* constants enumerate the bazel rules supported by this tool.
const (
RuleTypeGoBinary ruleType = iota
RuleTypeGoLibrary
RuleTypeGoTest
RuleTypeGoXTest
RuleTypeCGoGenrule
RuleTypeFileGroup
RuleTypeOpenAPILibrary
)
// RuleKind converts a value of the RuleType* enum into the BUILD string.
func (rt ruleType) RuleKind() string {
switch rt {
case RuleTypeGoBinary:
return "go_binary"
case RuleTypeGoLibrary:
return "go_library"
case RuleTypeGoTest:
return "go_test"
case RuleTypeGoXTest:
return "go_test"
case RuleTypeCGoGenrule:
return "cgo_genrule"
case RuleTypeFileGroup:
return "filegroup"
case RuleTypeOpenAPILibrary:
return "openapi_library"
}
panic("unreachable")
}
// NamerFunc is a function that returns the appropriate name for the rule for the provided RuleType.
type NamerFunc func(ruleType) string
func (v *Vendorer) updatePkg(path, _ string, pkg *build.Package) error {
srcNameMap := func(srcs ...[]string) *bzl.ListExpr {
return asExpr(merge(srcs...)).(*bzl.ListExpr)
}
srcs := srcNameMap(pkg.GoFiles, pkg.SFiles)
cgoSrcs := srcNameMap(pkg.CgoFiles, pkg.CFiles, pkg.CXXFiles, pkg.HFiles)
testSrcs := srcNameMap(pkg.TestGoFiles)
xtestSrcs := srcNameMap(pkg.XTestGoFiles)
v.addRules(path, v.emit(srcs, cgoSrcs, testSrcs, xtestSrcs, pkg, func(rt ruleType) string {
switch rt {
case RuleTypeGoBinary:
return filepath.Base(pkg.Dir)
case RuleTypeGoLibrary:
return "go_default_library"
case RuleTypeGoTest:
return "go_default_test"
case RuleTypeGoXTest:
return "go_default_xtest"
case RuleTypeCGoGenrule:
return "cgo_codegen"
}
panic("unreachable")
}))
return nil
}
func (v *Vendorer) emit(srcs, cgoSrcs, testSrcs, xtestSrcs *bzl.ListExpr, pkg *build.Package, namer NamerFunc) []*bzl.Rule {
var goLibAttrs = make(Attrs)
var rules []*bzl.Rule
deps := v.extractDeps(pkg.Imports)
if len(srcs.List) >= 0 {
goLibAttrs.Set("srcs", srcs)
} else if len(cgoSrcs.List) == 0 {
return nil
}
if len(deps.List) > 0 {
goLibAttrs.SetList("deps", deps)
}
if pkg.IsCommand() {
rules = append(rules, newRule(RuleTypeGoBinary, namer, map[string]bzl.Expr{
"library": asExpr(":" + namer(RuleTypeGoLibrary)),
}))
}
addGoDefaultLibrary := len(cgoSrcs.List) > 0 || len(srcs.List) > 0
if len(cgoSrcs.List) != 0 {
cgoRuleAttrs := make(Attrs)
cgoRuleAttrs.SetList("srcs", cgoSrcs)
cgoRuleAttrs.SetList("clinkopts", asExpr([]string{"-lz", "-lm", "-lpthread", "-ldl"}).(*bzl.ListExpr))
rules = append(rules, newRule(RuleTypeCGoGenrule, namer, cgoRuleAttrs))
goLibAttrs.Set("library", asExpr(":"+namer(RuleTypeCGoGenrule)))
}
if len(testSrcs.List) != 0 {
testRuleAttrs := make(Attrs)
testRuleAttrs.SetList("srcs", testSrcs)
testRuleAttrs.SetList("deps", v.extractDeps(pkg.TestImports))
if addGoDefaultLibrary {
testRuleAttrs.Set("library", asExpr(":"+namer(RuleTypeGoLibrary)))
}
rules = append(rules, newRule(RuleTypeGoTest, namer, testRuleAttrs))
}
if addGoDefaultLibrary {
rules = append(rules, newRule(RuleTypeGoLibrary, namer, goLibAttrs))
}
if len(xtestSrcs.List) != 0 {
xtestRuleAttrs := make(Attrs)
xtestRuleAttrs.SetList("srcs", xtestSrcs)
xtestRuleAttrs.SetList("deps", v.extractDeps(pkg.XTestImports))
rules = append(rules, newRule(RuleTypeGoXTest, namer, xtestRuleAttrs))
}
return rules
}
func (v *Vendorer) addRules(pkgPath string, rules []*bzl.Rule) {
cleanPath := filepath.Clean(pkgPath)
v.newRules[cleanPath] = append(v.newRules[cleanPath], rules...)
}
func (v *Vendorer) walkVendor() error {
var rules []*bzl.Rule
updateFunc := func(path, ipath string, pkg *build.Package) error {
srcNameMap := func(srcs ...[]string) *bzl.ListExpr {
return asExpr(
apply(
merge(srcs...),
mapper(func(s string) string {
return strings.TrimPrefix(filepath.Join(path, s), "vendor/")
}),
),
).(*bzl.ListExpr)
}
srcs := srcNameMap(pkg.GoFiles, pkg.SFiles)
cgoSrcs := srcNameMap(pkg.CgoFiles, pkg.CFiles, pkg.CXXFiles, pkg.HFiles)
testSrcs := srcNameMap(pkg.TestGoFiles)
xtestSrcs := srcNameMap(pkg.XTestGoFiles)
tagBase := v.resolve(ipath).tag
rules = append(rules, v.emit(srcs, cgoSrcs, testSrcs, xtestSrcs, pkg, func(rt ruleType) string {
switch rt {
case RuleTypeGoBinary:
return tagBase + "_bin"
case RuleTypeGoLibrary:
return tagBase
case RuleTypeGoTest:
return tagBase + "_test"
case RuleTypeGoXTest:
return tagBase + "_xtest"
case RuleTypeCGoGenrule:
return tagBase + "_cgo"
}
panic("unreachable")
})...)
return nil
}
if v.cfg.VendorMultipleBuildFiles {
updateFunc = v.updatePkg
}
if err := v.walk(vendorPath, updateFunc); err != nil {
return err
}
v.addRules(vendorPath, rules)
return nil
}
func (v *Vendorer) extractDeps(deps []string) *bzl.ListExpr {
return asExpr(
apply(
merge(deps),
filterer(func(s string) bool {
pkg, err := v.importPkg(s, v.root)
if err != nil {
if strings.Contains(err.Error(), `cannot find package "C"`) ||
// added in go1.7
strings.Contains(err.Error(), `cannot find package "context"`) ||
strings.Contains(err.Error(), `cannot find package "net/http/httptrace"`) {
return false
}
fmt.Fprintf(os.Stderr, "extract err: %v\n", err)
return false
}
if pkg.Goroot {
return false
}
return true
}),
mapper(func(s string) string {
return v.resolve(s).String()
}),
),
).(*bzl.ListExpr)
}
func (v *Vendorer) reconcileAllRules() (int, error) {
var paths []string
for path := range v.newRules {
paths = append(paths, path)
}
sort.Strings(paths)
written := 0
for _, path := range paths {
w, err := ReconcileRules(path, v.newRules[path], v.managedAttrs, v.dryRun, v.cfg.ManageGoRules)
if w {
written++
}
if err != nil {
return written, err
}
}
return written, nil
}
// Attrs collects the attributes for a rule.
type Attrs map[string]bzl.Expr
// Set sets the named attribute to the provided bazel expression.
func (a Attrs) Set(name string, expr bzl.Expr) {
a[name] = expr
}
// SetList sets the named attribute to the provided bazel expression list.
func (a Attrs) SetList(name string, expr *bzl.ListExpr) {
if len(expr.List) == 0 {
return
}
a[name] = expr
}
// Label defines a bazel label.
type Label struct {
pkg, tag string
}
func (l Label) String() string {
return fmt.Sprintf("//%v:%v", l.pkg, l.tag)
}
func asExpr(e interface{}) bzl.Expr {
rv := reflect.ValueOf(e)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return &bzl.LiteralExpr{Token: fmt.Sprintf("%d", e)}
case reflect.Float32, reflect.Float64:
return &bzl.LiteralExpr{Token: fmt.Sprintf("%f", e)}
case reflect.String:
return &bzl.StringExpr{Value: e.(string)}
case reflect.Slice, reflect.Array:
var list []bzl.Expr
for i := 0; i < rv.Len(); i++ {
list = append(list, asExpr(rv.Index(i).Interface()))
}
return &bzl.ListExpr{List: list}
default:
klog.Fatalf("Uh oh")
return nil
}
}
type sed func(s []string) []string
func mapString(in []string, f func(string) string) []string {
var out []string
for _, s := range in {
out = append(out, f(s))
}
return out
}
func mapper(f func(string) string) sed {
return func(in []string) []string {
return mapString(in, f)
}
}
func filterString(in []string, f func(string) bool) []string {
var out []string
for _, s := range in {
if f(s) {
out = append(out, s)
}
}
return out
}
func filterer(f func(string) bool) sed {
return func(in []string) []string {
return filterString(in, f)
}
}
func apply(stream []string, seds ...sed) []string {
for _, sed := range seds {
stream = sed(stream)
}
return stream
}
func merge(streams ...[]string) []string {
var out []string
for _, stream := range streams {
out = append(out, stream...)
}
return out
}
func newRule(rt ruleType, namer NamerFunc, attrs map[string]bzl.Expr) *bzl.Rule {
rule := &bzl.Rule{
Call: &bzl.CallExpr{
X: &bzl.LiteralExpr{Token: rt.RuleKind()},
},
}
rule.SetAttr("name", asExpr(namer(rt)))
for k, v := range attrs {
rule.SetAttr(k, v)
}
rule.SetAttr("tags", asExpr([]string{automanagedTag}))
return rule
}
// findBuildFile determines the name of a preexisting BUILD file, returning
// a default if no such file exists.
func findBuildFile(pkgPath string) (bool, string) {
options := []string{"BUILD.bazel", "BUILD"}
for _, b := range options {
path := filepath.Join(pkgPath, b)
info, err := os.Stat(path)
if err == nil && !info.IsDir() {
return true, path
}
}
return false, filepath.Join(pkgPath, "BUILD")
}
// ReconcileRules reconciles, simplifies, and writes the rules for the specified package, adding
// additional dependency rules as needed.
func ReconcileRules(pkgPath string, rules []*bzl.Rule, managedAttrs []string, dryRun bool, manageGoRules bool) (bool, error) {
_, path := findBuildFile(pkgPath)
info, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
f := &bzl.File{}
writeHeaders(f)
if manageGoRules {
reconcileLoad(f, rules)
}
writeRules(f, rules)
return writeFile(path, f, false, dryRun)
} else if err != nil {
return false, err
}
if info.IsDir() {
return false, fmt.Errorf("%q cannot be a directory", path)
}
b, err := ioutil.ReadFile(path)
if err != nil {
return false, err
}
f, err := bzl.Parse(path, b)
if err != nil {
return false, err
}
oldRules := make(map[string]*bzl.Rule)
for _, r := range f.Rules("") {
oldRules[r.Name()] = r
}
for _, r := range rules {
o, ok := oldRules[r.Name()]
if !ok {
f.Stmt = append(f.Stmt, r.Call)
continue
}
if !RuleIsManaged(o, manageGoRules) {
continue
}
reconcileAttr := func(o, n *bzl.Rule, name string) {
if e := n.Attr(name); e != nil {
o.SetAttr(name, e)
} else {
o.DelAttr(name)
}
}
for _, attr := range managedAttrs {
reconcileAttr(o, r, attr)
}
delete(oldRules, r.Name())
}
for _, r := range oldRules {
if !RuleIsManaged(r, manageGoRules) {
continue
}
f.DelRules(r.Kind(), r.Name())
}
if manageGoRules {
reconcileLoad(f, f.Rules(""))
}
return writeFile(path, f, true, dryRun)
}
func reconcileLoad(f *bzl.File, rules []*bzl.Rule) {
usedRuleKindsMap := map[string]bool{}
for _, r := range rules {
// Select only the Go rules we need to import, excluding builtins like filegroup.
// TODO: make less fragile
switch r.Kind() {
case "go_prefix", "go_library", "go_binary", "go_test", "go_proto_library", "cgo_genrule", "cgo_library":
usedRuleKindsMap[r.Kind()] = true
}
}
usedRuleKindsList := []string{}
for k := range usedRuleKindsMap {
usedRuleKindsList = append(usedRuleKindsList, k)
}
sort.Strings(usedRuleKindsList)
for _, r := range f.Rules("load") {
const goRulesLabel = "@io_bazel_rules_go//go:def.bzl"
args := bzl.Strings(&bzl.ListExpr{List: r.Call.List})
if len(args) == 0 {
continue
}
if args[0] != goRulesLabel {
continue
}
if len(usedRuleKindsList) == 0 {
f.DelRules(r.Kind(), r.Name())
continue
}
r.Call.List = asExpr(append(
[]string{goRulesLabel}, usedRuleKindsList...,
)).(*bzl.ListExpr).List
break
}
}
// RuleIsManaged returns whether the provided rule is managed by this tool,
// based on the tags set on the rule.
func RuleIsManaged(r *bzl.Rule, manageGoRules bool) bool {
var automanaged bool
if !manageGoRules && (strings.HasPrefix(r.Kind(), "go_") || strings.HasPrefix(r.Kind(), "cgo_")) {
return false
}
for _, tag := range r.AttrStrings("tags") {
if tag == automanagedTag {
automanaged = true
break
}
}
return automanaged
}
func writeFile(path string, f *bzl.File, exists, dryRun bool) (bool, error) {
var info bzl.RewriteInfo
bzl.Rewrite(f, &info)
out := bzl.Format(f)
if exists {
orig, err := ioutil.ReadFile(path)
if err != nil {
return false, err
}
if bytes.Compare(orig, out) == 0 {
return false, nil
}
if *printDiff {
Diff(orig, out)
}
}
if dryRun {
fmt.Fprintf(os.Stderr, "DRY-RUN: wrote %q\n", path)
return true, nil
}
werr := ioutil.WriteFile(path, out, 0644)
if werr == nil {
fmt.Fprintf(os.Stderr, "wrote %q\n", path)
}
return werr == nil, werr
}
func context() *build.Context {
return &build.Context{
GOARCH: "amd64",
GOOS: "linux",
GOROOT: build.Default.GOROOT,
GOPATH: build.Default.GOPATH,
ReleaseTags: []string{"go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6", "go1.7", "go1.8"},
Compiler: runtime.Compiler,
CgoEnabled: true,
}
}
func walk(root string, walkFn filepath.WalkFunc) error {
return nil
}
func compileSkippedPaths(skippedPaths []string) ([]*regexp.Regexp, error) {
regexPaths := []*regexp.Regexp{}
for _, sp := range skippedPaths {
r, err := regexp.Compile(sp)
if err != nil {
return nil, err
}
regexPaths = append(regexPaths, r)
}
return regexPaths, nil
}

View File

@ -9,8 +9,8 @@ go_library(
"kazel.go",
"sourcerer.go",
],
importmap = "k8s.io/kubernetes/vendor/github.com/kubernetes/repo-infra/kazel",
importpath = "github.com/kubernetes/repo-infra/kazel",
importmap = "k8s.io/kubernetes/vendor/k8s.io/repo-infra/kazel",
importpath = "k8s.io/repo-infra/kazel",
visibility = ["//visibility:private"],
deps = [
"//vendor/github.com/bazelbuild/buildtools/build:go_default_library",

View File

@ -28,19 +28,25 @@ type Cfg struct {
SrcDirs []string
// regexps that match packages to skip
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.
SkippedOpenAPIGenPaths []string
SkippedK8sCodegenPaths []string
// 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
AddSourcesRules bool
// whether to have multiple build files in vendor/ or just one.
VendorMultipleBuildFiles bool
// whether to manage kubernetes' pkg/generated/openapi.
K8sOpenAPIGen bool
// Whether to manage the upstream Go rules provided by bazelbuild/rules_go.
// If using gazelle, set this to false (or omit).
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.

178
vendor/k8s.io/repo-infra/kazel/generator.go generated vendored Normal file
View File

@ -0,0 +1,178 @@
/*
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)
}

389
vendor/k8s.io/repo-infra/kazel/kazel.go generated vendored Normal file
View File

@ -0,0 +1,389 @@
/*
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 (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"regexp"
"sort"
"github.com/bazelbuild/buildtools/build"
"k8s.io/klog"
)
const (
automanagedTag = "automanaged"
)
var (
root = flag.String("root", ".", "root of go source")
dryRun = flag.Bool("dry-run", false, "run in dry mode")
printDiff = flag.Bool("print-diff", false, "print diff to stdout")
validate = flag.Bool("validate", false, "run in dry mode and exit nonzero if any BUILD files need to be updated")
cfgPath = flag.String("cfg-path", ".kazelcfg.json", "path to kazel config (relative paths interpreted relative to -repo.")
)
func main() {
flag.Parse()
flag.Set("alsologtostderr", "true")
if *root == "" {
klog.Fatalf("-root argument is required")
}
if *validate {
*dryRun = true
}
v, err := newVendorer(*root, *cfgPath, *dryRun)
if err != nil {
klog.Fatalf("unable to build vendorer: %v", err)
}
if err = os.Chdir(v.root); err != nil {
klog.Fatalf("cannot chdir into root %q: %v", v.root, err)
}
if v.cfg.ManageGoRules {
klog.Fatalf("kazel no longer supports managing Go rules")
}
wroteGenerated := false
if wroteGenerated, err = v.walkGenerated(); err != nil {
klog.Fatalf("err walking generated: %v", err)
}
if _, err = v.walkSource("."); err != nil {
klog.Fatalf("err walking source: %v", err)
}
written := 0
if written, err = v.reconcileAllRules(); err != nil {
klog.Fatalf("err reconciling rules: %v", err)
}
if wroteGenerated {
written++
}
if *validate && written > 0 {
fmt.Fprintf(os.Stderr, "\n%d BUILD files not up-to-date.\n", written)
os.Exit(1)
}
}
// Vendorer collects context, configuration, and cache while walking the tree.
type Vendorer struct {
skippedPaths []*regexp.Regexp
skippedK8sCodegenPaths []*regexp.Regexp
dryRun bool
root string
cfg *Cfg
newRules map[string][]*build.Rule // package path -> list of rules to add or update
managedAttrs []string // which rule attributes kazel will overwrite
}
func newVendorer(root, cfgPath string, dryRun bool) (*Vendorer, error) {
absRoot, err := filepath.Abs(root)
if err != nil {
return nil, fmt.Errorf("could not get absolute path: %v", err)
}
if !filepath.IsAbs(cfgPath) {
cfgPath = filepath.Join(absRoot, cfgPath)
}
cfg, err := ReadCfg(cfgPath)
if err != nil {
return nil, err
}
v := Vendorer{
dryRun: dryRun,
root: absRoot,
cfg: cfg,
newRules: make(map[string][]*build.Rule),
managedAttrs: []string{"srcs"},
}
builtIn, err := compileSkippedPaths([]string{"^\\.git", "^bazel-*"})
if err != nil {
return nil, err
}
sp, err := compileSkippedPaths(cfg.SkippedPaths)
if err != nil {
return nil, err
}
sp = append(builtIn, sp...)
v.skippedPaths = sp
sop, err := compileSkippedPaths(cfg.SkippedK8sCodegenPaths)
if err != nil {
return nil, err
}
v.skippedK8sCodegenPaths = append(sop, sp...)
return &v, nil
}
func writeRules(file *build.File, rules []*build.Rule) {
for _, rule := range rules {
file.Stmt = append(file.Stmt, rule.Call)
}
}
func (v *Vendorer) addRules(pkgPath string, rules []*build.Rule) {
cleanPath := filepath.Clean(pkgPath)
v.newRules[cleanPath] = append(v.newRules[cleanPath], rules...)
}
func (v *Vendorer) reconcileAllRules() (int, error) {
var paths []string
for path := range v.newRules {
paths = append(paths, path)
}
sort.Strings(paths)
written := 0
for _, path := range paths {
w, err := ReconcileRules(path, v.newRules[path], v.managedAttrs, v.dryRun)
if w {
written++
}
if err != nil {
return written, err
}
}
return written, nil
}
// addCommentBefore adds a whole-line comment before the provided Expr.
func addCommentBefore(e build.Expr, comment string) {
c := e.Comment()
c.Before = append(c.Before, build.Comment{Token: fmt.Sprintf("# %s", comment)})
}
// varExpr creates a variable expression of the form "name = expr".
// v will be converted into an appropriate Expr using asExpr.
// The optional description will be included as a comment before the expression.
func varExpr(name, desc string, v interface{}) build.Expr {
e := &build.BinaryExpr{
X: &build.LiteralExpr{Token: name},
Op: "=",
Y: asExpr(v),
}
if desc != "" {
addCommentBefore(e, desc)
}
return e
}
// rvSliceLessFunc returns a function that can be used with sort.Slice() or sort.SliceStable()
// to sort a slice of reflect.Values.
// It sorts ints and floats as their native kinds, and everything else as a string.
func rvSliceLessFunc(k reflect.Kind, vs []reflect.Value) func(int, int) bool {
switch k {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return func(i, j int) bool { return vs[i].Int() < vs[j].Int() }
case reflect.Float32, reflect.Float64:
return func(i, j int) bool { return vs[i].Float() < vs[j].Float() }
default:
return func(i, j int) bool {
return fmt.Sprintf("%v", vs[i]) < fmt.Sprintf("%v", vs[j])
}
}
}
// asExpr converts a native Go type into the equivalent Starlark expression using reflection.
// The keys of maps will be sorted for reproducibility.
func asExpr(e interface{}) build.Expr {
rv := reflect.ValueOf(e)
switch rv.Kind() {
case reflect.Bool:
return &build.LiteralExpr{Token: fmt.Sprintf("%t", e)}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return &build.LiteralExpr{Token: fmt.Sprintf("%d", e)}
case reflect.Float32, reflect.Float64:
return &build.LiteralExpr{Token: fmt.Sprintf("%g", e)}
case reflect.String:
return &build.StringExpr{Value: e.(string)}
case reflect.Slice, reflect.Array:
var list []build.Expr
for i := 0; i < rv.Len(); i++ {
list = append(list, asExpr(rv.Index(i).Interface()))
}
return &build.ListExpr{List: list}
case reflect.Map:
var list []build.Expr
keys := rv.MapKeys()
sort.SliceStable(keys, rvSliceLessFunc(rv.Type().Key().Kind(), keys))
for _, key := range keys {
list = append(list, &build.KeyValueExpr{
Key: asExpr(key.Interface()),
Value: asExpr(rv.MapIndex(key).Interface()),
})
}
return &build.DictExpr{List: list}
default:
klog.Fatalf("unhandled kind: %q for value: %q", rv.Kind(), rv)
return nil
}
}
func newRule(rt, name string, attrs map[string]build.Expr) *build.Rule {
rule := &build.Rule{
Call: &build.CallExpr{
X: &build.LiteralExpr{Token: rt},
},
}
rule.SetAttr("name", asExpr(name))
for k, v := range attrs {
rule.SetAttr(k, v)
}
rule.SetAttr("tags", asExpr([]string{automanagedTag}))
return rule
}
// findBuildFile determines the name of a preexisting BUILD file, returning
// a default if no such file exists.
func findBuildFile(pkgPath string) (bool, string) {
options := []string{"BUILD.bazel", "BUILD"}
for _, b := range options {
path := filepath.Join(pkgPath, b)
info, err := os.Stat(path)
if err == nil && !info.IsDir() {
return true, path
}
}
return false, filepath.Join(pkgPath, "BUILD.bazel")
}
// ReconcileRules reconciles, simplifies, and writes the rules for the specified package, adding
// additional dependency rules as needed.
func ReconcileRules(pkgPath string, rules []*build.Rule, managedAttrs []string, dryRun bool) (bool, error) {
_, path := findBuildFile(pkgPath)
info, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
f := &build.File{}
writeRules(f, rules)
return writeFile(path, f, nil, false, dryRun)
} else if err != nil {
return false, err
}
if info.IsDir() {
return false, fmt.Errorf("%q cannot be a directory", path)
}
b, err := ioutil.ReadFile(path)
if err != nil {
return false, err
}
f, err := build.Parse(path, b)
if err != nil {
return false, err
}
oldRules := make(map[string]*build.Rule)
for _, r := range f.Rules("") {
oldRules[r.Name()] = r
}
for _, r := range rules {
o, ok := oldRules[r.Name()]
if !ok {
f.Stmt = append(f.Stmt, r.Call)
continue
}
if !RuleIsManaged(o) {
continue
}
reconcileAttr := func(o, n *build.Rule, name string) {
if e := n.Attr(name); e != nil {
o.SetAttr(name, e)
} else {
o.DelAttr(name)
}
}
for _, attr := range managedAttrs {
reconcileAttr(o, r, attr)
}
delete(oldRules, r.Name())
}
for _, r := range oldRules {
if !RuleIsManaged(r) {
continue
}
f.DelRules(r.Kind(), r.Name())
}
return writeFile(path, f, nil, true, dryRun)
}
// RuleIsManaged returns whether the provided rule is managed by this tool,
// based on the tags set on the rule.
func RuleIsManaged(r *build.Rule) bool {
for _, tag := range r.AttrStrings("tags") {
if tag == automanagedTag {
return true
}
}
return false
}
// writeFile writes out f to path, prepending boilerplate to the output.
// If exists is true, compares against the existing file specified by path,
// returning false if there are no changes.
// Otherwise, returns true.
// If dryRun is false, no files are actually changed; otherwise, the file will be written.
func writeFile(path string, f *build.File, boilerplate []byte, exists, dryRun bool) (bool, error) {
var info build.RewriteInfo
build.Rewrite(f, &info)
var out []byte
out = append(out, boilerplate...)
out = append(out, build.Format(f)...)
if exists {
orig, err := ioutil.ReadFile(path)
if err != nil {
return false, err
}
if bytes.Compare(orig, out) == 0 {
return false, nil
}
if *printDiff {
Diff(orig, out)
}
}
if dryRun {
fmt.Fprintf(os.Stderr, "DRY-RUN: wrote %q\n", path)
return true, nil
}
werr := ioutil.WriteFile(path, out, 0644)
if werr == nil {
fmt.Fprintf(os.Stderr, "wrote %q\n", path)
}
return werr == nil, werr
}
func compileSkippedPaths(skippedPaths []string) ([]*regexp.Regexp, error) {
regexPaths := []*regexp.Regexp{}
for _, sp := range skippedPaths {
r, err := regexp.Compile(sp)
if err != nil {
return nil, err
}
regexPaths = append(regexPaths, r)
}
return regexPaths, nil
}

View File

@ -21,7 +21,7 @@ import (
"io/ioutil"
"path/filepath"
bzl "github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/buildtools/build"
)
const (
@ -85,21 +85,21 @@ func (v *Vendorer) walkSource(pkgPath string) ([]string, error) {
return nil, nil
}
pkgSrcsExpr := &bzl.LiteralExpr{Token: `glob(["**"])`}
pkgSrcsExpr := &build.LiteralExpr{Token: `glob(["**"])`}
if pkgPath == "." {
pkgSrcsExpr = &bzl.LiteralExpr{Token: `glob(["**"], exclude=["bazel-*/**", ".git/**"])`}
pkgSrcsExpr = &build.LiteralExpr{Token: `glob(["**"], exclude=["bazel-*/**", ".git/**"])`}
}
v.addRules(pkgPath, []*bzl.Rule{
newRule(RuleTypeFileGroup,
func(_ ruleType) string { return pkgSrcsTarget },
map[string]bzl.Expr{
v.addRules(pkgPath, []*build.Rule{
newRule("filegroup",
pkgSrcsTarget,
map[string]build.Expr{
"srcs": pkgSrcsExpr,
"visibility": asExpr([]string{"//visibility:private"}),
}),
newRule(RuleTypeFileGroup,
func(_ ruleType) string { return allSrcsTarget },
map[string]bzl.Expr{
newRule("filegroup",
allSrcsTarget,
map[string]build.Expr{
"srcs": asExpr(append(children, fmt.Sprintf(":%s", pkgSrcsTarget))),
// TODO: should this be more restricted?
"visibility": asExpr([]string{"//visibility:public"}),