flannel.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 encapsulation
  15. import (
  16. "fmt"
  17. "net"
  18. "sync"
  19. "github.com/squat/kilo/pkg/iptables"
  20. "github.com/vishvananda/netlink"
  21. )
  22. const flannelDeviceName = "flannel.1"
  23. type flannel struct {
  24. iface int
  25. strategy Strategy
  26. ch chan netlink.LinkUpdate
  27. done chan struct{}
  28. // mu guards updates to the iface field.
  29. mu sync.Mutex
  30. }
  31. // NewFlannel returns an encapsulator that uses Flannel.
  32. func NewFlannel(strategy Strategy) Encapsulator {
  33. return &flannel{
  34. ch: make(chan netlink.LinkUpdate),
  35. done: make(chan struct{}),
  36. strategy: strategy,
  37. }
  38. }
  39. // CleanUp is a no-op.
  40. func (f *flannel) CleanUp() error {
  41. close(f.done)
  42. return nil
  43. }
  44. // Gw returns the correct gateway IP associated with the given node.
  45. func (f *flannel) Gw(_, _ net.IP, subnet *net.IPNet) net.IP {
  46. return subnet.IP
  47. }
  48. // Index returns the index of the Flannel interface.
  49. func (f *flannel) Index() int {
  50. return f.iface
  51. }
  52. // Init finds the Flannel interface index.
  53. func (f *flannel) Init(_ int) error {
  54. if err := netlink.LinkSubscribe(f.ch, f.done); err != nil {
  55. return fmt.Errorf("failed to subscribe to updates to %s: %v", flannelDeviceName, err)
  56. }
  57. go func() {
  58. var lu netlink.LinkUpdate
  59. for {
  60. select {
  61. case lu = <-f.ch:
  62. if lu.Attrs().Name == flannelDeviceName {
  63. f.mu.Lock()
  64. f.iface = lu.Attrs().Index
  65. f.mu.Unlock()
  66. }
  67. case <-f.done:
  68. return
  69. }
  70. }
  71. }()
  72. i, err := netlink.LinkByName(flannelDeviceName)
  73. if _, ok := err.(netlink.LinkNotFoundError); ok {
  74. return nil
  75. }
  76. if err != nil {
  77. return fmt.Errorf("failed to query for Flannel interface: %v", err)
  78. }
  79. f.mu.Lock()
  80. f.iface = i.Attrs().Index
  81. f.mu.Unlock()
  82. return nil
  83. }
  84. // Rules is a no-op.
  85. func (f *flannel) Rules(_ []*net.IPNet) []iptables.Rule {
  86. return nil
  87. }
  88. // Set is a no-op.
  89. func (f *flannel) Set(_ *net.IPNet) error {
  90. return nil
  91. }
  92. // Strategy returns the configured strategy for encapsulation.
  93. func (f *flannel) Strategy() Strategy {
  94. return f.strategy
  95. }