Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing VisitArbitrary methods in kubectl explain #74029

Merged
merged 1 commit into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/kubectl/explain/field_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (f *fieldLookup) VisitKind(k *proto.Kind) {
subSchema.Accept(f)
}

func (f *fieldLookup) VisitArbitrary(a *proto.Arbitrary) {
f.Schema = a
}

// VisitReference is mostly a passthrough.
func (f *fieldLookup) VisitReference(r proto.Reference) {
if f.SaveLeafSchema(r) {
Expand Down
45 changes: 45 additions & 0 deletions pkg/kubectl/explain/field_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,48 @@ func TestFindField(t *testing.T) {
})
}
}
func TestCrdFindField(t *testing.T) {
schema := resources.LookupResource(schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "CrdKind",
})
if schema == nil {
t.Fatal("Counldn't find schema v1.CrdKind")
}

tests := []struct {
name string
path []string

err string
expectedPath string
}{
{
name: "test1",
path: []string{},
expectedPath: "CrdKind",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path, err := LookupSchemaForField(schema, tt.path)

gotErr := ""
if err != nil {
gotErr = err.Error()
}

gotPath := ""
if path != nil {
gotPath = path.GetPath().String()
}

if gotErr != tt.err || gotPath != tt.expectedPath {
t.Errorf("LookupSchemaForField(schema, %v) = (path: %q, err: %q), expected (path: %q, err: %q)",
tt.path, gotPath, gotErr, tt.expectedPath, tt.err)
}
})
}
}
14 changes: 14 additions & 0 deletions pkg/kubectl/explain/model_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ func (m *modelPrinter) PrintDescription(schema proto.Schema) error {
if err := m.Writer.Write("DESCRIPTION:"); err != nil {
return err
}
empty := true
for i, desc := range append(m.Descriptions, schema.GetDescription()) {
if desc == "" {
continue
}
empty = false
if i != 0 {
if err := m.Writer.Write(""); err != nil {
return err
Expand All @@ -69,6 +71,9 @@ func (m *modelPrinter) PrintDescription(schema proto.Schema) error {
return err
}
}
if empty {
return m.Writer.Indent(descriptionIndentLevel).WriteWrapped("<empty>")
}
return nil
}

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

func (m *modelPrinter) VisitArbitrary(a *proto.Arbitrary) {
if err := m.PrintKindAndVersion(); err != nil {
m.Error = err
return
}

m.Error = m.PrintDescription(a)
}

// VisitReference recurses inside the subtype, while collecting the description.
func (m *modelPrinter) VisitReference(r proto.Reference) {
m.Descriptions = append(m.Descriptions, r.GetDescription())
Expand Down
37 changes: 37 additions & 0 deletions pkg/kubectl/explain/model_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,40 @@ DESCRIPTION:
}
}
}
func TestCRDModel(t *testing.T) {
gvk := schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "CrdKind",
}
schema := resources.LookupResource(gvk)
if schema == nil {
t.Fatal("Couldn't find schema v1.CrdKind")
}

tests := []struct {
path []string
want string
}{
{
path: []string{},
want: `KIND: CrdKind
VERSION: v1

DESCRIPTION:
<empty>
`,
},
}

for _, test := range tests {
buf := bytes.Buffer{}
if err := PrintModelDescription(test.path, &buf, schema, gvk, false); err != nil {
t.Fatalf("Failed to PrintModelDescription for path %v: %v", test.path, err)
}
got := buf.String()
if got != test.want {
t.Errorf("Got:\n%v\nWant:\n%v\n", buf.String(), test.want)
}
}
}
9 changes: 9 additions & 0 deletions pkg/kubectl/explain/test-swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@
"$ref": "#/definitions/PrimitiveDef"
}
}
},
"CrdKind": {
"x-kubernetes-group-version-kind": [
{
"group": "",
"kind": "CrdKind",
"version": "v1"
}
]
}
}
}