Skip to content

Conversation

@saschagrunert
Copy link
Member

The statussubresource linter was incorrectly flagging Kubernetes List types that don't have a status field. List types follow a different pattern (TypeMeta, ListMeta, Items) and don't use the status subresource.

Fixes #53

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Nov 3, 2025
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 3, 2025
@saschagrunert saschagrunert force-pushed the fix-issue-53-marker-spacing branch 9 times, most recently from 81acf4a to 05d83e6 Compare November 3, 2025 12:02
@saschagrunert saschagrunert marked this pull request as ready for review November 3, 2025 12:03
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 3, 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.

Aside from one, non-blocking, question about the nolint:... usage, this looks good to me.

/lgtm

}

func checkStruct(pass *analysis.Pass, sTyp *ast.StructType, name string, structMarkers markershelper.MarkerSet, jsonTags extractjsontags.StructFieldTags) {
func checkStruct(pass *analysis.Pass, sTyp *ast.StructType, name string, structMarkers markershelper.MarkerSet, jsonTags extractjsontags.StructFieldTags) { //nolint:cyclop
Copy link
Contributor

Choose a reason for hiding this comment

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

What was the cyclomatic complexity here that required setting the nolint?

Copy link
Member Author

Choose a reason for hiding this comment

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

Did some slight refactoring and kept the cyclo <=10.

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

Will need @JoelSpeed for proper approval

// metav1.ListMeta `json:"metadata,omitempty"`
// Items []Foo `json:"items"`
// }
func isKubernetesListType(sTyp *ast.StructType, name string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

We have this kind of check in at least one other place don't we? The helper inspector? Should we centralise this knowledge into a util so as not to duplicate it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we can refactor the logic into a utility function IsKubernetesListType in pkg/analysis/utils/utils.go.

@saschagrunert saschagrunert force-pushed the fix-issue-53-marker-spacing branch from 05d83e6 to 133ab28 Compare November 6, 2025 12:50
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 6, 2025
@saschagrunert saschagrunert force-pushed the fix-issue-53-marker-spacing branch 2 times, most recently from c86522d to b2aac8d Compare November 6, 2025 13:00
Comment on lines -103 to +136
switch {
case (hasStatusSubresourceMarker && hasStatusField), (!hasStatusSubresourceMarker && !hasStatusField):
// acceptable state
case hasStatusSubresourceMarker && !hasStatusField:
// Might be able to have some suggested fixes here, but it is likely much more complex
// so for now leave it with a descriptive failure message.
// Both present or both absent is acceptable
if hasStatusSubresourceMarker == hasStatusField {
return
}

// Marker present but no status field
if hasStatusSubresourceMarker {
pass.Reportf(sTyp.Pos(), "root object type %q is marked to enable the status subresource with marker %q but has no status field", name, markers.KubebuilderStatusSubresourceMarker)
case !hasStatusSubresourceMarker && hasStatusField:
// In this case we can suggest the autofix to add the status subresource marker
pass.Report(analysis.Diagnostic{
Pos: sTyp.Pos(),
Message: fmt.Sprintf("root object type %q has a status field but does not have the marker %q to enable the status subresource", name, markers.KubebuilderStatusSubresourceMarker),
SuggestedFixes: []analysis.SuggestedFix{
{
Message: "should add the kubebuilder:subresource:status marker",
TextEdits: []analysis.TextEdit{
// go one line above the struct and add the marker
{
// sTyp.Pos() is the beginning of the 'struct' keyword. Subtract
// the length of the struct name + 7 (2 for spaces surrounding type name, 4 for the 'type' keyword,
// and 1 for the newline) to position at the end of the line above the struct
// definition.
Pos: sTyp.Pos() - token.Pos(len(name)+7),
// prefix with a newline to ensure we aren't appending to a previous comment
NewText: []byte("\n// +kubebuilder:subresource:status"),
},
return
}

// Status field present but no marker - suggest autofix
pass.Report(analysis.Diagnostic{
Pos: sTyp.Pos(),
Message: fmt.Sprintf("root object type %q has a status field but does not have the marker %q to enable the status subresource", name, markers.KubebuilderStatusSubresourceMarker),
SuggestedFixes: []analysis.SuggestedFix{
{
Message: "should add the kubebuilder:subresource:status marker",
TextEdits: []analysis.TextEdit{
{
// sTyp.Pos() is the beginning of the 'struct' keyword. Subtract
// the length of the struct name + 7 (2 for spaces surrounding type name, 4 for the 'type' keyword,
// and 1 for the newline) to position at the end of the line above the struct
// definition.
Pos: sTyp.Pos() - token.Pos(len(name)+7),
// prefix with a newline to ensure we aren't appending to a previous comment
NewText: []byte("\n// +kubebuilder:subresource:status"),
Copy link
Member Author

Choose a reason for hiding this comment

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

Refactoring this keeps the cyclo <=10.

The statussubresource linter was incorrectly flagging Kubernetes List
types that don't have a status field. List types follow a different
pattern (TypeMeta, ListMeta, Items) and don't use the status subresource.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
@saschagrunert saschagrunert force-pushed the fix-issue-53-marker-spacing branch from b2aac8d to 799c28e Compare November 6, 2025 13:12
@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 Nov 6, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

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

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 Nov 6, 2025
@k8s-ci-robot k8s-ci-robot merged commit a9a8d5c into kubernetes-sigs:main Nov 6, 2025
4 checks passed
@saschagrunert saschagrunert deleted the fix-issue-53-marker-spacing branch November 6, 2025 13:45
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.

markers helper can't detect markers which are separated with space.

4 participants