From 93b2845bec77f3162a3893be84c57465fdb1509a Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Sun, 12 Mar 2023 18:17:58 -0700 Subject: [PATCH 1/3] Codegen: subprojects openapi Use the "subprojects" aspect of update-codegen to generat openapi for the subprojects. Next we can simplify and remove the generic support. apiextensions-apiserver seems like it was ALWAYS broken: k8s.io/apiextensions/ doesn't exist, but k8s.io/apiextensions-apiserver does. Fixing that causes different openapi results, obviously. --- hack/update-codegen.sh | 2 +- .../k8s.io/apiextensions-apiserver/hack/update-codegen.sh | 7 +++++++ staging/src/k8s.io/kube-aggregator/hack/update-codegen.sh | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/hack/update-codegen.sh b/hack/update-codegen.sh index e5cfef5634d..30ede5fea8a 100755 --- a/hack/update-codegen.sh +++ b/hack/update-codegen.sh @@ -898,8 +898,8 @@ function codegen::subprojects() { vendor/k8s.io/kube-aggregator vendor/k8s.io/sample-apiserver vendor/k8s.io/sample-controller - vendor/k8s.io/apiextensions-apiserver vendor/k8s.io/metrics + vendor/k8s.io/apiextensions-apiserver vendor/k8s.io/apiextensions-apiserver/examples/client-go ) diff --git a/staging/src/k8s.io/apiextensions-apiserver/hack/update-codegen.sh b/staging/src/k8s.io/apiextensions-apiserver/hack/update-codegen.sh index 44aca6f1ec2..a532fff837a 100755 --- a/staging/src/k8s.io/apiextensions-apiserver/hack/update-codegen.sh +++ b/staging/src/k8s.io/apiextensions-apiserver/hack/update-codegen.sh @@ -28,6 +28,13 @@ kube::codegen::gen_helpers \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" +kube::codegen::gen_openapi \ + --input-pkg-root k8s.io/apiextensions-apiserver/pkg \ + --extra-pkgs k8s.io/api/autoscaling/v1 `# needed for Scale type` \ + --output-pkg-root k8s.io/apiextensions-apiserver/pkg/generated \ + --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ + --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" + kube::codegen::gen_client \ --with-watch \ --input-pkg-root k8s.io/apiextensions-apiserver/pkg/apis \ diff --git a/staging/src/k8s.io/kube-aggregator/hack/update-codegen.sh b/staging/src/k8s.io/kube-aggregator/hack/update-codegen.sh index 85815696fbe..8e77aa5f25b 100755 --- a/staging/src/k8s.io/kube-aggregator/hack/update-codegen.sh +++ b/staging/src/k8s.io/kube-aggregator/hack/update-codegen.sh @@ -28,6 +28,12 @@ kube::codegen::gen_helpers \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" +kube::codegen::gen_openapi \ + --input-pkg-root k8s.io/kube-aggregator/pkg/apis \ + --output-pkg-root k8s.io/kube-aggregator/pkg/generated \ + --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ + --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" + kube::codegen::gen_client \ --with-watch \ --input-pkg-root k8s.io/kube-aggregator/pkg/apis \ From ef796dca9bb8cf58de1051d7ad5108b8a30ec9c7 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Sun, 12 Mar 2023 21:07:02 -0700 Subject: [PATCH 2/3] Simpler openapi gen - subprojects do themselves This involves moving the report files, but it allows me to delete the indirect variable and indirect array code in update-codgen. As proud as I was of figuring that out, I am also ashamed of myself for doing it. This is my atonement. --- api/api-rules/README.md | 8 +- hack/update-codegen.sh | 218 ++++-------------- .../api_violation_exceptions.list | 0 .../hack/update-codegen.sh | 6 + .../examples/api_violation_exceptions.list | 0 .../examples/hack/update-codegen.sh | 6 + .../src/k8s.io/code-generator/kube_codegen.sh | 24 +- .../api_violation_exceptions.list | 0 .../kube-aggregator/hack/update-codegen.sh | 6 + .../api_violation_exceptions.list | 0 .../sample-apiserver/hack/update-codegen.sh | 6 + 11 files changed, 103 insertions(+), 171 deletions(-) rename api/api-rules/apiextensions_violation_exceptions.list => staging/src/k8s.io/apiextensions-apiserver/api_violation_exceptions.list (100%) rename api/api-rules/codegen_violation_exceptions.list => staging/src/k8s.io/code-generator/examples/api_violation_exceptions.list (100%) rename api/api-rules/aggregator_violation_exceptions.list => staging/src/k8s.io/kube-aggregator/api_violation_exceptions.list (100%) rename api/api-rules/sample_apiserver_violation_exceptions.list => staging/src/k8s.io/sample-apiserver/api_violation_exceptions.list (100%) diff --git a/api/api-rules/README.md b/api/api-rules/README.md index 5edeaa77a5d..306ac93b8c9 100644 --- a/api/api-rules/README.md +++ b/api/api-rules/README.md @@ -1,8 +1,10 @@ # Existing API Rule Violations -This folder contains the checked-in report file of known API rule violations. -The file violation\_exceptions.list is used by Make rule during OpenAPI spec generation to make -sure that no new API rule violation is introduced into our code base. +This folder contains the checked-in report file of known API rule violations +for the main Kubernetes repository. Reports for staging repositories are +checked in to those repositories. These files are used during OpenAPI spec +generation to make sure that no new API rule violation is introduced into our +code base. ## API Rule Violation Format diff --git a/hack/update-codegen.sh b/hack/update-codegen.sh index 30ede5fea8a..89f05332b5f 100755 --- a/hack/update-codegen.sh +++ b/hack/update-codegen.sh @@ -497,47 +497,6 @@ function k8s_tag_files_except() { done } -# $@: directories to exclude -# example: -# k8s_tag_files_matching foo bat/qux -function k8s_tag_files_matching() { - for f in "${ALL_K8S_TAG_FILES[@]}"; do - for x in "$@"; do - if [[ "$f" =~ "${x}"/.* ]]; then - echo "$f" - break - fi - done - done -} - -# $1: the name of a scalar variable to read -# example: -# FOO_VAR="foo value" -# BAR_VAR="bar value" -# x=FOO -# indirect "${x}_VAR" # -> "foo value\n" -function indirect() { - # This is a trick to get bash to indirectly read a variable. - # Thanks StackOverflow! - local var="$1" - echo "${!var}" -} - -# $1: the name of an array variable to read -# FOO_ARR=(a b c) -# BAR_ARR=(1 2 3) -# x=FOO -# indirect_array "${x}_ARR" # -> "a\nb\nc\n" -function indirect_array() { - # This is a trick to get bash to indirectly read an array. - # Thanks StackOverflow! - local arrayname="$1" - # shellcheck disable=SC1087 # intentional - local tmp="$arrayname[@]" - printf -- "%s\n" "${!tmp}" -} - # OpenAPI generation # # Any package that wants open-api functions generated must include a @@ -555,148 +514,73 @@ function codegen::openapi() { local gen_openapi_bin gen_openapi_bin="$(kube::util::find-binary "openapi-gen")" - # Standard dirs which all targets need. - local apimachinery_dirs=( - vendor/k8s.io/apimachinery/pkg/apis/meta/v1 - vendor/k8s.io/apimachinery/pkg/runtime - vendor/k8s.io/apimachinery/pkg/version - ) + local output_dir="pkg/generated/openapi" + local known_violations_file="api/api-rules/violation_exceptions.list" - # These should probably be configured by tags in code-files somewhere. - local targets=( - KUBE - AGGREGATOR - APIEXTENSIONS - CODEGEN - SAMPLEAPISERVER - ) + local report_file="${OUT_DIR}/api_violations.report" + # When UPDATE_API_KNOWN_VIOLATIONS is set to be true, let the generator to write + # updated API violations to the known API violation exceptions list. + if [[ "${UPDATE_API_KNOWN_VIOLATIONS}" == true ]]; then + report_file="${known_violations_file}" + fi - # shellcheck disable=SC2034 # used indirectly - local KUBE_output_dir="pkg/generated/openapi" - # shellcheck disable=SC2034 # used indirectly - local KUBE_known_violations_file="api/api-rules/violation_exceptions.list" - # shellcheck disable=SC2034 # used indirectly - local KUBE_tag_files=() - kube::util::read-array KUBE_tag_files < <( + if [[ "${DBG_CODEGEN}" == 1 ]]; then + kube::log::status "DBG: finding all +k8s:openapi-gen tags" + fi + + local tag_files=() + kube::util::read-array tag_files < <( k8s_tag_files_except \ vendor/k8s.io/code-generator \ vendor/k8s.io/sample-apiserver ) - # shellcheck disable=SC2034 # used indirectly - local AGGREGATOR_output_dir="staging/src/k8s.io/kube-aggregator/pkg/generated/openapi" - # shellcheck disable=SC2034 # used indirectly - local AGGREGATOR_known_violations_file="api/api-rules/aggregator_violation_exceptions.list" - # shellcheck disable=SC2034 # used indirectly - local AGGREGATOR_tag_files=() - kube::util::read-array AGGREGATOR_tag_files < <( - k8s_tag_files_matching \ - vendor/k8s.io/kube-aggregator \ - "${apimachinery_dirs[@]}" - ) + local tag_dirs=() + kube::util::read-array tag_dirs < <( + grep -l --null '+k8s:openapi-gen=' "${tag_files[@]}" \ + | xargs -0 -n1 dirname \ + | sort -u) - # shellcheck disable=SC2034 # used indirectly - local APIEXTENSIONS_output_dir="staging/src/k8s.io/apiextensions-apiserver/pkg/generated/openapi" - # shellcheck disable=SC2034 # used indirectly - local APIEXTENSIONS_known_violations_file="api/api-rules/apiextensions_violation_exceptions.list" - # shellcheck disable=SC2034 # used indirectly - local APIEXTENSIONS_tag_files=() - kube::util::read-array APIEXTENSIONS_tag_files < <( - k8s_tag_files_matching \ - vendor/k8s.io/apiextensions-apiserver \ - vendor/k8s.io/api/autoscaling/v1 \ - "${apimachinery_dirs[@]}" - ) + if [[ "${DBG_CODEGEN}" == 1 ]]; then + kube::log::status "DBG: found ${#tag_dirs[@]} +k8s:openapi-gen tagged dirs" + fi - # shellcheck disable=SC2034 # used indirectly - local CODEGEN_output_dir="staging/src/k8s.io/code-generator/examples/apiserver/openapi" - # shellcheck disable=SC2034 # used indirectly - local CODEGEN_known_violations_file="api/api-rules/codegen_violation_exceptions.list" - # shellcheck disable=SC2034 # used indirectly - local CODEGEN_tag_files=() - kube::util::read-array CODEGEN_tag_files < <( - k8s_tag_files_matching \ - vendor/k8s.io/code-generator \ - "${apimachinery_dirs[@]}" - ) + local tag_pkgs=() + for dir in "${tag_dirs[@]}"; do + tag_pkgs+=("${PRJ_SRC_PATH}/$dir") + done - # shellcheck disable=SC2034 # used indirectly - local SAMPLEAPISERVER_output_dir="staging/src/k8s.io/sample-apiserver/pkg/generated/openapi" - # shellcheck disable=SC2034 # used indirectly - local SAMPLEAPISERVER_known_violations_file="api/api-rules/sample_apiserver_violation_exceptions.list" - # shellcheck disable=SC2034 # used indirectly - local SAMPLEAPISERVER_tag_files=() - kube::util::read-array SAMPLEAPISERVER_tag_files < <( - k8s_tag_files_matching \ - vendor/k8s.io/sample-apiserver \ - "${apimachinery_dirs[@]}" - ) - - git_find -z ':(glob)**'/"${output_base}.go" | xargs -0 rm -f - - for prefix in "${targets[@]}"; do - local report_file="${OUT_DIR}/${prefix}_violations.report" - # When UPDATE_API_KNOWN_VIOLATIONS is set to be true, let the generator to write - # updated API violations to the known API violation exceptions list. - if [[ "${UPDATE_API_KNOWN_VIOLATIONS}" == true ]]; then - report_file=$(indirect "${prefix}_known_violations_file") - fi - - # 2 lines because shellcheck - local output_dir - output_dir=$(indirect "${prefix}_output_dir") - - if [[ "${DBG_CODEGEN}" == 1 ]]; then - kube::log::status "DBG: finding all +k8s:openapi-gen tags for ${prefix}" - fi - - local tag_dirs=() - kube::util::read-array tag_dirs < <( - grep -l --null '+k8s:openapi-gen=' $(indirect_array "${prefix}_tag_files") \ - | xargs -0 -n1 dirname \ - | sort -u) - - if [[ "${DBG_CODEGEN}" == 1 ]]; then - kube::log::status "DBG: found ${#tag_dirs[@]} +k8s:openapi-gen tagged dirs for ${prefix}" - fi - - local tag_pkgs=() + kube::log::status "Generating openapi code" + if [[ "${DBG_CODEGEN}" == 1 ]]; then + kube::log::status "DBG: running ${gen_openapi_bin} for:" for dir in "${tag_dirs[@]}"; do - tag_pkgs+=("${PRJ_SRC_PATH}/$dir") + kube::log::status "DBG: $dir" done + fi - kube::log::status "Generating openapi code for ${prefix}" - if [[ "${DBG_CODEGEN}" == 1 ]]; then - kube::log::status "DBG: running ${gen_openapi_bin} for:" - for dir in "${tag_dirs[@]}"; do - kube::log::status "DBG: $dir" - done - fi + git_find -z ':(glob)pkg/generated/**'/"${output_base}.go" | xargs -0 rm -f - ./hack/run-in-gopath.sh "${gen_openapi_bin}" \ - --v "${KUBE_VERBOSE}" \ - --logtostderr \ - -h "${BOILERPLATE_FILENAME}" \ - -O "${output_base}" \ - -p "${PRJ_SRC_PATH}/${output_dir}" \ - -r "${report_file}" \ - $(printf -- " -i %s" "${tag_pkgs[@]}") \ - "$@" + ./hack/run-in-gopath.sh "${gen_openapi_bin}" \ + --v "${KUBE_VERBOSE}" \ + --logtostderr \ + -h "${BOILERPLATE_FILENAME}" \ + -O "${output_base}" \ + -p "${PRJ_SRC_PATH}/${output_dir}" \ + -r "${report_file}" \ + $(printf -- " -i %s" "${tag_pkgs[@]}") \ + "$@" - touch "${report_file}" - # 2 lines because shellcheck - local known_filename - known_filename=$(indirect "${prefix}_known_violations_file") - if ! diff -u "${known_filename}" "${report_file}"; then - echo -e "ERROR:" - echo -e "\t'${prefix}' API rule check failed - reported violations differ from known violations" - echo -e "\tPlease read api/api-rules/README.md to resolve the failure in ${known_filename}" - fi + touch "${report_file}" + local known_filename="${known_violations_file}" + if ! diff -u "${known_filename}" "${report_file}"; then + echo -e "ERROR:" + echo -e "\tAPI rule check failed - reported violations differ from known violations" + echo -e "\tPlease read api/api-rules/README.md to resolve the failure in ${known_filename}" + fi - if [[ "${DBG_CODEGEN}" == 1 ]]; then - kube::log::status "Generated openapi code" - fi - done # for each prefix + if [[ "${DBG_CODEGEN}" == 1 ]]; then + kube::log::status "Generated openapi code" + fi } function codegen::applyconfigs() { diff --git a/api/api-rules/apiextensions_violation_exceptions.list b/staging/src/k8s.io/apiextensions-apiserver/api_violation_exceptions.list similarity index 100% rename from api/api-rules/apiextensions_violation_exceptions.list rename to staging/src/k8s.io/apiextensions-apiserver/api_violation_exceptions.list diff --git a/staging/src/k8s.io/apiextensions-apiserver/hack/update-codegen.sh b/staging/src/k8s.io/apiextensions-apiserver/hack/update-codegen.sh index a532fff837a..6ab4ee161e5 100755 --- a/staging/src/k8s.io/apiextensions-apiserver/hack/update-codegen.sh +++ b/staging/src/k8s.io/apiextensions-apiserver/hack/update-codegen.sh @@ -28,11 +28,17 @@ kube::codegen::gen_helpers \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" +if [[ "${UPDATE_API_KNOWN_VIOLATIONS:-}" == "true" ]]; then + update_report="--update-report" +fi + kube::codegen::gen_openapi \ --input-pkg-root k8s.io/apiextensions-apiserver/pkg \ --extra-pkgs k8s.io/api/autoscaling/v1 `# needed for Scale type` \ --output-pkg-root k8s.io/apiextensions-apiserver/pkg/generated \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ + --report-filename "${SCRIPT_ROOT}/api_violation_exceptions.list" \ + ${update_report:+"${update_report}"} \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" kube::codegen::gen_client \ diff --git a/api/api-rules/codegen_violation_exceptions.list b/staging/src/k8s.io/code-generator/examples/api_violation_exceptions.list similarity index 100% rename from api/api-rules/codegen_violation_exceptions.list rename to staging/src/k8s.io/code-generator/examples/api_violation_exceptions.list diff --git a/staging/src/k8s.io/code-generator/examples/hack/update-codegen.sh b/staging/src/k8s.io/code-generator/examples/hack/update-codegen.sh index 3d3a4f3a3bf..e9c65267065 100755 --- a/staging/src/k8s.io/code-generator/examples/hack/update-codegen.sh +++ b/staging/src/k8s.io/code-generator/examples/hack/update-codegen.sh @@ -34,10 +34,16 @@ kube::codegen::gen_helpers \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../../.." \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" +if [[ "${UPDATE_API_KNOWN_VIOLATIONS:-}" == "true" ]]; then + update_report="--update-report" +fi + kube::codegen::gen_openapi \ --input-pkg-root k8s.io/code-generator/examples/apiserver/apis \ --output-pkg-root k8s.io/code-generator/examples/apiserver \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../../.." \ + --report-filename "${SCRIPT_ROOT}/api_violation_exceptions.list" \ + ${update_report:+"${update_report}"} \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" kube::codegen::gen_client \ diff --git a/staging/src/k8s.io/code-generator/kube_codegen.sh b/staging/src/k8s.io/code-generator/kube_codegen.sh index 59e807929dc..3342b9dcaeb 100755 --- a/staging/src/k8s.io/code-generator/kube_codegen.sh +++ b/staging/src/k8s.io/code-generator/kube_codegen.sh @@ -237,6 +237,9 @@ function kube::codegen::gen_helpers() { # An optional path at which to write an API violations report. "-" means # stdout. # +# --update-report +# If specified, update the report file in place, rather than diffing it. +# # --boilerplate # An optional override for the header file to insert into generated files. # @@ -247,6 +250,7 @@ function kube::codegen::gen_openapi() { local openapi_subdir="openapi" local extra_pkgs=() local report="/dev/null" + local update_report="" local boilerplate="${KUBE_CODEGEN_ROOT}/hack/boilerplate.go.txt" local v="${KUBE_VERBOSE:-0}" @@ -276,6 +280,10 @@ function kube::codegen::gen_openapi() { report="$2" shift 2 ;; + "--update-report") + update_report="true" + shift + ;; "--boilerplate") boilerplate="$2" shift 2 @@ -300,6 +308,12 @@ function kube::codegen::gen_openapi() { return 1 fi + local new_report + new_report="$(mktemp -t "$(basename "$0").api_violations.XXXXXX")" + if [ -n "${update_report}" ]; then + new_report="${report}" + fi + ( # To support running this from anywhere, first cd into this directory, # and then install with forced module mode on and fully qualified name. @@ -348,12 +362,20 @@ function kube::codegen::gen_openapi() { --go-header-file "${boilerplate}" \ --output-base "${out_base}" \ --output-package "${out_pkg_root}/${openapi_subdir}" \ - --report-filename "${report}" \ + --report-filename "${new_report}" \ --input-dirs "k8s.io/apimachinery/pkg/apis/meta/v1" \ --input-dirs "k8s.io/apimachinery/pkg/runtime" \ --input-dirs "k8s.io/apimachinery/pkg/version" \ "${inputs[@]}" fi + + touch "${report}" # in case it doesn't exist yet + if ! diff -u "${report}" "${new_report}"; then + echo -e "ERROR:" + echo -e "\tAPI rule check failed for ${report}: new reported violations" + echo -e "\tPlease read api/api-rules/README.md" + return 1 + fi } # Generate client code diff --git a/api/api-rules/aggregator_violation_exceptions.list b/staging/src/k8s.io/kube-aggregator/api_violation_exceptions.list similarity index 100% rename from api/api-rules/aggregator_violation_exceptions.list rename to staging/src/k8s.io/kube-aggregator/api_violation_exceptions.list diff --git a/staging/src/k8s.io/kube-aggregator/hack/update-codegen.sh b/staging/src/k8s.io/kube-aggregator/hack/update-codegen.sh index 8e77aa5f25b..2fc6d555c6b 100755 --- a/staging/src/k8s.io/kube-aggregator/hack/update-codegen.sh +++ b/staging/src/k8s.io/kube-aggregator/hack/update-codegen.sh @@ -28,10 +28,16 @@ kube::codegen::gen_helpers \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" +if [[ "${UPDATE_API_KNOWN_VIOLATIONS:-}" == "true" ]]; then + update_report="--update-report" +fi + kube::codegen::gen_openapi \ --input-pkg-root k8s.io/kube-aggregator/pkg/apis \ --output-pkg-root k8s.io/kube-aggregator/pkg/generated \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ + --report-filename "${SCRIPT_ROOT}/api_violation_exceptions.list" \ + ${update_report:+"${update_report}"} \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" kube::codegen::gen_client \ diff --git a/api/api-rules/sample_apiserver_violation_exceptions.list b/staging/src/k8s.io/sample-apiserver/api_violation_exceptions.list similarity index 100% rename from api/api-rules/sample_apiserver_violation_exceptions.list rename to staging/src/k8s.io/sample-apiserver/api_violation_exceptions.list diff --git a/staging/src/k8s.io/sample-apiserver/hack/update-codegen.sh b/staging/src/k8s.io/sample-apiserver/hack/update-codegen.sh index be72c4f3694..25b548b1757 100755 --- a/staging/src/k8s.io/sample-apiserver/hack/update-codegen.sh +++ b/staging/src/k8s.io/sample-apiserver/hack/update-codegen.sh @@ -33,10 +33,16 @@ kube::codegen::gen_helpers \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" +if [[ "${UPDATE_API_KNOWN_VIOLATIONS:-}" == "true" ]]; then + update_report="--update-report" +fi + kube::codegen::gen_openapi \ --input-pkg-root k8s.io/sample-apiserver/pkg/apis \ --output-pkg-root k8s.io/sample-apiserver/pkg/generated \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ + --report-filename "${SCRIPT_ROOT}/api_violation_exceptions.list" \ + ${update_report:+"${update_report}"} \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" kube::codegen::gen_client \ From 6607834207a7f0a6083b2a1278e5842da024bc7b Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Tue, 9 May 2023 18:43:04 -0700 Subject: [PATCH 3/3] Use the same report files as before --- api/api-rules/README.md | 8 +++----- .../api-rules/aggregator_violation_exceptions.list | 0 .../api-rules/apiextensions_violation_exceptions.list | 0 .../api-rules/codegen_violation_exceptions.list | 0 .../api-rules/sample_apiserver_violation_exceptions.list | 0 hack/update-codegen.sh | 8 ++++++-- .../apiextensions-apiserver/hack/update-codegen.sh | 9 ++++++--- .../code-generator/examples/hack/update-codegen.sh | 9 ++++++--- staging/src/k8s.io/code-generator/kube_codegen.sh | 2 ++ .../src/k8s.io/kube-aggregator/hack/update-codegen.sh | 9 ++++++--- .../src/k8s.io/sample-apiserver/hack/update-codegen.sh | 9 ++++++--- 11 files changed, 35 insertions(+), 19 deletions(-) rename staging/src/k8s.io/kube-aggregator/api_violation_exceptions.list => api/api-rules/aggregator_violation_exceptions.list (100%) rename staging/src/k8s.io/apiextensions-apiserver/api_violation_exceptions.list => api/api-rules/apiextensions_violation_exceptions.list (100%) rename staging/src/k8s.io/code-generator/examples/api_violation_exceptions.list => api/api-rules/codegen_violation_exceptions.list (100%) rename staging/src/k8s.io/sample-apiserver/api_violation_exceptions.list => api/api-rules/sample_apiserver_violation_exceptions.list (100%) diff --git a/api/api-rules/README.md b/api/api-rules/README.md index 306ac93b8c9..5edeaa77a5d 100644 --- a/api/api-rules/README.md +++ b/api/api-rules/README.md @@ -1,10 +1,8 @@ # Existing API Rule Violations -This folder contains the checked-in report file of known API rule violations -for the main Kubernetes repository. Reports for staging repositories are -checked in to those repositories. These files are used during OpenAPI spec -generation to make sure that no new API rule violation is introduced into our -code base. +This folder contains the checked-in report file of known API rule violations. +The file violation\_exceptions.list is used by Make rule during OpenAPI spec generation to make +sure that no new API rule violation is introduced into our code base. ## API Rule Violation Format diff --git a/staging/src/k8s.io/kube-aggregator/api_violation_exceptions.list b/api/api-rules/aggregator_violation_exceptions.list similarity index 100% rename from staging/src/k8s.io/kube-aggregator/api_violation_exceptions.list rename to api/api-rules/aggregator_violation_exceptions.list diff --git a/staging/src/k8s.io/apiextensions-apiserver/api_violation_exceptions.list b/api/api-rules/apiextensions_violation_exceptions.list similarity index 100% rename from staging/src/k8s.io/apiextensions-apiserver/api_violation_exceptions.list rename to api/api-rules/apiextensions_violation_exceptions.list diff --git a/staging/src/k8s.io/code-generator/examples/api_violation_exceptions.list b/api/api-rules/codegen_violation_exceptions.list similarity index 100% rename from staging/src/k8s.io/code-generator/examples/api_violation_exceptions.list rename to api/api-rules/codegen_violation_exceptions.list diff --git a/staging/src/k8s.io/sample-apiserver/api_violation_exceptions.list b/api/api-rules/sample_apiserver_violation_exceptions.list similarity index 100% rename from staging/src/k8s.io/sample-apiserver/api_violation_exceptions.list rename to api/api-rules/sample_apiserver_violation_exceptions.list diff --git a/hack/update-codegen.sh b/hack/update-codegen.sh index 89f05332b5f..201aca0b004 100755 --- a/hack/update-codegen.sh +++ b/hack/update-codegen.sh @@ -32,6 +32,7 @@ kube::golang::setup_env DBG_CODEGEN="${DBG_CODEGEN:-0}" GENERATED_FILE_PREFIX="${GENERATED_FILE_PREFIX:-zz_generated.}" UPDATE_API_KNOWN_VIOLATIONS="${UPDATE_API_KNOWN_VIOLATIONS:-}" +API_KNOWN_VIOLATIONS_DIR="${API_KNOWN_VIOLATIONS_DIR:-"${KUBE_ROOT}/api/api-rules"}" OUT_DIR="_output" PRJ_SRC_PATH="k8s.io/kubernetes" @@ -515,7 +516,7 @@ function codegen::openapi() { gen_openapi_bin="$(kube::util::find-binary "openapi-gen")" local output_dir="pkg/generated/openapi" - local known_violations_file="api/api-rules/violation_exceptions.list" + local known_violations_file="${API_KNOWN_VIOLATIONS_DIR}/violation_exceptions.list" local report_file="${OUT_DIR}/api_violations.report" # When UPDATE_API_KNOWN_VIOLATIONS is set to be true, let the generator to write @@ -792,7 +793,10 @@ function codegen::subprojects() { for sub in "${subs[@]}"; do kube::log::status "Generating code for subproject ${sub}" pushd "${sub}" >/dev/null - CODEGEN_PKG="${codegen}" ./hack/update-codegen.sh > >(indent) 2> >(indent >&2) + CODEGEN_PKG="${codegen}" \ + UPDATE_API_KNOWN_VIOLATIONS="${UPDATE_API_KNOWN_VIOLATIONS}" \ + API_KNOWN_VIOLATIONS_DIR="${API_KNOWN_VIOLATIONS_DIR}" \ + ./hack/update-codegen.sh > >(indent) 2> >(indent >&2) popd >/dev/null done } diff --git a/staging/src/k8s.io/apiextensions-apiserver/hack/update-codegen.sh b/staging/src/k8s.io/apiextensions-apiserver/hack/update-codegen.sh index 6ab4ee161e5..37676e2e963 100755 --- a/staging/src/k8s.io/apiextensions-apiserver/hack/update-codegen.sh +++ b/staging/src/k8s.io/apiextensions-apiserver/hack/update-codegen.sh @@ -28,8 +28,11 @@ kube::codegen::gen_helpers \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" -if [[ "${UPDATE_API_KNOWN_VIOLATIONS:-}" == "true" ]]; then - update_report="--update-report" +if [[ -n "${API_KNOWN_VIOLATIONS_DIR:-}" ]]; then + report_filename="${API_KNOWN_VIOLATIONS_DIR}/apiextensions_violation_exceptions.list" + if [[ "${UPDATE_API_KNOWN_VIOLATIONS:-}" == "true" ]]; then + update_report="--update-report" + fi fi kube::codegen::gen_openapi \ @@ -37,7 +40,7 @@ kube::codegen::gen_openapi \ --extra-pkgs k8s.io/api/autoscaling/v1 `# needed for Scale type` \ --output-pkg-root k8s.io/apiextensions-apiserver/pkg/generated \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ - --report-filename "${SCRIPT_ROOT}/api_violation_exceptions.list" \ + --report-filename "${report_filename:-"/dev/null"}" \ ${update_report:+"${update_report}"} \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" diff --git a/staging/src/k8s.io/code-generator/examples/hack/update-codegen.sh b/staging/src/k8s.io/code-generator/examples/hack/update-codegen.sh index e9c65267065..7c7b251cde2 100755 --- a/staging/src/k8s.io/code-generator/examples/hack/update-codegen.sh +++ b/staging/src/k8s.io/code-generator/examples/hack/update-codegen.sh @@ -34,15 +34,18 @@ kube::codegen::gen_helpers \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../../.." \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" -if [[ "${UPDATE_API_KNOWN_VIOLATIONS:-}" == "true" ]]; then - update_report="--update-report" +if [[ -n "${API_KNOWN_VIOLATIONS_DIR:-}" ]]; then + report_filename="${API_KNOWN_VIOLATIONS_DIR}/codegen_violation_exceptions.list" + if [[ "${UPDATE_API_KNOWN_VIOLATIONS:-}" == "true" ]]; then + update_report="--update-report" + fi fi kube::codegen::gen_openapi \ --input-pkg-root k8s.io/code-generator/examples/apiserver/apis \ --output-pkg-root k8s.io/code-generator/examples/apiserver \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../../.." \ - --report-filename "${SCRIPT_ROOT}/api_violation_exceptions.list" \ + --report-filename "${report_filename:-"/dev/null"}" \ ${update_report:+"${update_report}"} \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" diff --git a/staging/src/k8s.io/code-generator/kube_codegen.sh b/staging/src/k8s.io/code-generator/kube_codegen.sh index 3342b9dcaeb..5d30fccb058 100755 --- a/staging/src/k8s.io/code-generator/kube_codegen.sh +++ b/staging/src/k8s.io/code-generator/kube_codegen.sh @@ -255,6 +255,7 @@ function kube::codegen::gen_openapi() { local v="${KUBE_VERBOSE:-0}" while [ "$#" -gt 0 ]; do + echo "$1" case "$1" in "--input-pkg-root") in_pkg_root="$2" @@ -308,6 +309,7 @@ function kube::codegen::gen_openapi() { return 1 fi + set -x local new_report new_report="$(mktemp -t "$(basename "$0").api_violations.XXXXXX")" if [ -n "${update_report}" ]; then diff --git a/staging/src/k8s.io/kube-aggregator/hack/update-codegen.sh b/staging/src/k8s.io/kube-aggregator/hack/update-codegen.sh index 2fc6d555c6b..c64782c821c 100755 --- a/staging/src/k8s.io/kube-aggregator/hack/update-codegen.sh +++ b/staging/src/k8s.io/kube-aggregator/hack/update-codegen.sh @@ -28,15 +28,18 @@ kube::codegen::gen_helpers \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" -if [[ "${UPDATE_API_KNOWN_VIOLATIONS:-}" == "true" ]]; then - update_report="--update-report" +if [[ -n "${API_KNOWN_VIOLATIONS_DIR:-}" ]]; then + report_filename="${API_KNOWN_VIOLATIONS_DIR}/aggregator_violation_exceptions.list" + if [[ "${UPDATE_API_KNOWN_VIOLATIONS:-}" == "true" ]]; then + update_report="--update-report" + fi fi kube::codegen::gen_openapi \ --input-pkg-root k8s.io/kube-aggregator/pkg/apis \ --output-pkg-root k8s.io/kube-aggregator/pkg/generated \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ - --report-filename "${SCRIPT_ROOT}/api_violation_exceptions.list" \ + --report-filename "${report_filename:-"/dev/null"}" \ ${update_report:+"${update_report}"} \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" diff --git a/staging/src/k8s.io/sample-apiserver/hack/update-codegen.sh b/staging/src/k8s.io/sample-apiserver/hack/update-codegen.sh index 25b548b1757..996bab05b09 100755 --- a/staging/src/k8s.io/sample-apiserver/hack/update-codegen.sh +++ b/staging/src/k8s.io/sample-apiserver/hack/update-codegen.sh @@ -33,15 +33,18 @@ kube::codegen::gen_helpers \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" -if [[ "${UPDATE_API_KNOWN_VIOLATIONS:-}" == "true" ]]; then - update_report="--update-report" +if [[ -n "${API_KNOWN_VIOLATIONS_DIR:-}" ]]; then + report_filename="${API_KNOWN_VIOLATIONS_DIR}/sample_apiserver_violation_exceptions.list" + if [[ "${UPDATE_API_KNOWN_VIOLATIONS:-}" == "true" ]]; then + update_report="--update-report" + fi fi kube::codegen::gen_openapi \ --input-pkg-root k8s.io/sample-apiserver/pkg/apis \ --output-pkg-root k8s.io/sample-apiserver/pkg/generated \ --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \ - --report-filename "${SCRIPT_ROOT}/api_violation_exceptions.list" \ + --report-filename "${report_filename:-"/dev/null"}" \ ${update_report:+"${update_report}"} \ --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt"