hostgw.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 hostgw
  15. import (
  16. "fmt"
  17. "github.com/coreos/flannel/backend"
  18. "github.com/coreos/flannel/pkg/ip"
  19. "github.com/coreos/flannel/subnet"
  20. "golang.org/x/net/context"
  21. )
  22. func init() {
  23. backend.Register("host-gw", New)
  24. }
  25. const (
  26. routeCheckRetries = 10
  27. )
  28. type HostgwBackend struct {
  29. sm subnet.Manager
  30. extIface *backend.ExternalInterface
  31. networks map[string]*network
  32. }
  33. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  34. if !extIface.ExtAddr.Equal(extIface.IfaceAddr) {
  35. return nil, fmt.Errorf("your PublicIP differs from interface IP, meaning that probably you're on a NAT, which is not supported by host-gw backend")
  36. }
  37. be := &HostgwBackend{
  38. sm: sm,
  39. extIface: extIface,
  40. networks: make(map[string]*network),
  41. }
  42. return be, nil
  43. }
  44. func (be *HostgwBackend) RegisterNetwork(ctx context.Context, config *subnet.Config) (backend.Network, error) {
  45. n := &network{
  46. extIface: be.extIface,
  47. sm: be.sm,
  48. }
  49. attrs := subnet.LeaseAttrs{
  50. PublicIP: ip.FromIP(be.extIface.ExtAddr),
  51. BackendType: "host-gw",
  52. }
  53. l, err := be.sm.AcquireLease(ctx, &attrs)
  54. switch err {
  55. case nil:
  56. n.lease = l
  57. case context.Canceled, context.DeadlineExceeded:
  58. return nil, err
  59. default:
  60. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  61. }
  62. /* NB: docker will create the local route to `sn` */
  63. return n, nil
  64. }