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

It should parse "Once" param from the config file #1439

Closed njegosrailic closed 3 years ago

njegosrailic commented 3 years ago

Seems like it ignores "Once" parameter from the config file. Tried to pass -once=true as a cmd parameter and it's working as expected.

Not sure did I miss something, but it parses out successfully all other params from the config file.

Consul Template version

consul-template v0.25.1 (b11fa800)

Command

/usr/local/bin/consul-template -config=/consul/config.json -log-level=debug

Configuration

{
  "consul": [
    {
      "address": "https://intra-consul.service.dev.csnzoo.com",
      "auth": [
        {
          "enabled": false
        }
      ],
      "retry": {
        "attempts": 6,
        "backoff": "250ms",
        "enabled": true,
        "max_backoff": "1m"
      },
      "ssl": {
        "ca_cert": "",
        "ca_path": "",
        "cert": "",
        "enabled": false,
        "key": "",
        "server_name": "",
        "verify": false
      }
    }
  ],
  "deduplicate": [
    {
      "enabled": false
    }
  ],
  "kill_signal": "SIGTERM",
  "log_level": "info",
  "max_stale": "10m",
  "pid_file": "",
  "reload_signal": null,
  "syslog": [
    {
      "enabled": false
    }
  ],
  "template": [
    {
      "backup": false,
      "contents": "---\n  resources:\n    {{- range ls \"kubernetes/config\" }}\n    {{ .Key }}: '{{ .Value | replaceAll \"\\n\" \"\" }}'{{ end }}",
      "create_dest_dirs": false,
      "destination": "/etc/config.yaml",
      "error_on_missing_key": false,
      "function_denylist": null,
      "left_delimiter": "{{",
      "perms": 0,
      "right_delimiter": "}}",
      "sandbox_path": "",
      "source": "",
      "wait": {
        "max": "10s",
        "min": "5s"
      }
    }
  ],
  "wait": [
    {
      "max": "10s",
      "min": "5s"
    }
  ],
  "block_query_wait": "60s"
  "Once": true

Debug output

2021/01/06 12:48:30.532305 [INFO] consul-template v0.25.1 (b11fa800)
2021/01/06 12:48:30.532320 [INFO] (runner) creating new runner (dry: false, once: false)
2021/01/06 12:48:30.532707 [DEBUG] (runner) final config: 
{
   "Consul":{
      "Address":"https://intra-consul.service",
      "Namespace":"",
      "Auth":{
         "Enabled":false,
         "Username":"",
         "Password":""
      },
      "Retry":{
         "Attempts":6,
         "Backoff":250000000,
         "MaxBackoff":60000000000,
         "Enabled":true
      },
      "SSL":{
         "CaCert":"",
         "CaPath":"",
         "Cert":"",
         "Enabled":false,
         "Key":"",
         "ServerName":"",
         "Verify":false
      },
      "Token":"",
      "Transport":{
         "DialKeepAlive":30000000000,
         "DialTimeout":30000000000,
         "DisableKeepAlives":false,
         "IdleConnTimeout":90000000000,
         "MaxIdleConns":100,
         "MaxIdleConnsPerHost":143,
         "TLSHandshakeTimeout":10000000000
      }
   },
   "Dedup":{
      "Enabled":false,
      "MaxStale":2000000000,
      "Prefix":"consul-template/dedup/",
      "TTL":15000000000,
      "BlockQueryWaitTime":60000000000
   },
   "DefaultDelims":{
      "Left":null,
      "Right":null
   },
   "Exec":{
      "Command":"",
      "Enabled":false,
      "Env":{
         "Denylist":[

         ],
         "Custom":[

         ],
         "Pristine":false,
         "Allowlist":[

         ]
      },
      "KillSignal":2,
      "KillTimeout":30000000000,
      "ReloadSignal":null,
      "Splay":0,
      "Timeout":0
   },
   "KillSignal":15,
   "LogLevel":"trace",
   "MaxStale":600000000000,
   "PidFile":"",
   "ReloadSignal":0,
   "Syslog":{
      "Enabled":false,
      "Facility":"LOCAL0",
      "Name":"consul-template"
   },
   "Templates":[
      {
         "Backup":false,
         "Command":"",
         "CommandTimeout":30000000000,
         "Contents":"---\n  resources:\n    {{- range ls \"kubernetes/config\" }}\n    {{ .Key }}: '{{ .Value | replaceAll \"\\n\" \"\" }}'{{ end }}",
         "CreateDestDirs":false,
         "Destination":"/etc/config.yaml",
         "ErrMissingKey":false,
         "Exec":{
            "Command":"",
            "Enabled":false,
            "Env":{
               "Denylist":[

               ],
               "Custom":[

               ],
               "Pristine":false,
               "Allowlist":[

               ]
            },
            "KillSignal":2,
            "KillTimeout":30000000000,
            "ReloadSignal":null,
            "Splay":0,
            "Timeout":30000000000
         },
         "Perms":0,
         "Source":"",
         "Wait":{
            "Enabled":true,
            "Min":5000000000,
            "Max":10000000000
         },
         "LeftDelim":"{{",
         "RightDelim":"}}",
         "FunctionDenylist":[

         ],
         "SandboxPath":""
      }
   ],
   "Vault":{
      "Address":"",
      "Enabled":false,
      "Namespace":"",
      "RenewToken":false,
      "Retry":{
         "Attempts":12,
         "Backoff":250000000,
         "MaxBackoff":60000000000,
         "Enabled":true
      },
      "SSL":{
         "CaCert":"",
         "CaPath":"",
         "Cert":"",
         "Enabled":true,
         "Key":"",
         "ServerName":"",
         "Verify":true
      },
      "Transport":{
         "DialKeepAlive":30000000000,
         "DialTimeout":30000000000,
         "DisableKeepAlives":false,
         "IdleConnTimeout":90000000000,
         "MaxIdleConns":100,
         "MaxIdleConnsPerHost":143,
         "TLSHandshakeTimeout":10000000000
      },
      "UnwrapToken":false
   },
   "Wait":{
      "Enabled":true,
      "Min":5000000000,
      "Max":10000000000
   },
   "Once":false,
   "BlockQueryWaitTime":60000000000
}

Expected behavior

0000},"UnwrapToken":false},"Wait":{"Enabled":true,"Min":5000000000,"Max":10000000000},"Once":true,"BlockQueryWaitTime":60000000000}

Actual behavior

0000},"UnwrapToken":false},"Wait":{"Enabled":true,"Min":5000000000,"Max":10000000000},"Once":false,"BlockQueryWaitTime":60000000000}
eikenb commented 3 years ago

Hey @njegosrailic, sorry that the docs are unclear on this point but -once is a command line only option, it has no corresponding config option.

njegosrailic commented 3 years ago

Hey @eikenb, thanks for the answer.

markschuh commented 1 year ago

Even when the issue itself is small, I wonder why this ticket is not at least kept open. The docs contain a very clear statements, the mode can be set via configuration file. As this is not the case this is more than just "unclear" docs - this inconsistency between docs and functionality is an open issue.

References: https://github.com/hashicorp/consul-template/blob/4e1d5b905c39a7e1464c52e8cca21e3422dfcb44/docs/configuration.md?plain=1#L622

Once Mode Configure Consul Template to execute each template exactly once and exits with the flag -once or in the configuration file. once = true

and https://github.com/hashicorp/consul-template/blob/4e1d5b905c39a7e1464c52e8cca21e3422dfcb44/docs/modes.md?plain=1#L17

To run in Once mode, include the -once flag or enable in the configuration file (links to above)

After some debugging and code inspection I understand, that a fix will not be that easy: It seems the used algorithm to render the final configuration would need some rework. At minimum for bool options the default value gets a higher prio during merge of configuration than the bool value set in a configuration file.

I will try, if I can strengthen this algorithm a bit and provide a merge request.