kubernetes-client / c

Official C client library for Kubernetes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

API Server authentication

ReddyArunreddy opened this issue · comments

Hi,

load_kube_config(&basePath, &sslConfig, &apiKeys, NULL); /* NULL means loading configuration from $HOME/.kube/config */

Here how can I connect to API sever without copying .kube/config file from the kubernetes master node.

Regards,
Arunreddy.

commented

Refer to #154 (comment) please.

Option 1 or 2 is what you need.

Actually I'm using the c API's from within the pod.

commented

Use load_incluster_config instead of load_kube_config if your program works within a pod.

 int rc = load_incluster_config(&basePath, &sslConfig, &apiKeys);

You don't need to copy the kube config as the c client library will authenticate for you by getting

/var/run/secrets/kubernetes.io/serviceaccount/token
/var/run/secrets/kubernetes.io/serviceaccount/ca.crt

in the pod.

An example:
https://github.com/kubernetes-client/c/blob/5ac5ff25e9809a92a48111b1f77574b6d040b711/examples/list_pod_incluster/main.c#L46C7-L46C7

Hi ,

I tried to run in cluster example but getting return value as 403.

root@ubuntu-test-6684b56c59-stdwv:/c/examples/list_pod_incluster# make
gcc main.c -g -I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api -I../../kubernetes/config -L../../kubernetes/build -lkubernetes -lyaml -lwebsockets -L/usr/local/lib -o list_pod_incluster_bin
root@ubuntu-test-6684b56c59-stdwv:
/c/examples/list_pod_incluster# ./list_pod_incluster_bin
The return code of HTTP request=403
Cannot get any pod.
root@ubuntu-test-6684b56c59-stdwv:~/c/examples/list_pod_incluster#

Regards,
Arunreddy.

commented

Can you try to debug the function load_incluster_config in your environment ?

Let's see what's wrong with it.

How to enable debugging:

c/README.md

Lines 46 to 47 in 5ac5ff2

# If you want to use `gdb` to debug the C client library, add `-DCMAKE_BUILD_TYPE=Debug` to the cmake command line, e.g.
# cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr/local ..

Hi,

checked with debug enabled.
load_incluster_config API returning 0 only,
curl request returned 403 .

commented

RBAC seems to restrict the access to the API server in the pod.
You can check to see if your service account has permissions.

Using RBAC I'm able a list pods and update pod labels.
using the load_incluster_config() API.
Thanks @ityuhui @brendandburns