@@ -71,8 +71,9 @@ type NetworkInfo interface {
7171type EndpointWalker func (ep Endpoint ) bool
7272
7373type svcInfo struct {
74- svcMap map [string ][]net.IP
75- ipMap map [string ]string
74+ svcMap map [string ][]net.IP
75+ svcIPv6Map map [string ][]net.IP
76+ ipMap map [string ]string
7677}
7778
7879// IpamConf contains all the ipam related configurations for a network
@@ -489,6 +490,10 @@ func (n *network) UnmarshalJSON(b []byte) (err error) {
489490 if v , ok := netMap ["inDelete" ]; ok {
490491 n .inDelete = v .(bool )
491492 }
493+ // Reconcile old networks with the recently added `--ipv6` flag
494+ if ! n .enableIPv6 {
495+ n .enableIPv6 = len (n .ipamV6Info ) > 0
496+ }
492497 return nil
493498}
494499
@@ -779,7 +784,7 @@ func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoi
779784 ep .ipamOptions [netlabel .MacAddress ] = ep .iface .mac .String ()
780785 }
781786
782- if err = ep .assignAddress (ipam .driver , true , ! n .postIPv6 ); err != nil {
787+ if err = ep .assignAddress (ipam .driver , true , n . enableIPv6 && ! n .postIPv6 ); err != nil {
783788 return nil , err
784789 }
785790 defer func () {
@@ -799,7 +804,7 @@ func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoi
799804 }
800805 }()
801806
802- if err = ep .assignAddress (ipam .driver , false , n .postIPv6 ); err != nil {
807+ if err = ep .assignAddress (ipam .driver , false , n .enableIPv6 && n . postIPv6 ); err != nil {
803808 return nil , err
804809 }
805810
@@ -890,68 +895,103 @@ func (n *network) EndpointByID(id string) (Endpoint, error) {
890895}
891896
892897func (n * network ) updateSvcRecord (ep * endpoint , localEps []* endpoint , isAdd bool ) {
898+ var ipv6 net.IP
893899 epName := ep .Name ()
894900 if iface := ep .Iface (); iface .Address () != nil {
895901 myAliases := ep .MyAliases ()
902+ if iface .AddressIPv6 () != nil {
903+ ipv6 = iface .AddressIPv6 ().IP
904+ }
905+
896906 if isAdd {
897907 // If anonymous endpoint has an alias use the first alias
898908 // for ip->name mapping. Not having the reverse mapping
899909 // breaks some apps
900910 if ep .isAnonymous () {
901911 if len (myAliases ) > 0 {
902- n .addSvcRecords (myAliases [0 ], iface .Address ().IP , true )
912+ n .addSvcRecords (myAliases [0 ], iface .Address ().IP , ipv6 , true )
903913 }
904914 } else {
905- n .addSvcRecords (epName , iface .Address ().IP , true )
915+ n .addSvcRecords (epName , iface .Address ().IP , ipv6 , true )
906916 }
907917 for _ , alias := range myAliases {
908- n .addSvcRecords (alias , iface .Address ().IP , false )
918+ n .addSvcRecords (alias , iface .Address ().IP , ipv6 , false )
909919 }
910920 } else {
911921 if ep .isAnonymous () {
912922 if len (myAliases ) > 0 {
913- n .deleteSvcRecords (myAliases [0 ], iface .Address ().IP , true )
923+ n .deleteSvcRecords (myAliases [0 ], iface .Address ().IP , ipv6 , true )
914924 }
915925 } else {
916- n .deleteSvcRecords (epName , iface .Address ().IP , true )
926+ n .deleteSvcRecords (epName , iface .Address ().IP , ipv6 , true )
917927 }
918928 for _ , alias := range myAliases {
919- n .deleteSvcRecords (alias , iface .Address ().IP , false )
929+ n .deleteSvcRecords (alias , iface .Address ().IP , ipv6 , false )
920930 }
921931 }
922932 }
923933}
924934
925- func (n * network ) addSvcRecords (name string , epIP net.IP , ipMapUpdate bool ) {
935+ func addIPToName (ipMap map [string ]string , name string , ip net.IP ) {
936+ reverseIP := netutils .ReverseIP (ip .String ())
937+ if _ , ok := ipMap [reverseIP ]; ! ok {
938+ ipMap [reverseIP ] = name
939+ }
940+ }
941+
942+ func addNameToIP (svcMap map [string ][]net.IP , name string , epIP net.IP ) {
943+ ipList := svcMap [name ]
944+ for _ , ip := range ipList {
945+ if ip .Equal (epIP ) {
946+ return
947+ }
948+ }
949+ svcMap [name ] = append (svcMap [name ], epIP )
950+ }
951+
952+ func delNameToIP (svcMap map [string ][]net.IP , name string , epIP net.IP ) {
953+ ipList := svcMap [name ]
954+ for i , ip := range ipList {
955+ if ip .Equal (epIP ) {
956+ ipList = append (ipList [:i ], ipList [i + 1 :]... )
957+ break
958+ }
959+ }
960+ svcMap [name ] = ipList
961+
962+ if len (ipList ) == 0 {
963+ delete (svcMap , name )
964+ }
965+ }
966+
967+ func (n * network ) addSvcRecords (name string , epIP net.IP , epIPv6 net.IP , ipMapUpdate bool ) {
926968 c := n .getController ()
927969 c .Lock ()
928970 defer c .Unlock ()
929971 sr , ok := c .svcDb [n .ID ()]
930972 if ! ok {
931973 sr = svcInfo {
932- svcMap : make (map [string ][]net.IP ),
933- ipMap : make (map [string ]string ),
974+ svcMap : make (map [string ][]net.IP ),
975+ svcIPv6Map : make (map [string ][]net.IP ),
976+ ipMap : make (map [string ]string ),
934977 }
935978 c .svcDb [n .ID ()] = sr
936979 }
937980
938981 if ipMapUpdate {
939- reverseIP := netutils . ReverseIP ( epIP . String () )
940- if _ , ok := sr . ipMap [ reverseIP ]; ! ok {
941- sr .ipMap [ reverseIP ] = name
982+ addIPToName ( sr . ipMap , name , epIP )
983+ if epIPv6 != nil {
984+ addIPToName ( sr .ipMap , name , epIPv6 )
942985 }
943986 }
944987
945- ipList := sr .svcMap [name ]
946- for _ , ip := range ipList {
947- if ip .Equal (epIP ) {
948- return
949- }
988+ addNameToIP (sr .svcMap , name , epIP )
989+ if epIPv6 != nil {
990+ addNameToIP (sr .svcIPv6Map , name , epIPv6 )
950991 }
951- sr .svcMap [name ] = append (sr .svcMap [name ], epIP )
952992}
953993
954- func (n * network ) deleteSvcRecords (name string , epIP net.IP , ipMapUpdate bool ) {
994+ func (n * network ) deleteSvcRecords (name string , epIP net.IP , epIPv6 net. IP , ipMapUpdate bool ) {
955995 c := n .getController ()
956996 c .Lock ()
957997 defer c .Unlock ()
@@ -962,19 +1002,16 @@ func (n *network) deleteSvcRecords(name string, epIP net.IP, ipMapUpdate bool) {
9621002
9631003 if ipMapUpdate {
9641004 delete (sr .ipMap , netutils .ReverseIP (epIP .String ()))
965- }
9661005
967- ipList := sr .svcMap [name ]
968- for i , ip := range ipList {
969- if ip .Equal (epIP ) {
970- ipList = append (ipList [:i ], ipList [i + 1 :]... )
971- break
1006+ if epIPv6 != nil {
1007+ delete (sr .ipMap , netutils .ReverseIP (epIPv6 .String ()))
9721008 }
9731009 }
974- sr .svcMap [name ] = ipList
9751010
976- if len (ipList ) == 0 {
977- delete (sr .svcMap , name )
1011+ delNameToIP (sr .svcMap , name , epIP )
1012+
1013+ if epIPv6 != nil {
1014+ delNameToIP (sr .svcIPv6Map , name , epIPv6 )
9781015 }
9791016}
9801017
@@ -1033,6 +1070,10 @@ func (n *network) ipamAllocate() error {
10331070 }
10341071 }()
10351072
1073+ if ! n .enableIPv6 {
1074+ return nil
1075+ }
1076+
10361077 return n .ipamAllocateVersion (6 , ipam )
10371078}
10381079
@@ -1153,7 +1194,7 @@ func (n *network) ipamReleaseVersion(ipVer int, ipam ipamapi.Ipam) {
11531194 return
11541195 }
11551196
1156- if * infoList == nil {
1197+ if len ( * infoList ) == 0 {
11571198 return
11581199 }
11591200
0 commit comments