In a previous blog post, we have seen how we can communicate programmatically from outside of the cluster, now we will see how we can talk to API server from inside. examples for this use case are custom controllers, operators, etc
Similar to the previous function, if we don’t pass either master URL or kubeconfig file path, it will fallback to
rest.InClusterConfig (coming from
config, err := clientcmd.BuildConfigFromFlags("", "")
you might be curious without providing master URL or kubeconfig file path, how it is accessing the API server, let’s have a look at
config, err := rest.InClusterConfig()
How does it work?
- first, it looks for
KUBERNETES_SERVICE_PORTenvironment variables, ideally, kubernetes injects those environment variables into every pod.
- then, it looks for
with all this information,
rest.Config struct which then used to create actual clientsets.
With this method,
namespace is determined either by
POD_NAMESPACE environment variable or by reading this file
/var/run/secrets/kubernetes.io/serviceaccount/namespace from the pod.