mirror of https://github.com/grafana/grafana
K8s: update hack codegen script (#81216)
parent
1595551a4a
commit
9167d67c05
@ -0,0 +1,7 @@ |
||||
package hack |
||||
|
||||
import ( |
||||
"k8s.io/code-generator/pkg/util" |
||||
) |
||||
|
||||
var _ = util.CurrentPackage |
@ -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 |
||||
} |
@ -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 |
||||
|
||||
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" |
||||
|
||||
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" |
||||
source "${CODEGEN_PKG}/kube_codegen.sh" |
||||
source "$(dirname "${BASH_SOURCE[0]}")/openapi-codegen.sh" |
||||
|
||||
kube::codegen::gen_helpers \ |
||||
--input-pkg-root github.com/grafana/grafana/pkg/apis \ |
||||
--output-base "${OUTDIR}" \ |
||||
--boilerplate "${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" |
||||
|
@ -1,4 +1,5 @@ |
||||
// +k8s:openapi-gen=true
|
||||
// +k8s:defaulter-gen=TypeMeta
|
||||
// +groupName=common.grafana.app
|
||||
|
||||
package v0alpha1 // import "github.com/grafana/grafana/pkg/apis/common/v0alpha1"
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1 @@ |
||||
API rule violation: names_match,github.com/grafana/grafana/pkg/apis/common/v0alpha1,Unstructured,Object |
@ -1,5 +1,6 @@ |
||||
// +k8s:deepcopy-gen=package
|
||||
// +k8s:openapi-gen=true
|
||||
// +k8s:defaulter-gen=TypeMeta
|
||||
// +groupName=dashboard.grafana.app
|
||||
|
||||
package v0alpha1 // import "github.com/grafana/grafana/pkg/apis/dashboard/v0alpha1"
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1 @@ |
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/dashboard/v0alpha1,DashboardSummarySpec,Tags |
@ -1,5 +1,6 @@ |
||||
// +k8s:deepcopy-gen=package
|
||||
// +k8s:openapi-gen=true
|
||||
// +k8s:defaulter-gen=TypeMeta
|
||||
// +groupName=datasources.grafana.com
|
||||
|
||||
package v0alpha1 |
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1 @@ |
||||
API rule violation: names_match,github.com/grafana/grafana/pkg/apis/example/v0alpha1,RuntimeInfo,IsEnterprise |
@ -1,5 +1,6 @@ |
||||
// +k8s:deepcopy-gen=package
|
||||
// +k8s:openapi-gen=true
|
||||
// +k8s:defaulter-gen=TypeMeta
|
||||
// +groupName=featuretoggle.grafana.app
|
||||
|
||||
package v0alpha1 // import "github.com/grafana/grafana/pkg/apis/featuretoggle/v0alpha1"
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,3 @@ |
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/featuretoggle/v0alpha1,ResolvedToggleState,Toggles |
||||
API rule violation: names_match,github.com/grafana/grafana/pkg/apis/featuretoggle/v0alpha1,FeatureSpec,FrontendOnly |
||||
API rule violation: names_match,github.com/grafana/grafana/pkg/apis/featuretoggle/v0alpha1,FeatureSpec,Owner |
@ -1,5 +1,6 @@ |
||||
// +k8s:deepcopy-gen=package
|
||||
// +k8s:openapi-gen=true
|
||||
// +k8s:defaulter-gen=TypeMeta
|
||||
// +groupName=folders.grafana.app
|
||||
|
||||
package v0alpha1 // import "github.com/grafana/grafana/pkg/apis/folders/v0alpha1"
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1 @@ |
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/folders/v0alpha1,FolderInfo,Items |
@ -1,5 +1,6 @@ |
||||
// +k8s:deepcopy-gen=package
|
||||
// +k8s:openapi-gen=true
|
||||
// +k8s:defaulter-gen=TypeMeta
|
||||
// +groupName=playlist.grafana.app
|
||||
|
||||
package v0alpha1 // import "github.com/grafana/grafana/pkg/apis/playlist/v0alpha1"
|
||||
|
@ -0,0 +1 @@ |
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/playlist/v0alpha1,Spec,Items |
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue