using k8s;
using Microsoft.AspNetCore.Mvc;
namespace webApiDependencyInjection.Controllers
{
[ApiController]
[Route("[controller]")]
public class ExampleDependencyInjectionOnConstructorController : ControllerBase
{
private readonly IKubernetes kubernetesClient;
///
/// Initializes a new instance of the class.
/// Injects the Kubernetes client into the controller.
///
/// The Kubernetes client to interact with the Kubernetes API.
public ExampleDependencyInjectionOnConstructorController(IKubernetes kubernetesClient)
{
this.kubernetesClient = kubernetesClient;
}
///
/// Retrieves the names of all pods in the default namespace using the injected Kubernetes client.
///
/// A collection of pod names in the default namespace.
[HttpGet]
public IEnumerable 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);
}
}
}