Merge pull request #331 from mengqiy/update_resources
update resource package
This commit is contained in:
commit
58f7d23214
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,28 +26,31 @@ 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{
|
||||
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{
|
||||
expected: []*resource.Resource{
|
||||
{
|
||||
Data: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "v1",
|
||||
|
|
@ -64,17 +67,20 @@ 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{
|
||||
expected: []*resource.Resource{
|
||||
{
|
||||
Data: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "v1",
|
||||
|
|
@ -92,15 +98,19 @@ BAR=baz
|
|||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "construct config map from literal",
|
||||
input: manifest.ConfigMap{
|
||||
input: []manifest.ConfigMap{
|
||||
{
|
||||
Name: "literalConfigMap",
|
||||
DataSources: manifest.DataSources{
|
||||
LiteralSources: []string{"a=x", "b=y"},
|
||||
},
|
||||
},
|
||||
expected: resource.Resource{
|
||||
},
|
||||
expected: []*resource.Resource{
|
||||
{
|
||||
Data: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "v1",
|
||||
|
|
@ -117,6 +127,7 @@ BAR=baz
|
|||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
// TODO: add testcase for data coming from multiple sources like
|
||||
// files/literal/env etc.
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,21 +26,24 @@ import (
|
|||
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
|
||||
)
|
||||
|
||||
func TestNewFromSecretGenerator(t *testing.T) {
|
||||
secret := manifest.SecretGenerator{
|
||||
func TestNewFromSecretGenerators(t *testing.T) {
|
||||
secrets := []manifest.SecretGenerator{
|
||||
{
|
||||
Name: "secret",
|
||||
Commands: map[string]string{
|
||||
"DB_USERNAME": "printf admin",
|
||||
"DB_PASSWORD": "printf somepw",
|
||||
},
|
||||
Type: "Opaque",
|
||||
},
|
||||
}
|
||||
re, err := NewFromSecretGenerator(secret, ".")
|
||||
re, err := NewFromSecretGenerators(".", secrets)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
expected := &unstructured.Unstructured{
|
||||
expected := []*Resource{
|
||||
{Data: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Secret",
|
||||
|
|
@ -54,9 +57,10 @@ func TestNewFromSecretGenerator(t *testing.T) {
|
|||
"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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue