Skip to content

Add notimestamp linter - #105

Merged
k8s-ci-robot merged 1 commit into
kubernetes-sigs:mainfrom
Karthik-K-N:add-notimestamp
Jul 23, 2025
Merged

Add notimestamp linter#105
k8s-ci-robot merged 1 commit into
kubernetes-sigs:mainfrom
Karthik-K-N:add-notimestamp

Conversation

@Karthik-K-N

Copy link
Copy Markdown
Contributor

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 k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 25, 2025

@everettraven everettraven left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread pkg/analysis/notimestamp/analyzer.go Outdated
// 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
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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

Comment thread pkg/analysis/notimestamp/analyzer.go Outdated
@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
Copy Markdown
Contributor 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.

Comment thread 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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
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
Copy Markdown
Contributor 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

Comment thread pkg/analysis/notimestamp/analyzer.go
Comment thread docs/linters.md Outdated
Comment thread pkg/analysis/notimestamp/initializer.go Outdated
Comment on lines +24 to +36
// 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
Copy Markdown
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
Copy Markdown
Contributor 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

Comment thread pkg/analysis/registry.go Outdated
nofloats.Initializer(),
nomaps.Initializer(),
nophase.Initializer(),
notimestamp.Initializer(),

Copy link
Copy Markdown
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

Comment thread pkg/analysis/notimestamp/analyzer.go Outdated
@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
@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 21, 2025
@Karthik-K-N

Copy link
Copy Markdown
Contributor Author

Rebased and updated the PR, Ready for review. Thanks.

Comment thread docs/linters.md Outdated
Comment thread docs/linters.md Outdated
Comment thread pkg/analysis/notimestamp/testdata/src/a/a.go.golden Outdated
Comment thread pkg/analysis/notimestamp/testdata/src/a/a.go
Comment thread go.mod Outdated
Comment thread pkg/analysis/notimestamp/analyzer.go Outdated
Comment thread pkg/analysis/notimestamp/analyzer.go Outdated
@JoelSpeed

Copy link
Copy Markdown
Contributor

I think this should be good to go once this last round is resolved, should we squash the commits also?

@Karthik-K-N

Copy link
Copy Markdown
Contributor Author

Addressed review comments, Added few more fields for testing, Squashed commits. Please take a look. Thanks.

Comment thread pkg/analysis/notimestamp/analyzer.go Outdated

if tagReplacementName != tagInfo.Name {
suggestedFixes = append(suggestedFixes, analysis.SuggestedFix{
Message: "replace json tag with Time",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we include the before and after strings here as we did with the field name please

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oops, Missed it, Updated it now.


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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does the linter look at the type spec at all? Would a metv1.Time still work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Currently the linter does not look at the type spec, on looks for fields with timestamp, wrt to checking type spec. I think we will be covering that in nodurations linter.

@JoelSpeed

Copy link
Copy Markdown
Contributor

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 23, 2025
@k8s-ci-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: JoelSpeed, Karthik-K-N

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details 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 Jul 23, 2025
@k8s-ci-robot
k8s-ci-robot merged commit 1b29e82 into kubernetes-sigs:main Jul 23, 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/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