Skip to content

Commit

Permalink
Add --random-fully=true|false flag to ip-masq-agent
Browse files Browse the repository at this point in the history
Defaults to true to keep the behavior added in 2.10.0 unchanged.
  • Loading branch information
jingyuanliang committed Jan 30, 2024
1 parent b0ce495 commit b67c8c2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
7 changes: 6 additions & 1 deletion cmd/ip-masq-agent/ip-masq-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var (
masqChainFlag = flag.String("masq-chain", "IP-MASQ-AGENT", `Name of nat chain for iptables masquerade rules.`)
noMasqueradeAllReservedRangesFlag = flag.Bool("nomasq-all-reserved-ranges", false, "Whether to disable masquerade for all IPv4 ranges reserved by RFCs.")
enableIPv6 = flag.Bool("enable-ipv6", false, "Whether to enable IPv6.")
randomFully = flag.Bool("random-fully", true, "Whether to add --random-fully to the masquerade rule.")
)

// MasqConfig object
Expand Down Expand Up @@ -384,7 +385,11 @@ func writeNonMasqRule(lines *bytes.Buffer, cidr string) {
const masqRuleComment = `-m comment --comment "ip-masq-agent: outbound traffic is subject to MASQUERADE (must be last in chain)"`

func writeMasqRule(lines *bytes.Buffer) {
writeRule(lines, utiliptables.Append, masqChain, masqRuleComment, "-j", "MASQUERADE", "--random-fully")
args := []string{masqRuleComment, "-j", "MASQUERADE"}
if *randomFully {
args = append(args, "--random-fully")
}
writeRule(lines, utiliptables.Append, masqChain, args...)
}

// Similar syntax to utiliptables.Interface.EnsureRule, except you don't pass a table
Expand Down
43 changes: 35 additions & 8 deletions cmd/ip-masq-agent/ip-masq-agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,38 @@ import (
iptest "k8s.io/kubernetes/pkg/util/iptables/testing"
)

var wantRandomFully string

// turn off glog logging during tests to avoid clutter in output
func TestMain(m *testing.M) {
flag.Set("logtostderr", "false")
flag.Set("masq-chain", "IP-MASQ-AGENT")
ec := m.Run()

ec := 0
randomFully := " --random-fully"

for _, tc := range []struct{
arg string
want string
}{
{
want: randomFully,
},
{
arg: "false",
},
{
arg: "true",
want: randomFully,
},
} {
if tc.arg != "" {
flag.Set("random-fully", tc.arg)
}
wantRandomFully = tc.want

ec = max(ec, m.Run())
}
os.Exit(ec)
}

Expand Down Expand Up @@ -282,7 +309,7 @@ func TestSyncMasqRules(t *testing.T) {
-A ` + string(utiliptables.ChainPostrouting) + ` -m comment --comment ` +
fmt.Sprintf(postRoutingMasqChainCommentFormat, masqChain) + ` -m addrtype ! --dst-type LOCAL -j ` + string(masqChain) + `
-A ` + string(masqChain) + ` ` + nonMasqRuleComment + ` -d 169.254.0.0/16 -j RETURN
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE --random-fully
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE` + wantRandomFully + `
COMMIT
`,
},
Expand All @@ -298,7 +325,7 @@ COMMIT
-A ` + string(masqChain) + ` ` + nonMasqRuleComment + ` -d 10.0.0.0/8 -j RETURN
-A ` + string(masqChain) + ` ` + nonMasqRuleComment + ` -d 172.16.0.0/12 -j RETURN
-A ` + string(masqChain) + ` ` + nonMasqRuleComment + ` -d 192.168.0.0/16 -j RETURN
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE --random-fully
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE` + wantRandomFully + `
COMMIT
`,
},
Expand All @@ -322,7 +349,7 @@ COMMIT
-A ` + string(masqChain) + ` ` + nonMasqRuleComment + ` -d 198.51.100.0/24 -j RETURN
-A ` + string(masqChain) + ` ` + nonMasqRuleComment + ` -d 203.0.113.0/24 -j RETURN
-A ` + string(masqChain) + ` ` + nonMasqRuleComment + ` -d 240.0.0.0/4 -j RETURN
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE --random-fully
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE` + wantRandomFully + `
COMMIT
`,
},
Expand All @@ -341,7 +368,7 @@ COMMIT
fmt.Sprintf(postRoutingMasqChainCommentFormat, masqChain) + ` -m addrtype ! --dst-type LOCAL -j ` + string(masqChain) + `
-A ` + string(masqChain) + ` ` + nonMasqRuleComment + ` -d 169.254.0.0/16 -j RETURN
-A ` + string(masqChain) + ` ` + nonMasqRuleComment + ` -d 10.244.0.0/16 -j RETURN
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE --random-fully
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE` + wantRandomFully + `
COMMIT
`,
},
Expand Down Expand Up @@ -383,7 +410,7 @@ func TestSyncMasqRulesIPv6(t *testing.T) {
-A ` + string(utiliptables.ChainPostrouting) + ` -m comment --comment ` +
fmt.Sprintf(postRoutingMasqChainCommentFormat, masqChain) + ` -m addrtype ! --dst-type LOCAL -j ` + string(masqChain) + `
-A ` + string(masqChain) + ` ` + nonMasqRuleComment + ` -d fe80::/10 -j RETURN
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE --random-fully
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE` + wantRandomFully + `
COMMIT
`,
},
Expand All @@ -402,7 +429,7 @@ COMMIT
fmt.Sprintf(postRoutingMasqChainCommentFormat, masqChain) + ` -m addrtype ! --dst-type LOCAL -j ` + string(masqChain) + `
-A ` + string(masqChain) + ` ` + nonMasqRuleComment + ` -d fe80::/10 -j RETURN
-A ` + string(masqChain) + ` ` + nonMasqRuleComment + ` -d fc00::/7 -j RETURN
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE --random-fully
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE` + wantRandomFully + `
COMMIT
`,
},
Expand All @@ -414,7 +441,7 @@ COMMIT
:` + string(masqChain) + ` - [0:0]
-A ` + string(utiliptables.ChainPostrouting) + ` -m comment --comment ` +
fmt.Sprintf(postRoutingMasqChainCommentFormat, masqChain) + ` -m addrtype ! --dst-type LOCAL -j ` + string(masqChain) + `
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE --random-fully
-A ` + string(masqChain) + ` ` + masqRuleComment + ` -j MASQUERADE` + wantRandomFully + `
COMMIT
`,
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module k8s.io/ip-masq-agent

go 1.20
go 1.21

require (
github.com/golang/glog v1.1.2
Expand Down

0 comments on commit b67c8c2

Please sign in to comment.