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