diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index f179cedba..a9cc0471b 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -38,8 +38,8 @@ type ByteReadWriter struct { // the Resources, otherwise they will be cleared. KeepReaderAnnotations bool - // AddSeqIndentAnnotation if true adds kioutil.SeqIndentAnnotation to each resource - AddSeqIndentAnnotation bool + // PreserveSeqIndent if true adds kioutil.SeqIndentAnnotation to each resource + PreserveSeqIndent bool // Style is a style that is set on the Resource Node Document. Style yaml.Style @@ -55,9 +55,9 @@ type ByteReadWriter struct { func (rw *ByteReadWriter) Read() ([]*yaml.RNode, error) { b := &ByteReader{ - Reader: rw.Reader, - OmitReaderAnnotations: rw.OmitReaderAnnotations, - AddSeqIndentAnnotation: rw.AddSeqIndentAnnotation, + Reader: rw.Reader, + OmitReaderAnnotations: rw.OmitReaderAnnotations, + PreserveSeqIndent: rw.PreserveSeqIndent, } val, err := b.Read() if rw.FunctionConfig == nil { @@ -117,8 +117,8 @@ type ByteReader struct { // and internal.config.kubernetes.io/seqindent annotations on Resources as they are Read. OmitReaderAnnotations bool - // AddSeqIndentAnnotation if true adds kioutil.SeqIndentAnnotation to each resource - AddSeqIndentAnnotation bool + // PreserveSeqIndent if true adds kioutil.SeqIndentAnnotation to each resource + PreserveSeqIndent bool // 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 @@ -174,6 +174,10 @@ func splitDocuments(s string) ([]string, error) { } func (r *ByteReader) Read() ([]*yaml.RNode, error) { + if r.PreserveSeqIndent && r.OmitReaderAnnotations { + return nil, errors.Errorf(`"PreserveSeqIndent" option adds a reader annotation, please set "OmitReaderAnnotations" to false`) + } + output := ResourceNodeSlice{} // by manually splitting resources -- otherwise the decoder will get the Resource @@ -278,7 +282,7 @@ func (r *ByteReader) decode(originalYAML string, index int, decoder *yaml.Decode if !r.OmitReaderAnnotations { r.SetAnnotations[kioutil.IndexAnnotation] = fmt.Sprintf("%d", index) - if r.AddSeqIndentAnnotation { + if r.PreserveSeqIndent { // derive and add the seqindent annotation seqIndentStyle := seqIndentAnno(node, originalYAML) if seqIndentStyle != "" { diff --git a/kyaml/kio/byteio_reader_test.go b/kyaml/kio/byteio_reader_test.go index e0ae8c0c2..396fa5880 100644 --- a/kyaml/kio/byteio_reader_test.go +++ b/kyaml/kio/byteio_reader_test.go @@ -806,12 +806,17 @@ items: } } -func TestByteReader_AddSeqIndent(t *testing.T) { +// TestByteReader_AddSeqIndentAnnotation tests if the internal.config.kubernetes.io/seqindent +// annotation is added to resources appropriately, the expectedOutput indentation may not +// match with the annotation as it is not using byteio_writer, this test will only verify +// byteio_reader behavior to add annotation +func TestByteReader_AddSeqIndentAnnotation(t *testing.T) { type testCase struct { - name string - err string - input string - expectedOutput string + name string + err string + input string + expectedOutput string + OmitReaderAnnotations bool } testCases := []testCase{ @@ -911,16 +916,46 @@ metadata: internal.config.kubernetes.io/seqindent: 'compact' `, }, + { + name: "error if conflicting options", + input: `apiVersion: apps/v1 +kind: Deployment +spec: +- foo +- bar +- baz +env: + - foo + - bar +`, + expectedOutput: `apiVersion: apps/v1 +kind: Deployment +spec: +- foo +- bar +- baz +env: + - foo + - bar +`, + OmitReaderAnnotations: true, + err: `"PreserveSeqIndent" option adds a reader annotation, please set "OmitReaderAnnotations" to false`, + }, } for i := range testCases { tc := testCases[i] t.Run(tc.name, func(t *testing.T) { rNodes, err := (&ByteReader{ - OmitReaderAnnotations: false, - AddSeqIndentAnnotation: true, - Reader: bytes.NewBuffer([]byte(tc.input)), + OmitReaderAnnotations: tc.OmitReaderAnnotations, + PreserveSeqIndent: true, + Reader: bytes.NewBuffer([]byte(tc.input)), }).Read() + if tc.err != "" { + assert.Error(t, err) + assert.Equal(t, tc.err, err.Error()) + return + } assert.NoError(t, err) actual, err := rNodes[0].String() assert.NoError(t, err) diff --git a/kyaml/kio/byteio_readwriter_test.go b/kyaml/kio/byteio_readwriter_test.go index 20f8cf8fd..040b8d81d 100644 --- a/kyaml/kio/byteio_readwriter_test.go +++ b/kyaml/kio/byteio_readwriter_test.go @@ -540,7 +540,7 @@ spec: w := tc.instance w.Writer = &out w.Reader = &in - w.AddSeqIndentAnnotation = true + w.PreserveSeqIndent = true nodes, err := w.Read() if !assert.NoError(t, err) { diff --git a/kyaml/kio/pkgio_reader.go b/kyaml/kio/pkgio_reader.go index 3dd9b99c7..f2ed9f004 100644 --- a/kyaml/kio/pkgio_reader.go +++ b/kyaml/kio/pkgio_reader.go @@ -40,8 +40,8 @@ type LocalPackageReadWriter struct { KeepReaderAnnotations bool `yaml:"keepReaderAnnotations,omitempty"` - // AddSeqIndentAnnotation if true adds kioutil.SeqIndentAnnotation to each resource - AddSeqIndentAnnotation bool + // PreserveSeqIndent if true adds kioutil.SeqIndentAnnotation to each resource + PreserveSeqIndent bool // PackagePath is the path to the package directory. PackagePath string `yaml:"path,omitempty"` @@ -82,14 +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, - AddSeqIndentAnnotation: r.AddSeqIndentAnnotation, + PackagePath: r.PackagePath, + MatchFilesGlob: r.MatchFilesGlob, + IncludeSubpackages: r.IncludeSubpackages, + ErrorIfNonResources: r.ErrorIfNonResources, + SetAnnotations: r.SetAnnotations, + PackageFileName: r.PackageFileName, + FileSkipFunc: r.FileSkipFunc, + PreserveSeqIndent: r.PreserveSeqIndent, }.Read() if err != nil { return nil, errors.Wrap(err) @@ -182,8 +182,8 @@ type LocalPackageReader struct { // the file FileSkipFunc LocalPackageSkipFileFunc - // AddSeqIndentAnnotation if true adds kioutil.SeqIndentAnnotation to each resource - AddSeqIndentAnnotation bool + // PreserveSeqIndent if true adds kioutil.SeqIndentAnnotation to each resource + PreserveSeqIndent bool } var _ Reader = LocalPackageReader{} @@ -270,11 +270,11 @@ func (r *LocalPackageReader) readFile(path string, _ os.FileInfo) ([]*yaml.RNode defer f.Close() rr := &ByteReader{ - DisableUnwrapping: true, - Reader: f, - OmitReaderAnnotations: r.OmitReaderAnnotations, - SetAnnotations: r.SetAnnotations, - AddSeqIndentAnnotation: r.AddSeqIndentAnnotation, + DisableUnwrapping: true, + Reader: f, + OmitReaderAnnotations: r.OmitReaderAnnotations, + SetAnnotations: r.SetAnnotations, + PreserveSeqIndent: r.PreserveSeqIndent, } return rr.Read() } diff --git a/kyaml/kio/pkgio_reader_test.go b/kyaml/kio/pkgio_reader_test.go index 0ac6c434b..e5462d88a 100644 --- a/kyaml/kio/pkgio_reader_test.go +++ b/kyaml/kio/pkgio_reader_test.go @@ -337,7 +337,7 @@ g: } } -func TestLocalPackageReader_Read_addSeqIndentAnnotation(t *testing.T) { +func TestLocalPackageReader_Read_PreserveSeqIndent(t *testing.T) { s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c")) defer s.Clean() s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA) @@ -351,7 +351,7 @@ func TestLocalPackageReader_Read_addSeqIndentAnnotation(t *testing.T) { } for _, p := range paths { // empty path - rfr := LocalPackageReader{PackagePath: p.path, AddSeqIndentAnnotation: true} + rfr := LocalPackageReader{PackagePath: p.path, PreserveSeqIndent: true} nodes, err := rfr.Read() if !assert.NoError(t, err) { return