Repository: moby/moby
File: libnetwork/drivers/macvlan/macvlan_network.go (Go)

Code before:

	return nil
}

// createNetwork is used by new network callbacks and persistent network cache
func (d *driver) createNetwork(config *configuration) (bool, error) {
	foundExisting := false
	networkList := d.getNetworks()
	for _, nw := range networkList {
		if config.Parent == nw.config.Parent {
			if config.ID != nw.config.ID {
				if config.MacvlanMode == modePassthru || nw.config.MacvlanMode == modePassthru {
					nwName := getDummyName(stringid.TruncateID(nw.config.ID))
					name := getDummyName(stringid.TruncateID(config.ID))
					return false, fmt.Errorf(
						"macvlan network %s is already using parent interface %s and either %s or %s has mode passthru",
						nwName, config.Parent, nwName, name,
					)
				}
				continue
			}
			log.G(context.TODO()).Debugf("Create Network for the same ID %s\n", config.ID)
			foundExisting = true
			break
		}
	}
	if !parentExists(config.Parent) {
		// Create a dummy link if a dummy name is set for parent
		if dummyName := getDummyName(stringid.TruncateID(config.ID)); dummyName == config.Parent {
			err := createDummyLink(config.Parent, dummyName)
			if err != nil {
				return false, err
			}
			config.CreatedSlaveLink = true

			// notify the user in logs that they have limited communications
			log.G(context.TODO()).Debugf("Empty -o parent= flags limit communications to other containers inside of network: %s",
				config.Parent)
		} else {
			// if the subinterface parent_iface.vlan_id checks do not pass, return err.
			//  a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10'
			err := createVlanLink(config.Parent)
			if err != nil {
				return false, err
			}
			// if driver created the networks slave link, record it for future deletion
			config.CreatedSlaveLink = true
		}
	}
	if !foundExisting {
		n := &network{

Review comment (suggestion):
The function `getDummyName()` is used to generate the name for a dummy link when no parent interface is specified - it just translates `foo` into `dm-foo`. So, for `nwName` - as the actual name doesn't seem to be stored by the driver, its short-id is just `stringid.TruncateID(nw.config.ID)`.

It might be worth making the error message simpler and more specific by splitting out the two cases...

```suggestion
                                if config.MacvlanMode == modePassthru {
                                        return false, fmt.Errorf(
                                                "cannot use mode passthru, macvlan network %s is already using parent interface %s",
                                                stringid.TruncateID(nw.config.ID),
                                                config.Parent,
                                        )
                                } else if nw.config.MacvlanMode == modePassthru {
                                        return false, fmt.Errorf(
                                                "macvlan network %s is already using parent interface %s in mode passthru",
                                                stringid.TruncateID(nw.config.ID),
                                                config.Parent,
                                        )
                                }
                                continue
```

Code after:
	}

	return nil
}

// createNetwork is used by new network callbacks and persistent network cache
func (d *driver) createNetwork(config *configuration) (bool, error) {
	foundExisting := false
	networkList := d.getNetworks()
	for _, nw := range networkList {
		if config.Parent == nw.config.Parent {
			if config.ID != nw.config.ID {
				if config.MacvlanMode == modePassthru {
					return false, fmt.Errorf(
						"cannot use mode passthru, macvlan network %s is already using parent interface %s",
						stringid.TruncateID(nw.config.ID),
						config.Parent,
					)
				} else if nw.config.MacvlanMode == modePassthru {
					return false, fmt.Errorf(
						"macvlan network %s is already using parent interface %s in mode passthru",
						stringid.TruncateID(nw.config.ID),
						config.Parent,
					)
				}
				continue
			}
			log.G(context.TODO()).Debugf("Create Network for the same ID %s\n", config.ID)
			foundExisting = true
			break
		}
	}
	if !parentExists(config.Parent) {
		// Create a dummy link if a dummy name is set for parent
		if dummyName := getDummyName(stringid.TruncateID(config.ID)); dummyName == config.Parent {
			err := createDummyLink(config.Parent, dummyName)
			if err != nil {
				return false, err
			}
			config.CreatedSlaveLink = true

			// notify the user in logs that they have limited communications
			log.G(context.TODO()).Debugf("Empty -o parent= flags limit communications to other containers inside of network: %s",
				config.Parent)
		} else {
			// if the subinterface parent_iface.vlan_id checks do not pass, return err.
			//  a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10'
			err := createVlanLink(config.Parent)
			if err != nil {
				return false, err
			}