Skip to content

Commit

Permalink
MGDCTRS-516 : health check and error handler not working when kafka i…
Browse files Browse the repository at this point in the history
…nstance is del… (#637)

* health check and error handler not working when kafka instance is deleted

* Update Integration test to check for KBL conditions rather than phase
  • Loading branch information
vaibhavjainwiz authored Apr 10, 2023
1 parent 47c3e84 commit cb9c40d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);

Expand Down Expand Up @@ -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<String, Object> status = (Map<String, Object>) 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -373,25 +373,20 @@ public static Optional<KameletBinding> 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<Condition> 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;
}
}
}

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class OperandTest extends BaseSpec {
CamelOperandSupport.computeStatus(
status,
new KameletBindingStatusBuilder()
.withPhase(KameletBindingStatus.PHASE_READY)
.addToConditions(new ConditionBuilder()
.withType("Ready")
.withStatus("False")
Expand All @@ -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()
Expand All @@ -65,7 +87,6 @@ class OperandTest extends BaseSpec {
CamelOperandSupport.computeStatus(
status,
new KameletBindingStatusBuilder()
.withPhase(KameletBindingStatus.PHASE_READY)
.addToConditions(new ConditionBuilder()
.withType("Ready")
.withStatus("True")
Expand All @@ -81,8 +102,6 @@ class OperandTest extends BaseSpec {
}
}



def 'status (error)'() {
given:
def status = new ConnectorStatusSpec()
Expand All @@ -91,19 +110,18 @@ 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());

then:
status.phase == STATE_FAILED
status.conditions.any {
it.type == 'Ready' && it.reason == 'reason'
it.type == 'Ready' && it.reason == 'Error'
}
}
}

0 comments on commit cb9c40d

Please sign in to comment.