subnet.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // Copyright 2015 CoreOS, Inc.
  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/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
  24. log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
  25. "github.com/coreos/flannel/pkg/ip"
  26. "github.com/coreos/flannel/pkg/task"
  27. )
  28. const (
  29. registerRetries = 10
  30. subnetTTL = 24 * 3600
  31. renewMargin = time.Hour
  32. )
  33. // etcd error codes
  34. const (
  35. etcdKeyNotFound = 100
  36. etcdKeyAlreadyExists = 105
  37. etcdEventIndexCleared = 401
  38. )
  39. const (
  40. SubnetAdded = iota
  41. SubnetRemoved
  42. )
  43. var (
  44. subnetRegex *regexp.Regexp = regexp.MustCompile(`(\d+\.\d+.\d+.\d+)-(\d+)`)
  45. )
  46. type LeaseAttrs struct {
  47. PublicIP ip.IP4
  48. BackendType string `json:",omitempty"`
  49. BackendData json.RawMessage `json:",omitempty"`
  50. }
  51. type SubnetLease struct {
  52. Network ip.IP4Net
  53. Attrs LeaseAttrs
  54. }
  55. type SubnetManager struct {
  56. registry subnetRegistry
  57. config *Config
  58. myLease SubnetLease
  59. leaseExp time.Time
  60. lastIndex uint64
  61. leases []SubnetLease
  62. }
  63. type EventType int
  64. type Event struct {
  65. Type EventType
  66. Lease SubnetLease
  67. }
  68. type EventBatch []Event
  69. func NewSubnetManager(config *EtcdConfig) (*SubnetManager, error) {
  70. esr, err := newEtcdSubnetRegistry(config)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return newSubnetManager(esr)
  75. }
  76. func (sm *SubnetManager) AcquireLease(attrs *LeaseAttrs, cancel chan bool) (ip.IP4Net, error) {
  77. for {
  78. sn, err := sm.acquireLeaseOnce(attrs, cancel)
  79. switch {
  80. case err == nil:
  81. log.Info("Subnet lease acquired: ", sn)
  82. return sn, nil
  83. case err == task.ErrCanceled:
  84. return ip.IP4Net{}, err
  85. default:
  86. log.Error("Failed to acquire subnet: ", err)
  87. }
  88. select {
  89. case <-time.After(time.Second):
  90. case <-cancel:
  91. return ip.IP4Net{}, task.ErrCanceled
  92. }
  93. }
  94. }
  95. func findLeaseByIP(leases []SubnetLease, pubIP ip.IP4) *SubnetLease {
  96. for _, l := range leases {
  97. if pubIP == l.Attrs.PublicIP {
  98. return &l
  99. }
  100. }
  101. return nil
  102. }
  103. func (sm *SubnetManager) tryAcquireLease(extIP ip.IP4, attrs *LeaseAttrs) (ip.IP4Net, error) {
  104. var err error
  105. sm.leases, err = sm.getLeases()
  106. if err != nil {
  107. return ip.IP4Net{}, err
  108. }
  109. attrBytes, err := json.Marshal(attrs)
  110. if err != nil {
  111. log.Errorf("marshal failed: %#v, %v", attrs, err)
  112. return ip.IP4Net{}, err
  113. }
  114. // try to reuse a subnet if there's one that matches our IP
  115. if l := findLeaseByIP(sm.leases, extIP); l != nil {
  116. resp, err := sm.registry.updateSubnet(l.Network.StringSep(".", "-"), string(attrBytes), subnetTTL)
  117. if err != nil {
  118. return ip.IP4Net{}, err
  119. }
  120. sm.myLease.Network = l.Network
  121. sm.myLease.Attrs = *attrs
  122. sm.leaseExp = *resp.Node.Expiration
  123. return l.Network, nil
  124. }
  125. // no existing match, grab a new one
  126. sn, err := sm.allocateSubnet()
  127. if err != nil {
  128. return ip.IP4Net{}, err
  129. }
  130. resp, err := sm.registry.createSubnet(sn.StringSep(".", "-"), string(attrBytes), subnetTTL)
  131. switch {
  132. case err == nil:
  133. sm.myLease.Network = sn
  134. sm.myLease.Attrs = *attrs
  135. sm.leaseExp = *resp.Node.Expiration
  136. return sn, nil
  137. // if etcd returned Key Already Exists, try again.
  138. case err.(*etcd.EtcdError).ErrorCode == etcdKeyAlreadyExists:
  139. return ip.IP4Net{}, nil
  140. default:
  141. return ip.IP4Net{}, err
  142. }
  143. }
  144. func (sm *SubnetManager) acquireLeaseOnce(attrs *LeaseAttrs, cancel chan bool) (ip.IP4Net, error) {
  145. for i := 0; i < registerRetries; i++ {
  146. sn, err := sm.tryAcquireLease(attrs.PublicIP, attrs)
  147. switch {
  148. case err != nil:
  149. return ip.IP4Net{}, err
  150. case sn.IP != 0:
  151. return sn, nil
  152. }
  153. // before moving on, check for cancel
  154. if interrupted(cancel) {
  155. return ip.IP4Net{}, task.ErrCanceled
  156. }
  157. }
  158. return ip.IP4Net{}, errors.New("Max retries reached trying to acquire a subnet")
  159. }
  160. func (sm *SubnetManager) GetConfig() *Config {
  161. return sm.config
  162. }
  163. /// Implementation
  164. func parseSubnetKey(s string) (ip.IP4Net, error) {
  165. if parts := subnetRegex.FindStringSubmatch(s); len(parts) == 3 {
  166. snIp := net.ParseIP(parts[1]).To4()
  167. prefixLen, err := strconv.ParseUint(parts[2], 10, 5)
  168. if snIp != nil && err == nil {
  169. return ip.IP4Net{IP: ip.FromIP(snIp), PrefixLen: uint(prefixLen)}, nil
  170. }
  171. }
  172. return ip.IP4Net{}, errors.New("Error parsing IP Subnet")
  173. }
  174. func newSubnetManager(r subnetRegistry) (*SubnetManager, error) {
  175. cfgResp, err := r.getConfig()
  176. if err != nil {
  177. return nil, err
  178. }
  179. cfg, err := ParseConfig(cfgResp.Node.Value)
  180. if err != nil {
  181. return nil, err
  182. }
  183. sm := SubnetManager{
  184. registry: r,
  185. config: cfg,
  186. }
  187. return &sm, nil
  188. }
  189. func (sm *SubnetManager) getLeases() ([]SubnetLease, error) {
  190. resp, err := sm.registry.getSubnets()
  191. var leases []SubnetLease
  192. switch {
  193. case err == nil:
  194. for _, node := range resp.Node.Nodes {
  195. sn, err := parseSubnetKey(node.Key)
  196. if err == nil {
  197. var attrs LeaseAttrs
  198. if err = json.Unmarshal([]byte(node.Value), &attrs); err == nil {
  199. lease := SubnetLease{sn, attrs}
  200. leases = append(leases, lease)
  201. }
  202. }
  203. }
  204. sm.lastIndex = resp.EtcdIndex
  205. case err.(*etcd.EtcdError).ErrorCode == etcdKeyNotFound:
  206. // key not found: treat it as empty set
  207. sm.lastIndex = err.(*etcd.EtcdError).Index
  208. default:
  209. return nil, err
  210. }
  211. return leases, nil
  212. }
  213. func deleteLease(l []SubnetLease, i int) []SubnetLease {
  214. l[i], l = l[len(l)-1], l[:len(l)-1]
  215. return l
  216. }
  217. func (sm *SubnetManager) applyLeases(newLeases []SubnetLease) EventBatch {
  218. var batch EventBatch
  219. for _, l := range newLeases {
  220. // skip self
  221. if l.Network.Equal(sm.myLease.Network) {
  222. continue
  223. }
  224. found := false
  225. for i, c := range sm.leases {
  226. if c.Network.Equal(l.Network) {
  227. sm.leases = deleteLease(sm.leases, i)
  228. found = true
  229. break
  230. }
  231. }
  232. if !found {
  233. // new subnet
  234. batch = append(batch, Event{SubnetAdded, l})
  235. }
  236. }
  237. // everything left in sm.leases has been deleted
  238. for _, c := range sm.leases {
  239. batch = append(batch, Event{SubnetRemoved, c})
  240. }
  241. sm.leases = newLeases
  242. return batch
  243. }
  244. func (sm *SubnetManager) applySubnetChange(action string, ipn ip.IP4Net, data string) (Event, error) {
  245. switch action {
  246. case "delete", "expire":
  247. for i, l := range sm.leases {
  248. if l.Network.Equal(ipn) {
  249. deleteLease(sm.leases, i)
  250. return Event{SubnetRemoved, l}, nil
  251. }
  252. }
  253. log.Errorf("Removed subnet (%s) was not found", ipn)
  254. return Event{
  255. SubnetRemoved,
  256. SubnetLease{ipn, LeaseAttrs{}},
  257. }, nil
  258. default:
  259. var attrs LeaseAttrs
  260. err := json.Unmarshal([]byte(data), &attrs)
  261. if err != nil {
  262. return Event{}, err
  263. }
  264. for i, l := range sm.leases {
  265. if l.Network.Equal(ipn) {
  266. sm.leases[i] = SubnetLease{ipn, attrs}
  267. return Event{SubnetAdded, sm.leases[i]}, nil
  268. }
  269. }
  270. sm.leases = append(sm.leases, SubnetLease{ipn, attrs})
  271. return Event{SubnetAdded, sm.leases[len(sm.leases)-1]}, nil
  272. }
  273. }
  274. func (sm *SubnetManager) allocateSubnet() (ip.IP4Net, error) {
  275. log.Infof("Picking subnet in range %s ... %s", sm.config.SubnetMin, sm.config.SubnetMax)
  276. var bag []ip.IP4
  277. sn := ip.IP4Net{IP: sm.config.SubnetMin, PrefixLen: sm.config.SubnetLen}
  278. OuterLoop:
  279. for ; sn.IP <= sm.config.SubnetMax && len(bag) < 100; sn = sn.Next() {
  280. for _, l := range sm.leases {
  281. if sn.Overlaps(l.Network) {
  282. continue OuterLoop
  283. }
  284. }
  285. bag = append(bag, sn.IP)
  286. }
  287. if len(bag) == 0 {
  288. return ip.IP4Net{}, errors.New("out of subnets")
  289. } else {
  290. i := randInt(0, len(bag))
  291. return ip.IP4Net{IP: bag[i], PrefixLen: sm.config.SubnetLen}, nil
  292. }
  293. }
  294. func (sm *SubnetManager) WatchLeases(receiver chan EventBatch, cancel chan bool) {
  295. // "catch up" by replaying all the leases we discovered during
  296. // AcquireLease
  297. var batch EventBatch
  298. for _, l := range sm.leases {
  299. if !sm.myLease.Network.Equal(l.Network) {
  300. batch = append(batch, Event{SubnetAdded, l})
  301. }
  302. }
  303. if len(batch) > 0 {
  304. receiver <- batch
  305. }
  306. for {
  307. resp, err := sm.registry.watchSubnets(sm.lastIndex+1, cancel)
  308. // watchSubnets exited by cancel chan being signaled
  309. if err == nil && resp == nil {
  310. return
  311. }
  312. var batch *EventBatch
  313. if err == nil {
  314. batch, err = sm.parseSubnetWatchResponse(resp)
  315. } else {
  316. batch, err = sm.parseSubnetWatchError(err)
  317. }
  318. if err != nil {
  319. log.Errorf("%v", err)
  320. time.Sleep(time.Second)
  321. continue
  322. }
  323. if batch != nil {
  324. receiver <- *batch
  325. }
  326. }
  327. }
  328. func (sm *SubnetManager) parseSubnetWatchResponse(resp *etcd.Response) (batch *EventBatch, err error) {
  329. sm.lastIndex = resp.Node.ModifiedIndex
  330. sn, err := parseSubnetKey(resp.Node.Key)
  331. if err != nil {
  332. err = fmt.Errorf("Error parsing subnet IP: %s", resp.Node.Key)
  333. return
  334. }
  335. // Don't process our own changes
  336. if !sm.myLease.Network.Equal(sn) {
  337. evt, err := sm.applySubnetChange(resp.Action, sn, resp.Node.Value)
  338. if err != nil {
  339. return nil, err
  340. }
  341. batch = &EventBatch{evt}
  342. }
  343. return
  344. }
  345. func (sm *SubnetManager) parseSubnetWatchError(err error) (batch *EventBatch, out error) {
  346. etcdErr, ok := err.(*etcd.EtcdError)
  347. if ok && etcdErr.ErrorCode == etcdEventIndexCleared {
  348. // etcd maintains a history window for events and it's possible to fall behind.
  349. // to recover, get the current state and then "diff" against our cache to generate
  350. // events for the caller
  351. log.Warning("Watch of subnet leases failed because etcd index outside history window")
  352. leases, err := sm.getLeases()
  353. if err == nil {
  354. lb := sm.applyLeases(leases)
  355. batch = &lb
  356. } else {
  357. out = fmt.Errorf("Failed to retrieve subnet leases: %v", err)
  358. }
  359. } else {
  360. out = fmt.Errorf("Watch of subnet leases failed: %v", err)
  361. }
  362. return
  363. }
  364. func (sm *SubnetManager) LeaseRenewer(cancel chan bool) {
  365. for {
  366. dur := sm.leaseExp.Sub(time.Now()) - renewMargin
  367. select {
  368. case <-time.After(dur):
  369. attrBytes, err := json.Marshal(&sm.myLease.Attrs)
  370. if err != nil {
  371. log.Error("Error renewing lease (trying again in 1 min): ", err)
  372. dur = time.Minute
  373. continue
  374. }
  375. resp, err := sm.registry.updateSubnet(sm.myLease.Network.StringSep(".", "-"), string(attrBytes), subnetTTL)
  376. if err != nil {
  377. log.Error("Error renewing lease (trying again in 1 min): ", err)
  378. dur = time.Minute
  379. continue
  380. }
  381. sm.leaseExp = *resp.Node.Expiration
  382. log.Info("Lease renewed, new expiration: ", sm.leaseExp)
  383. dur = sm.leaseExp.Sub(time.Now()) - renewMargin
  384. case <-cancel:
  385. return
  386. }
  387. }
  388. }
  389. func interrupted(cancel chan bool) bool {
  390. select {
  391. case <-cancel:
  392. return true
  393. default:
  394. return false
  395. }
  396. }