talos/internal/app/networkd/pkg/nic/options.go
Spencer Smith d57c97fdb6 feat: allow ability to create dummy nics
This PR will introduce a new field to v1alpha1 configs that allows users
to set `dummy: true` when specifying interfaces. If present, we will
create a dummy interface with the CIDR information given. This is useful
for users that don't want to use loopback for things like ECMP (or want
more than one dummy interface).

The created dummy interface looked like this with `ip a`:

```
3: dummy0: <BROADCAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000
    link/ether 66:4a:e3:5f:38:10 brd ff:ff:ff:ff:ff:ff
    inet 10.254.0.5/32 brd 10.254.0.5 scope global dummy0
       valid_lft forever preferred_lft forever
```

Will close #2186.

Signed-off-by: Spencer Smith <robertspencersmith@gmail.com>
2020-06-17 17:15:07 -04:00

67 lines
1.7 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package nic
import (
"github.com/mdlayher/netlink"
"github.com/talos-systems/talos/internal/app/networkd/pkg/address"
)
// Option is the functional option func.
type Option func(*NetworkInterface) error
// defaultOptions defines our default network interface configuration.
func defaultOptions() *NetworkInterface {
return &NetworkInterface{
Bonded: false,
MTU: 1500,
AddressMethod: []address.Addressing{},
BondSettings: netlink.NewAttributeEncoder(),
}
}
// WithDummy indicates that the interface should be a virtual, dummy interface.
func WithDummy() Option {
return func(n *NetworkInterface) (err error) {
n.Dummy = true
return
}
}
// WithIgnore indicates that the interface should not be processed by talos.
func WithIgnore() Option {
return func(n *NetworkInterface) (err error) {
n.Ignore = true
return
}
}
// WithName sets the name of the interface to the given name.
func WithName(o string) Option {
return func(n *NetworkInterface) (err error) {
n.Name = o
return err
}
}
// WithAddressing defines how the addressing for a given interface
// should be configured
func WithAddressing(a address.Addressing) Option {
return func(n *NetworkInterface) (err error) {
n.AddressMethod = append(n.AddressMethod, a)
return err
}
}
// WithNoAddressing defines how the addressing for a given interface
// should be configured
func WithNoAddressing() Option {
return func(n *NetworkInterface) (err error) {
n.AddressMethod = []address.Addressing{}
return err
}
}