Customize python import for kubernetes plugin 48/99348/4
authorMiroslav Los <miroslav.los@pantheon.tech>
Mon, 9 Dec 2019 17:20:59 +0000 (18:20 +0100)
committerMiroslav Los <miroslav.los@pantheon.tech>
Thu, 12 Dec 2019 16:46:28 +0000 (17:46 +0100)
Cloudify manager fails to find the plugin without this helper.
The original code, cloudify-python-importer, is unmaintained.
It is just one module, licensed under Apache2.0 as well.
It also needs a python3 fix, hence the module code is added here.

Note that what the modification does should not be necessary,
and a proper root cause and fix needs to be found eventually.

Signed-off-by: Miroslav Los <miroslav.los@pantheon.tech>
Issue-ID: DCAEGEN2-1988
Change-Id: I28274dff902204362d7f5b6f97ac3381ff8b5411

k8s/ChangeLog.md
k8s/k8s-node-type.yaml
k8s/k8splugin/cloudify_importer.py [new file with mode: 0644]
k8s/k8splugin/tasks.py
k8s/pom.xml
k8s/setup.py

index 3ac813e..9690e15 100644 (file)
@@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](http://keepachangelog.com/)
 and this project adheres to [Semantic Versioning](http://semver.org/).
 
+## [1.7.1]
+* DCAEGEN2-1988 Customize python import for kubernetes plugin
+
 ## [1.7.0]
 * DCAEGEN2-1956 support python3 in all plugins
 
index fdba567..4c7f0d3 100644 (file)
@@ -22,7 +22,7 @@ plugins:
   k8s:
     executor: 'central_deployment_agent'
     package_name: k8splugin
-    package_version: 1.7.0
+    package_version: 1.7.1
 
 data_types:
 
diff --git a/k8s/k8splugin/cloudify_importer.py b/k8s/k8splugin/cloudify_importer.py
new file mode 100644 (file)
index 0000000..10a6cf5
--- /dev/null
@@ -0,0 +1,120 @@
+# #######
+# Copyright (c) 2017 GigaSpaces Technologies Ltd. All rights reserved
+# Copyright (c) 2019 Pantheon.tech. All rights reserved
+#
+# 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.
+
+# Copied and updated for python3 from cloudify-python-importer
+
+from __future__ import print_function
+
+import sys
+import imp
+import os
+try:
+    import builtins
+except ImportError:
+    import __builtin__ as builtins
+
+
+class _OurImporter(object):
+
+    def __init__(self, dir_name, load_file):
+        self.dirname = dir_name
+        self.load_file = load_file
+
+    def load_module(self, package_name):
+        try:
+            return sys.modules[package_name]
+        except KeyError:
+            pass
+
+        if self.load_file:
+            try:
+                fp, pathname, description = imp.find_module(
+                    package_name.split(".")[-1],
+                    ["/".join(self.dirname.split("/")[:-1])]
+                )
+                m = imp.load_module(package_name, fp, pathname, description)
+            except ImportError as e:
+                raise e
+        else:
+            m = imp.new_module(package_name)
+
+            m.__name__ = package_name
+            m.__path__ = [self.dirname]
+            m.__doc__ = None
+
+        m.__loader__ = self
+
+        sys.modules.setdefault(package_name, m)
+        return m
+
+
+class _OurFinder(object):
+
+    def __init__(self, dir_name):
+        self.dir_name = dir_name
+
+    def find_module(self, package_name):
+        real_path = "/".join(package_name.split("."))
+
+        for path in [self.dir_name] + sys.path:
+
+            full_name = os.path.abspath(path) + "/" + real_path
+            dir_root = os.path.abspath(path) + "/" + real_path.split("/")[0]
+
+            if os.path.isfile(path + "/" + real_path + ".py"):
+                return _OurImporter(full_name, True)
+
+            if os.path.isdir(full_name):
+                if not os.path.isfile(dir_root + "/" + "__init__.py"):
+                    print('Creating __init__.py in', dir_root, file=sys.stderr)
+                    with open(dir_root + "/" + "__init__.py", 'a+') as file:
+                        file.write("# Created by importer")
+                    return _OurImporter(dir_root, False)
+
+                return _OurImporter(full_name, True)
+
+        return None
+
+
+def _check_import(dir_name):
+    return _OurFinder(dir_name)
+
+
+def register_callback():
+    sys.path_hooks.append(_check_import)
+
+    save_import = builtins.__import__
+
+    def new_import(*argv, **kwargs):
+        try:
+            module = save_import(*argv, **kwargs)
+        except ImportError as e:
+            finder = _OurFinder("")
+            if not finder:
+                raise e
+            importer = finder.find_module(argv[0])
+            if not importer:
+                raise e
+            module = importer.load_module(argv[0])
+            if not module:
+                raise e
+
+        return module
+
+    builtins.__import__ = new_import
+
+
+register_callback()
index 956fff2..2bfd3e1 100644 (file)
@@ -21,6 +21,9 @@
 
 # Lifecycle interface calls for containerized components
 
+# Needed by Cloudify Manager to load google.auth for the Kubernetes python client
+from . import cloudify_importer
+
 import time, copy
 import json
 from cloudify import ctx
index 811f8aa..ed823ac 100644 (file)
@@ -28,7 +28,7 @@ ECOMP is a trademark and service mark of AT&T Intellectual Property.
   <groupId>org.onap.dcaegen2.platform.plugins</groupId>
   <artifactId>k8s</artifactId>
   <name>k8s-plugin</name>
-  <version>1.7.0-SNAPSHOT</version>
+  <version>1.7.1-SNAPSHOT</version>
   <url>http://maven.apache.org</url>
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
index 6e8e96c..8a4cb7c 100644 (file)
@@ -24,7 +24,7 @@ from setuptools import setup
 setup(
     name='k8splugin',
     description='Cloudify plugin for containerized components deployed using Kubernetes',
-    version="1.7.0",
+    version="1.7.1",
     author='J. F. Lucas, Michael Hwang, Tommy Carpenter',
     packages=['k8splugin','k8sclient','msb','configure'],
     zip_safe=False,