Repository: moby/moby
File: libnetwork/drivers/bridge/bridge_linux.go (Go)

Code before:

	return nil
}

func (d *driver) configure(option map[string]interface{}) error {
	var config configuration
	switch opt := option[netlabel.GenericData].(type) {
	case options.Generic:
		opaqueConfig, err := options.GenerateFromModel(opt, &configuration{})
		if err != nil {
			return err
		}
		config = *opaqueConfig.(*configuration)
	case *configuration:
		config = *opt
	case nil:
		// No GenericData option set. Use defaults.
	default:
		return errdefs.InvalidParameter(fmt.Errorf("invalid configuration type (%T) passed", opt))
	}

	if config.EnableIPTables {
		removeIPChains(iptables.IPv4)

		if err := setupHashNetIpset(ipsetExtBridges4, unix.AF_INET); err != nil {
			return fmt.Errorf("creating ipset "+ipsetExtBridges4+": %w (kernel modules ip_set, ip_set_hash_net and netfilter_xt_set are required)", err)
		}
		if err := setupIPChains(config, iptables.IPv4); err != nil {
			return err
		}

		// Make sure on firewall reload, first thing being re-played is chains creation
		iptables.OnReloaded(func() {
			log.G(context.TODO()).Debugf("Recreating iptables chains on firewall reload")
			if err := setupIPChains(config, iptables.IPv4); err != nil {
				log.G(context.TODO()).WithError(err).Error("Error reloading iptables chains")
			}
		})
	}

	if config.EnableIP6Tables {
		if err := modprobe.LoadModules(context.TODO(), func() error {
			iptable := iptables.GetIptable(iptables.IPv6)
			_, err := iptable.Raw("-t", "filter", "-n", "-L", "FORWARD")
			return err
		}, "ip6_tables"); err != nil {
			log.G(context.TODO()).WithError(err).Debug("Loading ip6_tables")
		}

		removeIPChains(iptables.IPv6)


Review comment (suggestion):
As we're already requiring formatting, it's probably better in this case to use `ipsetExtBridges4` as a argument, and put a `%s` here;

```suggestion
			return fmt.Errorf("creating ipset %s: %w (kernel modules ip_set, ip_set_hash_net and netfilter_xt_set are required)", ipsetExtBridges4, err)
```


Code after:

	return nil
}

func (d *driver) configure(option map[string]interface{}) error {
	var config configuration
	switch opt := option[netlabel.GenericData].(type) {
	case options.Generic:
		opaqueConfig, err := options.GenerateFromModel(opt, &configuration{})
		if err != nil {
			return err
		}
		config = *opaqueConfig.(*configuration)
	case *configuration:
		config = *opt
	case nil:
		// No GenericData option set. Use defaults.
	default:
		return errdefs.InvalidParameter(fmt.Errorf("invalid configuration type (%T) passed", opt))
	}

	if config.EnableIPTables {
		removeIPChains(iptables.IPv4)

		if err := setupHashNetIpset(ipsetExtBridges4, unix.AF_INET); err != nil {
			return fmt.Errorf("%w (kernel modules ip_set, ip_set_hash_net and netfilter_xt_set are required)", err)
		}
		if err := setupIPChains(config, iptables.IPv4); err != nil {
			return err
		}

		// Make sure on firewall reload, first thing being re-played is chains creation
		iptables.OnReloaded(func() {
			log.G(context.TODO()).Debugf("Recreating iptables chains on firewall reload")
			if err := setupIPChains(config, iptables.IPv4); err != nil {
				log.G(context.TODO()).WithError(err).Error("Error reloading iptables chains")
			}
		})
	}

	if config.EnableIP6Tables {
		if err := modprobe.LoadModules(context.TODO(), func() error {
			iptable := iptables.GetIptable(iptables.IPv6)
			_, err := iptable.Raw("-t", "filter", "-n", "-L", "FORWARD")
			return err
		}, "ip6_tables"); err != nil {
			log.G(context.TODO()).WithError(err).Debug("Loading ip6_tables")
		}

		removeIPChains(iptables.IPv6)