...

Source file src/k8s.io/kubernetes/pkg/generated/openapi/cmd/models-schema/main.go

Documentation: k8s.io/kubernetes/pkg/generated/openapi/cmd/models-schema

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

View as plain text