@@ -622,35 +622,40 @@ func (nsc *NetworkServicesController) setupIpvsFirewall() error {
622622}
623623
624624func (nsc * NetworkServicesController ) cleanupIpvsFirewall () {
625- /*
626- - delete firewall rules
627- - delete ipsets
628- */
629- var err error
630-
631- // Clear iptables rules.
625+ // Clear iptables rules
632626 iptablesCmdHandler , err := iptables .New ()
633627 if err != nil {
634- klog .Errorf ("Failed to initialize iptables executor: %s " , err . Error () )
628+ klog .Errorf ("failed to initialize iptables executor: %v " , err )
635629 } else {
636630 ipvsFirewallInputChainRule := getIpvsFirewallInputChainRule ()
637- err = iptablesCmdHandler .Delete ("filter" , "INPUT" , ipvsFirewallInputChainRule ... )
631+ exists , err : = iptablesCmdHandler .Exists ("filter" , "INPUT" , ipvsFirewallInputChainRule ... )
638632 if err != nil {
639- klog .Errorf ("Failed to run iptables command: %s" , err .Error ())
633+ // Changing to level 1 logging as errors occur when ipsets have already been cleaned and needlessly worries users
634+ klog .V (1 ).Infof ("failed to check if iptables rules exists: %v" , err )
635+ } else if exists {
636+ err = iptablesCmdHandler .Delete ("filter" , "INPUT" , ipvsFirewallInputChainRule ... )
637+ if err != nil {
638+ klog .Errorf ("failed to run iptables command: %v" , err )
639+ }
640640 }
641641
642- err = iptablesCmdHandler .ClearChain ("filter" , ipvsFirewallChainName )
642+ exists , err = iptablesCmdHandler .ChainExists ("filter" , ipvsFirewallChainName )
643643 if err != nil {
644- klog .Errorf ("Failed to run iptables command: %s" , err .Error ())
645- }
644+ klog .Errorf ("failed to check if chain exists for deletion: %v" , err )
645+ } else if exists {
646+ err = iptablesCmdHandler .ClearChain ("filter" , ipvsFirewallChainName )
647+ if err != nil {
648+ klog .Errorf ("Failed to run iptables command: %s" , err .Error ())
649+ }
646650
647- err = iptablesCmdHandler .DeleteChain ("filter" , ipvsFirewallChainName )
648- if err != nil {
649- klog .Errorf ("Failed to run iptables command: %s" , err .Error ())
651+ err = iptablesCmdHandler .DeleteChain ("filter" , ipvsFirewallChainName )
652+ if err != nil {
653+ klog .Errorf ("Failed to run iptables command: %s" , err .Error ())
654+ }
650655 }
651656 }
652657
653- // Clear ipsets.
658+ // Clear ipsets
654659 // There are certain actions like Cleanup() actions that aren't working with full instantiations of the controller
655660 // and in these instances the mutex may not be present and may not need to be present as they are operating out of a
656661 // single goroutine where there is no need for locking
@@ -666,17 +671,29 @@ func (nsc *NetworkServicesController) cleanupIpvsFirewall() {
666671 ipSetHandler , err := utils .NewIPSet (false )
667672 if err != nil {
668673 klog .Errorf ("Failed to initialize ipset handler: %s" , err .Error ())
669- } else {
674+ return
675+ }
676+ err = ipSetHandler .Save ()
677+ if err != nil {
678+ klog .Fatalf ("failed to initialize ipsets command executor due to %v" , err )
679+ return
680+ }
681+
682+ if _ , ok := ipSetHandler .Sets [localIPsIPSetName ]; ok {
670683 err = ipSetHandler .Destroy (localIPsIPSetName )
671684 if err != nil {
672685 klog .Errorf ("failed to destroy ipset: %s" , err .Error ())
673686 }
687+ }
674688
689+ if _ , ok := ipSetHandler .Sets [serviceIPsIPSetName ]; ok {
675690 err = ipSetHandler .Destroy (serviceIPsIPSetName )
676691 if err != nil {
677692 klog .Errorf ("failed to destroy ipset: %s" , err .Error ())
678693 }
694+ }
679695
696+ if _ , ok := ipSetHandler .Sets [ipvsServicesIPSetName ]; ok {
680697 err = ipSetHandler .Destroy (ipvsServicesIPSetName )
681698 if err != nil {
682699 klog .Errorf ("failed to destroy ipset: %s" , err .Error ())
0 commit comments