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

Refactor: Use the netlink library instead of exec'ing out to ip #279

Merged
merged 1 commit into from
Aug 7, 2019

Conversation

alexeldeib
Copy link
Contributor

Fixes #85

@@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make tidy added this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's fine to include here, just forgot to update the docs in an earlier PR where I changed this 😅

Copy link
Contributor

@twelho twelho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @alexeldeib! 🎉
I added some minor design changes that will simplify the code and prevent excess lookups by interface names. After those have been addressed, looks like we're ready to merge!

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's fine to include here, just forgot to update the docs in an earlier PR where I changed this 😅

return err
}

return setLinkUp(tapName)
return setLinkUp(handle, tapName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should likely call handle.LinkSetUp(tuntap) here and get rid of setLinkUp entirely

return err
}

return setLinkUp(bridgeName)
return handle.LinkSetUp(bridge)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just like this, we don't need setLinkUp anymore

func setLinkUp(adapterName string) error {
_, err := util.ExecuteCommand("ip", "link", "set", adapterName, "up")
return err
func setLinkUp(handle *netlink.Handle, adapterName string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed in favor of handle.LinkSetUp

la.Name = bridgeName
bridge := &netlink.Bridge{LinkAttrs: la}
err := netlink.LinkAdd(bridge)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concatenate into if err := netlink.LinkAdd(bridge); err != nil {

return nil, err
}

if err := createBridge(bridgeName); err != nil {
if err := createBridge(handle, bridgeName); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return the *netlink.Bridge from createBridge

return nil, err
}

if err := createTAPAdapter(handle, tapName); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return the *netlink.Tuntap from createTAPAdapter

return nil, err
}

if err := connectAdapterToBridge(tapName, bridgeName); err != nil {
if err := connectAdapterToBridge(handle, tapName, bridgeName); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the Bridge and Tuntap devices returned above, call handle.LinkSetMaster here instead of connectAdapterToBridge

return nil, err
}

if err := connectAdapterToBridge(iface.Name, bridgeName); err != nil {
if err := connectAdapterToBridge(handle, iface.Name, bridgeName); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, use netlink.LinkByName(iface.Name) to retrieve the device for handle.LinkSetMaster

func connectAdapterToBridge(adapterName, bridgeName string) error {
_, err := util.ExecuteCommand("ip", "link", "set", adapterName, "master", bridgeName)
return err
func connectAdapterToBridge(handle *netlink.Handle, adapterName, bridgeName string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed in favor of calling handle.LinkSetMaster directly

Copy link
Contributor

@luxas luxas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for this contribution @alexeldeib 🎉!
Very appreciated. Sorry that the master branch had some outstanding issues
with the doc generation, something I just fixed at HEAD.
When you rebase those unnecessary changes should go away.

@luxas luxas added this to the v0.5.0 milestone Aug 6, 2019
@luxas luxas added the kind/enhancement Categorizes issue or PR as related to improving an existing feature. label Aug 6, 2019
@alexeldeib alexeldeib force-pushed the ace/netlink branch 2 times, most recently from a9cb096 to cbd2183 Compare August 6, 2019 20:20
@alexeldeib
Copy link
Contributor Author

Hmm...something in this PR breaks ignite ssh <my-vm>. Debugging.

@alexeldeib
Copy link
Contributor Author

alexeldeib commented Aug 6, 2019

oh, several things broke it! 😄 Should be better now.

@alexeldeib
Copy link
Contributor Author

@luxas glad I can lend a hand 😃 Thank you and @twelho for this project! Firecracker is very cool but the UX is not great. Ignite removes a lot of that pain. Being able to see the image + vm + networking setup in this project has been super helpful for my own learning process.

Copy link
Contributor

@twelho twelho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes @alexeldeib!
LGTM 👍

@twelho twelho merged commit 9a3f845 into weaveworks:master Aug 7, 2019
@luxas luxas changed the title refactor: use netlink instead of exec'ing Refactor: Use the netlink library instead of exec'ing out to ip Aug 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/enhancement Categorizes issue or PR as related to improving an existing feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use github.com/vishvananda/netlink instead of execing to ip
3 participants