mirror of
https://github.com/rancher/os.git
synced 2025-08-08 02:04:13 +00:00
Refactor network config to put that match as the key
This commit is contained in:
parent
bffd9865ea
commit
e143d04885
@ -35,11 +35,34 @@ func applyNetworkConfigs(cfg *config.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//apply network config
|
//apply network config
|
||||||
for _, netConf := range cfg.Network.Interfaces {
|
|
||||||
for _, link := range links {
|
for _, link := range links {
|
||||||
err := applyNetConf(link, netConf)
|
linkName := link.Attrs().Name
|
||||||
|
var match config.InterfaceConfig
|
||||||
|
|
||||||
|
for key, netConf := range cfg.Network.Interfaces {
|
||||||
|
if netConf.Match == "" {
|
||||||
|
netConf.Match = key
|
||||||
|
}
|
||||||
|
|
||||||
|
if netConf.Match == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// "" means match has not been found
|
||||||
|
if match.Match == "" && matches(linkName, netConf.Match) {
|
||||||
|
match = netConf
|
||||||
|
}
|
||||||
|
|
||||||
|
if netConf.Match == linkName {
|
||||||
|
// Found exact match, use it over wildcard match
|
||||||
|
match = netConf
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if match.Match != "" {
|
||||||
|
err = applyNetConf(link, match)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to apply settings to %s : %v", link.Attrs().Name, err)
|
log.Errorf("Failed to apply settings to %s : %v", linkName, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,7 +79,6 @@ func applyNetworkConfigs(cfg *config.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func applyNetConf(link netlink.Link, netConf config.InterfaceConfig) error {
|
func applyNetConf(link netlink.Link, netConf config.InterfaceConfig) error {
|
||||||
if matches(link.Attrs().Name, netConf.Match) {
|
|
||||||
if netConf.DHCP {
|
if netConf.DHCP {
|
||||||
log.Infof("Running DHCP on %s", link.Attrs().Name)
|
log.Infof("Running DHCP on %s", link.Attrs().Name)
|
||||||
cmd := exec.Command("udhcpc", "-i", link.Attrs().Name, "-t", "20", "-n")
|
cmd := exec.Command("udhcpc", "-i", link.Attrs().Name, "-t", "20", "-n")
|
||||||
@ -65,10 +87,9 @@ func applyNetConf(link netlink.Link, netConf config.InterfaceConfig) error {
|
|||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
|
} else if netConf.Address == "" {
|
||||||
|
return nil
|
||||||
} else {
|
} else {
|
||||||
if netConf.Address == "" {
|
|
||||||
return errors.New("DHCP is false and Address is not set")
|
|
||||||
}
|
|
||||||
addr, err := netlink.ParseAddr(netConf.Address)
|
addr, err := netlink.ParseAddr(netConf.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -109,7 +130,7 @@ func applyNetConf(link netlink.Link, netConf config.InterfaceConfig) error {
|
|||||||
|
|
||||||
log.Infof("Set default gateway %s", netConf.Gateway)
|
log.Infof("Set default gateway %s", netConf.Gateway)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user