in google_guest_agent/network/manager/systemd_networkd_linux.go [271:356]
func (n *systemdNetworkd) SetupVlanInterface(ctx context.Context, config *cfg.Sections, nics *Interfaces) error {
var keepMe []string
for _, curr := range nics.VlanInterfaces {
iface := fmt.Sprintf("gcp.%s.%d", curr.ParentInterfaceID, curr.Vlan)
// Create and setup .network file.
networkConfig := systemdConfig{
GuestAgent: guestAgentSection{
ManagedByGuestAgent: true,
},
Match: systemdMatchConfig{
Name: iface,
Type: "vlan",
},
Network: systemdNetworkConfig{
DHCP: "yes", // enables ipv4 and ipv6
},
Link: &systemdLinkConfig{
MACAddress: curr.Mac,
MTUBytes: curr.MTU,
},
}
if err := networkConfig.write(n, iface); err != nil {
return fmt.Errorf("failed to write systemd's vlan .network config: %+v", err)
}
// Create and setup .netdev file.
netdevConfig := systemdNetdevConfig{
GuestAgent: guestAgentSection{
ManagedByGuestAgent: true,
},
NetDev: systemdNetdev{
Name: iface,
Kind: "vlan",
},
VLAN: systemdVlan{
ID: curr.Vlan,
ReorderHeader: false,
},
}
if err := netdevConfig.write(n, iface); err != nil {
return fmt.Errorf("failed to write systemd's vlan .netdev config: %+v", err)
}
// Add VLAN keys to the VLAN's parent .network config file.
parentFile := n.networkFile(curr.ParentInterfaceID)
parentConfig := new(systemdConfig)
if err := readIniFile(parentFile, parentConfig); err != nil {
return fmt.Errorf("failed to read vlan's parent interface .network config: %+v", err)
}
// Add the vlan interface to parents VLAN key if not there already.
if !slices.Contains(parentConfig.Network.VLANS, iface) {
parentConfig.Network.VLANS = append(parentConfig.Network.VLANS, iface)
if err := parentConfig.write(n, curr.ParentInterfaceID); err != nil {
return fmt.Errorf("error writing vlan parent's .network config: %+v", err)
}
}
keepMe = append(keepMe, iface)
}
// Attempt to remove vlan interface configurations that are not known - i.e. they were previously
// added by users but are no longer present on their mds configuration.
requiresRestart, err := n.removeVlanInterfaces(ctx, keepMe)
if err != nil {
return fmt.Errorf("failed to remove vlan interface configuration: %+v", err)
}
if !requiresRestart {
logger.Debugf("No changes applied to systemd-network's vlan config, skipping restart.")
return nil
}
// Apply network changes avoiding to restart systemd-networkd.
if err := run.Quiet(ctx, "networkctl", "reload"); err != nil {
return fmt.Errorf("error reloading systemd-networkd network configs: %v", err)
}
return nil
}