@@ -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,24 +100,21 @@ 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
"""
104
- client_configuration = kwargs .get ("client_configuration" , kubernetes .client .Configuration ())
105
-
106
- # If the proxy is not set, set it from the environment
107
- if not client_configuration .proxy :
108
- proxy = os .environ .get ("HTTPS_PROXY" ) or os .environ .get ("HTTP_PROXY" )
109
- if proxy :
110
- client_configuration .proxy = proxy
111
-
112
- kwargs ["client_configuration" ] = client_configuration
113
-
114
110
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/kube_config.py
115
111
if config_dict :
116
112
_client = kubernetes .config .new_client_from_config_dict (
117
- config_dict = config_dict , context = context or None , ** kwargs
113
+ config_dict = config_dict ,
114
+ context = context ,
115
+ client_configuration = client_configuration ,
116
+ persist_config = persist_config ,
117
+ temp_file_path = temp_file_path ,
118
118
)
119
119
else :
120
120
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/__init__.py
@@ -127,20 +127,26 @@ def get_client(
127
127
128
128
_client = kubernetes .config .new_client_from_config (
129
129
config_file = config_file ,
130
- context = context or None ,
131
- ** kwargs ,
130
+ context = context ,
131
+ client_configuration = client_configuration ,
132
+ persist_config = persist_config ,
132
133
)
133
134
135
+ proxy = os .environ .get ("HTTPS_PROXY" ) or os .environ .get ("HTTP_PROXY" )
136
+
137
+ if not _client .configuration .proxy and proxy :
138
+ LOGGER .info (f"Setting proxy from environment variable: { proxy } " )
139
+ _client .configuration .proxy = proxy
140
+
134
141
try :
135
142
return kubernetes .dynamic .DynamicClient (client = _client )
136
143
except MaxRetryError :
137
144
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/incluster_config.py
138
145
LOGGER .info ("Trying to get client via incluster_config" )
139
146
return kubernetes .dynamic .DynamicClient (
140
147
client = kubernetes .config .incluster_config .load_incluster_config (
141
- client_configuration = client_configuration ,
142
- try_refresh_token = kwargs .get ("try_refresh_token" , True ),
143
- )
148
+ client_configuration = client_configuration , try_refresh_token = try_refresh_token
149
+ ),
144
150
)
145
151
146
152
0 commit comments