mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Write json files in sink
This commit is contained in:
@@ -33,7 +33,7 @@ items:
|
|||||||
annotations:
|
annotations:
|
||||||
app: nginx2
|
app: nginx2
|
||||||
config.kubernetes.io/index: '0'
|
config.kubernetes.io/index: '0'
|
||||||
config.kubernetes.io/path: 'f1.yaml'
|
config.kubernetes.io/path: 'f1.json'
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
- kind: Service
|
- kind: Service
|
||||||
@@ -42,7 +42,7 @@ items:
|
|||||||
annotations:
|
annotations:
|
||||||
app: nginx
|
app: nginx
|
||||||
config.kubernetes.io/index: '1'
|
config.kubernetes.io/index: '1'
|
||||||
config.kubernetes.io/path: 'f1.yaml'
|
config.kubernetes.io/path: 'f1.json'
|
||||||
spec:
|
spec:
|
||||||
selector:
|
selector:
|
||||||
app: nginx
|
app: nginx
|
||||||
@@ -77,28 +77,13 @@ items:
|
|||||||
t.FailNow()
|
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) {
|
if !assert.NoError(t, err) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
expected := `kind: Deployment
|
expected := `'{"kind":"Deployment","metadata":{"annotations":{"app":"nginx2"},"labels":{"app":"nginx2"},"name":"foo"},"spec":{"replicas":1}}'
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
app: nginx2
|
|
||||||
name: foo
|
|
||||||
annotations:
|
|
||||||
app: nginx2
|
|
||||||
spec:
|
|
||||||
replicas: 1
|
|
||||||
---
|
---
|
||||||
kind: Service
|
'{"kind":"Service","metadata":{"annotations":{"app":"nginx"},"name":"foo"},"spec":{"selector":{"app":"nginx"}}}'
|
||||||
metadata:
|
|
||||||
name: foo
|
|
||||||
annotations:
|
|
||||||
app: nginx
|
|
||||||
spec:
|
|
||||||
selector:
|
|
||||||
app: nginx
|
|
||||||
`
|
`
|
||||||
if !assert.Equal(t, expected, string(actual)) {
|
if !assert.Equal(t, expected, string(actual)) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -103,6 +104,7 @@ type ByteReader struct {
|
|||||||
WrappingKind string
|
WrappingKind string
|
||||||
|
|
||||||
// Path indicates file path which is being read
|
// Path indicates file path which is being read
|
||||||
|
// empty if it is being read from stdin
|
||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,12 +125,20 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) {
|
|||||||
index := 0
|
index := 0
|
||||||
for i := range values {
|
for i := range values {
|
||||||
value := values[i]
|
value := values[i]
|
||||||
if strings.HasSuffix(r.Path, JSONSuffix) {
|
|
||||||
value, err = yaml.ConvertJSONToYamlString(value)
|
if r.Path != "" {
|
||||||
if err != nil {
|
for _, g := range JSONMatch {
|
||||||
return nil, err
|
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))
|
decoder := yaml.NewDecoder(bytes.NewBufferString(value))
|
||||||
node, err := r.decode(index, decoder)
|
node, err := r.decode(index, decoder)
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ package kio
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||||
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
|
"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 if set, will cause ByteWriter to sort the the nodes before writing them.
|
||||||
Sort bool
|
Sort bool
|
||||||
|
|
||||||
|
// Path is the output file path to write to
|
||||||
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Writer = ByteWriter{}
|
var _ Writer = ByteWriter{}
|
||||||
@@ -90,9 +94,8 @@ func (w ByteWriter) Write(nodes []*yaml.RNode) error {
|
|||||||
// don't wrap the elements
|
// don't wrap the elements
|
||||||
if w.WrappingKind == "" {
|
if w.WrappingKind == "" {
|
||||||
for i := range nodes {
|
for i := range nodes {
|
||||||
err := encoder.Encode(nodes[i].Document())
|
if err := w.encode(encoder, nodes[i]); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return errors.Wrap(err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -125,7 +128,29 @@ func (w ByteWriter) Write(nodes []*yaml.RNode) error {
|
|||||||
for i := range nodes {
|
for i := range nodes {
|
||||||
items.Content = append(items.Content, nodes[i].YNode())
|
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)
|
yaml.UndoSerializationHacksOnNodes(nodes)
|
||||||
return err
|
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())
|
||||||
|
}
|
||||||
|
|||||||
@@ -166,8 +166,6 @@ type LocalPackageReader struct {
|
|||||||
|
|
||||||
var _ Reader = LocalPackageReader{}
|
var _ Reader = LocalPackageReader{}
|
||||||
|
|
||||||
const JSONSuffix = ".json"
|
|
||||||
|
|
||||||
var DefaultMatch = []string{"*.yaml", "*.yml"}
|
var DefaultMatch = []string{"*.yaml", "*.yml"}
|
||||||
var JSONMatch = []string{"*.json"}
|
var JSONMatch = []string{"*.json"}
|
||||||
var MatchAll = append(DefaultMatch, JSONMatch...)
|
var MatchAll = append(DefaultMatch, JSONMatch...)
|
||||||
|
|||||||
@@ -98,6 +98,7 @@ func (r LocalPackageWriter) Write(nodes []*yaml.RNode) error {
|
|||||||
Writer: f,
|
Writer: f,
|
||||||
KeepReaderAnnotations: r.KeepReaderAnnotations,
|
KeepReaderAnnotations: r.KeepReaderAnnotations,
|
||||||
ClearAnnotations: r.ClearAnnotations,
|
ClearAnnotations: r.ClearAnnotations,
|
||||||
|
Path: outputPath,
|
||||||
}
|
}
|
||||||
if err = w.Write(outputFiles[path]); err != nil {
|
if err = w.Write(outputFiles[path]); err != nil {
|
||||||
return errors.Wrap(err)
|
return errors.Wrap(err)
|
||||||
|
|||||||
@@ -735,7 +735,7 @@ func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) {
|
|||||||
return node, nil
|
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) {
|
func ConvertJSONToYamlString(jsonStr string) (string, error) {
|
||||||
var body map[string]interface{}
|
var body map[string]interface{}
|
||||||
err := json.Unmarshal([]byte(jsonStr), &body)
|
err := json.Unmarshal([]byte(jsonStr), &body)
|
||||||
@@ -749,6 +749,20 @@ func ConvertJSONToYamlString(jsonStr string) (string, error) {
|
|||||||
return string(yml), nil
|
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
|
// checkKey returns true if all elems have the key
|
||||||
func checkKey(key string, elems []*Node) bool {
|
func checkKey(key string, elems []*Node) bool {
|
||||||
count := 0
|
count := 0
|
||||||
|
|||||||
@@ -182,3 +182,21 @@ type: string
|
|||||||
}
|
}
|
||||||
assert.Equal(t, expected, actual)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user