Skip to content

Commit

Permalink
fix: connmanctl config nameservers and ipv4
Browse files Browse the repository at this point in the history
  • Loading branch information
yaocw2020 authored and guangbochen committed Dec 16, 2020
1 parent 756bd89 commit 44cac63
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pkg/bridge/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (l *Link) DelBridgeVlan(vid uint16) error {

const (
defaultRetryTimes = 10
defaultRetryInterval = 200 * time.Microsecond
defaultRetryInterval = time.Second
)

func (l *Link) setNoMaster() error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/vlan/bridge_vlan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (c *BridgeVLANController) configBridgeNetwork(setting *NetworkSetting) erro
// - No, make bridge return addresses to configured nic, lend addresses to bridge, return.
if nic.Attrs().MasterIndex != c.bridge.Index {
klog.Infof("configure network because the master of nic %s is not bridge %s", nic.Attrs().Name, c.bridge.Name)
if configuredNIC != nil {
if configuredNIC != nil && setting.NIC != setting.ConfiguredNIC {
if err := c.bridge.ReturnAddr(configuredNIC, vidList); err != nil {
klog.Errorf("return address failed, error: %v, NIC: %s", err, setting.ConfiguredNIC)
}
Expand Down
62 changes: 46 additions & 16 deletions pkg/dhcp/connman_dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import (

const (
// keep --ipv4 to the end to avoid empty gateway configuration
configDHCPCmd = "connmanctl config %s --nameservers 8.8.8.8 --ipv4 %s"
getIPv4ConfigCmd = "connmanctl services %s"
ipv4ConfigKeyword = "IPv4.Configuration"
ipv4MethodKeyword = "Method"
ipv4Keyword = "IPv4 = "
addrKeyword = "Address"
maskKeyword = "Netmask"
gatewayKeyword = "Gateway"
configDHCPCmd = "connmanctl config %s --ipv4 %s"
configNameserversCmd = "connmanctl config %s --nameservers 8.8.8.8"
getIPv4ConfigCmd = "connmanctl services %s"
ipv4ConfigKeyword = "IPv4.Configuration"
ipv4MethodKeyword = "Method"
ipv4Keyword = "IPv4 = "
addrKeyword = "Address"
maskKeyword = "Netmask"
gatewayKeyword = "Gateway"

// IPv4 mode
Off = "off"
Expand All @@ -47,10 +48,6 @@ func NewConnmanService(iface, hwaddr string) *ConnmanService {
// ConfigIPv4 with connmanctl
func (c *ConnmanService) configIPv4(mode string, args ...string) error {
if mode == Static {
if len(args) != 3 {
return fmt.Errorf("config manual mode should have 3 arguments, followed by address, netmask and gateway")
}

for _, arg := range args {
mode += " "
mode += arg
Expand All @@ -72,6 +69,7 @@ func (c *ConnmanService) configIPv4(mode string, args ...string) error {
}

statement := fmt.Sprintf(configDHCPCmd, c.service, mode)
klog.Infof("statement: %s", statement)
out, err := exec.Command("sh", "-c", statement).CombinedOutput()
if err != nil {
return fmt.Errorf("execute connmanctl config failed, error: %w, cmd: %s", err, statement)
Expand All @@ -80,8 +78,18 @@ func (c *ConnmanService) configIPv4(mode string, args ...string) error {
return fmt.Errorf("execute connmanctl config failed, stderr: [%s], cmd: %s", out, statement)
}

// wait for becoming effective
time.Sleep(waitEffectiveTime)
return nil
}

func (c *ConnmanService) configNameservers() error {
statement := fmt.Sprintf(configNameserversCmd, c.service)
out, err := exec.Command("sh", "-c", statement).CombinedOutput()
if err != nil {
return fmt.Errorf("execute connmanctl config failed, error: %w, cmd: %s", err, statement)
}
if len(out) != 0 {
return fmt.Errorf("execute connmanctl config failed, stderr: [%s], cmd: %s", out, statement)
}

return nil
}
Expand All @@ -94,11 +102,33 @@ func (c *ConnmanService) DHCP2Static() error {

addr, mask, gw := getIPv4Addr(out)

return c.configIPv4(Static, addr, mask, gw)
if err := c.configIPv4(Static, addr, mask, gw); err != nil {
return err
}

if err := c.configNameservers(); err != nil {
return err
}

// wait for becoming effective
time.Sleep(waitEffectiveTime)

return nil
}

func (c *ConnmanService) ToDHCP() error {
return c.configIPv4(DHCP)
if err := c.configIPv4(DHCP); err != nil {
return err
}

if err := c.configNameservers(); err != nil {
return err
}

// wait for becoming effective
time.Sleep(waitEffectiveTime)

return nil
}

// GetIPv4Config with connmanctl
Expand Down

0 comments on commit 44cac63

Please sign in to comment.