From 04180d322a47b2b260d6c636016c3226fa587520 Mon Sep 17 00:00:00 2001 From: Adam Friedman Date: Thu, 18 Jan 2024 13:36:37 +1100 Subject: [PATCH] Fix buggy implementation of DI-registration-for-all-K8s-contexts --- ...KubeClientOptionsRegistrationExtensions.cs | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/KubeClient.Extensions.DependencyInjection/KubeClientOptionsRegistrationExtensions.cs b/src/KubeClient.Extensions.DependencyInjection/KubeClientOptionsRegistrationExtensions.cs index e3e6b64..be0688c 100644 --- a/src/KubeClient.Extensions.DependencyInjection/KubeClientOptionsRegistrationExtensions.cs +++ b/src/KubeClient.Extensions.DependencyInjection/KubeClientOptionsRegistrationExtensions.cs @@ -111,6 +111,8 @@ public static IServiceCollection AddKubeClientOptionsFromKubeConfig(this IServic KubeClientOptions optionsForTargetContext = config.ToKubeClientOptions(kubeContextName, defaultKubeNamespace); optionsForTargetContext.CopyTo(kubeClientOptions); + + kubeClientOptions.KubeNamespace = defaultKubeNamespace; }); return services; @@ -156,6 +158,8 @@ public static IServiceCollection AddKubeClientOptionsFromKubeConfig(this IServic KubeClientOptions optionsForTargetContext = config.ToKubeClientOptions(kubeContextName, defaultKubeNamespace); optionsForTargetContext.CopyTo(kubeClientOptions); + + kubeClientOptions.KubeNamespace = defaultKubeNamespace; }); return services; @@ -186,25 +190,20 @@ public static IServiceCollection AddKubeClientOptionsFromKubeConfig(this IServic FileInfo kubeConfigFile = GetKubeConfigFile(kubeConfigFileName); + // List of contexts is static for application lifetime... K8sConfig config = K8sConfig.Load(kubeConfigFile); - foreach (Context targetContext in config.Contexts) // AF: List of contexts is static for application lifetime, but config for those contexts is dynamic. + + foreach (Context targetContext in config.Contexts) { 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}'."); + // ...but config for those contexts is dynamic. + K8sConfig currentConfig = K8sConfig.Load(kubeConfigFile); - 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 optionsForTargetContext = currentConfig.ToKubeClientOptions(targetContext.Name, defaultKubeNamespace); + optionsForTargetContext.CopyTo(kubeClientOptions); - 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(); }); }