You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2022/12/19 10:35:04 UTC

[camel] 01/08: CAMEL-18131 - camel-health - Add health checks for components that has extension for connectivity verification - ECS

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 2459ba1dd360208c527b0cbb22f000ab5d79f0b1
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Mon Dec 19 11:18:50 2022 +0100

    CAMEL-18131 - camel-health - Add health checks for components that has extension for connectivity verification - ECS
    
    Signed-off-by: Andrea Cosentino <an...@gmail.com>
---
 components/camel-aws/camel-aws2-ecs/pom.xml        |  8 +++
 .../component/aws2/ecs/ECS2ClientHealthCheck.java  | 80 ++++++++++++++++++++++
 .../camel/component/aws2/ecs/ECS2Endpoint.java     | 17 +++++
 .../camel/component/aws2/eks/EKS2Endpoint.java     |  1 -
 4 files changed, 105 insertions(+), 1 deletion(-)

diff --git a/components/camel-aws/camel-aws2-ecs/pom.xml b/components/camel-aws/camel-aws2-ecs/pom.xml
index 7f2758a72ff..7cd72801a92 100644
--- a/components/camel-aws/camel-aws2-ecs/pom.xml
+++ b/components/camel-aws/camel-aws2-ecs/pom.xml
@@ -50,6 +50,10 @@
             <artifactId>apache-client</artifactId>
             <version>${aws-java-sdk2-version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-health</artifactId>
+        </dependency>
 
         <!-- for testing -->
         <dependency>
@@ -67,5 +71,9 @@
             <artifactId>log4j-slf4j-impl</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-health</artifactId>
+        </dependency>
     </dependencies>
 </project>
diff --git a/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/ECS2ClientHealthCheck.java b/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/ECS2ClientHealthCheck.java
new file mode 100644
index 00000000000..801f0391fe2
--- /dev/null
+++ b/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/ECS2ClientHealthCheck.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.camel.component.aws2.ecs;
+
+import org.apache.camel.component.aws2.ecs.client.ECS2ClientFactory;
+import org.apache.camel.component.aws2.ecs.client.ECS2InternalClient;
+import org.apache.camel.health.HealthCheckResultBuilder;
+import org.apache.camel.impl.health.AbstractHealthCheck;
+import org.apache.camel.util.ObjectHelper;
+import software.amazon.awssdk.awscore.exception.AwsServiceException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.ecs.EcsClient;
+import software.amazon.awssdk.services.ecs.model.ListClustersRequest;
+
+import java.util.Map;
+
+public class ECS2ClientHealthCheck extends AbstractHealthCheck {
+
+    private final ECS2Endpoint ecs2Endpoint;
+    private final String clientId;
+
+    public ECS2ClientHealthCheck(ECS2Endpoint ecs2Endpoint, String clientId) {
+        super("camel", "aws2-ecs-client-" + clientId);
+        this.ecs2Endpoint = ecs2Endpoint;
+        this.clientId = clientId;
+    }
+
+    @Override
+    public boolean isLiveness() {
+        // this health check is only readiness
+        return false;
+    }
+
+    @Override
+    protected void doCall(HealthCheckResultBuilder builder, Map<String, Object> options) {
+        ECS2Configuration configuration = ecs2Endpoint.getConfiguration();
+        if (ObjectHelper.isNotEmpty(configuration.getRegion())) {
+            if (!EcsClient.serviceMetadata().regions().contains(Region.of(configuration.getRegion()))) {
+                builder.message("The service is not supported in this region");
+                builder.down();
+                return;
+            }
+        }
+        try {
+            ECS2InternalClient ecs2Client = ECS2ClientFactory.getEcsClient(configuration);
+            ecs2Client.getEcsClient().listClusters(ListClustersRequest.builder().build());
+        } catch (AwsServiceException e) {
+            builder.message(e.getMessage());
+            builder.error(e);
+            if (ObjectHelper.isNotEmpty(e.statusCode())) {
+                builder.detail(SERVICE_STATUS_CODE, e.statusCode());
+            }
+            if (ObjectHelper.isNotEmpty(e.awsErrorDetails().errorCode())) {
+                builder.detail(SERVICE_ERROR_CODE, e.awsErrorDetails().errorCode());
+            }
+            builder.down();
+            return;
+        } catch (Exception e) {
+            builder.error(e);
+            builder.down();
+            return;
+        }
+        builder.up();
+    }
+}
diff --git a/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/ECS2Endpoint.java b/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/ECS2Endpoint.java
index 882a2821c28..45a40c3196c 100644
--- a/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/ECS2Endpoint.java
+++ b/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/ECS2Endpoint.java
@@ -22,6 +22,8 @@ import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.component.aws2.ecs.client.ECS2ClientFactory;
+import org.apache.camel.health.HealthCheckHelper;
+import org.apache.camel.impl.health.ComponentsHealthCheckRepository;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.support.ScheduledPollEndpoint;
@@ -37,6 +39,8 @@ import software.amazon.awssdk.services.ecs.EcsClient;
 public class ECS2Endpoint extends ScheduledPollEndpoint {
 
     private EcsClient ecsClient;
+    private ComponentsHealthCheckRepository healthCheckRepository;
+    private ECS2ClientHealthCheck clientHealthCheck;
 
     @UriParam
     private ECS2Configuration configuration;
@@ -62,10 +66,23 @@ public class ECS2Endpoint extends ScheduledPollEndpoint {
 
         ecsClient = configuration.getEcsClient() != null
                 ? configuration.getEcsClient() : ECS2ClientFactory.getEcsClient(configuration).getEcsClient();
+
+        healthCheckRepository = HealthCheckHelper.getHealthCheckRepository(getCamelContext(),
+                ComponentsHealthCheckRepository.REPOSITORY_ID, ComponentsHealthCheckRepository.class);
+
+        if (healthCheckRepository != null) {
+            clientHealthCheck = new ECS2ClientHealthCheck(this, getId());
+            healthCheckRepository.addHealthCheck(clientHealthCheck);
+        }
     }
 
     @Override
     public void doStop() throws Exception {
+        if (healthCheckRepository != null && clientHealthCheck != null) {
+            healthCheckRepository.removeHealthCheck(clientHealthCheck);
+            clientHealthCheck = null;
+        }
+        
         if (ObjectHelper.isEmpty(configuration.getEcsClient())) {
             if (ecsClient != null) {
                 ecsClient.close();
diff --git a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/EKS2Endpoint.java b/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/EKS2Endpoint.java
index a4eb5527ba1..3cc78c687b3 100644
--- a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/EKS2Endpoint.java
+++ b/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/EKS2Endpoint.java
@@ -78,7 +78,6 @@ public class EKS2Endpoint extends ScheduledPollEndpoint {
 
     @Override
     public void doStop() throws Exception {
-
         if (healthCheckRepository != null && clientHealthCheck != null) {
             healthCheckRepository.removeHealthCheck(clientHealthCheck);
             clientHealthCheck = null;