Skip to content

Commit

Permalink
feat(main.go): create a new variable and set a default one on cooldow…
Browse files Browse the repository at this point in the history
…n periods (#2)
  • Loading branch information
rafalitox2 authored Sep 20, 2024
1 parent c9ebf31 commit 0e78428
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ This project is designed to automate the scaling of Google Cloud Managed Instanc
* ELASTIC_USER: Elasticsearch user for authentication (Default `elastic`)
* ELASTIC_PASSWORD: Elasticsearch password for authentication (Default `password`)
* ELASTIC_SSL_INSECURE_SKIP_VERIFY: Elasticsearch SSL certificate skip validation (Default `false`)
* COOLDOWN_PERIOD_SEC: Cooldown seconds to wait between scale checks (Default `60`)
* DEFAULT_COOLDOWN_PERIOD_SEC: Cooldown seconds to wait between default scale checks (Default `60`)
* SCALEDOWN_COOLDOWN_PERIOD_SEC: Cooldown seconds to wait between scaledown checks (Default `60`)
* RETRY_INTERVAL_SEC: Retry timeout when an error is reached during the loop (Default `60`)
* DEBUG_MODE: Does not execute scalations, just log and send slack messages (Default `false`)
* MIN_SIZE: Minimum size for the nodegroup (Default `1`)
Expand Down
12 changes: 8 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func main() {
elasticPassword := globals.GetEnv("ELASTIC_PASSWORD", "password")

// Cooldown and retry intervals in seconds, parsed from environment variables
cooldownPeriodSeconds, _ := strconv.ParseInt(globals.GetEnv("COOLDOWN_PERIOD_SEC", "60"), 10, 64)
scaledowncooldownPeriodSeconds, _ := strconv.ParseInt(globals.GetEnv("SCALEDOWN_COOLDOWN_PERIOD_SEC", "60"), 10, 64)
defaultcooldownPeriodSeconds, _ := strconv.ParseInt(globals.GetEnv("DEFAULT_COOLDOWN_PERIOD_SEC", "60"), 10, 64)
retryIntervalSeconds, _ := strconv.ParseInt(globals.GetEnv("RETRY_INTERVAL_SEC", "60"), 10, 64)

// Debug mode flag, enabled if "DEBUG_MODE" is set to "true"
Expand Down Expand Up @@ -91,6 +92,8 @@ func main() {
message := fmt.Sprintf("Added new node to MIG %s. Current size is %d nodes and the maximum nodes to create are %d", migName, currentSize, maxSize)
slack.NotifySlack(message, slackWebhookURL)
}
// Sleep for the default cooldown period before checking the conditions again
time.Sleep(time.Duration(defaultcooldownPeriodSeconds) * time.Second)
} else if downCondition { // If the down condition is met, remove a node from the MIG
log.Printf("Down condition %s met. Trying to remove one node!", prometheusDownCondition)
currentSize, minSize, nodeRemoved, err := google.RemoveNodeFromMIG(projectID, zone, migName, elasticURL, elasticUser, elasticPassword, debugMode)
Expand All @@ -104,12 +107,13 @@ func main() {
message := fmt.Sprintf("Removed node %s from MIG %s. Current size is %d nodes and the minimum nodes to exist are %d", nodeRemoved, migName, currentSize, minSize)
slack.NotifySlack(message, slackWebhookURL)
}
// Sleep for the scaledown cooldown period before checking the conditions again
time.Sleep(time.Duration(scaledowncooldownPeriodSeconds) * time.Second)
} else {
// No scaling conditions met, so no changes to the MIG
log.Printf("No condition %s or %s met, keeping the same number of nodes!", prometheusUpCondition, prometheusDownCondition)
// Sleep for the default cooldown period before checking the conditions again
time.Sleep(time.Duration(defaultcooldownPeriodSeconds) * time.Second)
}

// Sleep for the cooldown period before checking the conditions again
time.Sleep(time.Duration(cooldownPeriodSeconds) * time.Second)
}
}
3 changes: 2 additions & 1 deletion examples/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ services:
- ELASTIC_URL=https://elasticsearch:9200
- ELASTIC_USER=elastic
- ELASTIC_PASSWORD=test
- COOLDOWN_PERIOD_SEC=30
- DEFAULT_COOLDOWN_PERIOD_SEC=60
- SCALEDOWN_COOLDOWN_PERIOD_SEC=60
- RETRY_INTERVAL_SEC=30
- GOOGLE_APPLICATION_CREDENTIALS=/tmp/credentials.json
- MIN_SIZE=1
Expand Down

0 comments on commit 0e78428

Please sign in to comment.