backend.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  19. "github.com/squat/kilo/pkg/wireguard"
  20. )
  21. const (
  22. // checkInPeriod is how often nodes should check-in.
  23. checkInPeriod = 30 * time.Second
  24. // DefaultKiloInterface is the default interface created and used by Kilo.
  25. DefaultKiloInterface = "kilo0"
  26. // DefaultKiloPort is the default UDP port Kilo uses.
  27. DefaultKiloPort = 51820
  28. // DefaultCNIPath is the default path to the CNI config file.
  29. DefaultCNIPath = "/etc/cni/net.d/10-kilo.conflist"
  30. )
  31. // DefaultKiloSubnet is the default CIDR for Kilo.
  32. var DefaultKiloSubnet = &net.IPNet{IP: []byte{10, 4, 0, 0}, Mask: []byte{255, 255, 0, 0}}
  33. // Granularity represents the abstraction level at which the network
  34. // should be meshed.
  35. type Granularity string
  36. const (
  37. // LogicalGranularity indicates that the network should create
  38. // a mesh between logical locations, e.g. data-centers, but not between
  39. // all nodes within a single location.
  40. LogicalGranularity Granularity = "location"
  41. // FullGranularity indicates that the network should create
  42. // a mesh between every node.
  43. FullGranularity Granularity = "full"
  44. // AutoGranularity can be used with kgctl to obtain
  45. // the granularity automatically.
  46. AutoGranularity Granularity = "auto"
  47. )
  48. // Node represents a node in the network.
  49. type Node struct {
  50. Endpoint *wireguard.Endpoint
  51. Key wgtypes.Key
  52. NoInternalIP bool
  53. InternalIP *net.IPNet
  54. // LastSeen is a Unix time for the last time
  55. // the node confirmed it was live.
  56. LastSeen int64
  57. // Leader is a suggestion to Kilo that
  58. // the node wants to lead its segment.
  59. Leader bool
  60. Location string
  61. Name string
  62. PersistentKeepalive time.Duration
  63. Subnet *net.IPNet
  64. WireGuardIP *net.IPNet
  65. // DiscoveredEndpoints cannot be DNS endpoints, only net.UDPAddr.
  66. DiscoveredEndpoints map[string]*net.UDPAddr
  67. AllowedLocationIPs []net.IPNet
  68. Granularity Granularity
  69. }
  70. // Ready indicates whether or not the node is ready.
  71. func (n *Node) Ready() bool {
  72. // Nodes that are not leaders will not have WireGuardIPs, so it is not required.
  73. return n != nil &&
  74. n.Endpoint.Ready() &&
  75. n.Key != wgtypes.Key{} &&
  76. n.Subnet != nil &&
  77. time.Now().Unix()-n.LastSeen < int64(checkInPeriod)*2/int64(time.Second)
  78. }
  79. // Peer represents a peer in the network.
  80. type Peer struct {
  81. wireguard.Peer
  82. Name string
  83. }
  84. // Ready indicates whether or not the peer is ready.
  85. // Peers can have empty endpoints because they may not have an
  86. // IP, for example if they are behind a NAT, and thus
  87. // will not declare their endpoint and instead allow it to be
  88. // discovered.
  89. func (p *Peer) Ready() bool {
  90. return p != nil &&
  91. p.AllowedIPs != nil &&
  92. len(p.AllowedIPs) != 0 &&
  93. p.PublicKey != wgtypes.Key{} // If Key was not set, it will be wgtypes.Key{}.
  94. }
  95. // EventType describes what kind of an action an event represents.
  96. type EventType string
  97. const (
  98. // AddEvent represents an action where an item was added.
  99. AddEvent EventType = "add"
  100. // DeleteEvent represents an action where an item was removed.
  101. DeleteEvent EventType = "delete"
  102. // UpdateEvent represents an action where an item was updated.
  103. UpdateEvent EventType = "update"
  104. )
  105. // NodeEvent represents an event concerning a node in the cluster.
  106. type NodeEvent struct {
  107. Type EventType
  108. Node *Node
  109. Old *Node
  110. }
  111. // PeerEvent represents an event concerning a peer in the cluster.
  112. type PeerEvent struct {
  113. Type EventType
  114. Peer *Peer
  115. Old *Peer
  116. }
  117. // Backend can create clients for all of the
  118. // primitive types that Kilo deals with, namely:
  119. // * nodes; and
  120. // * peers.
  121. type Backend interface {
  122. Nodes() NodeBackend
  123. Peers() PeerBackend
  124. }
  125. // NodeBackend can get nodes by name, init itself,
  126. // list the nodes that should be meshed,
  127. // set Kilo properties for a node,
  128. // clean up any changes applied to the backend,
  129. // and watch for changes to nodes.
  130. type NodeBackend interface {
  131. CleanUp(string) error
  132. Get(string) (*Node, error)
  133. Init(<-chan struct{}) error
  134. List() ([]*Node, error)
  135. Set(string, *Node) error
  136. Watch() <-chan *NodeEvent
  137. }
  138. // PeerBackend can get peers by name, init itself,
  139. // list the peers that should be in the mesh,
  140. // set fields for a peer,
  141. // clean up any changes applied to the backend,
  142. // and watch for changes to peers.
  143. type PeerBackend interface {
  144. CleanUp(string) error
  145. Get(string) (*Peer, error)
  146. Init(<-chan struct{}) error
  147. List() ([]*Peer, error)
  148. Set(string, *Peer) error
  149. Watch() <-chan *PeerEvent
  150. }