Skip to content

Conversation

everettraven
Copy link
Contributor

@everettraven everettraven commented Jun 25, 2025

Adds a new linter, namingconventions, that allows for configuration of custom naming conventions to be enforced on both Go type and serialized field names.

Additionally, this PR updates the nophase and notimestamp linters to wrap the namingconventions linter similarly to how the nonullable linter wraps the forbiddenmarkers linter.

This should make future default field naming convention linters much easier to write, while giving users flexibility to add their own custom naming conventions.

@k8s-ci-robot k8s-ci-robot requested review from jpbetz and sivchari June 25, 2025 19:55
@k8s-ci-robot k8s-ci-robot added 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 Jun 25, 2025
@everettraven
Copy link
Contributor Author

Just a PoC, not mergeable

/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 25, 2025
Comment on lines 44 to 88
var defaults = []NamingConvention{
{
Matcher: *regexp.MustCompile("(?i)phase"),
Operation: "Drop",
Message: "phase fields are deprecated and discouraged. conditions should be used instead.",
},
{
Matcher: *regexp.MustCompile("(?i)timestamp"),
Operation: "Replace",
Message: "prefer use of the term 'time' over 'timestamp'",
// replacement of `Time` follows CamelCase principles for field names and JSON tags
// TODO: Handle case where it is _not_ the second word in CamelCase for json tag
Replacement: "Time",
},
{
Matcher: *regexp.MustCompile("(?i)reference"),
Operation: "Replace",
Message: "prefer use of the term 'ref' over 'reference'",
// replacement of `Ref` follows CamelCase principles for field names and JSON tags
// TODO: Handle case where it is _not_ the second word in CamelCase for json tag
Replacement: "Ref",
},
}

type NamingConvention struct {
// Matcher is a regular expression
// used to identify field names
// where this convention applies
Matcher regexp.Regexp

// Replacement is an optional
// string value used to replace the matched content
// in a suggested fix.
// Only used when Operation is Replace.
Replacement string

// Operation is the type of operation that should take place for this
// naming convention.
// One of Drop, Replace.
Operation string

// Message is the message that should be included in the
// linter report when this naming convention is applied.
Message string
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we were to continue with this approach, the thinking is that we would implement a configuration layer for this linter that exposes this NamingConvention type to allow the development of custom naming conventions.

We could also add a configuration option to include/exclude the defaults we have put in place based on the Kubernetes API conventions doc.

@everettraven
Copy link
Contributor Author

/cc @JoelSpeed @sivchari - I'm curious about your thoughts here.

@Karthik-K-N - just a heads up since you have #105 open and I took some liberties to include that convention in this example as well. I don't think this should block your PR from going in (or supersede it). Also happy to hear your thoughts if you have any here.

@Karthik-K-N
Copy link
Contributor

Even I thought a bit about covering multiple linters under a cover, We have few linters doing similar things like nophase, notimestamp, noduration. Only thought came to my mind at that time was, what if user wants to relax one among many linterns. But with your approach of having a configuration file would be handy at that time.

@JoelSpeed
Copy link
Contributor

I'm a fan of the approach being taken for the NoNullable linter, where we still have a top level linter that contains the descriptions and reasoning of why we do/don't do certain things, but under the hood is just configuring another linter. Perhaps we can do similar here

@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 Sep 18, 2025
Comment on lines 58 to 59


Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO: revert this

@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 Sep 19, 2025
that allows for defining custom namingconventions
to be enforced on Go and serialized field names

Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
@everettraven everettraven changed the title poc: generic naming convention linter linter: add namingconventions linter Sep 19, 2025
@everettraven
Copy link
Contributor Author

@JoelSpeed I've spent some time updating this and think this is ready for a review.

@everettraven
Copy link
Contributor Author

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 22, 2025
docs/linters.md Outdated
linterConfig:
namingconventions:
conventions:
- name: englishcolour
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit, probably BritishEnglishColour? Is casing meaningful here?

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 can change it.

Casing will matter here - I'll update the documentation on the field and docs.

Comment on lines 111 to 121
suggestedFixes := []analysis.SuggestedFix{}
suggestedFixes = append(suggestedFixes, analysis.SuggestedFix{
Message: "remove the field",
TextEdits: []analysis.TextEdit{
{
Pos: field.Pos(),
NewText: []byte(""),
End: field.End(),
},
},
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering if we need to declare an empty list first?

Suggested change
suggestedFixes := []analysis.SuggestedFix{}
suggestedFixes = append(suggestedFixes, analysis.SuggestedFix{
Message: "remove the field",
TextEdits: []analysis.TextEdit{
{
Pos: field.Pos(),
NewText: []byte(""),
End: field.End(),
},
},
})
suggestedFixes := append([]analysis.SuggestedFix{}, analysis.SuggestedFix{
Message: "remove the field",
TextEdits: []analysis.TextEdit{
{
Pos: field.Pos(),
NewText: []byte(""),
End: field.End(),
},
},
})

Or

Suggested change
suggestedFixes := []analysis.SuggestedFix{}
suggestedFixes = append(suggestedFixes, analysis.SuggestedFix{
Message: "remove the field",
TextEdits: []analysis.TextEdit{
{
Pos: field.Pos(),
NewText: []byte(""),
End: field.End(),
},
},
})
suggestedFixes := []analysis.SuggestedFix{
{
Message: "remove the field",
TextEdits: []analysis.TextEdit{
{
Pos: field.Pos(),
NewText: []byte(""),
End: field.End(),
},
},
},
}

Comment on lines 35 to 36
// violationMatcher is a required regular expression
// used to identify violating portions of a field name.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a particular regex library we are using? Should we document which regex version we are compatible with? Go's standard library regix is RE2 compatible IIRC

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are using Go's standard library - I'll update this documentation


type NoTimeStampTestStruct struct {
// +optional
TimeStamp *time.Time `json:"timeStamp,omitempty"` // want "field TimeStamp: prefer use of the term time over timestamp"
Copy link
Contributor

Choose a reason for hiding this comment

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

When we don't have field <> in the name, it makes exceptions hard to craft in the golangci-lint file, I think ideally we would restore this part

*/

package notimestamp_test
package notimestamp
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't normally need to do this, we use the public Initializer() for the test normally

ViolationMatcher: "(?i)timestamp",
Operation: namingconventions.OperationReplace,
Message: "prefer use of the term 'time' over 'timestamp'",
Replace: "Time",
Copy link
Contributor

Choose a reason for hiding this comment

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

Replacement?

It has only just clicked to me that this is the new string, I thought we were replacing this initially.

IIUC, the section that matches the ViolationMatcher gets replaced with this string right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replacement?

It has only just clicked to me that this is the new string, I thought we were replacing this initially.

Fair enough. I used replace because the operation is Replace, but I can change both to "replacement" if that would be more clear.

IIUC, the section that matches the ViolationMatcher gets replaced with this string right?

Correct

limitations under the License.
*/
package nophase_test
package nophase
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be able to use the public Initializer() function rather than changing this

. "github.com/onsi/gomega"
)

func TestConditions(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func TestConditions(t *testing.T) {
func TestNamingConventions(t *testing.T) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦

Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
@JoelSpeed
Copy link
Contributor

/approve
/lgtm

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: everettraven, JoelSpeed

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 Sep 24, 2025
@k8s-ci-robot k8s-ci-robot merged commit 502783c into kubernetes-sigs:main Sep 24, 2025
4 checks passed
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. lgtm "Looks good to me", indicates that a PR is ready to be merged. 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.

4 participants