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

@ -57,6 +57,7 @@ const (
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
@ -287,6 +297,7 @@ func translateNode(node *v1.Node) *mesh.Node {
// 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{
@ -140,6 +149,7 @@ func TestTranslateNode(t *testing.T) {
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",
@ -153,6 +163,7 @@ func TestTranslateNode(t *testing.T) {
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

@ -84,6 +84,7 @@ type Node struct {
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.