diff --git a/examples/webApiDependencyInjection/Controllers/ExampleDependencyInjectionOnConstructorController.cs b/examples/webApiDependencyInjection/Controllers/ExampleDependencyInjectionOnConstructorController.cs new file mode 100644 index 0000000..30fd265 --- /dev/null +++ b/examples/webApiDependencyInjection/Controllers/ExampleDependencyInjectionOnConstructorController.cs @@ -0,0 +1,35 @@ +using k8s; +using Microsoft.AspNetCore.Mvc; + +namespace webApiDependencyInjection.Controllers +{ + [ApiController] + [Route("[controller]")] + public class ExampleDependencyInjectionOnConstructorController : ControllerBase + { + private readonly IKubernetes kubernetesClient; + + /// + /// Inject the kubernets class in the constructor. + /// + /// + public ExampleDependencyInjectionOnConstructorController(IKubernetes kubernetesClient) + { + this.kubernetesClient = kubernetesClient; + } + + /// + /// Example using the kubernetes client obtained from the constructor (this.kubernetesClient). + /// + /// + [HttpGet()] + public IEnumerable GetPods() + { + // Read the list of pods contained in default namespace + var podList = this.kubernetesClient.CoreV1.ListNamespacedPod("default"); + + // Return names of pods + return podList.Items.Select(pod => pod.Metadata.Name); + } + } +} diff --git a/examples/webApiDependencyInjection/Controllers/ExampleDependencyInjectionOnMethodController.cs b/examples/webApiDependencyInjection/Controllers/ExampleDependencyInjectionOnMethodController.cs new file mode 100644 index 0000000..0a831be --- /dev/null +++ b/examples/webApiDependencyInjection/Controllers/ExampleDependencyInjectionOnMethodController.cs @@ -0,0 +1,25 @@ +using k8s; +using Microsoft.AspNetCore.Mvc; + +namespace webApiDependencyInjection.Controllers +{ + [ApiController] + [Route("[controller]")] + public class ExampleDependencyInjectionOnMethodController : ControllerBase + { + /// + /// Example using the kubernetes client injected directly into the method ([FromServices] IKubernetes kubernetesClient). + /// + /// + /// + [HttpGet()] + public IEnumerable GetPods([FromServices] IKubernetes kubernetesClient) + { + // Read the list of pods contained in default namespace + var podList = kubernetesClient.CoreV1.ListNamespacedPod("default"); + + // Return names of pods + return podList.Items.Select(pod => pod.Metadata.Name); + } + } +} diff --git a/examples/webApiDependencyInjection/Program.cs b/examples/webApiDependencyInjection/Program.cs new file mode 100644 index 0000000..976e18d --- /dev/null +++ b/examples/webApiDependencyInjection/Program.cs @@ -0,0 +1,34 @@ +using k8s; + +var builder = WebApplication.CreateBuilder(args); + +// Load kubernetes configuration +var kubernetesClientConfig = KubernetesClientConfiguration.BuildDefaultConfig(); + +// Register Kubernetes client interface as sigleton +builder.Services.AddSingleton(new Kubernetes(kubernetesClientConfig)); + +// Add services to the container. +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +builder.Services.AddControllers(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseAuthorization(); + +app.MapControllers(); + +// Start the service +app.Run(); + + +// Swagger ui can be accesse at: http://localhost:/swagger diff --git a/examples/webApiDependencyInjection/appsettings.Development.json b/examples/webApiDependencyInjection/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/examples/webApiDependencyInjection/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/examples/webApiDependencyInjection/appsettings.json b/examples/webApiDependencyInjection/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/examples/webApiDependencyInjection/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/examples/webApiDependencyInjection/webApiDependencyInjection.csproj b/examples/webApiDependencyInjection/webApiDependencyInjection.csproj new file mode 100644 index 0000000..0fd3165 --- /dev/null +++ b/examples/webApiDependencyInjection/webApiDependencyInjection.csproj @@ -0,0 +1,12 @@ + + + + enable + enable + + + + + + + diff --git a/kubernetes-client.sln b/kubernetes-client.sln index 661bbf8..aead500 100644 --- a/kubernetes-client.sln +++ b/kubernetes-client.sln @@ -65,7 +65,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KubernetesClient.Kubectl", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kubectl.Tests", "tests\Kubectl.Tests\Kubectl.Tests.csproj", "{9128F6DC-6B7A-417F-937A-90461D6989A8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "openTelemetryConsole", "examples\openTelemetryConsole\openTelemetryConsole.csproj", "{8E266190-AE6E-44A8-948D-BD974AA82428}" +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}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -425,6 +427,18 @@ Global {8E266190-AE6E-44A8-948D-BD974AA82428}.Release|x64.Build.0 = Release|Any CPU {8E266190-AE6E-44A8-948D-BD974AA82428}.Release|x86.ActiveCfg = Release|Any CPU {8E266190-AE6E-44A8-948D-BD974AA82428}.Release|x86.Build.0 = Release|Any CPU + {C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Debug|x64.ActiveCfg = Debug|Any CPU + {C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Debug|x64.Build.0 = Debug|Any CPU + {C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Debug|x86.ActiveCfg = Debug|Any CPU + {C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Debug|x86.Build.0 = Debug|Any CPU + {C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Release|Any CPU.Build.0 = Release|Any CPU + {C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Release|x64.ActiveCfg = Release|Any CPU + {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 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -459,6 +473,7 @@ Global {21201F30-5463-4FC6-93C3-FBF157F0D46C} = {3D1864AA-1FFC-4512-BB13-46055E410F73} {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} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {049A763A-C891-4E8D-80CF-89DD3E22ADC7}