{"action":"created","comment":{"url":"https://api.github.com/repos/coreos/flannel/pulls/comments/42909259","id":42909259,"diff_hunk":"@@ -0,0 +1,329 @@\n+// Copyright 2015 flannel authors\n+//\n+// Licensed under the Apache License, Version 2.0 (the \"License\");\n+// you may not use this file except in compliance with the License.\n+// You may obtain a copy of the License at\n+//\n+//     http://www.apache.org/licenses/LICENSE-2.0\n+//\n+// Unless required by applicable law or agreed to in writing, software\n+// distributed under the License is distributed on an \"AS IS\" BASIS,\n+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+// See the License for the specific language governing permissions and\n+// limitations under the License.\n+\n+package subnet\n+\n+import (\n+\t\"encoding/json\"\n+\t\"errors\"\n+\t\"fmt\"\n+\t\"strconv\"\n+\t\"time\"\n+\n+\tetcd \"github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/etcd/client\"\n+\tlog \"github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog\"\n+\t\"github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context\"\n+\t\"github.com/coreos/flannel/pkg/ip\"\n+)\n+\n+const (\n+\tregisterRetries = 10\n+\tsubnetTTL       = 24 * time.Hour\n+)\n+\n+type LocalManager struct {\n+\tregistry Registry\n+}\n+\n+type watchCursor struct {\n+\tindex uint64\n+}\n+\n+func (c watchCursor) String() string {\n+\treturn strconv.FormatUint(c.index, 10)\n+}\n+\n+func NewLocalManager(config *EtcdConfig) (Manager, error) {\n+\tr, err := newEtcdSubnetRegistry(config)\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\treturn newLocalManager(r, config.Prefix), nil\n+}\n+\n+func newLocalManager(r Registry, prefix string) Manager {\n+\treturn &LocalManager{\n+\t\tregistry: r,\n+\t}\n+}\n+\n+func (m *LocalManager) GetNetworkConfig(ctx context.Context, network string) (*Config, error) {\n+\tcfg, err := m.registry.getNetworkConfig(ctx, network)\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\n+\treturn ParseConfig(cfg)\n+}\n+\n+func (m *LocalManager) AcquireLease(ctx context.Context, network string, attrs *LeaseAttrs) (*Lease, error) {\n+\tconfig, err := m.GetNetworkConfig(ctx, network)\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\n+\tfor i := 0; i < registerRetries; i++ {\n+\t\tl, err := m.tryAcquireLease(ctx, network, config, attrs.PublicIP, attrs)\n+\t\tswitch {\n+\t\tcase err != nil:\n+\t\t\treturn nil, err\n+\t\tcase l != nil:\n+\t\t\treturn l, nil\n+\t\t}\n+\t}\n+\n+\treturn nil, errors.New(\"Max retries reached trying to acquire a subnet\")\n+}\n+\n+func findLeaseByIP(leases []Lease, pubIP ip.IP4) *Lease {\n+\tfor _, l := range leases {\n+\t\tif pubIP == l.Attrs.PublicIP {\n+\t\t\treturn &l\n+\t\t}\n+\t}\n+\n+\treturn nil\n+}\n+\n+func (m *LocalManager) tryAcquireLease(ctx context.Context, network string, config *Config, extIaddr ip.IP4, attrs *LeaseAttrs) (*Lease, error) {\n+\tvar err error\n+\tleases, _, err := m.registry.getSubnets(ctx, network)\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\n+\t// try to reuse a subnet if there's one that matches our IP\n+\tif l := findLeaseByIP(leases, extIaddr); l != nil {\n+\t\t// make sure the existing subnet is still within the configured network\n+\t\tif isSubnetConfigCompat(config, l.Subnet) {\n+\t\t\tlog.Infof(\"Found lease (%v) for current IP (%v), reusing\", l.Subnet, extIaddr)\n+\t\t\texp, err := m.registry.updateSubnet(ctx, network, l.Subnet, attrs, subnetTTL, 0)\n+\t\t\tif err != nil {\n+\t\t\t\treturn nil, err\n+\t\t\t}\n+\n+\t\t\tl.Attrs = *attrs\n+\t\t\tl.Expiration = exp\n+\t\t\treturn l, nil\n+\t\t} else {\n+\t\t\tlog.Infof(\"Found lease (%v) for current IP (%v) but not compatible with current config, deleting\", l.Subnet, extIaddr)\n+\t\t\tif err := m.registry.deleteSubnet(ctx, network, l.Subnet); err != nil {\n+\t\t\t\treturn nil, err\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\t// no existing match, grab a new one\n+\tsn, err := m.allocateSubnet(config, leases)\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\n+\texp, err := m.registry.createSubnet(ctx, network, sn, attrs, subnetTTL)\n+\tif err == nil {\n+\t\treturn &Lease{\n+\t\t\tSubnet:     sn,\n+\t\t\tAttrs:      *attrs,\n+\t\t\tExpiration: exp,\n+\t\t}, nil\n+\t}\n+\n+\tif etcdErr, ok := err.(etcd.Error); ok && etcdErr.Code == etcd.ErrorCodeNodeExist {\n+\t\t// if etcd returned Key Already Exists, try again.\n+\t\treturn nil, nil\n+\t}\n+\n+\treturn nil, err\n+}\n+\n+func (m *LocalManager) allocateSubnet(config *Config, leases []Lease) (ip.IP4Net, error) {\n+\tlog.Infof(\"Picking subnet in range %s ... %s\", config.SubnetMin, config.SubnetMax)\n+\n+\tvar bag []ip.IP4\n+\tsn := ip.IP4Net{IP: config.SubnetMin, PrefixLen: config.SubnetLen}\n+\n+OuterLoop:\n+\tfor ; sn.IP <= config.SubnetMax && len(bag) < 100; sn = sn.Next() {\n+\t\tfor _, l := range leases {\n+\t\t\tif sn.Overlaps(l.Subnet) {\n+\t\t\t\tcontinue OuterLoop\n+\t\t\t}\n+\t\t}\n+\t\tbag = append(bag, sn.IP)\n+\t}\n+\n+\tif len(bag) == 0 {\n+\t\treturn ip.IP4Net{}, errors.New(\"out of subnets\")\n+\t} else {\n+\t\ti := randInt(0, len(bag))\n+\t\treturn ip.IP4Net{IP: bag[i], PrefixLen: config.SubnetLen}, nil\n+\t}\n+}\n+\n+func nodeToLease(node *etcd.Node) (*Lease, error) {","path":"subnet/local_manager.go","position":174,"original_position":174,"commit_id":"5a7ee64b394b9900d7b9f1378f956892d25e09f7","original_commit_id":"5a7ee64b394b9900d7b9f1378f956892d25e09f7","user":{"login":"MohdAhmad","id":1475849,"avatar_url":"https://avatars.githubusercontent.com/u/1475849?v=3","gravatar_id":"","url":"https://api.github.com/users/MohdAhmad","html_url":"https://github.com/MohdAhmad","followers_url":"https://api.github.com/users/MohdAhmad/followers","following_url":"https://api.github.com/users/MohdAhmad/following{/other_user}","gists_url":"https://api.github.com/users/MohdAhmad/gists{/gist_id}","starred_url":"https://api.github.com/users/MohdAhmad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MohdAhmad/subscriptions","organizations_url":"https://api.github.com/users/MohdAhmad/orgs","repos_url":"https://api.github.com/users/MohdAhmad/repos","events_url":"https://api.github.com/users/MohdAhmad/events{/privacy}","received_events_url":"https://api.github.com/users/MohdAhmad/received_events","type":"User","site_admin":false},"body":"I can't see where `nodeToLease` gets used. ","created_at":"2015-10-23T20:11:55Z","updated_at":"2015-10-23T20:11:55Z","html_url":"https://github.com/coreos/flannel/pull/344#discussion_r42909259","pull_request_url":"https://api.github.com/repos/coreos/flannel/pulls/344","_links":{"self":{"href":"https://api.github.com/repos/coreos/flannel/pulls/comments/42909259"},"html":{"href":"https://github.com/coreos/flannel/pull/344#discussion_r42909259"},"pull_request":{"href":"https://api.github.com/repos/coreos/flannel/pulls/344"}}},"pull_request":{"url":"https://api.github.com/repos/coreos/flannel/pulls/344","id":47607251,"html_url":"https://github.com/coreos/flannel/pull/344","diff_url":"https://github.com/coreos/flannel/pull/344.diff","patch_url":"https://github.com/coreos/flannel/pull/344.patch","issue_url":"https://api.github.com/repos/coreos/flannel/issues/344","number":344,"state":"open","locked":false,"title":"Typed registry","user":{"login":"eyakubovich","id":3586053,"avatar_url":"https://avatars.githubusercontent.com/u/3586053?v=3","gravatar_id":"","url":"https://api.github.com/users/eyakubovich","html_url":"https://github.com/eyakubovich","followers_url":"https://api.github.com/users/eyakubovich/followers","following_url":"https://api.github.com/users/eyakubovich/following{/other_user}","gists_url":"https://api.github.com/users/eyakubovich/gists{/gist_id}","starred_url":"https://api.github.com/users/eyakubovich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eyakubovich/subscriptions","organizations_url":"https://api.github.com/users/eyakubovich/orgs","repos_url":"https://api.github.com/users/eyakubovich/repos","events_url":"https://api.github.com/users/eyakubovich/events{/privacy}","received_events_url":"https://api.github.com/users/eyakubovich/received_events","type":"User","site_admin":false},"body":"Another refactor. This makes the registry interface more etcd agnostic. Not 100% though.","created_at":"2015-10-14T00:56:51Z","updated_at":"2015-10-23T20:11:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7dcaa232f25e2151e9f77a7b6ae38ef01a5cfc26","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/coreos/flannel/pulls/344/commits","review_comments_url":"https://api.github.com/repos/coreos/flannel/pulls/344/comments","review_comment_url":"https://api.github.com/repos/coreos/flannel/pulls/comments{/number}","comments_url":"https://api.github.com/repos/coreos/flannel/issues/344/comments","statuses_url":"https://api.github.com/repos/coreos/flannel/statuses/5a7ee64b394b9900d7b9f1378f956892d25e09f7","head":{"label":"eyakubovich:typed-registry","ref":"typed-registry","sha":"5a7ee64b394b9900d7b9f1378f956892d25e09f7","user":{"login":"eyakubovich","id":3586053,"avatar_url":"https://avatars.githubusercontent.com/u/3586053?v=3","gravatar_id":"","url":"https://api.github.com/users/eyakubovich","html_url":"https://github.com/eyakubovich","followers_url":"https://api.github.com/users/eyakubovich/followers","following_url":"https://api.github.com/users/eyakubovich/following{/other_user}","gists_url":"https://api.github.com/users/eyakubovich/gists{/gist_id}","starred_url":"https://api.github.com/users/eyakubovich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eyakubovich/subscriptions","organizations_url":"https://api.github.com/users/eyakubovich/orgs","repos_url":"https://api.github.com/users/eyakubovich/repos","events_url":"https://api.github.com/users/eyakubovich/events{/privacy}","received_events_url":"https://api.github.com/users/eyakubovich/received_events","type":"User","site_admin":false},"repo":{"id":23436332,"name":"flannel","full_name":"eyakubovich/flannel","owner":{"login":"eyakubovich","id":3586053,"avatar_url":"https://avatars.githubusercontent.com/u/3586053?v=3","gravatar_id":"","url":"https://api.github.com/users/eyakubovich","html_url":"https://github.com/eyakubovich","followers_url":"https://api.github.com/users/eyakubovich/followers","following_url":"https://api.github.com/users/eyakubovich/following{/other_user}","gists_url":"https://api.github.com/users/eyakubovich/gists{/gist_id}","starred_url":"https://api.github.com/users/eyakubovich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eyakubovich/subscriptions","organizations_url":"https://api.github.com/users/eyakubovich/orgs","repos_url":"https://api.github.com/users/eyakubovich/repos","events_url":"https://api.github.com/users/eyakubovich/events{/privacy}","received_events_url":"https://api.github.com/users/eyakubovich/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/eyakubovich/flannel","description":"flannel is an etcd backed overlay network for containers","fork":true,"url":"https://api.github.com/repos/eyakubovich/flannel","forks_url":"https://api.github.com/repos/eyakubovich/flannel/forks","keys_url":"https://api.github.com/repos/eyakubovich/flannel/keys{/key_id}","collaborators_url":"https://api.github.com/repos/eyakubovich/flannel/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/eyakubovich/flannel/teams","hooks_url":"https://api.github.com/repos/eyakubovich/flannel/hooks","issue_events_url":"https://api.github.com/repos/eyakubovich/flannel/issues/events{/number}","events_url":"https://api.github.com/repos/eyakubovich/flannel/events","assignees_url":"https://api.github.com/repos/eyakubovich/flannel/assignees{/user}","branches_url":"https://api.github.com/repos/eyakubovich/flannel/branches{/branch}","tags_url":"https://api.github.com/repos/eyakubovich/flannel/tags","blobs_url":"https://api.github.com/repos/eyakubovich/flannel/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/eyakubovich/flannel/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/eyakubovich/flannel/git/refs{/sha}","trees_url":"https://api.github.com/repos/eyakubovich/flannel/git/trees{/sha}","statuses_url":"https://api.github.com/repos/eyakubovich/flannel/statuses/{sha}","languages_url":"https://api.github.com/repos/eyakubovich/flannel/languages","stargazers_url":"https://api.github.com/repos/eyakubovich/flannel/stargazers","contributors_url":"https://api.github.com/repos/eyakubovich/flannel/contributors","subscribers_url":"https://api.github.com/repos/eyakubovich/flannel/subscribers","subscription_url":"https://api.github.com/repos/eyakubovich/flannel/subscription","commits_url":"https://api.github.com/repos/eyakubovich/flannel/commits{/sha}","git_commits_url":"https://api.github.com/repos/eyakubovich/flannel/git/commits{/sha}","comments_url":"https://api.github.com/repos/eyakubovich/flannel/comments{/number}","issue_comment_url":"https://api.github.com/repos/eyakubovich/flannel/issues/comments{/number}","contents_url":"https://api.github.com/repos/eyakubovich/flannel/contents/{+path}","compare_url":"https://api.github.com/repos/eyakubovich/flannel/compare/{base}...{head}","merges_url":"https://api.github.com/repos/eyakubovich/flannel/merges","archive_url":"https://api.github.com/repos/eyakubovich/flannel/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/eyakubovich/flannel/downloads","issues_url":"https://api.github.com/repos/eyakubovich/flannel/issues{/number}","pulls_url":"https://api.github.com/repos/eyakubovich/flannel/pulls{/number}","milestones_url":"https://api.github.com/repos/eyakubovich/flannel/milestones{/number}","notifications_url":"https://api.github.com/repos/eyakubovich/flannel/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/eyakubovich/flannel/labels{/name}","releases_url":"https://api.github.com/repos/eyakubovich/flannel/releases{/id}","created_at":"2014-08-28T17:11:27Z","updated_at":"2015-04-24T20:54:32Z","pushed_at":"2015-10-22T01:01:32Z","git_url":"git://github.com/eyakubovich/flannel.git","ssh_url":"git@github.com:eyakubovich/flannel.git","clone_url":"https://github.com/eyakubovich/flannel.git","svn_url":"https://github.com/eyakubovich/flannel","homepage":"","size":4699,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"coreos:master","ref":"master","sha":"86ec8945474a159f20dbc6128f53b19471129aa3","user":{"login":"coreos","id":3730757,"avatar_url":"https://avatars.githubusercontent.com/u/3730757?v=3","gravatar_id":"","url":"https://api.github.com/users/coreos","html_url":"https://github.com/coreos","followers_url":"https://api.github.com/users/coreos/followers","following_url":"https://api.github.com/users/coreos/following{/other_user}","gists_url":"https://api.github.com/users/coreos/gists{/gist_id}","starred_url":"https://api.github.com/users/coreos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coreos/subscriptions","organizations_url":"https://api.github.com/users/coreos/orgs","repos_url":"https://api.github.com/users/coreos/repos","events_url":"https://api.github.com/users/coreos/events{/privacy}","received_events_url":"https://api.github.com/users/coreos/received_events","type":"Organization","site_admin":false},"repo":{"id":21704134,"name":"flannel","full_name":"coreos/flannel","owner":{"login":"coreos","id":3730757,"avatar_url":"https://avatars.githubusercontent.com/u/3730757?v=3","gravatar_id":"","url":"https://api.github.com/users/coreos","html_url":"https://github.com/coreos","followers_url":"https://api.github.com/users/coreos/followers","following_url":"https://api.github.com/users/coreos/following{/other_user}","gists_url":"https://api.github.com/users/coreos/gists{/gist_id}","starred_url":"https://api.github.com/users/coreos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coreos/subscriptions","organizations_url":"https://api.github.com/users/coreos/orgs","repos_url":"https://api.github.com/users/coreos/repos","events_url":"https://api.github.com/users/coreos/events{/privacy}","received_events_url":"https://api.github.com/users/coreos/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/coreos/flannel","description":"flannel is an etcd backed network fabric for containers","fork":false,"url":"https://api.github.com/repos/coreos/flannel","forks_url":"https://api.github.com/repos/coreos/flannel/forks","keys_url":"https://api.github.com/repos/coreos/flannel/keys{/key_id}","collaborators_url":"https://api.github.com/repos/coreos/flannel/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/coreos/flannel/teams","hooks_url":"https://api.github.com/repos/coreos/flannel/hooks","issue_events_url":"https://api.github.com/repos/coreos/flannel/issues/events{/number}","events_url":"https://api.github.com/repos/coreos/flannel/events","assignees_url":"https://api.github.com/repos/coreos/flannel/assignees{/user}","branches_url":"https://api.github.com/repos/coreos/flannel/branches{/branch}","tags_url":"https://api.github.com/repos/coreos/flannel/tags","blobs_url":"https://api.github.com/repos/coreos/flannel/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/coreos/flannel/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/coreos/flannel/git/refs{/sha}","trees_url":"https://api.github.com/repos/coreos/flannel/git/trees{/sha}","statuses_url":"https://api.github.com/repos/coreos/flannel/statuses/{sha}","languages_url":"https://api.github.com/repos/coreos/flannel/languages","stargazers_url":"https://api.github.com/repos/coreos/flannel/stargazers","contributors_url":"https://api.github.com/repos/coreos/flannel/contributors","subscribers_url":"https://api.github.com/repos/coreos/flannel/subscribers","subscription_url":"https://api.github.com/repos/coreos/flannel/subscription","commits_url":"https://api.github.com/repos/coreos/flannel/commits{/sha}","git_commits_url":"https://api.github.com/repos/coreos/flannel/git/commits{/sha}","comments_url":"https://api.github.com/repos/coreos/flannel/comments{/number}","issue_comment_url":"https://api.github.com/repos/coreos/flannel/issues/comments{/number}","contents_url":"https://api.github.com/repos/coreos/flannel/contents/{+path}","compare_url":"https://api.github.com/repos/coreos/flannel/compare/{base}...{head}","merges_url":"https://api.github.com/repos/coreos/flannel/merges","archive_url":"https://api.github.com/repos/coreos/flannel/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/coreos/flannel/downloads","issues_url":"https://api.github.com/repos/coreos/flannel/issues{/number}","pulls_url":"https://api.github.com/repos/coreos/flannel/pulls{/number}","milestones_url":"https://api.github.com/repos/coreos/flannel/milestones{/number}","notifications_url":"https://api.github.com/repos/coreos/flannel/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/coreos/flannel/labels{/name}","releases_url":"https://api.github.com/repos/coreos/flannel/releases{/id}","created_at":"2014-07-10T17:45:29Z","updated_at":"2015-10-23T16:20:21Z","pushed_at":"2015-10-22T01:01:32Z","git_url":"git://github.com/coreos/flannel.git","ssh_url":"git@github.com:coreos/flannel.git","clone_url":"https://github.com/coreos/flannel.git","svn_url":"https://github.com/coreos/flannel","homepage":"","size":5336,"stargazers_count":994,"watchers_count":994,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":132,"mirror_url":null,"open_issues_count":48,"forks":132,"open_issues":48,"watchers":994,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/coreos/flannel/pulls/344"},"html":{"href":"https://github.com/coreos/flannel/pull/344"},"issue":{"href":"https://api.github.com/repos/coreos/flannel/issues/344"},"comments":{"href":"https://api.github.com/repos/coreos/flannel/issues/344/comments"},"review_comments":{"href":"https://api.github.com/repos/coreos/flannel/pulls/344/comments"},"review_comment":{"href":"https://api.github.com/repos/coreos/flannel/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/coreos/flannel/pulls/344/commits"},"statuses":{"href":"https://api.github.com/repos/coreos/flannel/statuses/5a7ee64b394b9900d7b9f1378f956892d25e09f7"}}}}