Skip to content
Snippets Groups Projects
Commit 87b86fdd authored by Sören Henning's avatar Sören Henning
Browse files

Rework state handling

parent 230e497c
No related branches found
No related tags found
1 merge request!315Draft: Rework state handling
Pipeline #15995 passed
...@@ -17,7 +17,7 @@ abstract class AbstractStateHandler<S : HasMetadata>( ...@@ -17,7 +17,7 @@ abstract class AbstractStateHandler<S : HasMetadata>(
private val crd: Class<S> private val crd: Class<S>
) { ) {
private val crdClient: MixedOperation<S, KubernetesResourceList<S>, Resource<S>> = this.client.resources(this.crd) protected val crdClient: MixedOperation<S, KubernetesResourceList<S>, Resource<S>> = this.client.resources(this.crd)
@Synchronized @Synchronized
fun setState(resourceName: String, setter: (S) -> S?) { fun setState(resourceName: String, setter: (S) -> S?) {
...@@ -29,7 +29,8 @@ abstract class AbstractStateHandler<S : HasMetadata>( ...@@ -29,7 +29,8 @@ abstract class AbstractStateHandler<S : HasMetadata>(
// find out the difference between patchStatus and replaceStatus // find out the difference between patchStatus and replaceStatus
// see also https://github.com/fabric8io/kubernetes-client/pull/3798 // see also https://github.com/fabric8io/kubernetes-client/pull/3798
if (resourcePatched != null) { if (resourcePatched != null) {
this.crdClient.withName(resourcePatched.metadata.name).patchStatus(resourcePatched) //this.crdClient.withName(resourcePatched.metadata.name).patchStatus(resourcePatched)
this.client.resource(resourcePatched).patchStatus()
} }
} }
} catch (e: KubernetesClientException) { } catch (e: KubernetesClientException) {
......
...@@ -19,13 +19,19 @@ class ExecutionStateHandler(val client: NamespacedKubernetesClient) : ...@@ -19,13 +19,19 @@ class ExecutionStateHandler(val client: NamespacedKubernetesClient) :
private val executionStateAccessor = { cr: ExecutionCRD -> cr.status.executionState.value } private val executionStateAccessor = { cr: ExecutionCRD -> cr.status.executionState.value }
fun setExecutionState(resourceName: String, status: ExecutionState): Boolean { fun setExecutionState(resourceName: String, status: ExecutionState): Boolean {
super.setState(resourceName) { cr -> cr.status.executionState = status; cr } val execution = super.crdClient.withName(resourceName).get()
if (execution != null) {
execution.status.executionState = status
this.crdClient.resource(execution).patchStatus()
}
//super.setState(resourceName) { it.apply { this.status.executionState = status } }
return blockUntilStateIsSet(resourceName, status.value, executionStateAccessor) return blockUntilStateIsSet(resourceName, status.value, executionStateAccessor)
} }
fun getExecutionState(resourceName: String): ExecutionState { fun getExecutionState(resourceName: String): ExecutionState {
val statusString = this.getState(resourceName, executionStateAccessor) val statusString = this.getState(resourceName, executionStateAccessor)
return ExecutionState.values().first { it.value == statusString } return ExecutionState.values().first { it.value == statusString }
//return ExecutionState.entries.first { it.value == statusString }
} }
private fun updateDurationState(resourceName: String) { private fun updateDurationState(resourceName: String) {
......
...@@ -64,8 +64,8 @@ class ExecutionEventHandlerTestWithInformer { ...@@ -64,8 +64,8 @@ class ExecutionEventHandlerTestWithInformer {
@AfterEach @AfterEach
fun tearDown() { fun tearDown() {
server.after()
this.server.client.informers().stopAllRegisteredInformers() this.server.client.informers().stopAllRegisteredInformers()
this.server.after()
} }
@Test @Test
...@@ -95,6 +95,7 @@ class ExecutionEventHandlerTestWithInformer { ...@@ -95,6 +95,7 @@ class ExecutionEventHandlerTestWithInformer {
@Test @Test
@DisplayName("Test onAdd method for executions without execution state") @DisplayName("Test onAdd method for executions without execution state")
fun testOnAddWithoutStatus() { fun testOnAddWithoutStatus() {
println("Start testOnAddWithoutStatus")
// Create first version of execution resource // Create first version of execution resource
val executionResource = getExecutionFromSystemResource("k8s-resource-files/test-execution.yaml") val executionResource = getExecutionFromSystemResource("k8s-resource-files/test-execution.yaml")
val execution = executionResource.create() val execution = executionResource.create()
...@@ -139,6 +140,7 @@ class ExecutionEventHandlerTestWithInformer { ...@@ -139,6 +140,7 @@ class ExecutionEventHandlerTestWithInformer {
@Test @Test
@DisplayName("Test onUpdate method for execution with no status") @DisplayName("Test onUpdate method for execution with no status")
fun testOnUpdateWithoutStatus() { fun testOnUpdateWithoutStatus() {
println("start testOnUpdateWithoutStatus")
// Start informer // Start informer
this.executionClient.inform(eventHandler) this.executionClient.inform(eventHandler)
...@@ -168,6 +170,7 @@ class ExecutionEventHandlerTestWithInformer { ...@@ -168,6 +170,7 @@ class ExecutionEventHandlerTestWithInformer {
@MethodSource("provideOnUpdateTestArguments") @MethodSource("provideOnUpdateTestArguments")
@DisplayName("Test onUpdate method for execution with different status") @DisplayName("Test onUpdate method for execution with different status")
fun testOnUpdateWithStatus(beforeState: ExecutionState, expectedState: ExecutionState) { fun testOnUpdateWithStatus(beforeState: ExecutionState, expectedState: ExecutionState) {
println("Start testOnUpdateWithStatus / $beforeState / $expectedState")
// Create first version of execution resource // Create first version of execution resource
val firstExecutionResource = getExecutionFromSystemResource("k8s-resource-files/test-execution.yaml") val firstExecutionResource = getExecutionFromSystemResource("k8s-resource-files/test-execution.yaml")
val firstExecution = firstExecutionResource.create() val firstExecution = firstExecutionResource.create()
...@@ -233,37 +236,56 @@ class ExecutionEventHandlerTestWithInformer { ...@@ -233,37 +236,56 @@ class ExecutionEventHandlerTestWithInformer {
@Test @Test
fun testOnDeleteWithExecutionNotRunning() { fun testOnDeleteWithExecutionNotRunning() {
println("start testOnDeleteWithExecutionNotRunning")
// Create first version of execution resource // Create first version of execution resource
val firstExecutionResource = getExecutionFromSystemResource("k8s-resource-files/test-execution.yaml") val firstExecutionResource = getExecutionFromSystemResource("k8s-resource-files/test-execution.yaml")
val firstExecution = firstExecutionResource.create() val firstExecution = firstExecutionResource.create()
val executionName = firstExecution.metadata.name val executionName = firstExecution.metadata.name
println("Create first version of execution resource")
// Update status of execution to be running // Update status of execution to be running
firstExecution.status.executionState = ExecutionState.RUNNING firstExecution.status.executionState = ExecutionState.RUNNING
firstExecutionResource.patchStatus(firstExecution) firstExecutionResource.patchStatus(firstExecution)
println("Update status of execution to be running")
// Get execution from server // Get execution from server
val firstExecutionResponse = this.executionClient.withName(executionName).get() val firstExecutionResponse = this.executionClient.withName(executionName).get()
// Assert that execution created at server // Assert that execution created at server
assertNotNull(firstExecutionResponse) assertNotNull(firstExecutionResponse)
println("Get execution from server")
Thread.sleep(1000)
// Start informer // Start informer
this.executionClient.inform(eventHandler) this.executionClient.inform(eventHandler)
println("Start informer")
// We consider execution to be running // We consider execution to be running
whenever(this.controller.isExecutionRunning(executionName)).thenReturn(false) whenever(this.controller.isExecutionRunning(executionName)).thenReturn(false)
println("We consider execution to be running")
// Delete execution // Delete execution
this.executionClient.delete(firstExecutionResponse) this.executionClient.resource(firstExecutionResponse).delete()
println("Delete execution")
// Get execution from server // Get execution from server
val secondExecutionResponse = this.executionClient.withName(executionName).get() val secondExecutionResponse = this.executionClient.withName(executionName).get()
// Assert that execution created at server // Assert that execution created at server
assertNull(secondExecutionResponse) assertNull(secondExecutionResponse)
println("Get execution from server")
// Await informer called // Await informer called
this.deleteCountDownLatch.await(10, TimeUnit.SECONDS) this.deleteCountDownLatch.await(10, TimeUnit.SECONDS)
println("Await informer called")
verify(this.controller, never()).stop(false) verify(this.controller, never()).stop(false)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment