Skip to content

Understanding how to use whereLabelExists #354

@mattheys

Description

@mattheys

Let me start by saying this is a brilliant project, however I think the templating section could do with a bit of an overhaul on the documentation side, or some examples that include all the available functions, they wouldn't need to be real world examples but just examples of how each function is used and a quick description would be very handy.

Anyway, I want to use the whereLabelExists in my nginx.tmpl file so I can include an external config file to setup vouch automatically if I have a label like nginx.vouch.enable=true on a contianer.

I tried to add this in

{{ whereLabelExists $containers "nginx.vouch" }}
  include vouch.conf;
{{ end }}

but docker-gen reported it couldn't parse my template as $host was undefined, I think by adding {{ end }} i've closed the {{ range $index, $value := $containers }} further up.

I then tried this

{{ $vouchContainers := whereLabelExists $containers "nginx.vouch" }}
{{ $vouch := len $vouchContainers }}


server {
  listen 443 ssl http2;
  server_name {{ $host }};
{{ if gt $vouch 0 }}
  include vouch.conf;
{{ end }}

but now I'm passing in an interface array not a dockergen.Context

I assumed the $containers in {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} was a slice and that's what the README.MD says whereLabelExists takes.

Does anyone have any hints on how I can enable the include vouch.conf; line only if a label exists? I realise this can map multiple containers to a single host but I don't use that functionality, every container has a unique fqdn.

{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
upstream {{ $host }} {

{{ range $index, $value := $containers }}

        {{ $addrLen := len $value.Addresses }}
        {{ $network := index $value.Networks 0 }}

        {{/* If only 1 port exposed, use that */}}
        {{ if eq $addrLen 1 }}
                {{ with $address := index $value.Addresses 0 }}
                        # {{$value.Name}}
                        server {{ $network.IP }}:{{ $address.Port }};
                {{ end }}

        {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var */}}
        {{ else if $value.Env.VIRTUAL_PORT }}
                {{ range $i, $address := $value.Addresses }}
                        {{ if eq $address.Port $value.Env.VIRTUAL_PORT }}
                        # {{$value.Name}}
                        server {{ $network.IP }}:{{ $address.Port }};
                        {{ end }}
                {{ end }}

        {{/* Else default to standard web port 80 */}}
        {{ else }}
                {{ range $i, $address := $value.Addresses }}
                        {{ if eq $address.Port "80" }}
                        # {{$value.Name}}
                        server {{ $network.IP }}:{{ $address.Port }};
                        {{ end }}
                {{ end }}
        {{ end }}
{{ end }}
}

server {
  listen 443 ssl http2;
  server_name {{ $host }};
  include vouch.conf;

  location / {
    proxy_pass http://{{ trim $host }};
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    # HTTP 1.1 support
    proxy_http_version 1.1;
    proxy_set_header Connection "";
  }
}
{{ end }}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions