mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
flatten nested lists for flatten in visitor
This commit is contained in:
parent
63c33f3812
commit
a31d2c44f4
@ -376,12 +376,28 @@ func (v FlattenListVisitor) Visit(fn VisitorFunc) error {
|
||||
if info.Object == nil {
|
||||
return fn(info, nil)
|
||||
}
|
||||
items, err := meta.ExtractList(info.Object)
|
||||
if err != nil {
|
||||
if !meta.IsListType(info.Object) {
|
||||
return fn(info, nil)
|
||||
}
|
||||
if errs := runtime.DecodeList(items, v.mapper.decoder); len(errs) > 0 {
|
||||
return utilerrors.NewAggregate(errs)
|
||||
|
||||
items := []runtime.Object{}
|
||||
itemsToProcess := []runtime.Object{info.Object}
|
||||
|
||||
for i := 0; i < len(itemsToProcess); i++ {
|
||||
currObj := itemsToProcess[i]
|
||||
if !meta.IsListType(currObj) {
|
||||
items = append(items, currObj)
|
||||
continue
|
||||
}
|
||||
|
||||
currItems, err := meta.ExtractList(currObj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if errs := runtime.DecodeList(currItems, v.mapper.decoder); len(errs) > 0 {
|
||||
return utilerrors.NewAggregate(errs)
|
||||
}
|
||||
itemsToProcess = append(itemsToProcess, currItems...)
|
||||
}
|
||||
|
||||
// If we have a GroupVersionKind on the list, prioritize that when asking for info on the objects contained in the list
|
||||
|
@ -24,6 +24,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -163,3 +164,19 @@ func TestVisitorHttpGet(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlattenListVisitor(t *testing.T) {
|
||||
b := newDefaultBuilder().
|
||||
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: []string{"../../../../test/fixtures/pkg/kubectl/builder/deeply-nested.yaml"}}).
|
||||
Flatten()
|
||||
|
||||
test := &testVisitor{}
|
||||
|
||||
err := b.Do().Visit(test.Handle)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(test.Infos) != 6 {
|
||||
t.Fatal(spew.Sdump(test.Infos))
|
||||
}
|
||||
}
|
||||
|
21
test/fixtures/pkg/kubectl/builder/deeply-nested.yaml
vendored
Normal file
21
test/fixtures/pkg/kubectl/builder/deeply-nested.yaml
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
apiVersion: v1
|
||||
kind: List
|
||||
items:
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
- apiVersion: v1
|
||||
kind: List
|
||||
items:
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
- apiVersion: v1
|
||||
kind: List
|
||||
items:
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
Loading…
Reference in New Issue
Block a user