Skip to content

Commit 8e4c5eb

Browse files
committed
Merge branch 'master' into m3e-issue-1005
2 parents 2c9ddf9 + fb425a3 commit 8e4c5eb

19 files changed

+910
-18
lines changed

OWNERS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# See the OWNERS docs at https://go.k8s.io/owners
22

33
approvers:
4-
- mbohlool
54
- yliaog
65
- roycaihw
6+
emeritus_approvers:
7+
- mbohlool
8+

config/incluster_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ def _load_config(self):
7070
self._environ[SERVICE_PORT_ENV_NAME]))
7171

7272
if not os.path.isfile(self._token_filename):
73-
raise ConfigException("Service token file does not exists.")
73+
raise ConfigException("Service token file does not exist.")
7474

7575
self._read_token_file()
7676

7777
if not os.path.isfile(self._cert_filename):
7878
raise ConfigException(
79-
"Service certification file does not exists.")
79+
"Service certification file does not exist.")
8080

8181
with open(self._cert_filename) as f:
8282
if not f.read():

config/incluster_config_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def test_empty_host(self):
142142

143143
def test_no_cert_file(self):
144144
loader = self.get_test_loader(cert_filename="not_exists_file_1123")
145-
self._should_fail_load(loader, "cert file does not exists")
145+
self._should_fail_load(loader, "cert file does not exist")
146146

147147
def test_empty_cert_file(self):
148148
loader = self.get_test_loader(
@@ -151,7 +151,7 @@ def test_empty_cert_file(self):
151151

152152
def test_no_token_file(self):
153153
loader = self.get_test_loader(token_filename="not_exists_file_1123")
154-
self._should_fail_load(loader, "token file does not exists")
154+
self._should_fail_load(loader, "token file does not exist")
155155

156156
def test_empty_token_file(self):
157157
loader = self.get_test_loader(

config/kube_config.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def as_file(self):
120120
else:
121121
self._file = _create_temp_file_with_content(self._data)
122122
if self._file and not os.path.isfile(self._file):
123-
raise ConfigException("File does not exists: %s" % self._file)
123+
raise ConfigException("File does not exist: %s" % self._file)
124124
return self._file
125125

126126
def as_data(self):
@@ -682,6 +682,9 @@ def _load_config_from_file_like_object(self, string):
682682
else:
683683
config = yaml.safe_load(string.read())
684684

685+
if config is None:
686+
raise ConfigException(
687+
'Invalid kube-config.')
685688
if self.config_merged is None:
686689
self.config_merged = copy.deepcopy(config)
687690
# doesn't need to do any further merging
@@ -699,6 +702,11 @@ def load_config(self, path):
699702
with open(path) as f:
700703
config = yaml.safe_load(f)
701704

705+
if config is None:
706+
raise ConfigException(
707+
'Invalid kube-config. '
708+
'%s file is empty' % path)
709+
702710
if self.config_merged is None:
703711
config_merged = copy.deepcopy(config)
704712
for item in ('clusters', 'contexts', 'users'):

config/kube_config_test.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def test_file_given_non_existing_file(self):
178178
temp_filename = NON_EXISTING_FILE
179179
obj = {TEST_FILE_KEY: temp_filename}
180180
t = FileOrData(obj=obj, file_key_name=TEST_FILE_KEY)
181-
self.expect_exception(t.as_file, "does not exists")
181+
self.expect_exception(t.as_file, "does not exist")
182182

183183
def test_file_given_data(self):
184184
obj = {TEST_DATA_KEY: TEST_DATA_BASE64}
@@ -1165,7 +1165,7 @@ def test_ssl_no_cert_files(self):
11651165
active_context="ssl-no_file")
11661166
self.expect_exception(
11671167
loader.load_and_set,
1168-
"does not exists",
1168+
"does not exist",
11691169
FakeConfig())
11701170

11711171
def test_ssl(self):
@@ -1290,6 +1290,21 @@ def test_load_kube_config_from_dict(self):
12901290
client_configuration=actual)
12911291
self.assertEqual(expected, actual)
12921292

1293+
def test_load_kube_config_from_empty_file_like_object(self):
1294+
config_file_like_object = io.StringIO()
1295+
self.assertRaises(
1296+
ConfigException,
1297+
load_kube_config,
1298+
config_file_like_object)
1299+
1300+
def test_load_kube_config_from_empty_file(self):
1301+
config_file = self._create_temp_file(
1302+
yaml.safe_dump(None))
1303+
self.assertRaises(
1304+
ConfigException,
1305+
load_kube_config,
1306+
config_file)
1307+
12931308
def test_list_kube_config_contexts(self):
12941309
config_file = self._create_temp_file(
12951310
yaml.safe_dump(self.TEST_KUBE_CONFIG))

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_client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ def test_configmap_apis(self):
331331
"apiVersion": "v1",
332332
"metadata": {
333333
"name": name,
334+
"labels": {
335+
"e2e-test": "true",
336+
},
334337
},
335338
"data": {
336339
"config.json": "{\"command\":\"/usr/bin/mysqld_safe\"}",
@@ -344,7 +347,7 @@ def test_configmap_apis(self):
344347
self.assertEqual(name, resp.metadata.name)
345348

346349
resp = api.get(
347-
name=name, namespace='default')
350+
name=name, namespace='default', label_selector="e2e-test=true")
348351
self.assertEqual(name, resp.metadata.name)
349352

350353
test_configmap['data']['config.json'] = "{}"
@@ -354,7 +357,7 @@ def test_configmap_apis(self):
354357
resp = api.delete(
355358
name=name, body={}, namespace='default')
356359

357-
resp = api.get(namespace='default', pretty=True)
360+
resp = api.get(namespace='default', pretty=True, label_selector="e2e-test=true")
358361
self.assertEqual([], resp.items)
359362

360363
def test_node_apis(self):

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)

leaderelection/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Leader Election Example
2+
This example demonstrates how to use the leader election library.
3+
4+
## Running
5+
Run the following command in multiple separate terminals preferably an odd number.
6+
Each running process uses a unique identifier displayed when it starts to run.
7+
8+
- When a program runs, if a lock object already exists with the specified name,
9+
all candidates will start as followers.
10+
- If a lock object does not exist with the specified name then whichever candidate
11+
creates a lock object first will become the leader and the rest will be followers.
12+
- The user will be prompted about the status of the candidates and transitions.
13+
14+
### Command to run
15+
```python example.py```
16+
17+
Now kill the existing leader. You will see from the terminal outputs that one of the
18+
remaining running processes will be elected as the new leader.

leaderelection/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2021 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.

0 commit comments

Comments
 (0)