backend.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 k8s
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "net"
  20. "path"
  21. "strconv"
  22. "strings"
  23. "time"
  24. crdutils "github.com/ant31/crd-validation/pkg"
  25. v1 "k8s.io/api/core/v1"
  26. "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
  27. apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
  28. apierrors "k8s.io/apimachinery/pkg/api/errors"
  29. "k8s.io/apimachinery/pkg/labels"
  30. "k8s.io/apimachinery/pkg/types"
  31. "k8s.io/apimachinery/pkg/util/strategicpatch"
  32. v1informers "k8s.io/client-go/informers/core/v1"
  33. "k8s.io/client-go/kubernetes"
  34. v1listers "k8s.io/client-go/listers/core/v1"
  35. "k8s.io/client-go/tools/cache"
  36. "github.com/squat/kilo/pkg/k8s/apis/kilo/v1alpha1"
  37. kiloclient "github.com/squat/kilo/pkg/k8s/clientset/versioned"
  38. v1alpha1informers "github.com/squat/kilo/pkg/k8s/informers/kilo/v1alpha1"
  39. v1alpha1listers "github.com/squat/kilo/pkg/k8s/listers/kilo/v1alpha1"
  40. "github.com/squat/kilo/pkg/mesh"
  41. "github.com/squat/kilo/pkg/wireguard"
  42. )
  43. const (
  44. // Backend is the name of this mesh backend.
  45. Backend = "kubernetes"
  46. externalIPAnnotationKey = "kilo.squat.ai/external-ip"
  47. forceExternalIPAnnotationKey = "kilo.squat.ai/force-external-ip"
  48. forceInternalIPAnnotationKey = "kilo.squat.ai/force-internal-ip"
  49. internalIPAnnotationKey = "kilo.squat.ai/internal-ip"
  50. keyAnnotationKey = "kilo.squat.ai/key"
  51. lastSeenAnnotationKey = "kilo.squat.ai/last-seen"
  52. leaderAnnotationKey = "kilo.squat.ai/leader"
  53. locationAnnotationKey = "kilo.squat.ai/location"
  54. wireGuardIPAnnotationKey = "kilo.squat.ai/wireguard-ip"
  55. regionLabelKey = "failure-domain.beta.kubernetes.io/region"
  56. jsonPatchSlash = "~1"
  57. jsonRemovePatch = `{"op": "remove", "path": "%s"}`
  58. )
  59. type backend struct {
  60. nodes *nodeBackend
  61. peers *peerBackend
  62. }
  63. // Nodes implements the mesh.Backend interface.
  64. func (b *backend) Nodes() mesh.NodeBackend {
  65. return b.nodes
  66. }
  67. // Peers implements the mesh.Backend interface.
  68. func (b *backend) Peers() mesh.PeerBackend {
  69. return b.peers
  70. }
  71. type nodeBackend struct {
  72. client kubernetes.Interface
  73. events chan *mesh.NodeEvent
  74. informer cache.SharedIndexInformer
  75. lister v1listers.NodeLister
  76. }
  77. type peerBackend struct {
  78. client kiloclient.Interface
  79. extensionsClient apiextensions.Interface
  80. events chan *mesh.PeerEvent
  81. informer cache.SharedIndexInformer
  82. lister v1alpha1listers.PeerLister
  83. }
  84. // New creates a new instance of a mesh.Backend.
  85. func New(c kubernetes.Interface, kc kiloclient.Interface, ec apiextensions.Interface) mesh.Backend {
  86. ni := v1informers.NewNodeInformer(c, 5*time.Minute, nil)
  87. pi := v1alpha1informers.NewPeerInformer(kc, 5*time.Minute, nil)
  88. return &backend{
  89. &nodeBackend{
  90. client: c,
  91. events: make(chan *mesh.NodeEvent),
  92. informer: ni,
  93. lister: v1listers.NewNodeLister(ni.GetIndexer()),
  94. },
  95. &peerBackend{
  96. client: kc,
  97. extensionsClient: ec,
  98. events: make(chan *mesh.PeerEvent),
  99. informer: pi,
  100. lister: v1alpha1listers.NewPeerLister(pi.GetIndexer()),
  101. },
  102. }
  103. }
  104. // CleanUp removes configuration applied to the backend.
  105. func (nb *nodeBackend) CleanUp(name string) error {
  106. patch := []byte("[" + strings.Join([]string{
  107. fmt.Sprintf(jsonRemovePatch, path.Join("/metadata", "annotations", strings.Replace(externalIPAnnotationKey, "/", jsonPatchSlash, 1))),
  108. fmt.Sprintf(jsonRemovePatch, path.Join("/metadata", "annotations", strings.Replace(internalIPAnnotationKey, "/", jsonPatchSlash, 1))),
  109. fmt.Sprintf(jsonRemovePatch, path.Join("/metadata", "annotations", strings.Replace(keyAnnotationKey, "/", jsonPatchSlash, 1))),
  110. fmt.Sprintf(jsonRemovePatch, path.Join("/metadata", "annotations", strings.Replace(lastSeenAnnotationKey, "/", jsonPatchSlash, 1))),
  111. fmt.Sprintf(jsonRemovePatch, path.Join("/metadata", "annotations", strings.Replace(wireGuardIPAnnotationKey, "/", jsonPatchSlash, 1))),
  112. }, ",") + "]")
  113. if _, err := nb.client.CoreV1().Nodes().Patch(name, types.JSONPatchType, patch); err != nil {
  114. return fmt.Errorf("failed to patch node: %v", err)
  115. }
  116. return nil
  117. }
  118. // Get gets a single Node by name.
  119. func (nb *nodeBackend) Get(name string) (*mesh.Node, error) {
  120. n, err := nb.lister.Get(name)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return translateNode(n), nil
  125. }
  126. // Init initializes the backend; for this backend that means
  127. // syncing the informer cache.
  128. func (nb *nodeBackend) Init(stop <-chan struct{}) error {
  129. go nb.informer.Run(stop)
  130. if ok := cache.WaitForCacheSync(stop, func() bool {
  131. return nb.informer.HasSynced()
  132. }); !ok {
  133. return errors.New("failed to sync node cache")
  134. }
  135. nb.informer.AddEventHandler(
  136. cache.ResourceEventHandlerFuncs{
  137. AddFunc: func(obj interface{}) {
  138. n, ok := obj.(*v1.Node)
  139. if !ok {
  140. // Failed to decode Node; ignoring...
  141. return
  142. }
  143. nb.events <- &mesh.NodeEvent{Type: mesh.AddEvent, Node: translateNode(n)}
  144. },
  145. UpdateFunc: func(old, obj interface{}) {
  146. n, ok := obj.(*v1.Node)
  147. if !ok {
  148. // Failed to decode Node; ignoring...
  149. return
  150. }
  151. o, ok := old.(*v1.Node)
  152. if !ok {
  153. // Failed to decode Node; ignoring...
  154. return
  155. }
  156. nb.events <- &mesh.NodeEvent{Type: mesh.UpdateEvent, Node: translateNode(n), Old: translateNode(o)}
  157. },
  158. DeleteFunc: func(obj interface{}) {
  159. n, ok := obj.(*v1.Node)
  160. if !ok {
  161. // Failed to decode Node; ignoring...
  162. return
  163. }
  164. nb.events <- &mesh.NodeEvent{Type: mesh.DeleteEvent, Node: translateNode(n)}
  165. },
  166. },
  167. )
  168. return nil
  169. }
  170. // List gets all the Nodes in the cluster.
  171. func (nb *nodeBackend) List() ([]*mesh.Node, error) {
  172. ns, err := nb.lister.List(labels.Everything())
  173. if err != nil {
  174. return nil, err
  175. }
  176. nodes := make([]*mesh.Node, len(ns))
  177. for i := range ns {
  178. nodes[i] = translateNode(ns[i])
  179. }
  180. return nodes, nil
  181. }
  182. // Set sets the fields of a node.
  183. func (nb *nodeBackend) Set(name string, node *mesh.Node) error {
  184. old, err := nb.lister.Get(name)
  185. if err != nil {
  186. return fmt.Errorf("failed to find node: %v", err)
  187. }
  188. n := old.DeepCopy()
  189. n.ObjectMeta.Annotations[externalIPAnnotationKey] = node.ExternalIP.String()
  190. n.ObjectMeta.Annotations[internalIPAnnotationKey] = node.InternalIP.String()
  191. n.ObjectMeta.Annotations[keyAnnotationKey] = string(node.Key)
  192. n.ObjectMeta.Annotations[lastSeenAnnotationKey] = strconv.FormatInt(node.LastSeen, 10)
  193. if node.WireGuardIP == nil {
  194. n.ObjectMeta.Annotations[wireGuardIPAnnotationKey] = ""
  195. } else {
  196. n.ObjectMeta.Annotations[wireGuardIPAnnotationKey] = node.WireGuardIP.String()
  197. }
  198. oldData, err := json.Marshal(old)
  199. if err != nil {
  200. return err
  201. }
  202. newData, err := json.Marshal(n)
  203. if err != nil {
  204. return err
  205. }
  206. patch, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, v1.Node{})
  207. if err != nil {
  208. return fmt.Errorf("failed to create patch for node %q: %v", n.Name, err)
  209. }
  210. if _, err = nb.client.CoreV1().Nodes().Patch(name, types.StrategicMergePatchType, patch); err != nil {
  211. return fmt.Errorf("failed to patch node: %v", err)
  212. }
  213. return nil
  214. }
  215. // Watch returns a chan of node events.
  216. func (nb *nodeBackend) Watch() <-chan *mesh.NodeEvent {
  217. return nb.events
  218. }
  219. // translateNode translates a Kubernetes Node to a mesh.Node.
  220. func translateNode(node *v1.Node) *mesh.Node {
  221. if node == nil {
  222. return nil
  223. }
  224. _, subnet, err := net.ParseCIDR(node.Spec.PodCIDR)
  225. // The subnet should only ever fail to parse if the pod CIDR has not been set,
  226. // so in this case set the subnet to nil and let the node be updated.
  227. if err != nil {
  228. subnet = nil
  229. }
  230. _, leader := node.ObjectMeta.Annotations[leaderAnnotationKey]
  231. // Allow the region to be overridden by an explicit location.
  232. location, ok := node.ObjectMeta.Annotations[locationAnnotationKey]
  233. if !ok {
  234. location = node.ObjectMeta.Labels[regionLabelKey]
  235. }
  236. // Allow the IPs to be overridden.
  237. externalIP, ok := node.ObjectMeta.Annotations[forceExternalIPAnnotationKey]
  238. if !ok {
  239. externalIP = node.ObjectMeta.Annotations[externalIPAnnotationKey]
  240. }
  241. internalIP, ok := node.ObjectMeta.Annotations[forceInternalIPAnnotationKey]
  242. if !ok {
  243. internalIP = node.ObjectMeta.Annotations[internalIPAnnotationKey]
  244. }
  245. var lastSeen int64
  246. if ls, ok := node.ObjectMeta.Annotations[lastSeenAnnotationKey]; !ok {
  247. lastSeen = 0
  248. } else {
  249. if lastSeen, err = strconv.ParseInt(ls, 10, 64); err != nil {
  250. lastSeen = 0
  251. }
  252. }
  253. return &mesh.Node{
  254. // ExternalIP and InternalIP should only ever fail to parse if the
  255. // remote node's agent has not yet set its IP address;
  256. // in this case the IP will be nil and
  257. // the mesh can wait for the node to be updated.
  258. ExternalIP: normalizeIP(externalIP),
  259. InternalIP: normalizeIP(internalIP),
  260. Key: []byte(node.ObjectMeta.Annotations[keyAnnotationKey]),
  261. LastSeen: lastSeen,
  262. Leader: leader,
  263. Location: location,
  264. Name: node.Name,
  265. Subnet: subnet,
  266. // WireGuardIP can fail to parse if the node is not a leader or if
  267. // the node's agent has not yet reconciled. In either case, the IP
  268. // will parse as nil.
  269. WireGuardIP: normalizeIP(node.ObjectMeta.Annotations[wireGuardIPAnnotationKey]),
  270. }
  271. }
  272. // translatePeer translates a Peer CRD to a mesh.Peer.
  273. func translatePeer(peer *v1alpha1.Peer) *mesh.Peer {
  274. if peer == nil {
  275. return nil
  276. }
  277. var aips []*net.IPNet
  278. for _, aip := range peer.Spec.AllowedIPs {
  279. aip := normalizeIP(aip)
  280. // Skip any invalid IPs.
  281. if aip == nil {
  282. continue
  283. }
  284. aips = append(aips, aip)
  285. }
  286. var endpoint *wireguard.Endpoint
  287. if peer.Spec.Endpoint != nil {
  288. ip := net.ParseIP(peer.Spec.Endpoint.IP)
  289. if ip4 := ip.To4(); ip4 != nil {
  290. ip = ip4
  291. } else {
  292. ip = ip.To16()
  293. }
  294. if peer.Spec.Endpoint.Port > 0 && ip != nil {
  295. endpoint = &wireguard.Endpoint{
  296. IP: ip,
  297. Port: peer.Spec.Endpoint.Port,
  298. }
  299. }
  300. }
  301. var key []byte
  302. if len(peer.Spec.PublicKey) > 0 {
  303. key = []byte(peer.Spec.PublicKey)
  304. }
  305. var pka int
  306. if peer.Spec.PersistentKeepalive > 0 {
  307. pka = peer.Spec.PersistentKeepalive
  308. }
  309. return &mesh.Peer{
  310. Name: peer.Name,
  311. Peer: wireguard.Peer{
  312. AllowedIPs: aips,
  313. Endpoint: endpoint,
  314. PublicKey: key,
  315. PersistentKeepalive: pka,
  316. },
  317. }
  318. }
  319. // CleanUp removes configuration applied to the backend.
  320. func (pb *peerBackend) CleanUp(name string) error {
  321. return nil
  322. }
  323. // Get gets a single Peer by name.
  324. func (pb *peerBackend) Get(name string) (*mesh.Peer, error) {
  325. p, err := pb.lister.Get(name)
  326. if err != nil {
  327. return nil, err
  328. }
  329. return translatePeer(p), nil
  330. }
  331. // Init initializes the backend; for this backend that means
  332. // syncing the informer cache.
  333. func (pb *peerBackend) Init(stop <-chan struct{}) error {
  334. // Register CRD.
  335. crd := crdutils.NewCustomResourceDefinition(crdutils.Config{
  336. SpecDefinitionName: "github.com/squat/kilo/pkg/k8s/apis/kilo/v1alpha1.Peer",
  337. EnableValidation: true,
  338. ResourceScope: string(v1beta1.ClusterScoped),
  339. Group: v1alpha1.GroupName,
  340. Kind: v1alpha1.PeerKind,
  341. Version: v1alpha1.SchemeGroupVersion.Version,
  342. Plural: v1alpha1.PeerPlural,
  343. ShortNames: v1alpha1.PeerShortNames,
  344. GetOpenAPIDefinitions: v1alpha1.GetOpenAPIDefinitions,
  345. })
  346. crd.Spec.Subresources.Scale = nil
  347. crd.Spec.Subresources.Status = nil
  348. _, err := pb.extensionsClient.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd)
  349. if err != nil && !apierrors.IsAlreadyExists(err) {
  350. return fmt.Errorf("failed to create CRD: %v", err)
  351. }
  352. go pb.informer.Run(stop)
  353. if ok := cache.WaitForCacheSync(stop, func() bool {
  354. return pb.informer.HasSynced()
  355. }); !ok {
  356. return errors.New("failed to sync peer cache")
  357. }
  358. pb.informer.AddEventHandler(
  359. cache.ResourceEventHandlerFuncs{
  360. AddFunc: func(obj interface{}) {
  361. p, ok := obj.(*v1alpha1.Peer)
  362. if !ok || p.Validate() != nil {
  363. // Failed to decode Peer; ignoring...
  364. return
  365. }
  366. pb.events <- &mesh.PeerEvent{Type: mesh.AddEvent, Peer: translatePeer(p)}
  367. },
  368. UpdateFunc: func(old, obj interface{}) {
  369. p, ok := obj.(*v1alpha1.Peer)
  370. if !ok || p.Validate() != nil {
  371. // Failed to decode Peer; ignoring...
  372. return
  373. }
  374. o, ok := old.(*v1alpha1.Peer)
  375. if !ok || o.Validate() != nil {
  376. // Failed to decode Peer; ignoring...
  377. return
  378. }
  379. pb.events <- &mesh.PeerEvent{Type: mesh.UpdateEvent, Peer: translatePeer(p), Old: translatePeer(o)}
  380. },
  381. DeleteFunc: func(obj interface{}) {
  382. p, ok := obj.(*v1alpha1.Peer)
  383. if !ok || p.Validate() != nil {
  384. // Failed to decode Peer; ignoring...
  385. return
  386. }
  387. pb.events <- &mesh.PeerEvent{Type: mesh.DeleteEvent, Peer: translatePeer(p)}
  388. },
  389. },
  390. )
  391. return nil
  392. }
  393. // List gets all the Peers in the cluster.
  394. func (pb *peerBackend) List() ([]*mesh.Peer, error) {
  395. ps, err := pb.lister.List(labels.Everything())
  396. if err != nil {
  397. return nil, err
  398. }
  399. peers := make([]*mesh.Peer, len(ps))
  400. for i := range ps {
  401. // Skip invalid peers.
  402. if ps[i].Validate() != nil {
  403. continue
  404. }
  405. peers[i] = translatePeer(ps[i])
  406. }
  407. return peers, nil
  408. }
  409. // Set sets the fields of a peer.
  410. func (pb *peerBackend) Set(name string, peer *mesh.Peer) error {
  411. old, err := pb.lister.Get(name)
  412. if err != nil {
  413. return fmt.Errorf("failed to find peer: %v", err)
  414. }
  415. p := old.DeepCopy()
  416. p.Spec.AllowedIPs = make([]string, len(peer.AllowedIPs))
  417. for i := range peer.AllowedIPs {
  418. p.Spec.AllowedIPs[i] = peer.AllowedIPs[i].String()
  419. }
  420. if peer.Endpoint != nil {
  421. p.Spec.Endpoint = &v1alpha1.PeerEndpoint{
  422. IP: peer.Endpoint.IP.String(),
  423. Port: peer.Endpoint.Port,
  424. }
  425. }
  426. p.Spec.PersistentKeepalive = peer.PersistentKeepalive
  427. p.Spec.PublicKey = string(peer.PublicKey)
  428. if _, err = pb.client.KiloV1alpha1().Peers().Update(p); err != nil {
  429. return fmt.Errorf("failed to update peer: %v", err)
  430. }
  431. return nil
  432. }
  433. // Watch returns a chan of peer events.
  434. func (pb *peerBackend) Watch() <-chan *mesh.PeerEvent {
  435. return pb.events
  436. }
  437. func normalizeIP(ip string) *net.IPNet {
  438. i, ipNet, err := net.ParseCIDR(ip)
  439. if err != nil || ipNet == nil {
  440. return nil
  441. }
  442. if ip4 := i.To4(); ip4 != nil {
  443. ipNet.IP = ip4
  444. return ipNet
  445. }
  446. ipNet.IP = i.To16()
  447. return ipNet
  448. }