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

Move HTTP/handler utils to a separate package #1311

Merged
merged 3 commits into from
Nov 26, 2019

Conversation

SimonAlling
Copy link
Contributor

Description of the change

This PR moves a few types and (mostly pure) functions to a separate package.

Benefits

The code in question becomes more reusable, which will help with implementing Helm 3 support down the road (see #1309).

Possible drawbacks

Currently, this will expose some entities that weren't exposed before (Params, WithParams, WithoutParams, ErrorCode and ErrorCodeWithDefault). If this is undesired, I'd be happy to create the new package at e.g. internal/handlerutil or cmd/internal/handlerutil instead of pkg/handlerutil. (AFAIK, pkg/internal/handlerutil won't work because the moved entities need to be imported in cmd.)

Moving forward, they will be needed to implement Helm 3 support (see
vmware-tanzu#1309), and they were in an internal package, so they couldn't be
imported outside the `cmd/tiller-proxy` directory. Because they were all
pure (or, in the case of ServeHTTP, at least very simple), I felt
confident moving them to another file and reusing them.
Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I just have a minor comment, I am okay exposing those methods if they are going to be reused by a different package.

@@ -49,7 +50,7 @@ func TestErrorCodeWithDefault(t *testing.T) {
{fmt.Errorf("This is an unexpected error"), http.StatusUnprocessableEntity, http.StatusUnprocessableEntity},
}
for _, s := range tests {
code := errorCodeWithDefault(s.err, s.defaultCode)
code := handlerutil.ErrorCodeWithDefault(s.err, s.defaultCode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this test is just testing ErrorCodeWithDefault, it's better to move this test to kg/handlerutil/handlerutil_test.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree.

Copy link
Contributor

@absoludity absoludity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only question is whether we need to replicate the WithParams etc. in new code. I mentioned in this comment that I don't see why we have them in tiller proxy (that is, over using standard handler func signature (w http.ResponseWriter, r *httpRequest) and a simple function to extract args), but i don't care either way. So if you think it's useful to keep using them, great.

@SimonAlling
Copy link
Contributor Author

My only question is whether we need to replicate the WithParams etc. in new code. I mentioned in this comment that I don't see why we have them in tiller proxy (that is, over using standard handler func signature (w http.ResponseWriter, r *httpRequest) and a simple function to extract args), but i don't care either way. So if you think it's useful to keep using them, great.

You might be right, @absoludity. I'll see what I can do as soon as I have a usable internet connection again. Can't make otherwise ...

@andresmgot
Copy link
Contributor

Merging this as it is, if you want to add something else, you can add it in a new PR :)

@andresmgot andresmgot merged commit db3fc03 into vmware-tanzu:master Nov 26, 2019
@SimonAlling
Copy link
Contributor Author

SimonAlling commented Nov 26, 2019

OK, @andresmgot! I tried removing the WithParams abstraction yesterday, as suggested by @absoludity. No problem according to the typechecker, but I couldn't get a single test to pass without it.

EDIT: Found the reason in the tests.

@SimonAlling
Copy link
Contributor Author

By the way, @absoludity, I think I can confirm (after a lengthy and painful debugging session) that the WithParams abstraction is necessary to be able to extract the data we want from the http.Request. It just doesn't contain any useful data to extract anymore as soon as we're inside the actual handler (e.g. CreateRelease).

@absoludity
Copy link
Contributor

It just doesn't contain any useful data to extract anymore as soon as we're inside the actual handler (e.g. CreateRelease).

Thanks for digging there. I'm very interested to know why the request would not contain the same data when passed to CreateRelease but can look at that separately.

@SimonAlling SimonAlling deleted the move-http-functions branch December 3, 2019 09:45
SimonAlling added a commit to SimonAlling/kubeapps that referenced this pull request Dec 3, 2019
@SimonAlling
Copy link
Contributor Author

Thanks for digging there. I'm very interested to know why the request would not contain the same data when passed to CreateRelease but can look at that separately.

I may have been wrong in stating that it "doesn't contain any useful data to extract anymore" above. It seems at least as likely that it never did to begin with. You can see what I tried on the remove-withparams branch in my fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants