Skip to content

Commit 7712421

Browse files
authored
Merge pull request kubernetes-client#2187 from tomplus/fix/mergin-current-context
Fix merging current-context in kube-configs
2 parents a43cda5 + 69cfcda commit 7712421

File tree

2 files changed

+66
-26
lines changed

2 files changed

+66
-26
lines changed

kubernetes/base/config/kube_config.py

+4
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,10 @@ def load_config(self, path):
727727
self.config_merged = ConfigNode(path, config_merged, path)
728728
for item in ('clusters', 'contexts', 'users'):
729729
self._merge(item, config.get(item, []) or [], path)
730+
731+
if 'current-context' in config:
732+
self.config_merged.value['current-context'] = config['current-context']
733+
730734
self.config_files[path] = config
731735

732736
def _merge(self, item, add_cfg, path):

kubernetes/base/config/kube_config_test.py

+62-26
Original file line numberDiff line numberDiff line change
@@ -1653,7 +1653,7 @@ def refresh_api_key_hook(client_config):
16531653

16541654

16551655
class TestKubeConfigMerger(BaseTestCase):
1656-
TEST_KUBE_CONFIG_PART1 = {
1656+
TEST_KUBE_CONFIG_SET1 = [{
16571657
"current-context": "no_user",
16581658
"contexts": [
16591659
{
@@ -1672,9 +1672,7 @@ class TestKubeConfigMerger(BaseTestCase):
16721672
},
16731673
],
16741674
"users": []
1675-
}
1676-
1677-
TEST_KUBE_CONFIG_PART2 = {
1675+
}, {
16781676
"current-context": "",
16791677
"contexts": [
16801678
{
@@ -1712,9 +1710,7 @@ class TestKubeConfigMerger(BaseTestCase):
17121710
}
17131711
},
17141712
]
1715-
}
1716-
1717-
TEST_KUBE_CONFIG_PART3 = {
1713+
}, {
17181714
"current-context": "no_user",
17191715
"contexts": [
17201716
{
@@ -1761,12 +1757,10 @@ class TestKubeConfigMerger(BaseTestCase):
17611757
}
17621758
},
17631759
]
1764-
}
1765-
TEST_KUBE_CONFIG_PART4 = {
1760+
}, {
17661761
"current-context": "no_user",
1767-
}
1768-
# Config with user having cmd-path
1769-
TEST_KUBE_CONFIG_PART5 = {
1762+
}, {
1763+
# Config with user having cmd-path
17701764
"contexts": [
17711765
{
17721766
"name": "contexttestcmdpath",
@@ -1795,8 +1789,7 @@ class TestKubeConfigMerger(BaseTestCase):
17951789
}
17961790
}
17971791
]
1798-
}
1799-
TEST_KUBE_CONFIG_PART6 = {
1792+
}, {
18001793
"current-context": "no_user",
18011794
"contexts": [
18021795
{
@@ -1815,22 +1808,49 @@ class TestKubeConfigMerger(BaseTestCase):
18151808
},
18161809
],
18171810
"users": None
1818-
}
1811+
}]
1812+
# 3 parts with different keys/data to merge
1813+
TEST_KUBE_CONFIG_SET2 = [{
1814+
"clusters": [
1815+
{
1816+
"name": "default",
1817+
"cluster": {
1818+
"server": TEST_HOST
1819+
}
1820+
},
1821+
],
1822+
}, {
1823+
"current-context": "simple_token",
1824+
"contexts": [
1825+
{
1826+
"name": "simple_token",
1827+
"context": {
1828+
"cluster": "default",
1829+
"user": "simple_token"
1830+
}
1831+
},
1832+
],
1833+
}, {
1834+
"users": [
1835+
{
1836+
"name": "simple_token",
1837+
"user": {
1838+
"token": TEST_DATA_BASE64,
1839+
"username": TEST_USERNAME,
1840+
"password": TEST_PASSWORD,
1841+
}
1842+
},
1843+
]
1844+
}]
18191845

1820-
def _create_multi_config(self):
1846+
def _create_multi_config(self, parts):
18211847
files = []
1822-
for part in (
1823-
self.TEST_KUBE_CONFIG_PART1,
1824-
self.TEST_KUBE_CONFIG_PART2,
1825-
self.TEST_KUBE_CONFIG_PART3,
1826-
self.TEST_KUBE_CONFIG_PART4,
1827-
self.TEST_KUBE_CONFIG_PART5,
1828-
self.TEST_KUBE_CONFIG_PART6):
1848+
for part in parts:
18291849
files.append(self._create_temp_file(yaml.safe_dump(part)))
18301850
return ENV_KUBECONFIG_PATH_SEPARATOR.join(files)
18311851

18321852
def test_list_kube_config_contexts(self):
1833-
kubeconfigs = self._create_multi_config()
1853+
kubeconfigs = self._create_multi_config(self.TEST_KUBE_CONFIG_SET1)
18341854
expected_contexts = [
18351855
{'context': {'cluster': 'default'}, 'name': 'no_user'},
18361856
{'context': {'cluster': 'ssl', 'user': 'ssl'}, 'name': 'ssl'},
@@ -1849,15 +1869,31 @@ def test_list_kube_config_contexts(self):
18491869
self.assertEqual(active_context, expected_contexts[0])
18501870

18511871
def test_new_client_from_config(self):
1852-
kubeconfigs = self._create_multi_config()
1872+
kubeconfigs = self._create_multi_config(self.TEST_KUBE_CONFIG_SET1)
18531873
client = new_client_from_config(
18541874
config_file=kubeconfigs, context="simple_token")
18551875
self.assertEqual(TEST_HOST, client.configuration.host)
18561876
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
18571877
client.configuration.api_key['authorization'])
18581878

1879+
def test_merge_with_context_in_different_file(self):
1880+
kubeconfigs = self._create_multi_config(self.TEST_KUBE_CONFIG_SET2)
1881+
client = new_client_from_config(config_file=kubeconfigs)
1882+
1883+
expected_contexts = [
1884+
{'context': {'cluster': 'default', 'user': 'simple_token'},
1885+
'name': 'simple_token'}
1886+
]
1887+
contexts, active_context = list_kube_config_contexts(
1888+
config_file=kubeconfigs)
1889+
self.assertEqual(contexts, expected_contexts)
1890+
self.assertEqual(active_context, expected_contexts[0])
1891+
self.assertEqual(TEST_HOST, client.configuration.host)
1892+
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
1893+
client.configuration.api_key['authorization'])
1894+
18591895
def test_save_changes(self):
1860-
kubeconfigs = self._create_multi_config()
1896+
kubeconfigs = self._create_multi_config(self.TEST_KUBE_CONFIG_SET1)
18611897

18621898
# load configuration, update token, save config
18631899
kconf = KubeConfigMerger(kubeconfigs)

0 commit comments

Comments
 (0)