-
Notifications
You must be signed in to change notification settings - Fork 25
Skip statussubresource linting for Kubernetes List types #177
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
Skip statussubresource linting for Kubernetes List types #177
Conversation
81acf4a to
05d83e6
Compare
everettraven
left a comment
There was a problem hiding this 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
Will need @JoelSpeed for proper approval |
| // metav1.ListMeta `json:"metadata,omitempty"` | ||
| // Items []Foo `json:"items"` | ||
| // } | ||
| func isKubernetesListType(sTyp *ast.StructType, name string) bool { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
05d83e6 to
133ab28
Compare
c86522d to
b2aac8d
Compare
| 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"), |
There was a problem hiding this comment.
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>
b2aac8d to
799c28e
Compare
|
/approve |
|
[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 |
The
statussubresourcelinter 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