K8s: update hack codegen script (#81216)

pull/81309/head
Charandas 1 year ago committed by GitHub
parent 1595551a4a
commit 9167d67c05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      go.mod
  2. 6
      go.sum
  3. 20
      hack/README.md
  4. 7
      hack/externalTools.go
  5. 161
      hack/openapi-codegen.sh
  6. 79
      hack/update-codegen.sh
  7. 1
      pkg/apis/common/v0alpha1/doc.go
  8. 2477
      pkg/apis/common/v0alpha1/zz_generated.openapi.go
  9. 1
      pkg/apis/common/v0alpha1/zz_generated.openapi_violation_exceptions.list
  10. 1
      pkg/apis/dashboard/v0alpha1/doc.go
  11. 2478
      pkg/apis/dashboard/v0alpha1/zz_generated.openapi.go
  12. 1
      pkg/apis/dashboard/v0alpha1/zz_generated.openapi_violation_exceptions.list
  13. 1
      pkg/apis/datasource/v0alpha1/doc.go
  14. 2477
      pkg/apis/datasource/v0alpha1/zz_generated.openapi.go
  15. 1
      pkg/apis/example/v0alpha1/doc.go
  16. 1
      pkg/apis/example/v0alpha1/zz_generated.openapi_violation_exceptions.list
  17. 1
      pkg/apis/featuretoggle/v0alpha1/doc.go
  18. 2477
      pkg/apis/featuretoggle/v0alpha1/zz_generated.openapi.go
  19. 3
      pkg/apis/featuretoggle/v0alpha1/zz_generated.openapi_violation_exceptions.list
  20. 1
      pkg/apis/folders/v0alpha1/doc.go
  21. 2477
      pkg/apis/folders/v0alpha1/zz_generated.openapi.go
  22. 1
      pkg/apis/folders/v0alpha1/zz_generated.openapi_violation_exceptions.list
  23. 1
      pkg/apis/playlist/v0alpha1/doc.go
  24. 16
      pkg/apis/playlist/v0alpha1/zz_generated.deepcopy.go
  25. 16
      pkg/apis/playlist/v0alpha1/zz_generated.defaults.go
  26. 1
      pkg/apis/playlist/v0alpha1/zz_generated.openapi_violation_exceptions.list
  27. 2477
      pkg/apis/service/v0alpha1/zz_generated.openapi.go
  28. 14
      pkg/generated/informers/externalversions/factory.go

@ -111,7 +111,7 @@ require (
golang.org/x/oauth2 v0.15.0 // @grafana/grafana-authnz-team
golang.org/x/sync v0.5.0 // @grafana/alerting-squad-backend
golang.org/x/time v0.5.0 // @grafana/backend-platform
golang.org/x/tools v0.16.0 // @grafana/grafana-as-code
golang.org/x/tools v0.16.1 // @grafana/grafana-as-code
gonum.org/v1/gonum v0.12.0 // @grafana/observability-metrics
google.golang.org/api v0.149.0 // @grafana/backend-platform
google.golang.org/grpc v1.60.1 // @grafana/plugins-platform-backend
@ -484,6 +484,8 @@ require (
require k8s.io/apiextensions-apiserver v0.29.0 // @grafana/grafana-app-platform-squad
require k8s.io/code-generator v0.29.1 // @grafana/grafana-app-platform-squad
// Use fork of crewjam/saml with fixes for some issues until changes get merged into upstream
replace github.com/crewjam/saml => github.com/grafana/saml v0.4.15-0.20231025143828-a6c0e9b86a4c

@ -3728,8 +3728,8 @@ golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM=
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@ -4164,6 +4164,8 @@ k8s.io/apiserver v0.29.0/go.mod h1:31n78PsRKPmfpee7/l9NYEv67u6hOL6AfcE761HapDM=
k8s.io/client-go v0.26.2/go.mod h1:u5EjOuSyBa09yqqyY7m3abZeovO/7D/WehVVlZ2qcqU=
k8s.io/client-go v0.29.0 h1:KmlDtFcrdUzOYrBhXHgKw5ycWzc3ryPX5mQe0SkG3y8=
k8s.io/client-go v0.29.0/go.mod h1:yLkXH4HKMAywcrD82KMSmfYg2DlE8mepPR4JGSo5n38=
k8s.io/code-generator v0.29.1 h1:8ba8BdtSmAVHgAMpzThb/fuyQeTRtN7NtN7VjMcDLew=
k8s.io/code-generator v0.29.1/go.mod h1:FwFi3C9jCrmbPjekhaCYcYG1n07CYiW1+PAPCockaos=
k8s.io/component-base v0.29.0 h1:T7rjd5wvLnPBV1vC4zWd/iWRbV8Mdxs+nGaoaFzGw3s=
k8s.io/component-base v0.29.0/go.mod h1:sADonFTQ9Zc9yFLghpDpmNXEdHyQmFIGbiuZbqAXQ1M=
k8s.io/gengo v0.0.0-20210813121822-485abfe95c7c/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=

@ -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.

@ -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

@ -1,5 +1,6 @@
// +k8s:deepcopy-gen=package
// +k8s:openapi-gen=true
// +k8s:defaulter-gen=TypeMeta
// +groupName=example.grafana.app
// The testing api is a dependency free service that we can use to experiment with

@ -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"

@ -1,21 +1,7 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
/*
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.
*/
// SPDX-License-Identifier: AGPL-3.0-only
// Code generated by deepcopy-gen. DO NOT EDIT.

@ -1,21 +1,7 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
/*
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.
*/
// SPDX-License-Identifier: AGPL-3.0-only
// Code generated by defaulter-gen. DO NOT EDIT.

@ -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

@ -28,6 +28,7 @@ type sharedInformerFactory struct {
lock sync.Mutex
defaultResync time.Duration
customResync map[reflect.Type]time.Duration
transform cache.TransformFunc
informers map[reflect.Type]cache.SharedIndexInformer
// startedInformers is used for tracking which informers have been started.
@ -66,6 +67,14 @@ func WithNamespace(namespace string) SharedInformerOption {
}
}
// WithTransform sets a transform on all informers.
func WithTransform(transform cache.TransformFunc) SharedInformerOption {
return func(factory *sharedInformerFactory) *sharedInformerFactory {
factory.transform = transform
return factory
}
}
// NewSharedInformerFactory constructs a new instance of sharedInformerFactory for all namespaces.
func NewSharedInformerFactory(client versioned.Interface, defaultResync time.Duration) SharedInformerFactory {
return NewSharedInformerFactoryWithOptions(client, defaultResync)
@ -152,7 +161,7 @@ func (f *sharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[ref
return res
}
// InternalInformerFor returns the SharedIndexInformer for obj using an internal
// InformerFor returns the SharedIndexInformer for obj using an internal
// client.
func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer {
f.lock.Lock()
@ -170,6 +179,7 @@ func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internal
}
informer = newFunc(f.client, resyncPeriod)
informer.SetTransform(f.transform)
f.informers[informerType] = informer
return informer
@ -225,7 +235,7 @@ type SharedInformerFactory interface {
// ForResource gives generic access to a shared informer of the matching type.
ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
// InternalInformerFor returns the SharedIndexInformer for obj using an internal
// InformerFor returns the SharedIndexInformer for obj using an internal
// client.
InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) cache.SharedIndexInformer

Loading…
Cancel
Save