@@ -261,23 +261,17 @@ func (npc *NetworkPolicyController) syncNetworkPolicyChains() (map[string]bool,
261261 return nil , nil , fmt .Errorf ("failed to create ipset: %s" , err .Error ())
262262 }
263263
264- // flush all entries in the set
265- if targetSourcePodIpSet .Flush () != nil {
266- return nil , nil , fmt .Errorf ("failed to flush ipset while syncing iptables: %s" , err .Error ())
267- }
268- if targetDestPodIpSet .Flush () != nil {
269- return nil , nil , fmt .Errorf ("failed to flush ipset while syncing iptables: %s" , err .Error ())
270- }
271-
272264 activePolicyIpSets [targetDestPodIpSet .Name ] = true
273265 activePolicyIpSets [targetSourcePodIpSet .Name ] = true
274266
275- for k := range policy .targetPods {
276- // TODO restrict ipset to ip's of pods running on the node
277- targetDestPodIpSet .Add (k , utils .OptionTimeout , "0" )
278- targetSourcePodIpSet .Add (k , utils .OptionTimeout , "0" )
267+ currnetPodIps := make ([]string , 0 , len (policy .targetPods ))
268+ for ip := range policy .targetPods {
269+ currnetPodIps = append (currnetPodIps , ip )
279270 }
280271
272+ targetSourcePodIpSet .Refresh (currnetPodIps , utils .OptionTimeout , "0" )
273+ targetDestPodIpSet .Refresh (currnetPodIps , utils .OptionTimeout , "0" )
274+
281275 // TODO use iptables-restore to better implement the logic, than flush and add rules
282276 err = iptablesCmdHandler .ClearChain ("filter" , policyChainName )
283277 if err != nil && err .(* iptables.Error ).ExitStatus () != 1 {
@@ -326,16 +320,14 @@ func (npc *NetworkPolicyController) processIngressRules(policy networkPolicyInfo
326320 if err != nil {
327321 return fmt .Errorf ("failed to create ipset: %s" , err .Error ())
328322 }
329- // flush all entries in the set
330- if srcPodIpSet .Flush () != nil {
331- return fmt .Errorf ("failed to flush ipset while syncing iptables: %s" , err .Error ())
332- }
333323
334324 activePolicyIpSets [srcPodIpSet .Name ] = true
335325
326+ ingressRuleSrcPodIps := make ([]string , 0 , len (ingressRule .srcPods ))
336327 for _ , pod := range ingressRule .srcPods {
337- srcPodIpSet . Add ( pod . ip , utils . OptionTimeout , "0" )
328+ ingressRuleSrcPodIps = append ( ingressRuleSrcPodIps , pod . ip )
338329 }
330+ srcPodIpSet .Refresh (ingressRuleSrcPodIps , utils .OptionTimeout , "0" )
339331
340332 if len (ingressRule .ports ) != 0 {
341333 // case where 'ports' details and 'from' details specified in the ingress rule
@@ -463,16 +455,14 @@ func (npc *NetworkPolicyController) processEgressRules(policy networkPolicyInfo,
463455 if err != nil {
464456 return fmt .Errorf ("failed to create ipset: %s" , err .Error ())
465457 }
466- // flush all entries in the set
467- if dstPodIpSet .Flush () != nil {
468- return fmt .Errorf ("failed to flush ipset while syncing iptables: %s" , err .Error ())
469- }
470458
471459 activePolicyIpSets [dstPodIpSet .Name ] = true
472460
461+ egressRuleDstPodIps := make ([]string , 0 , len (egressRule .dstPods ))
473462 for _ , pod := range egressRule .dstPods {
474- dstPodIpSet . Add ( pod . ip , utils . OptionTimeout , "0" )
463+ egressRuleDstPodIps = append ( egressRuleDstPodIps , pod . ip )
475464 }
465+ dstPodIpSet .Refresh (egressRuleDstPodIps , utils .OptionTimeout , "0" )
476466
477467 if len (egressRule .ports ) != 0 {
478468 // case where 'ports' details and 'from' details specified in the egress rule
0 commit comments