...
1
16
17 package v2
18
19 import (
20 "encoding/json"
21 "errors"
22 "fmt"
23 "io"
24
25 "k8s.io/apimachinery/pkg/runtime"
26 "k8s.io/apimachinery/pkg/runtime/schema"
27 "k8s.io/client-go/openapi"
28 )
29
30
31
32
33 func PrintModelDescription(
34 fieldsPath []string,
35 w io.Writer,
36 client openapi.Client,
37 gvr schema.GroupVersionResource,
38 recursive bool,
39 outputFormat string,
40 ) error {
41 generator := NewGenerator()
42 if err := registerBuiltinTemplates(generator); err != nil {
43 return fmt.Errorf("error parsing builtin templates. Please file a bug on GitHub: %w", err)
44 }
45
46 return printModelDescriptionWithGenerator(
47 generator, fieldsPath, w, client, gvr, recursive, outputFormat)
48 }
49
50
51 func printModelDescriptionWithGenerator(
52 generator Generator,
53 fieldsPath []string,
54 w io.Writer,
55 client openapi.Client,
56 gvr schema.GroupVersionResource,
57 recursive bool,
58 outputFormat string,
59 ) error {
60 paths, err := client.Paths()
61
62 if err != nil {
63 return fmt.Errorf("failed to fetch list of groupVersions: %w", err)
64 }
65
66 var resourcePath string
67 if len(gvr.Group) == 0 {
68 resourcePath = fmt.Sprintf("api/%s", gvr.Version)
69 } else {
70 resourcePath = fmt.Sprintf("apis/%s/%s", gvr.Group, gvr.Version)
71 }
72
73 gv, exists := paths[resourcePath]
74
75 if !exists {
76 return fmt.Errorf("couldn't find resource for \"%v\"", gvr)
77 }
78
79 openAPISchemaBytes, err := gv.Schema(runtime.ContentTypeJSON)
80 if err != nil {
81 return fmt.Errorf("failed to fetch openapi schema for %s: %w", resourcePath, err)
82 }
83
84 var parsedV3Schema map[string]interface{}
85 if err := json.Unmarshal(openAPISchemaBytes, &parsedV3Schema); err != nil {
86 return fmt.Errorf("failed to parse openapi schema for %s: %w", resourcePath, err)
87 }
88
89 err = generator.Render(outputFormat, parsedV3Schema, gvr, fieldsPath, recursive, w)
90
91 explainErr := explainError("")
92 if errors.As(err, &explainErr) {
93 return explainErr
94 }
95
96 return err
97 }
98
View as plain text