From 18a5ccc3ef621e85a8d02249270bad0a46e3addc Mon Sep 17 00:00:00 2001 From: jamesgetx Date: Fri, 29 Jan 2021 17:27:01 +0800 Subject: [PATCH 1/3] fix: load cache error when CacheDecoder object is not callable --- dynamic/discovery.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/dynamic/discovery.py b/dynamic/discovery.py index d2f801f2..5c2f4ac4 100644 --- a/dynamic/discovery.py +++ b/dynamic/discovery.py @@ -15,8 +15,10 @@ import os import six import json +import logging import hashlib import tempfile +from functools import partial from collections import defaultdict from abc import abstractmethod, abstractproperty @@ -54,11 +56,12 @@ def __init_cache(self, refresh=False): else: try: with open(self.__cache_file, 'r') as f: - self._cache = json.load(f, cls=CacheDecoder(self.client)) + self._cache = json.load(f, cls=partial(CacheDecoder, self.client)) if self._cache.get('library_version') != __version__: # Version mismatch, need to refresh cache self.invalidate_cache() - except Exception: + except Exception as e: + logging.error("load cache error: %s", e) self.invalidate_cache() self._load_server_info() self.discover() From ec1e85ec105bd05404bcec728a57bed0e74a8d1f Mon Sep 17 00:00:00 2001 From: jamesgetx Date: Mon, 1 Feb 2021 21:18:40 +0800 Subject: [PATCH 2/3] test: self._cache = json.load(f, cls=partial(CacheDecoder, self.client)) --- dynamic/test_discovery.py | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 dynamic/test_discovery.py diff --git a/dynamic/test_discovery.py b/dynamic/test_discovery.py new file mode 100644 index 00000000..ef3cd8e1 --- /dev/null +++ b/dynamic/test_discovery.py @@ -0,0 +1,40 @@ +# Copyright 2019 The Kubernetes Authors. +# +# 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. + +import os +import unittest + +from kubernetes.e2e_test import base +from kubernetes.client import api_client + +from . import DynamicClient + + +class TestDiscoverer(unittest.TestCase): + + @classmethod + def setUpClass(cls): + cls.config = base.get_e2e_configuration() + + def test_init_cache_from_file(self): + client = DynamicClient(api_client.ApiClient(configuration=self.config)) + client.resources.get(api_version='v1', kind='Node') + mtime1 = os.path.getmtime(client.resources._Discoverer__cache_file) + + client = DynamicClient(api_client.ApiClient(configuration=self.config)) + client.resources.get(api_version='v1', kind='Node') + mtime2 = os.path.getmtime(client.resources._Discoverer__cache_file) + + # test no Discoverer._write_cache called + self.assertTrue(mtime1 == mtime2) \ No newline at end of file From e09312a31e8bb12759421a49088f350ed2735b52 Mon Sep 17 00:00:00 2001 From: jamesgetx Date: Mon, 1 Feb 2021 21:20:19 +0800 Subject: [PATCH 3/3] test: self._cache = json.load(f, cls=partial(CacheDecoder, self.client)) --- dynamic/test_discovery.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic/test_discovery.py b/dynamic/test_discovery.py index ef3cd8e1..4897f244 100644 --- a/dynamic/test_discovery.py +++ b/dynamic/test_discovery.py @@ -37,4 +37,4 @@ def test_init_cache_from_file(self): mtime2 = os.path.getmtime(client.resources._Discoverer__cache_file) # test no Discoverer._write_cache called - self.assertTrue(mtime1 == mtime2) \ No newline at end of file + self.assertTrue(mtime1 == mtime2)