Skip to content

Commit 233dc03

Browse files
authored
Share code to create ServiceClientCredentials (#425)
1 parent 392ebcf commit 233dc03

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/KubernetesClient/Kubernetes.ConfigInit.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -224,22 +224,7 @@ private void CreateHttpClient(DelegatingHandler[] handlers)
224224
/// Set credentials for the Client
225225
/// </summary>
226226
/// <param name="config">k8s client configuration</param>
227-
private void SetCredentials(KubernetesClientConfiguration config)
228-
{
229-
// set the Credentails for token based auth
230-
if (!string.IsNullOrWhiteSpace(config.AccessToken))
231-
{
232-
Credentials = new TokenCredentials(config.AccessToken);
233-
}
234-
else if (!string.IsNullOrWhiteSpace(config.Username) && !string.IsNullOrWhiteSpace(config.Password))
235-
{
236-
Credentials = new BasicAuthenticationCredentials
237-
{
238-
UserName = config.Username,
239-
Password = config.Password
240-
};
241-
}
242-
}
227+
private void SetCredentials(KubernetesClientConfiguration config) => Credentials = CreateCredentials(config);
243228

244229
/// <summary>
245230
/// SSl Cert Validation Callback
@@ -296,5 +281,22 @@ public static bool CertificateValidationCallBack(
296281
// In all other cases, return false.
297282
return false;
298283
}
284+
285+
/// <summary>Creates <see cref="ServiceClientCredentials"/> based on the given config, or returns null if no such credentials are
286+
/// needed.
287+
/// </summary>
288+
public static ServiceClientCredentials CreateCredentials(KubernetesClientConfiguration config)
289+
{
290+
if (config == null) throw new ArgumentNullException(nameof(config));
291+
if (!string.IsNullOrEmpty(config.AccessToken))
292+
{
293+
return new TokenCredentials(config.AccessToken);
294+
}
295+
else if (!string.IsNullOrEmpty(config.Username))
296+
{
297+
return new BasicAuthenticationCredentials() { UserName = config.Username, Password = config.Password };
298+
}
299+
return null;
300+
}
299301
}
300302
}

0 commit comments

Comments
 (0)