mirror of
https://github.com/outbackdingo/kubernetes.git
synced 2026-01-28 02:19:27 +00:00
This removes a dependency from generated applyconfigurations to a testing package. To do this, the type converter in the testing package has been moved out to the apimachinery package and the utilities the converter depend on have been reorganized.
78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
/*
|
|
Copyright 2021 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.
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"k8s.io/kube-openapi/pkg/common"
|
|
"k8s.io/kube-openapi/pkg/util"
|
|
"k8s.io/kube-openapi/pkg/validation/spec"
|
|
"k8s.io/kubernetes/pkg/generated/openapi"
|
|
)
|
|
|
|
// Outputs openAPI schema JSON containing the schema definitions in zz_generated.openapi.go.
|
|
func main() {
|
|
err := output()
|
|
if err != nil {
|
|
os.Stderr.WriteString(fmt.Sprintf("Failed: %v", err))
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func output() error {
|
|
refFunc := func(name string) spec.Ref {
|
|
return spec.MustCreateRef(fmt.Sprintf("#/definitions/%s", util.ToRESTFriendlyName(name)))
|
|
}
|
|
defs := openapi.GetOpenAPIDefinitions(refFunc)
|
|
schemaDefs := make(map[string]spec.Schema, len(defs))
|
|
for k, v := range defs {
|
|
// Replace top-level schema with v2 if a v2 schema is embedded
|
|
// so that the output of this program is always in OpenAPI v2.
|
|
// This is done by looking up an extension that marks the embedded v2
|
|
// schema, and, if the v2 schema is found, make it the resulting schema for
|
|
// the type.
|
|
if schema, ok := v.Schema.Extensions[common.ExtensionV2Schema]; ok {
|
|
if v2Schema, isOpenAPISchema := schema.(spec.Schema); isOpenAPISchema {
|
|
schemaDefs[util.ToRESTFriendlyName(k)] = v2Schema
|
|
continue
|
|
}
|
|
}
|
|
|
|
schemaDefs[util.ToRESTFriendlyName(k)] = v.Schema
|
|
}
|
|
data, err := json.Marshal(&spec.Swagger{
|
|
SwaggerProps: spec.SwaggerProps{
|
|
Definitions: schemaDefs,
|
|
Info: &spec.Info{
|
|
InfoProps: spec.InfoProps{
|
|
Title: "Kubernetes",
|
|
Version: "unversioned",
|
|
},
|
|
},
|
|
Swagger: "2.0",
|
|
},
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("error serializing api definitions: %w", err)
|
|
}
|
|
os.Stdout.Write(data)
|
|
return nil
|
|
}
|