-
Notifications
You must be signed in to change notification settings - Fork 168
/
containers.go
44 lines (33 loc) · 1.22 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
package generator
import (
"github.com/docker/docker/api/types"
"github.com/lucaslorentz/caddy-docker-proxy/v2/caddyfile"
"go.uber.org/zap"
)
func (g *CaddyfileGenerator) getContainerCaddyfile(container *types.Container, logger *zap.Logger) (*caddyfile.Container, error) {
caddyLabels := g.filterLabels(container.Labels)
return labelsToCaddyfile(caddyLabels, container, func() ([]string, error) {
return g.getContainerIPAddresses(container, logger, true)
})
}
func (g *CaddyfileGenerator) getContainerIPAddresses(container *types.Container, logger *zap.Logger, onlyIngressIps bool) ([]string, error) {
ips := []string{}
ingressNetworkFromLabel, overrideNetwork := container.Labels[IngressNetworkLabel]
for networkName, network := range container.NetworkSettings.Networks {
include := false
if !onlyIngressIps {
include = true
} else if overrideNetwork {
include = networkName == ingressNetworkFromLabel
} else {
include = g.ingressNetworks[network.NetworkID]
}
if include {
ips = append(ips, network.IPAddress)
}
}
if len(ips) == 0 {
logger.Warn("Container is not in same network as caddy", zap.String("container", container.ID), zap.String("container id", container.ID))
}
return ips, nil
}