Add support for templating Node.Hostname in docker executor#2266
Add support for templating Node.Hostname in docker executor#2266aaronlehmann merged 1 commit intomoby:masterfrom
Conversation
3bab7af to
12dfa91
Compare
Codecov Report
@@ Coverage Diff @@
## master #2266 +/- ##
==========================================
+ Coverage 60.35% 60.36% +0.01%
==========================================
Files 125 125
Lines 20394 20412 +18
==========================================
+ Hits 12308 12322 +14
Misses 6701 6701
- Partials 1385 1389 +4 |
| } | ||
|
|
||
| // NodeInspectWithRaw is part of the APIClient interface | ||
| func (sa *StubAPIClient) NodeInspectWithRaw(ctx context.Context, nodeID string) (swarm.Node, []byte, error) { |
There was a problem hiding this comment.
What is this for? I don't see anything that uses it.
There was a problem hiding this comment.
It was a leftover from my attempts at using NodeInspect with the Docker client.. I'll remove this change.
agent/exec/dockerapi/executor.go
Outdated
| ctlr, err := newController(e.client, t, secrets.Restrict(e.secrets, t)) | ||
| ctx := context.Background() | ||
| // Get node information | ||
| nodeDescription, err := e.Describe(ctx) |
There was a problem hiding this comment.
If I remember correctly, Describe is a relatively expensive operation. I'm not sure we should call Describe for every new controller. It might be better to store the latest nodeDescription returned by Describe in the executor struct, and use that (with any necessary locking).
| Node struct { | ||
| ID string | ||
| ID string | ||
| Hostname string |
There was a problem hiding this comment.
While we're at it, would it make sense to add other properties from NodeDescription, like Architecture and OS?
template/context.go
Outdated
| // task. The provided context can then be used to populate runtime values in a | ||
| // ContainerSpec. | ||
| func NewContextFromTask(t *api.Task) (ctx Context) { | ||
| func NewContextFromTask(t *api.Task, n *api.NodeDescription) (ctx Context) { |
There was a problem hiding this comment.
If we make this dependent on NodeDescription, we should rename the function to NewContext.
Another option would be to keep NewContextFromTask as-is, and add a WithNodeDescription method that fills in the node-dependent values. This would keep the node description optional and avoid needing to pass it through code paths that don't need it (for example, tests exercising other parts of the templating functionality). However, I'm not sure there's much value in NodeDescription being optional if we expect all executors to provide it.
@stevvooe WDYT?
template/getter_test.go
Outdated
| dependencyManager.Configs().Add(*referencedConfig) | ||
|
|
||
| templatedDependencies := NewTemplatedDependencyGetter(agent.Restrict(dependencyManager, testCase.task), testCase.task) | ||
| templatedDependencies := NewTemplatedDependencyGetter(agent.Restrict(dependencyManager, testCase.task), testCase.task, &api.NodeDescription{}) |
There was a problem hiding this comment.
Can you please add test cases that expand Hostname in a template?
|
Please sign your commits following these rules: $ git clone -b "feature/template-node-hostname" git@github.com:mion00/swarmkit.git somewhere
$ cd somewhere
$ git rebase -i HEAD~842353841144
editor opens
change each 'pick' to 'edit'
save the file and quit
$ git commit --amend -s --no-edit
$ git rebase --continue # and repeat the amend for each commit
$ git push -fAmending updates the existing PR. You DO NOT need to open a new one. |
0684538 to
da63b8b
Compare
da63b8b to
064c801
Compare
manager/controlapi/service.go
Outdated
| // - Returns an error if the creation fails. | ||
| func (s *Server) CreateService(ctx context.Context, request *api.CreateServiceRequest) (*api.CreateServiceResponse, error) { | ||
| // TODO(gianarb) not sure about how to get here the nodeID or something | ||
| // similar. but the service has already a host at this point? |
There was a problem hiding this comment.
The service is not associated with any particular nodes when it is first created.
| ) | ||
|
|
||
| // Platform holds information about the underlying platform of the node | ||
| type Platform struct { |
There was a problem hiding this comment.
You can use api.Platform instead of redefining the struct here.
There was a problem hiding this comment.
Forget I said that... realized this is going into a struct that will be accessable by templates, so no methods should be defined on the type.
agent/exec/dockerapi/executor.go
Outdated
| genericResources: genericResources, | ||
| } | ||
| // Get the node information from the newly created executor | ||
| var ctx = context.Background() |
There was a problem hiding this comment.
Using context.Background here is problematic. It meanst NewExecutor can potentially block forever.
I would make Describe set executor.node. Describe is called when the agent first starts up, before anything else happens. It is also called periodically to refresh the information.
We should protect executor.node with a mutex so it isn't accessed during an update.
There was a problem hiding this comment.
I added a mutex to the executor field. Sorry but it is my first time using Golang. I hope the code makes sense.
template/context.go
Outdated
| ID string | ||
| ID string | ||
| Hostname string | ||
| Platform |
template/context.go
Outdated
| // The provided context can then be used to populate runtime values in a | ||
| // ContainerSpec. | ||
| func NewContextFromTask(t *api.Task) (ctx Context) { | ||
| func NewContext(t *api.Task, n *api.NodeDescription) (ctx Context) { |
There was a problem hiding this comment.
Argument should be in order of context, so node, then task.
aaronlehmann
left a comment
There was a problem hiding this comment.
Thanks for making the changes. I think this is getting close.
agent/exec/dockerapi/executor.go
Outdated
| client engineapi.APIClient | ||
| secrets exec.SecretsManager | ||
| genericResources []*api.GenericResource | ||
| mutex *sync.Mutex // This mutex protects the following node field |
There was a problem hiding this comment.
The field doesn't have to be a pointer. This can be something like:
mu sync.Mutexand then it doesn't need to be initialized.
| ctx.Node.Platform = Platform{ | ||
| Architecture: n.Platform.Architecture, | ||
| OS: n.Platform.OS, | ||
| } |
There was a problem hiding this comment.
Let's make this tolerate a nil n, just in case Describe failed before storing the node description. In this case, we can just skip filling in the ctx.Node fields.
|
LGTM Please squash the commits before merging |
…er executor. Add unit testing. Rename NewContextFromTask in NewContext. Populate node description in executor in Describe() method, protected using mutex. Change order of parameters using node first and task second. Check if node is null in template context before populating fields. Signed-off-by: Carlo Mion <cmion@fbk.eu>
06c398f to
69602b4
Compare
|
LGTM |
- moby/swarmkit#2266 (support for templating Node.Hostname in docker executor) - moby/swarmkit#2281 (change restore action on objects to be update, not delete/create) - moby/swarmkit#2285 (extend watch queue with timeout and size limit) - moby/swarmkit#2253 (version-aware failure tracking in the scheduler) - moby/swarmkit#2275 (update containerd and port executor to container client library) - moby/swarmkit#2292 (rename some generic resources) - moby/swarmkit#2300 (limit the size of the external CA response) - moby/swarmkit#2301 (delete global tasks when the node running them is deleted) Minor cleanups, dependency bumps, and vendoring: - moby/swarmkit#2271 - moby/swarmkit#2279 - moby/swarmkit#2283 - moby/swarmkit#2282 - moby/swarmkit#2274 - moby/swarmkit#2296 (dependency bump of etcd, go-winio) Signed-off-by: Ying Li <ying.li@docker.com> Upstream-commit: 4509a00 Component: engine
- moby/swarmkit#2266 (support for templating Node.Hostname in docker executor) - moby/swarmkit#2281 (change restore action on objects to be update, not delete/create) - moby/swarmkit#2285 (extend watch queue with timeout and size limit) - moby/swarmkit#2253 (version-aware failure tracking in the scheduler) - moby/swarmkit#2275 (update containerd and port executor to container client library) - moby/swarmkit#2292 (rename some generic resources) - moby/swarmkit#2300 (limit the size of the external CA response) - moby/swarmkit#2301 (delete global tasks when the node running them is deleted) Minor cleanups, dependency bumps, and vendoring: - moby/swarmkit#2271 - moby/swarmkit#2279 - moby/swarmkit#2283 - moby/swarmkit#2282 - moby/swarmkit#2274 - moby/swarmkit#2296 (dependency bump of etcd, go-winio) Signed-off-by: Ying Li <ying.li@docker.com> Upstream-commit: 4509a00 Component: engine
At the moment we can only use NodeID as a template variable. The hostname is better for some kind of application and it's also more flexible and human readable. See #1951 or moby/moby#32561 for related proposals.
This PR is built on the previous work done by @gianarb