how do I get with confd the key string value generate with etcd

2.1k Views Asked by At

I am using confd and etcd. I am following the confd example for nginx. I put these keys in my etcd service:

curl http://127.0.0.1:4001/v2/keys/myapp/upstream -XPUT -d dir=true
curl http://127.0.0.1:4001/v2/keys/myapp/subdomain -XPUT -d value="myapp"
curl http://127.0.0.1:4001/v2/keys/myapp/upstream/app2 -XPUT -d value="10.0.1.101:80"
curl http://127.0.0.1:4001/v2/keys/myapp/upstream/app1 -XPUT -d value="10.0.1.100:80"

It is my toml configuration:

[template]
prefix = "myapp"
keys = [
  "subdomain",
  "upstream",
]
owner = "nginx"
mode = "0644"
src = "nginx.tmpl"
dest = "/tmp/myapp.conf"
check_cmd  = "/usr/sbin/nginx -t -c {{ .src }}"
reload_cmd = "/usr/sbin/service nginx reload"

And it is my tmpl configuration.

upstream {{.subdomain}} {
{{range $server := .upstream}}
    #I want to get the key string value
    server {{$sever.key}} {{$server.Value}};
{{end}}
}

server {
    server_name  {{.subdomain}}.example.com;

    location / {
        proxy_pass        http://{{.subdomain}};
        proxy_redirect    off;
        proxy_set_header  Host             $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
   }
}

Here I want to catch the key string value like app2 or app1. I am only know how to get its values. I want to do something like this {{$sever.key}} but it doesn't work. The configuration above {{$sever.key}} is wrong but I did it to show what do I want.

Is this idea possible?

Is there any reserverd word for that or syntax?

1

There are 1 best solutions below

0
On

I had the same problem. For me this code worked (adopted to your config):

{{range gets "/myapp/upstream/*"}}
server {{base .Key}} {{.Value}}
{{end}}

For me the main problem was the gets function. All examples I found on the net used getvs, which only provides the values. The base functions, just removes the first parts of the path of your key.

For further information, take a look at these confd docs.