Skip to content

Commit

Permalink
fix(pipeline_templates): load parent templates when inlining template…
Browse files Browse the repository at this point in the history
… for plan
  • Loading branch information
danielpeach committed Sep 14, 2017
1 parent 7b7733c commit 60fe165
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,18 @@ private Map<String, Object> processInternal(Map<String, Object> pipeline) {
}

private PipelineTemplate getPipelineTemplate(TemplatedPipelineRequest request, TemplateConfiguration templateConfiguration) {
List<PipelineTemplate> templates;
if (request.plan && request.template != null) {
// Allow template inlining to perform plans without first publishing the template somewhere.
return request.template;
}

if (request.getConfig().getPipeline().getTemplate() == null) {
PipelineTemplate template = pipelineTemplateObjectMapper.convertValue(request.template, PipelineTemplate.class);
templates = templateLoader.load(template);
} else if (request.getConfig().getPipeline().getTemplate() != null) {
setTemplateSourceWithJinja(request);
templates = templateLoader.load(templateConfiguration.getPipeline().getTemplate());
} else {
throw new IllegalTemplateConfigurationException(new Error().withMessage("configuration is missing a template"));
}

setTemplateSourceWithJinja(request);
List<PipelineTemplate> templates = templateLoader.load(templateConfiguration.getPipeline().getTemplate());

PipelineTemplate pipelineTemplate = TemplateMerge.merge(templates);

// ensure that any expressions contained with template variables are rendered
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ public TemplateLoader(Collection<TemplateSchemeLoader> schemeLoaders) {
* @return a LIFO list of pipeline templates
*/
public List<PipelineTemplate> load(TemplateConfiguration.TemplateSource template) {
PipelineTemplate pipelineTemplate = load(template.getSource());
return load(pipelineTemplate);
}

public List<PipelineTemplate> load(PipelineTemplate pipelineTemplate) {
List<PipelineTemplate> pipelineTemplates = new ArrayList<>();

PipelineTemplate pipelineTemplate = load(template.getSource());
pipelineTemplates.add(0, pipelineTemplate);

Set<String> seenTemplateSources = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.TemplateConfiguration;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -40,7 +41,9 @@ public void visitPipelineTemplate(PipelineTemplate pipelineTemplate) {
return;
}

Map<String, Object> configVars = templateConfiguration.getPipeline().getVariables();
Map<String, Object> configVars = templateConfiguration.getPipeline().getVariables() != null
? templateConfiguration.getPipeline().getVariables()
: new HashMap<>();

// if the config is missing vars and the template defines a default value, assign those values from the config
pipelineTemplate.getVariables().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ class PipelineTemplatePipelinePreprocessorSpec extends Specification {

then:
noExceptionThrown()
0 * templateLoader.load(_)
result.stages*.name == ['wait']

when:
Expand All @@ -228,6 +227,21 @@ class PipelineTemplatePipelinePreprocessorSpec extends Specification {
result.errors != null
}

def 'should load parent templates of inlined template during plan'() {
when:
def result = subject.process(createInlinedTemplateRequestWithParent(true, 'jinja-001.yml'))

then:
noExceptionThrown()
result.stages*.name == ['jinja1', 'childTemplateWait']

when:
result = subject.process(createInlinedTemplateRequestWithParent(false, 'jinja-001.yml'))

then:
result.errors != null
}

@Unroll
def 'should render jinja expressions contained within template variables'() {
given:
Expand Down Expand Up @@ -431,4 +445,34 @@ class PipelineTemplatePipelinePreprocessorSpec extends Specification {
plan: plan
]
}

Map<String, Object> createInlinedTemplateRequestWithParent(boolean plan, String templatePath) {
return [
type: 'templatedPipeline',
config: [
schema: '1',
pipeline: [
application: 'myapp'
]
],
template: [
schema: '1',
id: 'myTemplate',
stages: [
[
id: 'childTemplateWait',
type: 'wait',
config: [
waitTime: 5
],
inject: [
last: true
]
]
],
source: getClass().getResource("/templates/${templatePath}").toURI()
],
plan: plan,
]
}
}

0 comments on commit 60fe165

Please sign in to comment.