-
Notifications
You must be signed in to change notification settings - Fork 0
/
nft.go
66 lines (53 loc) · 1.08 KB
/
nft.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"flag"
"log"
"os"
"os/exec"
)
var (
nftEnabled = flag.Bool("nft", false, "enable NFT basic packet filter (pods SNAT...)")
nftMasqOif = flag.String("nft-masq-oif", "eth0", "output interface match to masquerade (ie: \"eth0\", \"{eth0, eth1}\", etc)")
)
func setupNFT(podCIDRs []string) {
if !*nftEnabled {
return
}
var err error
defer func() {
if err != nil {
log.Print("nft failed: ", err)
}
}()
nft := exec.Command("nft", "-f", "-")
nft.Stdout = os.Stdout
nft.Stderr = os.Stderr
nftIn, err := nft.StdinPipe()
if err != nil {
return
}
err = nft.Start()
if err != nil {
return
}
write := func(s string) {
if err != nil {
return
}
_, err = nftIn.Write([]byte(s))
}
write(`
table ip knet_wg;
delete table ip knet_wg;
table ip knet_wg {
chain hook_postrouting {
type nat hook postrouting priority 999;
fib saddr type local oif "` + *ifName + `" masquerade;
`)
for _, cidr := range podCIDRs {
write(" ip saddr " + cidr + " oif " + *nftMasqOif + " masquerade;\n")
}
write(" }\n}")
nftIn.Close()
err = nft.Wait()
}