backend.go 13 KB

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