Skip to content

fix: prioritize current-context based on file order #2386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kubernetes/base/config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ def load_config(self, path):
for item in ('clusters', 'contexts', 'users'):
self._merge(item, config.get(item, []) or [], path)

if 'current-context' in config:
if 'current-context' in config and ('current-context' not in self.config_merged or self.config_merged.value['current-context'] == ""):
self.config_merged.value['current-context'] = config['current-context']

self.config_files[path] = config
Expand Down
45 changes: 45 additions & 0 deletions kubernetes/base/config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,38 @@ class TestKubeConfigMerger(BaseTestCase):
},
]
}]
TEST_KUBE_CONFIG_SET3 = [{
"current-context": "",
"contexts": [
{
"name": "context1",
"context": {
"cluster": "cluster1"
}
},
{
"name": "context2",
"context": {
"cluster": "cluster2"
}
},
],
"clusters": [
{
"name": "cluster1",
"cluster": {
"server": TEST_HOST
}
},
{
"name": "cluster2",
"cluster": {
"server": TEST_HOST
}
},
],
"users": []
}]

def _create_multi_config(self, parts):
files = []
Expand Down Expand Up @@ -1892,6 +1924,19 @@ def test_merge_with_context_in_different_file(self):
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
client.configuration.api_key['authorization'])

def test_current_context_in_different_file(self):
cases = [
([{"current-context": "context1"}] + self.TEST_KUBE_CONFIG_SET3, 'context1'),
(self.TEST_KUBE_CONFIG_SET3 + [{"current-context": "context2"}], 'context2'),
([{}, {"current-context": "context2"}] + self.TEST_KUBE_CONFIG_SET3, 'context2'),
([{"current-context": "context1"}, {"current-context": "context2"}] + self.TEST_KUBE_CONFIG_SET3, 'context1'),
]

for kube_config_set, expected_current_context_name in cases:
kubeconfigs = self._create_multi_config(kube_config_set)
_, current_context = list_kube_config_contexts(config_file=kubeconfigs)
self.assertEqual(current_context["name"], expected_current_context_name)

def test_save_changes(self):
kubeconfigs = self._create_multi_config(self.TEST_KUBE_CONFIG_SET1)

Expand Down