Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e2e494

Browse files
authored
Merge pull request #226 from jamesgetx/fix_cache_not_work
fix: load cache error when CacheDecoder object is not callable
2 parents 4bf72d7 + e09312a commit 6e2e494

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

dynamic/discovery.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import os
1616
import six
1717
import json
18+
import logging
1819
import hashlib
1920
import tempfile
21+
from functools import partial
2022
from collections import defaultdict
2123
from abc import abstractmethod, abstractproperty
2224

@@ -54,11 +56,12 @@ def __init_cache(self, refresh=False):
5456
else:
5557
try:
5658
with open(self.__cache_file, 'r') as f:
57-
self._cache = json.load(f, cls=CacheDecoder(self.client))
59+
self._cache = json.load(f, cls=partial(CacheDecoder, self.client))
5860
if self._cache.get('library_version') != __version__:
5961
# Version mismatch, need to refresh cache
6062
self.invalidate_cache()
61-
except Exception:
63+
except Exception as e:
64+
logging.error("load cache error: %s", e)
6265
self.invalidate_cache()
6366
self._load_server_info()
6467
self.discover()

dynamic/test_discovery.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2019 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import unittest
17+
18+
from kubernetes.e2e_test import base
19+
from kubernetes.client import api_client
20+
21+
from . import DynamicClient
22+
23+
24+
class TestDiscoverer(unittest.TestCase):
25+
26+
@classmethod
27+
def setUpClass(cls):
28+
cls.config = base.get_e2e_configuration()
29+
30+
def test_init_cache_from_file(self):
31+
client = DynamicClient(api_client.ApiClient(configuration=self.config))
32+
client.resources.get(api_version='v1', kind='Node')
33+
mtime1 = os.path.getmtime(client.resources._Discoverer__cache_file)
34+
35+
client = DynamicClient(api_client.ApiClient(configuration=self.config))
36+
client.resources.get(api_version='v1', kind='Node')
37+
mtime2 = os.path.getmtime(client.resources._Discoverer__cache_file)
38+
39+
# test no Discoverer._write_cache called
40+
self.assertTrue(mtime1 == mtime2)

0 commit comments

Comments
 (0)