Skip to content

Commit 695e59c

Browse files
authored
Merge pull request kubernetes#130981 from thockin/kk_hack_verify_api_lint
Add a verify script for codegen lint
2 parents 8cdc67c + 252d584 commit 695e59c

File tree

7 files changed

+297
-107
lines changed

7 files changed

+297
-107
lines changed

hack/update-codegen.sh

Lines changed: 120 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ fi
108108
# Some of the later codegens depend on the results of this, so it needs to come
109109
# first in the case of regenerating everything.
110110
function codegen::protobuf() {
111+
if [[ -n "${LINT:-}" ]]; then
112+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
113+
kube::log::status "No linter for protobuf codegen"
114+
fi
115+
return
116+
fi
117+
111118
# NOTE: All output from this script needs to be copied back to the calling
112119
# source tree. This is managed in kube::build::copy_output in build/common.sh.
113120
# If the output set is changed update that function.
@@ -122,7 +129,7 @@ function codegen::protobuf() {
122129
| sed 's|^|k8s.io/kubernetes/|;s|k8s.io/kubernetes/staging/src/||' \
123130
| sort -u)
124131

125-
kube::log::status "Generating protobufs for ${#apis[@]} targets"
132+
kube::log::status "protobufs: ${#apis[@]} targets"
126133
if [[ "${DBG_CODEGEN}" == 1 ]]; then
127134
kube::log::status "DBG: generating protobufs for:"
128135
for dir in "${apis[@]}"; do
@@ -154,6 +161,13 @@ function codegen::protobuf() {
154161
# register: generate deep-copy functions and register them with a
155162
# scheme
156163
function codegen::deepcopy() {
164+
if [[ -n "${LINT:-}" ]]; then
165+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
166+
kube::log::status "No linter for deepcopy codegen"
167+
fi
168+
return
169+
fi
170+
157171
# Build the tool.
158172
GOPROXY=off go install \
159173
k8s.io/code-generator/cmd/deepcopy-gen
@@ -179,7 +193,7 @@ function codegen::deepcopy() {
179193
tag_pkgs+=("./$dir")
180194
done
181195

182-
kube::log::status "Generating deepcopy code for ${#tag_pkgs[@]} targets"
196+
kube::log::status "deepcopy: ${#tag_pkgs[@]} targets"
183197
if [[ "${DBG_CODEGEN}" == 1 ]]; then
184198
kube::log::status "DBG: running deepcopy-gen for:"
185199
for dir in "${tag_dirs[@]}"; do
@@ -255,14 +269,21 @@ EOF
255269
# Some of the later codegens depend on the results of this, so it needs to come
256270
# first in the case of regenerating everything.
257271
function codegen::swagger() {
272+
if [[ -n "${LINT:-}" ]]; then
273+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
274+
kube::log::status "No linter for swagger codegen"
275+
fi
276+
return
277+
fi
278+
258279
# Build the tool
259280
GOPROXY=off go install \
260281
./cmd/genswaggertypedocs
261282

262283
local group_versions=()
263284
IFS=" " read -r -a group_versions <<< "meta/v1 meta/v1beta1 ${KUBE_AVAILABLE_GROUP_VERSIONS}"
264285

265-
kube::log::status "Generating swagger for ${#group_versions[@]} targets"
286+
kube::log::status "swagger: ${#group_versions[@]} targets"
266287

267288
git_find -z ':(glob)**/types_swagger_doc_generated.go' | xargs -0 rm -f
268289

@@ -278,6 +299,13 @@ function codegen::swagger() {
278299
# comment-tag in column 0 of one file of the form:
279300
# // +k8s:prerelease-lifecycle-gen=true
280301
function codegen::prerelease() {
302+
if [[ -n "${LINT:-}" ]]; then
303+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
304+
kube::log::status "No linter for prerelease codegen"
305+
fi
306+
return
307+
fi
308+
281309
# Build the tool.
282310
GOPROXY=off go install \
283311
k8s.io/code-generator/cmd/prerelease-lifecycle-gen
@@ -303,7 +331,7 @@ function codegen::prerelease() {
303331
tag_pkgs+=("./$dir")
304332
done
305333

306-
kube::log::status "Generating prerelease-lifecycle code for ${#tag_pkgs[@]} targets"
334+
kube::log::status "prerelease-lifecycle: ${#tag_pkgs[@]} targets"
307335
if [[ "${DBG_CODEGEN}" == 1 ]]; then
308336
kube::log::status "DBG: running prerelease-lifecycle-gen for:"
309337
for dir in "${tag_dirs[@]}"; do
@@ -342,6 +370,13 @@ function codegen::prerelease() {
342370
# FIELDNAME: any object with a field of this name is a candidate
343371
# for having a defaulter generated
344372
function codegen::defaults() {
373+
if [[ -n "${LINT:-}" ]]; then
374+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
375+
kube::log::status "No linter for defaults codegen"
376+
fi
377+
return
378+
fi
379+
345380
# Build the tool.
346381
GOPROXY=off go install \
347382
k8s.io/code-generator/cmd/defaulter-gen
@@ -367,7 +402,7 @@ function codegen::defaults() {
367402
tag_pkgs+=("./$dir")
368403
done
369404

370-
kube::log::status "Generating defaulter code for ${#tag_pkgs[@]} targets"
405+
kube::log::status "defaults: ${#tag_pkgs[@]} targets"
371406
if [[ "${DBG_CODEGEN}" == 1 ]]; then
372407
kube::log::status "DBG: running defaulter-gen for:"
373408
for dir in "${tag_dirs[@]}"; do
@@ -443,21 +478,27 @@ function codegen::validation() {
443478
time
444479
)
445480

446-
kube::log::status "Generating validation code for ${#tag_pkgs[@]} targets"
481+
kube::log::status "validation: ${#tag_pkgs[@]} targets"
447482
if [[ "${DBG_CODEGEN}" == 1 ]]; then
448483
kube::log::status "DBG: running validation-gen for:"
449484
for dir in "${tag_dirs[@]}"; do
450485
kube::log::status "DBG: $dir"
451486
done
452487
fi
453488

454-
git_find -z ':(glob)**'/"${output_file}" | xargs -0 rm -f
489+
local lint_flag=() # empty arrays expand to no-value (as opposed to "")
490+
if [[ -n "${LINT:-}" ]]; then
491+
lint_flag+=("--lint")
492+
else
493+
git_find -z ':(glob)**'/"${output_file}" | xargs -0 rm -f
494+
fi
455495

456496
validation-gen \
457497
-v "${KUBE_VERBOSE}" \
458498
--go-header-file "${BOILERPLATE_FILENAME}" \
459499
--output-file "${output_file}" \
460500
$(printf -- " --readonly-pkg %s" "${readonly_pkgs[@]}") \
501+
"${lint_flag[@]}" `# may expand to nothing` \
461502
"${tag_pkgs[@]}" \
462503
"$@"
463504

@@ -488,6 +529,13 @@ function codegen::validation() {
488529
# TODO: it might be better in the long term to make peer-types explicit in the
489530
# IDL.
490531
function codegen::conversions() {
532+
if [[ -n "${LINT:-}" ]]; then
533+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
534+
kube::log::status "No linter for conversions codegen"
535+
fi
536+
return
537+
fi
538+
491539
# Build the tool.
492540
GOPROXY=off go install \
493541
k8s.io/code-generator/cmd/conversion-gen
@@ -519,7 +567,7 @@ function codegen::conversions() {
519567
k8s.io/api/core/v1
520568
)
521569

522-
kube::log::status "Generating conversion code for ${#tag_pkgs[@]} targets"
570+
kube::log::status "conversion: ${#tag_pkgs[@]} targets"
523571
if [[ "${DBG_CODEGEN}" == 1 ]]; then
524572
kube::log::status "DBG: running conversion-gen for:"
525573
for dir in "${tag_dirs[@]}"; do
@@ -549,6 +597,13 @@ function codegen::conversions() {
549597
# // +k8s:register-gen=package
550598
#
551599
function codegen::register() {
600+
if [[ -n "${LINT:-}" ]]; then
601+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
602+
kube::log::status "No linter for register codegen"
603+
fi
604+
return
605+
fi
606+
552607
# Build the tool.
553608
GOPROXY=off go install \
554609
k8s.io/code-generator/cmd/register-gen
@@ -574,7 +629,7 @@ function codegen::register() {
574629
tag_pkgs+=("./$dir")
575630
done
576631

577-
kube::log::status "Generating register code for ${#tag_pkgs[@]} targets"
632+
kube::log::status "register: ${#tag_pkgs[@]} targets"
578633
if [[ "${DBG_CODEGEN}" == 1 ]]; then
579634
kube::log::status "DBG: running register-gen for:"
580635
for dir in "${tag_dirs[@]}"; do
@@ -620,6 +675,13 @@ function k8s_tag_files_except() {
620675
# comment-tag in column 0 of one file of the form:
621676
# // +k8s:openapi-gen=true
622677
function codegen::openapi() {
678+
if [[ -n "${LINT:-}" ]]; then
679+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
680+
kube::log::status "No linter for openapi codegen"
681+
fi
682+
return
683+
fi
684+
623685
# Build the tool.
624686
GOPROXY=off go install \
625687
k8s.io/kube-openapi/cmd/openapi-gen
@@ -664,7 +726,7 @@ function codegen::openapi() {
664726
tag_pkgs+=("./$dir")
665727
done
666728

667-
kube::log::status "Generating openapi code"
729+
kube::log::status "openapi: ${#tag_pkgs[@]} targets"
668730
if [[ "${DBG_CODEGEN}" == 1 ]]; then
669731
kube::log::status "DBG: running openapi-gen for:"
670732
for dir in "${tag_dirs[@]}"; do
@@ -698,6 +760,13 @@ function codegen::openapi() {
698760
}
699761

700762
function codegen::applyconfigs() {
763+
if [[ -n "${LINT:-}" ]]; then
764+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
765+
kube::log::status "No linter for applyconfigs codegen"
766+
fi
767+
return
768+
fi
769+
701770
GOPROXY=off go install \
702771
k8s.io/kubernetes/pkg/generated/openapi/cmd/models-schema \
703772
k8s.io/code-generator/cmd/applyconfiguration-gen
@@ -710,7 +779,7 @@ function codegen::applyconfigs() {
710779
| sort -u)
711780
ext_apis+=("k8s.io/apimachinery/pkg/apis/meta/v1")
712781

713-
kube::log::status "Generating apply-config code for ${#ext_apis[@]} targets"
782+
kube::log::status "apply-config: ${#ext_apis[@]} targets"
714783
if [[ "${DBG_CODEGEN}" == 1 ]]; then
715784
kube::log::status "DBG: running applyconfiguration-gen for:"
716785
for api in "${ext_apis[@]}"; do
@@ -740,6 +809,13 @@ function codegen::applyconfigs() {
740809
}
741810

742811
function codegen::clients() {
812+
if [[ -n "${LINT:-}" ]]; then
813+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
814+
kube::log::status "No linter for clients codegen"
815+
fi
816+
return
817+
fi
818+
743819
GOPROXY=off go install \
744820
k8s.io/code-generator/cmd/client-gen
745821

@@ -761,7 +837,7 @@ function codegen::clients() {
761837
gv_dirs+=("${pkg_dir}")
762838
done
763839

764-
kube::log::status "Generating client code for ${#gv_dirs[@]} targets"
840+
kube::log::status "clients: ${#gv_dirs[@]} targets"
765841
if [[ "${DBG_CODEGEN}" == 1 ]]; then
766842
kube::log::status "DBG: running client-gen for:"
767843
for dir in "${gv_dirs[@]}"; do
@@ -795,6 +871,13 @@ function codegen::clients() {
795871
}
796872

797873
function codegen::listers() {
874+
if [[ -n "${LINT:-}" ]]; then
875+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
876+
kube::log::status "No linter for listers codegen"
877+
fi
878+
return
879+
fi
880+
798881
GOPROXY=off go install \
799882
k8s.io/code-generator/cmd/lister-gen
800883

@@ -805,7 +888,7 @@ function codegen::listers() {
805888
| while read -r -d $'\0' F; do dirname "${F}"; done \
806889
| sort -u)
807890

808-
kube::log::status "Generating lister code for ${#ext_apis[@]} targets"
891+
kube::log::status "listers: ${#ext_apis[@]} targets"
809892
if [[ "${DBG_CODEGEN}" == 1 ]]; then
810893
kube::log::status "DBG: running lister-gen for:"
811894
for api in "${ext_apis[@]}"; do
@@ -835,6 +918,13 @@ function codegen::listers() {
835918
}
836919

837920
function codegen::informers() {
921+
if [[ -n "${LINT:-}" ]]; then
922+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
923+
kube::log::status "No linter for informers codegen"
924+
fi
925+
return
926+
fi
927+
838928
GOPROXY=off go install \
839929
k8s.io/code-generator/cmd/informer-gen
840930

@@ -845,7 +935,7 @@ function codegen::informers() {
845935
| while read -r -d $'\0' F; do dirname "${F}"; done \
846936
| sort -u)
847937

848-
kube::log::status "Generating informer code for ${#ext_apis[@]} targets"
938+
kube::log::status "informers: code for ${#ext_apis[@]} targets"
849939
if [[ "${DBG_CODEGEN}" == 1 ]]; then
850940
kube::log::status "DBG: running informer-gen for:"
851941
for api in "${ext_apis[@]}"; do
@@ -884,6 +974,13 @@ function indent() {
884974
}
885975

886976
function codegen::subprojects() {
977+
if [[ -n "${LINT:-}" ]]; then
978+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
979+
kube::log::status "No linter for subprojects codegen"
980+
fi
981+
return
982+
fi
983+
887984
# Call generation on sub-projects.
888985
local subs=(
889986
staging/src/k8s.io/code-generator/examples
@@ -898,7 +995,7 @@ function codegen::subprojects() {
898995
local codegen
899996
codegen="${KUBE_ROOT}/staging/src/k8s.io/code-generator"
900997
for sub in "${subs[@]}"; do
901-
kube::log::status "Generating code for subproject ${sub}"
998+
kube::log::status "subproject ${sub}:"
902999
pushd "${sub}" >/dev/null
9031000
CODEGEN_PKG="${codegen}" \
9041001
UPDATE_API_KNOWN_VIOLATIONS="${UPDATE_API_KNOWN_VIOLATIONS}" \
@@ -909,6 +1006,13 @@ function codegen::subprojects() {
9091006
}
9101007

9111008
function codegen::protobindings() {
1009+
if [[ -n "${LINT:-}" ]]; then
1010+
if [[ "${KUBE_VERBOSE}" -gt 2 ]]; then
1011+
kube::log::status "No linter for protobindings codegen"
1012+
fi
1013+
return
1014+
fi
1015+
9121016
# Each element of this array is a directory containing subdirectories which
9131017
# eventually contain a file named "api.proto".
9141018
local apis=(
@@ -929,7 +1033,7 @@ function codegen::protobindings() {
9291033
"staging/src/k8s.io/externaljwt/apis"
9301034
)
9311035

932-
kube::log::status "Generating protobuf bindings for ${#apis[@]} targets"
1036+
kube::log::status "protobuf bindings: ${#apis[@]} targets"
9331037
if [[ "${DBG_CODEGEN}" == 1 ]]; then
9341038
kube::log::status "DBG: generating protobuf bindings for:"
9351039
for dir in "${apis[@]}"; do

hack/verify-api-lint.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2024 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# This script runs API lint tools.
18+
#
19+
# Usage: `hack/verify-api-lint.sh`.
20+
21+
set -o errexit
22+
set -o nounset
23+
set -o pipefail
24+
25+
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
26+
source "${KUBE_ROOT}/hack/lib/init.sh"
27+
28+
cd "${KUBE_ROOT}"
29+
30+
LINT=true hack/update-codegen.sh

0 commit comments

Comments
 (0)