Skip to content

Commit 7c3fb18

Browse files
committed
1 parent c61c45e commit 7c3fb18

3 files changed

Lines changed: 222 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Copyright 2017 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Simple example to show loading config from the cluster
16+
17+
#
18+
# If you get 403 errors from API server you will have to configure
19+
# RBAC to add necessay permissions.
20+
#
21+
# ---
22+
# kind: ClusterRole
23+
# apiVersion: rbac.authorization.k8s.io/v1
24+
# metadata:
25+
# name: pods-list
26+
# rules:
27+
# - apiGroups: [""]
28+
# resources: ["pods"]
29+
# verbs: ["list"]
30+
# ---
31+
# kind: ClusterRoleBinding
32+
# apiVersion: rbac.authorization.k8s.io/v1
33+
# metadata:
34+
# name: pods-list
35+
# subjects:
36+
# - kind: ServiceAccount
37+
# name: default
38+
# namespace: default
39+
# roleRef:
40+
# kind: ClusterRole
41+
# name: pods-list
42+
# apiGroup: rbac.authorization.k8s.io
43+
# ---
44+
#
45+
# Doc: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
46+
#
47+
# This example can be run from the image: https://hub.docker.com/r/tpimages/kubernetes_asyncio_examples/
48+
#
49+
# $ kubectl run kubernetes-asyncio-examples --image tpimages/kubernetes_asyncio_examples
50+
#
51+
52+
import asyncio
53+
import sys
54+
import traceback
55+
56+
from kubernetes_asyncio import client, config
57+
58+
59+
async def main():
60+
61+
while True:
62+
63+
try:
64+
65+
# it works only if this script is run by K8s as a POD
66+
config.load_incluster_config()
67+
68+
v1 = client.CoreV1Api()
69+
print("Listing pods with their IPs:")
70+
ret = await v1.list_pod_for_all_namespaces()
71+
72+
for i in ret.items:
73+
print(i.status.pod_ip, i.metadata.namespace, i.metadata.name)
74+
75+
except Exception:
76+
traceback.print_exc(file=sys.stdout)
77+
78+
finally:
79+
print("sleep 10s")
80+
await asyncio.sleep(10)
81+
82+
83+
if __name__ == "__main__":
84+
loop = asyncio.get_event_loop()
85+
loop.run_until_complete(main())
86+
loop.close()

examples_asyncio/list_pods.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import asyncio
2+
3+
from kubernetes_asyncio import client, config
4+
from kubernetes_asyncio.client.api_client import ApiClient
5+
6+
7+
async def main():
8+
# Configs can be set in Configuration class directly or using helper
9+
# utility. If no argument provided, the config will be loaded from
10+
# default location.
11+
await config.load_kube_config()
12+
13+
# use the context manager to close http sessions automatically
14+
async with ApiClient() as api:
15+
16+
v1 = client.CoreV1Api(api)
17+
print("Listing pods with their IPs:")
18+
ret = await v1.list_pod_for_all_namespaces()
19+
20+
for i in ret.items:
21+
print(i.status.pod_ip, i.metadata.namespace, i.metadata.name)
22+
23+
24+
if __name__ == "__main__":
25+
loop = asyncio.get_event_loop()
26+
loop.run_until_complete(main())
27+
loop.close()

examples_asyncio/patch.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import asyncio
2+
3+
from kubernetes_asyncio import client, config
4+
from kubernetes_asyncio.client.api_client import ApiClient
5+
6+
SERVICE_NAME = "example-service"
7+
SERVICE_NS = "default"
8+
SERVICE_SPEC = {
9+
"apiVersion": "v1",
10+
"kind": "Service",
11+
"metadata": {
12+
"labels": {"name": SERVICE_NAME},
13+
"name": SERVICE_NAME,
14+
"resourceversion": "v1",
15+
},
16+
"spec": {
17+
"ports": [{"name": "port-80", "port": 80, "protocol": "TCP", "targetPort": 80}],
18+
"selector": {"name": SERVICE_NAME},
19+
},
20+
}
21+
22+
23+
async def main():
24+
25+
await config.load_kube_config()
26+
27+
async with ApiClient() as api:
28+
29+
v1 = client.CoreV1Api(api)
30+
31+
print(f"Recreate {SERVICE_NAME}...")
32+
try:
33+
await v1.read_namespaced_service(SERVICE_NAME, SERVICE_NS)
34+
await v1.delete_namespaced_service(SERVICE_NAME, SERVICE_NS)
35+
except client.exceptions.ApiException as ex:
36+
if ex.status == 404:
37+
pass
38+
39+
await v1.create_namespaced_service(SERVICE_NS, SERVICE_SPEC)
40+
41+
print("Patch using JSON patch - replace port-80 with port-1000")
42+
patch = [
43+
{
44+
"op": "replace",
45+
"path": "/spec/ports/0",
46+
"value": {
47+
"name": "port-1000",
48+
"protocol": "TCP",
49+
"port": 1000,
50+
"targetPort": 1000,
51+
},
52+
}
53+
]
54+
await v1.patch_namespaced_service(
55+
SERVICE_NAME,
56+
SERVICE_NS,
57+
patch,
58+
# _content_type='application/json-patch+json' # (optional, default if patch is a list)
59+
)
60+
61+
print(
62+
"Patch using strategic merge patch - add port-2000, service will have two ports: port-1000 and port-2000"
63+
)
64+
patch = {
65+
"spec": {
66+
"ports": [
67+
{
68+
"name": "port-2000",
69+
"protocol": "TCP",
70+
"port": 2000,
71+
"targetPort": 2000,
72+
}
73+
]
74+
}
75+
}
76+
await v1.patch_namespaced_service(
77+
SERVICE_NAME,
78+
SERVICE_NS,
79+
patch,
80+
# _content_type='application/strategic-merge-patch+json' # (optional, default if patch is a dict)
81+
)
82+
83+
print(
84+
"Patch using merge patch - recreate list of ports, service will have only one port: port-3000"
85+
)
86+
patch = {
87+
"spec": {
88+
"ports": [
89+
{
90+
"name": "port-3000",
91+
"protocol": "TCP",
92+
"port": 3000,
93+
"targetPort": 3000,
94+
}
95+
]
96+
}
97+
}
98+
await v1.patch_namespaced_service(
99+
SERVICE_NAME,
100+
SERVICE_NS,
101+
patch,
102+
_content_type="application/merge-patch+json", # required to force merge patch
103+
)
104+
105+
106+
if __name__ == "__main__":
107+
loop = asyncio.get_event_loop()
108+
loop.run_until_complete(main())
109+
loop.close()

0 commit comments

Comments
 (0)