From 0e784283180b48ad65bff11d677028694b9d8cbd Mon Sep 17 00:00:00 2001 From: Rafael Hormigo Gamez <81415832+rafalitox2@users.noreply.github.com> Date: Fri, 20 Sep 2024 10:04:13 +0200 Subject: [PATCH] feat(main.go): create a new variable and set a default one on cooldown periods (#2) --- README.md | 3 ++- cmd/main.go | 12 ++++++++---- examples/docker-compose.yml | 3 ++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fdf40f2..4699517 100644 --- a/README.md +++ b/README.md @@ -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`) diff --git a/cmd/main.go b/cmd/main.go index 06e6f4c..54bfcdb 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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" @@ -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) @@ -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) } } diff --git a/examples/docker-compose.yml b/examples/docker-compose.yml index e9c1778..499ff6d 100644 --- a/examples/docker-compose.yml +++ b/examples/docker-compose.yml @@ -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