Skip to content
Merged

Fi #36

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ func Cmd(args []string) error {
if c.Name == args[0] || (*all) {
c.HostArgs[0] = os.Args[0] // sync with current command
for i := range c.HostArgs {
k := strings.Split(c.HostArgs[i], "=")
if len(k) > 1 {
k[1] = "\"" + k[1]
k[len(k)-1] = k[len(k)-1] + "\""
c.HostArgs[i] = strings.Join(k, "=")
}

if len(c.HostArgs[i]) > 0 && c.HostArgs[i][:1] == "-" {
k := strings.Split(c.HostArgs[i], "=")
if len(k) > 1 {
k[1] = "\"" + k[1]
k[len(k)-1] = k[len(k)-1] + "\""
c.HostArgs[i] = strings.Join(k, "=")
}
}
if strings.Contains(c.HostArgs[i], " ") {
c.HostArgs[i] = `'` + c.HostArgs[i] + `'`
}
Expand Down
28 changes: 20 additions & 8 deletions pkg/container/cruntime/net/ip-allocation.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
package net

import (
"encoding/json"
"fmt"
"net"

"github.com/ahmetozer/sandal/pkg/container/config"
)

func ipRequest(conts *[]*config.Config, net *net.IPNet) (ip net.IP, err error) {
func ipRequest(conts *[]*config.Config, x *net.IPNet) (ip net.IP, err error) {
var IPnet net.IPNet
err = deepCopy(x, &IPnet)
if err != nil {
return nil, err
}

x := *net
IPnet := &x
brodcastSupport := false
cidr, bits := IPnet.Mask.Size()

ip = net.IP
ip = IPnet.IP

// Increment at below is bypass first ip like 192.168.2.0
switch bits {
case 128:
if cidr <= 120 {
ip, err = ipIncrement(ip, IPnet)
ip, err = ipIncrement(ip, &IPnet)
if err != nil {
return nil, err
}
}
case 32:
if cidr <= 24 {
brodcastSupport = true
ip, err = ipIncrement(ip, IPnet)
ip, err = ipIncrement(ip, &IPnet)
if err != nil {
return nil, err
}
Expand All @@ -41,20 +45,28 @@ func ipRequest(conts *[]*config.Config, net *net.IPNet) (ip net.IP, err error) {
for {
if !addrInUse(conts, ip) {
if ip.To4() != nil && brodcastSupport {
if ip.Equal(lastIP(IPnet)) {
if ip.Equal(lastIP(&IPnet)) {
return nil, fmt.Errorf("allocatable unicast ip block is exhausted")
}
}
// IP succesfully allocated
return ip, nil
}
ip, err = ipIncrement(ip, IPnet)
ip, err = ipIncrement(ip, &IPnet)
if err != nil {
return nil, err
}
}
}

func deepCopy(src, dst interface{}) error {
bytes, err := json.Marshal(src)
if err != nil {
return err
}
return json.Unmarshal(bytes, dst)
}

func addrInUse(configs *[]*config.Config, ip net.IP) bool {

addrs, _ := net.InterfaceAddrs()
Expand Down
1 change: 1 addition & 0 deletions pkg/container/cruntime/net/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (l Link) defaults(conts *[]*config.Config) Link {
}

hostAddrs, err := GetAddrsByName(l.Master)
slog.Debug("hostAddrs", "interface", l.Master, slog.Any("addrs", hostAddrs))
// Allocate IP addresses to container for each subnet
if len(l.Addr) == 0 {
// hostAddrs, err := stringToAddrs(env.DefaultHostNet) //
Expand Down
3 changes: 1 addition & 2 deletions pkg/container/cruntime/net/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ func (l Links) FindGateways() (IPv4 Addr, IPv6 Addr) {
IPv6 = gw
IPv6.IPNet.IP = net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
IPv6.IPNet.Mask = net.CIDRMask(0, 128)
IPv6.IP, IPv6.IPNet, _ = net.ParseCIDR("fd34:135:127::1/0")
// IPv6.IP, IPv6.IPNet, _ = net.ParseCIDR("fd34:135:127::1/0")
} else {
IPv4 = gw
IPv4.IPNet.IP = net.IP{0, 0, 0, 0}
IPv4.IPNet.Mask = net.CIDRMask(0, 32)
// IPv4.IP, IPv4.IPNet, _ = net.ParseCIDR("172.19.0.1/0")

}
// Return earlies as possible
if IPv4.IP != nil && IPv6.IP != nil {
Expand Down
Loading