kubernetes-sigs / kubectl-validate

Apache License 2.0
161 stars 40 forks source link

Add support for generating lint output #142

Open crandles opened 1 month ago

crandles commented 1 month ago

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 commented 1 month ago

[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: - **[OWNERS](https://github.com/kubernetes-sigs/kubectl-validate/blob/main/OWNERS)** 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 commented 1 month ago

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

crandles commented 1 month ago

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.

apelisse commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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