diff --git a/pkg/k8s/backend.go b/pkg/k8s/backend.go index a368f2b..2317cd5 100644 --- a/pkg/k8s/backend.go +++ b/pkg/k8s/backend.go @@ -47,17 +47,17 @@ import ( const ( // Backend is the name of this mesh backend. - Backend = "kubernetes" - externalIPAnnotationKey = "kilo.squat.ai/external-ip" - forceExternalIPAnnotationKey = "kilo.squat.ai/force-external-ip" - forceInternalIPAnnotationKey = "kilo.squat.ai/force-internal-ip" - internalIPAnnotationKey = "kilo.squat.ai/internal-ip" - keyAnnotationKey = "kilo.squat.ai/key" - lastSeenAnnotationKey = "kilo.squat.ai/last-seen" - leaderAnnotationKey = "kilo.squat.ai/leader" - locationAnnotationKey = "kilo.squat.ai/location" - wireGuardIPAnnotationKey = "kilo.squat.ai/wireguard-ip" - wireGuardPersistentKeepAliveKey = "kilo.squat.ai/wireguard-persistent-keepalive" + Backend = "kubernetes" + externalIPAnnotationKey = "kilo.squat.ai/external-ip" + forceExternalIPAnnotationKey = "kilo.squat.ai/force-external-ip" + forceInternalIPAnnotationKey = "kilo.squat.ai/force-internal-ip" + internalIPAnnotationKey = "kilo.squat.ai/internal-ip" + keyAnnotationKey = "kilo.squat.ai/key" + lastSeenAnnotationKey = "kilo.squat.ai/last-seen" + leaderAnnotationKey = "kilo.squat.ai/leader" + locationAnnotationKey = "kilo.squat.ai/location" + persistentKeepAliveKey = "kilo.squat.ai/persistent-keepalive" + wireGuardIPAnnotationKey = "kilo.squat.ai/wireguard-ip" regionLabelKey = "topology.kubernetes.io/region" jsonPatchSlash = "~1" @@ -264,12 +264,12 @@ func translateNode(node *v1.Node) *mesh.Node { internalIP = node.ObjectMeta.Annotations[internalIPAnnotationKey] } // Set Wireguard PersistentKeepAlive setting for the node. - var wireGuardPersistentKeepAlive int64 - if wgKeepAlive, ok := node.ObjectMeta.Annotations[wireGuardPersistentKeepAliveKey]; !ok { - wireGuardPersistentKeepAlive = 0 + var persistentKeepAlive int64 + if keepAlive, ok := node.ObjectMeta.Annotations[persistentKeepAliveKey]; !ok { + persistentKeepAlive = 0 } else { - if wireGuardPersistentKeepAlive, err = strconv.ParseInt(wgKeepAlive, 10, 64); err != nil { - wireGuardPersistentKeepAlive = 0 + if persistentKeepAlive, err = strconv.ParseInt(keepAlive, 10, 64); err != nil { + persistentKeepAlive = 0 } } var lastSeen int64 @@ -285,19 +285,19 @@ func translateNode(node *v1.Node) *mesh.Node { // remote node's agent has not yet set its IP address; // in this case the IP will be nil and // the mesh can wait for the node to be updated. - ExternalIP: normalizeIP(externalIP), - InternalIP: normalizeIP(internalIP), - Key: []byte(node.ObjectMeta.Annotations[keyAnnotationKey]), - LastSeen: lastSeen, - Leader: leader, - Location: location, - Name: node.Name, - Subnet: subnet, + ExternalIP: normalizeIP(externalIP), + InternalIP: normalizeIP(internalIP), + Key: []byte(node.ObjectMeta.Annotations[keyAnnotationKey]), + LastSeen: lastSeen, + Leader: leader, + Location: location, + Name: node.Name, + PersistentKeepAlive: int(persistentKeepAlive), + Subnet: subnet, // 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 // will parse as nil. - WireGuardIP: normalizeIP(node.ObjectMeta.Annotations[wireGuardIPAnnotationKey]), - WireGuardPersistentKeepAlive: int(wireGuardPersistentKeepAlive), + WireGuardIP: normalizeIP(node.ObjectMeta.Annotations[wireGuardIPAnnotationKey]), } } diff --git a/pkg/k8s/backend_test.go b/pkg/k8s/backend_test.go index 2dbbf22..c6e96bf 100644 --- a/pkg/k8s/backend_test.go +++ b/pkg/k8s/backend_test.go @@ -114,10 +114,10 @@ func TestTranslateNode(t *testing.T) { { name: "wireguard persistent keepalive override", annotations: map[string]string{ - wireGuardPersistentKeepAliveKey: "25", + persistentKeepAliveKey: "25", }, out: &mesh.Node{ - WireGuardPersistentKeepAlive: 25, + PersistentKeepAlive: 25, }, }, { @@ -140,30 +140,30 @@ func TestTranslateNode(t *testing.T) { { name: "complete", annotations: map[string]string{ - externalIPAnnotationKey: "10.0.0.1/24", - forceExternalIPAnnotationKey: "10.0.0.2/24", - forceInternalIPAnnotationKey: "10.1.0.2/32", - internalIPAnnotationKey: "10.1.0.1/32", - keyAnnotationKey: "foo", - lastSeenAnnotationKey: "1000000000", - leaderAnnotationKey: "", - locationAnnotationKey: "b", - wireGuardIPAnnotationKey: "10.4.0.1/16", - wireGuardPersistentKeepAliveKey: "25", + externalIPAnnotationKey: "10.0.0.1/24", + forceExternalIPAnnotationKey: "10.0.0.2/24", + forceInternalIPAnnotationKey: "10.1.0.2/32", + internalIPAnnotationKey: "10.1.0.1/32", + keyAnnotationKey: "foo", + lastSeenAnnotationKey: "1000000000", + leaderAnnotationKey: "", + locationAnnotationKey: "b", + persistentKeepAliveKey: "25", + wireGuardIPAnnotationKey: "10.4.0.1/16", }, labels: map[string]string{ regionLabelKey: "a", }, out: &mesh.Node{ - 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)}, - Key: []byte("foo"), - LastSeen: 1000000000, - Leader: true, - Location: "b", - 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)}, - WireGuardPersistentKeepAlive: 25, + 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)}, + Key: []byte("foo"), + LastSeen: 1000000000, + Leader: true, + Location: "b", + PersistentKeepAlive: 25, + 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)}, }, subnet: "10.2.1.0/24", }, diff --git a/pkg/mesh/mesh.go b/pkg/mesh/mesh.go index 5553886..4b84f2d 100644 --- a/pkg/mesh/mesh.go +++ b/pkg/mesh/mesh.go @@ -79,12 +79,12 @@ type Node struct { LastSeen int64 // Leader is a suggestion to Kilo that // the node wants to lead its segment. - Leader bool - Location string - Name string - Subnet *net.IPNet - WireGuardIP *net.IPNet - WireGuardPersistentKeepAlive int + Leader bool + Location string + Name string + PersistentKeepAlive int + Subnet *net.IPNet + WireGuardIP *net.IPNet } // Ready indicates whether or not the node is ready. diff --git a/pkg/mesh/topology.go b/pkg/mesh/topology.go index 6cb2d55..e237745 100644 --- a/pkg/mesh/topology.go +++ b/pkg/mesh/topology.go @@ -64,14 +64,14 @@ type segment struct { hostnames []string // leader is the index of the leader of the segment. leader int + // persistentKeepAlive is the interval in seconds of the emission + // of keepalive packets to the peer. + persistentKeepAlive int // privateIPs is a slice of private IPs of all peers in the segment. privateIPs []net.IP // wireGuardIP is the allocated IP address of the WireGuard // interface on the leader of the segment. wireGuardIP net.IP - // wireGuardPersistentKeepAlive is the interval in seconds of the emission - // of keepalive packets to the peer. - wireGuardPersistentKeepAlive int } // NewTopology creates a new Topology struct from a given set of nodes and peers. @@ -120,15 +120,15 @@ func NewTopology(nodes map[string]*Node, peers map[string]*Peer, granularity Gra privateIPs = append(privateIPs, node.InternalIP.IP) } t.segments = append(t.segments, &segment{ - allowedIPs: allowedIPs, - endpoint: topoMap[location][leader].ExternalIP.IP, - key: topoMap[location][leader].Key, - location: location, - cidrs: cidrs, - hostnames: hostnames, - leader: leader, - privateIPs: privateIPs, - wireGuardPersistentKeepAlive: topoMap[location][leader].WireGuardPersistentKeepAlive, + allowedIPs: allowedIPs, + endpoint: topoMap[location][leader].ExternalIP.IP, + key: topoMap[location][leader].Key, + location: location, + cidrs: cidrs, + hostnames: hostnames, + leader: leader, + privateIPs: privateIPs, + persistentKeepAlive: topoMap[location][leader].PersistentKeepAlive, }) } // Sort the Topology segments so the result is stable. @@ -339,7 +339,7 @@ func (t *Topology) Conf() *wireguard.Conf { Port: uint32(t.port), }, PublicKey: s.key, - PersistentKeepalive: s.wireGuardPersistentKeepAlive, + PersistentKeepalive: s.persistentKeepAlive, } c.Peers = append(c.Peers, peer) }