Browse code

libnetwork: extract plumpIngressProxy steps in a separate function

- Extract plumpIngressProxy steps in a separate function
- Don't create a new listener if there's already one in ingressProxyTbl

Signed-off-by: Andrey Epifanov <aepifanov@mirantis.com>

Andrey Epifanov authored on 2025/06/02 07:01:23
Showing 1 changed files
... ...
@@ -402,7 +402,13 @@ func programIngress(gwIP net.IP, ingressPorts []*PortConfig, isDelete bool) erro
402 402
 	// Filter the ingress ports until port rules start to be added/deleted
403 403
 	filteredPorts := filterPortConfigs(ingressPorts, isDelete)
404 404
 
405
-	return programIngressPorts(gwIP, filteredPorts, iptable, isDelete)
405
+	if err := programIngressPorts(gwIP, filteredPorts, iptable, isDelete); err != nil {
406
+		return fmt.Errorf("failed to program ingress ports: %v", err)
407
+	}
408
+
409
+	plumbIngressPortsProxy(filteredPorts, isDelete)
410
+
411
+	return nil
406 412
 }
407 413
 
408 414
 func programIngressPorts(gwIP net.IP, filteredPorts []*PortConfig, iptable *iptables.IPTable, isDelete bool) error {
... ...
@@ -472,12 +478,18 @@ func programIngressPorts(gwIP net.IP, filteredPorts []*PortConfig, iptable *ipta
472 472
 		rollbackRule = []string{rollbackAddDelOpt, ingressChain, "-p", protocol, "--dport", publishedPort, "-j", "ACCEPT"}
473 473
 		rollbackRules = append(rollbackRules, rollbackRule)
474 474
 
475
+	}
476
+
477
+	return nil
478
+}
479
+
480
+func plumbIngressPortsProxy(ingressPorts []*PortConfig, isDelete bool) {
481
+	for _, iPort := range ingressPorts {
482
+		publishedPort := strconv.FormatUint(uint64(iPort.PublishedPort), 10)
475 483
 		if err := plumbProxy(iPort, isDelete); err != nil {
476 484
 			log.G(context.TODO()).Warnf("failed to create proxy for port %s: %v", publishedPort, err)
477 485
 		}
478 486
 	}
479
-
480
-	return nil
481 487
 }
482 488
 
483 489
 func findOIFName(ip net.IP) (string, error) {
... ...
@@ -509,13 +521,17 @@ func plumbProxy(iPort *PortConfig, isDelete bool) error {
509 509
 	)
510 510
 
511 511
 	portSpec := fmt.Sprintf("%d/%s", iPort.PublishedPort, strings.ToLower(PortConfig_Protocol_name[int32(iPort.Protocol)]))
512
+	listener := ingressProxyTbl[portSpec]
512 513
 	if isDelete {
513
-		if listener, ok := ingressProxyTbl[portSpec]; ok {
514
-			if listener != nil {
515
-				listener.Close()
516
-			}
514
+		if listener != nil {
515
+			listener.Close()
517 516
 		}
517
+		delete(ingressProxyTbl, portSpec)
518
+
519
+		return nil
520
+	}
518 521
 
522
+	if listener != nil {
519 523
 		return nil
520 524
 	}
521 525