func()

in cmd/core_plugin/network/netplan/netplan_linux.go [107:168]


func (sn *serviceNetplan) Setup(ctx context.Context, opts *service.Options) error {
	galog.Info("Setting up netplan interfaces.")

	// Write the netplan drop-in file.
	netplanChanged, err := sn.writeDropin(opts.NICConfigs)
	if err != nil {
		return fmt.Errorf("error writing netplan dropin: %w", err)
	}

	// Write the netplan vlan drop-in file.
	netplanVlanChanged, err := sn.writeVlanDropin(opts.NICConfigs)
	if err != nil {
		return fmt.Errorf("error writing netplan vlan dropin: %w", err)
	}

	// Write the backend's drop-in files.
	backendChanged, err := sn.backend.WriteDropins(opts.NICConfigs, backendDropinPrefix)
	if err != nil {
		return err
	}

	// Reload the backend if networkd's configuration has changed.
	if backendChanged && sn.backendReload {
		if err := sn.backend.Reload(ctx); err != nil {
			return fmt.Errorf("error reloading backend(%q) configs: %v", sn.backend.ID(), err)
		}
	}

	// Apply the netplan configuration.
	if netplanChanged || netplanVlanChanged {
		opt := run.Options{OutputType: run.OutputNone, Name: "netplan", Args: []string{"apply"}}
		if _, err := run.WithContext(ctx, opt); err != nil {
			return fmt.Errorf("error applying netplan changes: %w", err)
		}
	}

	// Setup all nics routes using the "native" implementation if the running
	// system's netplan doesn't support local routes.
	if !sn.dropinRoutes {
		for _, nic := range opts.NICConfigs {
			galog.Debugf("Attempting to add any missing route for nic: %s", nic.Interface.Name())

			if nic.ExtraAddresses == nil {
				galog.V(2).Debugf("No extra addresses to add routes for: %s", nic.Interface.Name())
				continue
			}

			data, err := route.MissingRoutes(ctx, nic.Interface.Name(), nic.ExtraAddresses.MergedMap())
			if err != nil {
				return fmt.Errorf("failed to list missing routes: %w", err)
			}

			for _, addMe := range data {
				if err := route.Add(ctx, addMe); err != nil {
					return fmt.Errorf("failed to add route: %w", err)
				}
			}
		}
	}

	return nil
}