[SEBA-675] Checking that the Kubernetes cluster on which this service is deployed is compatible with the client library installed

Change-Id: I68d91ab4da87f7650595f590f689942b42070723
diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..0d57996
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,7 @@
+.idea
+.tox
+.coverage
+coverage.xml
+nose2-results.xml
+venv-service
+*.pyc
diff --git a/Makefile b/Makefile
index 71ed1b8..98f9de9 100644
--- a/Makefile
+++ b/Makefile
@@ -60,7 +60,7 @@
 venv-service:
 	virtualenv $@;\
     source ./$@/bin/activate ; set -u ;\
-    pip install -r requirements.txt xosmigrate~=3.0.1
+    pip install -r requirements.txt xosmigrate~=3.2.6
 
 create-migration: venv-service
 	source ./venv-service/bin/activate; set -u;\
diff --git a/VERSION b/VERSION
index 6085e94..b6bb93f 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.2.1
+1.3.0-dev
diff --git a/requirements.txt b/requirements.txt
index bcc4ae3..b57131d 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,4 +1,4 @@
-kubernetes~=8.0.1
+kubernetes~=9.0.0
 xossynchronizer~=3.0.1
 xosapi~=3.0.1
 xoskafka~=3.0.1
diff --git a/xos/synchronizer/steps/sync_kubernetes_service.py b/xos/synchronizer/steps/sync_kubernetes_service.py
new file mode 100644
index 0000000..5138c6c
--- /dev/null
+++ b/xos/synchronizer/steps/sync_kubernetes_service.py
@@ -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.
+
+
+from xossynchronizer.steps.syncstep import SyncStep
+from xossynchronizer.modelaccessor import KubernetesService
+
+from xosconfig import Config
+from multistructlog import create_logger
+
+log = create_logger(Config().get('logging'))
+
+class SyncK8Service(SyncStep):
+    provides = [KubernetesService]
+    observes = KubernetesService
+
+    max_version = "1.14"
+    [expected_major, expected_minor] = max_version.split(".")
+
+    def __init__(self, *args, **kwargs):
+        super(SyncK8Service, self).__init__(*args, **kwargs)
+        self.init_kubernetes_client()
+
+    def init_kubernetes_client(self):
+        from kubernetes.client.rest import ApiException
+        from kubernetes import client as kubernetes_client, config as kubernetes_config
+        kubernetes_config.load_incluster_config()
+        self.api_instance = kubernetes_client.VersionApi(kubernetes_client.ApiClient())
+        self.ApiException = ApiException
+
+    def sync_record(self, o):
+        log.info("[K8Service SyncStep] Sync'ing model", model=o, name=o.name)
+
+        res = self.api_instance.get_code()
+        major = res.major
+        minor = res.minor
+        log.info("[K8Service SyncStep] API Code", major=major, minor=minor,
+                 expected_major=self.expected_major, expected_minor=self.expected_minor)
+
+        if major != int(self.expected_major) or minor > int(self.expected_minor):
+            raise Exception("Kubernetes cluster of version %s is not supported by the kubernetes-services" % res.git_version+
+                            "the maximum supported version is %s" % self.max_version)
+
+    def delete_record(self, o):
+        pass
\ No newline at end of file
diff --git a/xos/synchronizer/tests/test_sync_kubernetesservice.py b/xos/synchronizer/tests/test_sync_kubernetesservice.py
new file mode 100644
index 0000000..75a79cc
--- /dev/null
+++ b/xos/synchronizer/tests/test_sync_kubernetesservice.py
@@ -0,0 +1,83 @@
+
+# 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
+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.api_instance = MagicMock()
+    self.ApiException = ApiException
+
+class TestSyncKubernetesServiceInstance(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.xproto")] )
+
+        self.MockObjectList = self.unittest_setup["MockObjectList"]
+        self.model_accessor = self.unittest_setup["model_accessor"]
+
+        sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../steps"))
+
+        from sync_kubernetes_service import SyncK8Service
+        self.step_class = SyncK8Service
+
+        self.service = KubernetesService(name="TestK8")
+
+    def tearDown(self):
+        sys.path = self.unittest_setup["sys_path_save"]
+
+    def test_valid_version(self):
+
+        version = MagicMock()
+        version.major = 1
+        version.minor = 13
+        version.git_version = "v1.13.0"
+
+        with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
+            step = self.step_class(model_accessor=self.model_accessor)
+
+            step.api_instance.get_code.return_value = version
+
+            step.sync_record(self.service)
+
+    def test_invalid_version(self):
+
+        version = MagicMock()
+        version.major = 1
+        version.minor = 15
+        version.git_version = "v1.15.0"
+
+        with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
+            step = self.step_class(model_accessor=self.model_accessor)
+
+            step.api_instance.get_code.return_value = version
+
+            with self.assertRaises(Exception):
+                step.sync_record(self.service)
+
+
+if __name__ == '__main__':
+    unittest.main()