Skip to content
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

Merged
merged 2 commits into from Nov 8, 2022

Conversation

annasong20
Copy link
Contributor

The HasRemoteFileScheme() function was introduced in #4652, but its use in fileloader.go was reverted due to the #4756 code freeze. This PR reinstates its usage.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 4, 2022
api/loader/fileloader.go Outdated Show resolved Hide resolved
@annasong20
Copy link
Contributor Author

@yuwenma, @natasha41575

@k8s-ci-robot
Copy link
Contributor

@annasong20: This PR has multiple commits, and the default merge method is: merge.
You can request commits to be squashed using the label: tide/merge-method-squash

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.

@annasong20
Copy link
Contributor Author

/label tide/merge-method-squash

@k8s-ci-robot k8s-ci-robot added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Nov 4, 2022
Copy link
Contributor

@yuwenma yuwenma left a 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"
}

Copy link
Contributor

@yuwenma yuwenma left a 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!

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)
Copy link
Contributor Author

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

Copy link
Contributor

@yuwenma yuwenma Nov 8, 2022

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)

Copy link
Contributor Author

@annasong20 annasong20 Nov 8, 2022

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?

Copy link
Contributor

@yuwenma yuwenma Nov 8, 2022

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.

@annasong20
Copy link
Contributor Author

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!

Addressed comment. Thank you so much for the code example!

linter currently failing due to previous change to master; #4854 should fix this, so I won't do anything here

@yuwenma
Copy link
Contributor

yuwenma commented Nov 7, 2022

Looks good to me.
Can you run go mod tidy to fix the CI error?

@natasha41575
Copy link
Contributor

natasha41575 commented Nov 7, 2022

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.

@k8s-ci-robot k8s-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Nov 7, 2022
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 7, 2022
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)
Copy link
Contributor

@yuwenma yuwenma Nov 8, 2022

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)

@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@yuwenma
Copy link
Contributor

yuwenma commented Nov 8, 2022

Approved! @annasong20
one nit comment. please resolve before merging.

@yuwenma
Copy link
Contributor

yuwenma commented Nov 8, 2022

/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 8, 2022
@k8s-ci-robot k8s-ci-robot merged commit 6d9b540 into kubernetes-sigs:master Nov 8, 2022
@annasong20 annasong20 deleted the address-4652-todo branch November 8, 2022 22:41
elisshafer pushed a commit to elisshafer/kustomize that referenced this pull request Dec 8, 2022
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/M Denotes a PR that changes 30-99 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants