common.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2015 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 backend
  15. import (
  16. "net"
  17. "golang.org/x/net/context"
  18. "github.com/coreos/flannel/subnet"
  19. )
  20. type ExternalInterface struct {
  21. Iface *net.Interface
  22. IfaceAddr net.IP
  23. ExtAddr net.IP
  24. }
  25. // Besides the entry points in the Backend interface, the backend's New()
  26. // function receives static network interface information (like internal and
  27. // external IP addresses, MTU, etc) which it should cache for later use if
  28. // needed.
  29. //
  30. // To implement a singleton backend which manages multiple networks, the
  31. // New() function should create the singleton backend object once, and return
  32. // that object on on further calls to New(). The backend is guaranteed that
  33. // the arguments passed via New() will not change across invocations. Also,
  34. // since multiple RegisterNetwork() and Run() calls may be in-flight at any
  35. // given time for a singleton backend, it must protect these calls with a mutex.
  36. type Backend interface {
  37. // Called first to start the necessary event loops and such
  38. Run(ctx context.Context)
  39. // Called when the backend should create or begin managing a new network
  40. RegisterNetwork(ctx context.Context, config *subnet.Config) (Network, error)
  41. }
  42. type Network interface {
  43. Lease() *subnet.Lease
  44. MTU() int
  45. Run(ctx context.Context)
  46. }
  47. type BackendCtor func(sm subnet.Manager, ei *ExternalInterface) (Backend, error)
  48. type SimpleNetwork struct {
  49. SubnetLease *subnet.Lease
  50. ExtIface *ExternalInterface
  51. }
  52. func (n *SimpleNetwork) Lease() *subnet.Lease {
  53. return n.SubnetLease
  54. }
  55. func (n *SimpleNetwork) MTU() int {
  56. return n.ExtIface.Iface.MTU
  57. }
  58. func (_ *SimpleNetwork) Run(ctx context.Context) {
  59. <-ctx.Done()
  60. }