From cb9c40dcdec96debd14757440f4eb28de3f592d3 Mon Sep 17 00:00:00 2001 From: Vaibhav Jain Date: Mon, 10 Apr 2023 17:13:59 +0530 Subject: [PATCH] =?UTF-8?q?MGDCTRS-516=20:=20health=20check=20and=20error?= =?UTF-8?q?=20handler=20not=20working=20when=20kafka=20instance=20is=20del?= =?UTF-8?q?=E2=80=A6=20(#637)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * health check and error handler not working when kafka instance is deleted * Update Integration test to check for KBL conditions rather than phase --- .../it/camel/glues/KameletBindingSteps.java | 24 +----------- .../resources/CamelConnectorEvents.feature | 2 +- .../resources/CamelConnectorReSync.feature | 2 +- .../CamelConnectorReifyErrorHandler.feature | 8 +++- .../operator/camel/CamelOperandSupport.java | 38 ++++++++++--------- .../camel/model/KameletBindingStatus.java | 2 - .../operator/camel/OperandTest.groovy | 32 ++++++++++++---- 7 files changed, 55 insertions(+), 53 deletions(-) diff --git a/cos-fleetshard-operator-camel-it/src/test/java/org/bf2/cos/fleetshard/operator/it/camel/glues/KameletBindingSteps.java b/cos-fleetshard-operator-camel-it/src/test/java/org/bf2/cos/fleetshard/operator/it/camel/glues/KameletBindingSteps.java index d7ffd2c4..8cb16898 100644 --- a/cos-fleetshard-operator-camel-it/src/test/java/org/bf2/cos/fleetshard/operator/it/camel/glues/KameletBindingSteps.java +++ b/cos-fleetshard-operator-camel-it/src/test/java/org/bf2/cos/fleetshard/operator/it/camel/glues/KameletBindingSteps.java @@ -40,8 +40,8 @@ public void does_not_exists() { } @SuppressWarnings("unchecked") - @When("the klb phase is {string} with conditions:") - public void klb_phase_and_conditions(String phase, DataTable table) { + @When("the klb with conditions:") + public void klb_phase_and_conditions(DataTable table) { // TODO: investigate using KubernetesClient.resources(KameletBinding.class) result in a bad patch kubernetesClient.genericKubernetesResources(KameletBinding.RESOURCE_DEFINITION) @@ -66,7 +66,6 @@ public void klb_phase_and_conditions(String phase, DataTable table) { .build()); } - status.put("phase", phase); status.put("conditions", conditions); binding.getAdditionalProperties().put("status", status); @@ -101,25 +100,6 @@ public void klb_phase_and_conditions(String phase, DataTable table) { // }); } - @SuppressWarnings("unchecked") - @When("the klb phase is {string}") - public void klb_phase_and_conditions(String phase) { - kubernetesClient.genericKubernetesResources(KameletBinding.RESOURCE_DEFINITION) - .inNamespace(ctx.connector().getMetadata().getNamespace()) - .withName(ctx.connector().getMetadata().getName()) - .editStatus(binding -> { - Map status = (Map) binding.getAdditionalProperties().get("status"); - if (status == null) { - status = new HashMap<>(); - } - - status.put("phase", phase); - binding.getAdditionalProperties().put("status", status); - - return binding; - }); - } - @When("the klb path {string} is set to json:") public void klb_pointer(String path, String payload) { kubernetesClient.resources(KameletBinding.class) diff --git a/cos-fleetshard-operator-camel-it/src/test/resources/CamelConnectorEvents.feature b/cos-fleetshard-operator-camel-it/src/test/resources/CamelConnectorEvents.feature index e65c781b..8cc1f687 100644 --- a/cos-fleetshard-operator-camel-it/src/test/resources/CamelConnectorEvents.feature +++ b/cos-fleetshard-operator-camel-it/src/test/resources/CamelConnectorEvents.feature @@ -22,7 +22,7 @@ Feature: Camel Connector Status Then the klb exists Then the klb secret exists - When the klb phase is "Ready" with conditions: + When the klb with conditions: | message | reason | status | type | lastTransitionTime | | a message | a reason | True | Ready | 2021-06-12T12:35:09+02:00 | Then the connector is in phase "Monitor" diff --git a/cos-fleetshard-operator-camel-it/src/test/resources/CamelConnectorReSync.feature b/cos-fleetshard-operator-camel-it/src/test/resources/CamelConnectorReSync.feature index 021f59ff..0dafcab9 100644 --- a/cos-fleetshard-operator-camel-it/src/test/resources/CamelConnectorReSync.feature +++ b/cos-fleetshard-operator-camel-it/src/test/resources/CamelConnectorReSync.feature @@ -22,7 +22,7 @@ Feature: Camel Connector ReSync Then the klb exists Then the klb secret exists - When the klb phase is "Ready" with conditions: + When the klb with conditions: | message | reason | status | type | lastTransitionTime | | a message | a reason | True | Ready | 2021-06-12T12:35:09+02:00 | Then the connector is in phase "Monitor" diff --git a/cos-fleetshard-operator-camel-it/src/test/resources/CamelConnectorReifyErrorHandler.feature b/cos-fleetshard-operator-camel-it/src/test/resources/CamelConnectorReifyErrorHandler.feature index 7095e841..08d44a63 100644 --- a/cos-fleetshard-operator-camel-it/src/test/resources/CamelConnectorReifyErrorHandler.feature +++ b/cos-fleetshard-operator-camel-it/src/test/resources/CamelConnectorReifyErrorHandler.feature @@ -80,7 +80,9 @@ Feature: Camel Connector Reify Then the klb exists And the klb has an entry at path "$.spec.errorHandler.sink.endpoint.uri" with value "rc:fail?routeId=current" - When the klb phase is "error" + When the klb with conditions: + | message | reason | status | type | lastTransitionTime | + | a message | Error | False | Ready | 2021-06-12T12:35:09+02:00 | Then the connector is in phase "Monitor" And the connector operand status is in phase "failed" @@ -102,6 +104,8 @@ Feature: Camel Connector Reify Then the klb exists And the klb has an entry at path "$.spec.errorHandler.sink.endpoint.uri" with value "rc:fail?routeId=current" - When the klb phase is "error" + When the klb with conditions: + | message | reason | status | type | lastTransitionTime | + | a message | Error | False | Ready | 2021-06-12T12:35:09+02:00 | Then the connector is in phase "Monitor" And the connector operand status is in phase "failed" diff --git a/cos-fleetshard-operator-camel/src/main/java/org/bf2/cos/fleetshard/operator/camel/CamelOperandSupport.java b/cos-fleetshard-operator-camel/src/main/java/org/bf2/cos/fleetshard/operator/camel/CamelOperandSupport.java index 5ec64fcc..82dd1f74 100644 --- a/cos-fleetshard-operator-camel/src/main/java/org/bf2/cos/fleetshard/operator/camel/CamelOperandSupport.java +++ b/cos-fleetshard-operator-camel/src/main/java/org/bf2/cos/fleetshard/operator/camel/CamelOperandSupport.java @@ -373,25 +373,20 @@ public static Optional lookupBinding(KubernetesClient client, Ma } public static void computeStatus(ConnectorStatusSpec statusSpec, KameletBindingStatus kameletBindingStatus) { - if (kameletBindingStatus.phase != null) { - switch (kameletBindingStatus.phase.toLowerCase(Locale.US)) { - case KameletBindingStatus.PHASE_READY: - statusSpec.setPhase(ManagedConnector.STATE_PROVISIONING); - if (kameletBindingStatus.conditions != null) { - boolean readyCondition = kameletBindingStatus.conditions.stream() - .anyMatch(cond -> "Ready".equals(cond.getType()) - && "True".equals(cond.getStatus())); - if (readyCondition) { - statusSpec.setPhase(ManagedConnector.STATE_READY); - } + statusSpec.setPhase(ManagedConnector.STATE_PROVISIONING); + if (kameletBindingStatus.conditions != null) { + Optional readyConditionOpt = kameletBindingStatus.conditions.stream() + .filter(cond -> "Ready".equals(cond.getType())).findAny(); + if (readyConditionOpt.isPresent()) { + Condition readyCondition = readyConditionOpt.get(); + boolean klbReady = "True".equals(readyCondition.getStatus()); + if (klbReady) { + statusSpec.setPhase(ManagedConnector.STATE_READY); + } else { + if (isErrorReasons(readyCondition.getReason())) { + statusSpec.setPhase(ManagedConnector.STATE_FAILED); } - break; - case KameletBindingStatus.PHASE_ERROR: - statusSpec.setPhase(ManagedConnector.STATE_FAILED); - break; - default: - statusSpec.setPhase(ManagedConnector.STATE_PROVISIONING); - break; + } } } @@ -416,6 +411,13 @@ public static void computeStatus(ConnectorStatusSpec statusSpec, KameletBindingS .ifPresent(ConditionMessageImprover::improve); } + private static boolean isErrorReasons(String reason) { + if (reason == null) { + return false; + } + return List.of("Error", "RuntimeNotReady").contains(reason); + } + public static ObjectNode createErrorHandler( CamelShardMetadata shardMetadata, ManagedConnector connector, diff --git a/cos-fleetshard-operator-camel/src/main/java/org/bf2/cos/fleetshard/operator/camel/model/KameletBindingStatus.java b/cos-fleetshard-operator-camel/src/main/java/org/bf2/cos/fleetshard/operator/camel/model/KameletBindingStatus.java index 756938e7..086240af 100644 --- a/cos-fleetshard-operator-camel/src/main/java/org/bf2/cos/fleetshard/operator/camel/model/KameletBindingStatus.java +++ b/cos-fleetshard-operator-camel/src/main/java/org/bf2/cos/fleetshard/operator/camel/model/KameletBindingStatus.java @@ -18,8 +18,6 @@ @JsonInclude(JsonInclude.Include.NON_NULL) @Buildable(builderPackage = "io.fabric8.kubernetes.api.builder") public class KameletBindingStatus { - public static final String PHASE_READY = "ready"; - public static final String PHASE_ERROR = "error"; @JsonProperty public String phase; diff --git a/cos-fleetshard-operator-camel/src/test/groovy/org/bf2/cos/fleetshard/operator/camel/OperandTest.groovy b/cos-fleetshard-operator-camel/src/test/groovy/org/bf2/cos/fleetshard/operator/camel/OperandTest.groovy index a757bbd3..de86f36f 100644 --- a/cos-fleetshard-operator-camel/src/test/groovy/org/bf2/cos/fleetshard/operator/camel/OperandTest.groovy +++ b/cos-fleetshard-operator-camel/src/test/groovy/org/bf2/cos/fleetshard/operator/camel/OperandTest.groovy @@ -41,7 +41,6 @@ class OperandTest extends BaseSpec { CamelOperandSupport.computeStatus( status, new KameletBindingStatusBuilder() - .withPhase(KameletBindingStatus.PHASE_READY) .addToConditions(new ConditionBuilder() .withType("Ready") .withStatus("False") @@ -57,6 +56,29 @@ class OperandTest extends BaseSpec { } } + def 'status (RuntimeNotReady)'() { + given: + def status = new ConnectorStatusSpec() + + when: + CamelOperandSupport.computeStatus( + status, + new KameletBindingStatusBuilder() + .addToConditions(new ConditionBuilder() + .withType("Ready") + .withStatus("False") + .withReason("RuntimeNotReady") + .withMessage("message") + .build()) + .build()); + + then: + status.phase == STATE_FAILED + status.conditions.any { + it.type == 'Ready' && it.reason == 'RuntimeNotReady' + } + } + def 'status (ready)'() { given: def status = new ConnectorStatusSpec() @@ -65,7 +87,6 @@ class OperandTest extends BaseSpec { CamelOperandSupport.computeStatus( status, new KameletBindingStatusBuilder() - .withPhase(KameletBindingStatus.PHASE_READY) .addToConditions(new ConditionBuilder() .withType("Ready") .withStatus("True") @@ -81,8 +102,6 @@ class OperandTest extends BaseSpec { } } - - def 'status (error)'() { given: def status = new ConnectorStatusSpec() @@ -91,11 +110,10 @@ class OperandTest extends BaseSpec { CamelOperandSupport.computeStatus( status, new KameletBindingStatusBuilder() - .withPhase(KameletBindingStatus.PHASE_ERROR) .addToConditions(new ConditionBuilder() .withType("Ready") .withStatus("False") - .withReason("reason") + .withReason("Error") .withMessage("message") .build()) .build()); @@ -103,7 +121,7 @@ class OperandTest extends BaseSpec { then: status.phase == STATE_FAILED status.conditions.any { - it.type == 'Ready' && it.reason == 'reason' + it.type == 'Ready' && it.reason == 'Error' } } }