Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/simple/PodList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ class PodList
{
static void Main(string[] args)
{
var k8sClientConfig = new KubernetesClientConfiguration();
var k8sClientConfig = KubernetesClientConfiguration.defaultConfiguration();
if (k8sClientConfig.CurrentContext == null) {
Console.WriteLine("No current context");
return;
}
IKubernetes client = new Kubernetes(k8sClientConfig);
Console.WriteLine("Starting Request!");
var listTask = client.ListNamespacedPodWithHttpMessagesAsync("default").Result;
Expand Down
5 changes: 4 additions & 1 deletion src/KubernetesClient.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netstandard2.0</TargetFramework>
<VersionPrefix>0.1.0</VersionPrefix>
<VersionSuffix>beta</VersionSuffix>
</PropertyGroup>
<ItemGroup>
<Compile Remove="GlobalSuppressions.cs" />
Expand All @@ -12,4 +15,4 @@
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
<PackageReference Include="YamlDotNet.NetCore" Version="1.0.0" />
</ItemGroup>
</Project>
</Project>
13 changes: 13 additions & 0 deletions src/KubernetesClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public KubernetesClientConfiguration(FileInfo kubeconfig = null, string currentC
this.Initialize(k8SConfig, currentContext);
}

public static KubernetesClientConfiguration defaultConfiguration() {
var kubeConfig = Environment.GetEnvironmentVariable("KUBECONFIG");
if (kubeConfig != null) {
return new KubernetesClientConfiguration(new FileInfo(kubeConfig));
}
return new KubernetesClientConfiguration();
}


/// <summary>
/// kubeconfig Default Location
/// </summary>
Expand Down Expand Up @@ -120,6 +129,10 @@ private void Initialize(K8SConfiguration k8SConfig, string currentContext = null

// current context
currentContext = currentContext ?? k8SConfig.CurrentContext;
if (string.IsNullOrEmpty(currentContext)) {
// Nothing more to do...
return;
}
Context activeContext = k8SConfig.Contexts.FirstOrDefault(c => c.Name.Equals(currentContext, StringComparison.OrdinalIgnoreCase));
if (activeContext == null)
{
Expand Down