Skip to content

Commit

Permalink
Ignore empty YAML documents in parser
Browse files Browse the repository at this point in the history
If a document contains only whitespace and we are not able to decode it
into one of the supported schemas, we ignore it and continue. We want to
avoid skipping a type that does not have a registered schema so that we
do not silently ignore errors when package building, but we consider it
safe to assume that a YAML document with no content is safe to skip.

Signed-off-by: hasheddan <[email protected]>
  • Loading branch information
hasheddan committed Jun 9, 2021
1 parent a37f37a commit d76ee9b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"io/ioutil"
"strings"
"unicode"

"github.com/pkg/errors"
"github.com/spf13/afero"
Expand Down Expand Up @@ -114,6 +115,18 @@ func (p *PackageParser) Parse(ctx context.Context, reader io.ReadCloser) (*Packa
if err != nil {
o, _, err := do.Decode(bytes, nil, nil)
if err != nil {
empty := true
for _, b := range bytes {
if !unicode.IsSpace(rune(b)) {
empty = false
break
}
}
// If the YAML document only contains Unicode White Space we
// ignore and do not return an error.
if empty {
continue
}
if anno, ok := reader.(AnnotatedReadCloser); ok {
return pkg, errors.Wrap(err, fmt.Sprintf("%+v", anno.Annotate()))
}
Expand Down
18 changes: 17 additions & 1 deletion pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ var _ Parser = &PackageParser{}
var (
crdBytes = []byte(`apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: test`)

whitespaceBytes = []byte(`---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: test
---
---
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: test`)

Expand All @@ -53,6 +68,7 @@ func TestParser(t *testing.T) {
allBytes := bytes.Join([][]byte{crdBytes, deployBytes}, []byte("\n---\n"))
fs := afero.NewMemMapFs()
_ = afero.WriteFile(fs, "crd.yaml", crdBytes, 0o644)
_ = afero.WriteFile(fs, "whitespace.yaml", whitespaceBytes, 0o644)
_ = afero.WriteFile(fs, "deployment.yaml", deployBytes, 0o644)
_ = afero.WriteFile(fs, "some/nested/dir/crd.yaml", crdBytes, 0o644)
_ = afero.WriteFile(fs, ".crossplane/bad.yaml", crdBytes, 0o644)
Expand Down Expand Up @@ -109,7 +125,7 @@ func TestParser(t *testing.T) {
backend: NewFsBackend(fs, FsDir("."), FsFilters(SkipDirs(), SkipNotYAML(), SkipPath(".crossplane/*"))),
pkg: &Package{
meta: []runtime.Object{deploy},
objects: []runtime.Object{crd, crd},
objects: []runtime.Object{crd, crd, crd, crd},
},
},
"FsBackendAll": {
Expand Down

0 comments on commit d76ee9b

Please sign in to comment.