You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2020/07/08 13:18:29 UTC

[brooklyn-server] 19/20: Add simple service sensor

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

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit 8ddf9b071cc9660b613fc9dcbb4e6b88cfbc7f12
Author: Duncan Grant <du...@cloudsoft.io>
AuthorDate: Wed Jul 8 13:24:19 2020 +0100

    Add simple service sensor
---
 .../brooklyn/container/entity/helm/HelmEntity.java |  8 ++---
 .../container/entity/helm/HelmEntityImpl.java      | 38 ++++++++++++++--------
 .../container/entity/helm/HelmEntityLiveTest.java  |  2 --
 3 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/locations/container/src/main/java/org/apache/brooklyn/container/entity/helm/HelmEntity.java b/locations/container/src/main/java/org/apache/brooklyn/container/entity/helm/HelmEntity.java
index bed1d9c..3695510 100644
--- a/locations/container/src/main/java/org/apache/brooklyn/container/entity/helm/HelmEntity.java
+++ b/locations/container/src/main/java/org/apache/brooklyn/container/entity/helm/HelmEntity.java
@@ -77,14 +77,10 @@ public interface HelmEntity extends Entity, Startable {
    AttributeSensor<Boolean> DEPLOYMENT_READY = Sensors.newBooleanSensor("kube.deployment.status",
            "The status of the deploymeny");
 
-   AttributeSensor<Integer> AVAILABLE_REPLICAS = Sensors.newIntegerSensor("kube.replicas.available",
-           "The number of available replicas");
-
-   AttributeSensor<Integer> REPLICAS = Sensors.newIntegerSensor("kube.replicas",
-           "The number of replicas");
-
    AttributeSensor<List<String>> DEPLOYMENTS = Sensors.newSensor(new TypeToken<List<String>>() {}, "kube.deployments");
 
+   AttributeSensor<List<String>> SERVICES = Sensors.newSensor(new TypeToken<List<String>>() {}, "kube.services");
+
    @Effector(description="")
    Integer resize(@EffectorParam(name="deplymentName") String name, @EffectorParam(name="desiredSize") Integer desiredSize);
 
diff --git a/locations/container/src/main/java/org/apache/brooklyn/container/entity/helm/HelmEntityImpl.java b/locations/container/src/main/java/org/apache/brooklyn/container/entity/helm/HelmEntityImpl.java
index 0c2ccf1..26d0a75 100644
--- a/locations/container/src/main/java/org/apache/brooklyn/container/entity/helm/HelmEntityImpl.java
+++ b/locations/container/src/main/java/org/apache/brooklyn/container/entity/helm/HelmEntityImpl.java
@@ -20,6 +20,7 @@ package org.apache.brooklyn.container.entity.helm;
 
 import com.google.common.base.Functions;
 import com.google.common.collect.ImmutableList;
+import io.fabric8.kubernetes.api.model.Service;
 import io.fabric8.kubernetes.api.model.apps.Deployment;
 import io.fabric8.kubernetes.api.model.apps.DoneableDeployment;
 import io.fabric8.kubernetes.client.*;
@@ -126,8 +127,14 @@ public class HelmEntityImpl extends AbstractEntity implements HelmEntity {
                 .period(Duration.TEN_SECONDS)
                 .build());
 
+        addFeed(FunctionFeed.builder()
+                .entity(this)
+                .poll(new FunctionPollConfig<String, List<String>>(SERVICES).callable(getKubeServicesCallable()))
+                .period(Duration.TEN_SECONDS)
+                .build());
     }
 
+
     private void addHelmFeed(String command, AttributeSensor<String> sensor) {
         Callable<String> status = getCallable(command);
         FunctionPollConfig<String, String> pollConfig = new FunctionPollConfig<String, String>(sensor)
@@ -221,6 +228,24 @@ public class HelmEntityImpl extends AbstractEntity implements HelmEntity {
     }
 
 
+    private Callable<List<String>> getKubeServicesCallable() {
+        String helmNameInstallName = getConfig(HelmEntity.HELM_DEPLOYMENT_NAME);
+        String config = getLocation().getConfig(KubernetesLocation.KUBECONFIG);
+
+        return new Callable<List<String>>() {
+            @Override
+            public List<String> call() throws Exception {
+                KubernetesClient client = getClient(config);
+                List<Service> services = client.services().inNamespace(getNamespace()).list().getItems();
+                return services.stream()
+                        .filter(service -> service.getMetadata().getAnnotations().get("meta.helm.sh/release-name").equals(helmNameInstallName))
+                        .map(service -> service.getMetadata().getName())
+                        .collect(Collectors.toList());
+
+            }
+        };
+    }
+
     private Callable<List<String>> getKubeDeploymentsCallable() {
         String helmNameInstallName = getConfig(HelmEntity.HELM_DEPLOYMENT_NAME);
         String config = getLocation().getConfig(KubernetesLocation.KUBECONFIG);
@@ -305,19 +330,6 @@ public class HelmEntityImpl extends AbstractEntity implements HelmEntity {
         };
     }
 
-    private Callable<Integer> getKubeReplicasAvailableCallable(String deploymentName) {
-        String config = getLocation().getConfig(KubernetesLocation.KUBECONFIG);
-
-        return new Callable<Integer>() {
-            @Override
-            public Integer call() throws Exception {
-                KubernetesClient client = getClient(config);
-                Deployment deployment = client.apps().deployments().inNamespace(getNamespace()).withName(deploymentName).get();
-                return deployment.getStatus().getAvailableReplicas();
-            }
-        };
-    }
-
 
     KubernetesClient getClient(String configFile) {
         Path configPath = Paths.get(configFile);
diff --git a/locations/container/src/test/java/org/apache/brooklyn/container/entity/helm/HelmEntityLiveTest.java b/locations/container/src/test/java/org/apache/brooklyn/container/entity/helm/HelmEntityLiveTest.java
index 291f84d..d201e89 100644
--- a/locations/container/src/test/java/org/apache/brooklyn/container/entity/helm/HelmEntityLiveTest.java
+++ b/locations/container/src/test/java/org/apache/brooklyn/container/entity/helm/HelmEntityLiveTest.java
@@ -105,7 +105,6 @@ public class HelmEntityLiveTest extends BrooklynAppLiveTestSupport {
         assertAttributeEqualsEventually(andManageChild, HelmEntity.DEPLOYMENT_READY, true);
     }
 
-    //TODO Why is this broken?
     @Test(groups = {"Live"})
     public void testCanScaleCluster() {
         HelmEntity andManageChild = newHelmSpec("nginx-test", "bitnami/nginx");
@@ -123,7 +122,6 @@ public class HelmEntityLiveTest extends BrooklynAppLiveTestSupport {
         assertAttributeEqualsEventually(andManageChild, HelmEntity.DEPLOYMENT_READY, true);
     }
 
-    //TODO Why is this broken?
     @Test(groups = {"Live"})
     public void testCanScaleClusterPrometheus() {
         HelmEntity andManageChild = newHelmSpec("prometheus", PROMETHEUS_TEMPLATE_LOCATION);