code-generator: call openapi-gen from generate-internal-groups.sh

OpenAPI schema is a requirement for API Servers to be used with API Aggregation
and for kubectl to talk to them.
This commit is contained in:
Martin Vladev 2018-06-25 15:16:36 +03:00 committed by Dr. Stefan Schimanski
parent bc33fc8dfe
commit 35092146a4
7 changed files with 118 additions and 5 deletions

View File

@ -17,6 +17,7 @@ filegroup(
"//staging/src/k8s.io/code-generator/cmd/import-boss:all-srcs",
"//staging/src/k8s.io/code-generator/cmd/informer-gen:all-srcs",
"//staging/src/k8s.io/code-generator/cmd/lister-gen:all-srcs",
"//staging/src/k8s.io/code-generator/cmd/openapi-gen:all-srcs",
"//staging/src/k8s.io/code-generator/cmd/register-gen:all-srcs",
"//staging/src/k8s.io/code-generator/cmd/set-gen:all-srcs",
"//staging/src/k8s.io/code-generator/hack:all-srcs",

View File

@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package
// +k8s:defaulter-gen=TypeMeta
// +k8s:conversion-gen=k8s.io/code-generator/_examples/apiserver/apis/example

View File

@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package
// +k8s:defaulter-gen=TypeMeta
// +groupName=example.test.apiserver.code-generator.k8s.io

View File

@ -0,0 +1,35 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
go_library(
name = "go_default_library",
srcs = ["main.go"],
importmap = "k8s.io/kubernetes/vendor/k8s.io/code-generator/cmd/openapi-gen",
importpath = "k8s.io/code-generator/cmd/openapi-gen",
visibility = ["//visibility:private"],
deps = [
"//vendor/github.com/spf13/pflag:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
"//vendor/k8s.io/kube-openapi/cmd/openapi-gen/args:go_default_library",
"//vendor/k8s.io/kube-openapi/pkg/generators:go_default_library",
],
)
go_binary(
name = "openapi-gen",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,57 @@
/*
Copyright 2018 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.
*/
// This package generates openAPI definition file to be used in open API spec generation on API servers. To generate
// definition for a specific type or package add "+k8s:openapi-gen=true" tag to the type/package comment lines. To
// exclude a type from a tagged package, add "+k8s:openapi-gen=false" tag to the type comment lines.
package main
import (
"flag"
"log"
generatorargs "k8s.io/kube-openapi/cmd/openapi-gen/args"
"k8s.io/kube-openapi/pkg/generators"
"github.com/spf13/pflag"
"k8s.io/klog"
)
func main() {
klog.InitFlags(nil)
genericArgs, customArgs := generatorargs.NewDefaults()
genericArgs.AddFlags(pflag.CommandLine)
customArgs.AddFlags(pflag.CommandLine)
flag.Set("logtostderr", "true")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
if err := generatorargs.Validate(genericArgs); err != nil {
log.Fatalf("Arguments validation error: %v", err)
}
// Generates the code for the OpenAPIDefinitions.
if err := genericArgs.Execute(
generators.NameSystems(),
generators.DefaultNameSystem(),
generators.Packages,
); err != nil {
log.Fatalf("OpenAPI code generation error: %v", err)
}
}

View File

@ -25,7 +25,7 @@ if [ "$#" -lt 5 ] || [ "${1}" == "--help" ]; then
cat <<EOF
Usage: $(basename "$0") <generators> <output-package> <internal-apis-package> <extensiona-apis-package> <groups-versions> ...
<generators> the generators comma separated to run (deepcopy,defaulter,conversion,client,lister,informer) or "all".
<generators> the generators comma separated to run (deepcopy,defaulter,conversion,client,lister,informer,openapi) or "all".
<output-package> the output package name (e.g. github.com/example/project/pkg/generated).
<int-apis-package> the internal types dir (e.g. github.com/example/project/pkg/apis).
<ext-apis-package> the external types dir (e.g. github.com/example/project/pkg/apis or githubcom/example/apis).
@ -47,7 +47,8 @@ EXT_APIS_PKG="$4"
GROUPS_WITH_VERSIONS="$5"
shift 5
go install ./"$(dirname "${0}")"/cmd/{defaulter-gen,conversion-gen,client-gen,lister-gen,informer-gen,deepcopy-gen}
go install ./"$(dirname "${0}")"/cmd/{defaulter-gen,conversion-gen,client-gen,lister-gen,informer-gen,deepcopy-gen,openapi-gen}
function codegen::join() { local IFS="$1"; shift; echo "$*"; }
# enumerate group versions
@ -108,3 +109,14 @@ if [ "${GENS}" = "all" ] || grep -qw "informer" <<<"${GENS}"; then
--output-package "${OUTPUT_PKG}/informers" \
"$@"
fi
if [ "${GENS}" = "all" ] || grep -qw "openapi" <<<"${GENS}"; then
echo "Generating OpenAPI definitions for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/openapi"
declare -a OPENAPI_EXTRA_PACKAGES
"${GOPATH}/bin/openapi-gen" \
--input-dirs "$(codegen::join , "${EXT_FQ_APIS[@]}" "${OPENAPI_EXTRA_PACKAGES[@]}")" \
--input-dirs "k8s.io/apimachinery/pkg/apis/meta/v1,k8s.io/apimachinery/pkg/runtime,k8s.io/apimachinery/pkg/version" \
--output-package "${OUTPUT_PKG}/openapi" \
-O zz_generated.openapi \
"$@"
fi

View File

@ -18,6 +18,8 @@ set -o errexit
set -o nounset
set -o pipefail
SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
# generate the code with:
# - --output-base because this script should also be able to run inside the vendor dir of
# k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir
@ -25,12 +27,16 @@ set -o pipefail
"$(dirname "${BASH_SOURCE[0]}")"/../generate-internal-groups.sh all \
k8s.io/code-generator/_examples/apiserver k8s.io/code-generator/_examples/apiserver/apis k8s.io/code-generator/_examples/apiserver/apis \
"example:v1 example2:v1" \
--output-base "$(dirname "${BASH_SOURCE[0]}")/../../.."
--output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \
--go-header-file "${SCRIPT_ROOT}/hack/boilerplate.go.txt"
"$(dirname "${BASH_SOURCE[0]}")"/../generate-groups.sh all \
k8s.io/code-generator/_examples/crd k8s.io/code-generator/_examples/crd/apis \
"example:v1 example2:v1" \
--output-base "$(dirname "${BASH_SOURCE[0]}")/../../.."
--output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \
--go-header-file "${SCRIPT_ROOT}/hack/boilerplate.go.txt"
"$(dirname "${BASH_SOURCE[0]}")"/../generate-groups.sh all \
k8s.io/code-generator/_examples/MixedCase k8s.io/code-generator/_examples/MixedCase/apis \
"example:v1" \
--output-base "$(dirname "${BASH_SOURCE[0]}")/../../.."
--output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \
--go-header-file "${SCRIPT_ROOT}/hack/boilerplate.go.txt"