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) +}