{"action":"created","comment":{"url":"https://api.github.com/repos/rancher/lb-controller/pulls/comments/107011870","pull_request_review_id":27961311,"id":107011870,"diff_hunk":"@@ -0,0 +1,317 @@\n+package rancher\n+\n+import (\n+\t\"fmt\"\n+\t\"github.com/Sirupsen/logrus\"\n+\t\"github.com/fsnotify/fsnotify\"\n+\t\"github.com/rancher/go-rancher-metadata/metadata\"\n+\t\"github.com/rancher/go-rancher/v2\"\n+\t\"github.com/rancher/lb-controller/config\"\n+\t\"io/ioutil\"\n+\t\"os\"\n+\t\"path\"\n+\t\"path/filepath\"\n+\t\"strings\"\n+\t\"sync\"\n+)\n+\n+const (\n+\tCertName = \"fullchain.pem\"\n+\tKeyName  = \"privkey.pem\"\n+)\n+\n+type CertificateFetcher interface {\n+\tFetchCertificate(certName string) (*config.Certificate, error)\n+\tUpdateEndpoints(lbSvc *metadata.Service, eps []client.PublicEndpoint) error\n+\tReadAllCertificatesFromDir(certDir string) ([]*config.Certificate, error)\n+\tReadDefaultCertificate(defaultCertDir string) (*config.Certificate, error)\n+\tStopWatcher() error\n+\tProcessFileUpdateEvents(do func(string))\n+}\n+\n+type RCertificateFetcher struct {\n+\tClient         *client.RancherClient\n+\tCertsCache     map[string]*config.Certificate //cert name (sub dir name) -> cert\n+\tCertDir        string\n+\tDefaultCertDir string\n+\tDefaultCert    *config.Certificate\n+\tWatcher        *fsnotify.Watcher\n+\tmu             *sync.RWMutex\n+\tTempCertsMap   map[string]*config.Certificate //cert name (sub dir name) -> cert\n+}\n+\n+func (fetcher *RCertificateFetcher) FetchCertificate(certName string) (*config.Certificate, error) {\n+\tif certName == \"\" {\n+\t\treturn nil, nil\n+\t}\n+\topts := client.NewListOpts()\n+\topts.Filters[\"name\"] = certName\n+\topts.Filters[\"removed_null\"] = \"1\"\n+\n+\tcerts, err := fetcher.Client.Certificate.List(opts)\n+\tif err != nil {\n+\t\treturn nil, fmt.Errorf(\"Coudln't get certificate by name [%s]. Error: %#v\", certName, err)\n+\t}\n+\tvar cert client.Certificate\n+\tvar certWithChain string\n+\tif len(certs.Data) >= 1 {\n+\t\tcert = certs.Data[0]\n+\t\tcertWithChain = fmt.Sprintf(\"%s\\n%s\", cert.Cert, cert.CertChain)\n+\t}\n+\treturn &config.Certificate{\n+\t\tName: cert.Name,\n+\t\tKey:  cert.Key,\n+\t\tCert: certWithChain,\n+\t}, nil\n+}\n+\n+func (fetcher *RCertificateFetcher) UpdateEndpoints(lbSvc *metadata.Service, eps []client.PublicEndpoint) error {\n+\topts := client.NewListOpts()\n+\topts.Filters[\"uuid\"] = lbSvc.UUID\n+\topts.Filters[\"removed_null\"] = \"1\"\n+\tlbs, err := fetcher.Client.LoadBalancerService.List(opts)\n+\tif err != nil {\n+\t\treturn fmt.Errorf(\"Coudln't get LB service by uuid [%s]. Error: %#v\", lbSvc.UUID, err)\n+\t}\n+\tif len(lbs.Data) == 0 {\n+\t\tlogrus.Infof(\"Failed to find lb by uuid %s\", lbSvc.UUID)\n+\t\treturn nil\n+\t}\n+\tlb := lbs.Data[0]\n+\n+\ttoUpdate := make(map[string]interface{})\n+\ttoUpdate[\"publicEndpoints\"] = eps\n+\tlogrus.Infof(\"Updating Rancher LB [%s] in stack [%s] with the new public endpoints [%v] \", lbSvc.Name, lbSvc.StackName, eps)\n+\tif _, err := fetcher.Client.LoadBalancerService.Update(&lb, toUpdate); err != nil {\n+\t\treturn fmt.Errorf(\"Failed to update Rancher LB [%s] in stack [%s]. Error: %#v\", lbSvc.Name, lbSvc.StackName, err)\n+\t}\n+\treturn nil\n+}\n+\n+/* Cache the certs present under CertDir and DefaultCertDir\n+   When the Cache is null, read from the dir and init the cache. When cache is not empty read from the cache.\n+   Clear cache when the directories change\n+   Start a Watcher thread when initialising the cache. For every certName, add to watcher.\n+   When watcher receives event, read that cert and update cache.\n+   Add a watcher for CertDir/DefaultCertDir also - upon events on these dirs clear the cache and enque.*/\n+\n+func (fetcher *RCertificateFetcher) ReadAllCertificatesFromDir(certDir string) ([]*config.Certificate, error) {\n+\tif _, err := os.Stat(certDir); os.IsNotExist(err) {\n+\t\treturn nil, fmt.Errorf(\"Certificate Directory does not exist %v\", certDir)\n+\t}\n+\tcerts := []*config.Certificate{}\n+\n+\tfetcher.mu.RLock()\n+\tcurrentCertD := fetcher.CertDir\n+\tfetcher.mu.RUnlock()\n+\n+\tif currentCertD != \"\" && currentCertD == certDir && fetcher.CertsCache != nil && len(fetcher.CertsCache) > 0 {\n+\t\t//read from cache if certDir matches and certs are cached.\n+\t\tlogrus.Debug(\"Reading certs from cache\")\n+\t} else {\n+\t\t// init/re-init cache\n+\t\tfetcher.mu.Lock()\n+\t\tfetcher.removeWatches()\n+\n+\t\tfetcher.CertsCache = make(map[string]*config.Certificate)\n+\t\tlogrus.Debug(\"Reading certs from directory and starting watchers\")\n+\t\tfetcher.TempCertsMap = make(map[string]*config.Certificate)\n+\t\terr := filepath.Walk(certDir, fetcher.readCertificate)\n+\t\tif err != nil {\n+\t\t\tfetcher.CertDir = \"\"\n+\t\t\tfetcher.CertsCache = nil\n+\t\t\tlogrus.Errorf(\"Error %v reading certs from directory %v\", err, certDir)","path":"controller/rancher/certificate_fetcher.go","position":123,"original_position":123,"commit_id":"598fc24422cbf6e1cd0284439bcf5655fc48fedc","original_commit_id":"598fc24422cbf6e1cd0284439bcf5655fc48fedc","user":{"login":"alena1108","id":1562176,"avatar_url":"https://avatars3.githubusercontent.com/u/1562176?v=3","gravatar_id":"","url":"https://api.github.com/users/alena1108","html_url":"https://github.com/alena1108","followers_url":"https://api.github.com/users/alena1108/followers","following_url":"https://api.github.com/users/alena1108/following{/other_user}","gists_url":"https://api.github.com/users/alena1108/gists{/gist_id}","starred_url":"https://api.github.com/users/alena1108/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alena1108/subscriptions","organizations_url":"https://api.github.com/users/alena1108/orgs","repos_url":"https://api.github.com/users/alena1108/repos","events_url":"https://api.github.com/users/alena1108/events{/privacy}","received_events_url":"https://api.github.com/users/alena1108/received_events","type":"User","site_admin":false},"body":"@prachidamle we better error out and NOT reload the config which is already running with the previously fetched certificates.","created_at":"2017-03-20T20:46:16Z","updated_at":"2017-03-20T20:46:16Z","html_url":"https://github.com/rancher/lb-controller/pull/63#discussion_r107011870","pull_request_url":"https://api.github.com/repos/rancher/lb-controller/pulls/63","_links":{"self":{"href":"https://api.github.com/repos/rancher/lb-controller/pulls/comments/107011870"},"html":{"href":"https://github.com/rancher/lb-controller/pull/63#discussion_r107011870"},"pull_request":{"href":"https://api.github.com/repos/rancher/lb-controller/pulls/63"}}},"pull_request":{"url":"https://api.github.com/repos/rancher/lb-controller/pulls/63","id":110958282,"html_url":"https://github.com/rancher/lb-controller/pull/63","diff_url":"https://github.com/rancher/lb-controller/pull/63.diff","patch_url":"https://github.com/rancher/lb-controller/pull/63.patch","issue_url":"https://api.github.com/repos/rancher/lb-controller/issues/63","number":63,"state":"open","locked":false,"title":"WIP: Load certificates in lb-controller by specifying a certificate directory","user":{"login":"prachidamle","id":1470602,"avatar_url":"https://avatars1.githubusercontent.com/u/1470602?v=3","gravatar_id":"","url":"https://api.github.com/users/prachidamle","html_url":"https://github.com/prachidamle","followers_url":"https://api.github.com/users/prachidamle/followers","following_url":"https://api.github.com/users/prachidamle/following{/other_user}","gists_url":"https://api.github.com/users/prachidamle/gists{/gist_id}","starred_url":"https://api.github.com/users/prachidamle/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/prachidamle/subscriptions","organizations_url":"https://api.github.com/users/prachidamle/orgs","repos_url":"https://api.github.com/users/prachidamle/repos","events_url":"https://api.github.com/users/prachidamle/events{/privacy}","received_events_url":"https://api.github.com/users/prachidamle/received_events","type":"User","site_admin":false},"body":"- This adds the ability to load the certificates from the dedicated directory of certificates.\r\n- Volume is mounted to the LB service and the directory is specified via labels \r\n\"io.rancher.lb_service.defaultCertDir\" and \"io.rancher.lb_service.certDir\"\r\n- This feature enhances controller to read certs from the directory instead of from Rancher.\r\n- When the labels are present, controller will not load certs uploaded on Rancher\r\n- Updates to the certificate directory should trigger a reload of haproxy config\r\n\r\n- Tests have been added as well controller/rancher/rancher_cert_dir_test.go\r\n\r\nhttps://github.com/rancher/rancher/issues/7520","created_at":"2017-03-15T22:42:59Z","updated_at":"2017-03-20T20:46:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ca9189d689f1c6f9eb4d49faa377b3b3a50f3391","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/rancher/lb-controller/pulls/63/commits","review_comments_url":"https://api.github.com/repos/rancher/lb-controller/pulls/63/comments","review_comment_url":"https://api.github.com/repos/rancher/lb-controller/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rancher/lb-controller/issues/63/comments","statuses_url":"https://api.github.com/repos/rancher/lb-controller/statuses/598fc24422cbf6e1cd0284439bcf5655fc48fedc","head":{"label":"prachidamle:haproxy_cert_dir","ref":"haproxy_cert_dir","sha":"598fc24422cbf6e1cd0284439bcf5655fc48fedc","user":{"login":"prachidamle","id":1470602,"avatar_url":"https://avatars1.githubusercontent.com/u/1470602?v=3","gravatar_id":"","url":"https://api.github.com/users/prachidamle","html_url":"https://github.com/prachidamle","followers_url":"https://api.github.com/users/prachidamle/followers","following_url":"https://api.github.com/users/prachidamle/following{/other_user}","gists_url":"https://api.github.com/users/prachidamle/gists{/gist_id}","starred_url":"https://api.github.com/users/prachidamle/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/prachidamle/subscriptions","organizations_url":"https://api.github.com/users/prachidamle/orgs","repos_url":"https://api.github.com/users/prachidamle/repos","events_url":"https://api.github.com/users/prachidamle/events{/privacy}","received_events_url":"https://api.github.com/users/prachidamle/received_events","type":"User","site_admin":false},"repo":{"id":81876571,"name":"lb-controller","full_name":"prachidamle/lb-controller","owner":{"login":"prachidamle","id":1470602,"avatar_url":"https://avatars1.githubusercontent.com/u/1470602?v=3","gravatar_id":"","url":"https://api.github.com/users/prachidamle","html_url":"https://github.com/prachidamle","followers_url":"https://api.github.com/users/prachidamle/followers","following_url":"https://api.github.com/users/prachidamle/following{/other_user}","gists_url":"https://api.github.com/users/prachidamle/gists{/gist_id}","starred_url":"https://api.github.com/users/prachidamle/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/prachidamle/subscriptions","organizations_url":"https://api.github.com/users/prachidamle/orgs","repos_url":"https://api.github.com/users/prachidamle/repos","events_url":"https://api.github.com/users/prachidamle/events{/privacy}","received_events_url":"https://api.github.com/users/prachidamle/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/prachidamle/lb-controller","description":"Load Balancer for Rancher services via ingress controllers backed up by a Load Balancer provider of choice ","fork":true,"url":"https://api.github.com/repos/prachidamle/lb-controller","forks_url":"https://api.github.com/repos/prachidamle/lb-controller/forks","keys_url":"https://api.github.com/repos/prachidamle/lb-controller/keys{/key_id}","collaborators_url":"https://api.github.com/repos/prachidamle/lb-controller/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/prachidamle/lb-controller/teams","hooks_url":"https://api.github.com/repos/prachidamle/lb-controller/hooks","issue_events_url":"https://api.github.com/repos/prachidamle/lb-controller/issues/events{/number}","events_url":"https://api.github.com/repos/prachidamle/lb-controller/events","assignees_url":"https://api.github.com/repos/prachidamle/lb-controller/assignees{/user}","branches_url":"https://api.github.com/repos/prachidamle/lb-controller/branches{/branch}","tags_url":"https://api.github.com/repos/prachidamle/lb-controller/tags","blobs_url":"https://api.github.com/repos/prachidamle/lb-controller/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/prachidamle/lb-controller/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/prachidamle/lb-controller/git/refs{/sha}","trees_url":"https://api.github.com/repos/prachidamle/lb-controller/git/trees{/sha}","statuses_url":"https://api.github.com/repos/prachidamle/lb-controller/statuses/{sha}","languages_url":"https://api.github.com/repos/prachidamle/lb-controller/languages","stargazers_url":"https://api.github.com/repos/prachidamle/lb-controller/stargazers","contributors_url":"https://api.github.com/repos/prachidamle/lb-controller/contributors","subscribers_url":"https://api.github.com/repos/prachidamle/lb-controller/subscribers","subscription_url":"https://api.github.com/repos/prachidamle/lb-controller/subscription","commits_url":"https://api.github.com/repos/prachidamle/lb-controller/commits{/sha}","git_commits_url":"https://api.github.com/repos/prachidamle/lb-controller/git/commits{/sha}","comments_url":"https://api.github.com/repos/prachidamle/lb-controller/comments{/number}","issue_comment_url":"https://api.github.com/repos/prachidamle/lb-controller/issues/comments{/number}","contents_url":"https://api.github.com/repos/prachidamle/lb-controller/contents/{+path}","compare_url":"https://api.github.com/repos/prachidamle/lb-controller/compare/{base}...{head}","merges_url":"https://api.github.com/repos/prachidamle/lb-controller/merges","archive_url":"https://api.github.com/repos/prachidamle/lb-controller/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/prachidamle/lb-controller/downloads","issues_url":"https://api.github.com/repos/prachidamle/lb-controller/issues{/number}","pulls_url":"https://api.github.com/repos/prachidamle/lb-controller/pulls{/number}","milestones_url":"https://api.github.com/repos/prachidamle/lb-controller/milestones{/number}","notifications_url":"https://api.github.com/repos/prachidamle/lb-controller/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/prachidamle/lb-controller/labels{/name}","releases_url":"https://api.github.com/repos/prachidamle/lb-controller/releases{/id}","deployments_url":"https://api.github.com/repos/prachidamle/lb-controller/deployments","created_at":"2017-02-13T22:02:24Z","updated_at":"2017-02-13T22:02:27Z","pushed_at":"2017-03-20T19:25:57Z","git_url":"git://github.com/prachidamle/lb-controller.git","ssh_url":"git@github.com:prachidamle/lb-controller.git","clone_url":"https://github.com/prachidamle/lb-controller.git","svn_url":"https://github.com/prachidamle/lb-controller","homepage":null,"size":4906,"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":"rancher:master","ref":"master","sha":"db5e55794971693c8dd2f8714e05b7c3c6c534ab","user":{"login":"rancher","id":9343010,"avatar_url":"https://avatars0.githubusercontent.com/u/9343010?v=3","gravatar_id":"","url":"https://api.github.com/users/rancher","html_url":"https://github.com/rancher","followers_url":"https://api.github.com/users/rancher/followers","following_url":"https://api.github.com/users/rancher/following{/other_user}","gists_url":"https://api.github.com/users/rancher/gists{/gist_id}","starred_url":"https://api.github.com/users/rancher/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rancher/subscriptions","organizations_url":"https://api.github.com/users/rancher/orgs","repos_url":"https://api.github.com/users/rancher/repos","events_url":"https://api.github.com/users/rancher/events{/privacy}","received_events_url":"https://api.github.com/users/rancher/received_events","type":"Organization","site_admin":false},"repo":{"id":57984557,"name":"lb-controller","full_name":"rancher/lb-controller","owner":{"login":"rancher","id":9343010,"avatar_url":"https://avatars0.githubusercontent.com/u/9343010?v=3","gravatar_id":"","url":"https://api.github.com/users/rancher","html_url":"https://github.com/rancher","followers_url":"https://api.github.com/users/rancher/followers","following_url":"https://api.github.com/users/rancher/following{/other_user}","gists_url":"https://api.github.com/users/rancher/gists{/gist_id}","starred_url":"https://api.github.com/users/rancher/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rancher/subscriptions","organizations_url":"https://api.github.com/users/rancher/orgs","repos_url":"https://api.github.com/users/rancher/repos","events_url":"https://api.github.com/users/rancher/events{/privacy}","received_events_url":"https://api.github.com/users/rancher/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rancher/lb-controller","description":"Load Balancer for Rancher services via ingress controllers backed up by a Load Balancer provider of choice ","fork":false,"url":"https://api.github.com/repos/rancher/lb-controller","forks_url":"https://api.github.com/repos/rancher/lb-controller/forks","keys_url":"https://api.github.com/repos/rancher/lb-controller/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rancher/lb-controller/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rancher/lb-controller/teams","hooks_url":"https://api.github.com/repos/rancher/lb-controller/hooks","issue_events_url":"https://api.github.com/repos/rancher/lb-controller/issues/events{/number}","events_url":"https://api.github.com/repos/rancher/lb-controller/events","assignees_url":"https://api.github.com/repos/rancher/lb-controller/assignees{/user}","branches_url":"https://api.github.com/repos/rancher/lb-controller/branches{/branch}","tags_url":"https://api.github.com/repos/rancher/lb-controller/tags","blobs_url":"https://api.github.com/repos/rancher/lb-controller/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rancher/lb-controller/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rancher/lb-controller/git/refs{/sha}","trees_url":"https://api.github.com/repos/rancher/lb-controller/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rancher/lb-controller/statuses/{sha}","languages_url":"https://api.github.com/repos/rancher/lb-controller/languages","stargazers_url":"https://api.github.com/repos/rancher/lb-controller/stargazers","contributors_url":"https://api.github.com/repos/rancher/lb-controller/contributors","subscribers_url":"https://api.github.com/repos/rancher/lb-controller/subscribers","subscription_url":"https://api.github.com/repos/rancher/lb-controller/subscription","commits_url":"https://api.github.com/repos/rancher/lb-controller/commits{/sha}","git_commits_url":"https://api.github.com/repos/rancher/lb-controller/git/commits{/sha}","comments_url":"https://api.github.com/repos/rancher/lb-controller/comments{/number}","issue_comment_url":"https://api.github.com/repos/rancher/lb-controller/issues/comments{/number}","contents_url":"https://api.github.com/repos/rancher/lb-controller/contents/{+path}","compare_url":"https://api.github.com/repos/rancher/lb-controller/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rancher/lb-controller/merges","archive_url":"https://api.github.com/repos/rancher/lb-controller/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rancher/lb-controller/downloads","issues_url":"https://api.github.com/repos/rancher/lb-controller/issues{/number}","pulls_url":"https://api.github.com/repos/rancher/lb-controller/pulls{/number}","milestones_url":"https://api.github.com/repos/rancher/lb-controller/milestones{/number}","notifications_url":"https://api.github.com/repos/rancher/lb-controller/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rancher/lb-controller/labels{/name}","releases_url":"https://api.github.com/repos/rancher/lb-controller/releases{/id}","deployments_url":"https://api.github.com/repos/rancher/lb-controller/deployments","created_at":"2016-05-03T16:35:25Z","updated_at":"2017-03-08T02:11:03Z","pushed_at":"2017-03-20T19:25:59Z","git_url":"git://github.com/rancher/lb-controller.git","ssh_url":"git@github.com:rancher/lb-controller.git","clone_url":"https://github.com/rancher/lb-controller.git","svn_url":"https://github.com/rancher/lb-controller","homepage":null,"size":4867,"stargazers_count":36,"watchers_count":36,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":24,"mirror_url":null,"open_issues_count":2,"forks":24,"open_issues":2,"watchers":36,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rancher/lb-controller/pulls/63"},"html":{"href":"https://github.com/rancher/lb-controller/pull/63"},"issue":{"href":"https://api.github.com/repos/rancher/lb-controller/issues/63"},"comments":{"href":"https://api.github.com/repos/rancher/lb-controller/issues/63/comments"},"review_comments":{"href":"https://api.github.com/repos/rancher/lb-controller/pulls/63/comments"},"review_comment":{"href":"https://api.github.com/repos/rancher/lb-controller/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rancher/lb-controller/pulls/63/commits"},"statuses":{"href":"https://api.github.com/repos/rancher/lb-controller/statuses/598fc24422cbf6e1cd0284439bcf5655fc48fedc"}}}}
{"actor":{"display_login":"alena1108"}}