Skip to content

Commit 1237a3b

Browse files
committed
Drop pkg/util/ipcmd, port to vishvananda/netlink (mostly)
1 parent d8cb876 commit 1237a3b

File tree

5 files changed

+61
-397
lines changed

5 files changed

+61
-397
lines changed

pkg/sdn/plugin/node.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
osclient "github.com/openshift/origin/pkg/client"
1919
"github.com/openshift/origin/pkg/sdn"
2020
osapi "github.com/openshift/origin/pkg/sdn/apis/network"
21-
"github.com/openshift/origin/pkg/util/ipcmd"
2221
"github.com/openshift/origin/pkg/util/netutils"
2322
"github.com/openshift/origin/pkg/util/ovs"
2423

@@ -39,6 +38,8 @@ import (
3938
knetwork "k8s.io/kubernetes/pkg/kubelet/network"
4039
ktypes "k8s.io/kubernetes/pkg/kubelet/types"
4140
kexec "k8s.io/kubernetes/pkg/util/exec"
41+
42+
"github.com/vishvananda/netlink"
4243
)
4344

4445
const (
@@ -203,13 +204,12 @@ func NewNodePlugin(c *OsdnNodeConfig) (sdn.NodeInterface, error) {
203204
// Detect whether we are upgrading from a pre-CNI openshift and clean up
204205
// interfaces and iptables rules that are no longer required
205206
func (node *OsdnNode) dockerPreCNICleanup() error {
206-
exec := kexec.New()
207-
itx := ipcmd.NewTransaction(exec, "lbr0")
208-
itx.SetLink("down")
209-
if err := itx.EndTransaction(); err != nil {
207+
l, err := netlink.LinkByName("lbr0")
208+
if err != nil {
210209
// no cleanup required
211210
return nil
212211
}
212+
_ = netlink.LinkSetDown(l)
213213

214214
node.clearLbr0IptablesRule = true
215215

@@ -226,10 +226,10 @@ func (node *OsdnNode) dockerPreCNICleanup() error {
226226

227227
// Delete pre-CNI interfaces
228228
for _, intf := range []string{"lbr0", "vovsbr", "vlinuxbr"} {
229-
itx := ipcmd.NewTransaction(exec, intf)
230-
itx.DeleteLink()
231-
itx.IgnoreError()
232-
itx.EndTransaction()
229+
l, err = netlink.LinkByName(intf)
230+
if err != nil {
231+
_ = netlink.LinkDel(l)
232+
}
233233
}
234234

235235
// Wait until docker has restarted since kubelet will exit if docker isn't running

pkg/sdn/plugin/pod.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"encoding/json"
55
"fmt"
66
"net"
7+
"os/exec"
78
"strconv"
89
"sync"
910
"syscall"
1011

1112
sdnapi "github.com/openshift/origin/pkg/sdn/apis/network"
1213
"github.com/openshift/origin/pkg/sdn/plugin/cniserver"
13-
"github.com/openshift/origin/pkg/util/ipcmd"
1414
"github.com/openshift/origin/pkg/util/netutils"
1515

1616
"github.com/golang/glog"
@@ -24,7 +24,6 @@ import (
2424
knetwork "k8s.io/kubernetes/pkg/kubelet/network"
2525
kubehostport "k8s.io/kubernetes/pkg/kubelet/network/hostport"
2626
kbandwidth "k8s.io/kubernetes/pkg/util/bandwidth"
27-
kexec "k8s.io/kubernetes/pkg/util/exec"
2827

2928
"github.com/containernetworking/cni/pkg/invoke"
3029
"github.com/containernetworking/cni/pkg/ip"
@@ -454,12 +453,10 @@ func setupPodBandwidth(ovs *ovsController, pod *kapi.Pod, hostVeth string) error
454453
if ingressVal != nil {
455454
ingressBPS = ingressVal.Value()
456455

457-
// FIXME: doesn't seem possible to do this with the netlink library?
458-
itx := ipcmd.NewTransaction(kexec.New(), hostVeth)
459-
itx.SetLink("qlen", "1000")
460-
err = itx.EndTransaction()
456+
// FIXME: rebase netlink after https://github.com/vishvananda/netlink/pull/257 merges
457+
out, err := exec.Command("ip", "link", "set", hostVeth, "qlen", "1000").CombinedOutput()
461458
if err != nil {
462-
return err
459+
return fmt.Errorf("failed to set host veth txqlen: %s", out)
463460
}
464461
}
465462
if egressVal != nil {

pkg/sdn/plugin/sdn_controller.go

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package plugin
33
import (
44
"fmt"
55
"net"
6-
"strings"
6+
"syscall"
77
"time"
88

99
"github.com/golang/glog"
1010

1111
osapi "github.com/openshift/origin/pkg/sdn/apis/network"
12-
"github.com/openshift/origin/pkg/util/ipcmd"
1312
"github.com/openshift/origin/pkg/util/netutils"
1413

1514
kapierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -20,6 +19,8 @@ import (
2019
kexec "k8s.io/kubernetes/pkg/util/exec"
2120
"k8s.io/kubernetes/pkg/util/iptables"
2221
"k8s.io/kubernetes/pkg/util/sysctl"
22+
23+
"github.com/vishvananda/netlink"
2324
)
2425

2526
func (plugin *OsdnNode) getLocalSubnet() (string, error) {
@@ -61,16 +62,18 @@ func (plugin *OsdnNode) getLocalSubnet() (string, error) {
6162
func (plugin *OsdnNode) alreadySetUp(localSubnetGatewayCIDR, clusterNetworkCIDR string) bool {
6263
var found bool
6364

64-
exec := kexec.New()
65-
itx := ipcmd.NewTransaction(exec, Tun0)
66-
addrs, err := itx.GetAddresses()
67-
itx.EndTransaction()
65+
l, err := netlink.LinkByName(Tun0)
66+
if err != nil {
67+
return false
68+
}
69+
70+
addrs, err := netlink.AddrList(l, syscall.AF_INET)
6871
if err != nil {
6972
return false
7073
}
7174
found = false
7275
for _, addr := range addrs {
73-
if strings.Contains(addr, localSubnetGatewayCIDR) {
76+
if addr.IPNet.String() == localSubnetGatewayCIDR {
7477
found = true
7578
break
7679
}
@@ -79,15 +82,13 @@ func (plugin *OsdnNode) alreadySetUp(localSubnetGatewayCIDR, clusterNetworkCIDR
7982
return false
8083
}
8184

82-
itx = ipcmd.NewTransaction(exec, Tun0)
83-
routes, err := itx.GetRoutes()
84-
itx.EndTransaction()
85+
routes, err := netlink.RouteList(l, syscall.AF_INET)
8586
if err != nil {
8687
return false
8788
}
8889
found = false
8990
for _, route := range routes {
90-
if strings.Contains(route, clusterNetworkCIDR+" ") {
91+
if route.Dst != nil && route.Dst.String() == clusterNetworkCIDR {
9192
found = true
9293
break
9394
}
@@ -110,15 +111,17 @@ func deleteLocalSubnetRoute(device, localSubnetCIDR string) {
110111
Steps: 6,
111112
}
112113
err := utilwait.ExponentialBackoff(backoff, func() (bool, error) {
113-
itx := ipcmd.NewTransaction(kexec.New(), device)
114-
routes, err := itx.GetRoutes()
114+
l, err := netlink.LinkByName(device)
115+
if err != nil {
116+
return false, fmt.Errorf("could not get interface %s: %v", device, err)
117+
}
118+
routes, err := netlink.RouteList(l, syscall.AF_INET)
115119
if err != nil {
116120
return false, fmt.Errorf("could not get routes: %v", err)
117121
}
118122
for _, route := range routes {
119-
if strings.Contains(route, localSubnetCIDR) {
120-
itx.DeleteRoute(localSubnetCIDR)
121-
err = itx.EndTransaction()
123+
if route.Dst != nil && route.Dst.String() == localSubnetCIDR {
124+
err = netlink.RouteDel(&route)
122125
if err != nil {
123126
return false, fmt.Errorf("could not delete route: %v", err)
124127
}
@@ -166,14 +169,35 @@ func (plugin *OsdnNode) SetupSDN() (bool, error) {
166169
return false, err
167170
}
168171

169-
itx := ipcmd.NewTransaction(exec, Tun0)
170-
itx.AddAddress(gwCIDR)
171-
defer deleteLocalSubnetRoute(Tun0, localSubnetCIDR)
172-
itx.SetLink("mtu", fmt.Sprint(plugin.mtu))
173-
itx.SetLink("up")
174-
itx.AddRoute(clusterNetworkCIDR, "proto", "kernel", "scope", "link")
175-
itx.AddRoute(serviceNetworkCIDR)
176-
err = itx.EndTransaction()
172+
l, err := netlink.LinkByName(Tun0)
173+
if err == nil {
174+
gwIP, _ := netlink.ParseIPNet(gwCIDR)
175+
err = netlink.AddrAdd(l, &netlink.Addr{IPNet: gwIP})
176+
if err == nil {
177+
defer deleteLocalSubnetRoute(Tun0, localSubnetCIDR)
178+
}
179+
}
180+
if err == nil {
181+
err = netlink.LinkSetMTU(l, int(plugin.mtu))
182+
}
183+
if err == nil {
184+
err = netlink.LinkSetUp(l)
185+
}
186+
if err == nil {
187+
route := &netlink.Route{
188+
LinkIndex: l.Attrs().Index,
189+
Scope: netlink.SCOPE_LINK,
190+
Dst: plugin.networkInfo.ClusterNetwork,
191+
}
192+
err = netlink.RouteAdd(route)
193+
}
194+
if err == nil {
195+
route := &netlink.Route{
196+
LinkIndex: l.Attrs().Index,
197+
Dst: plugin.networkInfo.ServiceNetwork,
198+
}
199+
err = netlink.RouteAdd(route)
200+
}
177201
if err != nil {
178202
return false, err
179203
}

pkg/util/ipcmd/ipcmd.go

Lines changed: 0 additions & 150 deletions
This file was deleted.

0 commit comments

Comments
 (0)