I'm looking for a lightweight way to access the Kubernetes API from a Pod in a C# app.
Kubernetes docs mention two ways of accessing the API from a Pod:
- Run kubectl proxy in a sidecar container in the pod, or as a background process within the container
This generally works, and allows for easily hitting an API endpoint with just a line of code or two - example:
using System;
using System.Net;
namespace GetIngresses
{
class Program
{
static void Main(string[] args)
{
var apiBaseUrl = "http://127.0.0.1:8001"; // requires kubectl proxy
Console.WriteLine((new WebClient()).
DownloadString($"{apiBaseUrl}/apis/networking.k8s.io/v1/ingresses"));
}
}
}
However, now there's a running kubectl proxy process to monitor, maintain etc. - this doesn't seem ideal for production.
- Use the Go client library, and create a client using the rest.InClusterConfig() and kubernetes.NewForConfig() functions. They handle locating and authenticating to the apiserver.
My app is written in C#, not Go. There's a C# client library which presumably might be able to achieve the same thing. But do I really have to bring a whole client library on board just for a simple GET to a single endpoint?
Ideally, I'd like to just use WebClient, like in the example above. Documentation mentions that
The recommended way to locate the apiserver within the pod is with the kubernetes.default.svc DNS name, which resolves to a Service IP which in turn will be routed to an apiserver.
So, in the example above, can I just do this...
var apiBaseUrl = "http://kubernetes.default.svc"
... and get WebClient to pass the required service account credentials? If yes, how?