/
container.go
111 lines (91 loc) · 2.45 KB
/
container.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package models
import (
"fmt"
"github.com/containers/libpod/libpod"
)
type Container struct {
// the container ID
Id string `json:"id"`
// the container name
Name string `json:"name"`
// true if it is an pod infra container
IsInfra bool `json:"is_infra"`
// true if belongs to a pod, false else
IsInPod bool `json:"is_in_pod"`
// ID of the pod it belongs to. "" if standalone
PodId string `json:"pod_id"`
// the container IP address in the podman network
IpAddress string `json:"ip_address"`
// the port exposed by the container
// set manually by the user
ExposedPort int `json:"exposed_port"`
// the container status
Status ContainerStatus `json:"state"`
}
func NewContainer(container *libpod.Container) (*Container, error) {
isInPod := container.PodID() != ""
isInfra := container.IsInfra()
state, err := container.ContainerState()
if err != nil {
return nil, err
}
statusStr := state.State.String()
status := NewContainerStatus(statusStr)
var ip string
// checking pod context
// able to get IP only if out of a pod, or is an infra
if !isInPod || (isInPod && isInfra) {
// checking status
// able to get IP only on running containers
if status == Running {
ips, err := container.IPs()
if err != nil {
return nil, err
}
if len(ips) == 0 {
// no IP detected for this container
// container might be running rootless or in incompatible state
return nil, fmt.Errorf("no IP detected for container with name %s", container.Name())
}
ip = ips[0].IP.String()
}
}
return &Container{
Id: container.ID(),
Name: container.Name(),
IsInfra: isInfra,
IsInPod: isInPod,
PodId: container.PodID(),
IpAddress: ip,
ExposedPort: 0,
Status: status,
}, nil
}
func (c Container) String() string {
return fmt.Sprintf("%s: %s:%d", c.Name, c.IpAddress, c.ExposedPort)
}
func NewContainerFromRow(scan func(dest ...interface{}) error) (Container, error) {
var id string
var name string
var isInfra int
var isInPod int
var podId string
var ipAddress string
var exposedPort int
var status string
err := scan(&id, &name, &isInfra, &isInPod, &podId,
&ipAddress, &exposedPort, &status)
if err != nil {
return Container{}, err
}
return Container{
Id: id,
Name: name,
IsInfra: isInfra == 1,
IsInPod: isInPod == 1,
PodId: podId,
IpAddress: ipAddress,
ExposedPort: exposedPort,
Status: NewContainerStatus(status),
}, nil
}