This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
forked from cloudfoundry/guardian
-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.go
193 lines (152 loc) · 5.01 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package gardener
import (
"encoding/json"
"fmt"
"io"
"time"
"code.cloudfoundry.org/garden"
"code.cloudfoundry.org/lager"
)
type container struct {
logger lager.Logger
handle string
containerizer Containerizer
volumeCreator VolumeCreator
networker Networker
propertyManager PropertyManager
}
func (c *container) Handle() string {
return c.handle
}
func (c *container) Run(spec garden.ProcessSpec, io garden.ProcessIO) (garden.Process, error) {
return c.containerizer.Run(c.logger, c.handle, spec, io)
}
func (c *container) Attach(processID string, io garden.ProcessIO) (garden.Process, error) {
return c.containerizer.Attach(c.logger, c.handle, processID, io)
}
func (c *container) Stop(kill bool) error {
return c.containerizer.Stop(c.logger, c.handle, kill)
}
func (c *container) Info() (garden.ContainerInfo, error) {
log := c.logger.Session("info", lager.Data{"handle": c.handle})
log.Debug("starting")
defer log.Debug("finished")
containerIP, ok := c.propertyManager.Get(c.handle, ContainerIPKey)
if !ok {
return garden.ContainerInfo{}, fmt.Errorf("info: no property found: %s", ContainerIPKey)
}
hostIP, ok := c.propertyManager.Get(c.handle, BridgeIPKey)
if !ok {
return garden.ContainerInfo{}, fmt.Errorf("info: no property found: %s", BridgeIPKey)
}
externalIP, ok := c.propertyManager.Get(c.handle, ExternalIPKey)
if !ok {
return garden.ContainerInfo{}, fmt.Errorf("info: no property found: %s", ExternalIPKey)
}
actualContainerSpec, err := c.containerizer.Info(c.logger, c.handle)
if err != nil {
return garden.ContainerInfo{}, err
}
properties, err := c.propertyManager.All(c.handle)
if err != nil {
return garden.ContainerInfo{}, err
}
mappedPorts := []garden.PortMapping{}
mappedPortsCfg, _ := c.propertyManager.Get(c.handle, MappedPortsKey)
state := "active"
if actualContainerSpec.Stopped {
state = "stopped"
}
json.Unmarshal([]byte(mappedPortsCfg), &mappedPorts)
return garden.ContainerInfo{
State: state,
ContainerIP: containerIP,
HostIP: hostIP,
ExternalIP: externalIP,
ContainerPath: actualContainerSpec.BundlePath,
Events: actualContainerSpec.Events,
Properties: properties,
MappedPorts: mappedPorts,
}, nil
}
func (c *container) StreamIn(spec garden.StreamInSpec) error {
return c.containerizer.StreamIn(c.logger, c.handle, spec)
}
func (c *container) StreamOut(spec garden.StreamOutSpec) (io.ReadCloser, error) {
return c.containerizer.StreamOut(c.logger, c.handle, spec)
}
func (c *container) LimitBandwidth(limits garden.BandwidthLimits) error {
return nil
}
func (c *container) CurrentBandwidthLimits() (garden.BandwidthLimits, error) {
return garden.BandwidthLimits{}, nil
}
func (c *container) LimitCPU(limits garden.CPULimits) error {
return nil
}
func (c *container) CurrentCPULimits() (garden.CPULimits, error) {
info, err := c.containerizer.Info(c.logger, c.handle)
return info.Limits.CPU, err
}
func (c *container) LimitDisk(limits garden.DiskLimits) error {
return nil
}
func (c *container) CurrentDiskLimits() (garden.DiskLimits, error) {
return garden.DiskLimits{}, nil
}
func (c *container) LimitMemory(limits garden.MemoryLimits) error {
return nil
}
func (c *container) CurrentMemoryLimits() (garden.MemoryLimits, error) {
info, err := c.containerizer.Info(c.logger, c.handle)
return info.Limits.Memory, err
}
func (c *container) NetIn(hostPort, containerPort uint32) (uint32, uint32, error) {
return c.networker.NetIn(c.logger, c.handle, hostPort, containerPort)
}
func (c *container) NetOut(netOutRule garden.NetOutRule) error {
return c.networker.NetOut(c.logger, c.handle, netOutRule)
}
func (c *container) BulkNetOut(netOutRules []garden.NetOutRule) error {
return c.networker.BulkNetOut(c.logger, c.handle, netOutRules)
}
func (c *container) Metrics() (garden.Metrics, error) {
actualContainerMetrics, err := c.containerizer.Metrics(c.logger, c.handle)
if err != nil {
return garden.Metrics{}, err
}
actualContainerSpec, err := c.containerizer.Info(c.logger, c.handle)
if err != nil {
return garden.Metrics{}, err
}
diskMetrics, err := c.volumeCreator.Metrics(c.logger, c.handle, !actualContainerSpec.Privileged)
if err != nil {
return garden.Metrics{}, err
}
return garden.Metrics{
CPUStat: actualContainerMetrics.CPU,
MemoryStat: actualContainerMetrics.Memory,
DiskStat: diskMetrics,
}, nil
}
func (c *container) Properties() (garden.Properties, error) {
return c.propertyManager.All(c.handle)
}
func (c *container) Property(name string) (string, error) {
if prop, ok := c.propertyManager.Get(c.handle, name); ok {
return prop, nil
}
return "", fmt.Errorf("property does not exist: %s", name)
}
func (c *container) SetProperty(name string, value string) error {
c.propertyManager.Set(c.handle, name, value)
return nil
}
func (c *container) RemoveProperty(name string) error {
c.propertyManager.Remove(c.handle, name)
return nil
}
func (c *container) SetGraceTime(t time.Duration) error {
c.propertyManager.Set(c.handle, GraceTimeKey, fmt.Sprintf("%d", t))
return nil
}