Skip to content

Commit 9934119

Browse files
johanotmurali-reddy
authored andcommitted
Fix nwplcy re-sync issue (#477) (#478)
* use strconv for converting int64 to string * change order of pod-fw sync, chain items has to be added before jumping to the chain starts * added logging of syncversion, decreased logging verbosity+severity for planned chain cleanups
1 parent 3a09fda commit 9934119

File tree

1 file changed

+42
-43
lines changed

1 file changed

+42
-43
lines changed

pkg/controllers/netpol/network_policy_controller.go

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (npc *NetworkPolicyController) Sync() error {
206206
defer npc.mu.Unlock()
207207

208208
start := time.Now()
209-
syncVersion := string(start.UnixNano())
209+
syncVersion := strconv.FormatInt(start.UnixNano(), 10)
210210
defer func() {
211211
endTime := time.Since(start)
212212
if npc.MetricsEnabled {
@@ -215,8 +215,7 @@ func (npc *NetworkPolicyController) Sync() error {
215215
glog.V(1).Infof("sync iptables took %v", endTime)
216216
}()
217217

218-
glog.V(1).Info("Starting sync of iptables")
219-
218+
glog.V(1).Infof("Starting sync of iptables with version: %s", syncVersion)
220219
if npc.v1NetworkPolicy {
221220
npc.networkPoliciesInfo, err = npc.buildNetworkPoliciesInfo()
222221
if err != nil {
@@ -625,6 +624,25 @@ func (npc *NetworkPolicyController) syncPodFirewallChains(version string) (map[s
625624
}
626625
activePodFwChains[podFwChainName] = true
627626

627+
// add entries in pod firewall to run through required network policies
628+
for _, policy := range *npc.networkPoliciesInfo {
629+
if _, ok := policy.targetPods[pod.ip]; ok {
630+
comment := "run through nw policy " + policy.name
631+
policyChainName := networkPolicyChainName(policy.namespace, policy.name, version)
632+
args := []string{"-m", "comment", "--comment", comment, "-j", policyChainName}
633+
exists, err := iptablesCmdHandler.Exists("filter", podFwChainName, args...)
634+
if err != nil {
635+
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
636+
}
637+
if !exists {
638+
err := iptablesCmdHandler.Insert("filter", podFwChainName, 1, args...)
639+
if err != nil && err.(*iptables.Error).ExitStatus() != 1 {
640+
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
641+
}
642+
}
643+
}
644+
}
645+
628646
comment := "rule to permit the traffic traffic to pods when source is the pod's local node"
629647
args := []string{"-m", "comment", "--comment", comment, "-m", "addrtype", "--src-type", "LOCAL", "-d", pod.ip, "-j", "ACCEPT"}
630648
exists, err := iptablesCmdHandler.Exists("filter", podFwChainName, args...)
@@ -694,25 +712,6 @@ func (npc *NetworkPolicyController) syncPodFirewallChains(version string) (map[s
694712
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
695713
}
696714

697-
// add entries in pod firewall to run through required network policies
698-
for _, policy := range *npc.networkPoliciesInfo {
699-
if _, ok := policy.targetPods[pod.ip]; ok {
700-
comment := "run through nw policy " + policy.name
701-
policyChainName := networkPolicyChainName(policy.namespace, policy.name, version)
702-
args := []string{"-m", "comment", "--comment", comment, "-j", policyChainName}
703-
exists, err := iptablesCmdHandler.Exists("filter", podFwChainName, args...)
704-
if err != nil {
705-
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
706-
}
707-
if !exists {
708-
err := iptablesCmdHandler.Insert("filter", podFwChainName, 1, args...)
709-
if err != nil && err.(*iptables.Error).ExitStatus() != 1 {
710-
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
711-
}
712-
}
713-
}
714-
}
715-
716715
// ensure statefull firewall, that permits return traffic for the traffic originated by the pod
717716
comment = "rule for stateful firewall for pod"
718717
args = []string{"-m", "comment", "--comment", comment, "-m", "conntrack", "--ctstate", "RELATED,ESTABLISHED", "-j", "ACCEPT"}
@@ -749,6 +748,25 @@ func (npc *NetworkPolicyController) syncPodFirewallChains(version string) (map[s
749748
}
750749
activePodFwChains[podFwChainName] = true
751750

751+
// add entries in pod firewall to run through required network policies
752+
for _, policy := range *npc.networkPoliciesInfo {
753+
if _, ok := policy.targetPods[pod.ip]; ok {
754+
comment := "run through nw policy " + policy.name
755+
policyChainName := networkPolicyChainName(policy.namespace, policy.name, version)
756+
args := []string{"-m", "comment", "--comment", comment, "-j", policyChainName}
757+
exists, err := iptablesCmdHandler.Exists("filter", podFwChainName, args...)
758+
if err != nil {
759+
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
760+
}
761+
if !exists {
762+
err := iptablesCmdHandler.Insert("filter", podFwChainName, 1, args...)
763+
if err != nil && err.(*iptables.Error).ExitStatus() != 1 {
764+
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
765+
}
766+
}
767+
}
768+
}
769+
752770
// ensure there is rule in filter table and FORWARD chain to jump to pod specific firewall chain
753771
// this rule applies to the traffic getting routed (coming for other node pods)
754772
comment := "rule to jump traffic from POD name:" + pod.name + " namespace: " + pod.namespace +
@@ -792,25 +810,6 @@ func (npc *NetworkPolicyController) syncPodFirewallChains(version string) (map[s
792810
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
793811
}
794812

795-
// add entries in pod firewall to run through required network policies
796-
for _, policy := range *npc.networkPoliciesInfo {
797-
if _, ok := policy.targetPods[pod.ip]; ok {
798-
comment := "run through nw policy " + policy.name
799-
policyChainName := networkPolicyChainName(policy.namespace, policy.name, version)
800-
args := []string{"-m", "comment", "--comment", comment, "-j", policyChainName}
801-
exists, err := iptablesCmdHandler.Exists("filter", podFwChainName, args...)
802-
if err != nil {
803-
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
804-
}
805-
if !exists {
806-
err := iptablesCmdHandler.Insert("filter", podFwChainName, 1, args...)
807-
if err != nil && err.(*iptables.Error).ExitStatus() != 1 {
808-
return nil, fmt.Errorf("Failed to run iptables command: %s", err.Error())
809-
}
810-
}
811-
}
812-
}
813-
814813
// ensure statefull firewall, that permits return traffic for the traffic originated by the pod
815814
comment = "rule for stateful firewall for pod"
816815
args = []string{"-m", "comment", "--comment", comment, "-m", "conntrack", "--ctstate", "RELATED,ESTABLISHED", "-j", "ACCEPT"}
@@ -908,7 +907,7 @@ func cleanupStaleRules(activePolicyChains, activePodFwChains, activePolicyIPSets
908907

909908
// cleanup pod firewall chain
910909
for _, chain := range cleanupPodFwChains {
911-
glog.Errorf("Found pod fw chain to cleanup: %s", chain)
910+
glog.V(2).Infof("Found pod fw chain to cleanup: %s", chain)
912911
err = iptablesCmdHandler.ClearChain("filter", chain)
913912
if err != nil {
914913
return fmt.Errorf("Failed to flush the rules in chain %s due to %s", chain, err.Error())
@@ -922,7 +921,7 @@ func cleanupStaleRules(activePolicyChains, activePodFwChains, activePolicyIPSets
922921

923922
// cleanup network policy chains
924923
for _, policyChain := range cleanupPolicyChains {
925-
glog.Infof("Found policy chain to cleanup %s", policyChain)
924+
glog.V(2).Infof("Found policy chain to cleanup %s", policyChain)
926925

927926
// first clean up any references from pod firewall chain
928927
for podFwChain := range activePodFwChains {

0 commit comments

Comments
 (0)