@@ -77,10 +77,13 @@ def _get_api_version(dyn_client: DynamicClient, api_group: str, kind: str) -> st
77
77
78
78
79
79
def get_client (
80
- config_file : str = "" ,
80
+ config_file : str | None = None ,
81
81
config_dict : dict [str , Any ] | None = None ,
82
- context : str = "" ,
83
- ** kwargs : Any ,
82
+ context : str | None = None ,
83
+ client_configuration : kubernetes .client .Configuration | None = None ,
84
+ persist_config : bool = True ,
85
+ temp_file_path : str | None = None ,
86
+ try_refresh_token : bool = True ,
84
87
) -> DynamicClient :
85
88
"""
86
89
Get a kubernetes client.
@@ -97,19 +100,31 @@ def get_client(
97
100
config_file (str): path to a kubeconfig file.
98
101
config_dict (dict): dict with kubeconfig configuration.
99
102
context (str): name of the context to use.
103
+ persist_config (bool): whether to persist config file.
104
+ temp_file_path (str): path to a temporary kubeconfig file.
105
+ try_refresh_token (bool): try to refresh token
100
106
101
107
Returns:
102
108
DynamicClient: a kubernetes client.
103
109
"""
110
+ proxy = os .environ .get ("HTTPS_PROXY" ) or os .environ .get ("HTTP_PROXY" )
111
+
112
+ client_configuration = client_configuration or kubernetes .client .Configuration ()
113
+
114
+ if not client_configuration .proxy and proxy :
115
+ LOGGER .info (f"Setting proxy from environment variable: { proxy } " )
116
+ client_configuration .proxy = proxy
117
+
104
118
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/kube_config.py
105
119
if config_dict :
106
- return kubernetes .dynamic .DynamicClient (
107
- client = kubernetes .config .new_client_from_config_dict (
108
- config_dict = config_dict , context = context or None , ** kwargs
109
- )
120
+ _client = kubernetes .config .new_client_from_config_dict (
121
+ config_dict = config_dict ,
122
+ context = context ,
123
+ client_configuration = client_configuration ,
124
+ persist_config = persist_config ,
125
+ temp_file_path = temp_file_path ,
110
126
)
111
- client_configuration = kwargs .get ("client_configuration" , kubernetes .client .Configuration ())
112
- try :
127
+ else :
113
128
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/__init__.py
114
129
LOGGER .info ("Trying to get client via new_client_from_config" )
115
130
@@ -118,36 +133,22 @@ def get_client(
118
133
# is populated during import which comes before setting the variable in code.
119
134
config_file = config_file or os .environ .get ("KUBECONFIG" , "~/.kube/config" )
120
135
121
- if os .environ .get ("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY" ):
122
- proxy = os .environ .get ("HTTPS_PROXY" ) or os .environ .get ("HTTP_PROXY" )
123
- if not proxy :
124
- raise ValueError (
125
- "Proxy configuration is enabled but neither HTTPS_PROXY nor HTTP_PROXY environment variables are set."
126
- )
127
- if client_configuration .proxy and client_configuration .proxy != proxy :
128
- raise ValueError (
129
- f"Conflicting proxy settings: client_configuration.proxy={ client_configuration .proxy } , "
130
- f"but the environment variable 'HTTPS_PROXY/HTTP_PROXY' defines proxy as { proxy } ."
131
- )
132
- client_configuration .proxy = proxy
133
-
134
- kwargs ["client_configuration" ] = client_configuration
135
-
136
- return kubernetes .dynamic .DynamicClient (
137
- client = kubernetes .config .new_client_from_config (
138
- config_file = config_file ,
139
- context = context or None ,
140
- ** kwargs ,
141
- )
136
+ _client = kubernetes .config .new_client_from_config (
137
+ config_file = config_file ,
138
+ context = context ,
139
+ client_configuration = client_configuration ,
140
+ persist_config = persist_config ,
142
141
)
142
+
143
+ try :
144
+ return kubernetes .dynamic .DynamicClient (client = _client )
143
145
except MaxRetryError :
144
146
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/incluster_config.py
145
147
LOGGER .info ("Trying to get client via incluster_config" )
146
148
return kubernetes .dynamic .DynamicClient (
147
149
client = kubernetes .config .incluster_config .load_incluster_config (
148
- client_configuration = client_configuration ,
149
- try_refresh_token = kwargs .get ("try_refresh_token" , True ),
150
- )
150
+ client_configuration = client_configuration , try_refresh_token = try_refresh_token
151
+ ),
151
152
)
152
153
153
154
@@ -429,9 +430,9 @@ def __init__(
429
430
dry_run : bool = False ,
430
431
node_selector : dict [str , Any ] | None = None ,
431
432
node_selector_labels : dict [str , str ] | None = None ,
432
- config_file : str = "" ,
433
+ config_file : str | None = None ,
433
434
config_dict : dict [str , Any ] | None = None ,
434
- context : str = "" ,
435
+ context : str | None = None ,
435
436
label : dict [str , str ] | None = None ,
436
437
annotations : dict [str , str ] | None = None ,
437
438
api_group : str = "" ,
@@ -486,11 +487,6 @@ def __init__(
486
487
self .node_selector = node_selector
487
488
self .node_selector_labels = node_selector_labels
488
489
self .config_file = config_file
489
- if not isinstance (self .config_file , str ):
490
- # If we pass config_file which isn't a string, get_client will fail and it will be very hard to know why.
491
- # Better fail here and let the user know.
492
- raise ValueError ("config_file must be a string" )
493
-
494
490
self .config_dict = config_dict or {}
495
491
self .context = context
496
492
self .label = label
@@ -967,10 +963,10 @@ def retry_cluster_exceptions(
967
963
def get (
968
964
cls ,
969
965
config_file : str = "" ,
970
- context : str = "" ,
971
966
singular_name : str = "" ,
972
967
exceptions_dict : dict [type [Exception ], list [str ]] = DEFAULT_CLUSTER_RETRY_EXCEPTIONS ,
973
968
raw : bool = False ,
969
+ context : str | None = None ,
974
970
dyn_client : DynamicClient | None = None ,
975
971
* args : Any ,
976
972
** kwargs : Any ,
@@ -1196,7 +1192,7 @@ def events(
1196
1192
def get_all_cluster_resources (
1197
1193
client : DynamicClient | None = None ,
1198
1194
config_file : str = "" ,
1199
- context : str = "" ,
1195
+ context : str | None = None ,
1200
1196
config_dict : dict [str , Any ] | None = None ,
1201
1197
* args : Any ,
1202
1198
** kwargs : Any ,
@@ -1333,10 +1329,10 @@ def __init__(
1333
1329
def get (
1334
1330
cls ,
1335
1331
config_file : str = "" ,
1336
- context : str = "" ,
1337
1332
singular_name : str = "" ,
1338
1333
exceptions_dict : dict [type [Exception ], list [str ]] = DEFAULT_CLUSTER_RETRY_EXCEPTIONS ,
1339
1334
raw : bool = False ,
1335
+ context : str | None = None ,
1340
1336
dyn_client : DynamicClient | None = None ,
1341
1337
* args : Any ,
1342
1338
** kwargs : Any ,
0 commit comments