WIP: Refactor code out of main package #286
Conversation
Move store.go and host.go out of main package and into their own package and an api package. Moves most calls that change the state of a host (ie start, stop) to the api so that there is a single point of entry for them. Signed-off-by: Craig Jellick <craig@rancher.com>
I agree that a some of the main package needs to be moved. The "api" package seems a bit generic for a name :) |
@sthulb I agree with the name. I struggled with the name and decided to use api as a strawman. If you have anything else in mind, I'd welcome the suggestion. Meanwhile, I'll think on it some more. |
@cjellick I think naming things are hard at this stage in a project. |
The |
@cjellick sorry for the delayed response. this is awesome. +1 to @sthulb @cjellick Could you also rebase? Thanks! |
I guess the obvious one is to rename |
+1 for |
This one got away from me. Sorry for leaving it open for so long. |
looks like you did -- thanks! |
This PR does the following:
store.go
andhost.go
code out of the main package and into a package calledstore
api
package to provide a single entry point for all operations on hostsMotivation and benefits
First, hopefully this will better organize business logic and separate concerns in the machine code base. It should give future developers better guidance on where business logic should go and force them to think through how it can be cleanly exposed. This should make machine more maintainable in the long run.
It should allow
commands.go
to be concerned only with interpreting CLI requests and passing them to the API. With the code in it's current state, it isn't clear where new functionality should drop. Here's a few examples where it seems the current model is falling short:host.Start()
, to restart it you callhost.DriverRestart()
, and to remove it you callstore.Remove()
, but there is also ahost.Remove()
function exposed.Second, this provides access to machine as a library so that integrators don't have to exec out to it. This is a good stop-gap measure while we work towards building a REST API for machine.
As @shykes and @ehazlett mentioned in an IRC conversation, a risk is that introducing a public api at this point means being forced to maintain it. It should be understood that the
api
provides no stability or backwards compatibility guarantees with regards to public consumption. It is first and foremost an internal API for better separating concerns within the machine code base. Users calling machine as a library would do so at their own risk. Would calling this out explicitly as a comment in theapi
package be sufficient for conveying this?Current deficencies or concerns
There a few deficencies with the PR as-is:
The SSH command code has note been refactored. This was just done for time savings. I wanted to get some feedback on the concept before spending the time to refactor it.
The abstraction is a little leaky. All state changing functions on the
host
struct have been unexposed, but theDriver
field is still exposed and it exposes state changing operations. The reason for this is that themachine inspect
command marshals the Driver field to json. I couldn't simply hide the Driver field because it would cause inspect to be much less useful. Suggests on how to address this are welcome.There is some logic in
store.go
andhost.go
that perhaps now should be moved directly into theapi
package. This was done intentionally to minimize the impact of the PR. Future PRs could be address further refactoring made possible by this PR.Some more testing may be needed