extension_network.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. log "github.com/golang/glog"
  19. "golang.org/x/net/context"
  20. "fmt"
  21. "github.com/coreos/flannel/backend"
  22. "github.com/coreos/flannel/subnet"
  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 := <-evts:
  53. n.handleSubnetEvents(evtBatch)
  54. case <-ctx.Done():
  55. return
  56. }
  57. }
  58. }
  59. func (n *network) handleSubnetEvents(batch []subnet.Event) {
  60. for _, evt := range batch {
  61. switch evt.Type {
  62. case subnet.EventAdded:
  63. log.Infof("Subnet added: %v via %v", evt.Lease.Subnet, evt.Lease.Attrs.PublicIP)
  64. if evt.Lease.Attrs.BackendType != "extension" {
  65. log.Warningf("Ignoring non-extension subnet: type=%v", evt.Lease.Attrs.BackendType)
  66. continue
  67. }
  68. if len(n.subnetAddCommand) > 0 {
  69. var dat interface{}
  70. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &dat); err != nil {
  71. log.Errorf("failed to unmarshal BackendData: %v", err)
  72. } else {
  73. backendData := dat.(string)
  74. cmd_output, err := runCmd([]string{
  75. fmt.Sprintf("SUBNET=%s", evt.Lease.Subnet),
  76. fmt.Sprintf("PUBLIC_IP=%s", evt.Lease.Attrs.PublicIP)},
  77. backendData,
  78. "sh", "-c", n.subnetAddCommand)
  79. if err != nil {
  80. log.Errorf("failed to run command: %s Err: %v Output: %s", n.subnetAddCommand, err, cmd_output)
  81. } else {
  82. log.Infof("Ran command: %s\n Output: %s", n.subnetAddCommand, cmd_output)
  83. }
  84. }
  85. }
  86. case subnet.EventRemoved:
  87. log.Info("Subnet removed: ", evt.Lease.Subnet)
  88. if evt.Lease.Attrs.BackendType != "extension" {
  89. log.Warningf("Ignoring non-extension subnet: type=%v", evt.Lease.Attrs.BackendType)
  90. continue
  91. }
  92. if len(n.subnetRemoveCommand) > 0 {
  93. var dat interface{}
  94. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &dat); err != nil {
  95. log.Errorf("failed to unmarshal BackendData: %v", err)
  96. } else {
  97. backendData := dat.(string)
  98. cmd_output, err := runCmd([]string{
  99. fmt.Sprintf("SUBNET=%s", evt.Lease.Subnet),
  100. fmt.Sprintf("PUBLIC_IP=%s", evt.Lease.Attrs.PublicIP)},
  101. backendData,
  102. "sh", "-c", n.subnetRemoveCommand)
  103. if err != nil {
  104. log.Errorf("failed to run command: %s Err: %v Output: %s", n.subnetRemoveCommand, err, cmd_output)
  105. } else {
  106. log.Infof("Ran command: %s\n Output: %s", n.subnetRemoveCommand, cmd_output)
  107. }
  108. }
  109. }
  110. default:
  111. log.Error("Internal error: unknown event type: ", int(evt.Type))
  112. }
  113. }
  114. }