subnet.go 3.8 KB

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