diff --git a/cmd/config/internal/commands/e2e/e2e_test.go b/cmd/config/internal/commands/e2e/e2e_test.go index f62ea336c..c74721d79 100644 --- a/cmd/config/internal/commands/e2e/e2e_test.go +++ b/cmd/config/internal/commands/e2e/e2e_test.go @@ -30,6 +30,7 @@ func TestRunE2e(t *testing.T) { expectedErr string skipIfFalseEnv string }{ + { name: "exec_function_no_args", args: func(d string) []string { @@ -63,6 +64,46 @@ metadata: }, }, + { + name: "exec_function_no_args_json", + args: func(d string) []string { + return []string{ + "--enable-exec", "--exec-path", filepath.Join(d, "e2econtainerconfig"), + } + }, + files: func(d string) map[string]string { + return map[string]string{ + "deployment.json": ` +{ + "apiVersion": "apps/v1", + "kind": "Deployment", + "metadata": { + "name": "foo" + } +} +`, + } + }, + expectedFiles: func(d string) map[string]string { + return map[string]string{ + "deployment.json": ` +{ + "apiVersion": "apps/v1", + "kind": "Deployment", + "metadata": { + "annotations": { + "a-bool-value": "false", + "a-int-value": "0", + "a-string-value": "" + }, + "name": "foo" + } +} +`, + } + }, + }, + { name: "exec_function_args", args: func(d string) []string { @@ -468,7 +509,6 @@ def run(r, fc): resource["metadata"]["annotations"]["a-string-value"] = fc["data"]["stringValue"] resource["metadata"]["annotations"]["a-int-value"] = fc["data"]["intValue"] resource["metadata"]["annotations"]["a-bool-value"] = fc["data"]["boolValue"] - run(ctx.resource_list["items"], ctx.resource_list["functionConfig"]) `, "config.yaml": ` @@ -545,7 +585,6 @@ def run(r, fc): resource["metadata"]["annotations"]["a-string-value"] = fc["data"]["stringValue"] resource["metadata"]["annotations"]["a-int-value"] = fc["data"]["intValue"] resource["metadata"]["annotations"]["a-bool-value"] = fc["data"]["boolValue"] - run(ctx.resource_list["items"], ctx.resource_list["functionConfig"]) `, "deployment.yaml": ` diff --git a/cmd/config/internal/commands/sink_test.go b/cmd/config/internal/commands/sink_test.go index cf70c6554..83fb47bf1 100644 --- a/cmd/config/internal/commands/sink_test.go +++ b/cmd/config/internal/commands/sink_test.go @@ -147,18 +147,9 @@ func TestSinkCommandJSON(t *testing.T) { r.Command.SetIn(bytes.NewBufferString(`apiVersion: config.kubernetes.io/v1alpha1 kind: ResourceList items: -- kind: Deployment - metadata: - labels: - app: nginx2 - name: foo - annotations: - app: nginx2 - config.kubernetes.io/index: '0' - config.kubernetes.io/path: 'f1.json' - config.kubernetes.io/format: 'json' - spec: - replicas: 1 +- {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo", + "annotations": {"app": "nginx2", config.kubernetes.io/format: 'json', config.kubernetes.io/index: '0', + config.kubernetes.io/path: 'f1.json'}}, "spec": {"replicas": 1}} `)) r.Command.SetArgs([]string{d}) if !assert.NoError(t, r.Command.Execute()) { @@ -317,17 +308,9 @@ func TestSinkCommandJSON_Stdout(t *testing.T) { r.Command.SetIn(bytes.NewBufferString(`apiVersion: config.kubernetes.io/v1alpha1 kind: ResourceList items: -- kind: Deployment - metadata: - labels: - app: nginx2 - name: foo - annotations: - app: nginx2 - config.kubernetes.io/index: '0' - config.kubernetes.io/format: 'json' - spec: - replicas: 1 +- {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo", + "annotations": {"app": "nginx2", config.kubernetes.io/format: 'json', config.kubernetes.io/index: '0', + config.kubernetes.io/path: 'f1.json'}}, "spec": {"replicas": 1}} `)) r.Command.SetOut(out) diff --git a/cmd/config/internal/commands/source_test.go b/cmd/config/internal/commands/source_test.go index 082306623..72d82270f 100644 --- a/cmd/config/internal/commands/source_test.go +++ b/cmd/config/internal/commands/source_test.go @@ -194,32 +194,14 @@ func TestSourceCommandJSON(t *testing.T) { if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1 kind: ResourceList items: -- kind: Deployment - metadata: - annotations: - app: nginx2 - config.kubernetes.io/format: 'json' - config.kubernetes.io/index: '0' - config.kubernetes.io/path: 'f1.json' - labels: - app: nginx2 - name: foo - spec: - replicas: 1 -- apiVersion: v1 - kind: Abstraction - metadata: - annotations: - config.kubernetes.io/function: | - container: - image: gcr.io/example/reconciler:v1 - config.kubernetes.io/local-config: "true" - config.kubernetes.io/format: 'json' - config.kubernetes.io/index: '0' - config.kubernetes.io/path: 'f2.json' - name: foo - spec: - replicas: 3 +- {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo", + "annotations": {"app": "nginx2", config.kubernetes.io/format: 'json', config.kubernetes.io/index: '0', + config.kubernetes.io/path: 'f1.json'}}, "spec": {"replicas": 1}} +- {"apiVersion": "v1", "kind": "Abstraction", "metadata": {"name": "foo", "annotations": { + "config.kubernetes.io/function": "container:\n image: gcr.io/example/reconciler:v1\n", + "config.kubernetes.io/local-config": "true", config.kubernetes.io/format: 'json', + config.kubernetes.io/index: '0', config.kubernetes.io/path: 'f2.json'}}, "spec": { + "replicas": 3}} `, b.String()) { return } @@ -326,17 +308,9 @@ func TestSourceCommandJSON_Stdin(t *testing.T) { if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1 kind: ResourceList items: -- kind: Deployment - metadata: - annotations: - app: nginx2 - config.kubernetes.io/format: 'json' - config.kubernetes.io/index: '0' - labels: - app: nginx2 - name: foo - spec: - replicas: 1 +- {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo", + "annotations": {"app": "nginx2", config.kubernetes.io/format: 'json', config.kubernetes.io/index: '0'}}, + "spec": {"replicas": 1}} `, out.String()) { return } diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index 86899d724..a3adcae41 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -130,17 +130,12 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) { if r.AcceptJSON && json.Valid([]byte(inputStr)) { // convert json to yaml string to generate resource list object // with appropriate format annotation - yamlValue, err := yaml.ConvertJSONToYAMLString(input.String()) - if err != nil { - return nil, err - } if r.SetAnnotations == nil { r.SetAnnotations = map[string]string{} } if !r.OmitReaderAnnotations { r.SetAnnotations[kioutil.FormatAnnotation] = JSON } - inputStr = yamlValue } values := strings.Split(inputStr, "\n---\n") diff --git a/kyaml/kio/pkgio_reader_test.go b/kyaml/kio/pkgio_reader_test.go index 62810277d..2b730b768 100644 --- a/kyaml/kio/pkgio_reader_test.go +++ b/kyaml/kio/pkgio_reader_test.go @@ -173,10 +173,11 @@ func TestLocalPackageReader_Read_JSON(t *testing.T) { } // TODO: Fix https://github.com/go-yaml/yaml/issues/44 so these are printed correctly expected := []string{ - `{"a": "b", metadata: {annotations: {config.kubernetes.io/index: '0', config.kubernetes.io/path: 'a_test.json'}}} + `{"a": "b", metadata: {annotations: {config.kubernetes.io/format: 'json', config.kubernetes.io/index: '0', + config.kubernetes.io/path: 'a_test.json'}}} `, - `{"e": "f", "g": {"h": ["i", "j"]}, metadata: {annotations: {config.kubernetes.io/index: '0', - config.kubernetes.io/path: 'b_test.json'}}} + `{"e": "f", "g": {"h": ["i", "j"]}, metadata: {annotations: {config.kubernetes.io/format: 'json', + config.kubernetes.io/index: '0', config.kubernetes.io/path: 'b_test.json'}}} `, } for i := range nodes { diff --git a/kyaml/runfn/runfn.go b/kyaml/runfn/runfn.go index 8564f343d..9b7c662ed 100644 --- a/kyaml/runfn/runfn.go +++ b/kyaml/runfn/runfn.go @@ -110,13 +110,13 @@ func (r RunFns) getNodesAndFilters() ( // the same one for reading must be used for writing if deleting Resources var outputPkg *kio.LocalPackageReadWriter if r.Path != "" { - outputPkg = &kio.LocalPackageReadWriter{PackagePath: r.Path} + outputPkg = &kio.LocalPackageReadWriter{PackagePath: r.Path, MatchFilesGlob: kio.MatchAll} } if r.Input == nil { p.Inputs = []kio.Reader{outputPkg} } else { - p.Inputs = []kio.Reader{&kio.ByteReader{Reader: r.Input}} + p.Inputs = []kio.Reader{&kio.ByteReader{Reader: r.Input, AcceptJSON: true}} } if err := p.Execute(); err != nil { return nil, nil, outputPkg, err diff --git a/kyaml/setters2/add.go b/kyaml/setters2/add.go index e12ed82d5..8517c66a4 100644 --- a/kyaml/setters2/add.go +++ b/kyaml/setters2/add.go @@ -183,7 +183,7 @@ func (sd SetterDefinition) Filter(object *yaml.RNode) (*yaml.RNode, error) { } if sd.Schema != "" { - schNode, err := yaml.ConvertJSONToYAMLNode(sd.Schema) + schNode, err := yaml.ConvertJSONToYamlNode(sd.Schema) if err != nil { return nil, err } diff --git a/kyaml/yaml/types.go b/kyaml/yaml/types.go index a1ab793e5..d74cb8471 100644 --- a/kyaml/yaml/types.go +++ b/kyaml/yaml/types.go @@ -722,33 +722,24 @@ func (rn *RNode) UnmarshalJSON(b []byte) error { return nil } -// ConvertJSONToYAMLNode parses input json string and returns equivalent yaml node -func ConvertJSONToYAMLNode(jsonStr string) (*RNode, error) { - yml, err := ConvertJSONToYAMLString(jsonStr) +// ConvertJSONToYamlNode parses input json string and returns equivalent yaml node +func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) { + var body map[string]interface{} + err := json.Unmarshal([]byte(jsonStr), &body) if err != nil { return nil, err } - node, err := Parse(yml) + yml, err := yaml.Marshal(body) + if err != nil { + return nil, err + } + node, err := Parse(string(yml)) if err != nil { return nil, err } return node, nil } -// ConvertJSONToYAMLString parses input json string and returns equivalent yaml string -func ConvertJSONToYAMLString(jsonStr string) (string, error) { - var body map[string]interface{} - err := json.Unmarshal([]byte(jsonStr), &body) - if err != nil { - return "", err - } - yml, err := yaml.Marshal(body) - if err != nil { - return "", err - } - return string(yml), nil -} - // checkKey returns true if all elems have the key func checkKey(key string, elems []*Node) bool { count := 0 diff --git a/kyaml/yaml/types_test.go b/kyaml/yaml/types_test.go index fa9aab215..b963d5b28 100644 --- a/kyaml/yaml/types_test.go +++ b/kyaml/yaml/types_test.go @@ -147,7 +147,7 @@ hello: world } } -func TestConvertJSONToYAMLNode(t *testing.T) { +func TestConvertJSONToYamlNode(t *testing.T) { inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}` expected := `enum: - allowedValue1 @@ -156,7 +156,7 @@ maxLength: 15 type: string ` - node, err := ConvertJSONToYAMLNode(inputJSON) + node, err := ConvertJSONToYamlNode(inputJSON) if !assert.NoError(t, err) { t.FailNow() } @@ -166,32 +166,3 @@ type: string } assert.Equal(t, expected, actual) } - -func TestConvertJSONToYAMLString(t *testing.T) { - inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}` - expected := `enum: - - allowedValue1 - - allowedValue2 -maxLength: 15 -type: string -` - - actual, err := ConvertJSONToYAMLString(inputJSON) - if !assert.NoError(t, err) { - t.FailNow() - } - assert.Equal(t, expected, actual) -} - -// error if there are multiple json blobs in input string -func TestConvertJSONToYAMLStringError(t *testing.T) { - inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]} -{"type": "string", "maxLength": 5, "enum": ["allowedValue2", "allowedValue3"]}` - expected := `invalid character '{' after top-level value` - - _, err := ConvertJSONToYAMLString(inputJSON) - if !assert.Error(t, err) { - t.FailNow() - } - assert.Equal(t, expected, err.Error()) -}