network.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2015 CoreOS, Inc.
  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 network
  15. import (
  16. "net"
  17. "sync"
  18. "time"
  19. log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
  20. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  21. "github.com/coreos/flannel/backend"
  22. "github.com/coreos/flannel/subnet"
  23. )
  24. type Network struct {
  25. Name string
  26. sm subnet.Manager
  27. ipMasq bool
  28. be backend.Backend
  29. }
  30. func New(sm subnet.Manager, name string, ipMasq bool) *Network {
  31. return &Network{
  32. Name: name,
  33. sm: sm,
  34. ipMasq: ipMasq,
  35. }
  36. }
  37. func (n *Network) Init(ctx context.Context, iface *net.Interface, iaddr net.IP, eaddr net.IP) *backend.SubnetDef {
  38. var cfg *subnet.Config
  39. var be backend.Backend
  40. var sn *backend.SubnetDef
  41. steps := []func() error{
  42. func() (err error) {
  43. cfg, err = n.sm.GetNetworkConfig(ctx, n.Name)
  44. if err != nil {
  45. log.Error("Failed to retrieve network config: ", err)
  46. }
  47. return
  48. },
  49. func() (err error) {
  50. be, err = newBackend(n.sm, n.Name, cfg)
  51. if err != nil {
  52. log.Error("Failed to create backend: ", err)
  53. } else {
  54. n.be = be
  55. }
  56. return
  57. },
  58. func() (err error) {
  59. sn, err = be.Init(iface, iaddr, eaddr)
  60. if err != nil {
  61. log.Errorf("Failed to initialize network %v (type %v): %v", n.Name, be.Name(), err)
  62. }
  63. return
  64. },
  65. func() (err error) {
  66. if n.ipMasq {
  67. flannelNet := cfg.Network
  68. if err = setupIPMasq(flannelNet); err != nil {
  69. log.Errorf("Failed to set up IP Masquerade for network %v: %v", n.Name, err)
  70. }
  71. }
  72. return
  73. },
  74. }
  75. for _, s := range steps {
  76. for ; ; time.Sleep(time.Second) {
  77. select {
  78. case <-ctx.Done():
  79. return nil
  80. default:
  81. }
  82. err := s()
  83. if err == nil {
  84. break
  85. }
  86. }
  87. }
  88. return sn
  89. }
  90. func (n *Network) Run(ctx context.Context) {
  91. wg := sync.WaitGroup{}
  92. wg.Add(1)
  93. go func() {
  94. n.be.Run()
  95. wg.Done()
  96. }()
  97. <-ctx.Done()
  98. n.be.Stop()
  99. wg.Wait()
  100. }