Skip to content

Commit 9bc251e

Browse files
committed
Use correct destination format for IPv6 DNAT rules
Signed-off-by: Antonin Bas <[email protected]>
1 parent 9560ed0 commit 9bc251e

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

pkg/agent/nodeportlocal/rules/iptable_rule.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package rules
2020
import (
2121
"bytes"
2222
"fmt"
23+
"net"
2324

2425
"k8s.io/klog/v2"
2526

@@ -95,7 +96,7 @@ func buildRuleForPod(port int, podIP, protocol string) []string {
9596

9697
// AddRule appends a DNAT rule in NodePortLocalChain chain of NAT table.
9798
func (ipt *iptablesRules) AddRule(nodePort int, podIP string, podPort int, protocol string) error {
98-
podAddr := fmt.Sprintf("%s:%d", podIP, podPort)
99+
podAddr := net.JoinHostPort(podIP, fmt.Sprint(podPort))
99100
rule := buildRuleForPod(nodePort, podAddr, protocol)
100101
if err := ipt.table.AppendRule(ipt.protocol, iptables.NATTable, NodePortLocalChain, rule); err != nil {
101102
return err

test/e2e/nodeportlocal_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"encoding/json"
2121
"fmt"
22+
"net"
2223
"regexp"
2324
"strconv"
2425
"strings"
@@ -202,7 +203,7 @@ func checkNPLRulesForWindowsPod(t *testing.T, data *TestData, r *require.Asserti
202203
func buildRuleForPod(rule nplRuleData) []string {
203204
return []string{
204205
"-p", rule.protocol, "-m", rule.protocol, "--dport", fmt.Sprint(rule.nodePort),
205-
"-j", "DNAT", "--to-destination", fmt.Sprintf("%s:%d", rule.podIP, rule.podPort),
206+
"-j", "DNAT", "--to-destination", net.JoinHostPort(rule.podIP, fmt.Sprint(rule.podPort)),
206207
}
207208
}
208209

0 commit comments

Comments
 (0)