Invalid kube-config file. No configuration found when i use kubernetes client python in pod

Viewed 9172

I am trying to get kubernetes api server from a pod.

Here is my pod configuration

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: test
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: test
            image: test:v5
            env:
            imagePullPolicy: IfNotPresent
            command: ['python3']
            args: ['test.py']
          restartPolicy: OnFailure

And here is my kubernetes-client python code inside test.py

from kubernetes import client, config

# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()

v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

But i am getting this error:

kubernetes.config.config_exception.ConfigException: Invalid kube-config file. No configuration found.
1 Answers

When operating in-cluster you want load_incluster_config() instead.

Related