diff --git a/changelogs/fragments/46-cachefile_dynamic_client.yml b/changelogs/fragments/46-cachefile_dynamic_client.yml new file mode 100644 index 0000000000..c32af4f544 --- /dev/null +++ b/changelogs/fragments/46-cachefile_dynamic_client.yml @@ -0,0 +1,4 @@ +--- +minor_changes: +- Add logic for cache file name generation (https://github.com/ansible-collections/kubernetes.core/pull/46). +- Add cache_file when DynamicClient is created (https://github.com/ansible-collections/kubernetes.core/pull/46). diff --git a/plugins/module_utils/cache.py b/plugins/module_utils/cache.py new file mode 100644 index 0000000000..963c3d6072 --- /dev/null +++ b/plugins/module_utils/cache.py @@ -0,0 +1,55 @@ +# Copyright [2017] [Red Hat, Inc.] +# +# 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 __future__ import absolute_import, division, print_function +__metaclass__ = type + +import os + +from ansible.module_utils.six import PY3 + + +def get_user(): + if hasattr(os, 'getlogin'): + try: + user = os.getlogin() + if user: + return str(user) + except OSError: + pass + if hasattr(os, 'getuid'): + try: + user = os.getuid() + if user: + return str(user) + except OSError: + pass + user = os.environ.get("USERNAME") + if user: + return str(user) + return None + + +def get_default_cache_id(client): + user = get_user() + if user: + cache_id = "{0}-{1}".format(client.configuration.host, user) + else: + cache_id = client.configuration.host + + if PY3: + return cache_id.encode('utf-8') + + return cache_id diff --git a/plugins/module_utils/common.py b/plugins/module_utils/common.py index 7e7d96cc5f..3f503ec59a 100644 --- a/plugins/module_utils/common.py +++ b/plugins/module_utils/common.py @@ -23,11 +23,14 @@ import os import traceback import sys +import tempfile +import hashlib from datetime import datetime from distutils.version import LooseVersion from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (AUTH_ARG_MAP, AUTH_ARG_SPEC) from ansible_collections.kubernetes.core.plugins.module_utils.hashes import generate_hash +from ansible_collections.kubernetes.core.plugins.module_utils.cache import get_default_cache_id from ansible.module_utils.basic import missing_required_lib from ansible.module_utils.six import iteritems, string_types @@ -101,7 +104,6 @@ def configuration_digest(configuration): - import hashlib m = hashlib.sha256() for k in AUTH_ARG_MAP: if not hasattr(configuration, k): @@ -184,8 +186,15 @@ def auth_set(*names): client = get_api_client._pool[digest] return client + def generate_cache_file(kubeclient): + cache_file_name = 'k8srcp-{0}.json'.format(hashlib.sha256(get_default_cache_id(kubeclient)).hexdigest()) + return os.path.join(tempfile.gettempdir(), cache_file_name) + + kubeclient = kubernetes.client.ApiClient(configuration) + cache_file = generate_cache_file(kubeclient) + try: - client = DynamicClient(kubernetes.client.ApiClient(configuration)) + client = DynamicClient(kubeclient, cache_file) except Exception as err: _raise_or_fail(err, 'Failed to get client due to %s')