extension_network.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2017 flannel 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 extension
  15. import (
  16. "encoding/json"
  17. "sync"
  18. "golang.org/x/net/context"
  19. "fmt"
  20. "github.com/flannel-io/flannel/backend"
  21. "github.com/flannel-io/flannel/subnet"
  22. log "k8s.io/klog"
  23. )
  24. type network struct {
  25. name string
  26. extIface *backend.ExternalInterface
  27. lease *subnet.Lease
  28. sm subnet.Manager
  29. preStartupCommand string
  30. postStartupCommand string
  31. subnetAddCommand string
  32. subnetRemoveCommand string
  33. }
  34. func (n *network) Lease() *subnet.Lease {
  35. return n.lease
  36. }
  37. func (n *network) MTU() int {
  38. return n.extIface.Iface.MTU
  39. }
  40. func (n *network) Run(ctx context.Context) {
  41. wg := sync.WaitGroup{}
  42. log.Info("Watching for new subnet leases")
  43. evts := make(chan []subnet.Event)
  44. wg.Add(1)
  45. go func() {
  46. subnet.WatchLeases(ctx, n.sm, n.lease, evts)
  47. wg.Done()
  48. }()
  49. defer wg.Wait()
  50. for {
  51. select {
  52. case evtBatch, ok := <-evts:
  53. if !ok {
  54. log.Infof("evts chan closed")
  55. return
  56. }
  57. n.handleSubnetEvents(evtBatch)
  58. }
  59. }
  60. }
  61. func (n *network) handleSubnetEvents(batch []subnet.Event) {
  62. for _, evt := range batch {
  63. switch evt.Type {
  64. case subnet.EventAdded:
  65. log.Infof("Subnet added: %v via %v", evt.Lease.Subnet, evt.Lease.Attrs.PublicIP)
  66. if evt.Lease.Attrs.BackendType != "extension" {
  67. log.Warningf("Ignoring non-extension subnet: type=%v", evt.Lease.Attrs.BackendType)
  68. continue
  69. }
  70. if len(n.subnetAddCommand) > 0 {
  71. backendData := ""
  72. if len(evt.Lease.Attrs.BackendData) > 0 {
  73. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &backendData); err != nil {
  74. log.Errorf("failed to unmarshal BackendData: %v", err)
  75. continue
  76. }
  77. }
  78. cmd_output, err := runCmd([]string{
  79. fmt.Sprintf("SUBNET=%s", evt.Lease.Subnet),
  80. fmt.Sprintf("PUBLIC_IP=%s", evt.Lease.Attrs.PublicIP)},
  81. backendData,
  82. "sh", "-c", n.subnetAddCommand)
  83. if err != nil {
  84. log.Errorf("failed to run command: %s Err: %v Output: %s", n.subnetAddCommand, err, cmd_output)
  85. } else {
  86. log.Infof("Ran command: %s\n Output: %s", n.subnetAddCommand, cmd_output)
  87. }
  88. }
  89. case subnet.EventRemoved:
  90. log.Info("Subnet removed: ", evt.Lease.Subnet)
  91. if evt.Lease.Attrs.BackendType != "extension" {
  92. log.Warningf("Ignoring non-extension subnet: type=%v", evt.Lease.Attrs.BackendType)
  93. continue
  94. }
  95. if len(n.subnetRemoveCommand) > 0 {
  96. backendData := ""
  97. if len(evt.Lease.Attrs.BackendData) > 0 {
  98. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &backendData); err != nil {
  99. log.Errorf("failed to unmarshal BackendData: %v", err)
  100. continue
  101. }
  102. }
  103. cmd_output, err := runCmd([]string{
  104. fmt.Sprintf("SUBNET=%s", evt.Lease.Subnet),
  105. fmt.Sprintf("PUBLIC_IP=%s", evt.Lease.Attrs.PublicIP)},
  106. backendData,
  107. "sh", "-c", n.subnetRemoveCommand)
  108. if err != nil {
  109. log.Errorf("failed to run command: %s Err: %v Output: %s", n.subnetRemoveCommand, err, cmd_output)
  110. } else {
  111. log.Infof("Ran command: %s\n Output: %s", n.subnetRemoveCommand, cmd_output)
  112. }
  113. }
  114. default:
  115. log.Error("Internal error: unknown event type: ", int(evt.Type))
  116. }
  117. }
  118. }