From 4435e8803a6844245d2529cae840a3d55d84c296 Mon Sep 17 00:00:00 2001 From: Miroslav Los Date: Mon, 9 Dec 2019 18:20:59 +0100 Subject: Customize python import for kubernetes plugin 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 Issue-ID: DCAEGEN2-1988 Change-Id: I28274dff902204362d7f5b6f97ac3381ff8b5411 --- k8s/ChangeLog.md | 3 + k8s/k8s-node-type.yaml | 2 +- k8s/k8splugin/cloudify_importer.py | 120 +++++++++++++++++++++++++++++++++++++ k8s/k8splugin/tasks.py | 3 + k8s/pom.xml | 2 +- k8s/setup.py | 2 +- 6 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 k8s/k8splugin/cloudify_importer.py diff --git a/k8s/ChangeLog.md b/k8s/ChangeLog.md index 3ac813e..9690e15 100644 --- a/k8s/ChangeLog.md +++ b/k8s/ChangeLog.md @@ -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 diff --git a/k8s/k8s-node-type.yaml b/k8s/k8s-node-type.yaml index fdba567..4c7f0d3 100644 --- a/k8s/k8s-node-type.yaml +++ b/k8s/k8s-node-type.yaml @@ -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 index 0000000..10a6cf5 --- /dev/null +++ b/k8s/k8splugin/cloudify_importer.py @@ -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() diff --git a/k8s/k8splugin/tasks.py b/k8s/k8splugin/tasks.py index 956fff2..2bfd3e1 100644 --- a/k8s/k8splugin/tasks.py +++ b/k8s/k8splugin/tasks.py @@ -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 diff --git a/k8s/pom.xml b/k8s/pom.xml index 811f8aa..ed823ac 100644 --- a/k8s/pom.xml +++ b/k8s/pom.xml @@ -28,7 +28,7 @@ ECOMP is a trademark and service mark of AT&T Intellectual Property. org.onap.dcaegen2.platform.plugins k8s k8s-plugin - 1.7.0-SNAPSHOT + 1.7.1-SNAPSHOT http://maven.apache.org UTF-8 diff --git a/k8s/setup.py b/k8s/setup.py index 6e8e96c..8a4cb7c 100644 --- a/k8s/setup.py +++ b/k8s/setup.py @@ -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, -- cgit 1.2.3-korg