This repository was archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdocker_resolver.go
More file actions
71 lines (59 loc) · 1.67 KB
/
docker_resolver.go
File metadata and controls
71 lines (59 loc) · 1.67 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package emissary
import (
"context"
"fmt"
"net"
)
// DockerResolver implements the Resolver interface.
type DockerResolver struct {
Client DockerClient
}
// DockerLookupPort is used to lookup the Ip and Port from the containers metadata.
var DockerServicePort int = 3000
// DockerLookupLabel is used to filter containers.
var DockerServiceLabel string = "emissary.service_name"
// Lookup for the service in docker from the docker api. Lookup with look for the label DockerServiceLabel
// in the container metadata.
//
// Currently no tag will be set in the resulted endpoints.
func (d *DockerResolver) Lookup(ctx context.Context, service string) ([]Endpoint, error) {
containers, err := d.Client.listContainers()
if err != nil {
return nil, err
}
var endpoints []Endpoint
for _, container := range containers {
if svc, ok := container.Labels[DockerServiceLabel]; ok && svc == service {
var containerIP string
for _, setting := range container.NetworkSettings.Networks {
if setting.IPAddress != "" {
containerIP = setting.IPAddress
break
}
}
addr, err := parseAddr(containerIP, DockerServicePort)
if err != nil {
continue
}
//TODO: do we need to set some tags ?
endpoints = append(endpoints, Endpoint{
Addr: addr,
})
}
}
return endpoints, nil
}
func (d *DockerResolver) Healthy(ctx context.Context) bool {
state, err := d.Client.getStatus("State")
if err != nil {
return false
}
return state == "Healthy"
}
func parseAddr(hostIp string, port int) (*net.TCPAddr, error) {
ip := net.ParseIP(hostIp)
if ip == nil {
return nil, fmt.Errorf("invalid hostIp %s", hostIp)
}
return &net.TCPAddr{IP: ip, Port: port}, nil
}