backend.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // AutoGranularity can be used with kgctl to obtain
  44. // the granularity automatically.
  45. AutoGranularity Granularity = "auto"
  46. )
  47. // Node represents a node in the network.
  48. type Node struct {
  49. Endpoint *wireguard.Endpoint
  50. Key []byte
  51. NoInternalIP bool
  52. InternalIP *net.IPNet
  53. // LastSeen is a Unix time for the last time
  54. // the node confirmed it was live.
  55. LastSeen int64
  56. // Leader is a suggestion to Kilo that
  57. // the node wants to lead its segment.
  58. Leader bool
  59. Location string
  60. Name string
  61. PersistentKeepalive int
  62. Subnet *net.IPNet
  63. WireGuardIP *net.IPNet
  64. DiscoveredEndpoints map[string]*wireguard.Endpoint
  65. AllowedLocationIPs []*net.IPNet
  66. Granularity Granularity
  67. }
  68. // Ready indicates whether or not the node is ready.
  69. func (n *Node) Ready() bool {
  70. // Nodes that are not leaders will not have WireGuardIPs, so it is not required.
  71. 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)
  72. }
  73. // Peer represents a peer in the network.
  74. type Peer struct {
  75. wireguard.Peer
  76. Name string
  77. }
  78. // Ready indicates whether or not the peer is ready.
  79. // Peers can have empty endpoints because they may not have an
  80. // IP, for example if they are behind a NAT, and thus
  81. // will not declare their endpoint and instead allow it to be
  82. // discovered.
  83. func (p *Peer) Ready() bool {
  84. return p != nil && p.AllowedIPs != nil && len(p.AllowedIPs) != 0 && p.PublicKey != nil
  85. }
  86. // EventType describes what kind of an action an event represents.
  87. type EventType string
  88. const (
  89. // AddEvent represents an action where an item was added.
  90. AddEvent EventType = "add"
  91. // DeleteEvent represents an action where an item was removed.
  92. DeleteEvent EventType = "delete"
  93. // UpdateEvent represents an action where an item was updated.
  94. UpdateEvent EventType = "update"
  95. )
  96. // NodeEvent represents an event concerning a node in the cluster.
  97. type NodeEvent struct {
  98. Type EventType
  99. Node *Node
  100. Old *Node
  101. }
  102. // PeerEvent represents an event concerning a peer in the cluster.
  103. type PeerEvent struct {
  104. Type EventType
  105. Peer *Peer
  106. Old *Peer
  107. }
  108. // Backend can create clients for all of the
  109. // primitive types that Kilo deals with, namely:
  110. // * nodes; and
  111. // * peers.
  112. type Backend interface {
  113. Nodes() NodeBackend
  114. Peers() PeerBackend
  115. }
  116. // NodeBackend can get nodes by name, init itself,
  117. // list the nodes that should be meshed,
  118. // set Kilo properties for a node,
  119. // clean up any changes applied to the backend,
  120. // and watch for changes to nodes.
  121. type NodeBackend interface {
  122. CleanUp(string) error
  123. Get(string) (*Node, error)
  124. Init(<-chan struct{}) error
  125. List() ([]*Node, error)
  126. Set(string, *Node) error
  127. Watch() <-chan *NodeEvent
  128. }
  129. // PeerBackend can get peers by name, init itself,
  130. // list the peers that should be in the mesh,
  131. // set fields for a peer,
  132. // clean up any changes applied to the backend,
  133. // and watch for changes to peers.
  134. type PeerBackend interface {
  135. CleanUp(string) error
  136. Get(string) (*Peer, error)
  137. Init(<-chan struct{}) error
  138. List() ([]*Peer, error)
  139. Set(string, *Peer) error
  140. Watch() <-chan *PeerEvent
  141. }