Skip to content

Commit

Permalink
Merge pull request #6 from ahmetozer/feat/issues3
Browse files Browse the repository at this point in the history
feat: wait until interface is ready.
  • Loading branch information
ahmetozer authored Jun 24, 2024
2 parents 86e1065 + f9b0663 commit 37808cd
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
13 changes: 7 additions & 6 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func run(args []string) error {
PodIface := config.NetIface{Type: "veth"}
f.StringVar(&PodIface.IP, "pod-ips", "", "container interface ips")

f.StringVar(&c.Resolv, "resolv", "cp-n", "cp (copy), cp-n (copy if not exist), image (use image), 1.1.1.1;2606:4700:4700::1111 (provide nameservers)")
f.StringVar(&c.Resolv, "resolv", "cp", "cp (copy), cp-n (copy if not exist), image (use image), 1.1.1.1;2606:4700:4700::1111 (provide nameservers)")
f.StringVar(&c.Hosts, "hosts", "cp", "cp (copy), cp-n (copy if not exist), image(use image)")

f.StringVar(&c.NS.Net, "ns-net", "", "net namespace or host")
Expand All @@ -68,11 +68,6 @@ func run(args []string) error {
if c.RootfsDir == "" {
c.RootfsDir = defaultRootfs(&c)
}
c.Ifaces = []config.NetIface{HostIface}
PodIface.Main = append(PodIface.Main, HostIface)
if PodIface.IP == "" {
PodIface.IP = net.FindFreePodIPs(HostIface.IP)
}

if help {
f.Usage()
Expand All @@ -91,6 +86,12 @@ func run(args []string) error {
return fmt.Errorf("container %s is already running", c.Name)
}

c.Ifaces = []config.NetIface{HostIface}
PodIface.Main = append(PodIface.Main, HostIface)
if PodIface.IP == "" {
PodIface.IP = net.FindFreePodIPs(HostIface.IP)
}

if c.NS.Net != "host" {

if HostIface.Type == "bridge" {
Expand Down
17 changes: 12 additions & 5 deletions pkg/container/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ func Exec() {
childSysMounts(&c)
childSysNodes(&c)

if err := net.SetInterfaceUp("lo"); err != nil {
log.Fatalf("unable to set lo up %s", err)
}

_, args := childArgs(os.Args)
if err := unix.Exec(c.Exec, append([]string{c.Exec}, args...), os.Environ()); err != nil {
log.Fatalf("unable to exec %s: %s", c.Exec, err)
Expand Down Expand Up @@ -57,9 +53,15 @@ func loadConfig() (config.Config, error) {

func configureIfaces(c *config.Config) {
var err error
ethNo := 0
var ethNo uint8 = 0
for i := range c.Ifaces {
if c.Ifaces[i].ALocFor == config.ALocForPod {

err = net.WaitInterface(c.Ifaces[i].Name)
if err != nil {
log.Fatalf("%s", err)
}

err = net.SetName(c, c.Ifaces[i].Name, fmt.Sprintf("eth%d", ethNo))
if err != nil {
log.Fatalf("unable to set name %s", err)
Expand All @@ -74,11 +76,16 @@ func configureIfaces(c *config.Config) {
if err != nil {
log.Fatalf("unable to set eth%d up %s", ethNo, err)
}

if ethNo == 0 {
net.AddDefaultRoutes(c.Ifaces[i])
}

ethNo++
}
}

if err := net.SetInterfaceUp("lo"); err != nil {
log.Fatalf("unable to set lo up %s", err)
}
}
3 changes: 3 additions & 0 deletions pkg/container/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func Start(c *config.Config, args []string) (int, error) {
}

c.SaveConftoDisk()

err := cmd.Start()
if err != nil {
return 0, fmt.Errorf("starting container: %v", err)
Expand All @@ -87,7 +88,9 @@ func Start(c *config.Config, args []string) (int, error) {
loadNamespaceIDs(c)

c.Status = ContainerStatusRunning

c.SaveConftoDisk()

for _, iface := range c.Ifaces {
if iface.ALocFor == config.ALocForPod {
err = net.SetNs(iface, c.ContPid)
Expand Down
2 changes: 1 addition & 1 deletion pkg/net/clear.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Clear(c *config.Config) {
link, err := netlink.LinkByName(iface.Name)
if err == nil && link != nil {
if err := netlink.LinkDel(link); err != nil {
slog.Info("linkdel: %v", err)
slog.Info("linkdel:", slog.String("err", err.Error()))
}
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/net/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func CreateIface(c *config.Config, iface *config.NetIface) error {
ifname := NewIfName(c)

ifaceLink = &netlink.Veth{LinkAttrs: netlink.LinkAttrs{Name: ifname.Host, MasterIndex: masterlink.Attrs().Index}, PeerName: ifname.Cont}
netlink.LinkDel(ifaceLink)
if err = netlink.LinkAdd(ifaceLink); err != nil {
return fmt.Errorf("error creating container host interface: %v", err)
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/net/iface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package net

import (
"fmt"
"time"

"github.com/ahmetozer/sandal/pkg/config"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -54,3 +57,33 @@ func SetInterfaceUp(name string) error {
err = netlink.LinkSetUp(link)
return err
}

func WaitInterface(name string) error {

interfaceReady := make(chan bool)

go func(killed chan<- bool) {
for {
link, err := netlink.LinkByName(name)
if err != nil {
fmt.Printf("%s", err)
}
if err == nil && link != nil {
interfaceReady <- true
break
}
time.Sleep(time.Second)
}
}(interfaceReady)

select {
case ret := <-interfaceReady:
if !ret {
return fmt.Errorf("unable to get interface")
}
return nil
case <-time.After(5 * time.Second):
return fmt.Errorf("unable to get interface %s in 5 second", name)
}

}

0 comments on commit 37808cd

Please sign in to comment.