Skip to content

Add support for templating Node.Hostname in docker executor#2266

Merged
aaronlehmann merged 1 commit intomoby:masterfrom
mion00:feature/template-node-hostname
Jun 22, 2017
Merged

Add support for templating Node.Hostname in docker executor#2266
aaronlehmann merged 1 commit intomoby:masterfrom
mion00:feature/template-node-hostname

Conversation

@mion00
Copy link
Copy Markdown

@mion00 mion00 commented Jun 17, 2017

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

@mion00 mion00 force-pushed the feature/template-node-hostname branch from 3bab7af to 12dfa91 Compare June 17, 2017 08:47
@codecov
Copy link
Copy Markdown

codecov bot commented Jun 17, 2017

Codecov Report

Merging #2266 into master will increase coverage by 0.01%.
The diff coverage is 80.85%.

@@            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) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What is this for? I don't see anything that uses it.

Copy link
Copy Markdown
Author

@mion00 mion00 Jun 20, 2017

Choose a reason for hiding this comment

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

It was a leftover from my attempts at using NodeInspect with the Docker client.. I'll remove this change.

ctlr, err := newController(e.client, t, secrets.Restrict(e.secrets, t))
ctx := context.Background()
// Get node information
nodeDescription, err := e.Describe(ctx)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

While we're at it, would it make sense to add other properties from NodeDescription, like Architecture and OS?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

// 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) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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?

dependencyManager.Configs().Add(*referencedConfig)

templatedDependencies := NewTemplatedDependencyGetter(agent.Restrict(dependencyManager, testCase.task), testCase.task)
templatedDependencies := NewTemplatedDependencyGetter(agent.Restrict(dependencyManager, testCase.task), testCase.task, &api.NodeDescription{})
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you please add test cases that expand Hostname in a template?

@GordonTheTurtle
Copy link
Copy Markdown

Please sign your commits following these rules:
https://github.com/moby/moby/blob/master/CONTRIBUTING.md#sign-your-work
The easiest way to do this is to amend the last commit:

$ 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 -f

Amending updates the existing PR. You DO NOT need to open a new one.

@mion00 mion00 force-pushed the feature/template-node-hostname branch from 0684538 to da63b8b Compare June 20, 2017 11:24
@mion00 mion00 force-pushed the feature/template-node-hostname branch from da63b8b to 064c801 Compare June 20, 2017 11:34
// - 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?
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You can use api.Platform instead of redefining the struct here.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

That's exactly what I thought 😉

genericResources: genericResources,
}
// Get the node information from the newly created executor
var ctx = context.Background()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I added a mutex to the executor field. Sorry but it is my first time using Golang. I hope the code makes sense.

ID string
ID string
Hostname string
Platform
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is this embedded?

// 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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Argument should be in order of context, so node, then task.

Copy link
Copy Markdown
Collaborator

@aaronlehmann aaronlehmann left a comment

Choose a reason for hiding this comment

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

Thanks for making the changes. I think this is getting close.

client engineapi.APIClient
secrets exec.SecretsManager
genericResources []*api.GenericResource
mutex *sync.Mutex // This mutex protects the following node field
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The field doesn't have to be a pointer. This can be something like:

mu             sync.Mutex

and then it doesn't need to be initialized.

ctx.Node.Platform = Platform{
Architecture: n.Platform.Architecture,
OS: n.Platform.OS,
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@aaronlehmann
Copy link
Copy Markdown
Collaborator

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>
@mion00 mion00 force-pushed the feature/template-node-hostname branch from 06c398f to 69602b4 Compare June 21, 2017 20:25
@stevvooe
Copy link
Copy Markdown
Contributor

LGTM

@aaronlehmann aaronlehmann merged commit 9cbc2cc into moby:master Jun 22, 2017
andrewhsu pushed a commit to docker-archive/docker-ce that referenced this pull request Jul 14, 2017
- 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
silvin-lubecki pushed a commit to silvin-lubecki/engine-extract that referenced this pull request Mar 16, 2020
- 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
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.

4 participants