Add cache_file parameter for DynamicClient (#46)

This commit is contained in:
Alina Buzachis
2021-04-21 06:27:52 +02:00
committed by GitHub
parent a5a850d1da
commit c214376cac
3 changed files with 70 additions and 2 deletions

View File

@@ -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).

View File

@@ -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

View File

@@ -23,11 +23,14 @@ import time
import os import os
import traceback import traceback
import sys import sys
import tempfile
import hashlib
from datetime import datetime from datetime import datetime
from distutils.version import LooseVersion 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.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.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.basic import missing_required_lib
from ansible.module_utils.six import iteritems, string_types from ansible.module_utils.six import iteritems, string_types
@@ -101,7 +104,6 @@ except ImportError as e:
def configuration_digest(configuration): def configuration_digest(configuration):
import hashlib
m = hashlib.sha256() m = hashlib.sha256()
for k in AUTH_ARG_MAP: for k in AUTH_ARG_MAP:
if not hasattr(configuration, k): if not hasattr(configuration, k):
@@ -184,8 +186,15 @@ def get_api_client(module=None, **kwargs):
client = get_api_client._pool[digest] client = get_api_client._pool[digest]
return client 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: try:
client = DynamicClient(kubernetes.client.ApiClient(configuration)) client = DynamicClient(kubeclient, cache_file)
except Exception as err: except Exception as err:
_raise_or_fail(err, 'Failed to get client due to %s') _raise_or_fail(err, 'Failed to get client due to %s')