update resource pkg

This commit is contained in:
Mengqi Yu 2018-03-01 10:39:30 -08:00
parent 3bbff70115
commit 12e3e4da6f
6 changed files with 135 additions and 84 deletions

View File

@ -21,8 +21,7 @@ import (
"k8s.io/kubectl/pkg/loader" "k8s.io/kubectl/pkg/loader"
) )
// NewFromPath returns a Resource list given a resource path from manifest file. func resourcesFromPath(loader loader.Loader, path string) ([]*Resource, error) {
func NewFromPath(path string, loader loader.Loader) ([]*Resource, error) {
content, err := loader.Load(path) content, err := loader.Load(path)
if err != nil { if err != nil {
return nil, err return nil, err
@ -39,3 +38,16 @@ func NewFromPath(path string, loader loader.Loader) ([]*Resource, error) {
} }
return res, nil return res, nil
} }
// NewFromPaths returns a slice of Resources given a resource path slice from manifest file.
func NewFromPaths(loader loader.Loader, paths []string) ([]*Resource, error) {
allResources := []*Resource{}
for _, path := range paths {
res, err := resourcesFromPath(loader, path)
if err != nil {
return nil, err
}
allResources = append(allResources, res...)
}
return allResources, nil
}

View File

@ -21,7 +21,7 @@ func makeUnconstructed(name string) *unstructured.Unstructured {
} }
} }
func TestNewFromPath(t *testing.T) { func TestNewFromPaths(t *testing.T) {
resourceStr := `apiVersion: v1 resourceStr := `apiVersion: v1
kind: Deployment kind: Deployment
@ -43,7 +43,7 @@ metadata:
{Data: makeUnconstructed("dply2")}, {Data: makeUnconstructed("dply2")},
} }
resources, _ := NewFromPath("/home/seans/project/deployment.yaml", l) resources, _ := NewFromPaths(l, []string{"/home/seans/project/deployment.yaml"})
if len(resources) != 2 { if len(resources) != 2 {
t.Fatalf("%#v should contain 2 appResource, but got %d", resources, len(resources)) t.Fatalf("%#v should contain 2 appResource, but got %d", resources, len(resources))
} }

View File

@ -27,9 +27,8 @@ import (
"k8s.io/kubectl/pkg/loader" "k8s.io/kubectl/pkg/loader"
) )
// NewFromConfigMap returns a Resource given a configmap metadata from manifest file. func newFromConfigMap(l loader.Loader, cm manifest.ConfigMap) (*Resource, error) {
func NewFromConfigMap(cm manifest.ConfigMap, l loader.Loader) (*Resource, error) { corev1CM, err := makeConfigMap(l, cm)
corev1CM, err := makeConfigMap(cm, l)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -41,7 +40,7 @@ func NewFromConfigMap(cm manifest.ConfigMap, l loader.Loader) (*Resource, error)
return &Resource{Data: data}, nil return &Resource{Data: data}, nil
} }
func makeConfigMap(cm manifest.ConfigMap, l loader.Loader) (*corev1.ConfigMap, error) { func makeConfigMap(l loader.Loader, cm manifest.ConfigMap) (*corev1.ConfigMap, error) {
var envPairs, literalPairs, filePairs []kvPair var envPairs, literalPairs, filePairs []kvPair
var err error var err error
@ -130,3 +129,16 @@ func addKV(m map[string]string, kv kvPair) error {
m[kv.key] = kv.value m[kv.key] = kv.value
return nil return nil
} }
// NewFromConfigMaps returns a Resource slice given a configmap metadata slice from manifest file.
func NewFromConfigMaps(loader loader.Loader, cmList []manifest.ConfigMap) ([]*Resource, error) {
allResources := []*Resource{}
for _, cm := range cmList {
res, err := newFromConfigMap(loader, cm)
if err != nil {
return nil, err
}
allResources = append(allResources, res)
}
return allResources, nil
}

View File

@ -26,39 +26,43 @@ import (
"k8s.io/kubectl/pkg/loader/loadertest" "k8s.io/kubectl/pkg/loader/loadertest"
) )
func TestNewFromConfigMap(t *testing.T) { func TestNewFromConfigMaps(t *testing.T) {
type testCase struct { type testCase struct {
description string description string
input manifest.ConfigMap input []manifest.ConfigMap
filepath string filepath string
content string content string
expected resource.Resource expected []*resource.Resource
} }
l := loadertest.NewFakeLoader("/home/seans/project/") l := loadertest.NewFakeLoader("/home/seans/project/")
testCases := []testCase{ testCases := []testCase{
{ {
description: "construct config map from env", description: "construct config map from env",
input: manifest.ConfigMap{ input: []manifest.ConfigMap{
Name: "envConfigMap", {
DataSources: manifest.DataSources{ Name: "envConfigMap",
EnvSource: "app.env", DataSources: manifest.DataSources{
EnvSource: "app.env",
},
}, },
}, },
filepath: "/home/seans/project/app.env", filepath: "/home/seans/project/app.env",
content: "DB_USERNAME=admin\nDB_PASSWORD=somepw", content: "DB_USERNAME=admin\nDB_PASSWORD=somepw",
expected: resource.Resource{ expected: []*resource.Resource{
Data: &unstructured.Unstructured{ {
Object: map[string]interface{}{ Data: &unstructured.Unstructured{
"apiVersion": "v1", Object: map[string]interface{}{
"kind": "ConfigMap", "apiVersion": "v1",
"metadata": map[string]interface{}{ "kind": "ConfigMap",
"name": "envConfigMap", "metadata": map[string]interface{}{
"creationTimestamp": nil, "name": "envConfigMap",
}, "creationTimestamp": nil,
"data": map[string]interface{}{ },
"DB_USERNAME": "admin", "data": map[string]interface{}{
"DB_PASSWORD": "somepw", "DB_USERNAME": "admin",
"DB_PASSWORD": "somepw",
},
}, },
}, },
}, },
@ -66,27 +70,30 @@ func TestNewFromConfigMap(t *testing.T) {
}, },
{ {
description: "construct config map from file", description: "construct config map from file",
input: manifest.ConfigMap{ input: []manifest.ConfigMap{{
Name: "fileConfigMap", Name: "fileConfigMap",
DataSources: manifest.DataSources{ DataSources: manifest.DataSources{
FileSources: []string{"app-init.ini"}, FileSources: []string{"app-init.ini"},
}, },
}, },
},
filepath: "/home/seans/project/app-init.ini", filepath: "/home/seans/project/app-init.ini",
content: "FOO=bar\nBAR=baz\n", content: "FOO=bar\nBAR=baz\n",
expected: resource.Resource{ expected: []*resource.Resource{
Data: &unstructured.Unstructured{ {
Object: map[string]interface{}{ Data: &unstructured.Unstructured{
"apiVersion": "v1", Object: map[string]interface{}{
"kind": "ConfigMap", "apiVersion": "v1",
"metadata": map[string]interface{}{ "kind": "ConfigMap",
"name": "fileConfigMap", "metadata": map[string]interface{}{
"creationTimestamp": nil, "name": "fileConfigMap",
}, "creationTimestamp": nil,
"data": map[string]interface{}{ },
"app-init.ini": `FOO=bar "data": map[string]interface{}{
"app-init.ini": `FOO=bar
BAR=baz BAR=baz
`, `,
},
}, },
}, },
}, },
@ -94,24 +101,28 @@ BAR=baz
}, },
{ {
description: "construct config map from literal", description: "construct config map from literal",
input: manifest.ConfigMap{ input: []manifest.ConfigMap{
Name: "literalConfigMap", {
DataSources: manifest.DataSources{ Name: "literalConfigMap",
LiteralSources: []string{"a=x", "b=y"}, DataSources: manifest.DataSources{
LiteralSources: []string{"a=x", "b=y"},
},
}, },
}, },
expected: resource.Resource{ expected: []*resource.Resource{
Data: &unstructured.Unstructured{ {
Object: map[string]interface{}{ Data: &unstructured.Unstructured{
"apiVersion": "v1", Object: map[string]interface{}{
"kind": "ConfigMap", "apiVersion": "v1",
"metadata": map[string]interface{}{ "kind": "ConfigMap",
"name": "literalConfigMap", "metadata": map[string]interface{}{
"creationTimestamp": nil, "name": "literalConfigMap",
}, "creationTimestamp": nil,
"data": map[string]interface{}{ },
"a": "x", "data": map[string]interface{}{
"b": "y", "a": "x",
"b": "y",
},
}, },
}, },
}, },
@ -126,12 +137,12 @@ BAR=baz
if ferr := l.AddFile(tc.filepath, []byte(tc.content)); ferr != nil { if ferr := l.AddFile(tc.filepath, []byte(tc.content)); ferr != nil {
t.Fatalf("Error adding fake file: %v\n", ferr) t.Fatalf("Error adding fake file: %v\n", ferr)
} }
r, err := resource.NewFromConfigMap(tc.input, l) r, err := resource.NewFromConfigMaps(l, tc.input)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
if !reflect.DeepEqual(*r, tc.expected) { if !reflect.DeepEqual(r, tc.expected) {
t.Fatalf("in testcase: %q got:\n%+v\n expected:\n%+v\n", tc.description, *r, tc.expected) t.Fatalf("in testcase: %q got:\n%+v\n expected:\n%+v\n", tc.description, r, tc.expected)
} }
} }
} }

View File

@ -27,9 +27,7 @@ import (
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1" manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
) )
// NewFromSecretGenerator takes a SecretGenerator and executes its command in directory p func newFromSecretGenerator(p string, s manifest.SecretGenerator) (*Resource, error) {
// then writes the output to a Resource object and returns a pointer to it.
func NewFromSecretGenerator(s manifest.SecretGenerator, p string) (*Resource, error) {
corev1secret := &corev1.Secret{} corev1secret := &corev1.Secret{}
corev1secret.APIVersion = "v1" corev1secret.APIVersion = "v1"
corev1secret.Kind = "Secret" corev1secret.Kind = "Secret"
@ -69,3 +67,17 @@ func createSecretKey(wd string, command string) ([]byte, error) {
return cmd.Output() return cmd.Output()
} }
// NewFromSecretGenerators takes a SecretGenerator slice and executes its command in directory p
// then writes the output to a Resource slice and return it.
func NewFromSecretGenerators(p string, secretList []manifest.SecretGenerator) ([]*Resource, error) {
allResources := []*Resource{}
for _, secret := range secretList {
res, err := newFromSecretGenerator(p, secret)
if err != nil {
return nil, err
}
allResources = append(allResources, res)
}
return allResources, nil
}

View File

@ -26,37 +26,41 @@ import (
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1" manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
) )
func TestNewFromSecretGenerator(t *testing.T) { func TestNewFromSecretGenerators(t *testing.T) {
secret := manifest.SecretGenerator{ secrets := []manifest.SecretGenerator{
Name: "secret", {
Commands: map[string]string{ Name: "secret",
"DB_USERNAME": "printf admin", Commands: map[string]string{
"DB_PASSWORD": "printf somepw", "DB_USERNAME": "printf admin",
"DB_PASSWORD": "printf somepw",
},
Type: "Opaque",
}, },
Type: "Opaque",
} }
re, err := NewFromSecretGenerator(secret, ".") re, err := NewFromSecretGenerators(".", secrets)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
expected := &unstructured.Unstructured{ expected := []*Resource{
Object: map[string]interface{}{ {Data: &unstructured.Unstructured{
"apiVersion": "v1", Object: map[string]interface{}{
"kind": "Secret", "apiVersion": "v1",
"metadata": map[string]interface{}{ "kind": "Secret",
"name": "secret", "metadata": map[string]interface{}{
"creationTimestamp": nil, "name": "secret",
"creationTimestamp": nil,
},
"type": string(corev1.SecretTypeOpaque),
"data": map[string]interface{}{
"DB_USERNAME": base64.StdEncoding.EncodeToString([]byte("admin")),
"DB_PASSWORD": base64.StdEncoding.EncodeToString([]byte("somepw")),
},
}, },
"type": string(corev1.SecretTypeOpaque), }},
"data": map[string]interface{}{
"DB_USERNAME": base64.StdEncoding.EncodeToString([]byte("admin")),
"DB_PASSWORD": base64.StdEncoding.EncodeToString([]byte("somepw")),
},
},
} }
if !reflect.DeepEqual(*re.Data, *expected) { if !reflect.DeepEqual(re, expected) {
t.Fatalf("%#v\ndoesn't match expected:\n%#v", *re.Data, *expected) t.Fatalf("%#v\ndoesn't match expected:\n%#v", re, expected)
} }
} }