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

🌱 Enable optionalorrequired linter #11909

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sivchari
Copy link
Member

What this PR does / why we need it:

Part of #11834

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

/area api

@k8s-ci-robot k8s-ci-robot added area/api Issues or PRs related to the APIs cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 27, 2025
@JoelSpeed JoelSpeed mentioned this pull request Feb 27, 2025
10 tasks
Comment on lines +73 to +76
- path: "api/v1alpha1/*|api/v1alpha3/*|api/v1beta1/*"
text: "optionalorrequired"
linters:
- kal
Copy link
Contributor

Choose a reason for hiding this comment

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

We should be able to retrofit this linter without making any breaking changes.

What is the result of trying the make lint-api-fix for example? And how many unmarked fields does that leave?

Copy link
Member Author

Choose a reason for hiding this comment

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

make-lint-api can't fix since the all reporting is this report and it doesn't have suggested-fixes

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, so how many issues does it report? Going to be quite a few then that aren't marked optional or required?

We should be able to go through and add them, but that will take some time.

@sbueringer since you've been following this, thing this is juice worth the squeeze or wait until v1beta2?

Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to get everything that we possibly can out of the way ASAP.

I don't want to fix a huge number of linter findings in the PR that adds the v1beta2 types if we can somehow avoid it.

Copy link
Member

Choose a reason for hiding this comment

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

I think this change is also pretty safe as there simply should be 0 changes to the generated CRDs after we added the markers everywhere

Copy link
Member Author

Choose a reason for hiding this comment

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

We met 318 exceptions.

make lint-api | grep 'optionalorrequired' | wc -l
fatal: No names found, cannot describe anything.
INFO golangci-lint has version v1.63.4-custom-gcl built with go1.23.6 from ? on 2025-02-26 11:02:08.494916 +0000 UTC
INFO [config_reader] Used config file .golangci-kal.yml
INFO Loaded : kal
INFO [lintersdb] Active 1 linters: [kal]
INFO [loader] Go packages loading at mode 8767 (deps|name|types_sizes|compiled_files|files|imports|exports_file) took 1.7007665s
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 65.671958ms
INFO [linters_context/goanalysis] analyzers took 0s with no stages
INFO [runner] Issues before processing: 6204, after processing: 318
INFO [runner] Processors filtering stat (in/out): max_from_linter: 318/318, filename_unadjuster: 6204/6204, exclude: 2319/2319, nolint: 318/318, max_per_file_from_linter: 318/318, source_code: 318/318, path_shortener: 318/318, cgo: 6204/6204, autogenerated_exclude: 2319/2319, identifier_marker: 2319/2319, uniq_by_line: 318/318, max_same_issues: 318/318, severity-rules: 318/318, fixer: 318/318, path_prettifier: 6204/6204, skip_files: 6204/2319, exclude-rules: 2319/318, diff: 318/318, invalid_issue: 6204/6204, skip_dirs: 2319/2319, path_prefixer: 318/318, sort_results: 318/318
INFO [runner] processing took 69.128754ms with stages: identifier_marker: 27.185292ms, path_prettifier: 11.064709ms, autogenerated_exclude: 9.470958ms, exclude-rules: 7.659791ms, skip_files: 5.15125ms, nolint: 5.080751ms, skip_dirs: 1.009042ms, path_shortener: 941.375µs, source_code: 857.999µs, invalid_issue: 303.958µs, cgo: 197.001µs, filename_unadjuster: 157.167µs, uniq_by_line: 37.25µs, max_per_file_from_linter: 10.042µs, max_same_issues: 875ns, sort_results: 334ns, fixer: 250ns, diff: 250ns, exclude: 209ns, max_from_linter: 167ns, severity-rules: 42ns, path_prefixer: 42ns
INFO [runner] linters took 524.132291ms with stages: goanalysis_metalinter: 454.612917ms
INFO File cache stats: 28 entries of total size 419.2KiB
INFO Memory: 25 samples, avg is 43.1MB, max is 96.7MB
INFO Execution took 2.3149185s
make: *** [lint-api] Error 1
     318

Should be it fine to mark manyally ? Or Do I improve KAL to use --fix optionl (I'm not sure it's possible)

Copy link
Contributor

Choose a reason for hiding this comment

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

I have deliberately avoided adding the manual fix for having neither optional nor required, because it may not be obvious/correct.

We can infer from the omitempty generally whether a field is optional or not, but, it also depends on whether there is a package level optional marker, as that would then make fields that are not omitempty also optional.

We could probably script/fix this, but we would need to make sure there are no accidents, so we may as well go through each of these by hand IMO

Theoretically, as long as the CRD generator doesn't pick up changes, then we are correct

Copy link
Member

Choose a reason for hiding this comment

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

I think this would be non-trivial to implement in KAL, basically it would have to go backwards from who controller-tools decides today when a field becomes optional / required.

I think also this linter should not depend on the logic in controller-gen.

So I think manual is the only option

Copy link
Member

@sbueringer sbueringer Feb 28, 2025

Choose a reason for hiding this comment

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

@sivchari What you can try to hack here manually is to use a rough heuristic like omitempty => optional everything else required. Then run make generate-manifests and adjust the markers that lead to changes in the generated CRDs

Copy link
Member

@sbueringer sbueringer Feb 28, 2025

Choose a reason for hiding this comment

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

One important note. Let's not do this for internal/apis. But I think they are probably already excluded anyway?
(EDIT: ah yeah we exclude them already)

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 28, 2025
@sivchari sivchari force-pushed the enable-optionalorrequired branch from b85a8d2 to d26f462 Compare February 28, 2025 02:24
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign fabriziopandini for approval. For more information see the 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 k8s-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Feb 28, 2025
@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api Issues or PRs related to the APIs cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. 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.

4 participants