Skip to content

Commit

Permalink
don't crash when encountering non-k8s yaml (#12)
Browse files Browse the repository at this point in the history
just ignore
  • Loading branch information
ryane authored Jul 11, 2020
1 parent 6d3db7b commit adad8eb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package decoder

import (
"io"
"strings"

"github.com/pkg/errors"
"github.com/ryane/kfilt/pkg/resource"
Expand All @@ -26,7 +27,7 @@ func (k *kubernetesDecoder) Decode(in io.Reader) ([]resource.Resource, error) {

decoder := k8syaml.NewYAMLOrJSONDecoder(in, 1024)

for err == nil {
for err == nil || isNotKubernetesYAML(err) {
var out resource.Resource
err = decoder.Decode(&out)
if err == nil && len(out.Object) > 0 {
Expand All @@ -48,3 +49,8 @@ func (k *kubernetesDecoder) Decode(in io.Reader) ([]resource.Resource, error) {

return result, nil
}

func isNotKubernetesYAML(err error) bool {
// no Kind
return strings.Contains(err.Error(), "Object 'Kind' is missing")
}
6 changes: 6 additions & 0 deletions pkg/decoder/list.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
---
# test with empty and non-k8s yaml
# warning below can come from helm charts
---
WARNING: This chart is deprecated
---
apiVersion: v1
kind: List
items:
Expand Down
7 changes: 7 additions & 0 deletions pkg/decoder/test.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
---
---
# test with empty and non-k8s yaml
# warning below can come from helm charts
---
WARNING: This chart is deprecated
---
apiVersion: v1
kind: ServiceAccount
metadata:
Expand Down

0 comments on commit adad8eb

Please sign in to comment.