|
| 1 | +/* |
| 2 | +Copyright 2024 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 | + "strings" |
| 24 | + |
| 25 | + "k8s.io/kube-openapi/pkg/common" |
| 26 | + "k8s.io/kube-openapi/pkg/validation/spec" |
| 27 | + |
| 28 | + "sigs.k8s.io/cluster-api-provider-openstack/hack/codegen/openapi" |
| 29 | +) |
| 30 | + |
| 31 | +// Outputs openAPI schema JSON containing the schema definitions in zz_generated.openapi.go. |
| 32 | +// pulled from model_schema command of k/k |
| 33 | +func main() { |
| 34 | + err := output() |
| 35 | + if err != nil { |
| 36 | + os.Stderr.WriteString(fmt.Sprintf("Failed: %v", err)) |
| 37 | + os.Exit(1) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func output() error { |
| 42 | + refFunc := func(name string) spec.Ref { |
| 43 | + return spec.MustCreateRef(fmt.Sprintf("#/definitions/%s", friendlyName(name))) |
| 44 | + } |
| 45 | + defs := openapi.GetOpenAPIDefinitions(refFunc) |
| 46 | + schemaDefs := make(map[string]spec.Schema, len(defs)) |
| 47 | + for k, v := range defs { |
| 48 | + // Replace top-level schema with v2 if a v2 schema is embedded |
| 49 | + // so that the output of this program is always in OpenAPI v2. |
| 50 | + // This is done by looking up an extension that marks the embedded v2 |
| 51 | + // schema, and, if the v2 schema is found, make it the resulting schema for |
| 52 | + // the type. |
| 53 | + if schema, ok := v.Schema.Extensions[common.ExtensionV2Schema]; ok { |
| 54 | + if v2Schema, isOpenAPISchema := schema.(spec.Schema); isOpenAPISchema { |
| 55 | + schemaDefs[friendlyName(k)] = v2Schema |
| 56 | + continue |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + schemaDefs[friendlyName(k)] = v.Schema |
| 61 | + } |
| 62 | + data, err := json.Marshal(&spec.Swagger{ |
| 63 | + SwaggerProps: spec.SwaggerProps{ |
| 64 | + Definitions: schemaDefs, |
| 65 | + Info: &spec.Info{ |
| 66 | + InfoProps: spec.InfoProps{ |
| 67 | + Title: "Kubernetes", |
| 68 | + Version: "unversioned", |
| 69 | + }, |
| 70 | + }, |
| 71 | + Swagger: "2.0", |
| 72 | + }, |
| 73 | + }) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("error serializing api definitions: %w", err) |
| 76 | + } |
| 77 | + os.Stdout.Write(data) |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// From vendor/k8s.io/apiserver/pkg/endpoints/openapi/openapi.go |
| 82 | +func friendlyName(name string) string { |
| 83 | + nameParts := strings.Split(name, "/") |
| 84 | + // Reverse first part. e.g., io.k8s... instead of k8s.io... |
| 85 | + if len(nameParts) > 0 && strings.Contains(nameParts[0], ".") { |
| 86 | + parts := strings.Split(nameParts[0], ".") |
| 87 | + for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 { |
| 88 | + parts[i], parts[j] = parts[j], parts[i] |
| 89 | + } |
| 90 | + nameParts[0] = strings.Join(parts, ".") |
| 91 | + } |
| 92 | + return strings.Join(nameParts, ".") |
| 93 | +} |
0 commit comments