@@ -1329,11 +1329,14 @@ func (n *Network) updateSvcRecord(ctx context.Context, ep *Endpoint, isAdd bool)
13291329 defer span .End ()
13301330
13311331 iface := ep .Iface ()
1332- if iface == nil || iface . Address () == nil {
1332+ if iface == nil {
13331333 return
13341334 }
13351335
1336- var ipv6 net.IP
1336+ var ipv4 , ipv6 net.IP
1337+ if iface .Address () != nil {
1338+ ipv4 = iface .Address ().IP
1339+ }
13371340 if iface .AddressIPv6 () != nil {
13381341 ipv6 = iface .AddressIPv6 ().IP
13391342 }
@@ -1347,12 +1350,12 @@ func (n *Network) updateSvcRecord(ctx context.Context, ep *Endpoint, isAdd bool)
13471350 if isAdd {
13481351 for i , dnsName := range dnsNames {
13491352 ipMapUpdate := i == 0 // ipMapUpdate indicates whether PTR records should be updated.
1350- n .addSvcRecords (ep .ID (), dnsName , serviceID , iface . Address (). IP , ipv6 , ipMapUpdate , "updateSvcRecord" )
1353+ n .addSvcRecords (ep .ID (), dnsName , serviceID , ipv4 , ipv6 , ipMapUpdate , "updateSvcRecord" )
13511354 }
13521355 } else {
13531356 for i , dnsName := range dnsNames {
13541357 ipMapUpdate := i == 0 // ipMapUpdate indicates whether PTR records should be updated.
1355- n .deleteSvcRecords (ep .ID (), dnsName , serviceID , iface . Address (). IP , ipv6 , ipMapUpdate , "updateSvcRecord" )
1358+ n .deleteSvcRecords (ep .ID (), dnsName , serviceID , ipv4 , ipv6 , ipMapUpdate , "updateSvcRecord" )
13561359 }
13571360 }
13581361}
@@ -1392,14 +1395,14 @@ func delNameToIP(svcMap *setmatrix.SetMatrix[svcMapEntry], name, serviceID strin
13921395}
13931396
13941397// TODO(aker): remove ipMapUpdate param and add a proper method dedicated to update PTR records.
1395- func (n * Network ) addSvcRecords (eID , name , serviceID string , epIP , epIPv6 net.IP , ipMapUpdate bool , method string ) {
1398+ func (n * Network ) addSvcRecords (eID , name , serviceID string , epIPv4 , epIPv6 net.IP , ipMapUpdate bool , method string ) {
13961399 // Do not add service names for ingress network as this is a
13971400 // routing only network
13981401 if n .ingress {
13991402 return
14001403 }
14011404 networkID := n .ID ()
1402- log .G (context .TODO ()).Debugf ("%s (%.7s).addSvcRecords(%s, %s, %s, %t) %s sid:%s" , eID , networkID , name , epIP , epIPv6 , ipMapUpdate , method , serviceID )
1405+ log .G (context .TODO ()).Debugf ("%s (%.7s).addSvcRecords(%s, %s, %s, %t) %s sid:%s" , eID , networkID , name , epIPv4 , epIPv6 , ipMapUpdate , method , serviceID )
14031406
14041407 c := n .getController ()
14051408 c .mu .Lock ()
@@ -1412,26 +1415,30 @@ func (n *Network) addSvcRecords(eID, name, serviceID string, epIP, epIPv6 net.IP
14121415 }
14131416
14141417 if ipMapUpdate {
1415- addIPToName (& sr .ipMap , name , serviceID , epIP )
1418+ if epIPv4 != nil {
1419+ addIPToName (& sr .ipMap , name , serviceID , epIPv4 )
1420+ }
14161421 if epIPv6 != nil {
14171422 addIPToName (& sr .ipMap , name , serviceID , epIPv6 )
14181423 }
14191424 }
14201425
1421- addNameToIP (& sr .svcMap , name , serviceID , epIP )
1426+ if epIPv4 != nil {
1427+ addNameToIP (& sr .svcMap , name , serviceID , epIPv4 )
1428+ }
14221429 if epIPv6 != nil {
14231430 addNameToIP (& sr .svcIPv6Map , name , serviceID , epIPv6 )
14241431 }
14251432}
14261433
1427- func (n * Network ) deleteSvcRecords (eID , name , serviceID string , epIP net. IP , epIPv6 net.IP , ipMapUpdate bool , method string ) {
1434+ func (n * Network ) deleteSvcRecords (eID , name , serviceID string , epIPv4 , epIPv6 net.IP , ipMapUpdate bool , method string ) {
14281435 // Do not delete service names from ingress network as this is a
14291436 // routing only network
14301437 if n .ingress {
14311438 return
14321439 }
14331440 networkID := n .ID ()
1434- log .G (context .TODO ()).Debugf ("%s (%.7s).deleteSvcRecords(%s, %s, %s, %t) %s sid:%s " , eID , networkID , name , epIP , epIPv6 , ipMapUpdate , method , serviceID )
1441+ log .G (context .TODO ()).Debugf ("%s (%.7s).deleteSvcRecords(%s, %s, %s, %t) %s sid:%s " , eID , networkID , name , epIPv4 , epIPv6 , ipMapUpdate , method , serviceID )
14351442
14361443 c := n .getController ()
14371444 c .mu .Lock ()
@@ -1443,15 +1450,17 @@ func (n *Network) deleteSvcRecords(eID, name, serviceID string, epIP net.IP, epI
14431450 }
14441451
14451452 if ipMapUpdate {
1446- delIPToName (& sr .ipMap , name , serviceID , epIP )
1447-
1453+ if epIPv4 != nil {
1454+ delIPToName (& sr .ipMap , name , serviceID , epIPv4 )
1455+ }
14481456 if epIPv6 != nil {
14491457 delIPToName (& sr .ipMap , name , serviceID , epIPv6 )
14501458 }
14511459 }
14521460
1453- delNameToIP (& sr .svcMap , name , serviceID , epIP )
1454-
1461+ if epIPv4 != nil {
1462+ delNameToIP (& sr .svcMap , name , serviceID , epIPv4 )
1463+ }
14551464 if epIPv6 != nil {
14561465 delNameToIP (& sr .svcIPv6Map , name , serviceID , epIPv6 )
14571466 }
@@ -1476,27 +1485,29 @@ func (n *Network) getSvcRecords(ep *Endpoint) []etchosts.Record {
14761485 return nil
14771486 }
14781487
1479- svcMapKeys := sr .svcMap .Keys ()
1480- // Loop on service names on this network
1481- for _ , k := range svcMapKeys {
1482- if strings .Split (k , "." )[0 ] == epName {
1483- continue
1484- }
1485- // Get all the IPs associated to this service
1486- mapEntryList , ok := sr .svcMap .Get (k )
1487- if ! ok {
1488- // The key got deleted
1489- continue
1490- }
1491- if len (mapEntryList ) == 0 {
1492- log .G (context .TODO ()).Warnf ("Found empty list of IP addresses for service %s on network %s (%s)" , k , n .name , n .id )
1493- continue
1494- }
1488+ for _ , svcMap := range []* setmatrix.SetMatrix [svcMapEntry ]{& sr .svcMap , & sr .svcIPv6Map } {
1489+ svcMapKeys := svcMap .Keys ()
1490+ // Loop on service names on this network
1491+ for _ , k := range svcMapKeys {
1492+ if strings .Split (k , "." )[0 ] == epName {
1493+ continue
1494+ }
1495+ // Get all the IPs associated to this service
1496+ mapEntryList , ok := svcMap .Get (k )
1497+ if ! ok {
1498+ // The key got deleted
1499+ continue
1500+ }
1501+ if len (mapEntryList ) == 0 {
1502+ log .G (context .TODO ()).Warnf ("Found empty list of IP addresses for service %s on network %s (%s)" , k , n .name , n .id )
1503+ continue
1504+ }
14951505
1496- recs = append (recs , etchosts.Record {
1497- Hosts : k ,
1498- IP : mapEntryList [0 ].ip ,
1499- })
1506+ recs = append (recs , etchosts.Record {
1507+ Hosts : k ,
1508+ IP : mapEntryList [0 ].ip ,
1509+ })
1510+ }
15001511 }
15011512
15021513 return recs
0 commit comments