Skip to content

Commit

Permalink
feat(Fixes): First version running
Browse files Browse the repository at this point in the history
  • Loading branch information
dfradehubs committed Sep 17, 2024
1 parent c15cf3a commit 280ed0f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions examples/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions internal/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"io"
"log"
"net/http"
"strings"
"regexp"
"time"

"github.com/elastic/go-elasticsearch/v8"
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions internal/google/mig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/rand"
"strconv"
"strings"
"time"

"elasticsearch-vm-autoscaler/internal/elasticsearch"
"elasticsearch-vm-autoscaler/internal/globals"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 280ed0f

Please sign in to comment.