-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Slimmer /api package #5213
Slimmer /api package #5213
Conversation
nomad/structs is an internal package and imports many libraries (e.g. raft, codec) that are not relevant to api clients, and may cause unnecessary dependency pain (e.g. `github.com/ugorji/go/codec` version is very old now). Here, we add a code generator that imports the relevant constants from `nomad/structs`. I considered using this approach for other structs, but didn't find a quick viable way to reduce duplication. `nomad/structs` use values as struct fields (e.g. `string`), while `api` uses value pointer (e.g. `*string`) instead. Also, sometimes, `api` structs contain deprecated fields or additional documentation, so simple copy-paste doesn't work. For these reasons, I opt to keep the status quo.
`api.MockJob` is a test utility, that's only used by `command/agent` package. This moves it to the package and removes it from the public API.
Embed pointer conversion functions in the API package to avoid unnecessary package dependency. `helper` package imports more dependencies relevant for internal use (e.g. `hcl`).
`helpers.FormatFloat` function is only used in `api`. Moving it and marking it as private. We can re-export it if we find value later.
@@ -224,7 +221,7 @@ func (n *Nodes) monitorDrainNode(ctx context.Context, nodeID string, | |||
return | |||
} | |||
|
|||
if node.Status == structs.NodeStatusDown { | |||
if node.Status == NodeStatusDown { |
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.
Ah! I see what you meant the other day when you said linters could help. This should have never happened, but thanks to the magic of goimports and human muscle memory it happens very easily!
Thanks for catching it!
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, interesting! How would a linter have helped here?
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.
In the past api/
should never have imported from nomad/structs
, so something like go list -f {{ .Deps }} | grep nomad/structs
finding a match could work as a linter.
I think the idea now is that api/
shouldn't import anything from anywhere else in Nomad at all, so a similar linter that inspects api/
's deps on commit would be helpful.
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.
Nomad few packages (e.g. plugins
, api
) that represent public "stable" API, and we have some opinions about the graph of package dependencies and layout. I proposed in a meeting formalizing it and adding a linter to enforce them.
935984e
to
ce52547
Compare
Given that the values will rarely change, specially considering that any changes would be backward incompatible change. As such, it's simpler to keep syncing manually in the rare occasion and avoid the syncing code overhead.
ce52547
to
5e18538
Compare
@@ -6,7 +6,6 @@ import ( | |||
"testing" | |||
"time" | |||
|
|||
"github.com/hashicorp/nomad/helper" | |||
"github.com/hashicorp/nomad/nomad/structs" |
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.
Having tests depend on the structs package and use the same constants (e.g DefaultBatchReschedulePolicy
) but having the api package copy the values over so that they have to be manually synced seems wrong to me. What were we trying to optimize for here?
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 opted to have the tests check that api package have the same values as structs version, so tests fail if they are out of sync.
Given that vendoring tools (e.g. govendor) prune out test files, by default, the dependency on structs
package wouldn't leak into clients of this package.
Does this make sense?
I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions. |
Reduces
api
dependency footprint to ease client depedency management, specially in preparation to using go mod.Following some internal conversation about nomad API, I noticed that the
/api
package surprisingly depends on many packages such as hashicorp's raft, lru, immutable-radix packages, andgithub.aaakk.us.kg/ugorji/go/codec
library.github.com/ugorji/go/codec
dependency causes pain to developers as it henders them from using a recent version, given that revision dates back to 2017-06-20.With these changes,
api
package removes 30 dependencies, keeping dependency on 5 only. The difference in non-std dependencies from v0.8.7:fyi @blalor @jefferai