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

Add support for generating lint output #142

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

crandles
Copy link

@crandles crandles commented Oct 5, 2024

What type of PR is this?

/kind feature

What this PR does / why we need it:

This adds support for rendering validation errors into a simple "lint error format" (e.g. see errorformat), with the intention of piping the lint output to reviewdog to post PR comments pertaining to any validation errors.

Which issue(s) this PR fixes:

N/A

Special notes for your reviewer:

This does pull in a new import, goccy/go-yaml. (I see it used in some other kubernetes projects)
I was unable to get the otherwise imported yaml packages to get accurate line numbers and have a way to search via field.

Does this PR introduce a user-facing change?

Adds a new output format, `--output lint`, that will render validation errors using the `%f:%l:%c: %m` 'errorformat', to be used with compatible lint tools.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 5, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: crandles
Once this PR has been reviewed and has the lgtm label, please assign jpbetz for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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
Copy link
Contributor

Welcome @crandles!

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/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Oct 5, 2024
@crandles
Copy link
Author

crandles commented Oct 6, 2024

Example usage with reviewdog:

      - name: Run reviewdog
        env:
          REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          go install github.com/reviewdog/reviewdog/cmd/reviewdog@latest
          kubectl validate path/to/yaml/* -o lint | $(go env GOPATH)/bin/reviewdog -f=golint -reporter=github-pr-check -fail-level=any -tee

https://github.com/crandles/kubectl-validate-example/blob/2cd070ac781bd87eb2dd36a4c43ab942922bc6b2/.github/workflows/pr.yaml#L18-L23

Screenshot 2024-10-06 at 8 44 30 AM

Although I'd plan to craft an action for this, in the long run.

@crandles crandles mentioned this pull request Oct 9, 2024
@apelisse
Copy link
Contributor

I really like the direction, I'm very sad that we have to add 14k lnes of code just to be able to print filename:line:col:error. On the other hand, I realize that the current go implementation of the yaml loader doesn't give us line numbers. So at the end of the day I think this goes in the right direction.

@crandles
Copy link
Author

crandles commented Oct 16, 2024

Definitely agree. It's probably worth another attempt at pulling the line numbers out of one of the currently vendor-ed decoders. Perhaps I can at least share my failed attempt, if nothing else. Let me know if that would be valuable.

Happy to spend a little effort to get this right.

An alternative might be to switch yaml decoders outright, but that seemed more than necessary.

@crandles
Copy link
Author

gopkg.in/yaml.v3, which is already imported, does support a way to decode yaml line numbers (see line/column fields): https://pkg.go.dev/gopkg.in/yaml.v3#Node, however it is not readily traversable.

There is no alternative for yaml.PathString that I can find for the gopkg.in/yaml.v3 package, so that code would need to be implemented. Given the existence of yaml anchors, you'd need a multi-step or recursive process. I believe this is when I turned to the goccy package which provided both capabilities.

@crandles
Copy link
Author

https://pkg.go.dev/github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath looks like the top alternative for easily traversing a yaml.Node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants