-
Notifications
You must be signed in to change notification settings - Fork 70
/
manager.go
198 lines (162 loc) · 5.71 KB
/
manager.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
194
195
196
197
198
package manager
import (
"errors"
"fmt"
"net"
"path/filepath"
"code.cloudfoundry.org/garden"
"code.cloudfoundry.org/lager"
"github.com/containernetworking/cni/pkg/types"
)
//go:generate counterfeiter -o ../fakes/cniController.go --fake-name CNIController . cniController
type cniController interface {
Up(namespacePath, handle string, properties map[string]string) (*types.Result, error)
Down(namespacePath, handle string) error
}
//go:generate counterfeiter -o ../fakes/mounter.go --fake-name Mounter . mounter
type mounter interface {
IdempotentlyMount(source, target string) error
RemoveMount(target string) error
}
//go:generate counterfeiter -o ../fakes/portAllocator.go --fake-name PortAllocator . portAllocator
type portAllocator interface {
AllocatePort(handle string, port int) (int, error)
ReleaseAllPorts(handle string) error
}
//go:generate counterfeiter -o ../fakes/netin_provider.go --fake-name NetInProvider . netInProvider
type netInProvider interface {
Initialize(containerHandle string) error
Cleanup(containerHandle string) error
AddRule(containerHandle string, hostPort, containerPort int, hostIP, containerIP string) error
}
//go:generate counterfeiter -o ../fakes/netout_provider.go --fake-name NetOutProvider . netOutProvider
type netOutProvider interface {
Initialize(logger lager.Logger, containerHandle string, containerIP net.IP, overlayNetwork string) error
Cleanup(containerHandle string) error
InsertRule(containerHandle string, rule garden.NetOutRule, containerIP string) error
BulkInsertRules(containerHandle string, rules []garden.NetOutRule, containerIP string) error
}
type Manager struct {
Logger lager.Logger
CNIController cniController
Mounter mounter
BindMountRoot string
OverlayNetwork string
PortAllocator portAllocator
NetInProvider netInProvider
NetOutProvider netOutProvider
}
type UpInputs struct {
Pid int
Properties map[string]string
}
type UpOutputs struct {
Properties struct {
ContainerIP string `json:"garden.network.container-ip"`
DeprecatedHostIP string `json:"garden.network.host-ip"`
} `json:"properties"`
}
func (m *Manager) Up(containerHandle string, inputs UpInputs) (*UpOutputs, error) {
if inputs.Pid == 0 {
return nil, errors.New("up missing pid")
}
if containerHandle == "" {
return nil, errors.New("up missing container handle")
}
procNsPath := fmt.Sprintf("/proc/%d/ns/net", inputs.Pid)
bindMountPath := filepath.Join(m.BindMountRoot, containerHandle)
err := m.Mounter.IdempotentlyMount(procNsPath, bindMountPath)
if err != nil {
return nil, fmt.Errorf("failed mounting %s to %s: %s", procNsPath, bindMountPath, err)
}
result, err := m.CNIController.Up(bindMountPath, containerHandle, inputs.Properties)
if err != nil {
return nil, fmt.Errorf("cni up failed: %s", err)
}
if result == nil {
return nil, errors.New("cni up failed: no ip allocated")
}
containerIP := result.IP4.IP.IP
if err := m.NetOutProvider.Initialize(m.Logger, containerHandle, containerIP, m.OverlayNetwork); err != nil {
return nil, fmt.Errorf("initialize net out: %s", err)
}
err = m.NetInProvider.Initialize(containerHandle)
if err != nil {
return nil, fmt.Errorf("initialize iptables for netin: %s", err)
}
outputs := UpOutputs{}
outputs.Properties.ContainerIP = containerIP.String()
outputs.Properties.DeprecatedHostIP = "255.255.255.255"
return &outputs, nil
}
func (m *Manager) Down(containerHandle string) error {
if containerHandle == "" {
return errors.New("down missing container handle")
}
bindMountPath := filepath.Join(m.BindMountRoot, containerHandle)
err := m.CNIController.Down(bindMountPath, containerHandle)
if err != nil {
return fmt.Errorf("cni down: %s", err)
}
if err = m.Mounter.RemoveMount(bindMountPath); err != nil {
m.Logger.Error("removing mount", err, lager.Data{"bind mount path": bindMountPath})
}
if err = m.NetOutProvider.Cleanup(containerHandle); err != nil {
m.Logger.Error("net out cleanup", err)
}
if err = m.NetInProvider.Cleanup(containerHandle); err != nil {
m.Logger.Error("net in cleanup", err)
}
if err = m.PortAllocator.ReleaseAllPorts(containerHandle); err != nil {
m.Logger.Error("releasing ports", err)
}
return nil
}
type NetOutInputs struct {
ContainerIP string `json:"container_ip"`
NetOutRule garden.NetOutRule `json:"netout_rule"`
}
func (m *Manager) NetOut(containerHandle string, inputs NetOutInputs) error {
return m.NetOutProvider.InsertRule(containerHandle, inputs.NetOutRule, inputs.ContainerIP)
}
type NetInInputs struct {
HostIP string
HostPort int
ContainerIP string
ContainerPort int
}
type NetInOutputs struct {
HostPort int `json:"host_port"`
ContainerPort int `json:"container_port"`
}
func (m *Manager) NetIn(containerHandle string, inputs NetInInputs) (*NetInOutputs, error) {
hostPort, err := m.PortAllocator.AllocatePort(containerHandle, inputs.HostPort)
if err != nil {
return nil, fmt.Errorf("allocate port: %s", err)
}
containerPort := inputs.ContainerPort
if containerPort == 0 {
containerPort = hostPort
}
containerIP := inputs.ContainerIP
hostIP := inputs.HostIP
err = m.NetInProvider.AddRule(containerHandle, hostPort, containerPort, hostIP, containerIP)
if err != nil {
return nil, fmt.Errorf("add rule: %s", err)
}
return &NetInOutputs{
HostPort: hostPort,
ContainerPort: containerPort,
}, nil
}
type BulkNetOutInputs struct {
ContainerIP string `json:"container_ip"`
NetOutRules []garden.NetOutRule `json:"netout_rules"`
}
func (m *Manager) BulkNetOut(containerHandle string, inputs BulkNetOutInputs) error {
err := m.NetOutProvider.BulkInsertRules(containerHandle, inputs.NetOutRules, inputs.ContainerIP)
if err != nil {
return fmt.Errorf("insert rule: %s", err)
}
return nil
}