extension.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "fmt"
  17. "io"
  18. "os"
  19. "strings"
  20. "encoding/json"
  21. "os/exec"
  22. "sync"
  23. "github.com/flannel-io/flannel/backend"
  24. "github.com/flannel-io/flannel/pkg/ip"
  25. "github.com/flannel-io/flannel/subnet"
  26. "golang.org/x/net/context"
  27. log "k8s.io/klog"
  28. )
  29. func init() {
  30. backend.Register("extension", New)
  31. }
  32. type ExtensionBackend struct {
  33. sm subnet.Manager
  34. extIface *backend.ExternalInterface
  35. networks map[string]*network
  36. }
  37. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  38. be := &ExtensionBackend{
  39. sm: sm,
  40. extIface: extIface,
  41. networks: make(map[string]*network),
  42. }
  43. return be, nil
  44. }
  45. func (_ *ExtensionBackend) Run(ctx context.Context) {
  46. <-ctx.Done()
  47. }
  48. func (be *ExtensionBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup, config *subnet.Config) (backend.Network, error) {
  49. n := &network{
  50. extIface: be.extIface,
  51. sm: be.sm,
  52. }
  53. // Parse out configuration
  54. if len(config.Backend) > 0 {
  55. cfg := struct {
  56. PreStartupCommand string
  57. PostStartupCommand string
  58. SubnetAddCommand string
  59. SubnetRemoveCommand string
  60. }{}
  61. if err := json.Unmarshal(config.Backend, &cfg); err != nil {
  62. return nil, fmt.Errorf("error decoding backend config: %v", err)
  63. }
  64. n.preStartupCommand = cfg.PreStartupCommand
  65. n.postStartupCommand = cfg.PostStartupCommand
  66. n.subnetAddCommand = cfg.SubnetAddCommand
  67. n.subnetRemoveCommand = cfg.SubnetRemoveCommand
  68. }
  69. data := []byte{}
  70. if len(n.preStartupCommand) > 0 {
  71. cmd_output, err := runCmd([]string{}, "", "sh", "-c", n.preStartupCommand)
  72. if err != nil {
  73. return nil, fmt.Errorf("failed to run command: %s Err: %v Output: %s", n.preStartupCommand, err, cmd_output)
  74. } else {
  75. log.Infof("Ran command: %s\n Output: %s", n.preStartupCommand, cmd_output)
  76. }
  77. data, err = json.Marshal(cmd_output)
  78. if err != nil {
  79. return nil, err
  80. }
  81. } else {
  82. log.Infof("No pre startup command configured - skipping")
  83. }
  84. attrs := subnet.LeaseAttrs{
  85. PublicIP: ip.FromIP(be.extIface.ExtAddr),
  86. BackendType: "extension",
  87. BackendData: data,
  88. }
  89. lease, err := be.sm.AcquireLease(ctx, &attrs)
  90. switch err {
  91. case nil:
  92. n.lease = lease
  93. case context.Canceled, context.DeadlineExceeded:
  94. return nil, err
  95. default:
  96. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  97. }
  98. if len(n.postStartupCommand) > 0 {
  99. cmd_output, err := runCmd([]string{
  100. fmt.Sprintf("NETWORK=%s", config.Network),
  101. fmt.Sprintf("SUBNET=%s", lease.Subnet),
  102. fmt.Sprintf("PUBLIC_IP=%s", attrs.PublicIP)},
  103. "", "sh", "-c", n.postStartupCommand)
  104. if err != nil {
  105. return nil, fmt.Errorf("failed to run command: %s Err: %v Output: %s", n.postStartupCommand, err, cmd_output)
  106. } else {
  107. log.Infof("Ran command: %s\n Output: %s", n.postStartupCommand, cmd_output)
  108. }
  109. } else {
  110. log.Infof("No post startup command configured - skipping")
  111. }
  112. return n, nil
  113. }
  114. // Run a cmd, returning a combined stdout and stderr.
  115. func runCmd(env []string, stdin string, name string, arg ...string) (string, error) {
  116. cmd := exec.Command(name, arg...)
  117. cmd.Env = append(os.Environ(), env...)
  118. stdinpipe, err := cmd.StdinPipe()
  119. if err != nil {
  120. return "", err
  121. }
  122. io.WriteString(stdinpipe, stdin)
  123. io.WriteString(stdinpipe, "\n")
  124. stdinpipe.Close()
  125. output, err := cmd.CombinedOutput()
  126. return strings.TrimSpace(string(output)), err
  127. }