|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -# Simple example to show loading config from the cluster |
16 |
| -# |
17 |
| -# It works only from a pod. You can start an image with Python |
18 |
| -# (for example python:latest), exec into the pod, install the library, |
19 |
| -# then try out this example. |
20 |
| -# |
21 |
| -# If you get 403 errors from API server you will have to configure |
22 |
| -# RBAC to add the permission to list pods. |
23 |
| -# |
24 |
| -# --- |
25 |
| -# kind: ClusterRole |
26 |
| -# apiVersion: rbac.authorization.k8s.io/v1 |
27 |
| -# metadata: |
28 |
| -# name: pods-list |
29 |
| -# rules: |
30 |
| -# - apiGroups: [""] |
31 |
| -# resources: ["pods"] |
32 |
| -# verbs: ["list"] |
33 |
| -# --- |
34 |
| -# kind: ClusterRoleBinding |
35 |
| -# apiVersion: rbac.authorization.k8s.io/v1 |
36 |
| -# metadata: |
37 |
| -# name: pods-list |
38 |
| -# subjects: |
39 |
| -# - kind: ServiceAccount |
40 |
| -# name: default |
41 |
| -# namespace: default |
42 |
| -# roleRef: |
43 |
| -# kind: ClusterRole |
44 |
| -# name: pods-list |
45 |
| -# apiGroup: rbac.authorization.k8s.io |
46 |
| -# --- |
47 |
| -# |
48 |
| -# Doc: https://kubernetes.io/docs/reference/access-authn-authz/rbac/ |
| 15 | +""" |
| 16 | +Showcases loading the Kubernetes config from within the cluster. This script |
| 17 | +must be run within a pod. You can start a pod with a Python image (for |
| 18 | +example, `python:latest`), exec into the pod, install the library, then run |
| 19 | +this example. |
| 20 | +
|
| 21 | +If you get 403 errors from the API server you will have to configure RBAC to |
| 22 | +add the permission to list pods by applying the following manifest: |
| 23 | +
|
| 24 | +--- |
| 25 | +kind: ClusterRole |
| 26 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 27 | +metadata: |
| 28 | + name: pods-list |
| 29 | +rules: |
| 30 | +- apiGroups: [""] |
| 31 | + resources: ["pods"] |
| 32 | + verbs: ["list"] |
| 33 | +
|
| 34 | +--- |
| 35 | +kind: ClusterRoleBinding |
| 36 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 37 | +metadata: |
| 38 | + name: pods-list |
| 39 | +subjects: |
| 40 | +- kind: ServiceAccount |
| 41 | + name: default |
| 42 | + namespace: default |
| 43 | +roleRef: |
| 44 | + kind: ClusterRole |
| 45 | + name: pods-list |
| 46 | + apiGroup: rbac.authorization.k8s.io |
| 47 | +
|
| 48 | +Documentation: https://kubernetes.io/docs/reference/access-authn-authz/rbac/ |
| 49 | +""" |
49 | 50 |
|
50 | 51 | from kubernetes import client, config
|
51 | 52 |
|
52 | 53 |
|
53 | 54 | def main():
|
54 |
| - |
55 |
| - # it works only if this script is run by K8s as a POD |
56 | 55 | config.load_incluster_config()
|
57 | 56 |
|
58 | 57 | v1 = client.CoreV1Api()
|
|
0 commit comments