Debian Codegraph @

test/integration/builder/main.go

raw symbols SCIP
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 package main
18
19 import (
20 "encoding/json"
21 "log"
22 "os"
23
24 builderv2 "k8s.io/kube-openapi/pkg/builder"
25 "k8s.io/kube-openapi/pkg/validation/spec"
26 "k8s.io/kube-openapi/test/integration/pkg/generated"
27 "k8s.io/kube-openapi/test/integration/testutil"
28 )
29
30 // TODO: Change this to output the generated swagger to stdout.
31 const defaultSwaggerFile = "generated.json"
32
33 func main() {
34 // Get the name of the generated swagger file from the args
35 // if it exists; otherwise use the default file name.
36 swaggerFilename := defaultSwaggerFile
37 if len(os.Args) > 1 {
38 swaggerFilename = os.Args[1]
39 }
40
41 // Generate the definition names from the map keys returned
42 // from GetOpenAPIDefinitions. Anonymous function returning empty
43 // Ref is not used.
44 var defNames []string
45 for name := range generated.GetOpenAPIDefinitions(func(name string) spec.Ref {
46 return spec.Ref{}
47 }) {
48 defNames = append(defNames, name)
49 }
50
51 // Create a minimal builder config, then call the builder with the definition names.
52 config := testutil.CreateOpenAPIBuilderConfig()
53 config.GetDefinitions = generated.GetOpenAPIDefinitions
54 // Build the Paths using a simple WebService for the final spec
55 swagger, serr := builderv2.BuildOpenAPISpec(testutil.CreateWebServices(true), config)
56 if serr != nil {
57 log.Fatalf("ERROR: %s", serr.Error())
58 }
59
60 // Marshal the swagger spec into JSON, then write it out.
61 specBytes, err := json.MarshalIndent(swagger, " ", " ")
62 if err != nil {
63 log.Fatalf("json marshal error: %s", err.Error())
64 }
65 err = os.WriteFile(swaggerFilename, specBytes, 0644)
66 if err != nil {
67 log.Fatalf("stdout write error: %s", err.Error())
68 }
69 }