alloc.go 1.6 KB

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