flannel.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. f.mu.Lock()
  51. defer f.mu.Unlock()
  52. return f.iface
  53. }
  54. // Init finds the Flannel interface index.
  55. func (f *flannel) Init(_ int) error {
  56. if err := netlink.LinkSubscribe(f.ch, f.done); err != nil {
  57. return fmt.Errorf("failed to subscribe to updates to %s: %v", flannelDeviceName, err)
  58. }
  59. go func() {
  60. var lu netlink.LinkUpdate
  61. for {
  62. select {
  63. case lu = <-f.ch:
  64. if lu.Attrs().Name == flannelDeviceName {
  65. f.mu.Lock()
  66. f.iface = lu.Attrs().Index
  67. f.mu.Unlock()
  68. }
  69. case <-f.done:
  70. return
  71. }
  72. }
  73. }()
  74. i, err := netlink.LinkByName(flannelDeviceName)
  75. if _, ok := err.(netlink.LinkNotFoundError); ok {
  76. return nil
  77. }
  78. if err != nil {
  79. return fmt.Errorf("failed to query for Flannel interface: %v", err)
  80. }
  81. f.mu.Lock()
  82. f.iface = i.Attrs().Index
  83. f.mu.Unlock()
  84. return nil
  85. }
  86. // Rules is a no-op.
  87. func (f *flannel) Rules(_ []*net.IPNet) iptables.RuleSet {
  88. return iptables.RuleSet{}
  89. }
  90. // Set is a no-op.
  91. func (f *flannel) Set(_ *net.IPNet) error {
  92. return nil
  93. }
  94. // Strategy returns the configured strategy for encapsulation.
  95. func (f *flannel) Strategy() Strategy {
  96. return f.strategy
  97. }