// Sync syncs all iptables rules
Sync syncs all iptables rules
// Sync syncs all iptables rules
func (i IptablesMngr) Sync(s state.State) error {
	start := time.Now()
	defer func() {
		log.Debugf("[iptables ] Sync took %v", time.Since(start))
	}()
	log.Debug("[iptables] Syncing")
	stateSet, err := i.getStateRulesSet(s)
	if err != nil {
		return err
	}
	kernelSet, err := i.getKernelRulesSet()
	if err != nil {
		return err
	}
	rulesToAdd := stateSet.Difference(kernelSet)
	rulesToRemove := kernelSet.Difference(stateSet)
	for r := range rulesToAdd.Iter() {
		rule := r.(SnatRule)
		err := i.addRule(rule)
		if err != nil {
			return err
		}
		log.Debugf("[iptables] Added rule: %#v", rule)
	}
	for r := range rulesToRemove.Iter() {
		rule := r.(SnatRule)
		err := i.removeRule(rule)
		if err != nil {
			return err
		}
		log.Debugf("[iptables] Removed rule: %#v", rule)
	}
	return nil
}
func ( i IptablesMngr ) Sync ( s state . State ) error { start := time . Now ( ) defer func ( ) { log . Debugf ( " " , time . Since ( start ) ) } ( ) log . Debug ( " " ) stateSet , err := i . getStateRulesSet ( s ) if err != nil { return err } kernelSet , err := i . getKernelRulesSet ( ) if err != nil { return err } rulesToAdd := stateSet . Difference ( kernelSet ) rulesToRemove := kernelSet . Difference ( stateSet ) for r := range rulesToAdd . Iter ( ) { rule := r . ( SnatRule ) err := i . addRule ( rule ) if err != nil { return err } log . Debugf ( " " , rule ) } for r := range rulesToRemove . Iter ( ) { rule := r . ( SnatRule ) err := i . removeRule ( rule ) if err != nil { return err } log . Debugf ( " " , rule ) } return nil }
Sync syncs all iptables rules
Sync syncs all iptables rules
// Adding missing rules // Cleaning rules