-
Notifications
You must be signed in to change notification settings - Fork 5
/
registrar.go
65 lines (57 loc) · 1.57 KB
/
registrar.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
)
type Registrar struct {
DockerClient *DockerClient
SRegistry *ServiceRegistry
}
func (r *Registrar) Init() error {
cList, err := r.DockerClient.ContainerList(context.Background(), types.ContainerListOptions{
Filters: filters.NewArgs(
filters.Arg("ancestor", HelloServiceImageName),
filters.Arg("status", ContainerRunningState),
),
})
if err != nil {
return err
}
for _, c := range cList {
r.SRegistry.Add(c.ID, findContainerAddress(c.Ports[0].PublicPort))
}
return nil
}
func (r *Registrar) Observe() {
msgCh, errCh := r.DockerClient.Events(context.Background(), types.EventsOptions{
Filters: filters.NewArgs(
filters.Arg("type", "container"),
filters.Arg("image", HelloServiceImageName),
filters.Arg("event", "start"),
filters.Arg("event", "kill"),
),
})
for {
select {
case c := <-msgCh:
fmt.Printf("State of the container %d is %s\n", c.ID, c.Status)
if c.Status == ContainerKillState {
r.SRegistry.RemoveByContainerID(c.ID)
} else if c.Status == ContainerStartState {
port, err := r.DockerClient.GetContainerPort(context.Background(), c.ID)
if err != nil {
fmt.Printf("err getting newly started container port %s\n", err.Error())
continue
}
r.SRegistry.Add(c.ID, findContainerAddress(port))
}
case err := <-errCh:
fmt.Println("Error Docker Event Chan", err.Error())
}
}
}
func findContainerAddress(cPort uint16) string {
return fmt.Sprintf("http://localhost:%d", cPort)
}