backend.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2019 the Kilo authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mesh
  15. import (
  16. "net"
  17. "time"
  18. "github.com/squat/kilo/pkg/wireguard"
  19. )
  20. const (
  21. // checkInPeriod is how often nodes should check-in.
  22. checkInPeriod = 30 * time.Second
  23. // DefaultKiloInterface is the default interface created and used by Kilo.
  24. DefaultKiloInterface = "kilo0"
  25. // DefaultKiloPort is the default UDP port Kilo uses.
  26. DefaultKiloPort = 51820
  27. // DefaultCNIPath is the default path to the CNI config file.
  28. DefaultCNIPath = "/etc/cni/net.d/10-kilo.conflist"
  29. )
  30. // DefaultKiloSubnet is the default CIDR for Kilo.
  31. var DefaultKiloSubnet = &net.IPNet{IP: []byte{10, 4, 0, 0}, Mask: []byte{255, 255, 0, 0}}
  32. // Granularity represents the abstraction level at which the network
  33. // should be meshed.
  34. type Granularity string
  35. const (
  36. // LogicalGranularity indicates that the network should create
  37. // a mesh between logical locations, e.g. data-centers, but not between
  38. // all nodes within a single location.
  39. LogicalGranularity Granularity = "location"
  40. // FullGranularity indicates that the network should create
  41. // a mesh between every node.
  42. FullGranularity Granularity = "full"
  43. )
  44. // Node represents a node in the network.
  45. type Node struct {
  46. Endpoint *wireguard.Endpoint
  47. Key []byte
  48. NoInternalIP bool
  49. InternalIP *net.IPNet
  50. // LastSeen is a Unix time for the last time
  51. // the node confirmed it was live.
  52. LastSeen int64
  53. // Leader is a suggestion to Kilo that
  54. // the node wants to lead its segment.
  55. Leader bool
  56. Location string
  57. Name string
  58. PersistentKeepalive int
  59. Subnet *net.IPNet
  60. WireGuardIP *net.IPNet
  61. DiscoveredEndpoints map[string]*wireguard.Endpoint
  62. }
  63. // Ready indicates whether or not the node is ready.
  64. func (n *Node) Ready() bool {
  65. // Nodes that are not leaders will not have WireGuardIPs, so it is not required.
  66. return n != nil && n.Endpoint != nil && !(n.Endpoint.IP == nil && n.Endpoint.DNS == "") && n.Endpoint.Port != 0 && n.Key != nil && n.Subnet != nil && time.Now().Unix()-n.LastSeen < int64(checkInPeriod)*2/int64(time.Second)
  67. }
  68. // Peer represents a peer in the network.
  69. type Peer struct {
  70. wireguard.Peer
  71. Name string
  72. }
  73. // Ready indicates whether or not the peer is ready.
  74. // Peers can have empty endpoints because they may not have an
  75. // IP, for example if they are behind a NAT, and thus
  76. // will not declare their endpoint and instead allow it to be
  77. // discovered.
  78. func (p *Peer) Ready() bool {
  79. return p != nil && p.AllowedIPs != nil && len(p.AllowedIPs) != 0 && p.PublicKey != nil
  80. }
  81. // EventType describes what kind of an action an event represents.
  82. type EventType string
  83. const (
  84. // AddEvent represents an action where an item was added.
  85. AddEvent EventType = "add"
  86. // DeleteEvent represents an action where an item was removed.
  87. DeleteEvent EventType = "delete"
  88. // UpdateEvent represents an action where an item was updated.
  89. UpdateEvent EventType = "update"
  90. )
  91. // NodeEvent represents an event concerning a node in the cluster.
  92. type NodeEvent struct {
  93. Type EventType
  94. Node *Node
  95. Old *Node
  96. }
  97. // PeerEvent represents an event concerning a peer in the cluster.
  98. type PeerEvent struct {
  99. Type EventType
  100. Peer *Peer
  101. Old *Peer
  102. }
  103. // Backend can create clients for all of the
  104. // primitive types that Kilo deals with, namely:
  105. // * nodes; and
  106. // * peers.
  107. type Backend interface {
  108. Nodes() NodeBackend
  109. Peers() PeerBackend
  110. }
  111. // NodeBackend can get nodes by name, init itself,
  112. // list the nodes that should be meshed,
  113. // set Kilo properties for a node,
  114. // clean up any changes applied to the backend,
  115. // and watch for changes to nodes.
  116. type NodeBackend interface {
  117. CleanUp(string) error
  118. Get(string) (*Node, error)
  119. Init(<-chan struct{}) error
  120. List() ([]*Node, error)
  121. Set(string, *Node) error
  122. Watch() <-chan *NodeEvent
  123. }
  124. // PeerBackend can get peers by name, init itself,
  125. // list the peers that should be in the mesh,
  126. // set fields for a peer,
  127. // clean up any changes applied to the backend,
  128. // and watch for changes to peers.
  129. type PeerBackend interface {
  130. CleanUp(string) error
  131. Get(string) (*Peer, error)
  132. Init(<-chan struct{}) error
  133. List() ([]*Peer, error)
  134. Set(string, *Peer) error
  135. Watch() <-chan *PeerEvent
  136. }