Skip to content
Merged
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
2 changes: 1 addition & 1 deletion examples/webApiDependencyInjection/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var kubernetesClientConfig = KubernetesClientConfiguration.BuildDefaultConfig();

// Register Kubernetes client interface as sigleton
builder.Services.AddSingleton<IKubernetes>(new Kubernetes(kubernetesClientConfig));
builder.Services.AddSingleton<IKubernetes>(_ => new Kubernetes(kubernetesClientConfig));

// Add services to the container.
builder.Services.AddEndpointsApiExplorer();
Expand Down
17 changes: 17 additions & 0 deletions examples/workerServiceDependencyInjection/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using k8s;
using workerServiceDependencyInjection;

IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
// Load kubernetes configuration
var kubernetesClientConfig = KubernetesClientConfiguration.BuildDefaultConfig();

// Register Kubernetes client interface as sigleton
services.AddSingleton<IKubernetes>(_ => new Kubernetes(kubernetesClientConfig));

services.AddHostedService<Worker>();
})
.Build();

await host.RunAsync();
40 changes: 40 additions & 0 deletions examples/workerServiceDependencyInjection/Worker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using k8s;

namespace workerServiceDependencyInjection
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> logger;
private readonly IKubernetes kubernetesClient;

/// <summary>
/// Inject in the constructor the IKubernetes interface.
/// </summary>
/// <param name="logger"></param>
/// <param name="kubernetesClient"></param>
public Worker(ILogger<Worker> logger, IKubernetes kubernetesClient)
{
this.logger = logger;
this.kubernetesClient = kubernetesClient;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

// Read the list of pods contained in default namespace
var podList = kubernetesClient.CoreV1.ListNamespacedPod("default");

// Print pods names
foreach (var pod in podList.Items)
{
Console.WriteLine(pod.Metadata.Name);
}

await Task.Delay(1000, stoppingToken);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
8 changes: 8 additions & 0 deletions examples/workerServiceDependencyInjection/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
</ItemGroup>
</Project>
17 changes: 16 additions & 1 deletion kubernetes-client.sln
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kubectl.Tests", "tests\Kube
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "openTelemetryConsole", "examples\openTelemetryConsole\openTelemetryConsole.csproj", "{8E266190-AE6E-44A8-948D-BD974AA82428}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "webApiDependencyInjection", "examples\webApiDependencyInjection\webApiDependencyInjection.csproj", "{C0759F88-A010-4DEF-BD3B-E183D3328FFC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "webApiDependencyInjection", "examples\webApiDependencyInjection\webApiDependencyInjection.csproj", "{C0759F88-A010-4DEF-BD3B-E183D3328FFC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "workerServiceDependencyInjection", "examples\workerServiceDependencyInjection\workerServiceDependencyInjection.csproj", "{05DC8884-AC54-4603-AC25-AE9D9F24E7AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -439,6 +441,18 @@ Global
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Release|x64.Build.0 = Release|Any CPU
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Release|x86.ActiveCfg = Release|Any CPU
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Release|x86.Build.0 = Release|Any CPU
{05DC8884-AC54-4603-AC25-AE9D9F24E7AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{05DC8884-AC54-4603-AC25-AE9D9F24E7AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{05DC8884-AC54-4603-AC25-AE9D9F24E7AE}.Debug|x64.ActiveCfg = Debug|Any CPU
{05DC8884-AC54-4603-AC25-AE9D9F24E7AE}.Debug|x64.Build.0 = Debug|Any CPU
{05DC8884-AC54-4603-AC25-AE9D9F24E7AE}.Debug|x86.ActiveCfg = Debug|Any CPU
{05DC8884-AC54-4603-AC25-AE9D9F24E7AE}.Debug|x86.Build.0 = Debug|Any CPU
{05DC8884-AC54-4603-AC25-AE9D9F24E7AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{05DC8884-AC54-4603-AC25-AE9D9F24E7AE}.Release|Any CPU.Build.0 = Release|Any CPU
{05DC8884-AC54-4603-AC25-AE9D9F24E7AE}.Release|x64.ActiveCfg = Release|Any CPU
{05DC8884-AC54-4603-AC25-AE9D9F24E7AE}.Release|x64.Build.0 = Release|Any CPU
{05DC8884-AC54-4603-AC25-AE9D9F24E7AE}.Release|x86.ActiveCfg = Release|Any CPU
{05DC8884-AC54-4603-AC25-AE9D9F24E7AE}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -474,6 +488,7 @@ Global
{9128F6DC-6B7A-417F-937A-90461D6989A8} = {8AF4A5C2-F0CE-47D5-A4C5-FE4AB83CA509}
{8E266190-AE6E-44A8-948D-BD974AA82428} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
{C0759F88-A010-4DEF-BD3B-E183D3328FFC} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
{05DC8884-AC54-4603-AC25-AE9D9F24E7AE} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {049A763A-C891-4E8D-80CF-89DD3E22ADC7}
Expand Down