Files
csharp/examples/webApiDependencyInjection/Controllers/ExampleDependencyInjectionOnConstructorController.cs
Boshi Lian ae79be6665 Clean up warnings in examples (#1628)
* Refactor examples to streamline code structure and improve readability

* Update LangVersion to 13.0 for improved compatibility
2025-04-29 16:55:55 -07:00

37 lines
1.3 KiB
C#

using k8s;
using Microsoft.AspNetCore.Mvc;
namespace webApiDependencyInjection.Controllers
{
[ApiController]
[Route("[controller]")]
public class ExampleDependencyInjectionOnConstructorController : ControllerBase
{
private readonly IKubernetes kubernetesClient;
/// <summary>
/// Initializes a new instance of the <see cref="ExampleDependencyInjectionOnConstructorController"/> class.
/// Injects the Kubernetes client into the controller.
/// </summary>
/// <param name="kubernetesClient">The Kubernetes client to interact with the Kubernetes API.</param>
public ExampleDependencyInjectionOnConstructorController(IKubernetes kubernetesClient)
{
this.kubernetesClient = kubernetesClient;
}
/// <summary>
/// Retrieves the names of all pods in the default namespace using the injected Kubernetes client.
/// </summary>
/// <returns>A collection of pod names in the default namespace.</returns>
[HttpGet]
public IEnumerable<string> GetPods()
{
// Read the list of pods contained in the default namespace
var podList = this.kubernetesClient.CoreV1.ListNamespacedPod("default");
// Return names of pods
return podList.Items.Select(pod => pod.Metadata.Name);
}
}
}