Merge pull request #3251 from Shell32-Natsu/krm-function-converter

[KRM function converter] Get plugin config from data field
This commit is contained in:
Jeff Regan
2020-11-21 06:25:42 -08:00
committed by GitHub
2 changed files with 46 additions and 31 deletions

View File

@@ -81,15 +81,15 @@ functionConfig:
kind: FulfillmentCenter
metadata:
name: staging
metadata:
annotations:
config.kubernetes.io/function: |
container:
image: gcr.io/example/foo:v1.0.0
namespace: foo
fieldSpecs:
- path: metadata/namespace
create: true
annotations:
config.kubernetes.io/function: |
container:
image: gcr.io/example/foo:v1.0.0
data:
namespace: foo
fieldSpecs:
- path: metadata/namespace
create: true
items:
- apiVersion: apps/v1
kind: foobar
@@ -108,7 +108,9 @@ func runKrmFunction(t *testing.T, input []byte, dir string) []byte {
cmd.Stderr = eb
cmd.Dir = dir
err := cmd.Run()
assert.NoErrorf(t, err, "Stdout:\n%s\nStderr:\n%s\n", ob.String(), eb.String())
if !assert.NoErrorf(t, err, "Stdout:\n%s\nStderr:\n%s\n", ob.String(), eb.String()) {
t.FailNow()
}
return ob.Bytes()
}
@@ -139,15 +141,15 @@ functionConfig:
kind: FulfillmentCenter
metadata:
name: staging
metadata:
annotations:
config.kubernetes.io/function: |
container:
image: gcr.io/example/foo:v1.0.0
namespace: foo
fieldSpecs:
- path: metadata/namespace
create: true
annotations:
config.kubernetes.io/function: |
container:
image: gcr.io/example/foo:v1.0.0
data:
namespace: foo
fieldSpecs:
- path: metadata/namespace
create: true
`, string(output))
}
@@ -198,11 +200,13 @@ functionConfig:
kind: FulfillmentCenter
metadata:
name: staging
annotations:
config.kubernetes.io/function: |
container:
image: gcr.io/example/foo:v1.0.0
data:
metadata:
annotations:
config.kubernetes.io/function: |
container:
image: gcr.io/example/foo:v1.0.0
name: staging
items: []
`)
}
@@ -232,10 +236,12 @@ functionConfig:
kind: FulfillmentCenter
metadata:
name: staging
annotations:
config.kubernetes.io/function: |
container:
image: gcr.io/example/foo:v1.0.0
data:
metadata:
annotations:
config.kubernetes.io/function: |
container:
image: gcr.io/example/foo:v1.0.0
name: staging
`, string(output))
}

View File

@@ -21,18 +21,23 @@ func main() {
pluginHelpers := newPluginHelpers(resmapFactory)
resourceList := &framework.ResourceList{}
resourceList.FunctionConfig = map[string]interface{}{}
cmd := framework.Command(resourceList, func() error {
resMap, err := resmapFactory.NewResMapFromRNodeSlice(resourceList.Items)
if err != nil {
return err
}
pluginConfig, err := functionConfigToPluginConfig(resourceList.FunctionConfig)
dataField, err := getDataFromFunctionConfig(resourceList.FunctionConfig)
if err != nil {
return err
}
dataValue, err := yaml.Marshal(dataField)
if err != nil {
return err
}
err = plugin.Config(pluginHelpers, pluginConfig)
err = plugin.Config(pluginHelpers, dataValue)
if err != nil {
return err
}
@@ -72,6 +77,10 @@ func newResMapFactory() *resmap.Factory {
}
//nolint
func functionConfigToPluginConfig(fc interface{}) ([]byte, error) {
return yaml.Marshal(fc)
func getDataFromFunctionConfig(fc interface{}) (interface{}, error) {
f, ok := fc.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("function config %#v is not valid", fc)
}
return f["data"], nil
}