Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
* Support for generating the cache file to pass to the DynamicClient.
Browse files Browse the repository at this point in the history
Signed-off-by: Alina Buzachis <[email protected]>
  • Loading branch information
alinabuzachis committed Mar 31, 2021
1 parent c9157ce commit aaaaff7
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion plugins/module_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import os
import traceback
import sys
import six
import hashlib
import tempfile
from datetime import datetime
from distutils.version import LooseVersion

Expand Down Expand Up @@ -118,6 +121,40 @@ def configuration_digest(configuration):
return digest


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(configuration):
user = get_user()
if user:
cache_id = "{0}-{1}".format(configuration.host, user)
else:
cache_id = configuration.host

if six.PY3:
return cache_id.encode('utf-8')

return cache_id


def get_api_client(module=None, **kwargs):
auth = {}

Expand Down Expand Up @@ -181,8 +218,14 @@ def auth_set(*names):
client = get_api_client._pool[digest]
return client

def generate_cache_file(configuration):
return 'osrcp-{0}.json'.format(hashlib.sha1(get_default_cache_id(configuration)).hexdigest())

kubeclient = kubernetes.client.ApiClient(configuration)
cache_file = generate_cache_file(kubeclient)

try:
client = DynamicClient(kubernetes.client.ApiClient(configuration))
client = DynamicClient(kubernetes.client.ApiClient(configuration), cache_file)
except Exception as err:
_raise_or_fail(err, 'Failed to get client due to %s')

Expand Down

0 comments on commit aaaaff7

Please sign in to comment.