-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Address TODO in PR #4652 #4856
Address TODO in PR #4652 #4856
Conversation
@annasong20: This PR has multiple commits, and the default merge method is: merge. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
/label tide/merge-method-squash |
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.
For "too many nested condition" linter error, a proper fix is normally about finding out the root cause and improve the readability (e.g. this doc gives some examples). Moving to another function is more or less a workaround.
When looking at the Load
function, a clearer logic would be
- if path is remote, fetch remote content
- if path is relative path, do ....
- if path is absolute path, do ...
I would change the code as
func (fl *fileLoader) Load(path string) ([]byte, error) {
if isRemote(path) {
return fl.httpClientGetContent(path)
}
if !filepath.IsAbs(path) {
path = fl.root.Join(path)
}
path, err := fl.loadRestrictor(fl.fSys, fl.root, path)
if err != nil {
return nil, err
}
return fl.fSys.ReadFile(path)
}
func (fl *fileLoader) httpClientGetContent(path string) ([]byte, error) {
var hc *http.Client
if fl.http != nil {
hc = fl.http
} else {
hc = &http.Client{}
}
resp, err := hc.Get(path)
defer resp.Body.Close()
// comment explaining what error are not accepted
if resp.StatusCode < 200 || resp.StatusCode > 299 {
_, err := git.NewRepoSpecFromURL(path)
if err == nil {
return nil, errors.Errorf("URL is a git repository")
}
return nil, fmt.Errorf("%w: status code %d (%s)", ErrHTTP, resp.StatusCode, http.StatusText(resp.StatusCode))
}
return io.ReadAll(resp.Body)
}
func isRemote(path string) bool {
u, err := url.Parse(path)
if err != nil {
return false
}
return u.Scheme == "http" || u.Scheme == "https"
}
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'm okay if you want to make the change in a follow-up PR. This is not a blocker of the kustomize localize
. Approved!
e8bd9a3
to
9e69706
Compare
return nil, fmt.Errorf("%w: status code %d (%s)", ErrHTTP, resp.StatusCode, http.StatusText(resp.StatusCode)) | ||
} | ||
content, err := io.ReadAll(resp.Body) | ||
return content, errors.Wrap(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.
linter
complains if I don't wrap 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.
hmmm, what kind of linter error? can you return the ReadAll directly (merge ln335-336)?
return io.ReadAll(resp.Body)
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.
No, I don't believe I can. I get the following linter
error:
api/loader/fileloader.go:335:9: error returned from external package is unwrapped: sig: func io.ReadAll(r io.Reader) ([]byte, error) (wrapcheck)
return io.ReadAll(resp.Body)
^
make: *** [lint] Error 1
I can wrap the error with a helpful message to make the wrap more meaningful?
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.
Oh I see, kustomize go linter enables wrapcheck
.
Addressed comment. Thank you so much for the code example!
|
Looks good to me. |
Deleted an above comment because it was no longer relevant (I think I had an old version of this PR up). /approve Will let @yuwenma give the final LGTM. |
9e69706
to
467b837
Compare
return nil, fmt.Errorf("%w: status code %d (%s)", ErrHTTP, resp.StatusCode, http.StatusText(resp.StatusCode)) | ||
} | ||
content, err := io.ReadAll(resp.Body) | ||
return content, errors.Wrap(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.
hmmm, what kind of linter error? can you return the ReadAll directly (merge ln335-336)?
return io.ReadAll(resp.Body)
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: annasong20, natasha41575, yuwenma 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 |
Approved! @annasong20 |
/lgtm |
* Address TODO in PR kubernetes-sigs#4652 * Improve readability
The
HasRemoteFileScheme()
function was introduced in #4652, but its use infileloader.go
was reverted due to the #4756 code freeze. This PR reinstates its usage.