-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
Vsphere: Add 15 missing err checks #73358
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1288,7 +1288,9 @@ func (vs *VSphere) NodeAdded(obj interface{}) { | |
} | ||
|
||
klog.V(4).Infof("Node added: %+v", node) | ||
vs.nodeManager.RegisterNode(node) | ||
if err := vs.nodeManager.RegisterNode(node); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initially that was true, but |
||
klog.Errorf("failed to add node %+v: %v", node, err) | ||
} | ||
} | ||
|
||
// Notification handler when node is removed from k8s cluster. | ||
|
@@ -1300,7 +1302,9 @@ func (vs *VSphere) NodeDeleted(obj interface{}) { | |
} | ||
|
||
klog.V(4).Infof("Node deleted: %+v", node) | ||
vs.nodeManager.UnRegisterNode(node) | ||
if err := vs.nodeManager.UnRegisterNode(node); err != nil { | ||
klog.Errorf("failed to delete node %s: %v", node.Name, err) | ||
} | ||
} | ||
|
||
func (vs *VSphere) NodeManager() (nodeManager *NodeManager) { | ||
|
@@ -1316,7 +1320,11 @@ func withTagsClient(ctx context.Context, connection *vclib.VSphereConnection, f | |
if err := c.Login(ctx, user); err != nil { | ||
return err | ||
} | ||
defer c.Logout(ctx) | ||
defer func() { | ||
if err := c.Logout(ctx); err != nil { | ||
klog.Errorf("failed to logout: %v", err) | ||
} | ||
}() | ||
return f(c) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,7 +72,12 @@ func getVSphereConfig() (*VSphereConfig, error) { | |
if err != nil { | ||
return nil, err | ||
} | ||
defer confFile.Close() | ||
defer func() { | ||
if err := confFile.Close(); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can just ignore this error |
||
klog.Errorf("failed to close config file: %v", err) | ||
} | ||
}() | ||
|
||
cfg, err := readConfig(confFile) | ||
if err != nil { | ||
return nil, err | ||
|
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.
does a linter complain about these?
it shouldn't.
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 it does, because the error return gets implicitly ignored. With this change, it gets explicitly ignored.
Why do you think it shouldn't complain about these?
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 is the exact linter message?
just tested with
golint
locally.something in the lines of this does not cause an issue:
it depends. the programmer has the freedom to execute the logic without caring about the outcome of function - i.e. in a case where the function also performs extra work unrelated to the returned result. linters in C do complain about these, then again they also complain about unused return values from
printf
.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.
The linter I used to find these is errcheck: https://github.com/kisielk/errcheck
It basically complains every time a returned
error
is not explicitly handled, so for your example this is the result:Yes but the returned error may also indicate that during that extra work unrelated to the other returns an error was encountered.
I personally find it a sensible requirement to explicitly ignore all errors, its the only safe way to avoid doing so by accident.
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.
since the PR is against
k/k
, to me it feels like unless./hack/verify-golint.sh
(uses golint) complains about the_, _ =
case i don't think we should fix these based on a third party linter.but the decision is obviously up to the maintainers. 👍
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.
golint doesn't care at all about unhandled errors, this is why the things I fixed here (which are mostly not
_ =
assignments) got merged in the first place.I asked about this in #sig-testing and it was agreed that adding linting for this would be a good thing: https://kubernetes.slack.com/archives/C09QZ4DQB/p1548535905927500