subnet.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/coreos/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. BackendType string `json:",omitempty"`
  34. BackendData json.RawMessage `json:",omitempty"`
  35. }
  36. type Lease struct {
  37. Subnet ip.IP4Net
  38. Attrs LeaseAttrs
  39. Expiration time.Time
  40. Asof uint64
  41. }
  42. func (l *Lease) Key() string {
  43. return MakeSubnetKey(l.Subnet)
  44. }
  45. type Reservation struct {
  46. Subnet ip.IP4Net
  47. PublicIP ip.IP4
  48. }
  49. type (
  50. EventType int
  51. Event struct {
  52. Type EventType `json:"type"`
  53. Lease Lease `json:"lease,omitempty"`
  54. }
  55. )
  56. const (
  57. EventAdded EventType = iota
  58. EventRemoved
  59. )
  60. type LeaseWatchResult struct {
  61. // Either Events or Snapshot will be set. If Events is empty, it means
  62. // the cursor was out of range and Snapshot contains the current list
  63. // of items, even if empty.
  64. Events []Event `json:"events"`
  65. Snapshot []Lease `json:"snapshot"`
  66. Cursor interface{} `json:"cursor"`
  67. }
  68. func (et EventType) MarshalJSON() ([]byte, error) {
  69. s := ""
  70. switch et {
  71. case EventAdded:
  72. s = "added"
  73. case EventRemoved:
  74. s = "removed"
  75. default:
  76. return nil, errors.New("bad event type")
  77. }
  78. return json.Marshal(s)
  79. }
  80. func (et *EventType) UnmarshalJSON(data []byte) error {
  81. switch string(data) {
  82. case "\"added\"":
  83. *et = EventAdded
  84. case "\"removed\"":
  85. *et = EventRemoved
  86. default:
  87. fmt.Println(string(data))
  88. return errors.New("bad event type")
  89. }
  90. return nil
  91. }
  92. func ParseSubnetKey(s string) *ip.IP4Net {
  93. if parts := subnetRegex.FindStringSubmatch(s); len(parts) == 3 {
  94. snIp := net.ParseIP(parts[1]).To4()
  95. prefixLen, err := strconv.ParseUint(parts[2], 10, 5)
  96. if snIp != nil && err == nil {
  97. return &ip.IP4Net{IP: ip.FromIP(snIp), PrefixLen: uint(prefixLen)}
  98. }
  99. }
  100. return nil
  101. }
  102. func MakeSubnetKey(sn ip.IP4Net) string {
  103. return sn.StringSep(".", "-")
  104. }
  105. type Manager interface {
  106. GetNetworkConfig(ctx context.Context) (*Config, error)
  107. AcquireLease(ctx context.Context, attrs *LeaseAttrs) (*Lease, error)
  108. RenewLease(ctx context.Context, lease *Lease) error
  109. RevokeLease(ctx context.Context, sn ip.IP4Net) error
  110. WatchLease(ctx context.Context, sn ip.IP4Net, cursor interface{}) (LeaseWatchResult, error)
  111. WatchLeases(ctx context.Context, cursor interface{}) (LeaseWatchResult, error)
  112. AddReservation(ctx context.Context, r *Reservation) error
  113. RemoveReservation(ctx context.Context, subnet ip.IP4Net) error
  114. ListReservations(ctx context.Context) ([]Reservation, error)
  115. }