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 jsontags lint of KAL #11890

Merged
merged 1 commit into from
Mar 4, 2025

Conversation

sivchari
Copy link
Member

What this PR does / why we need it:

This is part of #11834
Enabled jsontags linter

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 #

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-area PR is missing an area label size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Feb 22, 2025
@@ -26,7 +26,7 @@ linters-settings:
# Linters below this line are disabled, pending conversation on how and when to enable them.
# - "commentstart" # Ensure comments start with the serialized version of the field name.
# - "integers" # Ensure only int32 and int64 are used for integers.
# - "jsontags" # Ensure every field has a json tag.
- "jsontags" # Ensure every field has a json tag.
Copy link
Member

Choose a reason for hiding this comment

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

Please move this up below conditions. This is the section for the disabled linters

@@ -61,7 +61,36 @@ issues:
- path-except: "api/*"
linters:
- kal
# apiwarnings contains a `api` characters in the path, but it's not an API folder.
- path: "util/apiwarnings/*"
Copy link
Member

Choose a reason for hiding this comment

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

I don't follow why apiwarnings/ is apparently matching api/*

@JoelSpeed Am I missing something? :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, it doesn't appear to match with the path exception so I'm not sure why this package would be picked up, the pattern is api/*

- path-except: "api/*"

Copy link
Member

Choose a reason for hiding this comment

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

Definitely shows these findings if I drop this exclude

util/apiwarnings/handler.go:33:2: jsontags: field Logger is missing json tag (kal)
        Logger logr.Logger
        ^
util/apiwarnings/handler.go:37:2: jsontags: field Expressions is missing json tag (kal)
        Expressions []regexp.Regexp
        ^

Copy link
Member

Choose a reason for hiding this comment

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

Works fine with this

  # KAL should only run on API folders.
  - path-except: "api//*"
    linters:
      - kal

Not sure if I want to know why, @sivchari can you drop this exclude and adjust the path-except above please? Thx! :)

Copy link
Member

Choose a reason for hiding this comment

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

We should probably also change all the other / into // in other path values

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if I want to know why

How interesting 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Very unintuitive

Copy link
Member Author

Choose a reason for hiding this comment

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

Thx!
How confusing :(

Comment on lines 72 to 89
- path: "api/v1beta1/*"
text: "field XPreserveUnknownFields json tag does not match pattern"
linters:
- kal
- path: "api/v1beta1/*"
text: "field XValidations json tag does not match pattern"
linters:
- kal
- path: "api/v1beta1/*"
text: "field XMetadata json tag does not match pattern"
linters:
- kal
- path: "api/v1beta1/*"
text: "field XIntOrString json tag does not match pattern"
linters:
- kal
Copy link
Member

Choose a reason for hiding this comment

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

Let's merge them all into one regex match
Let's also ignore them in general and not only for v1beta1
Let's add a comment that we intentionally align these fields to the corresponding fields in CustomResourceDefinitions

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 this makes sense to me

@sbueringer sbueringer added the area/misc Issues or PRs not related to any other area label Feb 24, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/needs-area PR is missing an area label label Feb 24, 2025
linters:
- kal
- path: "api/v1beta1/*|api/v1alpha1/*"
- path: "api//*"
Copy link
Contributor

Choose a reason for hiding this comment

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

This one still wants to be v1beta1 so that when we introduce new v1beta2 APIs this is enforced

- kal
- path: "api/v1beta1/*"
text: "field XIntOrString json tag does not match pattern"
source: "X.*"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is too broad, can we be specific about the field names we are excepting here please

Copy link
Member

Choose a reason for hiding this comment

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

Absolutely. We have something like this in our main .golangci.yml. This here should be similar

    text: "SA1019: (bootstrapv1.ClusterStatus|KubeadmConfigSpec.UseExperimentalRetryJoin|scope.Config.Spec.UseExperimentalRetryJoin|DockerMachine.Spec.Bootstrapped|machineStatus.Bootstrapped|dockerMachine.Spec.Backend.Docker.Bootstrapped|devMachine.Spec.Backend.Docker.Bootstrapped|c.TopologyPlan|clusterv1.ClusterClassVariableMetadata|(variable|currentDefinition|specVar|newVariableDefinition|statusVarDefinition).Metadata) is deprecated"

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly. fixed.

linters:
- kal
# These reporting are false positives.
# When the following issue is fixed, remove the exclusion.
# https://github.com/JoelSpeed/kal/issues/43
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't actually understand what that issue is? The PR merged to fix this no?

Copy link
Member

Choose a reason for hiding this comment

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

Could it be that we just didn't bump KAL since the fix was merged?

Copy link
Member Author

Choose a reason for hiding this comment

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

I use the 1ad4d048de47e0fa31c48e8f5d2ebe9eded0f4e4 in e48fa3c.
I expect this rule is removed since the extractjsontags analyzer is fixed, but this report are still be existing.
Am I missing something ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok I see, we fixed the comments linter, but not the json tags linter itself, will need a small fix there too

Copy link
Member Author

@sivchari sivchari Feb 26, 2025

Choose a reason for hiding this comment

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

I open the pr (this is wip, yet) to fix jsontags linter.

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Feb 26, 2025
Comment on lines 42 to 43
jsonTags:
jsonTagRegex: "^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)*$" # The default regex is appropriate for our use case.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to uncomment this, or shall we just leave it commented so that if the default configuration changes over time we continue to use the default?

Copy link
Member Author

Choose a reason for hiding this comment

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

The CI will be failed if KAL changes default configuration. Then we'll end up adding the this configuration to manage the compability.
So I think it's better to keep this configuration.

Copy link
Member

Choose a reason for hiding this comment

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

CI will only fail if we bump KAL on the PR where we bump KAL

Copy link
Member

Choose a reason for hiding this comment

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

@JoelSpeed How do we keep the documented default values here up-to-date if they change in KAL?

Copy link
Member

Choose a reason for hiding this comment

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

(In general I'm in favor of using the default value here from KAL)

Copy link
Contributor

Choose a reason for hiding this comment

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

@JoelSpeed How do we keep the documented default values here up-to-date if they change in KAL?

Open to suggestions, I don't know at the moment.

I'm expecting in the not too distant future to move the project to kube-sigs. Once it's moved, and we've had agreement from sig-apimachinery that we are happy with the structure and defaults etc, that we probably will start treating the project like a v1 and therefore breaking changes won't be possible.

So I guess, as we update kal, we add the new sections in to show what the new config might be, but I don't have a great/automatic suggestion at the moment

Copy link
Member

Choose a reason for hiding this comment

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

All good. We can just sync the defaults once we reach "v1"

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed. thx!

@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 26, 2025
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed 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. labels Feb 27, 2025
@JoelSpeed JoelSpeed mentioned this pull request Feb 27, 2025
16 tasks
@@ -39,13 +39,11 @@ linters-settings:
isFirstField: Warn # Require conditions to be the first field in the status struct.
usePatchStrategy: Forbid # Conditions should not use the patch strategy on CRDs.
useProtobuf: Forbid # We don't use protobuf, so protobuf tags are not required.
# jsonTags:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should leave this section commented out so that we can easily see what configuration is available to us, rather than removing the comments. It may help us in the future when trying to work out why something isn't behaving in the way we might expect

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed!

Comment on lines 42 to 48
# jsontags:
# jsonTagRegex: "^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)*$" # The default regex is appropriate for our use case.
# optionalOrRequired:
# preferredOptionalMarker: optional | kubebuilder:validation:Optional # The preferred optional marker to use, fixes will suggest to use this marker. Defaults to `optional`.
# preferredRequiredMarker: required | kubebuilder:validation:Required # The preferred required marker to use, fixes will suggest to use this marker. Defaults to `required`.
# requiredFields:
# pointerPolicy: Warn | SuggestFix # Defaults to `SuggestFix`. We want our required fields to not be pointers.
Copy link
Contributor

Choose a reason for hiding this comment

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

This section has all moved, can we unmove it so that it's at the original indentation level prior to this PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, fixed.

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Feb 27, 2025
@JoelSpeed
Copy link
Contributor

Could we squash some of the fixup style commits please so that we have a set of logical, atomic commits to merge?

@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 27, 2025
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 3, 2025
@JoelSpeed
Copy link
Contributor

You might have better luck with the rebases if we were to order the list of enabled linters alphabetically again 🤔

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 3, 2025
@sbueringer
Copy link
Member

@JoelSpeed seems like KAL is also looking at interfaces

@JoelSpeed
Copy link
Contributor

@sbueringer Yes, it appears there was a regression (see JoelSpeed/kal#48) and then I'm fixing that in JoelSpeed/kal#49

Lets hold this one until 49 is merged please

@JoelSpeed
Copy link
Contributor

The KAL fix is merged, so if we can get this updated with the latest KAL, I think we can move forward to get this merged

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 4, 2025
Signed-off-by: sivchari <[email protected]>
@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Mar 4, 2025
@JoelSpeed
Copy link
Contributor

/lgtm

Thanks @sivchari

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

LGTM label has been added.

Git tree hash: 0ada23263c0b3c55a95a0d82bdb24e0f345436dc

@sbueringer
Copy link
Member

Thx!

/lgtm
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sbueringer

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 Mar 4, 2025
@k8s-ci-robot k8s-ci-robot merged commit dc17a9b into kubernetes-sigs:main Mar 4, 2025
19 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.10 milestone Mar 4, 2025
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. area/misc Issues or PRs not related to any other area cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants