func (s *CiliumNetClientSuite) TestGetIPAMConfOK(c *C) { cniReq := ipam.IPAMReq{} ciliumRoutes := []ipam.Route{ *ipam.NewRoute(net.IPNet{IP: NodeAddr, Mask: addressing.NodeIPv6Mask}, nil), *ipam.NewRoute(addressing.IPv6DefaultRoute, NodeAddr), } rep := ipam.IPAMConfigRep{ IPAMConfig: &ipam.IPAMRep{ IP6: &ipam.IPConfig{ Gateway: NodeAddr, Routes: ciliumRoutes, }, }, } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { c.Assert(r.Method, Equals, "POST") c.Assert(r.URL.Path, Equals, "/allocator/ipam-configuration/"+string(ipam.LibnetworkIPAMType)) var options ipam.IPAMReq err := json.NewDecoder(r.Body).Decode(&options) c.Assert(err, IsNil) c.Assert(options, DeepEquals, cniReq) w.WriteHeader(http.StatusOK) err = json.NewEncoder(w).Encode(rep) c.Assert(err, Equals, nil) })) defer server.Close() cli := NewTestClient(server.URL, c) ipamRep, err := cli.GetIPAMConf(ipam.LibnetworkIPAMType, cniReq) c.Assert(err, Equals, nil) c.Assert(*ipamRep, DeepEquals, rep) }
func (s *DaemonSuite) TestGetIPAMConfOK(c *C) { ciliumRoutes := []ipam.Route{ *ipam.NewRoute(net.IPNet{IP: NodeAddr, Mask: addressing.NodeIPv6Mask}, nil), *ipam.NewRoute(addressing.IPv6DefaultRoute, NodeAddr), } rep := ipam.IPAMConfigRep{ IPAMConfig: &ipam.IPAMRep{ IP6: &ipam.IPConfig{ Gateway: NodeAddr, Routes: ciliumRoutes, }, }, } s.d.OnGetIPAMConf = func(ipamType ipam.IPAMType, options ipam.IPAMReq) (*ipam.IPAMConfigRep, error) { c.Assert(ipamType, Equals, ipam.LibnetworkIPAMType) c.Assert(options, Equals, ipam.IPAMReq{}) return &rep, nil } ipamRep, err := s.c.GetIPAMConf(ipam.LibnetworkIPAMType, ipam.IPAMReq{}) c.Assert(err, IsNil) c.Assert(*ipamRep, DeepEquals, rep) }
// getIPAMConfLibnetwork returns the Libnetwork specific IPAM configuration. func (d *Daemon) getIPAMConfLibnetwork(ln ipam.IPAMReq) (*ipam.IPAMConfigRep, error) { if ln.RequestPoolRequest != nil { var poolID, pool, gw string if ln.RequestPoolRequest.V6 == false { poolID = ipam.LibnetworkDefaultPoolV4 pool = ipam.LibnetworkDummyV4AllocPool gw = ipam.LibnetworkDummyV4Gateway } else { subnetGo := net.IPNet(d.ipamConf.IPAMConfig.Subnet) poolID = ipam.LibnetworkDefaultPoolV6 pool = subnetGo.String() gw = d.ipamConf.IPAMConfig.Gateway.String() + "/128" } return &ipam.IPAMConfigRep{ RequestPoolResponse: &lnAPI.RequestPoolResponse{ PoolID: poolID, Pool: pool, Data: map[string]string{ "com.docker.network.gateway": gw, }, }, }, nil } ciliumV6Routes := []ipam.Route{} for _, r := range d.ipamConf.IPAMConfig.Routes { if r.Dst.IP.To4() == nil { ciliumRoute := ipam.NewRoute(r.Dst, r.GW) ciliumV6Routes = append(ciliumV6Routes, *ciliumRoute) } } rep := &ipam.IPAMConfigRep{ IPAMConfig: &ipam.IPAMRep{ IP6: &ipam.IPConfig{ Gateway: d.ipamConf.IPAMConfig.Gateway, Routes: ciliumV6Routes, }, }, } if d.conf.IPv4Enabled { ciliumV4Routes := []ipam.Route{} for _, r := range d.ipamConf.IPAMConfig.Routes { if r.Dst.IP.To4() != nil { ciliumRoute := ipam.NewRoute(r.Dst, r.GW) ciliumV4Routes = append(ciliumV4Routes, *ciliumRoute) } } rep.IPAMConfig.IP4 = &ipam.IPConfig{ Gateway: d.conf.NodeAddress.IPv4Address.IP(), Routes: ciliumV4Routes, } } return rep, nil }
// allocateIPCNI allocates an IP for the CNI plugin. func (d *Daemon) allocateIPCNI(cniReq ipam.IPAMReq) (*ipam.IPAMRep, error) { d.ipamConf.AllocatorMutex.Lock() defer d.ipamConf.AllocatorMutex.Unlock() if cniReq.IP != nil { var err error if cniReq.IP.To4() != nil { if d.conf.IPv4Enabled { err = d.ipamConf.IPv4Allocator.Allocate(*cniReq.IP) } } else { err = d.ipamConf.IPv6Allocator.Allocate(*cniReq.IP) } return nil, err } ipConf, err := d.ipamConf.IPv6Allocator.AllocateNext() if err != nil { return nil, err } v6Routes := []ipam.Route{} v4Routes := []ipam.Route{} for _, r := range d.ipamConf.IPAMConfig.Routes { rt := ipam.NewRoute(r.Dst, r.GW) if r.Dst.IP.To4() == nil { v6Routes = append(v6Routes, *rt) } else { v4Routes = append(v4Routes, *rt) } } ipamRep := &ipam.IPAMRep{ IP6: &ipam.IPConfig{ Gateway: d.conf.NodeAddress.IPv6Address.IP(), IP: net.IPNet{IP: ipConf, Mask: addressing.ContainerIPv6Mask}, Routes: v6Routes, }, } if d.ipamConf.IPv4Allocator != nil { ip4Conf, err := d.ipamConf.IPv4Allocator.AllocateNext() if err != nil { return nil, err } if d.ipamConf.IPv4Allocator != nil { ipamRep.IP4 = &ipam.IPConfig{ Gateway: d.conf.NodeAddress.IPv4Address.IP(), IP: net.IPNet{IP: ip4Conf, Mask: addressing.ContainerIPv4Mask}, Routes: v4Routes, } } } return ipamRep, nil }