diff --git a/pkg/kinflate/resource/appresource.go b/pkg/kinflate/resource/appresource.go new file mode 100644 index 000000000..ac267baec --- /dev/null +++ b/pkg/kinflate/resource/appresource.go @@ -0,0 +1,40 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package resource + +import ( + kutil "k8s.io/kubectl/pkg/kinflate/util" + "k8s.io/kubectl/pkg/loader" +) + +func ResourcesFromPath(path string, loader loader.Loader) ([]*Resource, error) { + content, err := loader.Load(path) + if err != nil { + return nil, err + } + + objs, err := kutil.Decode(content) + if err != nil { + return nil, err + } + + var res []*Resource + for _, obj := range objs { + res = append(res, &Resource{Data: obj}) + } + return res, nil +} diff --git a/pkg/kinflate/resource/appresource_test.go b/pkg/kinflate/resource/appresource_test.go new file mode 100644 index 000000000..7225d15ab --- /dev/null +++ b/pkg/kinflate/resource/appresource_test.go @@ -0,0 +1,63 @@ +package resource + +import ( + "testing" + + "reflect" + + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/kubectl/pkg/loader" +) + +var encoded = []byte(`apiVersion: v1 +kind: Deployment +metadata: + name: dply1 +--- +apiVersion: v1 +kind: Deployment +metadata: + name: dply2 +`) + +type fakeLoader struct { +} + +func (l fakeLoader) New(newRoot string) (loader.Loader, error) { + return l, nil +} + +func (l fakeLoader) Load(location string) ([]byte, error) { + return encoded, nil +} + +func makeUnconstructed(name string) *unstructured.Unstructured { + return &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "Deployment", + "metadata": map[string]interface{}{ + "name": name, + }, + }, + } +} + +func TestAppResourceList_Resources(t *testing.T) { + l := fakeLoader{} + expected := []*Resource{ + {Data: makeUnconstructed("dply1")}, + {Data: makeUnconstructed("dply2")}, + } + + resources, _ := ResourcesFromPath("fake/path", l) + if len(resources) != 2 { + t.Fatalf("%#v should contain 2 appResource, but got %d", resources, len(resources)) + } + + for i, r := range resources { + if !reflect.DeepEqual(r.Data, expected[i].Data) { + t.Fatalf("expected %v, but got %v", expected[i].Data, r.Data) + } + } +} diff --git a/pkg/kinflate/tree/builder.go b/pkg/kinflate/tree/builder.go index d0c445147..1f4da8db6 100644 --- a/pkg/kinflate/tree/builder.go +++ b/pkg/kinflate/tree/builder.go @@ -160,7 +160,7 @@ func (l *ManifestLoader) loadKObjectFromFile(filename string, into types.KObject return err } - _, err = kutil.Decode(content, into) + _, err = kutil.DecodeToKObject(content, into) if err != nil { return err } diff --git a/pkg/kinflate/util/util.go b/pkg/kinflate/util/util.go index 30b206a3a..04389ad13 100644 --- a/pkg/kinflate/util/util.go +++ b/pkg/kinflate/util/util.go @@ -31,10 +31,8 @@ import ( "k8s.io/kubectl/pkg/kinflate/types" ) -// Decode decodes a list of objects in byte array format. -// Decoded object will be inserted in `into` if it's not nil. Otherwise, it will -// construct a new map and return it. -func Decode(in []byte, into types.KObject) (types.KObject, error) { +// Decode decodes a list of objects in byte array format +func Decode(in []byte) ([]*unstructured.Unstructured, error) { decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewReader(in), 1024) objs := []*unstructured.Unstructured{} @@ -50,6 +48,17 @@ func Decode(in []byte, into types.KObject) (types.KObject, error) { if err != io.EOF { return nil, err } + return objs, nil +} + +// DecodeToKObject decodes a list of objects in byte array format. +// Decoded object will be inserted in `into` if it's not nil. Otherwise, it will +// construct a new map and return it. +func DecodeToKObject(in []byte, into types.KObject) (types.KObject, error) { + objs, err := Decode(in) + if err != nil { + return nil, err + } if into == nil { into = types.KObject{} diff --git a/pkg/kinflate/util/util_test.go b/pkg/kinflate/util/util_test.go index 439f47ab1..7754e1d57 100644 --- a/pkg/kinflate/util/util_test.go +++ b/pkg/kinflate/util/util_test.go @@ -64,9 +64,10 @@ func makeConfigMaps(name1InGVKN, name2InGVKN, name1InObj, name2InObj string) typ } } -func TestDecode(t *testing.T) { +func TestDecodeToKObject(t *testing.T) { expected := makeConfigMaps("cm1", "cm2", "cm1", "cm2") - m, err := Decode(encoded, nil) + m, err := DecodeToKObject(encoded, nil) + fmt.Printf("%v\n", m) if err != nil { t.Fatalf("unexpected error: %v", err) }