pkg/wireguard: allow specifying iface named
This commit makes it possible to specify the Kilo interface name. If the specified interface exists, it will be used; if it does not exist, Kilo will create it. If the interface already existed, then it will not be deleted on shutdown; otherwise Kilo will destroy the interface. Fixes: https://github.com/squat/kilo/issues/8 Addresses: 1/2 of https://github.com/squat/kilo/issues/17
This commit is contained in:
parent
9fda84ec05
commit
20349de548
@ -86,6 +86,7 @@ func Main() error {
|
|||||||
granularity := flag.String("mesh-granularity", string(mesh.LogicalGranularity), fmt.Sprintf("The granularity of the network mesh to create. Possible values: %s", availableGranularities))
|
granularity := flag.String("mesh-granularity", string(mesh.LogicalGranularity), fmt.Sprintf("The granularity of the network mesh to create. Possible values: %s", availableGranularities))
|
||||||
kubeconfig := flag.String("kubeconfig", "", "Path to kubeconfig.")
|
kubeconfig := flag.String("kubeconfig", "", "Path to kubeconfig.")
|
||||||
hostname := flag.String("hostname", "", "Hostname of the node on which this process is running.")
|
hostname := flag.String("hostname", "", "Hostname of the node on which this process is running.")
|
||||||
|
iface := flag.String("interface", mesh.DefaultKiloInterface, "Name of the Kilo interface to use; if it does not exist, it will be created.")
|
||||||
listen := flag.String("listen", ":1107", "The address at which to listen for health and metrics.")
|
listen := flag.String("listen", ":1107", "The address at which to listen for health and metrics.")
|
||||||
local := flag.Bool("local", true, "Should Kilo manage routes within a location.")
|
local := flag.Bool("local", true, "Should Kilo manage routes within a location.")
|
||||||
logLevel := flag.String("log-level", logLevelInfo, fmt.Sprintf("Log level to use. Possible values: %s", availableLogLevels))
|
logLevel := flag.String("log-level", logLevelInfo, fmt.Sprintf("Log level to use. Possible values: %s", availableLogLevels))
|
||||||
@ -174,7 +175,7 @@ func Main() error {
|
|||||||
return fmt.Errorf("backend %v unknown; possible values are: %s", *backend, availableBackends)
|
return fmt.Errorf("backend %v unknown; possible values are: %s", *backend, availableBackends)
|
||||||
}
|
}
|
||||||
|
|
||||||
m, err := mesh.New(b, enc, gr, *hostname, uint32(port), s, *local, *cni, *cniPath, log.With(logger, "component", "kilo"))
|
m, err := mesh.New(b, enc, gr, *hostname, uint32(port), s, *local, *cni, *cniPath, *iface, log.With(logger, "component", "kilo"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create Kilo mesh: %v", err)
|
return fmt.Errorf("failed to create Kilo mesh: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,8 @@ const (
|
|||||||
PrivateKeyPath = KiloPath + "/key"
|
PrivateKeyPath = KiloPath + "/key"
|
||||||
// ConfPath is the filepath where the WireGuard configuration is stored.
|
// ConfPath is the filepath where the WireGuard configuration is stored.
|
||||||
ConfPath = KiloPath + "/conf"
|
ConfPath = KiloPath + "/conf"
|
||||||
|
// DefaultKiloInterface is the default iterface created and used by Kilo.
|
||||||
|
DefaultKiloInterface = "kilo0"
|
||||||
// DefaultKiloPort is the default UDP port Kilo uses.
|
// DefaultKiloPort is the default UDP port Kilo uses.
|
||||||
DefaultKiloPort = 51820
|
DefaultKiloPort = 51820
|
||||||
// DefaultCNIPath is the default path to the CNI config file.
|
// DefaultCNIPath is the default path to the CNI config file.
|
||||||
@ -169,6 +171,7 @@ type Mesh struct {
|
|||||||
Backend
|
Backend
|
||||||
cni bool
|
cni bool
|
||||||
cniPath string
|
cniPath string
|
||||||
|
deleteIface bool
|
||||||
enc encapsulation.Encapsulator
|
enc encapsulation.Encapsulator
|
||||||
externalIP *net.IPNet
|
externalIP *net.IPNet
|
||||||
granularity Granularity
|
granularity Granularity
|
||||||
@ -202,7 +205,7 @@ type Mesh struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new Mesh instance.
|
// New returns a new Mesh instance.
|
||||||
func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularity, hostname string, port uint32, subnet *net.IPNet, local, cni bool, cniPath string, logger log.Logger) (*Mesh, error) {
|
func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularity, hostname string, port uint32, subnet *net.IPNet, local, cni bool, cniPath, iface string, logger log.Logger) (*Mesh, error) {
|
||||||
if err := os.MkdirAll(KiloPath, 0700); err != nil {
|
if err := os.MkdirAll(KiloPath, 0700); err != nil {
|
||||||
return nil, fmt.Errorf("failed to create directory to store configuration: %v", err)
|
return nil, fmt.Errorf("failed to create directory to store configuration: %v", err)
|
||||||
}
|
}
|
||||||
@ -239,7 +242,7 @@ func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularit
|
|||||||
return nil, fmt.Errorf("failed to find interface for public IP: %v", err)
|
return nil, fmt.Errorf("failed to find interface for public IP: %v", err)
|
||||||
}
|
}
|
||||||
pubIface := ifaces[0].Index
|
pubIface := ifaces[0].Index
|
||||||
kiloIface, err := wireguard.New("kilo")
|
kiloIface, created, err := wireguard.New(iface)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create WireGuard interface: %v", err)
|
return nil, fmt.Errorf("failed to create WireGuard interface: %v", err)
|
||||||
}
|
}
|
||||||
@ -258,6 +261,7 @@ func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularit
|
|||||||
Backend: backend,
|
Backend: backend,
|
||||||
cni: cni,
|
cni: cni,
|
||||||
cniPath: cniPath,
|
cniPath: cniPath,
|
||||||
|
deleteIface: created,
|
||||||
enc: enc,
|
enc: enc,
|
||||||
externalIP: publicIP,
|
externalIP: publicIP,
|
||||||
granularity: granularity,
|
granularity: granularity,
|
||||||
@ -713,9 +717,11 @@ func (m *Mesh) cleanUp() {
|
|||||||
level.Error(m.logger).Log("error", fmt.Sprintf("failed to delete configuration file: %v", err))
|
level.Error(m.logger).Log("error", fmt.Sprintf("failed to delete configuration file: %v", err))
|
||||||
m.errorCounter.WithLabelValues("cleanUp").Inc()
|
m.errorCounter.WithLabelValues("cleanUp").Inc()
|
||||||
}
|
}
|
||||||
if err := iproute.RemoveInterface(m.kiloIface); err != nil {
|
if m.deleteIface {
|
||||||
level.Error(m.logger).Log("error", fmt.Sprintf("failed to remove WireGuard interface: %v", err))
|
if err := iproute.RemoveInterface(m.kiloIface); err != nil {
|
||||||
m.errorCounter.WithLabelValues("cleanUp").Inc()
|
level.Error(m.logger).Log("error", fmt.Sprintf("failed to remove WireGuard interface: %v", err))
|
||||||
|
m.errorCounter.WithLabelValues("cleanUp").Inc()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err := m.Nodes().CleanUp(m.hostname); err != nil {
|
if err := m.Nodes().CleanUp(m.hostname); err != nil {
|
||||||
level.Error(m.logger).Log("error", fmt.Sprintf("failed to clean up node backend: %v", err))
|
level.Error(m.logger).Log("error", fmt.Sprintf("failed to clean up node backend: %v", err))
|
||||||
|
@ -18,8 +18,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
)
|
)
|
||||||
@ -37,37 +35,28 @@ func (w wgLink) Type() string {
|
|||||||
return w.t
|
return w.t
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new WireGuard interface.
|
// New returns a WireGuard interface with the given name.
|
||||||
func New(prefix string) (int, error) {
|
// If the interface exists, its index is returned.
|
||||||
links, err := netlink.LinkList()
|
// Otherwise, a new interface is created.
|
||||||
if err != nil {
|
// The function also returns a boolean to indicate if the interface was created.
|
||||||
return 0, fmt.Errorf("failed to list links: %v", err)
|
func New(name string) (int, bool, error) {
|
||||||
|
link, err := netlink.LinkByName(name)
|
||||||
|
if err == nil {
|
||||||
|
return link.Attrs().Index, false, nil
|
||||||
}
|
}
|
||||||
max := 0
|
if _, ok := err.(netlink.LinkNotFoundError); !ok {
|
||||||
re := regexp.MustCompile(fmt.Sprintf("^%s([0-9]+)$", prefix))
|
return 0, false, fmt.Errorf("failed to get links: %v", err)
|
||||||
for _, link := range links {
|
|
||||||
if matches := re.FindStringSubmatch(link.Attrs().Name); len(matches) == 2 {
|
|
||||||
i, err := strconv.Atoi(matches[1])
|
|
||||||
if err != nil {
|
|
||||||
// This should never happen.
|
|
||||||
return 0, fmt.Errorf("failed to parse digits as an integer: %v", err)
|
|
||||||
}
|
|
||||||
if i >= max {
|
|
||||||
max = i + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
name := fmt.Sprintf("%s%d", prefix, max)
|
|
||||||
wl := wgLink{a: netlink.NewLinkAttrs(), t: "wireguard"}
|
wl := wgLink{a: netlink.NewLinkAttrs(), t: "wireguard"}
|
||||||
wl.a.Name = name
|
wl.a.Name = name
|
||||||
if err := netlink.LinkAdd(wl); err != nil {
|
if err := netlink.LinkAdd(wl); err != nil {
|
||||||
return 0, fmt.Errorf("failed to create interface %s: %v", name, err)
|
return 0, false, fmt.Errorf("failed to create interface %s: %v", name, err)
|
||||||
}
|
}
|
||||||
link, err := netlink.LinkByName(name)
|
link, err = netlink.LinkByName(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("failed to get interface index: %v", err)
|
return 0, false, fmt.Errorf("failed to get interface index: %v", err)
|
||||||
}
|
}
|
||||||
return link.Attrs().Index, nil
|
return link.Attrs().Index, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keys generates a WireGuard private and public key-pair.
|
// Keys generates a WireGuard private and public key-pair.
|
||||||
|
Loading…
Reference in New Issue
Block a user