template: support templating of task runtime parameters#1650
template: support templating of task runtime parameters#1650aaronlehmann merged 1 commit intomoby:masterfrom
Conversation
|
This is pretty cool. |
|
There are some golint issues around copying a structure with a lock inside it. I don't have a strong opinion about the template cache. I would think that templates are relatively cheap to parse (sort of like a printf format string), and if we didn't cache them, we would only need to reparse when handling a new task. This is not exactly something we'd be doing in an inner loop, so the caching probably doesn't buy us much. But maybe template parsing has more overhead than I naively expect. |
Yeah, template compilation is dirt cheap compared to prepping a container |
|
I'll remove the template cache. I was breaking my own rules about premature optimization and felt dirty while doing it. If we prove a bottleneck here, we'll take care of it later. The actual answer might be to "de-feature" the template system, since complex templating isn't really needed. |
8af1afe to
fcc7968
Compare
|
Looks pretty cool. Will this bear any relation with config management? |
|
@diogomonica This is the engine for configuration management:
|
|
That is awesome @stevvooe ! My usecase would be to create a dir mounted as tmpfs and create a file in it containing credentials pulled from Vault that my service can access transparently. Can't wait for this to land. |
|
@tlvenn for this release all the secrets will be stored on the swarm managers, and will have to be added/removed/managed directly from the |
|
This looks great. What do you think about supporting |
|
@dnephin There is no
|
|
LGTM Please rebase |
ed66a05 to
7e18ee1
Compare
|
Added support for parameterizing |
34a8aad to
c70ff24
Compare
Current coverage is 55.89% (diff: 54.34%)@@ master #1650 diff @@
==========================================
Files 93 96 +3
Lines 14796 14886 +90
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 8283 8320 +37
- Misses 5425 5466 +41
- Partials 1088 1100 +12
|
|
@diogomonica Can you link the Issue/PR/Proposal if any that introduce |
c70ff24 to
5ccd351
Compare
|
A few more changes:
|
|
ping @aaronlehmann @diogomonica PTAL, let's see if we can get this into 1.13 ❤️ |
template/context.go
Outdated
| ctx.Node.ID = t.NodeID | ||
|
|
||
| ctx.Task.ID = t.ID | ||
| ctx.Task.Name = fmt.Sprintf("%s.%d.%s", t.ServiceAnnotations.Name, t.Slot, t.ID) |
There was a problem hiding this comment.
I know it probably sounds like I'm trolling, but I remember you felt very strongly that task name derivation should not be duplicated throughout the code, and this seems to add another copy of it.
I think in the current code, t.Annotations.Name stores a precomputed task name? Although @aluzzardi said he wanted to revert this by 1.13.
There was a problem hiding this comment.
Do as I say, not as I do?
🐹
We still need to centralize the naming. Is this okay for now or do want me to introduce the naming package?
There was a problem hiding this comment.
A function in api/naming would do
There was a problem hiding this comment.
@aluzzardi Seems outside the scope of this PR. I've asked for this in 6 other PRs and I'm not gating this feature on that addition if no one else is willing to.
template/expand.go
Outdated
| func ExpandContainerSpec(t *api.Task) (*api.ContainerSpec, error) { | ||
| container := t.Spec.GetContainer() | ||
| if container == nil { | ||
| return nil, nil |
There was a problem hiding this comment.
Should this return an error?
| return values, err | ||
| } | ||
|
|
||
| entry = fmt.Sprintf("%s=%s", entry, expanded) |
There was a problem hiding this comment.
Personal preference - feel free to ignore - but I find entry + "=" + expanded easier to read and I like the type safety benefits.
There was a problem hiding this comment.
Doesn't that require two intermediates?
template/expand.go
Outdated
| } | ||
|
|
||
| n[k] = v | ||
|
|
There was a problem hiding this comment.
nit: blank line looks out of place
| return container, err | ||
| } | ||
|
|
||
| container.Hostname, err = ctx.Expand(container.Hostname) |
There was a problem hiding this comment.
In this and all the other calls to ctx.Expand, it seems like a good idea to wrap the error with an indication of which field caused the error.
a9ac2ae to
2b15064
Compare
|
LGTM Needs rebase |
To better support common use cases that require injected service and task information, we now treat ContainerSpec fields as templates. A common, simplified context is defined that contains commonly needed values, with access to names, identifiers and labels. This initial PR focuses on supporting mounts, hostname and environment variables. By supporting these, users will be able to locate mounts based on task context or service name, unlocking many use cases that were previously challenging or impossible. Environment variables open up further possibilities. Hostname is mostly harmless but useful in certain applications. Signed-off-by: Stephen J Day <[email protected]>
2b15064 to
21cdd63
Compare

To better support common use cases that require injected service and
task information, we now treat ContainerSpec fields as templates. A
common, simplified context is defined that contains commonly needed
values, with access to names, identifiers and labels.
This initial PR focuses on supporting mounts and environment variables.
By supporting these, users will be able to locate mounts based on task
context or service name, unlocking many use cases that were previously
challenging or impossible. Environment variables open up further
possibilities.
Signed-off-by: Stephen J Day [email protected]
cc @aluzzardi @mgoelzer @ehazlett @NathanMcCauley @diogomonica