Skip to content

Commit 22a3f6d

Browse files
committed
Add missing VisitArbitrary methods in kubectl explain
1 parent 01e7b30 commit 22a3f6d

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

pkg/kubectl/explain/field_lookup.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ func (f *fieldLookup) VisitKind(k *proto.Kind) {
8989
subSchema.Accept(f)
9090
}
9191

92+
func (f *fieldLookup) VisitArbitrary(a *proto.Arbitrary) {
93+
f.Schema = a
94+
}
95+
9296
// VisitReference is mostly a passthrough.
9397
func (f *fieldLookup) VisitReference(r proto.Reference) {
9498
if f.SaveLeafSchema(r) {

pkg/kubectl/explain/field_lookup_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,48 @@ func TestFindField(t *testing.T) {
8787
})
8888
}
8989
}
90+
func TestCrdFindField(t *testing.T) {
91+
schema := resources.LookupResource(schema.GroupVersionKind{
92+
Group: "",
93+
Version: "v1",
94+
Kind: "CrdKind",
95+
})
96+
if schema == nil {
97+
t.Fatal("Counldn't find schema v1.CrdKind")
98+
}
99+
100+
tests := []struct {
101+
name string
102+
path []string
103+
104+
err string
105+
expectedPath string
106+
}{
107+
{
108+
name: "test1",
109+
path: []string{},
110+
expectedPath: "CrdKind",
111+
},
112+
}
113+
114+
for _, tt := range tests {
115+
t.Run(tt.name, func(t *testing.T) {
116+
path, err := LookupSchemaForField(schema, tt.path)
117+
118+
gotErr := ""
119+
if err != nil {
120+
gotErr = err.Error()
121+
}
122+
123+
gotPath := ""
124+
if path != nil {
125+
gotPath = path.GetPath().String()
126+
}
127+
128+
if gotErr != tt.err || gotPath != tt.expectedPath {
129+
t.Errorf("LookupSchemaForField(schema, %v) = (path: %q, err: %q), expected (path: %q, err: %q)",
130+
tt.path, gotPath, gotErr, tt.expectedPath, tt.err)
131+
}
132+
})
133+
}
134+
}

pkg/kubectl/explain/model_printer.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ func (m *modelPrinter) PrintDescription(schema proto.Schema) error {
5656
if err := m.Writer.Write("DESCRIPTION:"); err != nil {
5757
return err
5858
}
59+
empty := true
5960
for i, desc := range append(m.Descriptions, schema.GetDescription()) {
6061
if desc == "" {
6162
continue
6263
}
64+
empty = false
6365
if i != 0 {
6466
if err := m.Writer.Write(""); err != nil {
6567
return err
@@ -69,6 +71,9 @@ func (m *modelPrinter) PrintDescription(schema proto.Schema) error {
6971
return err
7072
}
7173
}
74+
if empty {
75+
return m.Writer.Indent(descriptionIndentLevel).WriteWrapped("<empty>")
76+
}
7277
return nil
7378
}
7479

@@ -134,6 +139,15 @@ func (m *modelPrinter) VisitPrimitive(p *proto.Primitive) {
134139
m.Error = m.PrintDescription(p)
135140
}
136141

142+
func (m *modelPrinter) VisitArbitrary(a *proto.Arbitrary) {
143+
if err := m.PrintKindAndVersion(); err != nil {
144+
m.Error = err
145+
return
146+
}
147+
148+
m.Error = m.PrintDescription(a)
149+
}
150+
137151
// VisitReference recurses inside the subtype, while collecting the description.
138152
func (m *modelPrinter) VisitReference(r proto.Reference) {
139153
m.Descriptions = append(m.Descriptions, r.GetDescription())

pkg/kubectl/explain/model_printer_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,40 @@ DESCRIPTION:
133133
}
134134
}
135135
}
136+
func TestCRDModel(t *testing.T) {
137+
gvk := schema.GroupVersionKind{
138+
Group: "",
139+
Version: "v1",
140+
Kind: "CrdKind",
141+
}
142+
schema := resources.LookupResource(gvk)
143+
if schema == nil {
144+
t.Fatal("Couldn't find schema v1.CrdKind")
145+
}
146+
147+
tests := []struct {
148+
path []string
149+
want string
150+
}{
151+
{
152+
path: []string{},
153+
want: `KIND: CrdKind
154+
VERSION: v1
155+
156+
DESCRIPTION:
157+
<empty>
158+
`,
159+
},
160+
}
161+
162+
for _, test := range tests {
163+
buf := bytes.Buffer{}
164+
if err := PrintModelDescription(test.path, &buf, schema, gvk, false); err != nil {
165+
t.Fatalf("Failed to PrintModelDescription for path %v: %v", test.path, err)
166+
}
167+
got := buf.String()
168+
if got != test.want {
169+
t.Errorf("Got:\n%v\nWant:\n%v\n", buf.String(), test.want)
170+
}
171+
}
172+
}

pkg/kubectl/explain/test-swagger.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@
7373
"$ref": "#/definitions/PrimitiveDef"
7474
}
7575
}
76+
},
77+
"CrdKind": {
78+
"x-kubernetes-group-version-kind": [
79+
{
80+
"group": "",
81+
"kind": "CrdKind",
82+
"version": "v1"
83+
}
84+
]
7685
}
7786
}
7887
}

0 commit comments

Comments
 (0)