Skip to content

Commit 1a8c354

Browse files
committed
fix(NPC): Cleanup() function overhaul
Use existing cleanupStale*() methods to cleanup NPC based iptables and ipsets. This provides a more consistent method of cleanup, consolidates the logic, and updates it for all of the changes NPC has gone through.
1 parent 9bc55dc commit 1a8c354

File tree

1 file changed

+28
-106
lines changed

1 file changed

+28
-106
lines changed

pkg/controllers/netpol/network_policy_controller.go

Lines changed: 28 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func (npc *NetworkPolicyController) fullPolicySync() {
251251
return
252252
}
253253

254-
err = npc.cleanupStaleRules(activePolicyChains, activePodFwChains)
254+
err = npc.cleanupStaleRules(activePolicyChains, activePodFwChains, false)
255255
if err != nil {
256256
klog.Errorf("Aborting sync. Failed to cleanup stale iptables rules: %v", err.Error())
257257
return
@@ -418,7 +418,7 @@ func (npc *NetworkPolicyController) ensureDefaultNetworkPolicyChain() {
418418
}
419419
}
420420

421-
func (npc *NetworkPolicyController) cleanupStaleRules(activePolicyChains, activePodFwChains map[string]bool) error {
421+
func (npc *NetworkPolicyController) cleanupStaleRules(activePolicyChains, activePodFwChains map[string]bool, deleteDefaultChains bool) error {
422422

423423
cleanupPodFwChains := make([]string, 0)
424424
cleanupPolicyChains := make([]string, 0)
@@ -471,6 +471,14 @@ func (npc *NetworkPolicyController) cleanupStaleRules(activePolicyChains, active
471471
break
472472
}
473473
}
474+
if deleteDefaultChains {
475+
for _, chain := range []string{kubeInputChainName, kubeForwardChainName, kubeOutputChainName, kubeDefaultNetpolChain} {
476+
if strings.Contains(rule, chain) {
477+
skipRule = true
478+
break
479+
}
480+
}
481+
}
474482
if strings.Contains(rule, "COMMIT") || strings.HasPrefix(rule, "# ") {
475483
skipRule = true
476484
}
@@ -537,122 +545,36 @@ func (npc *NetworkPolicyController) cleanupStaleIPSets(activePolicyIPSets map[st
537545

538546
// Cleanup cleanup configurations done
539547
func (npc *NetworkPolicyController) Cleanup() {
548+
klog.Info("Cleaning up NetworkPolicyController configurations...")
540549

541-
klog.Info("Cleaning up iptables configuration permanently done by kube-router")
542-
543-
iptablesCmdHandler, err := iptables.New()
544-
if err != nil {
545-
klog.Errorf("Failed to initialize iptables executor: %s", err.Error())
546-
return
547-
}
548-
549-
// delete jump rules in FORWARD chain to pod specific firewall chain
550-
forwardChainRules, err := iptablesCmdHandler.List("filter", kubeForwardChainName)
551-
if err != nil {
552-
klog.Errorf("Failed to delete iptables rules as part of cleanup")
553-
return
554-
}
555-
556-
// TODO: need a better way to delete rule with out using number
557-
var realRuleNo int
558-
for i, rule := range forwardChainRules {
559-
if strings.Contains(rule, kubePodFirewallChainPrefix) {
560-
err = iptablesCmdHandler.Delete("filter", kubeForwardChainName, strconv.Itoa(i-realRuleNo))
561-
if err != nil {
562-
klog.Errorf("Failed to delete iptables rule as part of cleanup: %s", err)
563-
}
564-
realRuleNo++
565-
}
566-
}
567-
568-
// delete jump rules in OUTPUT chain to pod specific firewall chain
569-
forwardChainRules, err = iptablesCmdHandler.List("filter", kubeOutputChainName)
570-
if err != nil {
571-
klog.Errorf("Failed to delete iptables rules as part of cleanup")
550+
var emptySet map[string]bool
551+
// Take a dump (iptables-save) of the current filter table for cleanupStaleRules() to work on
552+
if err := utils.SaveInto("filter", &npc.filterTableRules); err != nil {
553+
klog.Errorf("error encountered attempting to list iptables rules for cleanup: %v", err)
572554
return
573555
}
574-
575-
// TODO: need a better way to delete rule with out using number
576-
realRuleNo = 0
577-
for i, rule := range forwardChainRules {
578-
if strings.Contains(rule, kubePodFirewallChainPrefix) {
579-
err = iptablesCmdHandler.Delete("filter", kubeOutputChainName, strconv.Itoa(i-realRuleNo))
580-
if err != nil {
581-
klog.Errorf("Failed to delete iptables rule as part of cleanup: %s", err)
582-
}
583-
realRuleNo++
584-
}
585-
}
586-
587-
// flush and delete pod specific firewall chain
588-
chains, err := iptablesCmdHandler.ListChains("filter")
556+
// Run cleanupStaleRules() to get rid of most of the kube-router rules (this is the same logic that runs as
557+
// part NPC's runtime loop). Setting the last parameter to true causes even the default chains are removed.
558+
err := npc.cleanupStaleRules(emptySet, emptySet, true)
589559
if err != nil {
590-
klog.Errorf("Unable to list chains: %s", err)
560+
klog.Errorf("error encountered attempting to cleanup iptables rules: %v", err)
591561
return
592562
}
593-
for _, chain := range chains {
594-
if strings.HasPrefix(chain, kubePodFirewallChainPrefix) {
595-
err = iptablesCmdHandler.ClearChain("filter", chain)
596-
if err != nil {
597-
klog.Errorf("Failed to cleanup iptables rules: " + err.Error())
598-
return
599-
}
600-
err = iptablesCmdHandler.DeleteChain("filter", chain)
601-
if err != nil {
602-
klog.Errorf("Failed to cleanup iptables rules: " + err.Error())
603-
return
604-
}
605-
}
563+
//klog.Infof("Final rules to save: %s", npc.filterTableRules)
564+
// Restore (iptables-restore) npc's cleaned up version of the iptables filter chain
565+
if err = utils.Restore("filter", npc.filterTableRules.Bytes()); err != nil {
566+
klog.Errorf(
567+
"error encountered while loading running iptables-restore: %v\n%s", err, npc.filterTableRules.String())
606568
}
607569

608-
// flush and delete per network policy specific chain
609-
chains, err = iptablesCmdHandler.ListChains("filter")
570+
// Cleanup ipsets
571+
err = npc.cleanupStaleIPSets(emptySet)
610572
if err != nil {
611-
klog.Errorf("Unable to list chains: %s", err)
573+
klog.Errorf("error encountered while cleaning ipsets: %v", err)
612574
return
613575
}
614-
for _, chain := range chains {
615-
if strings.HasPrefix(chain, kubeNetworkPolicyChainPrefix) {
616-
err = iptablesCmdHandler.ClearChain("filter", chain)
617-
if err != nil {
618-
klog.Errorf("Failed to cleanup iptables rules: " + err.Error())
619-
return
620-
}
621-
err = iptablesCmdHandler.DeleteChain("filter", chain)
622-
if err != nil {
623-
klog.Errorf("Failed to cleanup iptables rules: " + err.Error())
624-
return
625-
}
626-
}
627-
}
628576

629-
// delete all ipsets
630-
// There are certain actions like Cleanup() actions that aren't working with full instantiations of the controller
631-
// and in these instances the mutex may not be present and may not need to be present as they are operating out of a
632-
// single goroutine where there is no need for locking
633-
if nil != npc.ipsetMutex {
634-
klog.V(1).Infof("Attempting to attain ipset mutex lock")
635-
npc.ipsetMutex.Lock()
636-
klog.V(1).Infof("Attained ipset mutex lock, continuing...")
637-
defer func() {
638-
npc.ipsetMutex.Unlock()
639-
klog.V(1).Infof("Returned ipset mutex lock")
640-
}()
641-
}
642-
ipset, err := utils.NewIPSet(false)
643-
if err != nil {
644-
klog.Errorf("Failed to clean up ipsets: " + err.Error())
645-
return
646-
}
647-
err = ipset.Save()
648-
if err != nil {
649-
klog.Errorf("Failed to clean up ipsets: " + err.Error())
650-
}
651-
err = ipset.DestroyAllWithin()
652-
if err != nil {
653-
klog.Errorf("Failed to clean up ipsets: " + err.Error())
654-
}
655-
klog.Infof("Successfully cleaned the iptables configuration done by kube-router")
577+
klog.Infof("Successfully cleaned the NetworkPolicyController configurations done by kube-router")
656578
}
657579

658580
// NewNetworkPolicyController returns new NetworkPolicyController object

0 commit comments

Comments
 (0)