From 703c2b3e828beac2b570b36a8fa6cfba8936940a Mon Sep 17 00:00:00 2001 From: David Pilato Date: Fri, 28 Jun 2013 15:44:52 +0200 Subject: [PATCH] Filtering by tags ----------------- The GCE discovery can also filter machines to include in the cluster based on tags using `discovery.gce.tags` settings. For example, setting `discovery.gce.tags` to `dev` will only filter instances having a tag set to `dev`. Several tags set will require all of those tags to be set for the instance to be included. One practical use for tag filtering is when an GCE cluster contains many nodes that are not running elasticsearch. In this case (particularly with high ping_timeout values) there is a risk that a new node's discovery phase will end before it has found the cluster (which will result in it declaring itself master of a new cluster with the same name - highly undesirable). Adding tag on elasticsearch GCE nodes and then filtering by that tag will resolve this issue. Add your tag when building the new instance: ```sh gcutil --project=es-cloud addinstance myesnode1 --service_account_scope=compute-rw --persistent_boot_disk \ --tags=elasticsearch,dev ``` Then, define it in `elasticsearch.yml`: ```yaml cloud: gce: project_id: es-cloud zone: europe-west1-a discovery: type: gce gce: tags: elasticsearch, dev ``` Closes #2. --- README.md | 48 +++++++++++++++++++ .../cloud/gce/GceComputeService.java | 43 ++++++++++++++++- src/test/resources/elasticsearch.yml | 2 + 3 files changed, 91 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7eb3218e8ab..a4585750b4d 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,54 @@ gcutil --project=es-cloud deleteinstance myesnode1 myesnode2 --zone=europe-west1 gcutil --project=es-cloud deletedisk boot-myesnode1 boot-myesnode2 --zone=europe-west1-a ``` +Filtering by tags +----------------- + +The GCE discovery can also filter machines to include in the cluster based on tags using `discovery.gce.tags` settings. +For example, setting `discovery.gce.tags` to `dev` will only filter instances having a tag set to `dev`. Several tags +set will require all of those tags to be set for the instance to be included. + +One practical use for tag filtering is when an GCE cluster contains many nodes that are not running +elasticsearch. In this case (particularly with high ping_timeout values) there is a risk that a new node's discovery +phase will end before it has found the cluster (which will result in it declaring itself master of a new cluster +with the same name - highly undesirable). Adding tag on elasticsearch GCE nodes and then filtering by that +tag will resolve this issue. + +Add your tag when building the new instance: + +```sh +gcutil --project=es-cloud addinstance myesnode1 --service_account_scope=compute-rw --persistent_boot_disk \ + --tags=elasticsearch,dev +``` + +Then, define it in `elasticsearch.yml`: + +```yaml + cloud: + gce: + project_id: es-cloud + zone: europe-west1-a + discovery: + type: gce + gce: + tags: elasticsearch, dev +``` + +Tips +---- + +If you don't want to repeat the project id each time, you can save it in `~/.gcutil.flags` file using: + +```sh +gcutil getproject --project=es-cloud --cache_flag_values +``` + +`~/.gcutil.flags` file now contains: + +``` +--project=es-cloud +``` + License ------- diff --git a/src/main/java/org/elasticsearch/cloud/gce/GceComputeService.java b/src/main/java/org/elasticsearch/cloud/gce/GceComputeService.java index 8957384bd94..174dd283195 100644 --- a/src/main/java/org/elasticsearch/cloud/gce/GceComputeService.java +++ b/src/main/java/org/elasticsearch/cloud/gce/GceComputeService.java @@ -63,6 +63,8 @@ public class GceComputeService extends AbstractLifecycleComponent 0) { + if (instance.getTags() == null || instance.getTags().isEmpty()) { + // If this instance have no tag, we filter it + filterByTag = true; + } else { + // check that all tags listed are there on the instance + for (String tag : tags) { + boolean found = false; + for (String instancetag : instance.getTags().getItems()) { + if (instancetag.equals(tag)) { + found = true; + break; + } + } + if (!found) { + filterByTag = true; + break; + } + } + } + } + if (filterByTag) { + logger.trace("filtering out instance {} based tags {}, not part of {}", name, tags, + instance.getTags().getItems()); + continue; + } + String ip_public = null; String ip_private = null; @@ -133,12 +171,13 @@ public class GceComputeService extends AbstractLifecycleComponent