Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix YamlToObject file splitting. #1298

Merged
merged 1 commit into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/engine/renderer/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// If the type is not known in the scheme, it tries to parse it as Unstructured
// TODO(av) could we use something else than a global scheme here? Should we somehow inject it?
func YamlToObject(yaml string) (objs []runtime.Object, err error) {
sepYamlfiles := strings.Split(yaml, "---")
sepYamlfiles := strings.Split(yaml, "\n---\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering why we have to split the file here. Isn't this something the YAML decoder should take care of? Looking at the documentation of the decoder it looks like this is a supported use case. The Decode functions unmarshals the "next object from the underlying stream". I didn't test this though, but maybe a for-loop that calls Decode would do the splitting?

Not yours, but also wondering why we're using a YAMLOrJSONDecoder when there's a YAMLDecoder?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What Jan said ^^ There is a splitYamlDocument method that should handle multiple YAML documents. Funny enough, the separator it uses is defined as const yamlSeparator = "\n---" so only one \n.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we're using a YAMLOrJSONDecoder when there's a YAMLDecoder?

Turns out YAMLDecoder is not a decoder, it's a ReadCloser 😆
Or at least not in the sense that YAMLOrJSONDecoder provides a Decode function.

for _, f := range sepYamlfiles {
if f == "\n" || f == "" {
// ignore empty cases
Expand Down
7 changes: 6 additions & 1 deletion pkg/engine/renderer/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
)

func TestParseKubernetesObjects_UnknownType(t *testing.T) {
_, err := YamlToObject(`apiVersion: monitoring.coreos.com/v1
objects, err := YamlToObject(`apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
app: prometheus-operator
release: prometheus-kubeaddons
name: spark-cluster-monitor
annotations:
foo: |-
---
multiline
spec:
endpoints:
- interval: 5s
Expand All @@ -25,6 +29,7 @@ spec:
if err != nil {
t.Errorf("Expecting no error but got %s", err)
}
assert.Equal(t, 1, len(objects))
}

func TestParseKubernetesObjects_KnownType(t *testing.T) {
Expand Down