1
1
import os
2
+ from typing import Union
2
3
3
4
from colorama import Fore , Style
4
5
from kubernetes .client .exceptions import ApiException
@@ -74,14 +75,14 @@ def __init__(
74
75
fixer_config : dict = {},
75
76
mutelist_path : str = None ,
76
77
mutelist_content : dict = {},
77
- kubeconfig_content : dict = None ,
78
+ kubeconfig_content : Union [ dict , str ] = None ,
78
79
):
79
80
"""
80
81
Initializes the KubernetesProvider instance.
81
82
82
83
Args:
83
84
kubeconfig_file (str): Path to the kubeconfig file.
84
- kubeconfig_content (dict): Content of the kubeconfig file.
85
+ kubeconfig_content (str or dict): Content of the kubeconfig file.
85
86
context (str): Context name.
86
87
namespace (list): List of namespaces.
87
88
config_content (dict): Audit configuration.
@@ -224,15 +225,15 @@ def mutelist(self) -> KubernetesMutelist:
224
225
@staticmethod
225
226
def setup_session (
226
227
kubeconfig_file : str = None ,
227
- kubeconfig_content : dict = None ,
228
+ kubeconfig_content : Union [ dict , str ] = None ,
228
229
context : str = None ,
229
230
) -> KubernetesSession :
230
231
"""
231
232
Sets up the Kubernetes session.
232
233
233
234
Args:
234
235
kubeconfig_file (str): Path to the kubeconfig file.
235
- kubeconfig_content (dict): Content of the kubeconfig file.
236
+ kubeconfig_content (str or dict): Content of the kubeconfig file.
236
237
context (str): Context name.
237
238
238
239
Returns:
@@ -243,14 +244,20 @@ def setup_session(
243
244
KubernetesInvalidProviderIdError: If the provider ID is invalid.
244
245
KubernetesSetUpSessionError: If an error occurs while setting up the session.
245
246
"""
246
- logger .info (f"Using kubeconfig file: { kubeconfig_file } " )
247
247
try :
248
248
if kubeconfig_content :
249
- config .load_kube_config_from_dict (
250
- safe_load (kubeconfig_content ), context = context
251
- )
252
-
249
+ logger .info ("Using kubeconfig content..." )
250
+ config_data = safe_load (kubeconfig_content )
251
+ config .load_kube_config_from_dict (config_data , context = context )
252
+ if context :
253
+ contexts = config_data .get ("contexts" , [])
254
+ for context_item in contexts :
255
+ if context_item ["name" ] == context :
256
+ context = context_item
257
+ else :
258
+ context = config_data .get ("contexts" , [])[0 ]
253
259
else :
260
+ logger .info (f"Using kubeconfig file: { kubeconfig_file } ..." )
254
261
kubeconfig_file = (
255
262
kubeconfig_file if kubeconfig_file else "~/.kube/config"
256
263
)
@@ -273,17 +280,19 @@ def setup_session(
273
280
return KubernetesSession (
274
281
api_client = client .ApiClient (), context = context
275
282
)
276
- if context :
277
- contexts = config .list_kube_config_contexts (
278
- config_file = kubeconfig_file
279
- )[0 ]
280
- for context_item in contexts :
281
- if context_item ["name" ] == context :
282
- context = context_item
283
- else :
284
- context = config .list_kube_config_contexts (config_file = kubeconfig_file )[
285
- 1
286
- ]
283
+ if context :
284
+ contexts = config .list_kube_config_contexts (
285
+ config_file = kubeconfig_file
286
+ )[0 ]
287
+ for context_item in contexts :
288
+ if context_item ["name" ] == context :
289
+ context = context_item
290
+ else :
291
+ # If no context is provided, use the active context in the kubeconfig file
292
+ # The first element is the list of contexts, the second is the active context
293
+ context = config .list_kube_config_contexts (
294
+ config_file = kubeconfig_file
295
+ )[1 ]
287
296
return KubernetesSession (api_client = client .ApiClient (), context = context )
288
297
289
298
except parser .ParserError as parser_error :
@@ -318,7 +327,7 @@ def setup_session(
318
327
@staticmethod
319
328
def test_connection (
320
329
kubeconfig_file : str = "~/.kube/config" ,
321
- kubeconfig_content : dict = None ,
330
+ kubeconfig_content : Union [ dict , str ] = None ,
322
331
namespace : str = None ,
323
332
provider_id : str = None ,
324
333
raise_on_exception : bool = True ,
@@ -328,7 +337,7 @@ def test_connection(
328
337
329
338
Args:
330
339
kubeconfig_file (str): Path to the kubeconfig file.
331
- kubeconfig_content (dict): Content of the kubeconfig file.
340
+ kubeconfig_content (str or dict): Content of the kubeconfig file.
332
341
namespace (str): Namespace name.
333
342
provider_id (str): Provider ID to use, in this case, the Kubernetes context.
334
343
raise_on_exception (bool): Whether to raise an exception on error.
@@ -352,7 +361,7 @@ def test_connection(
352
361
... )
353
362
- Using the kubeconfig content:
354
363
>>> connection = KubernetesProvider.test_connection(
355
- ... kubeconfig_content={"kubecofig": " content"} ,
364
+ ... kubeconfig_content="kubeconfig content",
356
365
... namespace="default",
357
366
... provider_id="my-context",
358
367
... raise_on_exception=True,
0 commit comments