-
Notifications
You must be signed in to change notification settings - Fork 41
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
Ignore empty documents. #113
Ignore empty documents. #113
Conversation
Welcome @ah8ad3! |
/cc @alexzielenski |
Looking thru the code for this PR i see we already have logic to skip empty YAML documents, I am curious how that bug persists... kubectl-validate/pkg/cmd/validate.go Lines 261 to 263 in 8b66d4c
|
@ah8ad3 could you add a test case for the linked issue ( |
Good catch, lazy of me to not giving it enough attention. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the test cases! Just one comment about HasPrefix.
It surprised me that a document with just ---
would include ---
after being run through SplitYAMLDocuments
Usually the terminator is stripped by SplitYAMLDocuments
, but reading over the implementation, it looks like the terminator is included in cases where the doc is empty: e.g. ---\n
yields a single document output with contents ---
where instead I would expect two empty documents.:
kubectl-validate/vendor/k8s.io/apimachinery/pkg/util/yaml/decoder.go
Lines 318 to 344 in acf913d
sep := len([]byte(separator)) | |
if i := bytes.Index(line, []byte(separator)); i == 0 { | |
// We have a potential document terminator | |
i += sep | |
trimmed := strings.TrimSpace(string(line[i:])) | |
// We only allow comments and spaces following the yaml doc separator, otherwise we'll return an error | |
if len(trimmed) > 0 && string(trimmed[0]) != "#" { | |
return nil, YAMLSyntaxError{ | |
err: fmt.Errorf("invalid Yaml document separator: %s", trimmed), | |
} | |
} | |
if buffer.Len() != 0 { | |
return buffer.Bytes(), nil | |
} | |
if err == io.EOF { | |
return nil, err | |
} | |
} | |
if err == io.EOF { | |
if buffer.Len() != 0 { | |
// If we're at EOF, we have a final, non-terminated line. Return it. | |
return buffer.Bytes(), nil | |
} | |
return nil, err | |
} | |
buffer.Write(line) | |
} |
Empty docs bypass
if buffer.Len() != 0 {
check and instead have their separator written to buffer
Which seems like a bug to me, but unlikely to be fixed upstream.
I think this workaround is good, IsEmptyYamlDocument
returns true if every line is either a document terminator or a comment.
@@ -30,7 +30,7 @@ func SplitYamlDocuments(fileBytes Document) ([]Document, error) { | |||
func IsEmptyYamlDocument(document Document) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some godoc explaining that this function returns true for comment-only single documents, and strings with multiple documents where all docs are comment-only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment-only single documents
But it'll work for files like this too:
1:
# test doc
# test doc
---
# test doc
# test doc
---
# test doc
# test doc
2:
---
---
I think gendoc should be like this:
This function validate emptiness of yaml if it only contains comment-only documents or strings with multiple documents.
If we want to make the second example invalid we should change the logic here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes those two cases are covered by the second clause of my suggestion:
Could you add some godoc explaining that this function returns true for comment-only single documents, and strings with multiple documents where all docs are comment-only.
I'm not particular about the specific wording, as long as it mentions it works on multiple document yaml files, and comments are still considered empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, PTAL
e84d58c
to
a8164ff
Compare
Commits squashed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
/approve
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: ah8ad3, alexzielenski The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
CI is unhappy with a comment it seems |
Signed-off-by: ah8ad3 <ah8ad3@gmail.com> Add '---' to IsEmptyYamlDocument function as a condition to ignore empty yaml files. Signed-off-by: ah8ad3 <ah8ad3@gmail.com> refactor: add gendoc for IsEmptyYamlDocument Signed-off-by: ah8ad3 <ah8ad3@gmail.com> Update comment Signed-off-by: ah8ad3 <ah8ad3@gmail.com> Fix: gofmt document Signed-off-by: ah8ad3 <ah8ad3@gmail.com>
a8164ff
to
7793113
Compare
It was a odd one, my IDE didn't warn me that :|. Gofmt was not happy with |
/lgtm |
What type of PR is this?
/kind bug
What this PR does / why we need it:
Ignore empty documents.
Which issue(s) this PR fixes:
Fixes #109
Special notes for your reviewer:
I am not sure ignoring would effect other things or not (we don't have any failing test), please point out if it would.
Does this PR introduce a user-facing change?