Andrew Rynhard 4ae8186107 feat: add configurator interface
This moves from translating a config into an internal config
representation, to using an interface. The idea is that an interface
gives us stronger compile time checks, and will prevent us from having to copy
from on struct to another. As long as a concrete type implements the
Configurator interface, it can be used to provide instructions to Talos.

Signed-off-by: Andrew Rynhard <andrew@andrewrynhard.com>
2019-10-04 07:53:09 -07:00

94 lines
2.3 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 (
"errors"
"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{
Type: Single,
MTU: 1500,
AddressMethod: []address.Addressing{},
}
}
// 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
}
}
// WithIndex sets the interface index
func WithIndex(o uint32) Option {
return func(n *NetworkInterface) (err error) {
n.Index = o
return err
}
}
// WithType defines how the interface should be configured - bonded or single.
func WithType(o int) Option {
return func(n *NetworkInterface) (err error) {
switch o {
case Bond:
n.Type = Bond
case Single:
n.Type = Single
default:
return errors.New("unsupported network interface type")
}
return err
}
}
// WithMTU defines the MTU for the interface
// TODO: I think we should drop this since MTU is getting set
// by address configuration method ( either via dhcp or config )
func WithMTU(mtu uint32) Option {
return func(n *NetworkInterface) (err error) {
if (mtu < MinimumMTU) || (mtu > MaximumMTU) {
return errors.New("mtu is out of acceptable range")
}
n.MTU = mtu
return err
}
}
// WithSubInterface defines which interfaces make up the bond
func WithSubInterface(o string) Option {
return func(n *NetworkInterface) (err error) {
n.SubInterfaces = append(n.SubInterfaces, 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
}
}