mirror of https://github.com/k3s-io/k3s
Add missing VisitArbitrary methods in kubectl explain
parent
01e7b3040a
commit
22a3f6de5e
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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())
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,6 +73,15 @@
|
|||
"$ref": "#/definitions/PrimitiveDef"
|
||||
}
|
||||
}
|
||||
},
|
||||
"CrdKind": {
|
||||
"x-kubernetes-group-version-kind": [
|
||||
{
|
||||
"group": "",
|
||||
"kind": "CrdKind",
|
||||
"version": "v1"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue