2
0

backend.go 17 KB

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