diff --git a/config/disk.go b/config/disk.go index ab54a91e..dd3ddf9e 100755 --- a/config/disk.go +++ b/config/disk.go @@ -1,7 +1,9 @@ package config import ( + "encoding/json" "io/ioutil" + "net" "os" "path" "path/filepath" @@ -125,6 +127,30 @@ func SaveInitCmdline(cmdLineArgs string) { } } +func SaveCNISubnet(subnet, filename string) { + if _, _, err := net.ParseCIDR(subnet); err != nil { + log.Errorf("Failed to parse system-docker subnet from cmdline: %v", err) + } + rBytes, err := ioutil.ReadFile(filename) + if err != nil { + log.Errorf("Failed to read from %s: %v", filename, err) + } + var data map[string]interface{} + if err = json.Unmarshal(rBytes, &data); err != nil { + log.Errorf("Failed to pasre json: %v", err) + } + ipam := data["ipam"].(map[string]interface{}) + ipam["subnet"] = subnet + wBytes, err := json.MarshalIndent(data, "", "\t") + if err != nil { + log.Errorf("Failed to convert to bytes: %v", err) + } + log.Debugf("New cni bridge conf: %s", wBytes) + if err = ioutil.WriteFile(filename, wBytes, 0644); err != nil { + log.Errorf("Failed to write cni bridge file %s: %v", filename, err) + } +} + func CloudConfigDirFiles(dirPrefix string) []string { cloudConfigDir := path.Join(dirPrefix, CloudConfigDir) diff --git a/config/types.go b/config/types.go index ef3428a5..064705ad 100755 --- a/config/types.go +++ b/config/types.go @@ -47,6 +47,9 @@ const ( MetaDataFile = "/var/lib/rancher/conf/metadata" CloudConfigFile = "/var/lib/rancher/conf/cloud-config.yml" EtcResolvConfFile = "/etc/resolv.conf" + CNIBridgeConfigFile = "/etc/docker/cni/bridge.d/bridge.conf" + + RKPDockerSysBridgeSubnet = "rancher.system_docker.subnet" ) var ( diff --git a/init/init.go b/init/init.go index 6c645ef8..24343881 100755 --- a/init/init.go +++ b/init/init.go @@ -14,6 +14,7 @@ import ( "github.com/docker/docker/pkg/mount" "github.com/rancher/os/config" + "github.com/rancher/os/config/cmdline" "github.com/rancher/os/dfs" "github.com/rancher/os/log" "github.com/rancher/os/util" @@ -428,6 +429,12 @@ func RunInit() error { log.FsReady() log.Debugf("WARNING: switchroot and mount OEM2 phases not written to log file") + // update docker-sys bridge setting + if dockerSysSubnet := cmdline.GetCmdline(config.RKPDockerSysBridgeSubnet); dockerSysSubnet != "" { + val := dockerSysSubnet.(string) + config.SaveCNISubnet(val, config.CNIBridgeConfigFile) + } + return cfg, nil }}, config.CfgFuncData{"b2d Env", func(cfg *config.CloudConfig) (*config.CloudConfig, error) {