mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Include json files for fn source
This commit is contained in:
@@ -71,7 +71,7 @@ func (r *SourceRunner) runE(c *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
var inputs []kio.Reader
|
var inputs []kio.Reader
|
||||||
for _, a := range args {
|
for _, a := range args {
|
||||||
inputs = append(inputs, kio.LocalPackageReader{PackagePath: a})
|
inputs = append(inputs, kio.LocalPackageReader{PackagePath: a, IncludeJSON: true})
|
||||||
}
|
}
|
||||||
if len(inputs) == 0 {
|
if len(inputs) == 0 {
|
||||||
inputs = []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin()}}
|
inputs = []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin()}}
|
||||||
|
|||||||
@@ -21,25 +21,37 @@ func TestSourceCommand(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer os.RemoveAll(d)
|
defer os.RemoveAll(d)
|
||||||
|
|
||||||
err = ioutil.WriteFile(filepath.Join(d, "f1.yaml"), []byte(`
|
err = ioutil.WriteFile(filepath.Join(d, "f1.json"), []byte(`
|
||||||
kind: Deployment
|
{
|
||||||
metadata:
|
"kind": "Deployment",
|
||||||
labels:
|
"metadata": {
|
||||||
app: nginx2
|
"labels": {
|
||||||
name: foo
|
"app": "nginx2"
|
||||||
annotations:
|
},
|
||||||
app: nginx2
|
"name": "foo",
|
||||||
spec:
|
"annotations": {
|
||||||
replicas: 1
|
"app": "nginx2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"spec": {
|
||||||
|
"replicas": 1
|
||||||
|
}
|
||||||
|
}
|
||||||
---
|
---
|
||||||
kind: Service
|
{
|
||||||
metadata:
|
"kind": "Service",
|
||||||
name: foo
|
"metadata": {
|
||||||
annotations:
|
"name": "foo",
|
||||||
app: nginx
|
"annotations": {
|
||||||
spec:
|
"app": "nginx"
|
||||||
selector:
|
}
|
||||||
app: nginx
|
},
|
||||||
|
"spec": {
|
||||||
|
"selector": {
|
||||||
|
"app": "nginx"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
`), 0600)
|
`), 0600)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
return
|
return
|
||||||
@@ -86,22 +98,22 @@ kind: ResourceList
|
|||||||
items:
|
items:
|
||||||
- kind: Deployment
|
- kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
labels:
|
|
||||||
app: nginx2
|
|
||||||
name: foo
|
|
||||||
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'
|
||||||
|
labels:
|
||||||
|
app: nginx2
|
||||||
|
name: foo
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
- kind: Service
|
- kind: Service
|
||||||
metadata:
|
metadata:
|
||||||
name: foo
|
|
||||||
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'
|
||||||
|
name: foo
|
||||||
spec:
|
spec:
|
||||||
selector:
|
selector:
|
||||||
app: nginx
|
app: nginx
|
||||||
|
|||||||
@@ -101,6 +101,9 @@ type ByteReader struct {
|
|||||||
// WrappingKind is set by Read(), and is the kind of the object that
|
// WrappingKind is set by Read(), and is the kind of the object that
|
||||||
// the read objects were originally wrapped in.
|
// the read objects were originally wrapped in.
|
||||||
WrappingKind string
|
WrappingKind string
|
||||||
|
|
||||||
|
// JSON indicates if the input file source is json
|
||||||
|
JSON bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Reader = &ByteReader{}
|
var _ Reader = &ByteReader{}
|
||||||
@@ -119,7 +122,14 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) {
|
|||||||
|
|
||||||
index := 0
|
index := 0
|
||||||
for i := range values {
|
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)
|
node, err := r.decode(index, decoder)
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"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"
|
||||||
@@ -162,10 +163,18 @@ type LocalPackageReader struct {
|
|||||||
|
|
||||||
// SetAnnotations are annotations to set on the Resources as they are read.
|
// SetAnnotations are annotations to set on the Resources as they are read.
|
||||||
SetAnnotations map[string]string `yaml:"setAnnotations,omitempty"`
|
SetAnnotations map[string]string `yaml:"setAnnotations,omitempty"`
|
||||||
|
|
||||||
|
// IncludeJSON indicates if the json resources files must be included during read
|
||||||
|
IncludeJSON bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Reader = LocalPackageReader{}
|
var _ Reader = LocalPackageReader{}
|
||||||
|
|
||||||
|
const (
|
||||||
|
JSONFilePattern = "*.json"
|
||||||
|
JSONSuffix = ".json"
|
||||||
|
)
|
||||||
|
|
||||||
var DefaultMatch = []string{"*.yaml", "*.yml"}
|
var DefaultMatch = []string{"*.yaml", "*.yml"}
|
||||||
|
|
||||||
// Read reads the Resources.
|
// Read reads the Resources.
|
||||||
@@ -178,6 +187,9 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) {
|
|||||||
r.PackagePath = filepath.ToSlash(r.PackagePath)
|
r.PackagePath = filepath.ToSlash(r.PackagePath)
|
||||||
if len(r.MatchFilesGlob) == 0 {
|
if len(r.MatchFilesGlob) == 0 {
|
||||||
r.MatchFilesGlob = DefaultMatch
|
r.MatchFilesGlob = DefaultMatch
|
||||||
|
if r.IncludeJSON {
|
||||||
|
r.MatchFilesGlob = append(r.MatchFilesGlob, JSONFilePattern)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var operand ResourceNodeSlice
|
var operand ResourceNodeSlice
|
||||||
@@ -243,6 +255,7 @@ func (r *LocalPackageReader) readFile(path string, _ os.FileInfo) ([]*yaml.RNode
|
|||||||
Reader: f,
|
Reader: f,
|
||||||
OmitReaderAnnotations: r.OmitReaderAnnotations,
|
OmitReaderAnnotations: r.OmitReaderAnnotations,
|
||||||
SetAnnotations: r.SetAnnotations,
|
SetAnnotations: r.SetAnnotations,
|
||||||
|
JSON: strings.HasSuffix(path, JSONSuffix),
|
||||||
}
|
}
|
||||||
return rr.Read()
|
return rr.Read()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -724,22 +724,31 @@ func (rn *RNode) UnmarshalJSON(b []byte) error {
|
|||||||
|
|
||||||
// ConvertJSONToYamlNode parses input json string and returns equivalent yaml node
|
// ConvertJSONToYamlNode parses input json string and returns equivalent yaml node
|
||||||
func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) {
|
func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) {
|
||||||
var body map[string]interface{}
|
yml, err := ConvertJSONToYamlString(jsonStr)
|
||||||
err := json.Unmarshal([]byte(jsonStr), &body)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
yml, err := yaml.Marshal(body)
|
node, err := Parse(yml)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
node, err := Parse(string(yml))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return node, nil
|
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
|
// 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
|
||||||
|
|||||||
@@ -166,3 +166,19 @@ type: string
|
|||||||
}
|
}
|
||||||
assert.Equal(t, expected, actual)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user