-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_utils.go
More file actions
43 lines (36 loc) · 1.03 KB
/
docker_utils.go
File metadata and controls
43 lines (36 loc) · 1.03 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
package main
import (
"context"
"regexp"
"github.com/docker/docker/api/types/container"
)
var (
routerLabelRegex = regexp.MustCompile(`^traefik\.http\.routers\.(.*)\.rule$`)
hostInValueRegex = regexp.MustCompile(`Host\(\(([^)]+)\)\)`)
)
// getHostnameFromLabel extracts the hostname from a container's labels.
func getHostnameFromLabel(c container.Summary) (string, bool) {
for k, v := range c.Labels {
if routerLabelRegex.MatchString(k) {
if m := hostInValueRegex.FindStringSubmatch(v); len(m) > 1 {
return m[1], true
}
return "", false
}
}
return "", false
}
// getHostnames uses a ContainerLister to get all hostnames from running containers.
func getHostnames(ctx context.Context, lister ContainerLister) ([]string, error) {
containers, err := lister.ContainerList(ctx, container.ListOptions{})
if err != nil {
return nil, err
}
result := make([]string, 0, len(containers))
for _, c := range containers {
if h, ok := getHostnameFromLabel(c); ok {
result = append(result, h)
}
}
return result, nil
}