alloc.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 alloc
  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. "sync"
  22. )
  23. func init() {
  24. backend.Register("alloc", New)
  25. }
  26. type AllocBackend struct {
  27. sm subnet.Manager
  28. extIface *backend.ExternalInterface
  29. }
  30. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  31. be := AllocBackend{
  32. sm: sm,
  33. extIface: extIface,
  34. }
  35. return &be, nil
  36. }
  37. func (be *AllocBackend) RegisterNetwork(ctx context.Context, wg sync.WaitGroup, config *subnet.Config) (backend.Network, error) {
  38. attrs := subnet.LeaseAttrs{
  39. PublicIP: ip.FromIP(be.extIface.ExtAddr),
  40. }
  41. l, err := be.sm.AcquireLease(ctx, &attrs)
  42. switch err {
  43. case nil:
  44. return &backend.SimpleNetwork{
  45. SubnetLease: l,
  46. ExtIface: be.extIface,
  47. }, nil
  48. case context.Canceled, context.DeadlineExceeded:
  49. return nil, err
  50. default:
  51. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  52. }
  53. }