forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod.go
287 lines (255 loc) · 8.07 KB
/
pod.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package plugin
import (
"encoding/json"
"fmt"
"net"
"sort"
"sync"
"github.com/openshift/origin/pkg/sdn/plugin/cniserver"
"github.com/openshift/origin/pkg/util/netutils"
"github.com/openshift/origin/pkg/util/ovs"
"github.com/golang/glog"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
knetwork "k8s.io/kubernetes/pkg/kubelet/network"
kubehostport "k8s.io/kubernetes/pkg/kubelet/network/hostport"
cnitypes "github.com/containernetworking/cni/pkg/types"
)
type podHandler interface {
setup(req *cniserver.PodRequest) (*cnitypes.Result, *runningPod, error)
update(req *cniserver.PodRequest) (uint32, error)
teardown(req *cniserver.PodRequest) error
}
type runningPod struct {
activePod *kubehostport.ActivePod
vnid uint32
ofport int
}
type podManager struct {
// Common stuff used for both live and testing code
podHandler podHandler
cniServer *cniserver.CNIServer
// Request queue for pod operations incoming from the CNIServer
requests chan (*cniserver.PodRequest)
// Tracks pod :: IP address for hostport handling
runningPods map[string]*runningPod
runningPodsLock sync.Mutex
// Live pod setup/teardown stuff not used in testing code
kClient *kclientset.Clientset
policy osdnPolicy
ipamConfig []byte
mtu uint32
hostportHandler kubehostport.HostportHandler
host knetwork.Host
ovs *ovs.Interface
}
// Creates a new live podManager; used by node code
func newPodManager(host knetwork.Host, localSubnetCIDR string, netInfo *NetworkInfo, kClient *kclientset.Clientset, policy osdnPolicy, mtu uint32, ovs *ovs.Interface) (*podManager, error) {
pm := newDefaultPodManager(host)
pm.kClient = kClient
pm.policy = policy
pm.mtu = mtu
pm.hostportHandler = kubehostport.NewHostportHandler()
pm.podHandler = pm
pm.ovs = ovs
var err error
pm.ipamConfig, err = getIPAMConfig(netInfo.ClusterNetwork, localSubnetCIDR)
if err != nil {
return nil, err
}
return pm, nil
}
// Creates a new basic podManager; used by testcases
func newDefaultPodManager(host knetwork.Host) *podManager {
return &podManager{
runningPods: make(map[string]*runningPod),
requests: make(chan *cniserver.PodRequest, 20),
host: host,
}
}
// Generates a CNI IPAM config from a given node cluster and local subnet that
// CNI 'host-local' IPAM plugin will use to create an IP address lease for the
// container
func getIPAMConfig(clusterNetwork *net.IPNet, localSubnet string) ([]byte, error) {
nodeNet, err := cnitypes.ParseCIDR(localSubnet)
if err != nil {
return nil, fmt.Errorf("error parsing node network '%s': %v", localSubnet, err)
}
type hostLocalIPAM struct {
Type string `json:"type"`
Subnet cnitypes.IPNet `json:"subnet"`
Routes []cnitypes.Route `json:"routes"`
}
type cniNetworkConfig struct {
Name string `json:"name"`
Type string `json:"type"`
IPAM *hostLocalIPAM `json:"ipam"`
}
_, mcnet, _ := net.ParseCIDR("224.0.0.0/4")
return json.Marshal(&cniNetworkConfig{
Name: "openshift-sdn",
Type: "openshift-sdn",
IPAM: &hostLocalIPAM{
Type: "host-local",
Subnet: cnitypes.IPNet{
IP: nodeNet.IP,
Mask: nodeNet.Mask,
},
Routes: []cnitypes.Route{
{
// Default route
Dst: net.IPNet{
IP: net.IPv4zero,
Mask: net.IPMask(net.IPv4zero),
},
GW: netutils.GenerateDefaultGateway(nodeNet),
},
{
// Cluster network
Dst: *clusterNetwork,
},
{
// Multicast
Dst: *mcnet,
},
},
},
})
}
// Start the CNI server and start processing requests from it
func (m *podManager) Start(socketPath string) error {
go m.processCNIRequests()
m.cniServer = cniserver.NewCNIServer(socketPath)
return m.cniServer.Start(m.handleCNIRequest)
}
// Returns a key for use with the runningPods map
func getPodKey(request *cniserver.PodRequest) string {
return fmt.Sprintf("%s/%s", request.PodNamespace, request.PodName)
}
func (m *podManager) getPod(request *cniserver.PodRequest) *kubehostport.ActivePod {
if pod := m.runningPods[getPodKey(request)]; pod != nil {
return pod.activePod
}
return nil
}
// Return a list of Kubernetes RunningPod objects for hostport operations
func (m *podManager) getRunningPods() []*kubehostport.ActivePod {
pods := make([]*kubehostport.ActivePod, 0)
for _, runningPod := range m.runningPods {
pods = append(pods, runningPod.activePod)
}
return pods
}
// Add a request to the podManager CNI request queue
func (m *podManager) addRequest(request *cniserver.PodRequest) {
m.requests <- request
}
// Wait for and return the result of a pod request
func (m *podManager) waitRequest(request *cniserver.PodRequest) *cniserver.PodResult {
return <-request.Result
}
// Enqueue incoming pod requests from the CNI server, wait on the result,
// and return that result to the CNI client
func (m *podManager) handleCNIRequest(request *cniserver.PodRequest) ([]byte, error) {
glog.V(5).Infof("Dispatching pod network request %v", request)
m.addRequest(request)
result := m.waitRequest(request)
glog.V(5).Infof("Returning pod network request %v, result %s err %v", request, string(result.Response), result.Err)
return result.Response, result.Err
}
func localMulticastOutputs(runningPods map[string]*runningPod, vnid uint32) string {
var ofports []int
for _, pod := range runningPods {
if pod.vnid == vnid {
ofports = append(ofports, pod.ofport)
}
}
if len(ofports) == 0 {
return ""
}
sort.Ints(ofports)
outputs := ""
for _, ofport := range ofports {
if len(outputs) > 0 {
outputs += ","
}
outputs += fmt.Sprintf("output:%d", ofport)
}
return outputs
}
func (m *podManager) updateLocalMulticastRulesWithLock(vnid uint32) {
var outputs string
otx := m.ovs.NewTransaction()
if m.policy.GetMulticastEnabled(vnid) {
outputs = localMulticastOutputs(m.runningPods, vnid)
otx.AddFlow("table=110, reg0=%d, actions=goto_table:111", vnid)
} else {
otx.DeleteFlows("table=110, reg0=%d", vnid)
}
if len(outputs) > 0 {
otx.AddFlow("table=120, priority=100, reg0=%d, actions=%s", vnid, outputs)
} else {
otx.DeleteFlows("table=120, reg0=%d", vnid)
}
if err := otx.EndTransaction(); err != nil {
glog.Errorf("Error updating OVS multicast flows for VNID %d: %v", vnid, err)
}
}
// Update multicast OVS rules for the given vnid
func (m *podManager) UpdateLocalMulticastRules(vnid uint32) {
m.runningPodsLock.Lock()
defer m.runningPodsLock.Unlock()
m.updateLocalMulticastRulesWithLock(vnid)
}
// Process all CNI requests from the request queue serially. Our OVS interaction
// and scripts currently cannot run in parallel, and doing so greatly complicates
// setup/teardown logic
func (m *podManager) processCNIRequests() {
for request := range m.requests {
glog.V(5).Infof("Processing pod network request %v", request)
result := m.processRequest(request)
glog.V(5).Infof("Processed pod network request %v, result %s err %v", request, string(result.Response), result.Err)
request.Result <- result
}
panic("stopped processing CNI pod requests!")
}
func (m *podManager) processRequest(request *cniserver.PodRequest) *cniserver.PodResult {
m.runningPodsLock.Lock()
defer m.runningPodsLock.Unlock()
pk := getPodKey(request)
result := &cniserver.PodResult{}
switch request.Command {
case cniserver.CNI_ADD:
ipamResult, runningPod, err := m.podHandler.setup(request)
if ipamResult != nil {
result.Response, err = json.Marshal(ipamResult)
if result.Err == nil {
m.runningPods[pk] = runningPod
if m.ovs != nil {
m.updateLocalMulticastRulesWithLock(runningPod.vnid)
}
}
}
if err != nil {
result.Err = err
}
case cniserver.CNI_UPDATE:
vnid, err := m.podHandler.update(request)
if err == nil {
if runningPod, exists := m.runningPods[pk]; exists {
runningPod.vnid = vnid
}
}
result.Err = err
case cniserver.CNI_DEL:
if runningPod, exists := m.runningPods[pk]; exists {
delete(m.runningPods, pk)
if m.ovs != nil {
m.updateLocalMulticastRulesWithLock(runningPod.vnid)
}
}
result.Err = m.podHandler.teardown(request)
default:
result.Err = fmt.Errorf("unhandled CNI request %v", request.Command)
}
return result
}