Skip to content

Commit 1478527

Browse files
author
Murali Reddy
committed
setup masquerade rule for traffic destined for outside of cluster and pod network
change added iptable rule in NAT table POSTROUTING chain to masqurade outbound traffic from the pods. Fixes #8
1 parent c9bc18e commit 1478527

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Alternatively you can download the prebuilt binary from https://github.com/cloud
3737
--run-service-proxy If false, kube-router won't setup IPVS for services proxy. true by default.
3838
--cleanup-config If true cleanup iptables rules, ipvs, ipset configuration and exit.
3939
--masquerade-all SNAT all traffic to cluster IP/node port. False by default
40+
--cluster-cidr CIDR range of pods in the cluster. If specified external traffic from the pods will be masquraded
4041
--config-sync-period duration How often configuration from the apiserver is refreshed. Must be greater than 0. (default 1m0s)
4142
--iptables-sync-period duration The maximum interval of how often iptables rules are refreshed (e.g. '5s', '1m'). Must be greater than 0. (default 1m0s)
4243
--ipvs-sync-period duration The maximum interval of how often ipvs config is refreshed (e.g. '5s', '1m', '2h22m'). Must be greater than 0. (default 1m0s)

app/controllers/network_routes_controller.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/cloudnativelabs/kube-router/app/options"
1313
"github.com/cloudnativelabs/kube-router/app/watchers"
1414
"github.com/cloudnativelabs/kube-router/utils"
15+
"github.com/coreos/go-iptables/iptables"
1516
"github.com/golang/glog"
1617
bgpapi "github.com/osrg/gobgp/api"
1718
"github.com/osrg/gobgp/config"
@@ -34,6 +35,7 @@ type NetworkRoutingController struct {
3435
peerRouter string
3536
asnNumber uint32
3637
peerAsnNumber uint32
38+
clusterCIDR string
3739
}
3840
var(
3941
activeNodes = make(map[string]bool)
@@ -79,6 +81,18 @@ func (nrc *NetworkRoutingController) Run(stopCh <-chan struct{}, wg *sync.WaitGr
7981
}
8082
}
8183

84+
if len(nrc.clusterCIDR) != 0 {
85+
args := []string{"-s", nrc.clusterCIDR, "!", "-d", nrc.clusterCIDR, "-j", "MASQUERADE"}
86+
iptablesCmdHandler, err := iptables.New()
87+
if err != nil {
88+
glog.Errorf("Failed to add iptable rule to masqurade outbound traffic from pods due to %s. External connectivity will not work.", err.Error())
89+
}
90+
err = iptablesCmdHandler.AppendUnique("nat", "POSTROUTING", args...)
91+
if err != nil {
92+
glog.Errorf("Failed to add iptable rule to masqurade outbound traffic from pods due to %s. External connectivity will not work.", err.Error())
93+
}
94+
}
95+
8296
// loop forever till notified to stop on stopCh
8397
for {
8498
select {
@@ -305,6 +319,7 @@ func NewNetworkRoutingController(clientset *kubernetes.Clientset, kubeRouterConf
305319

306320
nrc := NetworkRoutingController{}
307321

322+
nrc.clusterCIDR = kubeRouterConfig.ClusterCIDR
308323
nrc.syncPeriod = kubeRouterConfig.RoutesSyncPeriod
309324
nrc.clientset = clientset
310325

app/options/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type KubeRouterConfig struct {
1818
RunFirewall bool
1919
RunRouter bool
2020
MasqueradeAll bool
21+
ClusterCIDR string
2122
AdvertiseClusterIp bool
2223
PeerRouter string
2324
ClusterAsn string
@@ -44,6 +45,7 @@ func (s *KubeRouterConfig) AddFlags(fs *pflag.FlagSet) {
4445
fs.StringVar(&s.Kubeconfig, "kubeconfig", s.Kubeconfig, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
4546
fs.BoolVar(&s.CleanupConfig, "cleanup-config", s.CleanupConfig, "If true cleanup iptables rules, ipvs, ipset configuration and exit.")
4647
fs.BoolVar(&s.MasqueradeAll, "masquerade-all", s.MasqueradeAll, "SNAT all traffic to cluster IP/node port. False by default")
48+
fs.StringVar(&s.ClusterCIDR, "cluster-cidr", s.ClusterCIDR, "CIDR range of pods in the cluster. It is used to identify traffic originating from and destinated to pods.")
4749
fs.DurationVar(&s.ConfigSyncPeriod, "config-sync-period", s.ConfigSyncPeriod, "How often configuration from the apiserver is refreshed. Must be greater than 0.")
4850
fs.DurationVar(&s.IPTablesSyncPeriod, "iptables-sync-period", s.IPTablesSyncPeriod, "The maximum interval of how often iptables rules are refreshed (e.g. '5s', '1m'). Must be greater than 0.")
4951
fs.DurationVar(&s.IpvsSyncPeriod, "ipvs-sync-period", s.IpvsSyncPeriod, "The maximum interval of how often ipvs config is refreshed (e.g. '5s', '1m', '2h22m'). Must be greater than 0.")

0 commit comments

Comments
 (0)