backend.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. }
  62. // Ready indicates whether or not the node is ready.
  63. func (n *Node) Ready() bool {
  64. // Nodes that are not leaders will not have WireGuardIPs, so it is not required.
  65. 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)
  66. }
  67. // Peer represents a peer in the network.
  68. type Peer struct {
  69. wireguard.Peer
  70. Name string
  71. }
  72. // Ready indicates whether or not the peer is ready.
  73. // Peers can have empty endpoints because they may not have an
  74. // IP, for example if they are behind a NAT, and thus
  75. // will not declare their endpoint and instead allow it to be
  76. // discovered.
  77. func (p *Peer) Ready() bool {
  78. return p != nil && p.AllowedIPs != nil && len(p.AllowedIPs) != 0 && p.PublicKey != nil
  79. }
  80. // EventType describes what kind of an action an event represents.
  81. type EventType string
  82. const (
  83. // AddEvent represents an action where an item was added.
  84. AddEvent EventType = "add"
  85. // DeleteEvent represents an action where an item was removed.
  86. DeleteEvent EventType = "delete"
  87. // UpdateEvent represents an action where an item was updated.
  88. UpdateEvent EventType = "update"
  89. )
  90. // NodeEvent represents an event concerning a node in the cluster.
  91. type NodeEvent struct {
  92. Type EventType
  93. Node *Node
  94. Old *Node
  95. }
  96. // PeerEvent represents an event concerning a peer in the cluster.
  97. type PeerEvent struct {
  98. Type EventType
  99. Peer *Peer
  100. Old *Peer
  101. }
  102. // Backend can create clients for all of the
  103. // primitive types that Kilo deals with, namely:
  104. // * nodes; and
  105. // * peers.
  106. type Backend interface {
  107. Nodes() NodeBackend
  108. Peers() PeerBackend
  109. }
  110. // NodeBackend can get nodes by name, init itself,
  111. // list the nodes that should be meshed,
  112. // set Kilo properties for a node,
  113. // clean up any changes applied to the backend,
  114. // and watch for changes to nodes.
  115. type NodeBackend interface {
  116. CleanUp(string) error
  117. Get(string) (*Node, error)
  118. Init(<-chan struct{}) error
  119. List() ([]*Node, error)
  120. Set(string, *Node) error
  121. Watch() <-chan *NodeEvent
  122. }
  123. // PeerBackend can get peers by name, init itself,
  124. // list the peers that should be in the mesh,
  125. // set fields for a peer,
  126. // clean up any changes applied to the backend,
  127. // and watch for changes to peers.
  128. type PeerBackend interface {
  129. CleanUp(string) error
  130. Get(string) (*Peer, error)
  131. Init(<-chan struct{}) error
  132. List() ([]*Peer, error)
  133. Set(string, *Peer) error
  134. Watch() <-chan *PeerEvent
  135. }