2
0

flannel.go 2.7 KB

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