Skip to content

Commit

Permalink
Add cache_file parameter for DynamicClient (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
alinabuzachis authored Apr 21, 2021
1 parent a5a850d commit c214376
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
4 changes: 4 additions & 0 deletions changelogs/fragments/46-cachefile_dynamic_client.yml
Original file line number Diff line number Diff line change
@@ -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).
55 changes: 55 additions & 0 deletions plugins/module_utils/cache.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 11 additions & 2 deletions plugins/module_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -101,7 +104,6 @@


def configuration_digest(configuration):
import hashlib
m = hashlib.sha256()
for k in AUTH_ARG_MAP:
if not hasattr(configuration, k):
Expand Down Expand Up @@ -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')

Expand Down

0 comments on commit c214376

Please sign in to comment.