Skip to content

Commit

Permalink
feat(pipeline_template): Jinja rendering in triggers, params and noti…
Browse files Browse the repository at this point in the history
…fications (#1619)
  • Loading branch information
robzienert authored Sep 18, 2017
1 parent a0089d2 commit 92d7270
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public Map<String, Object> generate(PipelineTemplate template, TemplateConfigura
}

addNotifications(pipeline, template, configuration);
addParameters(pipeline, template, configuration);

pipeline.put("stages", template.getStages()
.stream()
Expand Down Expand Up @@ -91,4 +92,21 @@ private void addNotifications(Map<String, Object> pipeline, PipelineTemplate tem
);
}
}

private void addParameters(Map<String, Object> pipeline, PipelineTemplate template, TemplateConfiguration configuration) {
if (configuration.getConfiguration().getInherit().contains("parameters")) {
pipeline.put(
"parameterConfig",
TemplateMerge.mergeNamedContent(
template.getConfiguration().getParameters(),
configuration.getConfiguration().getParameters()
)
);
} else {
pipeline.put(
"parameterConfig",
Optional.ofNullable(configuration.getConfiguration().getParameters()).orElse(Collections.emptyList())
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.netflix.spinnaker.orca.pipelinetemplate.exceptions.IllegalTemplateConfigurationException;
import com.netflix.spinnaker.orca.pipelinetemplate.exceptions.TemplateRenderException;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.PipelineTemplateVisitor;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.NamedHashMap;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.PartialDefinition;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.PipelineTemplate;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.StageDefinition;
Expand Down Expand Up @@ -74,6 +75,15 @@ private void render(PipelineTemplate template) {

// We don't care about configuration partials, they were already merged into the template at this point
renderPartials(template.getPartials(), filterStages(template.getStages(), true), context);

renderConfigurations(template.getConfiguration().getParameters(), context, "template:configuration.parameters");
renderConfigurations(templateConfiguration.getConfiguration().getParameters(), context, "configuration:configuration.parameters");

renderConfigurations(template.getConfiguration().getTriggers(), context, "template:configuration.triggers");
renderConfigurations(templateConfiguration.getConfiguration().getTriggers(), context, "configuration:configuration.triggers");

renderConfigurations(template.getConfiguration().getNotifications(), context, "template:configuration.notifications");
renderConfigurations(templateConfiguration.getConfiguration().getNotifications(), context, "configuration:configuration.notifications");
}

private void renderStages(List<StageDefinition> stages, RenderContext context, String locationNamespace) {
Expand Down Expand Up @@ -196,4 +206,25 @@ private void renderPartials(List<PartialDefinition> partials, List<StageDefiniti
partial.getRenderedPartials().put(stage.getId(), renderedStages);
}
}

private void renderConfigurations(List<NamedHashMap> configurations, RenderContext context, String location) {
if (configurations == null) {
return;
}

for (Map<String, Object> config : configurations) {
for (Map.Entry<String, Object> pair : config.entrySet()) {
try {
pair.setValue(RenderUtil.deepRender(renderer, pair.getValue(), context));
} catch (TemplateRenderException e) {
throw TemplateRenderException.fromError(
new Error()
.withMessage("Failed rendering configuration property")
.withLocation(location),
e
);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class PipelineTemplatePipelinePreprocessorSpec extends Specification {
requisiteStageRefIds: ['tagImage'],
waitTime: 5
]
]
],
parameterConfig: []
]
assertReflectionEquals(expected, result, ReflectionComparatorMode.IGNORE_DEFAULTS)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"application": "orca",
"name": "Unnamed Execution",
"stages": [],
"notifications": []
"notifications": [],
"parameterConfig": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
}
],
"id": "unknown",
"notifications": []
"notifications": [],
"parameterConfig": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
]
}
],
"notifications": []
"notifications": [],
"parameterConfig": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@
"waitTime": 5
}
],
"notifications": []
"notifications": [],
"parameterConfig": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
"waitTime": 5
}
],
"notifications": []
"notifications": [],
"parameterConfig": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"application": "orca",
"name": "Unnamed Execution",
"stages": [],
"notifications": []
"notifications": [],
"parameterConfig": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@
}
],
"id": "unknown",
"notifications": []
"notifications": [],
"parameterConfig": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
schema: "1"
pipeline:
application: orca
configuration:
inherit: ['parameters']
stages: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": "unknown",
"keepWaitingPipelines": false,
"limitConcurrent": true,
"application": "orca",
"name": "Unnamed Execution",
"stages": [],
"notifications": [],
"parameterConfig": [
{
"name": "instances",
"description": "Number of instances to start per region",
"default": 3
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
schema: "1"
id: variableParameters
metadata:
name: Variable Parameters
description: Asserts that variables can be used in parameters.
configuration:
parameters:
- name: instances
description: Number of instances to start per region
default: "{{ instances }}"
variables:
- name: instances
description: The number of instances to start per region
defaultValue: 3
stages: []

0 comments on commit 92d7270

Please sign in to comment.