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

Ignore empty documents. #113

Merged
merged 1 commit into from
Apr 23, 2024

Conversation

ah8ad3
Copy link
Contributor

@ah8ad3 ah8ad3 commented Apr 15, 2024

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?

Ignore empty documents and accept them as a valid file.

@k8s-ci-robot k8s-ci-robot added the kind/bug Categorizes issue or PR as related to a bug. label Apr 15, 2024
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 15, 2024
@k8s-ci-robot
Copy link
Contributor

Welcome @ah8ad3!

It looks like this is your first PR to kubernetes-sigs/kubectl-validate 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kubectl-validate has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Apr 15, 2024
@ah8ad3
Copy link
Contributor Author

ah8ad3 commented Apr 15, 2024

/cc @alexzielenski

@alexzielenski
Copy link
Contributor

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...

if utils.IsEmptyYamlDocument(document) {
errs = append(errs, nil)
} else {

@alexzielenski
Copy link
Contributor

alexzielenski commented Apr 15, 2024

@ah8ad3 could you add a test case for the linked issue (---) and investigate why the existing logic for this does not skip the document as expected for that case before we consider removing error checking logic

@ah8ad3
Copy link
Contributor Author

ah8ad3 commented Apr 16, 2024

Good catch, lazy of me to not giving it enough attention.
It seems that IsEmptyYamlDocument only accepts a yaml as a empty if you have some sort of comments with # in it, otherwise it react to it as a non empty file. I changed the condition of that function and added two files to testcases/manifests one with comments and one with only --- and it should work now.
PTAL @alexzielenski.

Copy link
Contributor

@alexzielenski alexzielenski left a 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.:

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.

pkg/utils/yaml.go Outdated Show resolved Hide resolved
@@ -30,7 +30,7 @@ func SplitYamlDocuments(fileBytes Document) ([]Document, error) {
func IsEmptyYamlDocument(document Document) bool {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, PTAL

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Apr 18, 2024
@ah8ad3
Copy link
Contributor Author

ah8ad3 commented Apr 18, 2024

Commits squashed.

Copy link
Contributor

@alexzielenski alexzielenski left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 23, 2024
@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 23, 2024
@alexzielenski
Copy link
Contributor

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>
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 23, 2024
@ah8ad3
Copy link
Contributor Author

ah8ad3 commented Apr 23, 2024

It was a odd one, my IDE didn't warn me that :|. Gofmt was not happy with , at the end of the line.
It should be fixed now. @alexzielenski .

@alexzielenski
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 23, 2024
@k8s-ci-robot k8s-ci-robot merged commit 4ffbdb6 into kubernetes-sigs:main Apr 23, 2024
5 checks passed
@ah8ad3 ah8ad3 deleted the ignore-empty-documents branch April 23, 2024 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validation fails for files with only ---
3 participants