Browse code

libnet: Use Endpoint.dnsNames to create DNS records

Instead of special-casing anonymous endpoints, use the list of DNS names
associated to the endpoint.

`(*Endpoint).isAnonymous()` has no more uses, so let's delete it.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>

Albin Kerouanton authored on 2023/11/04 23:12:21
Showing 3 changed files
... ...
@@ -598,7 +598,7 @@ func (ep *Endpoint) deleteDriverInfoFromCluster() error {
598 598
 }
599 599
 
600 600
 func (ep *Endpoint) addServiceInfoToCluster(sb *Sandbox) error {
601
-	if len(ep.myAliases) == 0 && ep.isAnonymous() || ep.Iface() == nil || ep.Iface().Address() == nil {
601
+	if len(ep.dnsNames) == 0 || ep.Iface() == nil || ep.Iface().Address() == nil {
602 602
 		return nil
603 603
 	}
604 604
 
... ...
@@ -628,10 +628,8 @@ func (ep *Endpoint) addServiceInfoToCluster(sb *Sandbox) error {
628 628
 		return nil
629 629
 	}
630 630
 
631
-	name := ep.Name()
632
-	if ep.isAnonymous() {
633
-		name = ep.MyAliases()[0]
634
-	}
631
+	dnsNames := ep.getDNSNames()
632
+	primaryDNSName, dnsAliases := dnsNames[0], dnsNames[1:]
635 633
 
636 634
 	var ingressPorts []*PortConfig
637 635
 	if ep.svcID != "" {
... ...
@@ -640,24 +638,24 @@ func (ep *Endpoint) addServiceInfoToCluster(sb *Sandbox) error {
640 640
 		if n.ingress {
641 641
 			ingressPorts = ep.ingressPorts
642 642
 		}
643
-		if err := n.getController().addServiceBinding(ep.svcName, ep.svcID, n.ID(), ep.ID(), name, ep.virtualIP, ingressPorts, ep.svcAliases, ep.myAliases, ep.Iface().Address().IP, "addServiceInfoToCluster"); err != nil {
643
+		if err := n.getController().addServiceBinding(ep.svcName, ep.svcID, n.ID(), ep.ID(), primaryDNSName, ep.virtualIP, ingressPorts, ep.svcAliases, dnsAliases, ep.Iface().Address().IP, "addServiceInfoToCluster"); err != nil {
644 644
 			return err
645 645
 		}
646 646
 	} else {
647 647
 		// This is a container simply attached to an attachable network
648
-		if err := n.getController().addContainerNameResolution(n.ID(), ep.ID(), name, ep.myAliases, ep.Iface().Address().IP, "addServiceInfoToCluster"); err != nil {
648
+		if err := n.getController().addContainerNameResolution(n.ID(), ep.ID(), primaryDNSName, dnsAliases, ep.Iface().Address().IP, "addServiceInfoToCluster"); err != nil {
649 649
 			return err
650 650
 		}
651 651
 	}
652 652
 
653 653
 	buf, err := proto.Marshal(&EndpointRecord{
654
-		Name:            name,
654
+		Name:            primaryDNSName,
655 655
 		ServiceName:     ep.svcName,
656 656
 		ServiceID:       ep.svcID,
657 657
 		VirtualIP:       ep.virtualIP.String(),
658 658
 		IngressPorts:    ingressPorts,
659 659
 		Aliases:         ep.svcAliases,
660
-		TaskAliases:     ep.myAliases,
660
+		TaskAliases:     dnsAliases,
661 661
 		EndpointIP:      ep.Iface().Address().IP.String(),
662 662
 		ServiceDisabled: false,
663 663
 	})
... ...
@@ -676,7 +674,7 @@ func (ep *Endpoint) addServiceInfoToCluster(sb *Sandbox) error {
676 676
 }
677 677
 
678 678
 func (ep *Endpoint) deleteServiceInfoFromCluster(sb *Sandbox, fullRemove bool, method string) error {
679
-	if len(ep.myAliases) == 0 && ep.isAnonymous() {
679
+	if len(ep.dnsNames) == 0 {
680 680
 		return nil
681 681
 	}
682 682
 
... ...
@@ -699,10 +697,8 @@ func (ep *Endpoint) deleteServiceInfoFromCluster(sb *Sandbox, fullRemove bool, m
699 699
 		return nil
700 700
 	}
701 701
 
702
-	name := ep.Name()
703
-	if ep.isAnonymous() {
704
-		name = ep.MyAliases()[0]
705
-	}
702
+	dnsNames := ep.getDNSNames()
703
+	primaryDNSName, dnsAliases := dnsNames[0], dnsNames[1:]
706 704
 
707 705
 	// First update the networkDB then locally
708 706
 	if fullRemove {
... ...
@@ -720,12 +716,12 @@ func (ep *Endpoint) deleteServiceInfoFromCluster(sb *Sandbox, fullRemove bool, m
720 720
 			if n.ingress {
721 721
 				ingressPorts = ep.ingressPorts
722 722
 			}
723
-			if err := n.getController().rmServiceBinding(ep.svcName, ep.svcID, n.ID(), ep.ID(), name, ep.virtualIP, ingressPorts, ep.svcAliases, ep.myAliases, ep.Iface().Address().IP, "deleteServiceInfoFromCluster", true, fullRemove); err != nil {
723
+			if err := n.getController().rmServiceBinding(ep.svcName, ep.svcID, n.ID(), ep.ID(), primaryDNSName, ep.virtualIP, ingressPorts, ep.svcAliases, dnsAliases, ep.Iface().Address().IP, "deleteServiceInfoFromCluster", true, fullRemove); err != nil {
724 724
 				return err
725 725
 			}
726 726
 		} else {
727 727
 			// This is a container simply attached to an attachable network
728
-			if err := n.getController().delContainerNameResolution(n.ID(), ep.ID(), name, ep.myAliases, ep.Iface().Address().IP, "deleteServiceInfoFromCluster"); err != nil {
728
+			if err := n.getController().delContainerNameResolution(n.ID(), ep.ID(), primaryDNSName, dnsAliases, ep.Iface().Address().IP, "deleteServiceInfoFromCluster"); err != nil {
729 729
 				return err
730 730
 			}
731 731
 		}
... ...
@@ -308,10 +308,15 @@ func (ep *Endpoint) Network() string {
308 308
 	return ep.network.name
309 309
 }
310 310
 
311
-func (ep *Endpoint) isAnonymous() bool {
311
+// getDNSNames returns a copy of the DNS names associated to this endpoint. The first entry is the one used for PTR
312
+// records.
313
+func (ep *Endpoint) getDNSNames() []string {
312 314
 	ep.mu.Lock()
313 315
 	defer ep.mu.Unlock()
314
-	return ep.anonymous
316
+
317
+	dnsNames := make([]string, len(ep.dnsNames))
318
+	copy(dnsNames, ep.dnsNames)
319
+	return dnsNames
315 320
 }
316 321
 
317 322
 // isServiceEnabled check if service is enabled on the endpoint
... ...
@@ -1302,8 +1302,6 @@ func (n *Network) updateSvcRecord(ep *Endpoint, isAdd bool) {
1302 1302
 	}
1303 1303
 
1304 1304
 	var ipv6 net.IP
1305
-	epName := ep.Name()
1306
-	myAliases := ep.MyAliases()
1307 1305
 	if iface.AddressIPv6() != nil {
1308 1306
 		ipv6 = iface.AddressIPv6().IP
1309 1307
 	}
... ...
@@ -1312,30 +1310,17 @@ func (n *Network) updateSvcRecord(ep *Endpoint, isAdd bool) {
1312 1312
 	if serviceID == "" {
1313 1313
 		serviceID = ep.ID()
1314 1314
 	}
1315
+
1316
+	dnsNames := ep.getDNSNames()
1315 1317
 	if isAdd {
1316
-		// If anonymous endpoint has an alias use the first alias
1317
-		// for ip->name mapping. Not having the reverse mapping
1318
-		// breaks some apps
1319
-		if ep.isAnonymous() {
1320
-			if len(myAliases) > 0 {
1321
-				n.addSvcRecords(ep.ID(), myAliases[0], serviceID, iface.Address().IP, ipv6, true, "updateSvcRecord")
1322
-			}
1323
-		} else {
1324
-			n.addSvcRecords(ep.ID(), epName, serviceID, iface.Address().IP, ipv6, true, "updateSvcRecord")
1325
-		}
1326
-		for _, alias := range myAliases {
1327
-			n.addSvcRecords(ep.ID(), alias, serviceID, iface.Address().IP, ipv6, false, "updateSvcRecord")
1318
+		for i, dnsName := range dnsNames {
1319
+			ipMapUpdate := i == 0 // ipMapUpdate indicates whether PTR records should be updated.
1320
+			n.addSvcRecords(ep.ID(), dnsName, serviceID, iface.Address().IP, ipv6, ipMapUpdate, "updateSvcRecord")
1328 1321
 		}
1329 1322
 	} else {
1330
-		if ep.isAnonymous() {
1331
-			if len(myAliases) > 0 {
1332
-				n.deleteSvcRecords(ep.ID(), myAliases[0], serviceID, iface.Address().IP, ipv6, true, "updateSvcRecord")
1333
-			}
1334
-		} else {
1335
-			n.deleteSvcRecords(ep.ID(), epName, serviceID, iface.Address().IP, ipv6, true, "updateSvcRecord")
1336
-		}
1337
-		for _, alias := range myAliases {
1338
-			n.deleteSvcRecords(ep.ID(), alias, serviceID, iface.Address().IP, ipv6, false, "updateSvcRecord")
1323
+		for i, dnsName := range dnsNames {
1324
+			ipMapUpdate := i == 0 // ipMapUpdate indicates whether PTR records should be updated.
1325
+			n.deleteSvcRecords(ep.ID(), dnsName, serviceID, iface.Address().IP, ipv6, ipMapUpdate, "updateSvcRecord")
1339 1326
 		}
1340 1327
 	}
1341 1328
 }
... ...
@@ -1374,6 +1359,7 @@ func delNameToIP(svcMap *setmatrix.SetMatrix[svcMapEntry], name, serviceID strin
1374 1374
 	})
1375 1375
 }
1376 1376
 
1377
+// TODO(aker): remove ipMapUpdate param and add a proper method dedicated to update PTR records.
1377 1378
 func (n *Network) addSvcRecords(eID, name, serviceID string, epIP, epIPv6 net.IP, ipMapUpdate bool, method string) {
1378 1379
 	// Do not add service names for ingress network as this is a
1379 1380
 	// routing only network