Add WireGuardPersistentKeepAlive to mesh.Node

This commit is contained in:
Francis Nguyen 2020-02-11 21:08:04 -07:00
parent a6afc3247d
commit 8a5dbbe368
3 changed files with 56 additions and 33 deletions

View File

@ -47,16 +47,17 @@ import (
const ( const (
// Backend is the name of this mesh backend. // Backend is the name of this mesh backend.
Backend = "kubernetes" Backend = "kubernetes"
externalIPAnnotationKey = "kilo.squat.ai/external-ip" externalIPAnnotationKey = "kilo.squat.ai/external-ip"
forceExternalIPAnnotationKey = "kilo.squat.ai/force-external-ip" forceExternalIPAnnotationKey = "kilo.squat.ai/force-external-ip"
forceInternalIPAnnotationKey = "kilo.squat.ai/force-internal-ip" forceInternalIPAnnotationKey = "kilo.squat.ai/force-internal-ip"
internalIPAnnotationKey = "kilo.squat.ai/internal-ip" internalIPAnnotationKey = "kilo.squat.ai/internal-ip"
keyAnnotationKey = "kilo.squat.ai/key" keyAnnotationKey = "kilo.squat.ai/key"
lastSeenAnnotationKey = "kilo.squat.ai/last-seen" lastSeenAnnotationKey = "kilo.squat.ai/last-seen"
leaderAnnotationKey = "kilo.squat.ai/leader" leaderAnnotationKey = "kilo.squat.ai/leader"
locationAnnotationKey = "kilo.squat.ai/location" locationAnnotationKey = "kilo.squat.ai/location"
wireGuardIPAnnotationKey = "kilo.squat.ai/wireguard-ip" wireGuardIPAnnotationKey = "kilo.squat.ai/wireguard-ip"
wireGuardPersistentKeepAliveKey = "kilo.squat.ai/wireguard-persistent-keepalive"
regionLabelKey = "topology.kubernetes.io/region" regionLabelKey = "topology.kubernetes.io/region"
jsonPatchSlash = "~1" jsonPatchSlash = "~1"
@ -262,6 +263,15 @@ func translateNode(node *v1.Node) *mesh.Node {
if !ok { if !ok {
internalIP = node.ObjectMeta.Annotations[internalIPAnnotationKey] internalIP = node.ObjectMeta.Annotations[internalIPAnnotationKey]
} }
// Set Wireguard PersistentKeepAliveKey.
var wireGuardPersistentKeepAlive int64
if wgKeepAlive, ok := node.ObjectMeta.Annotations[wireGuardIPAnnotationKey]; !ok {
wireGuardPersistentKeepAlive = 0
} else {
if wireGuardPersistentKeepAlive, err = strconv.ParseInt(wgKeepAlive, 10, 64); err != nil {
wireGuardPersistentKeepAlive = 0
}
}
var lastSeen int64 var lastSeen int64
if ls, ok := node.ObjectMeta.Annotations[lastSeenAnnotationKey]; !ok { if ls, ok := node.ObjectMeta.Annotations[lastSeenAnnotationKey]; !ok {
lastSeen = 0 lastSeen = 0
@ -286,7 +296,8 @@ func translateNode(node *v1.Node) *mesh.Node {
// WireGuardIP can fail to parse if the node is not a leader or if // WireGuardIP can fail to parse if the node is not a leader or if
// the node's agent has not yet reconciled. In either case, the IP // the node's agent has not yet reconciled. In either case, the IP
// will parse as nil. // will parse as nil.
WireGuardIP: normalizeIP(node.ObjectMeta.Annotations[wireGuardIPAnnotationKey]), WireGuardIP: normalizeIP(node.ObjectMeta.Annotations[wireGuardIPAnnotationKey]),
WireGuardPersistentKeepAlive: wireGuardPersistentKeepAlive,
} }
} }

View File

@ -111,6 +111,15 @@ func TestTranslateNode(t *testing.T) {
ExternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)}, ExternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
}, },
}, },
{
name: "wireguard persistent keepalive override",
annotations: map[string]string{
wireGuardPersistentKeepAliveKey: "25",
},
out: &mesh.Node{
WireGuardPersistentKeepAlive: 25,
},
},
{ {
name: "internal IP override", name: "internal IP override",
annotations: map[string]string{ annotations: map[string]string{
@ -131,28 +140,30 @@ func TestTranslateNode(t *testing.T) {
{ {
name: "complete", name: "complete",
annotations: map[string]string{ annotations: map[string]string{
externalIPAnnotationKey: "10.0.0.1/24", externalIPAnnotationKey: "10.0.0.1/24",
forceExternalIPAnnotationKey: "10.0.0.2/24", forceExternalIPAnnotationKey: "10.0.0.2/24",
forceInternalIPAnnotationKey: "10.1.0.2/32", forceInternalIPAnnotationKey: "10.1.0.2/32",
internalIPAnnotationKey: "10.1.0.1/32", internalIPAnnotationKey: "10.1.0.1/32",
keyAnnotationKey: "foo", keyAnnotationKey: "foo",
lastSeenAnnotationKey: "1000000000", lastSeenAnnotationKey: "1000000000",
leaderAnnotationKey: "", leaderAnnotationKey: "",
locationAnnotationKey: "b", locationAnnotationKey: "b",
wireGuardIPAnnotationKey: "10.4.0.1/16", wireGuardIPAnnotationKey: "10.4.0.1/16",
wireGuardPersistentKeepAliveKey: "25",
}, },
labels: map[string]string{ labels: map[string]string{
regionLabelKey: "a", regionLabelKey: "a",
}, },
out: &mesh.Node{ out: &mesh.Node{
ExternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)}, ExternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
InternalIP: &net.IPNet{IP: net.ParseIP("10.1.0.2"), Mask: net.CIDRMask(32, 32)}, InternalIP: &net.IPNet{IP: net.ParseIP("10.1.0.2"), Mask: net.CIDRMask(32, 32)},
Key: []byte("foo"), Key: []byte("foo"),
LastSeen: 1000000000, LastSeen: 1000000000,
Leader: true, Leader: true,
Location: "b", Location: "b",
Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)}, Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
WireGuardIP: &net.IPNet{IP: net.ParseIP("10.4.0.1"), Mask: net.CIDRMask(16, 32)}, WireGuardIP: &net.IPNet{IP: net.ParseIP("10.4.0.1"), Mask: net.CIDRMask(16, 32)},
WireGuardPersistentKeepAlive: 25,
}, },
subnet: "10.2.1.0/24", subnet: "10.2.1.0/24",
}, },

View File

@ -79,11 +79,12 @@ type Node struct {
LastSeen int64 LastSeen int64
// Leader is a suggestion to Kilo that // Leader is a suggestion to Kilo that
// the node wants to lead its segment. // the node wants to lead its segment.
Leader bool Leader bool
Location string Location string
Name string Name string
Subnet *net.IPNet Subnet *net.IPNet
WireGuardIP *net.IPNet WireGuardIP *net.IPNet
WireGuardPersistentKeepAlive int64
} }
// Ready indicates whether or not the node is ready. // Ready indicates whether or not the node is ready.