Skip to content

Commit 6d0bbcf

Browse files
Adam FriedmanAdam Friedman
Adam Friedman
authored and
Adam Friedman
committed
Merge branch 'develop'
2 parents d6f07f4 + 04180d3 commit 6d0bbcf

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/KubeClient.Extensions.DependencyInjection/KubeClientOptionsRegistrationExtensions.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public static IServiceCollection AddKubeClientOptionsFromKubeConfig(this IServic
111111
KubeClientOptions optionsForTargetContext = config.ToKubeClientOptions(kubeContextName, defaultKubeNamespace);
112112

113113
optionsForTargetContext.CopyTo(kubeClientOptions);
114+
115+
kubeClientOptions.KubeNamespace = defaultKubeNamespace;
114116
});
115117

116118
return services;
@@ -156,6 +158,8 @@ public static IServiceCollection AddKubeClientOptionsFromKubeConfig(this IServic
156158
KubeClientOptions optionsForTargetContext = config.ToKubeClientOptions(kubeContextName, defaultKubeNamespace);
157159

158160
optionsForTargetContext.CopyTo(kubeClientOptions);
161+
162+
kubeClientOptions.KubeNamespace = defaultKubeNamespace;
159163
});
160164

161165
return services;
@@ -186,25 +190,20 @@ public static IServiceCollection AddKubeClientOptionsFromKubeConfig(this IServic
186190

187191
FileInfo kubeConfigFile = GetKubeConfigFile(kubeConfigFileName);
188192

193+
// List of contexts is static for application lifetime...
189194
K8sConfig config = K8sConfig.Load(kubeConfigFile);
190-
foreach (Context targetContext in config.Contexts) // AF: List of contexts is static for application lifetime, but config for those contexts is dynamic.
195+
196+
foreach (Context targetContext in config.Contexts)
191197
{
192198
services.AddKubeClientOptions(targetContext.Name, kubeClientOptions =>
193199
{
194-
Cluster targetCluster = config.Clusters.Find(cluster => cluster.Name == targetContext.Config.ClusterName);
195-
if (targetCluster == null)
196-
throw new InvalidOperationException($"Cannot find a cluster in the Kubernetes client configuration named '{targetContext.Config.ClusterName}'.");
200+
// ...but config for those contexts is dynamic.
201+
K8sConfig currentConfig = K8sConfig.Load(kubeConfigFile);
197202

198-
UserIdentity targetUser = config.UserIdentities.Find(user => user.Name == targetContext.Config.UserName);
199-
if (targetUser == null)
200-
throw new InvalidOperationException($"Cannot find a user identity in the Kubernetes client configuration named '{targetContext.Config.UserName}'.");
203+
KubeClientOptions optionsForTargetContext = currentConfig.ToKubeClientOptions(targetContext.Name, defaultKubeNamespace);
204+
optionsForTargetContext.CopyTo(kubeClientOptions);
201205

202-
kubeClientOptions.ApiEndPoint = new Uri(targetCluster.Config.Server);
203206
kubeClientOptions.KubeNamespace = defaultKubeNamespace;
204-
205-
kubeClientOptions.ClientCertificate = targetUser.Config.GetClientCertificate();
206-
kubeClientOptions.CertificationAuthorityCertificate = targetCluster.Config.GetCACertificate();
207-
kubeClientOptions.AccessToken = targetUser.Config.GetRawToken();
208207
});
209208
}
210209

0 commit comments

Comments
 (0)