Skip to content

Commit

Permalink
Use common implementation of KubeClientOptions configuration from K8s…
Browse files Browse the repository at this point in the history
…Config

#108 #136
  • Loading branch information
tintoy committed Aug 10, 2022
1 parent 89f4549 commit 3c6a94b
Showing 1 changed file with 3 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,7 @@ public static IServiceCollection AddKubeClientOptionsFromKubeConfig(this IServic
{
K8sConfig config = K8sConfig.Load(kubeConfigFile);

string targetContextName = kubeContextName ?? config.CurrentContextName;
if (String.IsNullOrWhiteSpace(targetContextName))
throw new InvalidOperationException("The kubeContextName parameter was not specified, and the Kubernetes client configuration does not specify a current context.");

Context targetContext = config.Contexts.Find(context => context.Name == targetContextName);
if (targetContext == null)
throw new InvalidOperationException($"Cannot find a context in the Kubernetes client configuration named '{targetContextName}'.");

Cluster targetCluster = config.Clusters.Find(cluster => cluster.Name == targetContext.Config.ClusterName);
if (targetCluster == null)
throw new InvalidOperationException($"Cannot find a cluster in the Kubernetes client configuration named '{targetContext.Config.ClusterName}'.");

UserIdentity targetUser = config.UserIdentities.Find(user => user.Name == targetContext.Config.UserName);
if (targetUser == null)
throw new InvalidOperationException($"Cannot find a user identity in the Kubernetes client configuration named '{targetContext.Config.UserName}'.");

kubeClientOptions.ApiEndPoint = new Uri(targetCluster.Config.Server);
kubeClientOptions.KubeNamespace = defaultKubeNamespace;

kubeClientOptions.ClientCertificate = targetUser.Config.GetClientCertificate();
kubeClientOptions.CertificationAuthorityCertificate = targetCluster.Config.GetCACertificate();
kubeClientOptions.AccessToken = targetUser.Config.GetRawToken();
config.ConfigureKubeClientOptions(kubeClientOptions);
});

return services;
Expand Down Expand Up @@ -126,28 +105,7 @@ public static IServiceCollection AddKubeClientOptionsFromKubeConfig(this IServic
{
K8sConfig config = K8sConfig.Load(kubeConfigFile);

string targetContextName = kubeContextName ?? config.CurrentContextName;
if (String.IsNullOrWhiteSpace(targetContextName))
throw new K8sConfigException("The kubeContextName parameter was not specified, and the Kubernetes client configuration does not specify a current context.");

Context targetContext = config.Contexts.Find(context => context.Name == targetContextName);
if (targetContext == null)
throw new K8sConfigException($"Cannot find a context in the Kubernetes client configuration named '{targetContextName}'.");

Cluster targetCluster = config.Clusters.Find(cluster => cluster.Name == targetContext.Config.ClusterName);
if (targetCluster == null)
throw new K8sConfigException($"Cannot find a cluster in the Kubernetes client configuration named '{targetContext.Config.ClusterName}'.");

UserIdentity targetUser = config.UserIdentities.Find(user => user.Name == targetContext.Config.UserName);
if (targetUser == null)
throw new K8sConfigException($"Cannot find a user identity in the Kubernetes client configuration named '{targetContext.Config.UserName}'.");

kubeClientOptions.ApiEndPoint = new Uri(targetCluster.Config.Server);
kubeClientOptions.KubeNamespace = defaultKubeNamespace;
kubeClientOptions.ClientCertificate = targetUser.Config.GetClientCertificate();
kubeClientOptions.AllowInsecure = targetCluster.Config.AllowInsecure;
kubeClientOptions.CertificationAuthorityCertificate = targetCluster.Config.GetCACertificate();
kubeClientOptions.AccessToken = targetUser.Config.GetRawToken();
config.ConfigureKubeClientOptions(kubeClientOptions, kubeContextName, defaultKubeNamespace);
});

return services;
Expand Down Expand Up @@ -191,20 +149,7 @@ public static IServiceCollection AddKubeClientOptionsFromKubeConfig(this IServic
{
services.AddKubeClientOptions(targetContext.Name, kubeClientOptions =>
{
Cluster targetCluster = config.Clusters.Find(cluster => cluster.Name == targetContext.Config.ClusterName);
if (targetCluster == null)
throw new InvalidOperationException($"Cannot find a cluster in the Kubernetes client configuration named '{targetContext.Config.ClusterName}'.");

UserIdentity targetUser = config.UserIdentities.Find(user => user.Name == targetContext.Config.UserName);
if (targetUser == null)
throw new InvalidOperationException($"Cannot find a user identity in the Kubernetes client configuration named '{targetContext.Config.UserName}'.");

kubeClientOptions.ApiEndPoint = new Uri(targetCluster.Config.Server);
kubeClientOptions.KubeNamespace = defaultKubeNamespace;

kubeClientOptions.ClientCertificate = targetUser.Config.GetClientCertificate();
kubeClientOptions.CertificationAuthorityCertificate = targetCluster.Config.GetCACertificate();
kubeClientOptions.AccessToken = targetUser.Config.GetRawToken();
config.ConfigureKubeClientOptions(kubeClientOptions, kubeContextName: targetContext.Name);
});
}

Expand Down

0 comments on commit 3c6a94b

Please sign in to comment.