K8s: update hack codegen script (#81216)

This commit is contained in:
Charandas
2024-01-25 12:01:09 -08:00
committed by GitHub
parent 1595551a4a
commit 9167d67c05
28 changed files with 259 additions and 14938 deletions
+12 -8
View File
@@ -14,14 +14,18 @@ total 0
lrwxr-xr-x 1 ryan staff 37 Oct 5 09:34 grafana -> /Users/ryan/workspace/grafana/grafana
```
You can clone k8s [code-generator](https://github.com/kubernetes/code-generator) here and use `CODEGEN_PKG=<CODE-GENERATOR-GIT-ROOT>` when running the `update-codegen.sh` script.
The current workflow is to run the following:
The current workflow (sorry!) is to:
```shell
# ensure k8s.io/code-generator pkg is up to date
go mod download
1. update the script to point to the group+version you want
2. run the `update-codegen.sh` script. This will produce a bunch of new files
3. move `pkg/generated/openapi/zz_generated.openapi.go` to `pkg/apis/{group/version}/zz_generated.openapi.go`.
4. edit the package name so it is {version} and remove the boilerplate k8s kinds
5. `rm -rf pkg/generated` -- we are not yet using most of the generated client stuff
# the happy path
./hack/update-codegen.sh
Once we are more comfortable with the outputs and process, we will build these steps into a more standard codegen pattern, but until then... happy hacking!
# if wanting to agree to a change which introduces different openapi violations, run with the bool set to true
UPDATE_API_KNOWN_VIOLATIONS=true ./hack/update-codegen.sh
```
Note that the script deletes existing openapi go code and regenerates in place so that you will temporarily see
deleted files in your `git status`. After a successful run, you should see them restored.
+7
View File
@@ -0,0 +1,7 @@
package hack
import (
"k8s.io/code-generator/pkg/util"
)
var _ = util.CurrentPackage
+161
View File
@@ -0,0 +1,161 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Provenance-includes-location: https://github.com/kubernetes/code-generator/blob/master/kube_codegen.sh
# Provenance-includes-license: Apache-2.0
# Provenance-includes-copyright: The Kubernetes Authors.
## NOTE: The following is a fork of the original gen_openapi helper in k8s.io/code-generator
## It allows us to generate separate openapi packages per api group.
# Generate openapi code
#
# Args:
#
# --input-pkg-single <string>
# The root directory of a single grafana API Group.
#
# --output-base <string>
# The root directory under which to emit code. The concatenation of
# <output-base> + <input-pkg-single> must be valid.
#
# --report-filename <string = "/dev/null">
# The filename of the API violations report in the input pkg directory.
#
# --update-report
# If specified, update the report file in place, rather than diffing it.
#
# --boilerplate <string = path_to_kube_codegen_boilerplate>
# An optional override for the header file to insert into generated files.
set -o errexit
set -o nounset
set -o pipefail
KUBE_CODEGEN_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
source "${CODEGEN_PKG}/kube_codegen.sh"
#
function grafana::codegen::gen_openapi() {
local in_pkg_single=""
local out_base=""
local report="/dev/null"
local update_report=""
local boilerplate="${KUBE_CODEGEN_ROOT}/hack/boilerplate.go.txt"
local v="${KUBE_VERBOSE:-0}"
while [ "$#" -gt 0 ]; do
case "$1" in
"--input-pkg-single")
in_pkg_single="$2"
shift 2
;;
"--output-base")
out_base="$2"
shift 2
;;
"--report-filename")
report="$2"
shift 2
;;
"--update-report")
update_report="true"
shift
;;
"--boilerplate")
boilerplate="$2"
shift 2
;;
*)
echo "unknown argument: $1" >&2
return 1
;;
esac
done
if [ -z "${in_pkg_single}" ]; then
echo "--input-pkg-single is required" >&2
return 1
fi
if [ -z "${report}" ]; then
echo "--report-filename is required" >&2
return 1
fi
if [ -z "${out_base}" ]; then
echo "--output-base is required" >&2
return 1
fi
(
# To support running this from anywhere, first cd into this directory,
# and then install with forced module mode on and fully qualified name.
cd "${KUBE_CODEGEN_ROOT}"
BINS=(
openapi-gen
)
# shellcheck disable=2046 # printf word-splitting is intentional
GO111MODULE=on go install $(printf "k8s.io/code-generator/cmd/%s " "${BINS[@]}")
)
# Go installs in $GOBIN if defined, and $GOPATH/bin otherwise
gobin="${GOBIN:-$(go env GOPATH)/bin}"
# These tools all assume out-dir == in-dir.
root="${out_base}/${in_pkg_single}"
mkdir -p "${root}"
root="$(cd "${root}" && pwd -P)"
local input_pkgs=()
while read -r dir; do
echo ${dir}
pkg="$(cd "${dir}" && GO111MODULE=on go list -find .)"
input_pkgs+=("${pkg}")
done < <(
( kube::codegen::internal::git_grep -l --null \
-e '+k8s:openapi-gen=' \
":(glob)${root}"/'**/*.go' \
|| true \
) | while read -r -d $'\0' F; do dirname "${F}"; done \
| LC_ALL=C sort -u
)
if [ "${#input_pkgs[@]}" != 0 ]; then
echo "Generating openapi code for ${#input_pkgs[@]} targets"
kube::codegen::internal::git_find -z \
":(glob)${root}"/'**/zz_generated.openapi.go' \
| xargs -0 rm -f
local inputs=()
for arg in "${input_pkgs[@]}"; do
inputs+=("--input-dirs" "$arg")
done
local new_report
new_report="${root}/${report}.tmp"
if [ -n "${update_report}" ]; then
new_report="${root}/${report}"
fi
"${gobin}/openapi-gen" \
-v "${v}" \
-O zz_generated.openapi \
--go-header-file "${boilerplate}" \
--output-base "${out_base}" \
--output-package "${in_pkg_single}" \
--report-filename "${new_report}" \
"${inputs[@]}"
fi
touch "${root}/${report}" # in case it doesn't exist yet
if ! diff -u "${root}/${report}" "${new_report}"; then
echo -e "ERROR:"
echo -e "\tAPI rule check failed for ${root}/${report}: new reported violations"
echo -e "\tPlease read api/api-rules/README.md"
return 1
fi
# if all goes well, remove the temporary reports
if [ -z "${update_report}" ]; then
rm -f "${new_report}"
fi
}
+43 -32
View File
@@ -1,45 +1,56 @@
#!/usr/bin/env bash
# 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.
# SPDX-License-Identifier: AGPL-3.0-only
# Provenance-includes-location: https://github.com/kubernetes/sample-apiserver/blob/master/hack/update-codegen.sh
# Provenance-includes-license: Apache-2.0
# Provenance-includes-copyright: The Kubernetes Authors.
set -o errexit
set -o nounset
set -o pipefail
SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo $GOPATH/pkg/mod/k8s.io/code-generator@v0.27.1)}
CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo $GOPATH/pkg/mod/k8s.io/code-generator@v0.29.1)}
OUTDIR="${HOME}/go/src"
OPENAPI_VIOLATION_EXCEPTIONS_FILENAME="zz_generated.openapi_violation_exceptions.list"
echo $OUTDIR
source "${CODEGEN_PKG}/kube_codegen.sh"
source "$(dirname "${BASH_SOURCE[0]}")/openapi-codegen.sh"
CLIENTSET_NAME_VERSIONED=clientset \
CLIENTSET_PKG_NAME=clientset \
"${CODEGEN_PKG}/generate-groups.sh" "all" \
github.com/grafana/grafana/pkg/generated \
github.com/grafana/grafana/pkg/apis \
"datasource:v0alpha1" \
--output-base "${OUTDIR}" \
--go-header-file "${SCRIPT_ROOT}/hack/boilerplate.go.txt"
kube::codegen::gen_helpers \
--input-pkg-root github.com/grafana/grafana/pkg/apis \
--output-base "${OUTDIR}" \
--boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt"
CLIENTSET_NAME_VERSIONED=clientset \
CLIENTSET_PKG_NAME=clientset \
"${CODEGEN_PKG}/generate-internal-groups.sh" "deepcopy,defaulter,conversion,openapi" \
github.com/grafana/grafana/pkg/generated \
github.com/grafana/grafana/pkg/apis \
github.com/grafana/grafana/pkg/apis \
"datasource:v0alpha1" \
--output-base "${OUTDIR}" \
--go-header-file "${SCRIPT_ROOT}/hack/boilerplate.go.txt"
if [[ "${UPDATE_API_KNOWN_VIOLATIONS:-}" == "true" ]]; then
update_report="--update-report"
fi
for api_pkg in $(ls ./pkg/apis); do
for pkg_version in $(ls ./pkg/apis/${api_pkg}); do
echo "Generating openapi package for ${api_pkg}, version=${pkg_version} ..."
grafana::codegen::gen_openapi \
--input-pkg-single github.com/grafana/grafana/pkg/apis/${api_pkg}/${pkg_version} \
--output-base "${OUTDIR}" \
--report-filename "${OPENAPI_VIOLATION_EXCEPTIONS_FILENAME}" \
${update_report:+"${update_report}"} \
--boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt"
done
violations_file="${OUTDIR}/github.com/grafana/grafana/pkg/apis/${api_pkg}/${pkg_version}/${OPENAPI_VIOLATION_EXCEPTIONS_FILENAME}"
# delete violation exceptions file, if empty
if ! grep -q . "${violations_file}"; then
echo "Deleting ${violations_file} since it is empty"
rm ${violations_file}
fi
done
kube::codegen::gen_client \
--with-watch \
--with-applyconfig \
--input-pkg-root github.com/grafana/grafana/pkg/apis \
--output-pkg-root github.com/grafana/grafana/pkg/generated \
--output-base "${OUTDIR}" \
--boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt"