subnet.go 3.4 KB

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