Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse Library precedence during loading to prevent library overriding #132

Merged
merged 10 commits into from
Nov 23, 2020
9 changes: 7 additions & 2 deletions Version_2.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,17 @@ As JTE grows in adoption, there is plenty that can be done to improve our abilit

== Feature Development

* [] https://github.com/jenkinsci/templating-engine-plugin/pull/132[Allow for reversal of library sources traversal]
* [x] https://github.com/jenkinsci/templating-engine-plugin/issues/111[move JTE behavior feature flags into a root level `jte` block in the pipeline configuration]
* [x] https://github.com/jenkinsci/templating-engine-plugin/issues/100[Pipeline Job: added SCM pipeline configuration and template]
* [x] https://github.com/jenkinsci/templating-engine-plugin/pull/101[JTE pipelines can survive an ungraceful restart now]
* [x] https://github.com/jenkinsci/templating-engine-plugin/pull/98[Added pipelineConfig reserved variable]
* [x] https://github.com/jenkinsci/templating-engine-plugin/issues/46[Add support for Library Resources]
* [x] https://github.com/jenkinsci/templating-engine-plugin/issues/97[Field-level pipeline configuration governance]
* [x] https://github.com/jenkinsci/templating-engine-plugin/issues/79[Standardize on autowired variables - remove the Lifecycle Hook context parameter in favor of an autowired variable]
* [x] https://github.com/jenkinsci/templating-engine-plugin/issues/72[Document the StageContext functionality to pass arguments to steps run as part of a Stage]
* [ ] https://github.com/jenkinsci/templating-engine-plugin/issues/62[Introduce step namespacing so that multiple steps with the same name can be loaded via a less-strict initialization process configurable by the user]
* [ ] https://github.com/jenkinsci/templating-engine-plugin/issues/84[Improve overall initialization logging for JTE to help users debug issues]
* [x] https://github.com/jenkinsci/templating-engine-plugin/issues/62[Introduce step namespacing so that multiple steps with the same name can be loaded via a less-strict initialization process configurable by the user]
* [x] https://github.com/jenkinsci/templating-engine-plugin/issues/84[Improve overall initialization logging for JTE to help users debug issues]
* [ ] https://github.com/jenkinsci/templating-engine-plugin/issues/23[Investigate whether or not JTE can integrate with Jenkins Pipeline's Declarative Syntax]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jte{ <1>
allow_scm_jenkinsfile = true
pipeline_template = "some_template"
permissive_initialization = false
reverse_library_resolution = false
}

template_methods{} <2>
Expand Down Expand Up @@ -59,6 +60,10 @@ keywords{} <7>
| Whether or not JTE will allow a naming collision within the binding. For example, two library's that both contribute a step by the same name or a keyword and application environment by the same name.
| `false`

| `reverse_library_resolution`
| Normally, JTE resolves libraries based on the library sources from the highest governance tier down towards the job, xref:governance:library_selection.adoc[default library selection]. This flag, if enabled, reverses the library search order
| `false`

|===

[WARNING]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,9 @@ class PipelineDecorator extends InvisibleAction {
}

String determinePipelineTemplate(){
LinkedHashMap pipelineConfig = config.getConfig()
WorkflowJob job = getJob()
FlowDefinition flowDefinition = job.getDefinition()
JteBlockWrapper jteBlockWrapper = (pipelineConfig.jte ?: [:]) as JteBlockWrapper
JteBlockWrapper jteBlockWrapper = config.jteBlockWrapper
if (flowDefinition instanceof AdHocTemplateFlowDefinition){
String template = flowDefinition.getTemplate(flowOwner)
if(template){
Expand Down Expand Up @@ -172,14 +171,16 @@ class PipelineDecorator extends InvisibleAction {
String pipeline_template = null
Boolean allow_scm_jenkinsfile = true
Boolean permissive_initialization = false
Boolean reverse_library_resolution = false

static LinkedHashMap getSchema(){
return [
fields: [
optional: [
allow_scm_jenkinsfile: Boolean,
pipeline_template: String,
permissive_initialization: Boolean
permissive_initialization: Boolean,
reverse_library_resolution: Boolean
]
]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.boozallen.plugins.jte.init.governance.config.dsl

import org.boozallen.plugins.jte.init.PipelineDecorator
import org.boozallen.plugins.jte.util.TemplateLogger
import org.codehaus.groovy.runtime.InvokerHelper
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner
Expand All @@ -38,6 +39,10 @@ class PipelineConfigurationObject implements Serializable{
this.flowOwner = flowOwner
}

PipelineDecorator.JteBlockWrapper getJteBlockWrapper(){
return (config.jte ?: [:]) as PipelineDecorator.JteBlockWrapper
}

PipelineConfigurationObject plus(PipelineConfigurationObject child){
/*
If this is the first call to join, then there is no pre-existing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TemplateBindingFactory {

static TemplateBinding create(FlowExecutionOwner flowOwner, PipelineConfigurationObject config){
invoke("validateConfiguration", flowOwner, config)
JteBlockWrapper jte = (config.getConfig().jte ?: [:]) as JteBlockWrapper
JteBlockWrapper jte = config.jteBlockWrapper
TemplateBinding templateBinding = new TemplateBinding(flowOwner, jte.permissive_initialization)
invoke("injectPrimitives", flowOwner, config, templateBinding)
invoke("validateBinding", flowOwner, config, templateBinding)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ import org.jenkinsci.plugins.workflow.job.WorkflowJob
LinkedHashMap aggregatedConfig = config.getConfig()
AggregateException errors = new AggregateException()
List<LibraryProvider> providers = getLibraryProviders(flowOwner)
boolean reverseProviders = config.jteBlockWrapper.reverse_library_resolution
if(reverseProviders) {
providers = providers.reverse()
}
ConfigValidator validator = new ConfigValidator(flowOwner)
aggregatedConfig[KEY].each { libName, libConfig ->
LibraryProvider provider = providers.find{ provider ->
Expand Down Expand Up @@ -86,7 +90,12 @@ import org.jenkinsci.plugins.workflow.job.WorkflowJob
@Override
void injectPrimitives(FlowExecutionOwner flowOwner, PipelineConfigurationObject config, TemplateBinding binding){
LinkedHashMap aggregatedConfig = config.getConfig()

List<LibraryProvider> providers = getLibraryProviders(flowOwner)
boolean reverseProviders = config.jteBlockWrapper.reverse_library_resolution
if(reverseProviders) {
providers = providers.reverse()
}
aggregatedConfig[KEY].each{ libName, libConfig ->
LibraryProvider provider = providers.find{ provider ->
provider.hasLibrary(flowOwner, libName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ class PipelineDecoratorSpec extends Specification{
then:
jte.allow_scm_jenkinsfile == expected_allow
jte.pipeline_template == expected_template
jte.permissive_initialization == permissive
jte.reverse_library_resolution == reverse_libs

where:
config | expected_allow | expected_template
[:] | true | null
[allow_scm_jenkinsfile:false] | false | null
[allow_scm_jenkinsfile:false, pipeline_template:'dev_template'] | false | 'dev_template'
config | expected_allow | expected_template | permissive | reverse_libs
[:] | true | null | false | false
[allow_scm_jenkinsfile:false, reverse_library_resolution:true] | false | null | false | true
[allow_scm_jenkinsfile:false, pipeline_template:'dev_template', permissive_initialization:true] | false | 'dev_template' | true | false
}

}
Loading