mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-09-18 13:22:17 +00:00
Suggested changes
This commit is contained in:
@@ -38,6 +38,9 @@ type ByteReadWriter struct {
|
|||||||
// the Resources, otherwise they will be cleared.
|
// the Resources, otherwise they will be cleared.
|
||||||
KeepReaderAnnotations bool
|
KeepReaderAnnotations bool
|
||||||
|
|
||||||
|
// AddSeqIndentAnnotation if true adds kioutil.SeqIndentAnnotation to each resource
|
||||||
|
AddSeqIndentAnnotation bool
|
||||||
|
|
||||||
// Style is a style that is set on the Resource Node Document.
|
// Style is a style that is set on the Resource Node Document.
|
||||||
Style yaml.Style
|
Style yaml.Style
|
||||||
|
|
||||||
@@ -48,16 +51,13 @@ type ByteReadWriter struct {
|
|||||||
NoWrap bool
|
NoWrap bool
|
||||||
WrappingAPIVersion string
|
WrappingAPIVersion string
|
||||||
WrappingKind string
|
WrappingKind string
|
||||||
|
|
||||||
// RetainSeqIndent if true retains the sequence indentation of
|
|
||||||
RetainSeqIndent bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rw *ByteReadWriter) Read() ([]*yaml.RNode, error) {
|
func (rw *ByteReadWriter) Read() ([]*yaml.RNode, error) {
|
||||||
b := &ByteReader{
|
b := &ByteReader{
|
||||||
Reader: rw.Reader,
|
Reader: rw.Reader,
|
||||||
OmitReaderAnnotations: rw.OmitReaderAnnotations,
|
OmitReaderAnnotations: rw.OmitReaderAnnotations,
|
||||||
AddSeqIndentAnnotation: rw.RetainSeqIndent,
|
AddSeqIndentAnnotation: rw.AddSeqIndentAnnotation,
|
||||||
}
|
}
|
||||||
val, err := b.Read()
|
val, err := b.Read()
|
||||||
if rw.FunctionConfig == nil {
|
if rw.FunctionConfig == nil {
|
||||||
@@ -117,6 +117,9 @@ type ByteReader struct {
|
|||||||
// annotation on Resources as they are Read.
|
// annotation on Resources as they are Read.
|
||||||
OmitReaderAnnotations bool
|
OmitReaderAnnotations bool
|
||||||
|
|
||||||
|
// AddSeqIndentAnnotation if true adds kioutil.SeqIndentAnnotation to each resource
|
||||||
|
AddSeqIndentAnnotation bool
|
||||||
|
|
||||||
// SetAnnotations is a map of caller specified annotations to set on resources as they are read
|
// SetAnnotations is a map of caller specified annotations to set on resources as they are read
|
||||||
// These are independent of the annotations controlled by OmitReaderAnnotations
|
// These are independent of the annotations controlled by OmitReaderAnnotations
|
||||||
SetAnnotations map[string]string
|
SetAnnotations map[string]string
|
||||||
@@ -135,9 +138,6 @@ 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
|
||||||
|
|
||||||
// AddSeqIndentAnnotation if true adds kioutil.SeqIndentAnnotation to each resource
|
|
||||||
AddSeqIndentAnnotation bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Reader = &ByteReader{}
|
var _ Reader = &ByteReader{}
|
||||||
@@ -264,46 +264,35 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) {
|
|||||||
// value is the input yaml string and node is the decoded equivalent of value
|
// value is the input yaml string and node is the decoded equivalent of value
|
||||||
// the annotation value is decided by deriving the existing sequence indentation of resource
|
// the annotation value is decided by deriving the existing sequence indentation of resource
|
||||||
func addSeqIndentAnno(value string, node *yaml.RNode) error {
|
func addSeqIndentAnno(value string, node *yaml.RNode) error {
|
||||||
|
// step 1: derive the sequence indentation of the node
|
||||||
anno := node.GetAnnotations()
|
anno := node.GetAnnotations()
|
||||||
if anno[kioutil.SeqIndentAnnotation] != "" {
|
if anno[kioutil.SeqIndentAnnotation] != "" {
|
||||||
// the annotation already exists, so don't change it
|
// the annotation already exists, so don't change it
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
currentDefaultIndent := yaml.SequenceIndentationStyle()
|
// marshal the node with 2 space sequence indentation and calculate the diff
|
||||||
defer yaml.SetSequenceIndentationStyle(currentDefaultIndent)
|
out, err := yaml.MarshalWithIndent(node.Document(), yaml.DefaultMapIndent, yaml.WideSeqIndent)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
twoSpaceIndentDiff := copyutil.PrettyFileDiff(string(out), value)
|
||||||
|
|
||||||
// encode the node to string with default 2 space sequence indentation and calculate the diff
|
// marshal the node with 0 space sequence indentation and calculate the diff
|
||||||
yaml.SetSequenceIndentationStyle(yaml.WideSequenceStyle)
|
out, err = yaml.MarshalWithIndent(node.Document(), yaml.DefaultMapIndent, yaml.CompactSeqIndent)
|
||||||
n, err := yaml.Parse(value)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
out, err := n.String()
|
noIndentDiff := copyutil.PrettyFileDiff(string(out), value)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
twoSpaceIndentDiff := copyutil.PrettyFileDiff(out, value)
|
|
||||||
|
|
||||||
// encode the node to string with compact 0 space sequence indentation and calculate the diff
|
|
||||||
yaml.SetSequenceIndentationStyle(yaml.CompactSequenceStyle)
|
|
||||||
n, err = yaml.Parse(value)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
out, err = n.String()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
noIndentDiff := copyutil.PrettyFileDiff(out, value)
|
|
||||||
|
|
||||||
var style string
|
var style string
|
||||||
if len(noIndentDiff) <= len(twoSpaceIndentDiff) {
|
if len(noIndentDiff) <= len(twoSpaceIndentDiff) {
|
||||||
style = yaml.CompactSequenceStyle
|
style = string(yaml.CompactSeqIndent)
|
||||||
} else {
|
} else {
|
||||||
style = yaml.WideSequenceStyle
|
style = string(yaml.WideSeqIndent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// step 2: add the annotation to the node
|
||||||
return node.PipeE(
|
return node.PipeE(
|
||||||
yaml.LookupCreate(yaml.MappingNode, yaml.MetadataField, yaml.AnnotationsField),
|
yaml.LookupCreate(yaml.MappingNode, yaml.MetadataField, yaml.AnnotationsField),
|
||||||
yaml.SetField(kioutil.SeqIndentAnnotation, yaml.NewScalarRNode(style)))
|
yaml.SetField(kioutil.SeqIndentAnnotation, yaml.NewScalarRNode(style)))
|
||||||
|
|||||||
@@ -806,7 +806,7 @@ items:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestByteReader_RetainSeqIndent(t *testing.T) {
|
func TestByteReader_AddSeqIndent(t *testing.T) {
|
||||||
type testCase struct {
|
type testCase struct {
|
||||||
name string
|
name string
|
||||||
err string
|
err string
|
||||||
|
|||||||
@@ -581,7 +581,7 @@ spec:
|
|||||||
w := tc.instance
|
w := tc.instance
|
||||||
w.Writer = &out
|
w.Writer = &out
|
||||||
w.Reader = &in
|
w.Reader = &in
|
||||||
w.RetainSeqIndent = true
|
w.AddSeqIndentAnnotation = true
|
||||||
|
|
||||||
nodes, err := w.Read()
|
nodes, err := w.Read()
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
|
|||||||
@@ -89,6 +89,9 @@ func (w ByteWriter) Write(inputNodes []*yaml.RNode) error {
|
|||||||
if jsonEncodeSingleBareNode {
|
if jsonEncodeSingleBareNode {
|
||||||
encoder := json.NewEncoder(w.Writer)
|
encoder := json.NewEncoder(w.Writer)
|
||||||
encoder.SetIndent("", " ")
|
encoder.SetIndent("", " ")
|
||||||
|
if err := nodes[0].DeleteAnnotation(kioutil.SeqIndentAnnotation); err != nil {
|
||||||
|
return errors.Wrap(err)
|
||||||
|
}
|
||||||
return errors.Wrap(encoder.Encode(nodes[0]))
|
return errors.Wrap(encoder.Encode(nodes[0]))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,8 +100,7 @@ func (w ByteWriter) Write(inputNodes []*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 {
|
||||||
setSeqIndent(nodes[i], encoder)
|
if err := unwrapAndEncodeYAML(nodes[i], encoder); err != nil {
|
||||||
if err := encoder.Encode(nodes[i].Document()); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -135,23 +137,21 @@ func (w ByteWriter) Write(inputNodes []*yaml.RNode) error {
|
|||||||
return encoder.Encode(doc)
|
return encoder.Encode(doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// setSeqIndent reads the SeqIndentAnnotation from node and sets the sequence indentation
|
// unwrapAndEncodeYAML encodes the yaml RNode in unwrapped format,
|
||||||
// in the encoder
|
// as a pre-step, it clears the sets the sequence indentation for encoder,
|
||||||
// if the resource doesn't have SeqIndentAnnotation it will use CompactSeqIndent
|
// based on the kioutil.SeqIndentAnnotation and clears it before encoding.
|
||||||
func setSeqIndent(node *yaml.RNode, encoder *yaml.Encoder) {
|
func unwrapAndEncodeYAML(node *yaml.RNode, encoder *yaml.Encoder) error {
|
||||||
anno := node.GetAnnotations()
|
anno := node.GetAnnotations()
|
||||||
seqIndent := anno[kioutil.SeqIndentAnnotation]
|
seqIndent := anno[kioutil.SeqIndentAnnotation]
|
||||||
if seqIndent == yaml.WideSequenceStyle {
|
if seqIndent == string(yaml.WideSeqIndent) {
|
||||||
encoder.DefaultSeqIndent()
|
encoder.DefaultSeqIndent()
|
||||||
} else {
|
} else {
|
||||||
encoder.CompactSeqIndent()
|
encoder.CompactSeqIndent()
|
||||||
}
|
}
|
||||||
if err := node.PipeE(yaml.ClearAnnotation(kioutil.SeqIndentAnnotation)); err != nil {
|
if err := node.DeleteAnnotation(kioutil.SeqIndentAnnotation); err != nil {
|
||||||
return
|
return errors.Wrap(err)
|
||||||
}
|
|
||||||
if err := yaml.ClearEmptyAnnotations(node); err != nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
return encoder.Encode(node.Document())
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyRNodes(in []*yaml.RNode) []*yaml.RNode {
|
func copyRNodes(in []*yaml.RNode) []*yaml.RNode {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ const (
|
|||||||
// PathAnnotation records the path to the file the Resource was read from
|
// PathAnnotation records the path to the file the Resource was read from
|
||||||
PathAnnotation AnnotationKey = "config.kubernetes.io/path"
|
PathAnnotation AnnotationKey = "config.kubernetes.io/path"
|
||||||
|
|
||||||
// SeqIndentAnnotation records the path to the file the Resource was read from
|
// SeqIndentAnnotation records the sequence nodes indentation of the input resource
|
||||||
SeqIndentAnnotation AnnotationKey = "internal.config.kubernetes.io/seqindent"
|
SeqIndentAnnotation AnnotationKey = "internal.config.kubernetes.io/seqindent"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -178,7 +178,8 @@ type LocalPackageReader struct {
|
|||||||
// the file
|
// the file
|
||||||
FileSkipFunc LocalPackageSkipFileFunc
|
FileSkipFunc LocalPackageSkipFileFunc
|
||||||
|
|
||||||
RetainSeqIndent bool
|
// AddSeqIndentAnnotation if true adds kioutil.SeqIndentAnnotation to each resource
|
||||||
|
AddSeqIndentAnnotation bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Reader = LocalPackageReader{}
|
var _ Reader = LocalPackageReader{}
|
||||||
@@ -269,7 +270,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,
|
||||||
AddSeqIndentAnnotation: r.RetainSeqIndent,
|
AddSeqIndentAnnotation: r.AddSeqIndentAnnotation,
|
||||||
}
|
}
|
||||||
return rr.Read()
|
return rr.Read()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,24 +10,14 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/kyaml/internal/forked/github.com/go-yaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/internal/forked/github.com/go-yaml/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
const CompactSequenceStyle = "compact"
|
const (
|
||||||
const WideSequenceStyle = "wide"
|
WideSeqIndent SeqIndentType = "wide"
|
||||||
|
CompactSeqIndent SeqIndentType = "compact"
|
||||||
|
DefaultMapIndent = 2
|
||||||
|
)
|
||||||
|
|
||||||
const DefaultIndent = 2
|
// SeqIndentType holds the indentation style for sequence nodes
|
||||||
const DefaultSequenceStyle = CompactSequenceStyle
|
type SeqIndentType string
|
||||||
|
|
||||||
var sequenceIndentationStyle = DefaultSequenceStyle
|
|
||||||
var indent = DefaultIndent
|
|
||||||
|
|
||||||
// SetSequenceIndentationStyle sets the sequenceIndentationStyle variable
|
|
||||||
func SetSequenceIndentationStyle(style string) {
|
|
||||||
sequenceIndentationStyle = style
|
|
||||||
}
|
|
||||||
|
|
||||||
// SequenceIndentationStyle returns the value of sequenceIndentationStyle
|
|
||||||
func SequenceIndentationStyle() string {
|
|
||||||
return sequenceIndentationStyle
|
|
||||||
}
|
|
||||||
|
|
||||||
// Expose the yaml.v3 functions so this package can be used as a replacement
|
// Expose the yaml.v3 functions so this package can be used as a replacement
|
||||||
|
|
||||||
@@ -53,13 +43,33 @@ var Unmarshal = yaml.Unmarshal
|
|||||||
var NewDecoder = yaml.NewDecoder
|
var NewDecoder = yaml.NewDecoder
|
||||||
var NewEncoder = func(w io.Writer) *yaml.Encoder {
|
var NewEncoder = func(w io.Writer) *yaml.Encoder {
|
||||||
e := yaml.NewEncoder(w)
|
e := yaml.NewEncoder(w)
|
||||||
e.SetIndent(indent)
|
e.SetIndent(DefaultMapIndent)
|
||||||
if sequenceIndentationStyle == CompactSequenceStyle {
|
e.CompactSeqIndent()
|
||||||
e.CompactSeqIndent()
|
|
||||||
}
|
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalWithIndent marshals the input interface with provided indents
|
||||||
|
func MarshalWithIndent(in interface{}, mapIndent int, seqIndent SeqIndentType) ([]byte, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := NewEncoderWithIndent(&buf, mapIndent, seqIndent).Encode(in)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewEncoderWithIndent returns the encoder with configurable indents
|
||||||
|
func NewEncoderWithIndent(w io.Writer, mapIndent int, seqIndent SeqIndentType) *yaml.Encoder {
|
||||||
|
encoder := NewEncoder(w)
|
||||||
|
encoder.SetIndent(mapIndent)
|
||||||
|
if seqIndent == WideSeqIndent {
|
||||||
|
encoder.DefaultSeqIndent()
|
||||||
|
} else {
|
||||||
|
encoder.CompactSeqIndent()
|
||||||
|
}
|
||||||
|
return encoder
|
||||||
|
}
|
||||||
|
|
||||||
var AliasNode yaml.Kind = yaml.AliasNode
|
var AliasNode yaml.Kind = yaml.AliasNode
|
||||||
var DocumentNode yaml.Kind = yaml.DocumentNode
|
var DocumentNode yaml.Kind = yaml.DocumentNode
|
||||||
var MappingNode yaml.Kind = yaml.MappingNode
|
var MappingNode yaml.Kind = yaml.MappingNode
|
||||||
|
|||||||
@@ -505,6 +505,14 @@ func (rn *RNode) SetAnnotations(m map[string]string) error {
|
|||||||
return rn.setMapInMetadata(m, AnnotationsField)
|
return rn.setMapInMetadata(m, AnnotationsField)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteAnnotation tries to delete the annotation and clears the empty metadata field.
|
||||||
|
func (rn *RNode) DeleteAnnotation(annotation string) error {
|
||||||
|
if err := rn.PipeE(ClearAnnotation(annotation)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return ClearEmptyAnnotations(rn)
|
||||||
|
}
|
||||||
|
|
||||||
// GetLabels gets the metadata labels field.
|
// GetLabels gets the metadata labels field.
|
||||||
// If the field is missing, returns an empty map.
|
// If the field is missing, returns an empty map.
|
||||||
// Use another method to check for missing metadata.
|
// Use another method to check for missing metadata.
|
||||||
|
|||||||
Reference in New Issue
Block a user