Skip to content

Instantly share code, notes, and snippets.

@linnil1
Last active October 4, 2018 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linnil1/9795c20bec2065adc8306ee57093fdae to your computer and use it in GitHub Desktop.
Save linnil1/9795c20bec2065adc8306ee57093fdae to your computer and use it in GitHub Desktop.
Use PYTHON API in Kubernetes pod
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: myapi-k8s
rules:
- apiGroups: [""] # "" indicates the core API group
resources: ["pods"]
verbs: ["get", "watch", "list"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: myapi-k8s
subjects:
- kind: ServiceAccount
name: myapi-k8s
namespace: default
roleRef:
kind: ClusterRole
name: myapi-k8s
apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: myapi-k8s
---
apiVersion: v1
kind: Pod
metadata:
name: test-k8s
spec:
serviceAccountName: myapi-k8s
containers:
- name: test-k8s
image: python:3.7-slim
command: [tail]
args: ['-f', '/dev/null']
from kubernetes import client, config
config.load_incluster_config()
v1 = client.CoreV1Api()
ret = v1.list_pod_for_all_namespaces(watch=False)
print(ret.items[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment