-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
60 lines (56 loc) · 1.33 KB
/
state.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
package connstate
import (
"context"
"fmt"
"syscall"
"github.com/vishvananda/netns"
)
func (t *Tracker) GetConnectionState(container Container) (*ConnectionState, error) {
state := ConnectionState{
Container: container,
}
err := t.executeInContainerNetworkNamespace(container, func(nsHandler netns.NsHandle) error {
state.NetNSID = nsHandler.UniqueId()
for _, family := range t.families {
var receiver *TCPStates
switch family {
case syscall.AF_INET:
receiver = &state.IPv4
case syscall.AF_INET6:
receiver = &state.IPv6
default:
return fmt.Errorf("unsupported family %x", family)
}
result, err := diagTCPInfo(family)
if err != nil {
return err
}
*receiver = result
}
return nil
})
if err != nil {
return nil, err
}
return &state, nil
}
func (t *Tracker) ListAllConnectionState(ctx context.Context, filter ContainerFilter) ([]ConnectionState, error) {
containers, err := t.driver.ListContainer(ctx)
if err != nil {
return nil, err
}
var list []ConnectionState
for _, container := range containers {
if filter != nil && !filter(container) {
// bypass
continue
}
state, err := t.GetConnectionState(container)
if err != nil {
fmt.Printf("Failed to get container(%s) state: %s\n", container.Hostname, err)
continue
}
list = append(list, *state)
}
return list, nil
}