CORD-3224 Implement KubernetesResourceInstance

Change-Id: Ic3c7691aa16a20c0d7d69994fef1ad8ac6d1ca44
diff --git a/xos/examples/make_resource.xossh b/xos/examples/make_resource.xossh
new file mode 100644
index 0000000..1a507ce
--- /dev/null
+++ b/xos/examples/make_resource.xossh
@@ -0,0 +1,19 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This file is intended to be pasted into an xossh session
+
+rd='apiVersion: v1\nkind: Pod\nmetadata:\n  name: nginx\nspec:\n  containers:\n  - name: nginx\n    image: nginx:1.7.9\n    ports:\n    - containerPort: 80\n'
+k8r = KubernetesResourceInstance(owner=KubernetesService.objects.first(), name="nginx", resource_definition=rd)
+k8r.save()
diff --git a/xos/examples/make_resource.yaml b/xos/examples/make_resource.yaml
new file mode 100644
index 0000000..236deed
--- /dev/null
+++ b/xos/examples/make_resource.yaml
@@ -0,0 +1,56 @@
+---
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+tosca_definitions_version: tosca_simple_yaml_1_0
+
+description: Make a pod using Kubernetes Synchronizer
+
+imports:
+  - custom_types/trustdomain.yaml
+  - custom_types/principal.yaml
+  - custom_types/image.yaml
+  - custom_types/site.yaml
+  - custom_types/slice.yaml
+  - custom_types/kubernetesservice.yaml
+  - custom_types/kubernetesresourceinstance.yaml
+
+topology_template:
+  node_templates:
+    service#kubernetes:
+          type: tosca.nodes.KubernetesService
+          properties:
+            name: kubernetes
+            must-exist: true
+
+    demo_resource:
+      type: tosca.nodes.KubernetesResourceInstance
+      properties:
+        name: "demo-pod"
+        resource_definition: |
+          apiVersion: v1
+          kind: Pod
+          metadata:
+            name: nginx
+          spec:
+            containers:
+            - name: nginx
+              image: nginx:1.7.9
+              ports:
+              - containerPort: 80
+      requirements:
+        - owner:
+            node: service#kubernetes
+            relationship: tosca.relationships.BelongsToOne
+
diff --git a/xos/synchronizer/models/kubernetes.xproto b/xos/synchronizer/models/kubernetes.xproto
index 405f1c2..06aae56 100644
--- a/xos/synchronizer/models/kubernetes.xproto
+++ b/xos/synchronizer/models/kubernetes.xproto
@@ -6,6 +6,14 @@
 
 }
 
+message KubernetesResourceInstance (XOSBase){
+     option verbose_name = "Kubernetes Resource Instance";
+     required string name = 1 [db_index = True, max_length = 200, content_type = "stripped", tosca_key=True, unique=True, help_text = "Name of ResourceInstance"];
+     required manytoone owner->Service:kubernetes_resource_instances = 2 [db_index = True, null = False, blank = False];
+     optional string resource_definition = 3 [help_text = "yaml blob"];
+     optional string kubectl_state = 4 [max_length=32, db_index = False, choices = "(('created', 'CREATED'), ('updated', 'UPDATED'), ('deleted', 'DELETED'))", help_text = "Most recent state of kubectl"];
+}
+
 message KubernetesServiceInstance (ComputeServiceInstance){
      option verbose_name = "Kubernetes Service Instance";
      optional string pod_ip = 1 [max_length=32, db_index = False, null = True, blank = True, help_text = "IP address of pod"];
diff --git a/xos/synchronizer/steps/sync_kubernetesresourceinstance.py b/xos/synchronizer/steps/sync_kubernetesresourceinstance.py
new file mode 100644
index 0000000..60174bb
--- /dev/null
+++ b/xos/synchronizer/steps/sync_kubernetesresourceinstance.py
@@ -0,0 +1,80 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""
+    sync_kubernetesresourceinstance.py
+
+    Synchronize KubernetesResourceInstance.
+
+    This sync_step is instantiates generic resources by executing them with `kubectl`.
+"""
+
+import os
+import subprocess
+import tempfile
+from synchronizers.new_base.syncstep import SyncStep
+from synchronizers.new_base.modelaccessor import KubernetesResourceInstance
+
+from xosconfig import Config
+from multistructlog import create_logger
+
+log = create_logger(Config().get('logging'))
+
+class SyncKubernetesResourceInstance(SyncStep):
+
+    """
+        SyncKubernetesResourceInstance
+
+        Implements sync step for syncing kubernetes resource instances. These objects are basically a yaml blob that
+        is passed to `kubectl`.
+    """
+
+    provides = [KubernetesResourceInstance]
+    observes = KubernetesResourceInstance
+    requested_interval = 0
+
+    def __init__(self, *args, **kwargs):
+        super(SyncKubernetesResourceInstance, self).__init__(*args, **kwargs)
+
+    def run_kubectl(self, operation, recipe):
+        (tmpfile, fn)=tempfile.mkstemp()
+        os.write(tmpfile, recipe)
+        os.close(tmpfile)
+        try:
+            p = subprocess.Popen(args=["/usr/local/bin/kubectl", operation, "-f", fn],
+                                 stdin=None,
+                                 stderr=subprocess.PIPE,
+                                 stdout=subprocess.PIPE,
+                                 close_fds=True)
+            (stdout, stderr) = p.communicate()
+            log.info("kubectl completed", stderr=stderr, stdout=stdout, recipe=recipe)
+            if p.returncode!=0:
+                raise Exception("Process failed with returncode %s" % p.returncode)
+        finally:
+            os.remove(fn)
+
+    def sync_record(self, o):
+        self.run_kubectl("apply", o.resource_definition)
+        if (o.kubectl_state == "created"):
+            o.kubectl_state = "updated"
+        else:
+            o.kubectl_state = "created"
+        o.save(update_fields=["kubectl_state"])
+
+    def delete_record(self, o):
+        if o.kubectl_state in ["created", "updated"]:
+            self.run_kubectl("delete", o.resource_definition)
+            o.kubectl_state="deleted"
+            o.save(update_fields=["kubectl_state"])
diff --git a/xos/synchronizer/tests/test_sync_kubernetesresourceinstance.py b/xos/synchronizer/tests/test_sync_kubernetesresourceinstance.py
new file mode 100644
index 0000000..82d957e
--- /dev/null
+++ b/xos/synchronizer/tests/test_sync_kubernetesresourceinstance.py
@@ -0,0 +1,132 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import json
+import os
+import sys
+import unittest
+from mock import patch, PropertyMock, ANY, MagicMock, Mock
+from unit_test_common import setup_sync_unit_test
+
+class ApiException(Exception):
+    def __init__(self, status, *args, **kwargs):
+        super(ApiException, self).__init__(*args, **kwargs)
+        self.status = status
+
+def fake_init_kubernetes_client(self):
+    self.kubernetes_client = MagicMock()
+    self.v1core = MagicMock()
+    self.ApiException = ApiException
+
+class TestSyncKubernetesResourceInstance(unittest.TestCase):
+
+    def setUp(self):
+        self.unittest_setup = setup_sync_unit_test(os.path.abspath(os.path.dirname(os.path.realpath(__file__))),
+                                                   globals(),
+                                                   [("kubernetes-service", "kubernetes.proto")] )
+
+        self.MockObjectList = self.unittest_setup["MockObjectList"]
+
+        sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../steps"))
+
+        from sync_kubernetesresourceinstance import SyncKubernetesResourceInstance
+        self.step_class = SyncKubernetesResourceInstance
+
+        self.service = KubernetesService()
+
+    def tearDown(self):
+        sys.path = self.unittest_setup["sys_path_save"]
+
+    @patch('subprocess.Popen')
+    def test_run_kubectl(self, mock_popen):
+        proc = Mock()
+        proc.communicate.return_value = ('output', 'error')
+        proc.returncode = 0
+
+        mock_popen.return_value = proc
+
+        step = self.step_class()
+        step.run_kubectl("create", "foo")
+
+        mock_popen.assert_called()
+
+    @patch('subprocess.Popen')
+    def test_run_kubectl_fail(self, mock_popen):
+        proc = Mock()
+        proc.communicate.return_value = ('output', 'error')
+        proc.returncode = 1
+
+        mock_popen.return_value = proc
+
+        step = self.step_class()
+        with self.assertRaises(Exception) as e:
+          step.run_kubectl("create", "foo")
+
+        self.assertEqual(e.exception.message, "Process failed with returncode 1")
+
+    def test_sync_record_create(self):
+        with patch.object(self.step_class, "run_kubectl") as run_kubectl:
+            xos_ri = KubernetesResourceInstance(name="test-instance", owner=self.service, resource_definition="foo")
+
+            run_kubectl.return_value = None
+
+            step = self.step_class()
+            step.sync_record(xos_ri)
+
+            run_kubectl.assert_called_with("apply", "foo")
+
+            self.assertEqual(xos_ri.kubectl_state, "created")
+
+    def test_sync_record_update(self):
+        with patch.object(self.step_class, "run_kubectl") as run_kubectl:
+            xos_ri = KubernetesResourceInstance(name="test-instance", owner=self.service, resource_definition="foo", kubectl_state="created")
+
+            run_kubectl.return_value = None
+
+            step = self.step_class()
+            step.sync_record(xos_ri)
+
+            run_kubectl.assert_called_with("apply", "foo")
+
+            self.assertEqual(xos_ri.kubectl_state, "updated")
+
+    def test_sync_record_delete(self):
+        with patch.object(self.step_class, "run_kubectl") as run_kubectl:
+            xos_ri = KubernetesResourceInstance(name="test-instance", owner=self.service, resource_definition="foo", kubectl_state="created")
+
+            run_kubectl.return_value = None
+
+            step = self.step_class()
+            step.delete_record(xos_ri)
+
+            run_kubectl.assert_called_with("delete", "foo")
+
+            self.assertEqual(xos_ri.kubectl_state, "deleted")
+
+    def test_sync_record_delete_never_created(self):
+        """ If the object was never saved, then we shouldn't try to delete it """
+        with patch.object(self.step_class, "run_kubectl") as run_kubectl:
+            xos_ri = KubernetesResourceInstance(name="test-instance", owner=self.service, resource_definition="foo")
+
+            run_kubectl.return_value = None
+
+            step = self.step_class()
+            step.delete_record(xos_ri)
+
+            run_kubectl.assert_not_called()
+
+
+if __name__ == '__main__':
+    unittest.main()