hashicorp / consul-template

Template rendering, notifier, and supervisor for @HashiCorp Consul and Vault data.
https://www.hashicorp.com/
Mozilla Public License 2.0
4.76k stars 781 forks source link

Node/service maintenance should go to the .Status node attribute #1010

Closed 123BLiN closed 7 years ago

123BLiN commented 7 years ago

Consul Template version

consul-template v0.18.2 (ecb9e46)

Configuration

consul {
  address = "my-host.org:443"
  retry {
    enabled = true
    attempts = -1
    backoff = "2s"
  }
  ssl {
  # This enables SSL. Specifying any option for SSL will also enable it.
  enabled = true
  # This sets the SNI server name to use for validation.
    server_name = "my-host.org"
}
}

max_stale = "10s"
log_level = "debug"
template {
  source = "E:/consul-template-test/test-backend.ctpl"
  destination = "E:/consul-template-test/test-backend"
  #command = "sudo service nginx reload"
  perms = 0644
  left_delimiter  = "{:"
  right_delimiter = ":}"
}
{:/* Assign consul service name to the variable */:}
{: $consul_service := "test" :}
upstream {: $consul_service :} {

{:/* Check if such service exists in consul */:}
{: if service (printf "%s" $consul_service) :}
{: range service (printf "%s|critical,maintenance" $consul_service) :}
  server  {: .Address :}:{: .Port :} down; {: end :}
{: range service (printf "%s|passing,warning" $consul_service) :}
  server  {: .Address :}:{: .Port :};   {: end :}
{:/* Set dummy server if consul service not found in order to not break nginx config integrity */:}
{: else :}  server 127.0.0.1:8888 down;{: end :}
}

Command

consul-template.exe -config .\test.hcl -once

Debug output

PS E:\consul-template-test> consul-template.exe -config .\testu.hcl -once
2017/09/12 04:44:15.311665 [INFO] consul-template v0.18.2 (ecb9e46)
2017/09/12 04:44:15.311665 [INFO] (runner) creating new runner (dry: false, once: true)
2017/09/12 04:44:15.312668 [DEBUG] (runner) final config: {"Consul":{"Address":"my-host.org:443","Auth":{"Ena
bled":false,"Username":"","Password":""},"Retry":{"Attempts":-1,"Backoff":2000000000,"Enabled":true},"SSL":{"CaCert":"",
"CaPath":"","Cert":"","Enabled":true,"Key":"","ServerName":"my-host.org","Verify":true},"Token":"","Transport
":{"DialKeepAlive":30000000000,"DialTimeout":30000000000,"DisableKeepAlives":false,"IdleConnTimeout":90000000000,"MaxIdl
eConns":100,"MaxIdleConnsPerHost":9,"TLSHandshakeTimeout":10000000000}},"Dedup":{"Enabled":false,"MaxStale":2000000000,"
Prefix":"consul-template/dedup/","TTL":15000000000},"Exec":{"Command":"","Enabled":false,"Env":{"Blacklist":[],"Custom":
[],"Pristine":false,"Whitelist":[]},"KillSignal":2,"KillTimeout":30000000000,"ReloadSignal":null,"Splay":0,"Timeout":0},
"KillSignal":2,"LogLevel":"debug","MaxStale":10000000000,"PidFile":"","ReloadSignal":1,"Syslog":{"Enabled":false,"Facili
ty":"LOCAL0"},"Templates":[{"Backup":false,"Command":"","CommandTimeout":30000000000,"Contents":"","Destination":"E:/con
sul-template-test/testu-backend","Exec":{"Command":"","Enabled":false,"Env":{"Blacklist":[],"Custom":[],"Prist
ine":false,"Whitelist":[]},"KillSignal":2,"KillTimeout":30000000000,"ReloadSignal":null,"Splay":0,"Timeout":30000000000}
,"Perms":420,"Source":"E:/consul-template-test/testu-backend.ctpl","Wait":{"Enabled":false,"Min":0,"Max":0},"L
eftDelim":"{:","RightDelim":":}"}],"Vault":{"Address":"","Enabled":false,"RenewToken":true,"Retry":{"Attempts":5,"Backof
f":250000000,"Enabled":true},"SSL":{"CaCert":"","CaPath":"","Cert":"","Enabled":true,"Key":"","ServerName":"","Verify":t
rue},"Transport":{"DialKeepAlive":30000000000,"DialTimeout":30000000000,"DisableKeepAlives":false,"IdleConnTimeout":9000
0000000,"MaxIdleConns":100,"MaxIdleConnsPerHost":9,"TLSHandshakeTimeout":10000000000},"UnwrapToken":false},"Wait":{"Enab
led":false,"Min":0,"Max":0}}
2017/09/12 04:44:15.312668 [INFO] (runner) creating watcher
2017/09/12 04:44:15.313670 [INFO] (runner) starting
2017/09/12 04:44:15.313670 [DEBUG] (runner) running initial templates
2017/09/12 04:44:15.313670 [INFO] (runner) initiating run
2017/09/12 04:44:15.313670 [DEBUG] (runner) checking template b8e97d90919120f11c9c1de65b1b68e0
2017/09/12 04:44:15.313670 [DEBUG] (runner) was not watching 1 dependencies
2017/09/12 04:44:15.313670 [DEBUG] (watcher) adding health.service(testu|passing)
2017/09/12 04:44:15.314673 [DEBUG] (runner) diffing and updating dependencies
2017/09/12 04:44:15.314673 [DEBUG] (runner) watching 1 dependencies
2017/09/12 04:44:16.969155 [DEBUG] (runner) receiving dependency health.service(testu|passing)
2017/09/12 04:44:16.969670 [INFO] (runner) initiating run
2017/09/12 04:44:16.971157 [DEBUG] (runner) checking template b8e97d90919120f11c9c1de65b1b68e0
2017/09/12 04:44:16.972194 [DEBUG] (runner) was not watching 2 dependencies
2017/09/12 04:44:16.973169 [DEBUG] (watcher) adding health.service(testu|critical,maintenance)
2017/09/12 04:44:16.974171 [DEBUG] (watcher) adding health.service(testu|passing,warning)
2017/09/12 04:44:16.975203 [DEBUG] (runner) diffing and updating dependencies
2017/09/12 04:44:16.976174 [DEBUG] (runner) health.service(testu|passing) is still needed
2017/09/12 04:44:16.976174 [DEBUG] (runner) watching 3 dependencies
2017/09/12 04:44:17.262900 [DEBUG] (runner) receiving dependency health.service(testu|critical,maintenance)
2017/09/12 04:44:17.262900 [INFO] (runner) initiating run
2017/09/12 04:44:17.264404 [DEBUG] (runner) checking template b8e97d90919120f11c9c1de65b1b68e0
2017/09/12 04:44:17.265907 [DEBUG] (runner) missing data for 1 dependencies
2017/09/12 04:44:17.265907 [DEBUG] (runner) diffing and updating dependencies
2017/09/12 04:44:17.266910 [DEBUG] (runner) health.service(testu|passing) is still needed
2017/09/12 04:44:17.267412 [DEBUG] (runner) health.service(testu|critical,maintenance) is still needed
2017/09/12 04:44:17.268414 [DEBUG] (runner) health.service(testu|passing,warning) is still needed
2017/09/12 04:44:17.268914 [DEBUG] (runner) watching 3 dependencies
2017/09/12 04:44:17.547400 [DEBUG] (runner) receiving dependency health.service(testu|passing,warning)
2017/09/12 04:44:17.547400 [INFO] (runner) initiating run
2017/09/12 04:44:17.548430 [DEBUG] (runner) checking template b8e97d90919120f11c9c1de65b1b68e0
2017/09/12 04:44:17.550406 [DEBUG] (runner) rendering "E:/consul-template-test/testu-backend.ctpl" => "E:/cons
ul-template-test/testu-backend"
2017/09/12 04:44:17.574139 [INFO] (runner) rendered "E:/consul-template-test/testu-backend.ctpl" => "E:/consul
-template-test/testu-backend"
2017/09/12 04:44:17.574139 [DEBUG] (runner) diffing and updating dependencies
2017/09/12 04:44:17.575143 [DEBUG] (runner) health.service(testu|passing) is still needed
2017/09/12 04:44:17.576146 [DEBUG] (runner) health.service(testu|critical,maintenance) is still needed
2017/09/12 04:44:17.577144 [DEBUG] (runner) health.service(testu|passing,warning) is still needed
2017/09/12 04:44:17.577144 [DEBUG] (runner) watching 3 dependencies
2017/09/12 04:44:17.578150 [INFO] (runner) once mode and all templates rendered
2017/09/12 04:44:17.579153 [INFO] (runner) stopping
2017/09/12 04:44:17.579153 [DEBUG] (runner) stopping watcher
2017/09/12 04:44:17.580155 [DEBUG] (watcher) stopping all views

Expected behavior

We are going to have 1000-2000 nginx configs and consul services, every CT makes 3 calls - we have faced ephemeral port exhaustion, we need a way to make only one call per template like so:

{:/* Assign consul service name to the variable */:}
{: $consul_service := "test" :}
upstream {: $consul_service :} {

{: range service $consul_service :}
  {: if (or (eq .Status "critical") (eq .Status "maintenance")) :}server  {: .Address :}:{: .Port :}  down;
  {: else :}server  {: .Address :}:{: .Port :};{: end :}
{:/* Set dummy server if consul service not found in order to not break nginx config integrity */:}
{: else :}  server 127.0.0.1:8888 down;{: end :}
}

Unfortunately "maintenance" state is only accessible with filters but not with .Status property (it is still "passing")

It will be great if it will be accessible as a node's attribute, at least as an additional bool like .Maintenance = true/false if it is not possible to attach it to the .Status

What do you think?

sethvargo commented 7 years ago

Hi @123BLiN

You can call .Checks.AggregatedStatus() which should return "maintenance" for your maintenance-enabled services. It will also return "passing", "warning", etc where appropriate.

Please let me know if you have any questions.

123BLiN commented 7 years ago

Hi @sethvargo !

Could you please provide an example template? I'm having hard time to get it work.

update:

Sorry - my fault - it works with new consul-template version (0.19.3) {{ .Checks.AggregatedStatus }}