-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugins): Add callback API for constraint state changes (#1915)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
43f6199
commit acf3857
Showing
5 changed files
with
129 additions
and
19 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
28 changes: 28 additions & 0 deletions
28
keel-core/src/main/kotlin/com/netflix/spinnaker/keel/events/ConstraintStateChangedRelay.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,28 @@ | ||
package com.netflix.spinnaker.keel.events | ||
|
||
import com.netflix.spinnaker.keel.api.Constraint | ||
import com.netflix.spinnaker.keel.api.constraints.StatefulConstraintEvaluator | ||
import com.netflix.spinnaker.keel.api.events.ConstraintStateChanged | ||
import org.springframework.context.event.EventListener | ||
import org.springframework.stereotype.Component | ||
|
||
/** | ||
* An event listener that listens to [ConstraintStateChanged] events and relays them to the | ||
* corresponding [StatefulConstraintEvaluator] for processing. | ||
* | ||
* Note: in theory, constraint evaluators should be able to subscribe to these events directly, | ||
* but we've found that Spring auto-wiring does not seem to work for external plugins, so this | ||
* provides an alternative. | ||
*/ | ||
@Component | ||
class ConstraintStateChangedRelay( | ||
private val statefulEvaluators: List<StatefulConstraintEvaluator<*, *>> | ||
) { | ||
@EventListener(ConstraintStateChanged::class) | ||
fun onConstraintStateChanged(event: ConstraintStateChanged) { | ||
statefulEvaluators.supporting(event.constraint)?.onConstraintStateChanged(event) | ||
} | ||
|
||
private fun List<StatefulConstraintEvaluator<*, *>>.supporting(constraint: Constraint) = | ||
this.firstOrNull { it.supportedType.name == constraint.type } | ||
} |
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
70 changes: 70 additions & 0 deletions
70
...ore/src/test/kotlin/com/netflix/spinnaker/keel/events/ConstraintStateChangedRelayTests.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,70 @@ | ||
package com.netflix.spinnaker.keel.events | ||
|
||
import com.netflix.spinnaker.keel.api.Environment | ||
import com.netflix.spinnaker.keel.api.constraints.ConstraintState | ||
import com.netflix.spinnaker.keel.api.constraints.ConstraintStatus.OVERRIDE_PASS | ||
import com.netflix.spinnaker.keel.api.constraints.DefaultConstraintAttributes | ||
import com.netflix.spinnaker.keel.api.constraints.StatefulConstraintEvaluator | ||
import com.netflix.spinnaker.keel.api.constraints.SupportedConstraintType | ||
import com.netflix.spinnaker.keel.api.events.ConstraintStateChanged | ||
import com.netflix.spinnaker.keel.constraints.StatefulConstraintEvaluatorTests.Fixture.FakeConstraint | ||
import dev.minutest.junit.JUnit5Minutests | ||
import dev.minutest.rootContext | ||
import io.mockk.every | ||
import io.mockk.just | ||
import io.mockk.mockk | ||
import io.mockk.runs | ||
import io.mockk.verify | ||
|
||
class ConstraintStateChangedRelayTests : JUnit5Minutests { | ||
object Fixture { | ||
private val constraint = FakeConstraint() | ||
|
||
val event = ConstraintStateChanged( | ||
environment = Environment( | ||
name = "test", | ||
constraints = setOf(constraint) | ||
), | ||
constraint = constraint, | ||
previousState = null, | ||
currentState = ConstraintState( | ||
deliveryConfigName = "myconfig", | ||
environmentName = "test", | ||
artifactReference = "whatever", | ||
artifactVersion = "whatever", | ||
status = OVERRIDE_PASS, | ||
type = constraint.type | ||
) | ||
) | ||
|
||
internal val matchingEvaluator = mockk<StatefulConstraintEvaluator<FakeConstraint, DefaultConstraintAttributes>>() { | ||
every { supportedType } returns SupportedConstraintType("fake", FakeConstraint::class.java) | ||
every { onConstraintStateChanged(event) } just runs | ||
} | ||
|
||
internal val nonMatchingEvaluator = mockk<StatefulConstraintEvaluator<FakeConstraint, DefaultConstraintAttributes>>() { | ||
every { supportedType } returns SupportedConstraintType("the-wrong-fake", FakeConstraint::class.java) | ||
} | ||
|
||
val subject = ConstraintStateChangedRelay(listOf(matchingEvaluator, nonMatchingEvaluator)) | ||
} | ||
|
||
fun tests() = rootContext<Fixture> { | ||
context("a stateful constraint state changed event is emitted") { | ||
fixture { Fixture } | ||
|
||
before { | ||
subject.onConstraintStateChanged(event) | ||
} | ||
|
||
test("the event is relayed to the correct constraint evaluator") { | ||
verify(exactly = 1) { | ||
matchingEvaluator.onConstraintStateChanged(event) | ||
} | ||
verify(exactly = 0) { | ||
nonMatchingEvaluator.onConstraintStateChanged(event) | ||
} | ||
} | ||
} | ||
} | ||
} |