Skip to content

Commit 7b45817

Browse files
committed
fixup! Port openshift-sdn-ovs script to go
1 parent 7060be9 commit 7b45817

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

pkg/sdn/plugin/pod.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ func (m *podManager) getRunningPods() []*kubehostport.RunningPod {
143143
return pods
144144
}
145145

146+
// Get the VNID of a namespace
147+
func (m *podManager) getVNID(namespace string) (uint32, error) {
148+
if m.multitenant {
149+
return m.vnids.GetVNID(namespace)
150+
} else {
151+
return 0, nil
152+
}
153+
}
154+
146155
// Add a request to the podManager CNI request queue
147156
func (m *podManager) addRequest(request *cniserver.PodRequest) {
148157
m.requests <- request

pkg/sdn/plugin/pod_linux.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
sdnapi "github.com/openshift/origin/pkg/sdn/api"
1515
"github.com/openshift/origin/pkg/sdn/plugin/cniserver"
16+
"github.com/openshift/origin/pkg/util/ovs"
1617

1718
"github.com/golang/glog"
1819

@@ -190,21 +191,12 @@ func (m *podManager) ipamDel(id string) error {
190191
return nil
191192
}
192193

193-
func (m *podManager) ensureOvsPort(hostVeth string) (int, error) {
194-
return m.ovs.AddPort(hostVeth, -1)
194+
func ensureOvsPort(ovsif *ovs.Interface, hostVeth string) (int, error) {
195+
return ovsif.AddPort(hostVeth, -1)
195196
}
196197

197-
func (m *podManager) setupPodFlows(ofport int, podIP, podMac, namespace string) error {
198-
vnid := uint32(0)
199-
if m.multitenant {
200-
var err error
201-
vnid, err = m.vnids.GetVNID(namespace)
202-
if err != nil {
203-
return err
204-
}
205-
}
206-
207-
otx := m.ovs.NewTransaction()
198+
func setupPodFlows(ovsif *ovs.Interface, ofport int, podIP, podMac string, vnid uint32) error {
199+
otx := ovsif.NewTransaction()
208200

209201
// ARP/IP traffic from container
210202
otx.AddFlow("table=2, priority=100, in_port=%d, arp, nw_src=%s, arp_sha=%s, actions=load:%d->NXM_NX_REG0[], goto_table:5", ofport, podIP, podMac, vnid)
@@ -224,7 +216,7 @@ func (m *podManager) setupPodFlows(ofport int, podIP, podMac, namespace string)
224216
return otx.EndTransaction()
225217
}
226218

227-
func (m *podManager) setupPodBandwidth(pod *kapi.Pod, hostVeth string) error {
219+
func setupPodBandwidth(ovsif *ovs.Interface, pod *kapi.Pod, hostVeth string) error {
228220
podIngress, podEgress, err := kbandwidth.ExtractPodBandwidthResources(pod.Annotations)
229221
if err != nil {
230222
return fmt.Errorf("failed to parse pod bandwidth: %v", err)
@@ -243,17 +235,17 @@ func (m *podManager) setupPodBandwidth(pod *kapi.Pod, hostVeth string) error {
243235
}
244236

245237
if ovsEgress > 0 {
246-
qos, err := m.ovs.Create("qos", "type=linux-htb", fmt.Sprintf("other-config:max-rate=%d", ovsEgress))
238+
qos, err := ovsif.Create("qos", "type=linux-htb", fmt.Sprintf("other-config:max-rate=%d", ovsEgress))
247239
if err != nil {
248240
return err
249241
}
250-
err = m.ovs.Set("port", hostVeth, fmt.Sprintf("qos=%s", qos))
242+
err = ovsif.Set("port", hostVeth, fmt.Sprintf("qos=%s", qos))
251243
if err != nil {
252244
return err
253245
}
254246
}
255247
if ovsIngress > 0 {
256-
err := m.ovs.Set("interface", hostVeth, fmt.Sprintf("ingress_policing_rate=%d", ovsIngress))
248+
err := ovsif.Set("interface", hostVeth, fmt.Sprintf("ingress_policing_rate=%d", ovsIngress))
257249
if err != nil {
258250
return err
259251
}
@@ -262,25 +254,25 @@ func (m *podManager) setupPodBandwidth(pod *kapi.Pod, hostVeth string) error {
262254
return nil
263255
}
264256

265-
func (m *podManager) cleanupPodFlows(podIP string) error {
266-
otx := m.ovs.NewTransaction()
257+
func cleanupPodFlows(ovsif *ovs.Interface, podIP string) error {
258+
otx := ovsif.NewTransaction()
267259
otx.DeleteFlows("ip, nw_dst=%s", podIP)
268260
otx.DeleteFlows("ip, nw_src=%s", podIP)
269261
otx.DeleteFlows("arp, nw_dst=%s", podIP)
270262
otx.DeleteFlows("arp, nw_src=%s", podIP)
271263
return otx.EndTransaction()
272264
}
273265

274-
func (m *podManager) cleanupPodBandwidth(hostVeth string) error {
275-
qos, err := m.ovs.Get("port", hostVeth, "qos")
266+
func cleanupPodBandwidth(ovsif *ovs.Interface, hostVeth string) error {
267+
qos, err := ovsif.Get("port", hostVeth, "qos")
276268
if err != nil || qos == "[]" {
277269
return err
278270
}
279-
err = m.ovs.Clear("port", hostVeth, "qos")
271+
err = ovsif.Clear("port", hostVeth, "qos")
280272
if err != nil {
281273
return err
282274
}
283-
return m.ovs.Destroy("qos", qos)
275+
return ovsif.Destroy("qos", qos)
284276
}
285277

286278
func vnidToString(vnid uint32) string {
@@ -448,18 +440,23 @@ func (m *podManager) setup(req *cniserver.PodRequest) (*cnitypes.Result, *kubeho
448440
return nil, nil, err
449441
}
450442

443+
vnid, err := m.getVNID(req.PodNamespace)
444+
if err != nil {
445+
return nil, nil, err
446+
}
447+
451448
if err := maybeAddMacvlan(pod, req.Netns); err != nil {
452449
return nil, nil, err
453450
}
454451

455-
ofport, err := m.ensureOvsPort(hostVethName)
452+
ofport, err := ensureOvsPort(m.ovs, hostVethName)
456453
if err != nil {
457454
return nil, nil, err
458455
}
459-
if err := m.setupPodFlows(ofport, podIP.String(), contVethMac, req.PodNamespace); err != nil {
456+
if err := setupPodFlows(m.ovs, ofport, podIP.String(), contVethMac, vnid); err != nil {
460457
return nil, nil, err
461458
}
462-
if err := m.setupPodBandwidth(pod, hostVethName); err != nil {
459+
if err := setupPodBandwidth(m.ovs, pod, hostVethName); err != nil {
463460
return nil, nil, err
464461
}
465462

@@ -497,21 +494,25 @@ func (m *podManager) update(req *cniserver.PodRequest) error {
497494
if err != nil {
498495
return err
499496
}
497+
vnid, err := m.getVNID(req.PodNamespace)
498+
if err != nil {
499+
return err
500+
}
500501

501-
ofport, err := m.ensureOvsPort(hostVethName)
502+
ofport, err := ensureOvsPort(m.ovs, hostVethName)
502503
if err != nil {
503504
return err
504505
}
505-
if err := m.cleanupPodFlows(podIP); err != nil {
506+
if err := cleanupPodFlows(m.ovs, podIP); err != nil {
506507
return err
507508
}
508-
if err := m.setupPodFlows(ofport, podIP, contVethMac, req.PodNamespace); err != nil {
509+
if err := setupPodFlows(m.ovs, ofport, podIP, contVethMac, vnid); err != nil {
509510
return err
510511
}
511-
if err := m.cleanupPodBandwidth(hostVethName); err != nil {
512+
if err := cleanupPodBandwidth(m.ovs, hostVethName); err != nil {
512513
return err
513514
}
514-
if err := m.setupPodBandwidth(pod, hostVethName); err != nil {
515+
if err := setupPodBandwidth(m.ovs, pod, hostVethName); err != nil {
515516
return err
516517
}
517518

@@ -536,10 +537,10 @@ func (m *podManager) teardown(req *cniserver.PodRequest) error {
536537
return err
537538
}
538539

539-
if err := m.cleanupPodFlows(podIP); err != nil {
540+
if err := cleanupPodFlows(m.ovs, podIP); err != nil {
540541
errList = append(errList, err)
541542
}
542-
if err := m.cleanupPodBandwidth(hostVethName); err != nil {
543+
if err := cleanupPodBandwidth(m.ovs, hostVethName); err != nil {
543544
errList = append(errList, err)
544545
}
545546
if err := m.ovs.DeletePort(hostVethName); err != nil {

0 commit comments

Comments
 (0)