Skip to content

Commit 911fcef

Browse files
soltyshk8s-publishing-bot
authored andcommitted
Copy models-schema generator to sample-controller
Signed-off-by: Maciej Szulik <soltysh@gmail.com> Kubernetes-commit: 1d23ecbdde9caa688fddc17a3445c5fdd62e5c31
1 parent e1dc91e commit 911fcef

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

hack/verify-codegen.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ trap "cleanup" EXIT SIGINT
2929

3030
cleanup
3131

32+
# Ensure model-schema generator matches the version from
33+
# k8s.io/kubernetes/pkg/generated/openapi/cmd/models-schema/main.go
34+
echo "Ensuring models-schema is up-to-date"
35+
K8S_MODELS_SCHEMA="${SCRIPT_ROOT}/../../../../pkg/generated/openapi/cmd/models-schema/main.go"
36+
SAMPLE_CONTROLLER_MODELS_SCHEMA="${SCRIPT_ROOT}/pkg/generated/openapi/cmd/models-schema/main.go"
37+
# these two files will only differ in the imported lines for generated openapi
38+
if ! diff -I "k8s.io/kubernetes/pkg/generated/openapi" \
39+
-I "k8s.io/sample-controller/pkg/generated/openapi" \
40+
"${K8S_MODELS_SCHEMA}" "${SAMPLE_CONTROLLER_MODELS_SCHEMA}"; then
41+
echo "${SAMPLE_CONTROLLER_MODELS_SCHEMA} is out of date. Compare changes with ${K8S_MODELS_SCHEMA}"
42+
exit 1
43+
fi
44+
3245
mkdir -p "${TMP_DIFFROOT}"
3346
cp -a "${DIFFROOT}"/* "${TMP_DIFFROOT}"
3447

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
"os"
23+
24+
"k8s.io/kube-openapi/pkg/common"
25+
"k8s.io/kube-openapi/pkg/validation/spec"
26+
"k8s.io/sample-controller/pkg/generated/openapi"
27+
)
28+
29+
// Outputs openAPI schema JSON containing the schema definitions in zz_generated.openapi.go.
30+
func main() {
31+
err := output()
32+
if err != nil {
33+
os.Stderr.WriteString(fmt.Sprintf("Failed: %v", err)) // nolint:errcheck
34+
os.Exit(1)
35+
}
36+
}
37+
38+
func output() error {
39+
refFunc := func(name string) spec.Ref {
40+
return spec.MustCreateRef(fmt.Sprintf("#/definitions/%s", name))
41+
}
42+
defs := openapi.GetOpenAPIDefinitions(refFunc)
43+
schemaDefs := make(map[string]spec.Schema, len(defs))
44+
for k, v := range defs {
45+
// Replace top-level schema with v2 if a v2 schema is embedded
46+
// so that the output of this program is always in OpenAPI v2.
47+
// This is done by looking up an extension that marks the embedded v2
48+
// schema, and, if the v2 schema is found, make it the resulting schema for
49+
// the type.
50+
if schema, ok := v.Schema.Extensions[common.ExtensionV2Schema]; ok {
51+
if v2Schema, isOpenAPISchema := schema.(spec.Schema); isOpenAPISchema {
52+
schemaDefs[k] = v2Schema
53+
continue
54+
}
55+
}
56+
57+
schemaDefs[k] = v.Schema
58+
}
59+
data, err := json.Marshal(&spec.Swagger{
60+
SwaggerProps: spec.SwaggerProps{
61+
Definitions: schemaDefs,
62+
Info: &spec.Info{
63+
InfoProps: spec.InfoProps{
64+
Title: "Kubernetes",
65+
Version: "unversioned",
66+
},
67+
},
68+
Swagger: "2.0",
69+
},
70+
})
71+
if err != nil {
72+
return fmt.Errorf("error serializing api definitions: %w", err)
73+
}
74+
os.Stdout.Write(data) // nolint:errcheck
75+
return nil
76+
}

0 commit comments

Comments
 (0)