diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index 620eea1bc..f179cedba 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -114,7 +114,7 @@ type ByteReader struct { Reader io.Reader // OmitReaderAnnotations will configures Read to skip setting the config.kubernetes.io/index - // annotation on Resources as they are Read. + // and internal.config.kubernetes.io/seqindent annotations on Resources as they are Read. OmitReaderAnnotations bool // AddSeqIndentAnnotation if true adds kioutil.SeqIndentAnnotation to each resource @@ -314,20 +314,14 @@ func seqIndentAnno(node *yaml.Node, originalYAML string) string { } // marshal the node with 2 space sequence indentation and calculate the diff - out, err := yaml.MarshalWithOptions(rNode.Document(), &yaml.EncoderOptions{ - MapIndent: yaml.DefaultMapIndent, - SeqIndent: yaml.WideSeqIndent, - }) + out, err := yaml.MarshalWithOptions(rNode.Document(), &yaml.EncoderOptions{SeqIndent: yaml.WideSeqIndent}) if err != nil { return "" } twoSpaceIndentDiff := copyutil.PrettyFileDiff(string(out), originalYAML) // marshal the node with 0 space sequence indentation and calculate the diff - out, err = yaml.MarshalWithOptions(rNode.Document(), &yaml.EncoderOptions{ - MapIndent: yaml.DefaultMapIndent, - SeqIndent: yaml.CompactSeqIndent, - }) + out, err = yaml.MarshalWithOptions(rNode.Document(), &yaml.EncoderOptions{SeqIndent: yaml.CompactSeqIndent}) if err != nil { return "" } diff --git a/kyaml/kio/byteio_readwriter_test.go b/kyaml/kio/byteio_readwriter_test.go index e7657e1bc..20f8cf8fd 100644 --- a/kyaml/kio/byteio_readwriter_test.go +++ b/kyaml/kio/byteio_readwriter_test.go @@ -425,7 +425,7 @@ spec: `, }, { - name: "round_trip with mixed indentations in same resource, least diff wins", + name: "round_trip with mixed indentations in same resource, wide wins", input: ` apiVersion: apps/v1 kind: Deployment @@ -450,7 +450,7 @@ env: `, }, { - name: "round_trip with mixed indentations in same resource, least diff wins", + name: "round_trip with mixed indentations in same resource, compact wins", input: ` apiVersion: apps/v1 kind: Deployment diff --git a/kyaml/kio/pkgio_reader.go b/kyaml/kio/pkgio_reader.go index 08d20f39b..3dd9b99c7 100644 --- a/kyaml/kio/pkgio_reader.go +++ b/kyaml/kio/pkgio_reader.go @@ -40,6 +40,9 @@ type LocalPackageReadWriter struct { KeepReaderAnnotations bool `yaml:"keepReaderAnnotations,omitempty"` + // AddSeqIndentAnnotation if true adds kioutil.SeqIndentAnnotation to each resource + AddSeqIndentAnnotation bool + // PackagePath is the path to the package directory. PackagePath string `yaml:"path,omitempty"` @@ -79,13 +82,14 @@ type LocalPackageReadWriter struct { func (r *LocalPackageReadWriter) Read() ([]*yaml.RNode, error) { nodes, err := LocalPackageReader{ - PackagePath: r.PackagePath, - MatchFilesGlob: r.MatchFilesGlob, - IncludeSubpackages: r.IncludeSubpackages, - ErrorIfNonResources: r.ErrorIfNonResources, - SetAnnotations: r.SetAnnotations, - PackageFileName: r.PackageFileName, - FileSkipFunc: r.FileSkipFunc, + PackagePath: r.PackagePath, + MatchFilesGlob: r.MatchFilesGlob, + IncludeSubpackages: r.IncludeSubpackages, + ErrorIfNonResources: r.ErrorIfNonResources, + SetAnnotations: r.SetAnnotations, + PackageFileName: r.PackageFileName, + FileSkipFunc: r.FileSkipFunc, + AddSeqIndentAnnotation: r.AddSeqIndentAnnotation, }.Read() if err != nil { return nil, errors.Wrap(err) diff --git a/kyaml/yaml/alias.go b/kyaml/yaml/alias.go index 6fb4f219a..b4a60ad11 100644 --- a/kyaml/yaml/alias.go +++ b/kyaml/yaml/alias.go @@ -13,17 +13,15 @@ import ( const ( WideSeqIndent SeqIndentType = "wide" CompactSeqIndent SeqIndentType = "compact" - DefaultMapIndent = 2 + DefaultIndent = 2 ) // SeqIndentType holds the indentation style for sequence nodes type SeqIndentType string -// EncoderOptions are options that can be used to configure the encoder +// EncoderOptions are options that can be used to configure the encoder, +// do not expose new options without considerable justification type EncoderOptions struct { - // MapIndent is the indentation for YAML Mapping nodes - MapIndent int - // SeqIndent is the indentation style for YAML Sequence nodes SeqIndent SeqIndentType } @@ -52,7 +50,7 @@ var Unmarshal = yaml.Unmarshal var NewDecoder = yaml.NewDecoder var NewEncoder = func(w io.Writer) *yaml.Encoder { e := yaml.NewEncoder(w) - e.SetIndent(DefaultMapIndent) + e.SetIndent(DefaultIndent) e.CompactSeqIndent() return e } @@ -70,7 +68,7 @@ func MarshalWithOptions(in interface{}, opts *EncoderOptions) ([]byte, error) { // NewEncoderWithOptions returns the encoder with provided options func NewEncoderWithOptions(w io.Writer, opts *EncoderOptions) *yaml.Encoder { encoder := NewEncoder(w) - encoder.SetIndent(opts.MapIndent) + encoder.SetIndent(DefaultIndent) if opts.SeqIndent == WideSeqIndent { encoder.DefaultSeqIndent() } else {