Skip to content

Conversation

@sivchari
Copy link
Member

@sivchari sivchari commented Dec 6, 2025

Close #22

Implemented new linter defaults

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sivchari

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 approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Dec 6, 2025
Copy link
Contributor

@JoelSpeed JoelSpeed left a comment

Choose a reason for hiding this comment

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

Thanks for this one, left some feedback inline

// Initializer returns the AnalyzerInitializer for this
// Analyzer so that it can be added to the registry.
func Initializer() initializer.AnalyzerInitializer {
return initializer.NewInitializer(name, Analyzer, 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 think we want this to be configurable. We probably should align with the optionalorrequired linter in having a way to choose which default marker is preferred at least.

I think we also need a way to avoid the use of omitzero, some folks may not be ready for omitzero in their projects


// GoodK8sDefaultField uses +k8s:default which is also acceptable.
// +optional
// +k8s:default="default-value"
Copy link
Contributor

Choose a reason for hiding this comment

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

How do we handle this in optionalorrequired? I think we prefer a single marker rather than both. Bear in mind that +default and +k8s:default are not semantically equivalent for built-in types. Not all tools support both yet, and I think for some time at least, in K/K we will want to have +default and +k8s:default together.

For CRDs I'd expect them to be exclusive

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have some cases that show what happens for a required marked field? I'd expect we want to suggest that shouldn't have required and switch to optional?

Comment on lines +37 to +34
// NoDefaultField is a regular field without default - should not be flagged.
// +optional
NoDefaultField string `json:"noDefaultField,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a required field without omitempty, should also not be flagged?

InlineField B `json:",inline"`

// IgnoredField with json ignore tag should not require omitempty.
// +optional
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe remove this too to show it doesn't complain optional is missing?

}
}

func checkDefaultOptional(pass *analysis.Pass, field *ast.Field, markersAccess markershelper.Markers, qualifiedFieldName string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should also check that it's not required

{
Pos: jsonTagInfo.Pos,
End: jsonTagInfo.End,
NewText: fmt.Appendf([]byte{}, "%s,omitempty,omitzero", jsonTagInfo.RawValue),
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 think we want to just apply omitzero.

Omitzero should be applied for structs only
Omitempty for all other types

Plus a configuration to switch structs to omitempty as well for projects supporting older Go versions still

@JoelSpeed
Copy link
Contributor

/approve cancel

Just to prevent an accidental merge while there's feedback outstanding

@sivchari sivchari force-pushed the new-linter-defaults branch from c7d211e to 3d678fe Compare December 12, 2025 04:10
@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Dec 12, 2025
Signed-off-by: sivchari <shibuuuu5@gmail.com>
@sivchari sivchari force-pushed the new-linter-defaults branch from 3d678fe to 0c23a88 Compare December 12, 2025 04:50
@sivchari
Copy link
Member Author

Hi, @JoelSpeed
Thak you for reviewing. Updated it.

@sivchari
Copy link
Member Author

/hold

Same above reason.

@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 Dec 13, 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. 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: defaults

3 participants