public static IKubernetes GetKubernetesClient(KubernetesApplicationOptions appInfo, Action <KubernetesClientConfiguration> kubernetesClientConfiguration = null, ILogger logger = null) { KubernetesClientConfiguration k8sConfig = null; try { if (appInfo.Config.Paths.Any()) { var delimiter = Platform.IsWindows ? ';' : ':'; var joinedPaths = appInfo.Config.Paths.Aggregate((i, j) => i + delimiter + j); Environment.SetEnvironmentVariable("KUBECONFIG", joinedPaths); } k8sConfig = KubernetesClientConfiguration.BuildDefaultConfig(); } catch (KubeConfigException e) { // couldn't locate .kube\config or user-identified files. use an empty config object and fall back on user-defined Action to set the configuration logger?.LogWarning(e, "Failed to build KubernetesClientConfiguration, creating an empty config..."); } // BuildDefaultConfig() doesn't set a host if KubeConfigException is thrown k8sConfig ??= new KubernetesClientConfiguration() { Host = "http://localhost:8080" }; kubernetesClientConfiguration?.Invoke(k8sConfig); return(new k8s.Kubernetes(k8sConfig)); }
/// <summary> /// Removes any existing <see cref="IApplicationInstanceInfo"/> if found. Registers a <see cref="KubernetesApplicationOptions" /> /// </summary> /// <param name="serviceCollection">Collection of configured services</param> public static IServiceCollection AddKubernetesApplicationInstanceInfo(this IServiceCollection serviceCollection) { if (serviceCollection is null) { throw new ArgumentNullException(nameof(serviceCollection)); } var appInfo = serviceCollection.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IApplicationInstanceInfo)); if (appInfo?.ImplementationType?.IsAssignableFrom(typeof(KubernetesApplicationOptions)) != true) { if (appInfo != null) { serviceCollection.Remove(appInfo); } var sp = serviceCollection.BuildServiceProvider(); var config = sp.GetRequiredService <IConfiguration>(); var newAppInfo = new KubernetesApplicationOptions(config); serviceCollection.AddSingleton(newAppInfo); serviceCollection.AddSingleton(typeof(IApplicationInstanceInfo), newAppInfo); } return(serviceCollection); }