Skip to content

linters: add forbiddenmarkers and nonullable linters #111

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

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

Conversation

everettraven
Copy link
Contributor

Adds a disabled by default forbiddenmarkers linter that has configuration options to specify marker identifiers that should never exist on a type/field. Suggests fixes to remove the forbidden markers.

Adds an enabled by default nonullable linter that wraps the forbiddenmarkers linter and configures the nullable marker to be forbidden.

Fixes #100

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: everettraven
Once this PR has been reviewed and has the lgtm label, please assign jpbetz 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 requested a review from JoelSpeed June 26, 2025 13:48
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 26, 2025
@k8s-ci-robot k8s-ci-robot requested a review from jpbetz June 26, 2025 13:48
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 26, 2025
@everettraven
Copy link
Contributor Author

I'll need to add the linter documentation to the new location as well :P

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jun 26, 2025
const name = "forbiddenmarkers"

type analyzer struct {
forbiddenMarkers []string
Copy link
Contributor

Choose a reason for hiding this comment

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

What if I wanted to forbid a marker only if it had a certain parameter? Or a certain value?

E.g. I want to prevent someone from using a particular kubebuilder:validation:Format, would this allow me to do that?

Or I wanted to say, you can't use the optionalOldSelf property on an XValidation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good questions. This would not allow that for now. I think those are reasonable use cases and make sense to consider.

I can add some configuration options to allow for this type of customization.

Comment on lines 33 to 34
analyzer.Name = name
analyzer.Doc = "Check that nullable marker is not present on any types or fields."
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe create a constructor on the forbidden markers side that allows you to pass options for Name and Doc, rather than overriding them here? Then this just becomes

return NewAnalyzerWithOpts(forbidden.Opts{
  Name: ...
  Doc: ...
  Markers: []string{...}
})

Or even, you could just call that inside the initializer.

Comment on lines +18 to +21
* The `nonullable` linter ensures that types and fields do not have the `nullable` marker.
*
* Fixes are suggested to remove the `nullable` marker.
*
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the asterisks at the beginning of each line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copied format from another linter doc.go - but also I think my editor does this by default. I can strip the asterisks at the beginning of each line if necessary

Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't matter to me, but normally with /* */ syntax I didn't think it was needed

Comment on lines 23 to 35
// Initializer returns the AnalyzerInitializer for this
// Analyzer so that it can be added to the registry.
func Initializer() initializer {
return initializer{}
}

// intializer implements the AnalyzerInitializer interface.
type initializer struct{}

// Name returns the name of the Analyzer.
func (initializer) Name() string {
return name
}

// Init returns the intialized Analyzer.
func (initializer) Init(_ config.LintersConfig) (*analysis.Analyzer, error) {
return newAnalyzer(), nil
}

// Default determines whether this Analyzer is on by default, or not.
func (initializer) Default() bool {
return true
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm starting to wonder if we should create a util to generate the initializer from a func 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like that idea, but to keep the scope of this PR tightened to these two linters I'd suggest that be a follow up.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is now implemented, PR will need a rebase

@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 27, 2025
@everettraven
Copy link
Contributor Author

Working on allowing configuration of forbidden attributes and values.

/hold

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 9, 2025
@everettraven everettraven force-pushed the linter/forbiddenmarkers branch from 36bf9d5 to 5d01549 Compare July 25, 2025 15:40
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 25, 2025
@everettraven everettraven force-pushed the linter/forbiddenmarkers branch from 5d01549 to 4652204 Compare July 25, 2025 19:55
@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jul 25, 2025
@everettraven
Copy link
Contributor Author

This is mostly ready to go, just have to fix linting issues and squash commits.

return field.ErrorList{field.Required(fldPath, "configuration is required for the forbiddenmarkers linter when it is enabled")}
}

fieldErrors := field.ErrorList{}
Copy link
Contributor

Choose a reason for hiding this comment

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

Implementation of validation missing?

}

func validateMarkers(fldPath *field.Path, markers ...Marker) field.ErrorList {
childPath := fldPath.Child("markers")
Copy link
Contributor

Choose a reason for hiding this comment

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

Normally the child element is called by the parent, rather than here


for i, marker := range markers {
indexPath := childPath.Index(i)
if knownMarkers.Has(marker.Identifier) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What if I wanted to say identifier foo is allow, unless it has attribute A and B, or A and C? Are we sure the identifiers must be unique? Or should they be unique only when the attributes match?

For example, what if I wanted to forbid Blue Apples, or Green Oranges, but allow Red Apples and Orange Oranges? Would that be configurable today?

Copy link
Contributor Author

@everettraven everettraven Jul 29, 2025

Choose a reason for hiding this comment

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

Good question. I'd have to think through potential scenarios a bit further,but giving this a quick analysis.

In this example we essentially want the markers +custom:marker:fruit=apple,color=blue and +custom:marker:fruit=orange,color=green to be invalid. I think boils down to a configuration of:

markers:
  - identifier: "custom:marker"
    attributes: # entries join as an AND operation
      - name: fruit
         values: # OR operation
           - apple
           - orange
      - name: color
         values: # OR operation
           - blue
           - green

This basically becomes a boolean conditional chain of (fruit == apple || fruit == orange) && (color == blue || color == green) which means that it would successfully work for the exact constraints of your example, but would also forbid Green Apples and Blue Oranges.

I could imagine a case where you may want to forbid something like Blue Apples and Green Oranges but still want to allow something like Green Apples as valid.

Maybe doing something like a "rule set" that does an OR (basically a configured set of the existing ANDed attribute rules) would allow for that behavior while keeping all configuration under a unique identifier.

For example, to handle the case of forbidding Blue Apples and Green Oranges, but allowing Green Apples you might be able to do something like:

markers:
  - identifier: "custom:marker"
    ruleSets: # OR operation
      - attributes: # entries join as an AND operation
        - name: fruit
           values: # OR operation
             - apple
        - name: color
           values: # OR operation
             - blue
      - attributes: # entries join as an AND operation
        - name: fruit
           values: # OR operation
             - orange
        - name: color
           values: # OR operation
             - green

Which would essentially become a conditional of (fruit == apple && color == blue) || (fruit == orange && color == green) which would work.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I think the ruleset idea could work, shall we give that a go?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'll move this in that direction. I probably won't have time to do this until later this week though

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. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. 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.

New Linter: nonullable
3 participants