mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Suggested changes
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user