Merge pull request #314 from Liujingfang1/appresource
Add appresource implementation
This commit is contained in:
commit
b9c08a40a6
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -160,7 +160,7 @@ func (l *ManifestLoader) loadKObjectFromFile(filename string, into types.KObject
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = kutil.Decode(content, into)
|
_, err = kutil.DecodeToKObject(content, into)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,10 +31,8 @@ import (
|
||||||
"k8s.io/kubectl/pkg/kinflate/types"
|
"k8s.io/kubectl/pkg/kinflate/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Decode decodes a list of objects in byte array format.
|
// 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
|
func Decode(in []byte) ([]*unstructured.Unstructured, error) {
|
||||||
// construct a new map and return it.
|
|
||||||
func Decode(in []byte, into types.KObject) (types.KObject, error) {
|
|
||||||
decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewReader(in), 1024)
|
decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewReader(in), 1024)
|
||||||
objs := []*unstructured.Unstructured{}
|
objs := []*unstructured.Unstructured{}
|
||||||
|
|
||||||
|
|
@ -50,6 +48,17 @@ func Decode(in []byte, into types.KObject) (types.KObject, error) {
|
||||||
if err != io.EOF {
|
if err != io.EOF {
|
||||||
return nil, err
|
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 {
|
if into == nil {
|
||||||
into = types.KObject{}
|
into = types.KObject{}
|
||||||
|
|
|
||||||
|
|
@ -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")
|
expected := makeConfigMaps("cm1", "cm2", "cm1", "cm2")
|
||||||
m, err := Decode(encoded, nil)
|
m, err := DecodeToKObject(encoded, nil)
|
||||||
|
fmt.Printf("%v\n", m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue