Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
refactor: use netlink instead of exec'ing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Eldeib authored and alexeldeib committed Aug 6, 2019
1 parent 1e228d9 commit a9cb096
Show file tree
Hide file tree
Showing 232 changed files with 26,678 additions and 452 deletions.
7 changes: 4 additions & 3 deletions docs/cli/ignite/ignite_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ the flags for this command.

If the name flag (-n, --name) is not specified,
the VM is given a random name. Using the copy files
flag (-f, --copy-files), additional files can be added to
the VM during creation with the syntax /host/path:/vm/path.
flag (-f, --copy-files), additional files/directories
can be added to the VM during creation with the syntax
/host/path:/vm/path.

Example usage:
$ ignite create centos:7 \
Expand All @@ -34,7 +35,7 @@ ignite create <OCI image> [flags]

```
--config string Specify a path to a file with the API resources you want to pass
-f, --copy-files strings Copy files from the host to the created VM
-f, --copy-files strings Copy files/directories from the host to the created VM
--cpus uint VM vCPU count, 1 or even numbers between 1 and 32 (default 1)
-h, --help help for create
--kernel-args string Set the command line for the kernel (default "console=ttyS0 reboot=k panic=1 pci=off ip=dhcp")
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/ignite/ignite_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ignite run <OCI image> [flags]

```
--config string Specify a path to a file with the API resources you want to pass
-f, --copy-files strings Copy files from the host to the created VM
-f, --copy-files strings Copy files/directories from the host to the created VM
--cpus uint VM vCPU count, 1 or even numbers between 1 and 32 (default 1)
-d, --debug Debug mode, keep container after VM shutdown
-h, --help help for run
Expand Down
7 changes: 4 additions & 3 deletions docs/cli/ignite/ignite_vm_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ the flags for this command.

If the name flag (-n, --name) is not specified,
the VM is given a random name. Using the copy files
flag (-f, --copy-files), additional files can be added to
the VM during creation with the syntax /host/path:/vm/path.
flag (-f, --copy-files), additional files/directories
can be added to the VM during creation with the syntax
/host/path:/vm/path.

Example usage:
$ ignite create centos:7 \
Expand All @@ -34,7 +35,7 @@ ignite vm create <OCI image> [flags]

```
--config string Specify a path to a file with the API resources you want to pass
-f, --copy-files strings Copy files from the host to the created VM
-f, --copy-files strings Copy files/directories from the host to the created VM
--cpus uint VM vCPU count, 1 or even numbers between 1 and 32 (default 1)
-h, --help help for create
--kernel-args string Set the command line for the kernel (default "console=ttyS0 reboot=k panic=1 pci=off ip=dhcp")
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/ignite/ignite_vm_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ignite vm run <OCI image> [flags]

```
--config string Specify a path to a file with the API resources you want to pass
-f, --copy-files strings Copy files from the host to the created VM
-f, --copy-files strings Copy files/directories from the host to the created VM
--cpus uint VM vCPU count, 1 or even numbers between 1 and 32 (default 1)
-d, --debug Debug mode, keep container after VM shutdown
-h, --help help for run
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ require (
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/otiai10/copy v1.0.1
github.com/otiai10/curr v0.0.0-20190513014714-f5a3d24e5776 // indirect
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.0.0
github.com/rjeczalik/notify v0.9.2
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.3
github.com/vishvananda/netlink v1.0.0
github.com/vishvananda/netns v0.0.0-20190625233234-7109fa855b0f // indirect
github.com/weaveworks/flux v0.0.0-20190704153721-8292179855e1
github.com/whilp/git-urls v0.0.0-20160530060445-31bac0d230fa // indirect
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
golang.org/x/sys v0.0.0-20190422165155-953cdadca894
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bou.ke/monkey v1.0.1 h1:zEMLInw9xvNakzUUPjfS4Ds6jYPqCFx3m7bRmG5NH2U=
bou.ke/monkey v1.0.1/go.mod h1:FgHuK96Rv2Nlf+0u1OOVDpCMdsWyOFmeeketDHE7LIg=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8=
Expand Down Expand Up @@ -154,7 +155,13 @@ github.com/opencontainers/image-spec v1.0.1 h1:JMemWkRwHx4Zj+fVxWoMCFm/8sYGGrUVo
github.com/opencontainers/image-spec v1.0.1/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
github.com/otiai10/copy v1.0.1 h1:gtBjD8aq4nychvRZ2CyJvFWAw0aja+VHazDdruZKGZA=
github.com/otiai10/copy v1.0.1/go.mod h1:8bMCJrAqOtN/d9oyh5HR7HhLQMvcGMpGdwRDYsfOCHc=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/curr v0.0.0-20190513014714-f5a3d24e5776 h1:o59bHXu8Ejas8Kq6pjoVJQ9/neN66SM8AKh6wI42BBs=
github.com/otiai10/curr v0.0.0-20190513014714-f5a3d24e5776/go.mod h1:3HNVkVOU7vZeFXocWuvtcS0XSFLcf2XUSDHkq9t1jU4=
github.com/otiai10/mint v1.2.3 h1:PsrRBmrxR68kyNu6YlqYHbNlItc5vOkuS6LBEsNttVA=
github.com/otiai10/mint v1.2.3/go.mod h1:YnfyPNhBvnY8bW4SGQHCs/aAFhkgySlMZbrF5U0bOVw=
github.com/otiai10/mint v1.2.4 h1:DxYL0itZyPaR5Z9HILdxSoHx+gNs6Yx+neOGS3IVUk0=
github.com/otiai10/mint v1.2.4/go.mod h1:d+b7n/0R3tdyUYYylALXpWQ/kTN+QobSq/4SRGBkR3M=
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down Expand Up @@ -197,6 +204,10 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/vishvananda/netlink v1.0.0 h1:bqNY2lgheFIu1meHUFSH3d7vG93AFyqg3oGbJCOJgSM=
github.com/vishvananda/netlink v1.0.0/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk=
github.com/vishvananda/netns v0.0.0-20190625233234-7109fa855b0f h1:nBX3nTcmxEtHSERBJaIo1Qa26VwRaopnZmfDQUXsF4I=
github.com/vishvananda/netns v0.0.0-20190625233234-7109fa855b0f/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI=
github.com/whilp/git-urls v0.0.0-20160530060445-31bac0d230fa h1:rW+Lu6281ed/4XGuVIa4/YebTRNvoUJlfJ44ktEVwZk=
github.com/whilp/git-urls v0.0.0-20160530060445-31bac0d230fa/go.mod h1:2rx5KE5FLD0HRfkkpyn8JwbVLBdhgeiOb2D2D9LLKM4=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
Expand All @@ -216,6 +227,8 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJV
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
4 changes: 2 additions & 2 deletions hack/cobra.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"fmt"
"log"
"os"
"os/exec"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
Expand All @@ -20,7 +20,7 @@ func main() {
}

cmds := map[string]*cobra.Command{
"ignite": ignitecmd.NewIgniteCommand(os.Stdin, os.Stdout, os.Stderr),
"ignite": ignitecmd.NewIgniteCommand(os.Stdin, os.Stdout, os.Stderr),
"ignited": ignitedcmd.NewIgnitedCommand(os.Stdin, os.Stdout, os.Stderr),
}

Expand Down
77 changes: 51 additions & 26 deletions pkg/container/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"github.com/weaveworks/ignite/pkg/constants"
"github.com/weaveworks/ignite/pkg/util"
"k8s.io/apimachinery/pkg/util/wait"
)

Expand Down Expand Up @@ -117,19 +117,26 @@ func bridge(iface *net.Interface) (*DHCPInterface, error) {
tapName := constants.TAP_PREFIX + iface.Name
bridgeName := constants.BRIDGE_PREFIX + iface.Name

if err := createTAPAdapter(tapName); err != nil {
handle, err := netlink.NewHandle()
if err != nil {
return nil, err
}

if err := createBridge(bridgeName); err != nil {
tuntap, err := createTAPAdapter(handle, tapName)
if err != nil {
return nil, err
}

if err := connectAdapterToBridge(tapName, bridgeName); err != nil {
bridge, err := createBridge(handle, bridgeName)
if err != nil {
return nil, err
}

if err := connectAdapterToBridge(iface.Name, bridgeName); err != nil {
if err = handle.LinkSetMaster(tuntap, bridge); err != nil {
return nil, err
}

if err = handle.LinkSetMaster(tuntap, bridge); err != nil {
return nil, err
}

Expand All @@ -147,6 +154,11 @@ func takeAddress(iface *net.Interface) (*net.IPNet, bool, error) {
return nil, true, fmt.Errorf("interface %s has no address", iface.Name)
}

handle, err := netlink.NewHandle()
if err != nil {
return nil, false, errors.Wrapf(err, "failed to acquire handle on network namespace")
}

for _, addr := range addrs {
var ip net.IP
var mask net.IPMask
Expand All @@ -169,7 +181,17 @@ func takeAddress(iface *net.Interface) (*net.IPNet, bool, error) {
continue
}

if _, err := util.ExecuteCommand("ip", "addr", "del", ip.String(), "dev", iface.Name); err != nil {
link, err := netlink.LinkByName(iface.Name)
if err != nil {
return nil, false, errors.Wrapf(err, "failed to get interface by name %s", iface.Name)
}

delAddr, err := netlink.ParseAddr(addr.String())
if err != nil {
return nil, false, errors.Wrapf(err, "failed to parse address from stringified ip %s", addr.String())
}

if err = handle.AddrDel(link, delAddr); err != nil {
return nil, false, errors.Wrapf(err, "failed to remove address from interface %s", iface.Name)
}

Expand All @@ -184,28 +206,31 @@ func takeAddress(iface *net.Interface) (*net.IPNet, bool, error) {
return nil, false, fmt.Errorf("interface %s has no valid addresses", iface.Name)
}

func createTAPAdapter(tapName string) error {
if _, err := util.ExecuteCommand("ip", "tuntap", "add", "mode", "tap", tapName); err != nil {
return err
func createTAPAdapter(handle *netlink.Handle, tapName string) (*netlink.Tuntap, error) {
la := netlink.NewLinkAttrs()
la.Name = tapName
tuntap := &netlink.Tuntap{
LinkAttrs: la,
Mode: netlink.TUNTAP_MODE_TAP,
}

return setLinkUp(tapName)
}

func createBridge(bridgeName string) error {
if _, err := util.ExecuteCommand("ip", "link", "add", "name", bridgeName, "type", "bridge"); err != nil {
return err
if err := netlink.LinkAdd(tuntap); err != nil {
return nil, err
}

return setLinkUp(bridgeName)
}

func setLinkUp(adapterName string) error {
_, err := util.ExecuteCommand("ip", "link", "set", adapterName, "up")
return err
if err := netlink.LinkSetUp(tuntap); err != nil {
return nil, err
}
return tuntap, nil
}

func connectAdapterToBridge(adapterName, bridgeName string) error {
_, err := util.ExecuteCommand("ip", "link", "set", adapterName, "master", bridgeName)
return err
func createBridge(handle *netlink.Handle, bridgeName string) (*netlink.Bridge, error) {
la := netlink.NewLinkAttrs()
la.Name = bridgeName
bridge := &netlink.Bridge{LinkAttrs: la}
if err := netlink.LinkAdd(bridge); err != nil {
return nil, err
}
if err := netlink.LinkSetUp(bridge); err != nil {
return nil, err
}
return bridge, nil
}
2 changes: 1 addition & 1 deletion third_party/forked/github.com/weaveworks/flux/git/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"net/url"

"github.com/whilp/git-urls"
giturls "github.com/whilp/git-urls"
)

// Remote points at a git repo somewhere.
Expand Down
13 changes: 13 additions & 0 deletions vendor/github.com/vishvananda/netlink/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/vishvananda/netlink/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a9cb096

Please sign in to comment.