-
Notifications
You must be signed in to change notification settings - Fork 807
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(pipeline_template): Support multiple MPT schema versions (#1621
) Adds the ability to parse different schema versions. This PR doesn't actually change any functionality, except for switching to the new handler API. All existing v1 schema code works the same way it did. Additionally introduced Kotlin to module as new default.
- Loading branch information
1 parent
d0b3574
commit 59d9b1b
Showing
21 changed files
with
644 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 0 additions & 175 deletions
175
...ava/com/netflix/spinnaker/orca/pipelinetemplate/PipelineTemplatePipelinePreprocessor.java
This file was deleted.
Oops, something went wrong.
95 changes: 95 additions & 0 deletions
95
...src/main/java/com/netflix/spinnaker/orca/pipelinetemplate/PipelineTemplatePreprocessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2017 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.orca.pipelinetemplate | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.netflix.spectator.api.BasicTag | ||
import com.netflix.spectator.api.Registry | ||
import com.netflix.spinnaker.orca.extensionpoint.pipeline.PipelinePreprocessor | ||
import com.netflix.spinnaker.orca.pipelinetemplate.handler.* | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.stereotype.Component | ||
import javax.annotation.PostConstruct | ||
|
||
@Component("pipelineTemplatePreprocessor") | ||
class PipelineTemplatePreprocessor | ||
@Autowired constructor( | ||
private val pipelineTemplateObjectMapper: ObjectMapper, | ||
private val schemaVersionHandler: SchemaVersionHandler, | ||
private val errorHandler: PipelineTemplateErrorHandler, | ||
private val registry: Registry | ||
) : PipelinePreprocessor { | ||
|
||
private val log = LoggerFactory.getLogger(javaClass) | ||
private val requestsId = registry.createId("mpt.requests") | ||
|
||
@PostConstruct fun confirmUsage() = log.info("Using ${javaClass.simpleName}") | ||
|
||
override fun process(pipeline: MutableMap<String, Any>?): MutableMap<String, Any> { | ||
if (pipeline == null) { | ||
return mutableMapOf() | ||
} | ||
|
||
val request = pipelineTemplateObjectMapper.convertValue(pipeline, TemplatedPipelineRequest::class.java) | ||
if (!request.isTemplatedPipelineRequest) { | ||
return pipeline | ||
} | ||
|
||
log.debug("Starting handler chain") | ||
|
||
val chain = DefaultHandlerChain() | ||
val context = GlobalPipelineTemplateContext(chain, request) | ||
|
||
chain.add(schemaVersionHandler) | ||
|
||
while (!chain.isEmpty()) { | ||
val handler = chain.removeFirst() | ||
try { | ||
handler.handle(chain, context) | ||
} catch (t: Throwable) { | ||
if (handler is PipelineTemplateErrorHandler) { | ||
recordRequest(context, false) | ||
throw IrrecoverableConditionException(t) | ||
} | ||
|
||
log.error("Unexpected error occurred while processing template: (requestId: {})", context.getRequestId(), t) | ||
errorHandler.recordThrowable(t) | ||
chain.clear() | ||
} | ||
|
||
// Ensure the error handler is always the last thing we run | ||
if (chain.isEmpty() && handler !is PipelineTemplateErrorHandler) { | ||
chain.add(errorHandler) | ||
} | ||
} | ||
|
||
recordRequest(context, context.getErrors().hasErrors(false)) | ||
|
||
log.debug("Handler chain complete") | ||
return context.getProcessedOutput() | ||
} | ||
|
||
private fun recordRequest(context: PipelineTemplateContext, success: Boolean) { | ||
registry.counter(requestsId.withTags(listOf( | ||
BasicTag("status", if (success) "success" else "failure"), | ||
BasicTag("schema", context.getRequest().schema ?: "unknown"), | ||
BasicTag("plan", context.getRequest().plan.toString()) | ||
))).increment() | ||
} | ||
} | ||
|
||
private class IrrecoverableConditionException(t: Throwable) : RuntimeException("Could not recover from an error condition", t) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.