@@ -54,14 +54,16 @@ const (
5454
5555// NetworkPolicyController strcut to hold information required by NetworkPolicyController
5656type NetworkPolicyController struct {
57- nodeIP net.IP
58- nodeHostName string
59- mu sync.Mutex
60- syncPeriod time.Duration
61- MetricsEnabled bool
62- v1NetworkPolicy bool
63- healthChan chan <- * healthcheck.ControllerHeartbeat
64- fullSyncRequestChan chan struct {}
57+ nodeIP net.IP
58+ nodeHostName string
59+ serviceClusterIPRange string
60+ serviceNodePortRange string
61+ mu sync.Mutex
62+ syncPeriod time.Duration
63+ MetricsEnabled bool
64+ v1NetworkPolicy bool
65+ healthChan chan <- * healthcheck.ControllerHeartbeat
66+ fullSyncRequestChan chan struct {}
6567
6668 ipSetHandler * utils.IPSet
6769
@@ -197,48 +199,65 @@ func (npc *NetworkPolicyController) ensureTopLevelChains() {
197199 glog .Fatalf ("Failed to initialize iptables executor due to %s" , err .Error ())
198200 }
199201
200- chains := map [string ]string {"INPUT" : kubeInputChainName , "FORWARD" : kubeForwardChainName , "OUTPUT" : kubeOutputChainName }
201-
202- for builtinChain , customChain := range chains {
203- err = iptablesCmdHandler .NewChain ("filter" , customChain )
204- if err != nil && err .(* iptables.Error ).ExitStatus () != 1 {
205- glog .Fatalf ("Failed to run iptables command to create %s chain due to %s" , customChain , err .Error ())
202+ ensureRuleAtposition := func (chain string , ruleSpec []string , position int ) {
203+ rules , err := iptablesCmdHandler .List ("filter" , chain )
204+ if err != nil {
205+ glog .Fatalf ("failed to list rules in filter table %s chain due to %s" , chain , err .Error ())
206206 }
207- args := [] string { "-m" , "comment" , "--comment" , "kube-router netpol" , "-j" , customChain }
208- exists , err := iptablesCmdHandler .Exists ("filter" , builtinChain , args ... )
207+
208+ exists , err := iptablesCmdHandler .Exists ("filter" , chain , ruleSpec ... )
209209 if err != nil {
210- glog .Fatalf ("Failed to verify rule exists to jump to chain %s in %s chain due to %s" , customChain , builtinChain , err .Error ())
210+ glog .Fatalf ("Failed to verify rule exists in %s chain due to %s" , chain , err .Error ())
211211 }
212212 if ! exists {
213- err := iptablesCmdHandler .Insert ("filter" , builtinChain , 1 , args ... )
213+ err := iptablesCmdHandler .Insert ("filter" , chain , position , ruleSpec ... )
214214 if err != nil {
215- glog .Fatalf ("Failed to run iptables command to insert in %s chain %s" , builtinChain , err .Error ())
215+ glog .Fatalf ("Failed to run iptables command to insert in %s chain %s" , chain , err .Error ())
216216 }
217- } else {
218- rules , err := iptablesCmdHandler .List ("filter" , builtinChain )
219- if err != nil {
220- glog .Fatalf ("failed to list rules in filter table %s chain due to %s" , builtinChain , err .Error ())
217+ return
218+ }
219+ var ruleNo int
220+ for i , rule := range rules {
221+ rule = strings .Replace (rule , "\" " , "" , 2 ) //removes quote from comment string
222+ if strings .Contains (rule , strings .Join (ruleSpec , " " )) {
223+ ruleNo = i
224+ break
221225 }
222-
223- var ruleNo int
224- for i , rule := range rules {
225- if strings .Contains (rule , customChain ) {
226- ruleNo = i
227- break
228- }
226+ }
227+ if ruleNo != position {
228+ err = iptablesCmdHandler .Insert ("filter" , chain , position , ruleSpec ... )
229+ if err != nil {
230+ glog .Fatalf ("Failed to run iptables command to insert in %s chain %s" , chain , err .Error ())
229231 }
230- if ruleNo != 1 {
231- err = iptablesCmdHandler .Insert ("filter" , builtinChain , 1 , args ... )
232- if err != nil {
233- glog .Fatalf ("Failed to run iptables command to insert in %s chain %s" , builtinChain , err .Error ())
234- }
235- err = iptablesCmdHandler .Delete ("filter" , builtinChain , strconv .Itoa (ruleNo + 1 ))
236- if err != nil {
237- glog .Fatalf ("Failed to delete wrong rule to jump to chain %s in %s chain due to %s" , customChain , builtinChain , err .Error ())
238- }
232+ err = iptablesCmdHandler .Delete ("filter" , chain , strconv .Itoa (ruleNo + 1 ))
233+ if err != nil {
234+ glog .Fatalf ("Failed to delete incorrect rule in %s chain due to %s" , chain , err .Error ())
239235 }
240236 }
241237 }
238+
239+ chains := map [string ]string {"INPUT" : kubeInputChainName , "FORWARD" : kubeForwardChainName , "OUTPUT" : kubeOutputChainName }
240+
241+ for builtinChain , customChain := range chains {
242+ err = iptablesCmdHandler .NewChain ("filter" , customChain )
243+ if err != nil && err .(* iptables.Error ).ExitStatus () != 1 {
244+ glog .Fatalf ("Failed to run iptables command to create %s chain due to %s" , customChain , err .Error ())
245+ }
246+ args := []string {"-m" , "comment" , "--comment" , "kube-router netpol" , "-j" , customChain }
247+ ensureRuleAtposition (builtinChain , args , 1 )
248+ }
249+
250+ whitelistServiceVips := []string {"-m" , "comment" , "--comment" , "allow traffic to cluster IP" , "-d" , npc .serviceClusterIPRange , "-j" , "RETURN" }
251+ ensureRuleAtposition (kubeInputChainName , whitelistServiceVips , 1 )
252+
253+ whitelistTCPNodeports := []string {"-p" , "tcp" , "-m" , "comment" , "--comment" , "allow LOCAL traffic to node ports" , "-m" , "addrtype" , "--dst-type" , "LOCAL" ,
254+ "-m" , "multiport" , "--dports" , npc .serviceNodePortRange , "-j" , "RETURN" }
255+ ensureRuleAtposition (kubeInputChainName , whitelistTCPNodeports , 2 )
256+
257+ whitelistUDPNodeports := []string {"-p" , "udp" , "-m" , "comment" , "--comment" , "allow LOCAL traffic to node ports" , "-m" , "addrtype" , "--dst-type" , "LOCAL" ,
258+ "-m" , "multiport" , "--dports" , npc .serviceNodePortRange , "-j" , "RETURN" }
259+ ensureRuleAtposition (kubeInputChainName , whitelistUDPNodeports , 3 )
260+
242261}
243262
244263// OnPodUpdate handles updates to pods from the Kubernetes api server
@@ -953,7 +972,7 @@ func (npc *NetworkPolicyController) syncPodFirewallChains(networkPoliciesInfo []
953972 return nil , fmt .Errorf ("Failed to run iptables command: %s" , err .Error ())
954973 }
955974 if ! exists {
956- err := iptablesCmdHandler .Insert ("filter" , chain , 1 , args ... )
975+ err := iptablesCmdHandler .AppendUnique ("filter" , chain , args ... )
957976 if err != nil {
958977 return nil , fmt .Errorf ("Failed to run iptables command: %s" , err .Error ())
959978 }
@@ -1780,6 +1799,9 @@ func NewNetworkPolicyController(clientset kubernetes.Interface,
17801799 // be up to date with all of the policy changes from any enqueued request after that
17811800 npc .fullSyncRequestChan = make (chan struct {}, 1 )
17821801
1802+ npc .serviceClusterIPRange = config .ClusterIPCIDR
1803+ npc .serviceNodePortRange = config .NodePortRange
1804+
17831805 if config .MetricsEnabled {
17841806 //Register the metrics for this controller
17851807 prometheus .MustRegister (metrics .ControllerIptablesSyncTime )
0 commit comments