Skip to content

Commit 6ced06e

Browse files
committed
Update eks-kubeconfig for better null checks
1 parent 655af71 commit 6ced06e

File tree

5 files changed

+133
-3
lines changed

5 files changed

+133
-3
lines changed

awscli/customizations/eks/kubeconfig.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ def has_cluster(self, name):
6262
Return true if this kubeconfig contains an entry
6363
For the passed cluster name.
6464
"""
65-
if 'clusters' not in self.content:
65+
if 'clusters' not in self.content or \
66+
self.content['clusters'] is None:
6667
return False
6768
return name in [cluster['name']
68-
for cluster in self.content['clusters']]
69+
for cluster in self.content['clusters'] if 'name' in cluster]
6970

7071

7172
class KubeconfigValidator(object):
@@ -211,7 +212,8 @@ def insert_entry(self, config, key, entry):
211212
:param config: The kubeconfig to insert an entry into
212213
:type config: Kubeconfig
213214
"""
214-
if key not in config.content:
215+
if key not in config.content or \
216+
config.content[key] is None:
215217
config.content[key] = []
216218
array = config.content[key]
217219
if not isinstance(array, list):
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: v1
2+
clusters: null
3+
contexts:
4+
- context:
5+
cluster: arn:aws:eks:us-west-2:111222333444:cluster/Existing
6+
user: arn:aws:eks:us-west-2:111222333444:cluster/Existing
7+
name: arn:aws:eks:us-west-2:111222333444:cluster/Existing
8+
current-context: arn:aws:eks:us-west-2:111222333444:cluster/Existing
9+
kind: Config
10+
preferences: {}
11+
users:
12+
- name: arn:aws:eks:us-west-2:111222333444:cluster/Existing
13+
user:
14+
exec:
15+
apiVersion: client.authentication.k8s.io/v1alpha1
16+
args:
17+
- --region
18+
- us-west-2
19+
- eks
20+
- get-token
21+
- --cluster-name
22+
- Existing
23+
command: aws
24+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: v1
2+
contexts: null
3+
clusters:
4+
- cluster:
5+
certificate-authority-data: DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA=
6+
server: https://existingEndpoint.eks.amazonaws.com
7+
name: arn:aws:eks:us-west-2:111222333444:cluster/Existing
8+
current-context: arn:aws:eks:us-west-2:111222333444:cluster/Existing
9+
kind: Config
10+
preferences: {}
11+
users:
12+
- name: arn:aws:eks:us-west-2:111222333444:cluster/Existing
13+
user:
14+
exec:
15+
apiVersion: client.authentication.k8s.io/v1alpha1
16+
args:
17+
- --region
18+
- us-west-2
19+
- eks
20+
- get-token
21+
- --cluster-name
22+
- Existing
23+
command: aws
24+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: v1
2+
clusters:
3+
- cluster:
4+
certificate-authority-data: DATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATADATA=
5+
server: https://existingEndpoint.eks.amazonaws.com
6+
name: arn:aws:eks:us-west-2:111222333444:cluster/Existing
7+
contexts:
8+
- context:
9+
cluster: arn:aws:eks:us-west-2:111222333444:cluster/Existing
10+
user: arn:aws:eks:us-west-2:111222333444:cluster/Existing
11+
name: arn:aws:eks:us-west-2:111222333444:cluster/Existing
12+
current-context: arn:aws:eks:us-west-2:111222333444:cluster/Existing
13+
users: null
14+
kind: Config
15+
preferences: {}
16+

tests/unit/customizations/eks/test_kubeconfig.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,31 @@ def test_has_cluster_with_no_clusters(self):
6565
config = Kubeconfig(self._path, self._content)
6666
self.assertFalse(config.has_cluster("clustername"))
6767

68+
def test_has_cluster_with_none_clusters(self):
69+
self._content["clusters"] = None
70+
71+
config = Kubeconfig(self._path, self._content)
72+
self.assertFalse(config.has_cluster("anyclustername"))
73+
74+
def test_has_cluster_with_cluster_no_name(self):
75+
self._content["clusters"] = [
76+
OrderedDict([
77+
("cluster", None),
78+
("name", "clustername")
79+
]),
80+
OrderedDict([
81+
("cluster", None),
82+
("name", None),
83+
]),
84+
OrderedDict([
85+
("cluster", None),
86+
]),
87+
]
88+
89+
config = Kubeconfig(self._path, self._content)
90+
self.assertTrue(config.has_cluster("clustername"))
91+
self.assertFalse(config.has_cluster("othercluster"))
92+
6893

6994
class TestKubeconfigWriter(unittest.TestCase):
7095

@@ -257,6 +282,45 @@ def test_key_not_exist(self):
257282
cluster)
258283
self.assertDictEqual(updated.content, correct)
259284

285+
def test_key_none(self):
286+
initial = OrderedDict([
287+
("apiVersion", "v1"),
288+
("clusters", None),
289+
("contexts", []),
290+
("current-context", None),
291+
("kind", "Config"),
292+
("preferences", OrderedDict()),
293+
("users", [])
294+
])
295+
cluster = OrderedDict([
296+
("cluster", OrderedDict([
297+
("certificate-authority-data", "data"),
298+
("server", "endpoint")
299+
])),
300+
("name", "clustername")
301+
])
302+
correct = OrderedDict([
303+
("apiVersion", "v1"),
304+
("clusters", [
305+
OrderedDict([
306+
("cluster", OrderedDict([
307+
("certificate-authority-data", "data"),
308+
("server", "endpoint")
309+
])),
310+
("name", "clustername")
311+
])
312+
]),
313+
("contexts", []),
314+
("current-context", None),
315+
("kind", "Config"),
316+
("preferences", OrderedDict()),
317+
("users", []),
318+
])
319+
updated = self._appender.insert_entry(Kubeconfig(None, initial),
320+
"clusters",
321+
cluster)
322+
self.assertDictEqual(updated.content, correct)
323+
260324
def test_key_not_array(self):
261325
initial = OrderedDict([
262326
("apiVersion", "v1"),

0 commit comments

Comments
 (0)