From 4cd39448605b1aaf548c107ed9aa83b3ac647e4d Mon Sep 17 00:00:00 2001 From: Phani Teja Marupaka Date: Wed, 27 May 2020 17:45:15 -0700 Subject: [PATCH 1/6] Include json files for fn source --- cmd/config/internal/commands/source.go | 2 +- cmd/config/internal/commands/source_test.go | 60 ++++++++++++--------- kyaml/kio/byteio_reader.go | 12 ++++- kyaml/kio/pkgio_reader.go | 13 +++++ kyaml/yaml/types.go | 23 +++++--- kyaml/yaml/types_test.go | 16 ++++++ 6 files changed, 93 insertions(+), 33 deletions(-) diff --git a/cmd/config/internal/commands/source.go b/cmd/config/internal/commands/source.go index 27382bb2f..555e2d660 100644 --- a/cmd/config/internal/commands/source.go +++ b/cmd/config/internal/commands/source.go @@ -71,7 +71,7 @@ func (r *SourceRunner) runE(c *cobra.Command, args []string) error { var inputs []kio.Reader for _, a := range args { - inputs = append(inputs, kio.LocalPackageReader{PackagePath: a}) + inputs = append(inputs, kio.LocalPackageReader{PackagePath: a, IncludeJSON: true}) } if len(inputs) == 0 { inputs = []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin()}} diff --git a/cmd/config/internal/commands/source_test.go b/cmd/config/internal/commands/source_test.go index 141541143..b7f77d2ca 100644 --- a/cmd/config/internal/commands/source_test.go +++ b/cmd/config/internal/commands/source_test.go @@ -21,25 +21,37 @@ func TestSourceCommand(t *testing.T) { } defer os.RemoveAll(d) - err = ioutil.WriteFile(filepath.Join(d, "f1.yaml"), []byte(` -kind: Deployment -metadata: - labels: - app: nginx2 - name: foo - annotations: - app: nginx2 -spec: - replicas: 1 + err = ioutil.WriteFile(filepath.Join(d, "f1.json"), []byte(` +{ + "kind": "Deployment", + "metadata": { + "labels": { + "app": "nginx2" + }, + "name": "foo", + "annotations": { + "app": "nginx2" + } + }, + "spec": { + "replicas": 1 + } +} --- -kind: Service -metadata: - name: foo - annotations: - app: nginx -spec: - selector: - app: nginx +{ + "kind": "Service", + "metadata": { + "name": "foo", + "annotations": { + "app": "nginx" + } + }, + "spec": { + "selector": { + "app": "nginx" + } + } +} `), 0600) if !assert.NoError(t, err) { return @@ -86,22 +98,22 @@ kind: ResourceList items: - kind: Deployment metadata: - labels: - app: nginx2 - name: foo annotations: app: nginx2 config.kubernetes.io/index: '0' - config.kubernetes.io/path: 'f1.yaml' + config.kubernetes.io/path: 'f1.json' + labels: + app: nginx2 + name: foo spec: replicas: 1 - kind: Service metadata: - name: foo annotations: app: nginx config.kubernetes.io/index: '1' - config.kubernetes.io/path: 'f1.yaml' + config.kubernetes.io/path: 'f1.json' + name: foo spec: selector: app: nginx diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index 670fb96f5..662f04437 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -101,6 +101,9 @@ type ByteReader struct { // WrappingKind is set by Read(), and is the kind of the object that // the read objects were originally wrapped in. WrappingKind string + + // JSON indicates if the input file source is json + JSON bool } var _ Reader = &ByteReader{} @@ -119,7 +122,14 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) { index := 0 for i := range values { - decoder := yaml.NewDecoder(bytes.NewBufferString(values[i])) + value := values[i] + if r.JSON { + value, err = yaml.ConvertJSONToYamlString(value) + if err != nil { + return nil, err + } + } + decoder := yaml.NewDecoder(bytes.NewBufferString(value)) node, err := r.decode(index, decoder) if err == io.EOF { continue diff --git a/kyaml/kio/pkgio_reader.go b/kyaml/kio/pkgio_reader.go index 854c8d392..0392949d6 100644 --- a/kyaml/kio/pkgio_reader.go +++ b/kyaml/kio/pkgio_reader.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/kio/kioutil" @@ -162,10 +163,18 @@ type LocalPackageReader struct { // SetAnnotations are annotations to set on the Resources as they are read. SetAnnotations map[string]string `yaml:"setAnnotations,omitempty"` + + // IncludeJSON indicates if the json resources files must be included during read + IncludeJSON bool } var _ Reader = LocalPackageReader{} +const ( + JSONFilePattern = "*.json" + JSONSuffix = ".json" +) + var DefaultMatch = []string{"*.yaml", "*.yml"} // Read reads the Resources. @@ -178,6 +187,9 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) { r.PackagePath = filepath.ToSlash(r.PackagePath) if len(r.MatchFilesGlob) == 0 { r.MatchFilesGlob = DefaultMatch + if r.IncludeJSON { + r.MatchFilesGlob = append(r.MatchFilesGlob, JSONFilePattern) + } } var operand ResourceNodeSlice @@ -243,6 +255,7 @@ func (r *LocalPackageReader) readFile(path string, _ os.FileInfo) ([]*yaml.RNode Reader: f, OmitReaderAnnotations: r.OmitReaderAnnotations, SetAnnotations: r.SetAnnotations, + JSON: strings.HasSuffix(path, JSONSuffix), } return rr.Read() } diff --git a/kyaml/yaml/types.go b/kyaml/yaml/types.go index d74cb8471..bf2e9cacc 100644 --- a/kyaml/yaml/types.go +++ b/kyaml/yaml/types.go @@ -724,22 +724,31 @@ func (rn *RNode) UnmarshalJSON(b []byte) error { // 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) + yml, err := ConvertJSONToYamlString(jsonStr) if err != nil { return nil, err } - yml, err := yaml.Marshal(body) - if err != nil { - return nil, err - } - node, err := Parse(string(yml)) + node, err := Parse(yml) if err != nil { return nil, err } return node, nil } +// ConvertJSONToYamlNode 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 b963d5b28..52c1eb5ea 100644 --- a/kyaml/yaml/types_test.go +++ b/kyaml/yaml/types_test.go @@ -166,3 +166,19 @@ 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) +} From a158eeaaff15880447cff46e2ebd038d6092aeeb Mon Sep 17 00:00:00 2001 From: Phani Teja Marupaka Date: Thu, 28 May 2020 15:46:58 -0700 Subject: [PATCH 2/6] Suggested changes --- cmd/config/internal/commands/source.go | 2 +- kyaml/kio/byteio_reader.go | 6 +++--- kyaml/kio/pkgio_reader.go | 16 ++++------------ 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/cmd/config/internal/commands/source.go b/cmd/config/internal/commands/source.go index 555e2d660..7873956ce 100644 --- a/cmd/config/internal/commands/source.go +++ b/cmd/config/internal/commands/source.go @@ -71,7 +71,7 @@ func (r *SourceRunner) runE(c *cobra.Command, args []string) error { var inputs []kio.Reader for _, a := range args { - inputs = append(inputs, kio.LocalPackageReader{PackagePath: a, IncludeJSON: true}) + inputs = append(inputs, kio.LocalPackageReader{PackagePath: a, MatchFilesGlob: kio.MatchAll}) } if len(inputs) == 0 { inputs = []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin()}} diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index 662f04437..c91ea5183 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -102,8 +102,8 @@ type ByteReader struct { // the read objects were originally wrapped in. WrappingKind string - // JSON indicates if the input file source is json - JSON bool + // Path indicates file path which is being read + Path string } var _ Reader = &ByteReader{} @@ -123,7 +123,7 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) { index := 0 for i := range values { value := values[i] - if r.JSON { + if strings.HasSuffix(r.Path, JSONSuffix) { value, err = yaml.ConvertJSONToYamlString(value) if err != nil { return nil, err diff --git a/kyaml/kio/pkgio_reader.go b/kyaml/kio/pkgio_reader.go index 0392949d6..17fc81fa8 100644 --- a/kyaml/kio/pkgio_reader.go +++ b/kyaml/kio/pkgio_reader.go @@ -7,7 +7,6 @@ import ( "fmt" "os" "path/filepath" - "strings" "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/kio/kioutil" @@ -163,19 +162,15 @@ type LocalPackageReader struct { // SetAnnotations are annotations to set on the Resources as they are read. SetAnnotations map[string]string `yaml:"setAnnotations,omitempty"` - - // IncludeJSON indicates if the json resources files must be included during read - IncludeJSON bool } var _ Reader = LocalPackageReader{} -const ( - JSONFilePattern = "*.json" - JSONSuffix = ".json" -) +const JSONSuffix = ".json" var DefaultMatch = []string{"*.yaml", "*.yml"} +var JSONMatch = []string{"*.json"} +var MatchAll = append(DefaultMatch, JSONMatch...) // Read reads the Resources. func (r LocalPackageReader) Read() ([]*yaml.RNode, error) { @@ -187,9 +182,6 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) { r.PackagePath = filepath.ToSlash(r.PackagePath) if len(r.MatchFilesGlob) == 0 { r.MatchFilesGlob = DefaultMatch - if r.IncludeJSON { - r.MatchFilesGlob = append(r.MatchFilesGlob, JSONFilePattern) - } } var operand ResourceNodeSlice @@ -255,7 +247,7 @@ func (r *LocalPackageReader) readFile(path string, _ os.FileInfo) ([]*yaml.RNode Reader: f, OmitReaderAnnotations: r.OmitReaderAnnotations, SetAnnotations: r.SetAnnotations, - JSON: strings.HasSuffix(path, JSONSuffix), + Path: path, } return rr.Read() } From dc4bf03da2565e07163d1a5b1612c9d90e339898 Mon Sep 17 00:00:00 2001 From: Phani Teja Marupaka Date: Mon, 1 Jun 2020 16:59:50 -0700 Subject: [PATCH 3/6] Write json files in sink --- cmd/config/internal/commands/sink_test.go | 25 ++++------------- kyaml/kio/byteio_reader.go | 18 ++++++++++--- kyaml/kio/byteio_writer.go | 33 ++++++++++++++++++++--- kyaml/kio/pkgio_reader.go | 2 -- kyaml/kio/pkgio_writer.go | 1 + kyaml/yaml/types.go | 16 ++++++++++- kyaml/yaml/types_test.go | 18 +++++++++++++ 7 files changed, 82 insertions(+), 31 deletions(-) diff --git a/cmd/config/internal/commands/sink_test.go b/cmd/config/internal/commands/sink_test.go index 9fc4d869c..3e992ac3b 100644 --- a/cmd/config/internal/commands/sink_test.go +++ b/cmd/config/internal/commands/sink_test.go @@ -33,7 +33,7 @@ items: annotations: app: nginx2 config.kubernetes.io/index: '0' - config.kubernetes.io/path: 'f1.yaml' + config.kubernetes.io/path: 'f1.json' spec: replicas: 1 - kind: Service @@ -42,7 +42,7 @@ items: annotations: app: nginx config.kubernetes.io/index: '1' - config.kubernetes.io/path: 'f1.yaml' + config.kubernetes.io/path: 'f1.json' spec: selector: app: nginx @@ -77,28 +77,13 @@ items: t.FailNow() } - actual, err := ioutil.ReadFile(filepath.Join(d, "f1.yaml")) + actual, err := ioutil.ReadFile(filepath.Join(d, "f1.json")) if !assert.NoError(t, err) { t.FailNow() } - expected := `kind: Deployment -metadata: - labels: - app: nginx2 - name: foo - annotations: - app: nginx2 -spec: - replicas: 1 + expected := `'{"kind":"Deployment","metadata":{"annotations":{"app":"nginx2"},"labels":{"app":"nginx2"},"name":"foo"},"spec":{"replicas":1}}' --- -kind: Service -metadata: - name: foo - annotations: - app: nginx -spec: - selector: - app: nginx +'{"kind":"Service","metadata":{"annotations":{"app":"nginx"},"name":"foo"},"spec":{"selector":{"app":"nginx"}}}' ` if !assert.Equal(t, expected, string(actual)) { t.FailNow() diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index c91ea5183..e49dae6b6 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -7,6 +7,7 @@ import ( "bytes" "fmt" "io" + "path/filepath" "sort" "strings" @@ -103,6 +104,7 @@ type ByteReader struct { WrappingKind string // Path indicates file path which is being read + // empty if it is being read from stdin Path string } @@ -123,12 +125,20 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) { index := 0 for i := range values { value := values[i] - if strings.HasSuffix(r.Path, JSONSuffix) { - value, err = yaml.ConvertJSONToYamlString(value) - if err != nil { - return nil, err + + if r.Path != "" { + for _, g := range JSONMatch { + if match, err := filepath.Match(g, filepath.Ext(r.Path)); err != nil { + return nil, errors.Wrap(err) + } else if match { + value, err = yaml.ConvertJSONToYamlString(value) + if err != nil { + return nil, err + } + } } } + decoder := yaml.NewDecoder(bytes.NewBufferString(value)) node, err := r.decode(index, decoder) if err == io.EOF { diff --git a/kyaml/kio/byteio_writer.go b/kyaml/kio/byteio_writer.go index 1d03cfb1d..e44c52a7c 100644 --- a/kyaml/kio/byteio_writer.go +++ b/kyaml/kio/byteio_writer.go @@ -5,6 +5,7 @@ package kio import ( "io" + "path/filepath" "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/kio/kioutil" @@ -42,6 +43,9 @@ type ByteWriter struct { // Sort if set, will cause ByteWriter to sort the the nodes before writing them. Sort bool + + // Path is the output file path to write to + Path string } var _ Writer = ByteWriter{} @@ -90,9 +94,8 @@ func (w ByteWriter) Write(nodes []*yaml.RNode) error { // don't wrap the elements if w.WrappingKind == "" { for i := range nodes { - err := encoder.Encode(nodes[i].Document()) - if err != nil { - return errors.Wrap(err) + if err := w.encode(encoder, nodes[i]); err != nil { + return err } } return nil @@ -125,7 +128,29 @@ func (w ByteWriter) Write(nodes []*yaml.RNode) error { for i := range nodes { items.Content = append(items.Content, nodes[i].YNode()) } - err := errors.Wrap(encoder.Encode(doc)) + rNode := yaml.RNode{} + rNode.SetYNode(doc) + err := w.encode(encoder, &rNode) yaml.UndoSerializationHacksOnNodes(nodes) return err } + +// encode encodes the input RNode to appropriate node format based on file path extension +func (w ByteWriter) encode(encoder *yaml.Encoder, node *yaml.RNode) error { + for _, g := range JSONMatch { + if match, err := filepath.Match(g, filepath.Ext(w.Path)); err != nil { + return errors.Wrap(err) + } else if match { + json, err := yaml.ConvertYamlNodeToJSONString(node) + if err != nil { + return errors.Wrap(err) + } + err = encoder.Encode(json) + if err != nil { + return errors.Wrap(err) + } + return nil + } + } + return encoder.Encode(node.Document()) +} diff --git a/kyaml/kio/pkgio_reader.go b/kyaml/kio/pkgio_reader.go index 17fc81fa8..f02aa54a3 100644 --- a/kyaml/kio/pkgio_reader.go +++ b/kyaml/kio/pkgio_reader.go @@ -166,8 +166,6 @@ type LocalPackageReader struct { var _ Reader = LocalPackageReader{} -const JSONSuffix = ".json" - var DefaultMatch = []string{"*.yaml", "*.yml"} var JSONMatch = []string{"*.json"} var MatchAll = append(DefaultMatch, JSONMatch...) diff --git a/kyaml/kio/pkgio_writer.go b/kyaml/kio/pkgio_writer.go index b671512e3..dbbc52fd1 100644 --- a/kyaml/kio/pkgio_writer.go +++ b/kyaml/kio/pkgio_writer.go @@ -98,6 +98,7 @@ func (r LocalPackageWriter) Write(nodes []*yaml.RNode) error { Writer: f, KeepReaderAnnotations: r.KeepReaderAnnotations, ClearAnnotations: r.ClearAnnotations, + Path: outputPath, } if err = w.Write(outputFiles[path]); err != nil { return errors.Wrap(err) diff --git a/kyaml/yaml/types.go b/kyaml/yaml/types.go index bf2e9cacc..9d715337a 100644 --- a/kyaml/yaml/types.go +++ b/kyaml/yaml/types.go @@ -735,7 +735,7 @@ func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) { return node, nil } -// ConvertJSONToYamlNode parses input json string and returns equivalent yaml string +// 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) @@ -749,6 +749,20 @@ func ConvertJSONToYamlString(jsonStr string) (string, error) { return string(yml), nil } +// ConvertYamlNodeToJSONString returns json string from input yaml RNode +func ConvertYamlNodeToJSONString(node *RNode) (string, error) { + nodeStr, err := node.String() + if err != nil { + return "", err + } + var body interface{} + if err := yaml.Unmarshal([]byte(nodeStr), &body); err != nil { + return "", err + } + res, err := json.Marshal(body) + return string(res), err +} + // 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 52c1eb5ea..23a2a3873 100644 --- a/kyaml/yaml/types_test.go +++ b/kyaml/yaml/types_test.go @@ -182,3 +182,21 @@ type: string } assert.Equal(t, expected, actual) } + +func TestConvertYamlNodeToJSONStr(t *testing.T) { + yl := `enum: + - allowedValue1 + - allowedValue2 +maxLength: 15 +type: string +` + node, err := Parse(yl) + if !assert.NoError(t, err) { + t.FailNow() + } + res, err := ConvertYamlNodeToJSONString(node) + if !assert.NoError(t, err) { + t.FailNow() + } + assert.Equal(t, `{"enum":["allowedValue1","allowedValue2"],"maxLength":15,"type":"string"}`, res) +} From b39c522cc1b76a874729343eb366dd9e5adcce30 Mon Sep 17 00:00:00 2001 From: Phani Teja Marupaka Date: Sun, 7 Jun 2020 15:13:21 -0700 Subject: [PATCH 4/6] Suggested changes --- cmd/config/internal/commands/sink_test.go | 133 ++++++++++++- cmd/config/internal/commands/source.go | 2 +- cmd/config/internal/commands/source_test.go | 203 ++++++++++++++++---- kyaml/kio/byteio_reader.go | 46 +++-- kyaml/kio/byteio_writer.go | 26 +-- kyaml/kio/kioutil/kioutil.go | 10 +- kyaml/kio/pkgio_reader.go | 19 +- kyaml/kio/pkgio_writer.go | 1 - kyaml/setters2/add.go | 2 +- kyaml/setters2/set.go | 4 +- kyaml/yaml/types.go | 24 +-- kyaml/yaml/types_test.go | 31 ++- 12 files changed, 379 insertions(+), 122 deletions(-) diff --git a/cmd/config/internal/commands/sink_test.go b/cmd/config/internal/commands/sink_test.go index 3e992ac3b..cf70c6554 100644 --- a/cmd/config/internal/commands/sink_test.go +++ b/cmd/config/internal/commands/sink_test.go @@ -33,7 +33,7 @@ items: annotations: app: nginx2 config.kubernetes.io/index: '0' - config.kubernetes.io/path: 'f1.json' + config.kubernetes.io/path: 'f1.yaml' spec: replicas: 1 - kind: Service @@ -42,7 +42,7 @@ items: annotations: app: nginx config.kubernetes.io/index: '1' - config.kubernetes.io/path: 'f1.json' + config.kubernetes.io/path: 'f1.yaml' spec: selector: app: nginx @@ -77,13 +77,28 @@ items: t.FailNow() } - actual, err := ioutil.ReadFile(filepath.Join(d, "f1.json")) + actual, err := ioutil.ReadFile(filepath.Join(d, "f1.yaml")) if !assert.NoError(t, err) { t.FailNow() } - expected := `'{"kind":"Deployment","metadata":{"annotations":{"app":"nginx2"},"labels":{"app":"nginx2"},"name":"foo"},"spec":{"replicas":1}}' + expected := `kind: Deployment +metadata: + labels: + app: nginx2 + name: foo + annotations: + app: nginx2 +spec: + replicas: 1 --- -'{"kind":"Service","metadata":{"annotations":{"app":"nginx"},"name":"foo"},"spec":{"selector":{"app":"nginx"}}}' +kind: Service +metadata: + name: foo + annotations: + app: nginx +spec: + selector: + app: nginx ` if !assert.Equal(t, expected, string(actual)) { t.FailNow() @@ -121,6 +136,60 @@ spec: } } +func TestSinkCommandJSON(t *testing.T) { + d, err := ioutil.TempDir("", "kustomize-source-test") + if !assert.NoError(t, err) { + t.FailNow() + } + defer os.RemoveAll(d) + + r := commands.GetSinkRunner("") + 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 +`)) + r.Command.SetArgs([]string{d}) + if !assert.NoError(t, r.Command.Execute()) { + t.FailNow() + } + + actual, err := ioutil.ReadFile(filepath.Join(d, "f1.json")) + if !assert.NoError(t, err) { + t.FailNow() + } + expected := `{ + "kind": "Deployment", + "metadata": { + "annotations": { + "app": "nginx2" + }, + "labels": { + "app": "nginx2" + }, + "name": "foo" + }, + "spec": { + "replicas": 1 + } +} +` + if !assert.Equal(t, expected, string(actual)) { + t.FailNow() + } +} + func TestSinkCommand_Stdout(t *testing.T) { d, err := ioutil.TempDir("", "kustomize-source-test") if !assert.NoError(t, err) { @@ -234,3 +303,57 @@ spec: t.FailNow() } } + +func TestSinkCommandJSON_Stdout(t *testing.T) { + d, err := ioutil.TempDir("", "kustomize-source-test") + if !assert.NoError(t, err) { + t.FailNow() + } + defer os.RemoveAll(d) + + // fmt the files + out := &bytes.Buffer{} + r := commands.GetSinkRunner("") + 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 +`)) + + r.Command.SetOut(out) + r.Command.SetArgs([]string{}) + if !assert.NoError(t, r.Command.Execute()) { + t.FailNow() + } + + expected := `{ + "kind": "Deployment", + "metadata": { + "annotations": { + "app": "nginx2" + }, + "labels": { + "app": "nginx2" + }, + "name": "foo" + }, + "spec": { + "replicas": 1 + } +} +` + if !assert.Equal(t, expected, out.String()) { + println(out.String()) + t.FailNow() + } +} diff --git a/cmd/config/internal/commands/source.go b/cmd/config/internal/commands/source.go index 7873956ce..8dc6296ae 100644 --- a/cmd/config/internal/commands/source.go +++ b/cmd/config/internal/commands/source.go @@ -74,7 +74,7 @@ func (r *SourceRunner) runE(c *cobra.Command, args []string) error { inputs = append(inputs, kio.LocalPackageReader{PackagePath: a, MatchFilesGlob: kio.MatchAll}) } if len(inputs) == 0 { - inputs = []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin()}} + inputs = []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin(), AcceptJSON: true}} } err := kio.Pipeline{Inputs: inputs, Outputs: outputs}.Execute() diff --git a/cmd/config/internal/commands/source_test.go b/cmd/config/internal/commands/source_test.go index b7f77d2ca..082306623 100644 --- a/cmd/config/internal/commands/source_test.go +++ b/cmd/config/internal/commands/source_test.go @@ -21,37 +21,25 @@ func TestSourceCommand(t *testing.T) { } defer os.RemoveAll(d) - err = ioutil.WriteFile(filepath.Join(d, "f1.json"), []byte(` -{ - "kind": "Deployment", - "metadata": { - "labels": { - "app": "nginx2" - }, - "name": "foo", - "annotations": { - "app": "nginx2" - } - }, - "spec": { - "replicas": 1 - } -} + err = ioutil.WriteFile(filepath.Join(d, "f1.yaml"), []byte(` +kind: Deployment +metadata: + labels: + app: nginx2 + name: foo + annotations: + app: nginx2 +spec: + replicas: 1 --- -{ - "kind": "Service", - "metadata": { - "name": "foo", - "annotations": { - "app": "nginx" - } - }, - "spec": { - "selector": { - "app": "nginx" - } - } -} +kind: Service +metadata: + name: foo + annotations: + app: nginx +spec: + selector: + app: nginx `), 0600) if !assert.NoError(t, err) { return @@ -98,22 +86,22 @@ kind: ResourceList items: - kind: Deployment metadata: - annotations: - app: nginx2 - config.kubernetes.io/index: '0' - config.kubernetes.io/path: 'f1.json' labels: app: nginx2 name: foo + annotations: + app: nginx2 + config.kubernetes.io/index: '0' + config.kubernetes.io/path: 'f1.yaml' spec: replicas: 1 - kind: Service metadata: + name: foo annotations: app: nginx config.kubernetes.io/index: '1' - config.kubernetes.io/path: 'f1.json' - name: foo + config.kubernetes.io/path: 'f1.yaml' spec: selector: app: nginx @@ -147,6 +135,96 @@ items: } } +func TestSourceCommandJSON(t *testing.T) { + d, err := ioutil.TempDir("", "kustomize-source-test") + if !assert.NoError(t, err) { + return + } + defer os.RemoveAll(d) + + err = ioutil.WriteFile(filepath.Join(d, "f1.json"), []byte(` +{ + "kind": "Deployment", + "metadata": { + "labels": { + "app": "nginx2" + }, + "name": "foo", + "annotations": { + "app": "nginx2" + } + }, + "spec": { + "replicas": 1 + } +} +`), 0600) + if !assert.NoError(t, err) { + return + } + err = ioutil.WriteFile(filepath.Join(d, "f2.json"), []byte(` +{ + "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" + } + }, + "spec": { + "replicas": 3 + } +} +`), 0600) + if !assert.NoError(t, err) { + return + } + + // fmt the files + b := &bytes.Buffer{} + r := commands.GetSourceRunner("") + r.Command.SetArgs([]string{d}) + r.Command.SetOut(b) + if !assert.NoError(t, r.Command.Execute()) { + return + } + + 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 +`, b.String()) { + return + } +} + func TestSourceCommand_Stdin(t *testing.T) { d, err := ioutil.TempDir("", "kustomize-source-test") if !assert.NoError(t, err) { @@ -210,3 +288,56 @@ items: return } } + +func TestSourceCommandJSON_Stdin(t *testing.T) { + d, err := ioutil.TempDir("", "kustomize-source-test") + if !assert.NoError(t, err) { + return + } + defer os.RemoveAll(d) + + in := bytes.NewBufferString(` +{ + "kind": "Deployment", + "metadata": { + "labels": { + "app": "nginx2" + }, + "name": "foo", + "annotations": { + "app": "nginx2" + } + }, + "spec": { + "replicas": 1 + } +} +`) + + out := &bytes.Buffer{} + r := commands.GetSourceRunner("") + r.Command.SetArgs([]string{}) + r.Command.SetIn(in) + r.Command.SetOut(out) + if !assert.NoError(t, r.Command.Execute()) { + return + } + + 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 +`, out.String()) { + return + } +} diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index e49dae6b6..86899d724 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -5,9 +5,9 @@ package kio import ( "bytes" + "encoding/json" "fmt" "io" - "path/filepath" "sort" "strings" @@ -103,11 +103,15 @@ type ByteReader struct { // the read objects were originally wrapped in. WrappingKind string - // Path indicates file path which is being read - // empty if it is being read from stdin - Path string + // AcceptJSON indicates if the input json bytes should be processed + AcceptJSON bool } +const ( + YAML = "yaml" + JSON = "json" +) + var _ Reader = &ByteReader{} func (r *ByteReader) Read() ([]*yaml.RNode, error) { @@ -120,25 +124,31 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) { if err != nil { return nil, errors.Wrap(err) } - values := strings.Split(input.String(), "\n---\n") + inputStr := input.String() + + // check if json is accepted and if input bytes are in json format + 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") index := 0 for i := range values { value := values[i] - if r.Path != "" { - for _, g := range JSONMatch { - if match, err := filepath.Match(g, filepath.Ext(r.Path)); err != nil { - return nil, errors.Wrap(err) - } else if match { - value, err = yaml.ConvertJSONToYamlString(value) - if err != nil { - return nil, err - } - } - } - } - decoder := yaml.NewDecoder(bytes.NewBufferString(value)) node, err := r.decode(index, decoder) if err == io.EOF { diff --git a/kyaml/kio/byteio_writer.go b/kyaml/kio/byteio_writer.go index e44c52a7c..72b80331f 100644 --- a/kyaml/kio/byteio_writer.go +++ b/kyaml/kio/byteio_writer.go @@ -4,8 +4,8 @@ package kio import ( + "encoding/json" "io" - "path/filepath" "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/kio/kioutil" @@ -43,9 +43,6 @@ type ByteWriter struct { // Sort if set, will cause ByteWriter to sort the the nodes before writing them. Sort bool - - // Path is the output file path to write to - Path string } var _ Writer = ByteWriter{} @@ -137,20 +134,17 @@ func (w ByteWriter) Write(nodes []*yaml.RNode) error { // encode encodes the input RNode to appropriate node format based on file path extension func (w ByteWriter) encode(encoder *yaml.Encoder, node *yaml.RNode) error { - for _, g := range JSONMatch { - if match, err := filepath.Match(g, filepath.Ext(w.Path)); err != nil { + _, _, format, err := kioutil.GetFileAnnotations(node) + if !w.KeepReaderAnnotations { + _, err := node.Pipe(yaml.ClearAnnotation(kioutil.FormatAnnotation)) + if err != nil { return errors.Wrap(err) - } else if match { - json, err := yaml.ConvertYamlNodeToJSONString(node) - if err != nil { - return errors.Wrap(err) - } - err = encoder.Encode(json) - if err != nil { - return errors.Wrap(err) - } - return nil } } + if err == nil && format == JSON { + je := json.NewEncoder(w.Writer) + je.SetIndent("", " ") + return errors.Wrap(je.Encode(node)) + } return encoder.Encode(node.Document()) } diff --git a/kyaml/kio/kioutil/kioutil.go b/kyaml/kio/kioutil/kioutil.go index 1d5e3bf58..8839b7ca4 100644 --- a/kyaml/kio/kioutil/kioutil.go +++ b/kyaml/kio/kioutil/kioutil.go @@ -22,16 +22,20 @@ const ( // PathAnnotation records the path to the file the Resource was read from PathAnnotation AnnotationKey = "config.kubernetes.io/path" + + // FormatAnnotation records the format of the resource ex: json + FormatAnnotation AnnotationKey = "config.kubernetes.io/format" ) -func GetFileAnnotations(rn *yaml.RNode) (string, string, error) { +func GetFileAnnotations(rn *yaml.RNode) (string, string, string, error) { meta, err := rn.GetMeta() if err != nil { - return "", "", err + return "", "", "", err } path := meta.Annotations[PathAnnotation] index := meta.Annotations[IndexAnnotation] - return path, index, nil + format := meta.Annotations[FormatAnnotation] + return path, index, format, nil } // ErrorIfMissingAnnotation validates the provided annotations are present on the given resources diff --git a/kyaml/kio/pkgio_reader.go b/kyaml/kio/pkgio_reader.go index f02aa54a3..3b6fa9c3d 100644 --- a/kyaml/kio/pkgio_reader.go +++ b/kyaml/kio/pkgio_reader.go @@ -123,7 +123,7 @@ func (r *LocalPackageReadWriter) Write(nodes []*yaml.RNode) error { func (r *LocalPackageReadWriter) getFiles(nodes []*yaml.RNode) (sets.String, error) { val := sets.String{} for _, n := range nodes { - path, _, err := kioutil.GetFileAnnotations(n) + path, _, _, err := kioutil.GetFileAnnotations(n) if err != nil { return nil, errors.Wrap(err) } @@ -240,16 +240,31 @@ func (r *LocalPackageReader) readFile(path string, _ os.FileInfo) ([]*yaml.RNode return nil, err } defer f.Close() + rr := &ByteReader{ DisableUnwrapping: true, Reader: f, OmitReaderAnnotations: r.OmitReaderAnnotations, SetAnnotations: r.SetAnnotations, - Path: path, + AcceptJSON: r.acceptJSON(path), } return rr.Read() } +// acceptJSON returns true if the input path has json ext and if the MatchFilesGlob +// has any of the JSONMatch ext +func (r *LocalPackageReader) acceptJSON(path string) bool { + for _, gm := range r.MatchFilesGlob { + for _, jm := range JSONMatch { + if filepath.Ext(path) == filepath.Ext(jm) && + filepath.Ext(gm) == filepath.Ext(jm) { + return true + } + } + } + return false +} + // ShouldSkipFile returns true if the file should be skipped func (r *LocalPackageReader) ShouldSkipFile(info os.FileInfo) (bool, error) { // check if the files are in scope diff --git a/kyaml/kio/pkgio_writer.go b/kyaml/kio/pkgio_writer.go index dbbc52fd1..b671512e3 100644 --- a/kyaml/kio/pkgio_writer.go +++ b/kyaml/kio/pkgio_writer.go @@ -98,7 +98,6 @@ func (r LocalPackageWriter) Write(nodes []*yaml.RNode) error { Writer: f, KeepReaderAnnotations: r.KeepReaderAnnotations, ClearAnnotations: r.ClearAnnotations, - Path: outputPath, } if err = w.Write(outputFiles[path]); err != nil { return errors.Wrap(err) diff --git a/kyaml/setters2/add.go b/kyaml/setters2/add.go index 8517c66a4..e12ed82d5 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/setters2/set.go b/kyaml/setters2/set.go index c95b0f0bc..8e08cd83d 100644 --- a/kyaml/setters2/set.go +++ b/kyaml/setters2/set.go @@ -447,7 +447,7 @@ func SetAll(s *Set) kio.Filter { return nil, errors.Wrap(err) } if s.Count > preCount { - path, _, err := kioutil.GetFileAnnotations(nodes[i]) + path, _, _, err := kioutil.GetFileAnnotations(nodes[i]) if err != nil { return nil, errors.Wrap(err) } @@ -457,7 +457,7 @@ func SetAll(s *Set) kio.Filter { var nodesInUpdatedFiles []*yaml.RNode // return only the nodes whose corresponding file has at least one node with input setter for i := range nodes { - path, _, err := kioutil.GetFileAnnotations(nodes[i]) + path, _, _, err := kioutil.GetFileAnnotations(nodes[i]) if err != nil { return nil, errors.Wrap(err) } diff --git a/kyaml/yaml/types.go b/kyaml/yaml/types.go index 9d715337a..a1ab793e5 100644 --- a/kyaml/yaml/types.go +++ b/kyaml/yaml/types.go @@ -722,9 +722,9 @@ 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) { + yml, err := ConvertJSONToYAMLString(jsonStr) if err != nil { return nil, err } @@ -735,8 +735,8 @@ func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) { return node, nil } -// ConvertJSONToYamlString parses input json string and returns equivalent yaml string -func ConvertJSONToYamlString(jsonStr string) (string, error) { +// 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 { @@ -749,20 +749,6 @@ func ConvertJSONToYamlString(jsonStr string) (string, error) { return string(yml), nil } -// ConvertYamlNodeToJSONString returns json string from input yaml RNode -func ConvertYamlNodeToJSONString(node *RNode) (string, error) { - nodeStr, err := node.String() - if err != nil { - return "", err - } - var body interface{} - if err := yaml.Unmarshal([]byte(nodeStr), &body); err != nil { - return "", err - } - res, err := json.Marshal(body) - return string(res), err -} - // 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 23a2a3873..fa9aab215 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() } @@ -167,7 +167,7 @@ type: string assert.Equal(t, expected, actual) } -func TestConvertJSONToYamlString(t *testing.T) { +func TestConvertJSONToYAMLString(t *testing.T) { inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}` expected := `enum: - allowedValue1 @@ -176,27 +176,22 @@ maxLength: 15 type: string ` - actual, err := ConvertJSONToYamlString(inputJSON) + actual, err := ConvertJSONToYAMLString(inputJSON) if !assert.NoError(t, err) { t.FailNow() } assert.Equal(t, expected, actual) } -func TestConvertYamlNodeToJSONStr(t *testing.T) { - yl := `enum: - - allowedValue1 - - allowedValue2 -maxLength: 15 -type: string -` - node, err := Parse(yl) - if !assert.NoError(t, err) { +// 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() } - res, err := ConvertYamlNodeToJSONString(node) - if !assert.NoError(t, err) { - t.FailNow() - } - assert.Equal(t, `{"enum":["allowedValue1","allowedValue2"],"maxLength":15,"type":"string"}`, res) + assert.Equal(t, expected, err.Error()) } From e994b3b56601277059435e08cd92a12091f0d0af Mon Sep 17 00:00:00 2001 From: Phani Teja Marupaka Date: Mon, 8 Jun 2020 20:51:01 -0700 Subject: [PATCH 5/6] E2e test for run with json --- cmd/config/internal/commands/e2e/e2e_test.go | 43 +++++++++++++++++- cmd/config/internal/commands/sink_test.go | 29 +++--------- cmd/config/internal/commands/source_test.go | 48 +++++--------------- kyaml/kio/byteio_reader.go | 5 -- kyaml/kio/pkgio_reader_test.go | 7 +-- kyaml/runfn/runfn.go | 4 +- kyaml/setters2/add.go | 2 +- kyaml/yaml/types.go | 27 ++++------- kyaml/yaml/types_test.go | 33 +------------- 9 files changed, 76 insertions(+), 122 deletions(-) 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()) -} From 28307bc435ce18189c2a6cb5d70eef32b64bbd66 Mon Sep 17 00:00:00 2001 From: Phani Teja Marupaka Date: Fri, 19 Jun 2020 17:17:32 -0700 Subject: [PATCH 6/6] Simplify parsing --- cmd/config/internal/commands/e2e/e2e_test.go | 14 ++-------- cmd/config/internal/commands/sink_test.go | 4 +-- cmd/config/internal/commands/source.go | 2 +- cmd/config/internal/commands/source_test.go | 13 ++++----- kyaml/kio/byteio_reader.go | 29 ++------------------ kyaml/kio/byteio_writer.go | 27 ++++++++---------- kyaml/kio/filters/testyaml/testyaml.go | 14 ++++++++-- kyaml/kio/kioutil/kioutil.go | 10 ++----- kyaml/kio/pkgio_reader.go | 17 +----------- kyaml/kio/pkgio_reader_test.go | 7 ++--- kyaml/runfn/runfn.go | 2 +- kyaml/setters2/set.go | 4 +-- kyaml/yaml/types.go | 2 -- 13 files changed, 47 insertions(+), 98 deletions(-) diff --git a/cmd/config/internal/commands/e2e/e2e_test.go b/cmd/config/internal/commands/e2e/e2e_test.go index c74721d79..962134c99 100644 --- a/cmd/config/internal/commands/e2e/e2e_test.go +++ b/cmd/config/internal/commands/e2e/e2e_test.go @@ -87,18 +87,8 @@ metadata: 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" - } -} +{"apiVersion": "apps/v1", "kind": "Deployment", "metadata": {"name": "foo", annotations: { + a-string-value: '', a-int-value: '0', a-bool-value: 'false'}}} `, } }, diff --git a/cmd/config/internal/commands/sink_test.go b/cmd/config/internal/commands/sink_test.go index 83fb47bf1..67d364d62 100644 --- a/cmd/config/internal/commands/sink_test.go +++ b/cmd/config/internal/commands/sink_test.go @@ -148,7 +148,7 @@ func TestSinkCommandJSON(t *testing.T) { kind: ResourceList items: - {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo", - "annotations": {"app": "nginx2", config.kubernetes.io/format: 'json', config.kubernetes.io/index: '0', + "annotations": {"app": "nginx2", config.kubernetes.io/index: '0', config.kubernetes.io/path: 'f1.json'}}, "spec": {"replicas": 1}} `)) r.Command.SetArgs([]string{d}) @@ -309,7 +309,7 @@ func TestSinkCommandJSON_Stdout(t *testing.T) { kind: ResourceList items: - {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo", - "annotations": {"app": "nginx2", config.kubernetes.io/format: 'json', config.kubernetes.io/index: '0', + "annotations": {"app": "nginx2", config.kubernetes.io/index: '0', config.kubernetes.io/path: 'f1.json'}}, "spec": {"replicas": 1}} `)) diff --git a/cmd/config/internal/commands/source.go b/cmd/config/internal/commands/source.go index 8dc6296ae..7873956ce 100644 --- a/cmd/config/internal/commands/source.go +++ b/cmd/config/internal/commands/source.go @@ -74,7 +74,7 @@ func (r *SourceRunner) runE(c *cobra.Command, args []string) error { inputs = append(inputs, kio.LocalPackageReader{PackagePath: a, MatchFilesGlob: kio.MatchAll}) } if len(inputs) == 0 { - inputs = []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin(), AcceptJSON: true}} + inputs = []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin()}} } err := kio.Pipeline{Inputs: inputs, Outputs: outputs}.Execute() diff --git a/cmd/config/internal/commands/source_test.go b/cmd/config/internal/commands/source_test.go index 72d82270f..717b22c4a 100644 --- a/cmd/config/internal/commands/source_test.go +++ b/cmd/config/internal/commands/source_test.go @@ -195,13 +195,12 @@ func TestSourceCommandJSON(t *testing.T) { kind: ResourceList items: - {"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}} + "annotations": {"app": "nginx2", 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}} + "config.kubernetes.io/local-config": "true", config.kubernetes.io/index: '0', + config.kubernetes.io/path: 'f2.json'}}, "spec": {"replicas": 3}} `, b.String()) { return } @@ -309,8 +308,8 @@ func TestSourceCommandJSON_Stdin(t *testing.T) { kind: ResourceList items: - {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo", - "annotations": {"app": "nginx2", config.kubernetes.io/format: 'json', config.kubernetes.io/index: '0'}}, - "spec": {"replicas": 1}} + "annotations": {"app": "nginx2", 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 a3adcae41..670fb96f5 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -5,7 +5,6 @@ package kio import ( "bytes" - "encoding/json" "fmt" "io" "sort" @@ -102,16 +101,8 @@ type ByteReader struct { // WrappingKind is set by Read(), and is the kind of the object that // the read objects were originally wrapped in. WrappingKind string - - // AcceptJSON indicates if the input json bytes should be processed - AcceptJSON bool } -const ( - YAML = "yaml" - JSON = "json" -) - var _ Reader = &ByteReader{} func (r *ByteReader) Read() ([]*yaml.RNode, error) { @@ -124,27 +115,11 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) { if err != nil { return nil, errors.Wrap(err) } - inputStr := input.String() - - // check if json is accepted and if input bytes are in json format - if r.AcceptJSON && json.Valid([]byte(inputStr)) { - // convert json to yaml string to generate resource list object - // with appropriate format annotation - if r.SetAnnotations == nil { - r.SetAnnotations = map[string]string{} - } - if !r.OmitReaderAnnotations { - r.SetAnnotations[kioutil.FormatAnnotation] = JSON - } - } - - values := strings.Split(inputStr, "\n---\n") + values := strings.Split(input.String(), "\n---\n") index := 0 for i := range values { - value := values[i] - - decoder := yaml.NewDecoder(bytes.NewBufferString(value)) + decoder := yaml.NewDecoder(bytes.NewBufferString(values[i])) node, err := r.decode(index, decoder) if err == io.EOF { continue diff --git a/kyaml/kio/byteio_writer.go b/kyaml/kio/byteio_writer.go index 72b80331f..c6fe96db4 100644 --- a/kyaml/kio/byteio_writer.go +++ b/kyaml/kio/byteio_writer.go @@ -91,7 +91,7 @@ func (w ByteWriter) Write(nodes []*yaml.RNode) error { // don't wrap the elements if w.WrappingKind == "" { for i := range nodes { - if err := w.encode(encoder, nodes[i]); err != nil { + if err := w.encode(encoder, nodes[i].Document()); err != nil { return err } } @@ -125,26 +125,23 @@ func (w ByteWriter) Write(nodes []*yaml.RNode) error { for i := range nodes { items.Content = append(items.Content, nodes[i].YNode()) } - rNode := yaml.RNode{} - rNode.SetYNode(doc) - err := w.encode(encoder, &rNode) + err := w.encode(encoder, doc) yaml.UndoSerializationHacksOnNodes(nodes) return err } -// encode encodes the input RNode to appropriate node format based on file path extension -func (w ByteWriter) encode(encoder *yaml.Encoder, node *yaml.RNode) error { - _, _, format, err := kioutil.GetFileAnnotations(node) - if !w.KeepReaderAnnotations { - _, err := node.Pipe(yaml.ClearAnnotation(kioutil.FormatAnnotation)) - if err != nil { - return errors.Wrap(err) - } +// encode encodes the input document node to appropriate node format +func (w ByteWriter) encode(encoder *yaml.Encoder, doc *yaml.Node) error { + rNode := &yaml.RNode{} + rNode.SetYNode(doc) + str, err := rNode.String() + if err != nil { + return errors.Wrap(err) } - if err == nil && format == JSON { + if json.Valid([]byte(str)) { je := json.NewEncoder(w.Writer) je.SetIndent("", " ") - return errors.Wrap(je.Encode(node)) + return errors.Wrap(je.Encode(rNode)) } - return encoder.Encode(node.Document()) + return encoder.Encode(doc) } diff --git a/kyaml/kio/filters/testyaml/testyaml.go b/kyaml/kio/filters/testyaml/testyaml.go index 555d95da0..4052e5ffb 100644 --- a/kyaml/kio/filters/testyaml/testyaml.go +++ b/kyaml/kio/filters/testyaml/testyaml.go @@ -56,6 +56,16 @@ status2: - 2 `) -var FormattedJSON1 = []byte(`{"apiVersion": "example.com/v1beta1", "kind": "MyType", "spec": "a", "status": {"conditions": [ - 3, 1, 2]}} +var FormattedJSON1 = []byte(`{ + "apiVersion": "example.com/v1beta1", + "kind": "MyType", + "spec": "a", + "status": { + "conditions": [ + 3, + 1, + 2 + ] + } +} `) diff --git a/kyaml/kio/kioutil/kioutil.go b/kyaml/kio/kioutil/kioutil.go index 8839b7ca4..1d5e3bf58 100644 --- a/kyaml/kio/kioutil/kioutil.go +++ b/kyaml/kio/kioutil/kioutil.go @@ -22,20 +22,16 @@ const ( // PathAnnotation records the path to the file the Resource was read from PathAnnotation AnnotationKey = "config.kubernetes.io/path" - - // FormatAnnotation records the format of the resource ex: json - FormatAnnotation AnnotationKey = "config.kubernetes.io/format" ) -func GetFileAnnotations(rn *yaml.RNode) (string, string, string, error) { +func GetFileAnnotations(rn *yaml.RNode) (string, string, error) { meta, err := rn.GetMeta() if err != nil { - return "", "", "", err + return "", "", err } path := meta.Annotations[PathAnnotation] index := meta.Annotations[IndexAnnotation] - format := meta.Annotations[FormatAnnotation] - return path, index, format, nil + return path, index, nil } // ErrorIfMissingAnnotation validates the provided annotations are present on the given resources diff --git a/kyaml/kio/pkgio_reader.go b/kyaml/kio/pkgio_reader.go index 3b6fa9c3d..d0f4c0ee6 100644 --- a/kyaml/kio/pkgio_reader.go +++ b/kyaml/kio/pkgio_reader.go @@ -123,7 +123,7 @@ func (r *LocalPackageReadWriter) Write(nodes []*yaml.RNode) error { func (r *LocalPackageReadWriter) getFiles(nodes []*yaml.RNode) (sets.String, error) { val := sets.String{} for _, n := range nodes { - path, _, _, err := kioutil.GetFileAnnotations(n) + path, _, err := kioutil.GetFileAnnotations(n) if err != nil { return nil, errors.Wrap(err) } @@ -246,25 +246,10 @@ func (r *LocalPackageReader) readFile(path string, _ os.FileInfo) ([]*yaml.RNode Reader: f, OmitReaderAnnotations: r.OmitReaderAnnotations, SetAnnotations: r.SetAnnotations, - AcceptJSON: r.acceptJSON(path), } return rr.Read() } -// acceptJSON returns true if the input path has json ext and if the MatchFilesGlob -// has any of the JSONMatch ext -func (r *LocalPackageReader) acceptJSON(path string) bool { - for _, gm := range r.MatchFilesGlob { - for _, jm := range JSONMatch { - if filepath.Ext(path) == filepath.Ext(jm) && - filepath.Ext(gm) == filepath.Ext(jm) { - return true - } - } - } - return false -} - // ShouldSkipFile returns true if the file should be skipped func (r *LocalPackageReader) ShouldSkipFile(info os.FileInfo) (bool, error) { // check if the files are in scope diff --git a/kyaml/kio/pkgio_reader_test.go b/kyaml/kio/pkgio_reader_test.go index 2b730b768..62810277d 100644 --- a/kyaml/kio/pkgio_reader_test.go +++ b/kyaml/kio/pkgio_reader_test.go @@ -173,11 +173,10 @@ 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/format: 'json', config.kubernetes.io/index: '0', - config.kubernetes.io/path: 'a_test.json'}}} + `{"a": "b", metadata: {annotations: {config.kubernetes.io/index: '0', config.kubernetes.io/path: 'a_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'}}} + `{"e": "f", "g": {"h": ["i", "j"]}, metadata: {annotations: {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 9b7c662ed..b6a5ce166 100644 --- a/kyaml/runfn/runfn.go +++ b/kyaml/runfn/runfn.go @@ -116,7 +116,7 @@ func (r RunFns) getNodesAndFilters() ( if r.Input == nil { p.Inputs = []kio.Reader{outputPkg} } else { - p.Inputs = []kio.Reader{&kio.ByteReader{Reader: r.Input, AcceptJSON: true}} + p.Inputs = []kio.Reader{&kio.ByteReader{Reader: r.Input}} } if err := p.Execute(); err != nil { return nil, nil, outputPkg, err diff --git a/kyaml/setters2/set.go b/kyaml/setters2/set.go index 8e08cd83d..c95b0f0bc 100644 --- a/kyaml/setters2/set.go +++ b/kyaml/setters2/set.go @@ -447,7 +447,7 @@ func SetAll(s *Set) kio.Filter { return nil, errors.Wrap(err) } if s.Count > preCount { - path, _, _, err := kioutil.GetFileAnnotations(nodes[i]) + path, _, err := kioutil.GetFileAnnotations(nodes[i]) if err != nil { return nil, errors.Wrap(err) } @@ -457,7 +457,7 @@ func SetAll(s *Set) kio.Filter { var nodesInUpdatedFiles []*yaml.RNode // return only the nodes whose corresponding file has at least one node with input setter for i := range nodes { - path, _, _, err := kioutil.GetFileAnnotations(nodes[i]) + path, _, err := kioutil.GetFileAnnotations(nodes[i]) if err != nil { return nil, errors.Wrap(err) } diff --git a/kyaml/yaml/types.go b/kyaml/yaml/types.go index d74cb8471..862a0ecc8 100644 --- a/kyaml/yaml/types.go +++ b/kyaml/yaml/types.go @@ -497,7 +497,6 @@ func String(node *yaml.Node, opts ...string) (string, error) { if node == nil { return "", nil } - DoSerializationHacks(node) optsSet := sets.String{} optsSet.Insert(opts...) if optsSet.Has(Flow) { @@ -516,7 +515,6 @@ func String(node *yaml.Node, opts ...string) (string, error) { if optsSet.Has(Trim) { val = strings.TrimSpace(val) } - UndoSerializationHacks(node) return val, errors.Wrap(err) }