From 280ed0f5d116f580e714db6e760d3439ff3260df Mon Sep 17 00:00:00 2001 From: dfradehubs Date: Tue, 17 Sep 2024 15:24:11 +0200 Subject: [PATCH] feat(Fixes): First version running --- examples/docker-compose.yml | 4 ++-- internal/elasticsearch/elasticsearch.go | 15 +++++++++------ internal/google/mig.go | 4 ++++ internal/prometheus/prometheus.go | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/examples/docker-compose.yml b/examples/docker-compose.yml index b525af4..a669239 100644 --- a/examples/docker-compose.yml +++ b/examples/docker-compose.yml @@ -38,8 +38,8 @@ services: - ELASTIC_URL=https://elasticsearch:9200 - ELASTIC_USER=elastic - ELASTIC_PASSWORD=test - - COOLDOWN_PERIOD_SEC=10 - - RETRY_INTERVAL_SEC=5 + - COOLDOWN_PERIOD_SEC=30 + - RETRY_INTERVAL_SEC=30 - GOOGLE_APPLICATION_CREDENTIALS=/tmp/credentials.json - MIN_SIZE=1 - MAX_SIZE=2 diff --git a/internal/elasticsearch/elasticsearch.go b/internal/elasticsearch/elasticsearch.go index 04ae794..c42c045 100644 --- a/internal/elasticsearch/elasticsearch.go +++ b/internal/elasticsearch/elasticsearch.go @@ -8,7 +8,7 @@ import ( "io" "log" "net/http" - "strings" + "regexp" "time" "github.com/elastic/go-elasticsearch/v8" @@ -89,7 +89,6 @@ func DrainElasticsearchNode(elasticURL, nodeName, username, password string) err // getNodeIP retrieves the IP address of the Elasticsearch node. func getNodeIP(es *elasticsearch.Client, nodeName string) (string, error) { - nodeName = "63fc4f0dd9ab" // Request to get the nodes information res, err := es.Cat.Nodes( es.Cat.Nodes.WithFormat("json"), @@ -154,6 +153,12 @@ func updateClusterSettings(es *elasticsearch.Client, nodeIP string) error { // waitForNodeRemoval waits for the node to be removed from the cluster. func waitForNodeRemoval(es *elasticsearch.Client, nodeName string) error { + // Prepare regex to match shards with + re, err := regexp.Compile(nodeName) + if err != nil { + log.Fatalf("Error compilando regex: %v", err) + } + for { res, err := es.Cat.Shards( es.Cat.Shards.WithFormat("json"), @@ -169,7 +174,6 @@ func waitForNodeRemoval(es *elasticsearch.Client, nodeName string) error { return fmt.Errorf("error reading response body: %w", err) } - fmt.Println(string(body)) var shards []ShardInfo err = json.Unmarshal([]byte(string(body)), &shards) if err != nil { @@ -178,15 +182,14 @@ func waitForNodeRemoval(es *elasticsearch.Client, nodeName string) error { nodeFound := false for _, shard := range shards { - log.Printf("Shard: %s, Node: %s", shard.Index, shard.Node) // Assuming `node` field contains the node name - if strings.Contains(shard.Node, nodeName) { + if re.MatchString(shard.Node) { nodeFound = true - break } } if !nodeFound { + log.Printf("node %s is fully empty and ready to delete", nodeName) break } diff --git a/internal/google/mig.go b/internal/google/mig.go index ae78a38..7aebd54 100644 --- a/internal/google/mig.go +++ b/internal/google/mig.go @@ -7,6 +7,7 @@ import ( "math/rand" "strconv" "strings" + "time" "elasticsearch-vm-autoscaler/internal/elasticsearch" "elasticsearch-vm-autoscaler/internal/globals" @@ -120,6 +121,9 @@ func RemoveNodeFromMIG(projectID, zone, migName, elasticURL, elasticUser, elasti if err != nil { return fmt.Errorf("error deleting instance: %v", err) } + // Wait 90 seconds until instance is fully deleted + // Google Cloud has a deletion timeout of 90 seconds max + time.Sleep(90 * time.Second) } // If not in debug mode, remove the elasticsearch node from cluster settings diff --git a/internal/prometheus/prometheus.go b/internal/prometheus/prometheus.go index de7f58c..5a1b6bb 100644 --- a/internal/prometheus/prometheus.go +++ b/internal/prometheus/prometheus.go @@ -21,7 +21,7 @@ type customTransport struct { // RoundTrip executes a single HTTP transaction and adds custom headers. func (t *customTransport) RoundTrip(req *http.Request) (*http.Response, error) { // Add custom headers from environment variables - if orgIdHeader := os.Getenv("X-SCOPE-ORGID_HEADER"); orgIdHeader != "" { + if orgIdHeader := os.Getenv("X_SCOPE_ORGID_HEADER"); orgIdHeader != "" { req.Header.Set("X-Scope-OrgID", orgIdHeader) } return t.Transport.RoundTrip(req)