Merge pull request #331 from mengqiy/update_resources

update resource package
This commit is contained in:
k8s-ci-robot 2018-03-01 14:13:29 -08:00 committed by GitHub
commit 58f7d23214
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 135 additions and 84 deletions

View File

@ -21,8 +21,7 @@ import (
"k8s.io/kubectl/pkg/loader"
)
// NewFromPath returns a Resource list given a resource path from manifest file.
func NewFromPath(path string, loader loader.Loader) ([]*Resource, error) {
func resourcesFromPath(loader loader.Loader, path string) ([]*Resource, error) {
content, err := loader.Load(path)
if err != nil {
return nil, err
@ -39,3 +38,16 @@ func NewFromPath(path string, loader loader.Loader) ([]*Resource, error) {
}
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
kind: Deployment
@ -43,7 +43,7 @@ metadata:
{Data: makeUnconstructed("dply2")},
}
resources, _ := NewFromPath("/home/seans/project/deployment.yaml", l)
resources, _ := NewFromPaths(l, []string{"/home/seans/project/deployment.yaml"})
if len(resources) != 2 {
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"
)
// NewFromConfigMap returns a Resource given a configmap metadata from manifest file.
func NewFromConfigMap(cm manifest.ConfigMap, l loader.Loader) (*Resource, error) {
corev1CM, err := makeConfigMap(cm, l)
func newFromConfigMap(l loader.Loader, cm manifest.ConfigMap) (*Resource, error) {
corev1CM, err := makeConfigMap(l, cm)
if err != nil {
return nil, err
}
@ -41,7 +40,7 @@ func NewFromConfigMap(cm manifest.ConfigMap, l loader.Loader) (*Resource, error)
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 err error
@ -130,3 +129,16 @@ func addKV(m map[string]string, kv kvPair) error {
m[kv.key] = kv.value
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"
)
func TestNewFromConfigMap(t *testing.T) {
func TestNewFromConfigMaps(t *testing.T) {
type testCase struct {
description string
input manifest.ConfigMap
input []manifest.ConfigMap
filepath string
content string
expected resource.Resource
expected []*resource.Resource
}
l := loadertest.NewFakeLoader("/home/seans/project/")
testCases := []testCase{
{
description: "construct config map from env",
input: manifest.ConfigMap{
Name: "envConfigMap",
DataSources: manifest.DataSources{
EnvSource: "app.env",
input: []manifest.ConfigMap{
{
Name: "envConfigMap",
DataSources: manifest.DataSources{
EnvSource: "app.env",
},
},
},
filepath: "/home/seans/project/app.env",
content: "DB_USERNAME=admin\nDB_PASSWORD=somepw",
expected: resource.Resource{
Data: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "envConfigMap",
"creationTimestamp": nil,
},
"data": map[string]interface{}{
"DB_USERNAME": "admin",
"DB_PASSWORD": "somepw",
expected: []*resource.Resource{
{
Data: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "envConfigMap",
"creationTimestamp": nil,
},
"data": map[string]interface{}{
"DB_USERNAME": "admin",
"DB_PASSWORD": "somepw",
},
},
},
},
@ -66,27 +70,30 @@ func TestNewFromConfigMap(t *testing.T) {
},
{
description: "construct config map from file",
input: manifest.ConfigMap{
input: []manifest.ConfigMap{{
Name: "fileConfigMap",
DataSources: manifest.DataSources{
FileSources: []string{"app-init.ini"},
},
},
},
filepath: "/home/seans/project/app-init.ini",
content: "FOO=bar\nBAR=baz\n",
expected: resource.Resource{
Data: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "fileConfigMap",
"creationTimestamp": nil,
},
"data": map[string]interface{}{
"app-init.ini": `FOO=bar
expected: []*resource.Resource{
{
Data: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "fileConfigMap",
"creationTimestamp": nil,
},
"data": map[string]interface{}{
"app-init.ini": `FOO=bar
BAR=baz
`,
},
},
},
},
@ -94,24 +101,28 @@ BAR=baz
},
{
description: "construct config map from literal",
input: manifest.ConfigMap{
Name: "literalConfigMap",
DataSources: manifest.DataSources{
LiteralSources: []string{"a=x", "b=y"},
input: []manifest.ConfigMap{
{
Name: "literalConfigMap",
DataSources: manifest.DataSources{
LiteralSources: []string{"a=x", "b=y"},
},
},
},
expected: resource.Resource{
Data: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "literalConfigMap",
"creationTimestamp": nil,
},
"data": map[string]interface{}{
"a": "x",
"b": "y",
expected: []*resource.Resource{
{
Data: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "literalConfigMap",
"creationTimestamp": nil,
},
"data": map[string]interface{}{
"a": "x",
"b": "y",
},
},
},
},
@ -126,12 +137,12 @@ BAR=baz
if ferr := l.AddFile(tc.filepath, []byte(tc.content)); ferr != nil {
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 {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(*r, tc.expected) {
t.Fatalf("in testcase: %q got:\n%+v\n expected:\n%+v\n", tc.description, *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)
}
}
}

View File

@ -27,9 +27,7 @@ import (
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
)
// NewFromSecretGenerator takes a SecretGenerator and executes its command in directory p
// then writes the output to a Resource object and returns a pointer to it.
func NewFromSecretGenerator(s manifest.SecretGenerator, p string) (*Resource, error) {
func newFromSecretGenerator(p string, s manifest.SecretGenerator) (*Resource, error) {
corev1secret := &corev1.Secret{}
corev1secret.APIVersion = "v1"
corev1secret.Kind = "Secret"
@ -69,3 +67,17 @@ func createSecretKey(wd string, command string) ([]byte, error) {
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"
)
func TestNewFromSecretGenerator(t *testing.T) {
secret := manifest.SecretGenerator{
Name: "secret",
Commands: map[string]string{
"DB_USERNAME": "printf admin",
"DB_PASSWORD": "printf somepw",
func TestNewFromSecretGenerators(t *testing.T) {
secrets := []manifest.SecretGenerator{
{
Name: "secret",
Commands: map[string]string{
"DB_USERNAME": "printf admin",
"DB_PASSWORD": "printf somepw",
},
Type: "Opaque",
},
Type: "Opaque",
}
re, err := NewFromSecretGenerator(secret, ".")
re, err := NewFromSecretGenerators(".", secrets)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Secret",
"metadata": map[string]interface{}{
"name": "secret",
"creationTimestamp": nil,
expected := []*Resource{
{Data: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Secret",
"metadata": map[string]interface{}{
"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) {
t.Fatalf("%#v\ndoesn't match expected:\n%#v", *re.Data, *expected)
if !reflect.DeepEqual(re, expected) {
t.Fatalf("%#v\ndoesn't match expected:\n%#v", re, expected)
}
}