k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/cmd/openapi-gen/openapi-gen.go (about)

     1  /*
     2  Copyright 2018 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  // This package generates openAPI definition file to be used in open API spec generation on API servers. To generate
    18  // definition for a specific type or package add "+k8s:openapi-gen=true" tag to the type/package comment lines. To
    19  // exclude a type from a tagged package, add "+k8s:openapi-gen=false" tag to the type comment lines.
    20  
    21  package main
    22  
    23  import (
    24  	"flag"
    25  	"log"
    26  
    27  	"github.com/spf13/pflag"
    28  	"k8s.io/gengo/v2"
    29  	"k8s.io/gengo/v2/generator"
    30  	"k8s.io/klog/v2"
    31  	"k8s.io/kube-openapi/cmd/openapi-gen/args"
    32  	"k8s.io/kube-openapi/pkg/generators"
    33  )
    34  
    35  func main() {
    36  	klog.InitFlags(nil)
    37  	args := args.New()
    38  
    39  	args.AddFlags(pflag.CommandLine)
    40  	flag.Set("logtostderr", "true")
    41  	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
    42  	pflag.Parse()
    43  
    44  	if err := args.Validate(); err != nil {
    45  		log.Fatalf("Arguments validation error: %v", err)
    46  	}
    47  
    48  	myTargets := func(context *generator.Context) []generator.Target {
    49  		return generators.GetTargets(context, args)
    50  	}
    51  
    52  	// Generates the code for the OpenAPIDefinitions.
    53  	if err := gengo.Execute(
    54  		generators.NameSystems(),
    55  		generators.DefaultNameSystem(),
    56  		myTargets,
    57  		gengo.StdBuildTag,
    58  		pflag.Args(),
    59  	); err != nil {
    60  		log.Fatalf("OpenAPI code generation error: %v", err)
    61  	}
    62  }