{"action":"created","comment":{"url":"https://api.github.com/repos/projectcalico/calico-rkt/pulls/comments/47312454","id":47312454,"diff_hunk":"@@ -0,0 +1,149 @@\n+# Copyright 2015 Metaswitch Networks\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+import logging\n+from constants import *\n+from calico_rkt import configure_logging\n+from pycalico.datastore import DatastoreClient\n+from pycalico.datastore_datatypes import Rule, Rules\n+\n+KUBERNETES_DEFAULT_PROFILE = \"default_profile\"\n+\n+_log = logging.getLogger(__name__)\n+configure_logging(_log)\n+\n+class BasePolicyDriver(object):\n+    \"\"\"\n+    Abstract base class of a CNI Policy Driver.\n+\n+    The CNI Policy Driver is responsible for applying network policy to\n+    networked containers and creating/applying Calico profiles to Calico\n+    endpoints.\n+    \"\"\"\n+    def __init__(self):\n+\n+        self._client = DatastoreClient()\n+        \"\"\"\n+        DatastoreClient for access to the Calico datastore.\n+        \"\"\"\n+\n+        self.profile_name = None\n+        \"\"\"\n+        Name of profile for attach to endpoint. Must be set in init function\n+        of subclass.\n+        \"\"\"\n+\n+    def set_profile(self, endpoint):\n+        \"\"\"Sets a profile for the networked container on the given endpoint.\n+\n+        Create a profile if it is not yet created.\n+\n+        :param endpoint:\n+        :return: None\n+        \"\"\"\n+        assert self.profile_name, \"No profile name set.\"\n+        if not self._client.profile_exists(self.profile_name):\n+            # If the profile doesn't exist, create it.\n+            _log.info(\"Creating new profile '%s'\", self.profile_name)\n+            rules = self.generate_rules()\n+            self._client.create_profile(self.profile_name, rules)\n+\n+        # Set the default profile on this pod's Calico endpoint.\n+        _log.info(\"Setting profile '%s' on endpoint %s\",\n+                  self.profile_name, endpoint.endpoint_id)\n+        self._client.set_profiles_on_endpoint(profile_names=[self.profile_name],\n+                                              endpoint_id=endpoint.endpoint_id)\n+\n+    def remove_profile(self):\n+        \"\"\"Remove the profile if there are no endpoints attached.\n+\n+        :return: None\n+        \"\"\"\n+        raise NotImplementedError(\"Must implement method in subclass\")\n+\n+    def generate_rules(self):\n+        \"\"\"Generates Calico Rules\n+\n+        :rtype: Calico Rules datatype or None\n+        :return: rules - contains inbound and outbound policy rules; None\n+        uses default rules\n+        \"\"\"\n+        raise NotImplementedError(\"Must implement method in subclass\")\n+\n+\n+class DefaultPolicyDriver(BasePolicyDriver):\n+    \"\"\"\n+    Implements default network policy for a generic CNI plugin.\n+    \"\"\"\n+    def __init__(self, network_name):\n+        BasePolicyDriver.__init__(self)\n+        self.profile_name = network_name","path":"calico_rkt/calico_policy.py","position":91,"original_position":91,"commit_id":"b34e3725315ee1bdae27c9f346859a208ce2f40a","original_commit_id":"b34e3725315ee1bdae27c9f346859a208ce2f40a","user":{"login":"caseydavenport","id":1944093,"avatar_url":"https://avatars.githubusercontent.com/u/1944093?v=3","gravatar_id":"","url":"https://api.github.com/users/caseydavenport","html_url":"https://github.com/caseydavenport","followers_url":"https://api.github.com/users/caseydavenport/followers","following_url":"https://api.github.com/users/caseydavenport/following{/other_user}","gists_url":"https://api.github.com/users/caseydavenport/gists{/gist_id}","starred_url":"https://api.github.com/users/caseydavenport/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caseydavenport/subscriptions","organizations_url":"https://api.github.com/users/caseydavenport/orgs","repos_url":"https://api.github.com/users/caseydavenport/repos","events_url":"https://api.github.com/users/caseydavenport/events{/privacy}","received_events_url":"https://api.github.com/users/caseydavenport/received_events","type":"User","site_admin":false},"body":"I think we're going to need to validate and sanitize this network name.  Calico profiles have a limited character set, so we can't just allow any old string here.\r\n\r\nMight need to check for illegal characters and raise an Exception if we find them.","created_at":"2015-12-11T01:32:54Z","updated_at":"2015-12-11T01:32:54Z","html_url":"https://github.com/projectcalico/calico-rkt/pull/8#discussion_r47312454","pull_request_url":"https://api.github.com/repos/projectcalico/calico-rkt/pulls/8","_links":{"self":{"href":"https://api.github.com/repos/projectcalico/calico-rkt/pulls/comments/47312454"},"html":{"href":"https://github.com/projectcalico/calico-rkt/pull/8#discussion_r47312454"},"pull_request":{"href":"https://api.github.com/repos/projectcalico/calico-rkt/pulls/8"}}},"pull_request":{"url":"https://api.github.com/repos/projectcalico/calico-rkt/pulls/8","id":53187601,"html_url":"https://github.com/projectcalico/calico-rkt/pull/8","diff_url":"https://github.com/projectcalico/calico-rkt/pull/8.diff","patch_url":"https://github.com/projectcalico/calico-rkt/pull/8.patch","issue_url":"https://api.github.com/repos/projectcalico/calico-rkt/issues/8","number":8,"state":"open","locked":false,"title":"Create policy plugin for CNI","user":{"login":"L-MA","id":9704719,"avatar_url":"https://avatars.githubusercontent.com/u/9704719?v=3","gravatar_id":"","url":"https://api.github.com/users/L-MA","html_url":"https://github.com/L-MA","followers_url":"https://api.github.com/users/L-MA/followers","following_url":"https://api.github.com/users/L-MA/following{/other_user}","gists_url":"https://api.github.com/users/L-MA/gists{/gist_id}","starred_url":"https://api.github.com/users/L-MA/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/L-MA/subscriptions","organizations_url":"https://api.github.com/users/L-MA/orgs","repos_url":"https://api.github.com/users/L-MA/repos","events_url":"https://api.github.com/users/L-MA/events{/privacy}","received_events_url":"https://api.github.com/users/L-MA/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-12-09T22:26:53Z","updated_at":"2015-12-11T01:32:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ed74c51cfd9b4f0f56d32cf5f337b18cb8af0ccf","assignee":{"login":"caseydavenport","id":1944093,"avatar_url":"https://avatars.githubusercontent.com/u/1944093?v=3","gravatar_id":"","url":"https://api.github.com/users/caseydavenport","html_url":"https://github.com/caseydavenport","followers_url":"https://api.github.com/users/caseydavenport/followers","following_url":"https://api.github.com/users/caseydavenport/following{/other_user}","gists_url":"https://api.github.com/users/caseydavenport/gists{/gist_id}","starred_url":"https://api.github.com/users/caseydavenport/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caseydavenport/subscriptions","organizations_url":"https://api.github.com/users/caseydavenport/orgs","repos_url":"https://api.github.com/users/caseydavenport/repos","events_url":"https://api.github.com/users/caseydavenport/events{/privacy}","received_events_url":"https://api.github.com/users/caseydavenport/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/projectcalico/calico-rkt/pulls/8/commits","review_comments_url":"https://api.github.com/repos/projectcalico/calico-rkt/pulls/8/comments","review_comment_url":"https://api.github.com/repos/projectcalico/calico-rkt/pulls/comments{/number}","comments_url":"https://api.github.com/repos/projectcalico/calico-rkt/issues/8/comments","statuses_url":"https://api.github.com/repos/projectcalico/calico-rkt/statuses/b34e3725315ee1bdae27c9f346859a208ce2f40a","head":{"label":"L-MA:lma-policy-plugins","ref":"lma-policy-plugins","sha":"b34e3725315ee1bdae27c9f346859a208ce2f40a","user":{"login":"L-MA","id":9704719,"avatar_url":"https://avatars.githubusercontent.com/u/9704719?v=3","gravatar_id":"","url":"https://api.github.com/users/L-MA","html_url":"https://github.com/L-MA","followers_url":"https://api.github.com/users/L-MA/followers","following_url":"https://api.github.com/users/L-MA/following{/other_user}","gists_url":"https://api.github.com/users/L-MA/gists{/gist_id}","starred_url":"https://api.github.com/users/L-MA/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/L-MA/subscriptions","organizations_url":"https://api.github.com/users/L-MA/orgs","repos_url":"https://api.github.com/users/L-MA/repos","events_url":"https://api.github.com/users/L-MA/events{/privacy}","received_events_url":"https://api.github.com/users/L-MA/received_events","type":"User","site_admin":false},"repo":{"id":44204927,"name":"calico-rkt","full_name":"L-MA/calico-rkt","owner":{"login":"L-MA","id":9704719,"avatar_url":"https://avatars.githubusercontent.com/u/9704719?v=3","gravatar_id":"","url":"https://api.github.com/users/L-MA","html_url":"https://github.com/L-MA","followers_url":"https://api.github.com/users/L-MA/followers","following_url":"https://api.github.com/users/L-MA/following{/other_user}","gists_url":"https://api.github.com/users/L-MA/gists{/gist_id}","starred_url":"https://api.github.com/users/L-MA/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/L-MA/subscriptions","organizations_url":"https://api.github.com/users/L-MA/orgs","repos_url":"https://api.github.com/users/L-MA/repos","events_url":"https://api.github.com/users/L-MA/events{/privacy}","received_events_url":"https://api.github.com/users/L-MA/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/L-MA/calico-rkt","description":"Calico plugin for rkt deployments","fork":true,"url":"https://api.github.com/repos/L-MA/calico-rkt","forks_url":"https://api.github.com/repos/L-MA/calico-rkt/forks","keys_url":"https://api.github.com/repos/L-MA/calico-rkt/keys{/key_id}","collaborators_url":"https://api.github.com/repos/L-MA/calico-rkt/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/L-MA/calico-rkt/teams","hooks_url":"https://api.github.com/repos/L-MA/calico-rkt/hooks","issue_events_url":"https://api.github.com/repos/L-MA/calico-rkt/issues/events{/number}","events_url":"https://api.github.com/repos/L-MA/calico-rkt/events","assignees_url":"https://api.github.com/repos/L-MA/calico-rkt/assignees{/user}","branches_url":"https://api.github.com/repos/L-MA/calico-rkt/branches{/branch}","tags_url":"https://api.github.com/repos/L-MA/calico-rkt/tags","blobs_url":"https://api.github.com/repos/L-MA/calico-rkt/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/L-MA/calico-rkt/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/L-MA/calico-rkt/git/refs{/sha}","trees_url":"https://api.github.com/repos/L-MA/calico-rkt/git/trees{/sha}","statuses_url":"https://api.github.com/repos/L-MA/calico-rkt/statuses/{sha}","languages_url":"https://api.github.com/repos/L-MA/calico-rkt/languages","stargazers_url":"https://api.github.com/repos/L-MA/calico-rkt/stargazers","contributors_url":"https://api.github.com/repos/L-MA/calico-rkt/contributors","subscribers_url":"https://api.github.com/repos/L-MA/calico-rkt/subscribers","subscription_url":"https://api.github.com/repos/L-MA/calico-rkt/subscription","commits_url":"https://api.github.com/repos/L-MA/calico-rkt/commits{/sha}","git_commits_url":"https://api.github.com/repos/L-MA/calico-rkt/git/commits{/sha}","comments_url":"https://api.github.com/repos/L-MA/calico-rkt/comments{/number}","issue_comment_url":"https://api.github.com/repos/L-MA/calico-rkt/issues/comments{/number}","contents_url":"https://api.github.com/repos/L-MA/calico-rkt/contents/{+path}","compare_url":"https://api.github.com/repos/L-MA/calico-rkt/compare/{base}...{head}","merges_url":"https://api.github.com/repos/L-MA/calico-rkt/merges","archive_url":"https://api.github.com/repos/L-MA/calico-rkt/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/L-MA/calico-rkt/downloads","issues_url":"https://api.github.com/repos/L-MA/calico-rkt/issues{/number}","pulls_url":"https://api.github.com/repos/L-MA/calico-rkt/pulls{/number}","milestones_url":"https://api.github.com/repos/L-MA/calico-rkt/milestones{/number}","notifications_url":"https://api.github.com/repos/L-MA/calico-rkt/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/L-MA/calico-rkt/labels{/name}","releases_url":"https://api.github.com/repos/L-MA/calico-rkt/releases{/id}","created_at":"2015-10-13T21:07:45Z","updated_at":"2015-10-13T21:07:45Z","pushed_at":"2015-12-11T01:01:35Z","git_url":"git://github.com/L-MA/calico-rkt.git","ssh_url":"git@github.com:L-MA/calico-rkt.git","clone_url":"https://github.com/L-MA/calico-rkt.git","svn_url":"https://github.com/L-MA/calico-rkt","homepage":"http://www.projectcalico.org/","size":51,"stargazers_count":0,"watchers_count":0,"language":"Python","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":"projectcalico:k8s-cni-integration","ref":"k8s-cni-integration","sha":"0f82f9b426432e7c1dabddc3a1976a199f94a948","user":{"login":"projectcalico","id":12304728,"avatar_url":"https://avatars.githubusercontent.com/u/12304728?v=3","gravatar_id":"","url":"https://api.github.com/users/projectcalico","html_url":"https://github.com/projectcalico","followers_url":"https://api.github.com/users/projectcalico/followers","following_url":"https://api.github.com/users/projectcalico/following{/other_user}","gists_url":"https://api.github.com/users/projectcalico/gists{/gist_id}","starred_url":"https://api.github.com/users/projectcalico/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/projectcalico/subscriptions","organizations_url":"https://api.github.com/users/projectcalico/orgs","repos_url":"https://api.github.com/users/projectcalico/repos","events_url":"https://api.github.com/users/projectcalico/events{/privacy}","received_events_url":"https://api.github.com/users/projectcalico/received_events","type":"Organization","site_admin":false},"repo":{"id":39909298,"name":"calico-rkt","full_name":"projectcalico/calico-rkt","owner":{"login":"projectcalico","id":12304728,"avatar_url":"https://avatars.githubusercontent.com/u/12304728?v=3","gravatar_id":"","url":"https://api.github.com/users/projectcalico","html_url":"https://github.com/projectcalico","followers_url":"https://api.github.com/users/projectcalico/followers","following_url":"https://api.github.com/users/projectcalico/following{/other_user}","gists_url":"https://api.github.com/users/projectcalico/gists{/gist_id}","starred_url":"https://api.github.com/users/projectcalico/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/projectcalico/subscriptions","organizations_url":"https://api.github.com/users/projectcalico/orgs","repos_url":"https://api.github.com/users/projectcalico/repos","events_url":"https://api.github.com/users/projectcalico/events{/privacy}","received_events_url":"https://api.github.com/users/projectcalico/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/projectcalico/calico-rkt","description":"Calico plugin for rkt deployments","fork":false,"url":"https://api.github.com/repos/projectcalico/calico-rkt","forks_url":"https://api.github.com/repos/projectcalico/calico-rkt/forks","keys_url":"https://api.github.com/repos/projectcalico/calico-rkt/keys{/key_id}","collaborators_url":"https://api.github.com/repos/projectcalico/calico-rkt/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/projectcalico/calico-rkt/teams","hooks_url":"https://api.github.com/repos/projectcalico/calico-rkt/hooks","issue_events_url":"https://api.github.com/repos/projectcalico/calico-rkt/issues/events{/number}","events_url":"https://api.github.com/repos/projectcalico/calico-rkt/events","assignees_url":"https://api.github.com/repos/projectcalico/calico-rkt/assignees{/user}","branches_url":"https://api.github.com/repos/projectcalico/calico-rkt/branches{/branch}","tags_url":"https://api.github.com/repos/projectcalico/calico-rkt/tags","blobs_url":"https://api.github.com/repos/projectcalico/calico-rkt/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/projectcalico/calico-rkt/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/projectcalico/calico-rkt/git/refs{/sha}","trees_url":"https://api.github.com/repos/projectcalico/calico-rkt/git/trees{/sha}","statuses_url":"https://api.github.com/repos/projectcalico/calico-rkt/statuses/{sha}","languages_url":"https://api.github.com/repos/projectcalico/calico-rkt/languages","stargazers_url":"https://api.github.com/repos/projectcalico/calico-rkt/stargazers","contributors_url":"https://api.github.com/repos/projectcalico/calico-rkt/contributors","subscribers_url":"https://api.github.com/repos/projectcalico/calico-rkt/subscribers","subscription_url":"https://api.github.com/repos/projectcalico/calico-rkt/subscription","commits_url":"https://api.github.com/repos/projectcalico/calico-rkt/commits{/sha}","git_commits_url":"https://api.github.com/repos/projectcalico/calico-rkt/git/commits{/sha}","comments_url":"https://api.github.com/repos/projectcalico/calico-rkt/comments{/number}","issue_comment_url":"https://api.github.com/repos/projectcalico/calico-rkt/issues/comments{/number}","contents_url":"https://api.github.com/repos/projectcalico/calico-rkt/contents/{+path}","compare_url":"https://api.github.com/repos/projectcalico/calico-rkt/compare/{base}...{head}","merges_url":"https://api.github.com/repos/projectcalico/calico-rkt/merges","archive_url":"https://api.github.com/repos/projectcalico/calico-rkt/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/projectcalico/calico-rkt/downloads","issues_url":"https://api.github.com/repos/projectcalico/calico-rkt/issues{/number}","pulls_url":"https://api.github.com/repos/projectcalico/calico-rkt/pulls{/number}","milestones_url":"https://api.github.com/repos/projectcalico/calico-rkt/milestones{/number}","notifications_url":"https://api.github.com/repos/projectcalico/calico-rkt/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/projectcalico/calico-rkt/labels{/name}","releases_url":"https://api.github.com/repos/projectcalico/calico-rkt/releases{/id}","created_at":"2015-07-29T18:23:48Z","updated_at":"2015-09-23T00:45:01Z","pushed_at":"2015-12-11T01:01:35Z","git_url":"git://github.com/projectcalico/calico-rkt.git","ssh_url":"git@github.com:projectcalico/calico-rkt.git","clone_url":"https://github.com/projectcalico/calico-rkt.git","svn_url":"https://github.com/projectcalico/calico-rkt","homepage":"http://www.projectcalico.org/","size":76,"stargazers_count":12,"watchers_count":12,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":2,"forks":3,"open_issues":2,"watchers":12,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/projectcalico/calico-rkt/pulls/8"},"html":{"href":"https://github.com/projectcalico/calico-rkt/pull/8"},"issue":{"href":"https://api.github.com/repos/projectcalico/calico-rkt/issues/8"},"comments":{"href":"https://api.github.com/repos/projectcalico/calico-rkt/issues/8/comments"},"review_comments":{"href":"https://api.github.com/repos/projectcalico/calico-rkt/pulls/8/comments"},"review_comment":{"href":"https://api.github.com/repos/projectcalico/calico-rkt/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/projectcalico/calico-rkt/pulls/8/commits"},"statuses":{"href":"https://api.github.com/repos/projectcalico/calico-rkt/statuses/b34e3725315ee1bdae27c9f346859a208ce2f40a"}}}}