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
Replace gorilla/mux dependency #5483
Replace gorilla/mux dependency #5483
Conversation
vendor/github.com/docker/distribution/registry/api/v2/routes.go
Outdated
Show resolved
Hide resolved
mux/doc.go
Outdated
// license that can be found in the LICENSE file. | ||
|
||
/* | ||
Package mux implements a request router and dispatcher. |
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 we move this into internal/mux/
just so no outside dependencies can happen? Or is it going to be a problem with the runtime exposing it's mux? 🤔 I'm not sure I remember the details right.
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.
While I have not considered adding it to internal, I have had the same question regarding the plugins package. While the router member is private on Manager Manager.r
, the package does expose the WithRouter(r *mux.Router) func(*Manager) {...}
function.
Might also apply to Server with its WithRouter
method as well(?).
Not exactly sure about the usage of server and runtime outside of OPA itself though.
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.
Thanks a lot for looking into this! 👏
✅ Deploy Preview for openpolicyagent ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
Thanks @kristiansvalland for looking into this, and helping us pull in this dependency. 👍 I'll echo @srenatus's concern: Can we move the I'm leaning towards breaking things if necessary, but I'm open to other opinions. @srenatus, what do you think? 🤔 |
Here is what will break as far as I know if we move it into internal:
On the other hand, keeping it outside internal will likely necessitate us maintaining the whole functionality of the gorilla/mux library here, as removing features (like I have already tried doing to some extent in this PR) will limit the users that want to extend the functionality. |
So, technically, the move from On the detailed questions (thanks for finding out about them!):
I think we shouldn't have exported
This looks like it's the other way around -- we're expecting something to be set with the interface we're calling on it. From a brief look, we seem to be using type Mux interface {
Handle(pattern string, hndl http.Handler) Route
}
type Route interface {
Methods(string...) Route
} So we could work with old code that feeds us a gorilla mux. Basically feeding us a gorilla mux would be the only way to use that feature (unless we re-worked it), if we move our copy to internal/mux. Ok enough of that. What are you thoughts? Viable? Worth it? @kristiansvalland @philipaconrad |
Good points, @srenatus. However, regarding #2777, it currently exposes the func (m *Manager) AddHandler(p string, h http.Handler) { ...} or just return our new proposed interface // Return our mux interface allowing the uses to use the `Mux.Handle` method.
func (m *Manager) GetMux () Mux { ... } Thus being explicit about how the plugins can extend OPA. To summarize, what we receive (e.g. |
I tried defining the interfaces that you mentioned, @srenatus, however I now realise that it won't be as simple as I initially thought. Let's say that the user wants to pass a gorilla/mux router := gorillamux.NewRouter()
server := New()
server.WithRouter(router) In this case, the go compiler would complain with the following
(in the above message, I have moved the implementation into ./internal/mux, and defined the interfaces in ./mux for now). The following is the interface type Mux interface {
http.Handler
Handle(path string, h http.Handler) Route
}
type Route interface {
Methods(methods ...string) Route
} and func (s *Server) WithRouter(router muxi.Mux) *Server {
s.router = router
return s
} While the returned values of the Handle method on gorilla's Router also implements the interface we require, go still complains because it is strict on the return type it seems. Is this correct, or is there a way to define the interfaces and methods in go that would allow this? If not, an option would be to break existing code passing a gorilla/mux, but enabling a rewrite through wrapper structs for gorilla/mux.Router. |
Ugh I had forgotten about this restriction. Thanks for trying it out. I wonder if some sort of clean break isn't the better approach here, then. But what about build tags? Perhaps we could split the go files in such a way that previous users can resort to the old behaviour; but anyone not setting the tags would get the new... 🤔 As for the other problem,
You're right, of course, we've given them all of it when we should have only given them what they need to do mux.Handle("/opa", pm.GetRouter()) So there's a mismatch between intent and implementation here, I think. If we restrict stuff to |
3641e76
to
458cf65
Compare
I split the relevant files into *_new and *_old in in the latest commit. To keep the tests in internal/mux unchanged, it simplified things to create a wrapper for the mux.Route struct, as otherwise the interface it implements would need to be extended to allow for the method chaining in the test files. Still this is a possibility if you think its better to not create a wrapper. List of unfinished work:
|
This is great, @kristiansvalland! Thanks for helping out with this 👏 |
In preparation of removing the mux dependency. Signed-off-by: Kristian Svalland <kristian.svalland@gmail.com>
Signed-off-by: Kristian Svalland <kristian.svalland@gmail.com>
Signed-off-by: Kristian Svalland <kristian.svalland@gmail.com>
Signed-off-by: Kristian Svalland <kristian.svalland@gmail.com>
Signed-off-by: Kristian Svalland <kristian.svalland@gmail.com>
server: Remove redundant line. Signed-off-by: Kristian Svalland <kristian.svalland@gmail.com>
Signed-off-by: Kristian Svalland <kristian.svalland@gmail.com>
server/,runtime/,plugins/: Split old (gorilla) and new (internal/mux) implementations into *_new.go and *_old.go files. Signed-off-by: Kristian Svalland <kristian.svalland@gmail.com>
Makefile: Add test command for gorilla mux implementation. plugins: Document functions and add getter for router. server: Remove test duplication. Signed-off-by: Kristian Svalland <kristian.svalland@gmail.com>
928e9c1
to
78417eb
Compare
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.
Thanks again for looking into this. And especially for pushing the build tag approach as far as you've taken it. I am, however, slightly hesitant about this as a way forward 🤔 There's no way to know what it feels like to go this route without trying, but looking at where we've landed, I think we should consider other options.
Since it's been a few days, do you know if any other maintainers have adopted gorilla in the mean time? It keeps re-appearing in my news feeds... 👀
After reading all your comments I wonder if we shouldn't ask @kristiansvalland to take a stab on a PR implementing gin-gonic/gin instead? I think it would be a more fruitful idea. Most benchmarks (not only gin-gonic/gin themselves) indicates that there could be performance benefits from it too. |
@srenatus, apologies for such a late reply, I have been a bit busy with other tasks in the meantime. About gorilla: I have not heard or seen anything, but to be honest, I am not actively monitoring the situation either. Regarding what @johanneslarsson says: Opting for using gin, or another actively maintained framework for that matter, could well be a good solution. We would of course take care not to expose anything that is gin-specific in how the runtime etc. can be configured. I will close this PR for now and look into using gin in a different PR. |
Due to the archiving of gorilla/mux as a result of missing maintainers, it has been considered to swap out the dependency and use our own implementation. For reference, we had a conversation on slack about this topic, and this PR is a proposed solution to what we discussed.
This PR copies the relevant files from gorilla/mux including its license, and tries to keep the resulting implementation as bare bone as possible.
It is likely that there is more code that can be removed from the implementation, as the usage of the
mux.Router
mainly usesRouter.Handle
andRoute.Methods
, but I did not remove the other matchers yet.It is implied that the commits will be squashed before merge, but for now I leave them separate to keep the review process simple and to make the process of copy and edit transparent.
Disclaimer: I am not familiar with the usage of licenses and redistribution of licensed works, so I would appreciate that someone can vouch for the methodology used here.