subnet.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 subnet
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "net"
  20. "regexp"
  21. "strconv"
  22. "time"
  23. "github.com/flannel-io/flannel/pkg/ip"
  24. "golang.org/x/net/context"
  25. )
  26. var (
  27. ErrLeaseTaken = errors.New("subnet: lease already taken")
  28. ErrNoMoreTries = errors.New("subnet: no more tries")
  29. subnetRegex = regexp.MustCompile(`(\d+\.\d+.\d+.\d+)-(\d+)`)
  30. )
  31. type LeaseAttrs struct {
  32. PublicIP ip.IP4
  33. PublicIPv6 *ip.IP6
  34. BackendType string `json:",omitempty"`
  35. BackendData json.RawMessage `json:",omitempty"`
  36. BackendV6Data json.RawMessage `json:",omitempty"`
  37. }
  38. type Lease struct {
  39. EnableIPv4 bool
  40. EnableIPv6 bool
  41. Subnet ip.IP4Net
  42. IPv6Subnet ip.IP6Net
  43. Attrs LeaseAttrs
  44. Expiration time.Time
  45. Asof uint64
  46. }
  47. func (l *Lease) Key() string {
  48. return MakeSubnetKey(l.Subnet)
  49. }
  50. type (
  51. EventType int
  52. Event struct {
  53. Type EventType `json:"type"`
  54. Lease Lease `json:"lease,omitempty"`
  55. }
  56. )
  57. const (
  58. EventAdded EventType = iota
  59. EventRemoved
  60. )
  61. type LeaseWatchResult struct {
  62. // Either Events or Snapshot will be set. If Events is empty, it means
  63. // the cursor was out of range and Snapshot contains the current list
  64. // of items, even if empty.
  65. Events []Event `json:"events"`
  66. Snapshot []Lease `json:"snapshot"`
  67. Cursor interface{} `json:"cursor"`
  68. }
  69. func (et EventType) MarshalJSON() ([]byte, error) {
  70. s := ""
  71. switch et {
  72. case EventAdded:
  73. s = "added"
  74. case EventRemoved:
  75. s = "removed"
  76. default:
  77. return nil, errors.New("bad event type")
  78. }
  79. return json.Marshal(s)
  80. }
  81. func (et *EventType) UnmarshalJSON(data []byte) error {
  82. switch string(data) {
  83. case "\"added\"":
  84. *et = EventAdded
  85. case "\"removed\"":
  86. *et = EventRemoved
  87. default:
  88. fmt.Println(string(data))
  89. return errors.New("bad event type")
  90. }
  91. return nil
  92. }
  93. func ParseSubnetKey(s string) *ip.IP4Net {
  94. if parts := subnetRegex.FindStringSubmatch(s); len(parts) == 3 {
  95. snIp := net.ParseIP(parts[1]).To4()
  96. prefixLen, err := strconv.ParseUint(parts[2], 10, 5)
  97. if snIp != nil && err == nil {
  98. return &ip.IP4Net{IP: ip.FromIP(snIp), PrefixLen: uint(prefixLen)}
  99. }
  100. }
  101. return nil
  102. }
  103. func MakeSubnetKey(sn ip.IP4Net) string {
  104. return sn.StringSep(".", "-")
  105. }
  106. type Manager interface {
  107. GetNetworkConfig(ctx context.Context) (*Config, error)
  108. AcquireLease(ctx context.Context, attrs *LeaseAttrs) (*Lease, error)
  109. RenewLease(ctx context.Context, lease *Lease) error
  110. WatchLease(ctx context.Context, sn ip.IP4Net, cursor interface{}) (LeaseWatchResult, error)
  111. WatchLeases(ctx context.Context, cursor interface{}) (LeaseWatchResult, error)
  112. Name() string
  113. }