Skip to content

Add notimestamp linter #105

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 2 commits into
base: main
Choose a base branch
from

Conversation

Karthik-K-N
Copy link

Adds a new notimestamp linter that checks for any fields in struct having timestamp string or substring.

Fixes: #26

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 25, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Karthik-K-N
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 added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 25, 2025
Copy link
Contributor

@everettraven everettraven left a comment

Choose a reason for hiding this comment

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

Unlike the nophase linter, this linter has a suggested replacement for the substring timestamp (time).

At the very least, we should ensure that we include that replacement suggestion in the report message.

// First check if the struct field name contains 'timestamp'
if strings.Contains(strings.ToLower(fieldName), "timestamp") {
pass.Reportf(field.Pos(),
"field %s: fields with timestamp substring should be avoided",
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 probably include in the message what alternative should be used in place of this. I believe the appropriate replacement here would be time.

Seeing as how this linter has a proposed replacement, should we also implement the suggested fix for this so that if someone were to run lint --fix that it would automatically apply the suggestion?

Copy link
Author

Choose a reason for hiding this comment

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

hey, A quick question, whats the right way of debugging or to get to know the value of some variable, If I try

fmt.Println(fieldName)

I dont see anything getting printed on the console

Copy link
Contributor

Choose a reason for hiding this comment

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

How are you trying to debug it? Running the actual tool or via the tests?

If it is through the tests, you'd want to tell it to be verbose using the -v flag with go test.

Copy link
Author

Choose a reason for hiding this comment

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

oh ok, I will give a try, Thanks, I was trying with tool

./bin/golangci-kube-api-linter run kube-api-linter/pkg/analysis/notimestamp/testdata/src/a/a.go --fix

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 26, 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 Jun 30, 2025
@Karthik-K-N
Copy link
Author

Updated the PR to address review comments, Couple of pending questions

  1. Currently I need to run
./bin/golangci-kube-api-linter run /kube-api-linter/pkg/analysis/notimestamp/testdata/src/a/a.go --fix

twice to fix the names, Once for fixing field names and second time is for fixing json tags. Not sure thats ideal.

  1. GIven that we are updating the json tags and field comments are starting with json tags, Should we fix that comment also to use the latest tag or can we depend on the commentStart linter to fix it, by running it again.

docs/linters.md Outdated
Comment on lines 156 to 157

The `notimestamp` linter checks that the fields in the API types don't contain a 'Timestamp', or any field which contains 'Timestamp' as a substring, e.g CreateTimestamp.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe?

Suggested change
The `notimestamp` linter checks that the fields in the API types don't contain a 'Timestamp', or any field which contains 'Timestamp' as a substring, e.g CreateTimestamp.
The `notimestamp` linter checks that the fields in the API are not named with the word 'Timestamp'.

What is the reason for this from the API conventions, might be good to help explain that context here a little?

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I would generally try to use git blame to work out the PR that introduced the rule, and see if there is additional context there. If not, perhaps #api-reviews on the Kuberentes slack might be able to help

Copy link
Author

Choose a reason for hiding this comment

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

sure thanks, I will check

return
}

var suggestedFixes []analysis.SuggestedFix
Copy link
Contributor

Choose a reason for hiding this comment

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

Fixes should be explained in the linters documentation, see how others have done that


The `notimestamp` linter checks that the fields in the API are not named with the word 'Timestamp'.

The name of a field that specifies the time at which something occurs should be called `somethingTime`.Its recommended not use stamp (e.g., creationTimestamp).
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
The name of a field that specifies the time at which something occurs should be called `somethingTime`.Its recommended not use stamp (e.g., creationTimestamp).
The name of a field that specifies the time at which something occurs should be called `somethingTime`. It is recommended not use stamp (e.g., creationTimestamp).

Comment on lines +24 to +46
// 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(cfg config.LintersConfig) (*analysis.Analyzer, error) {
return Analyzer, 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.

A recent refactor has changed the way functions init, can you please rebase and look at another, non-configurable linter to see how this should look now.

commentstart is probably a good example to crib from

Copy link
Author

Choose a reason for hiding this comment

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

sure will do that

@@ -86,6 +87,7 @@ func NewRegistry() Registry {
nofloats.Initializer(),
nomaps.Initializer(),
nophase.Initializer(),
notimestamp.Initializer(),
Copy link
Contributor

Choose a reason for hiding this comment

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

This will conflict when you rebase, we don't maintain this list anymore as each linter now self registers by blank importing in a package called registration

fieldReplacementName := timeStampRegEx.ReplaceAllString(fieldName, "Time")
if fieldReplacementName != fieldName {
suggestedFixes = append(suggestedFixes, analysis.SuggestedFix{
Message: "replace field with Time",
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 these normally have to be unique, have you tried this against a file that has multiple failures using a full golangci-lint run?

@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.

@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
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. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New Linter: timestamps
4 participants