extension.go 3.8 KB

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