-
Notifications
You must be signed in to change notification settings - Fork 209
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
tighten validation of inlining and metadata #504
Conversation
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.
Minor comments.
cc @liggitt for changes affect api linting
@@ -0,0 +1,70 @@ | |||
/* | |||
Copyright 2018 The Kubernetes Authors. |
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.
nit: 2024
pkg/generators/api_linter.go
Outdated
@@ -136,6 +136,7 @@ type apiLinter struct { | |||
func newAPILinter() *apiLinter { | |||
return &apiLinter{ | |||
rules: []APIRule{ | |||
&rules.NamingConvention{}, |
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.
Should we be more specific that this is only referring to ListMeta naming convention?
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.
I asked on Slack whether there should be other conventions that this should enforce:
https://kubernetes.slack.com/archives/C0409NGC1TK/p1724256359105049?thread_ts=1723854248.858719&cid=C0409NGC1TK
If there aren't then renaming makes sense.
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.
It's also for ObjectMeta. That wasn't in my original commit, but I've included it in the revised one.
// Validate evaluates API rule on type t and returns a list of field names in | ||
// the type that violate the rule. Empty field name [""] implies the entire | ||
// type violates the rule. | ||
func (n *NamingConvention) Validate(t *types.Type) ([]string, error) { |
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.
can you add a test for this?
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.
Absolutely. It's WIP because I wanted to be sure first that this is going in the right direction.
ae7eba5
to
250daf8
Compare
return false | ||
} | ||
jsonName := strings.Split(jsonTag, ",")[0] | ||
if jsonName != "metadata" { |
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.
"metadata" is excluded from name checking in
kube-openapi/pkg/generators/rules/names_match.go
Lines 39 to 40 in 76de80e
// Special case for object and list meta | |
"metadata", |
The effect is that this mistake is not caught:
Items []ResourceSlice `json:"metadata" protobuf:"bytes,2,rep,name=metadata"`
An alternative to adding this NamingConvention rule would be to extend the NamesMatch rule:
- Check for ListMeta and ObjectMeta there.
- Those fields must be called "metadata".
- Other fields that are called "metadata" must have matching Go and JSON names.
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.
Something along those lines sounds like a good direction to me
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.
Done, PR updated accordingly.
I couldn't resist also addressing this false positive:
SomeInt int `json:""`
This wasn't reported because "" was also in the jsonNameBlacklist
.
250daf8
to
3d12da0
Compare
Not checking empty JSON names and "metadata" has the effect that incorrect usage of those does not get detected. These new unit tests demonstrate that. Fixing the behavior and updating the tests accordingly will follow.
/lgtm |
Using this in Kubernetes might be tricky, updating the dependency wasn't straight-forward - see kubernetes/kubernetes#126917 (not including this PR yet). |
I'd recommend waiting until kubernetes/kubernetes#126787 gets in and handles the regen cleanup |
pkg/generators/rules/names_match.go
Outdated
func nameIsOkay(member types.Member, jsonName string) bool { | ||
if jsonName == "" { | ||
return member.Type.Kind == types.Struct || | ||
member.Type.Kind == types.Pointer && member.Type.Underlying.Kind == types.Struct |
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.
Ooops. This should be member.Type.Elem.
/cancel lgtm
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.
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.
Noticed just in time when looking at the gengo API changes, but missed it earlier when writing the code... sorry!
3d12da0
to
5b13d40
Compare
/lgtm |
Because "" and "metadata" were excluded from checking, some incorrect usage of those were not detected. Also, naming ListMeta or ObjectMeta "listMeta" or "objectMeta" was allowed because the names match, even though by convention the name should have been "metadata".
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: Jefftree, liggitt, pohly 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 |
This catches the following mistake:
The "listMeta" name matches the Go field name, but the convention is to call that JSON field "metadata".
The same applies to
ObjectMeta
.While at it, usage of the empty JSON name also gets checked more thoroughly. This is a mistake which was not detected earlier: