2022-07-14 01:36:56 +02:00
|
|
|
using k8s;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace webApiDependencyInjection.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("[controller]")]
|
|
|
|
|
public class ExampleDependencyInjectionOnMethodController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Example using the kubernetes client injected directly into the method ([FromServices] IKubernetes kubernetesClient).
|
|
|
|
|
/// </summary>
|
2025-04-29 16:55:55 -07:00
|
|
|
/// <param name="kubernetesClient">The Kubernetes client instance injected via dependency injection.</param>
|
|
|
|
|
/// <returns>A collection of pod names in the default namespace.</returns>
|
|
|
|
|
[HttpGet]
|
2022-07-14 01:36:56 +02:00
|
|
|
public IEnumerable<string> GetPods([FromServices] IKubernetes kubernetesClient)
|
|
|
|
|
{
|
2025-04-29 16:55:55 -07:00
|
|
|
ArgumentNullException.ThrowIfNull(kubernetesClient);
|
|
|
|
|
|
2022-07-14 01:36:56 +02:00
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|