forked from lucaslorentz/caddy-docker-proxy
/
containers.go
33 lines (25 loc) · 967 Bytes
/
containers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package generator
import (
"bytes"
"fmt"
"github.com/docker/docker/api/types"
"github.com/Ranger-X/caddy-docker-proxy/plugin/v2/caddyfile"
)
func (g *CaddyfileGenerator) getContainerCaddyfile(container *types.Container, logsBuffer *bytes.Buffer) (*caddyfile.Container, error) {
caddyLabels := g.filterLabels(container.Labels)
return labelsToCaddyfile(caddyLabels, container, func() ([]string, error) {
return g.getContainerIPAddresses(container, logsBuffer, true)
})
}
func (g *CaddyfileGenerator) getContainerIPAddresses(container *types.Container, logsBuffer *bytes.Buffer, ingress bool) ([]string, error) {
ips := []string{}
for _, network := range container.NetworkSettings.Networks {
if !ingress || g.ingressNetworks[network.NetworkID] {
ips = append(ips, network.IPAddress)
}
}
if len(ips) == 0 {
logsBuffer.WriteString(fmt.Sprintf("[WARNING] Container %v and caddy are not in same network\n", container.ID))
}
return ips, nil
}