kubernetes Controller to API communication

7/11/2017

I've a requirement where I need to create/write ConfigMap and this code will be part of kube-controller-manager.I'm deploying my kubernetes with single master and kubernetes components are running as pods. I'm using "k8s.io/client-go/rest" to get client which communicates with API.

import "k8s.io/client-go/rest"
.
.
.
rest.InClusterConfig()

As per the code InClusterConfig checks for the kubernetes service environment variables i.e "KUBERNETES_SERVICE_HOST" "KUBERNETES_SERVICE_PORT". And even it checks for service token (/var/run/secrets/kubernetes.io/serviceaccount/token).

Problem I'm facing is when controller manager comes up environment variables ("KUBERNETES_SERVICE_HOST" "KUBERNETES_SERVICE_PORT") are not present and service token (/var/run/secrets/kubernetes.io/serviceaccount/token) is also not mounted.

"k8s.io/client-go/rest" should not be used from controller-manager ? If it should not be used then what is the best way to communicate API from controller manager to create/write configMaps.?

-- ahmed meraj
kubernetes

1 Answer

7/11/2017

In order to create k8s resources (pods, configmaps, deployments etc.) your cluster has to be in stable state, that means kube-apiserver, kube-controller-manager and kube-scheduler must be in ready state. From my understanding of your description you are using static manifest to deploy kubernetes components as pods, in this case, those pods are not purely managed by kubernetes, those are docker(rkt) containers, that once in running state, are attached to kubernetes. As a consequence of this, they don't have any of the kubernetes specific pod environment configuration (as you noticed, they are missing kubernetes secrets and environments), this is the reason why you cannot use InClusterConfig() method. Another thing is that, creating any kubernetes resources from within kube-controller-manager does not look like a good idea. But if you really want to do this, then use BuildConfigFromFlags() from k8s.io/client-go/tools/clientcmd, in this function you can specify manually the kube-apiserver url and kubeconfig file location and reuse kube-controller-manager flags.

-- Adam Otto
Source: StackOverflow