Browse Source

Add FLANNEL_NETWORK to subnet.env

CNI plugin needs to know the whole flannel network
in order to set up a route for it.
Eugene Yakubovich 9 years ago
parent
commit
73bb01d3cb
2 changed files with 9 additions and 8 deletions
  1. 4 3
      main.go
  2. 5 5
      network/network.go

+ 4 - 3
main.go

@@ -101,7 +101,7 @@ func flagsFromEnv(prefix string, fs *flag.FlagSet) {
 	})
 }
 
-func writeSubnetFile(path string, sn *backend.SubnetDef) error {
+func writeSubnetFile(path string, nw ip.IP4Net, sn *backend.SubnetDef) error {
 	dir, name := filepath.Split(path)
 	os.MkdirAll(dir, 0755)
 
@@ -115,6 +115,7 @@ func writeSubnetFile(path string, sn *backend.SubnetDef) error {
 	// sn.IP by one
 	sn.Net.IP += 1
 
+	fmt.Fprintf(f, "FLANNEL_NETWORK=%s\n", nw)
 	fmt.Fprintf(f, "FLANNEL_SUBNET=%s\n", sn.Net)
 	fmt.Fprintf(f, "FLANNEL_MTU=%d\n", sn.MTU)
 	_, err = fmt.Fprintf(f, "FLANNEL_IPMASQ=%v\n", opts.ipMasq)
@@ -223,11 +224,11 @@ func initAndRun(ctx context.Context, sm subnet.Manager, netnames []string) {
 			if sn != nil {
 				if isMultiNetwork() {
 					path := filepath.Join(opts.subnetDir, n.Name) + ".env"
-					if err := writeSubnetFile(path, sn); err != nil {
+					if err := writeSubnetFile(path, n.Config.Network, sn); err != nil {
 						return
 					}
 				} else {
-					if err := writeSubnetFile(opts.subnetFile, sn); err != nil {
+					if err := writeSubnetFile(opts.subnetFile, n.Config.Network, sn); err != nil {
 						return
 					}
 					daemon.SdNotify("READY=1")

+ 5 - 5
network/network.go

@@ -26,7 +26,8 @@ import (
 )
 
 type Network struct {
-	Name string
+	Name   string
+	Config *subnet.Config
 
 	sm     subnet.Manager
 	ipMasq bool
@@ -42,13 +43,12 @@ func New(sm subnet.Manager, name string, ipMasq bool) *Network {
 }
 
 func (n *Network) Init(ctx context.Context, iface *net.Interface, iaddr net.IP, eaddr net.IP) *backend.SubnetDef {
-	var cfg *subnet.Config
 	var be backend.Backend
 	var sn *backend.SubnetDef
 
 	steps := []func() error{
 		func() (err error) {
-			cfg, err = n.sm.GetNetworkConfig(ctx, n.Name)
+			n.Config, err = n.sm.GetNetworkConfig(ctx, n.Name)
 			if err != nil {
 				log.Error("Failed to retrieve network config: ", err)
 			}
@@ -56,7 +56,7 @@ func (n *Network) Init(ctx context.Context, iface *net.Interface, iaddr net.IP,
 		},
 
 		func() (err error) {
-			be, err = newBackend(n.sm, n.Name, cfg)
+			be, err = newBackend(n.sm, n.Name, n.Config)
 			if err != nil {
 				log.Error("Failed to create backend: ", err)
 			} else {
@@ -75,7 +75,7 @@ func (n *Network) Init(ctx context.Context, iface *net.Interface, iaddr net.IP,
 
 		func() (err error) {
 			if n.ipMasq {
-				flannelNet := cfg.Network
+				flannelNet := n.Config.Network
 				if err = setupIPMasq(flannelNet); err != nil {
 					log.Errorf("Failed to set up IP Masquerade for network %v: %v", n.Name, err)
 				}