Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0fe0f07

Browse files
committed
Added examples for Jobs and Hooks
1 parent 8db79d0 commit 0fe0f07

28 files changed

+107
-119
lines changed

examples/__init__.py

100644100755
File mode changed.

examples/create_deployment.py

100644100755
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ def main():
2929
dep = yaml.safe_load(f)
3030
k8s_beta = client.ExtensionsV1beta1Api()
3131
resp = k8s_beta.create_namespaced_deployment(
32-
body=dep,
33-
namespace="default")
32+
body=dep, namespace="default")
3433
print("Deployment created. status='%s'" % str(resp.status))
3534

3635

37-
if __name__ == "__main__":
36+
if __name__ == '__main__':
3837
main()

examples/create_deployment_from_yaml.py

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ def main():
2929
print("Deployment {0} created".format(deps.metadata.name))
3030

3131

32-
if __name__ == "__main__":
32+
if __name__ == '__main__':
3333
main()

examples/create_thirdparty_resource.md

100644100755
File mode changed.

examples/deployment_examples.py

100644100755
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ def create_deployment_object():
2626
container = client.V1Container(
2727
name="nginx",
2828
image="nginx:1.7.9",
29-
ports=[client.V1ContainerPort(container_port=80)],
30-
)
29+
ports=[client.V1ContainerPort(container_port=80)])
3130
# Create and configurate a spec section
3231
template = client.V1PodTemplateSpec(
3332
metadata=client.V1ObjectMeta(labels={"app": "nginx"}),
34-
spec=client.V1PodSpec(containers=[container]),
35-
)
33+
spec=client.V1PodSpec(containers=[container]))
3634
# Create the specification of deployment
3735
spec = client.ExtensionsV1beta1DeploymentSpec(
3836
replicas=3,
@@ -42,17 +40,16 @@ def create_deployment_object():
4240
api_version="extensions/v1beta1",
4341
kind="Deployment",
4442
metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME),
45-
spec=spec,
46-
)
43+
spec=spec)
4744

4845
return deployment
4946

5047

5148
def create_deployment(api_instance, deployment):
5249
# Create deployement
5350
api_response = api_instance.create_namespaced_deployment(
54-
body=deployment, namespace="default"
55-
)
51+
body=deployment,
52+
namespace="default")
5653
print("Deployment created. status='%s'" % str(api_response.status))
5754

5855

@@ -61,8 +58,9 @@ def update_deployment(api_instance, deployment):
6158
deployment.spec.template.spec.containers[0].image = "nginx:1.9.1"
6259
# Update the deployment
6360
api_response = api_instance.patch_namespaced_deployment(
64-
name=DEPLOYMENT_NAME, namespace="default", body=deployment
65-
)
61+
name=DEPLOYMENT_NAME,
62+
namespace="default",
63+
body=deployment)
6664
print("Deployment updated. status='%s'" % str(api_response.status))
6765

6866

@@ -72,9 +70,8 @@ def delete_deployment(api_instance):
7270
name=DEPLOYMENT_NAME,
7371
namespace="default",
7472
body=client.V1DeleteOptions(
75-
propagation_policy="Foreground", grace_period_seconds=5
76-
),
77-
)
73+
propagation_policy='Foreground',
74+
grace_period_seconds=5))
7875
print("Deployment deleted. status='%s'" % str(api_response.status))
7976

8077

@@ -95,5 +92,5 @@ def main():
9592
delete_deployment(extensions_v1beta1)
9693

9794

98-
if __name__ == "__main__":
95+
if __name__ == '__main__':
9996
main()

examples/example1.py

100644100755
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ def main():
2525
print("Listing pods with their IPs:")
2626
ret = v1.list_pod_for_all_namespaces(watch=False)
2727
for i in ret.items:
28-
print("%s\t%s\t%s" % (i.status.pod_ip,
29-
i.metadata.namespace,
30-
i.metadata.name))
28+
print("%s\t%s\t%s" %
29+
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
3130

3231

33-
if __name__ == "__main__":
32+
if __name__ == '__main__':
3433
main()

examples/example2.py

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ def main():
2525
count = 10
2626
w = watch.Watch()
2727
for event in w.stream(v1.list_namespace, timeout_seconds=10):
28-
print("Event: %s %s" % (event["type"], event["object"].metadata.name))
28+
print("Event: %s %s" % (event['type'], event['object'].metadata.name))
2929
count -= 1
3030
if not count:
3131
w.stop()
3232

3333
print("Ended.")
3434

3535

36-
if __name__ == "__main__":
36+
if __name__ == '__main__':
3737
main()

examples/example3.py

100644100755
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,19 @@ def main():
2222
config.load_kube_config()
2323

2424
print("Supported APIs (* is preferred version):")
25-
print("%-20s %s" % ("core",
26-
",".join(client.CoreApi()
27-
.get_api_versions()
28-
.versions)))
25+
print("%-20s %s" %
26+
("core", ",".join(client.CoreApi().get_api_versions().versions)))
2927
for api in client.ApisApi().get_api_versions().groups:
3028
versions = []
3129
for v in api.versions:
3230
name = ""
33-
if v.version == api.preferred_version.version and \
34-
len(api.versions) > 1:
31+
if v.version == api.preferred_version.version and len(
32+
api.versions) > 1:
3533
name += "*"
3634
name += v.version
3735
versions.append(name)
3836
print("%-40s %s" % (api.name, ",".join(versions)))
3937

4038

41-
if __name__ == "__main__":
39+
if __name__ == '__main__':
4240
main()

examples/example4.py

100644100755
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from kubernetes import client, config
1616
from kubernetes.client import configuration
17-
1817
# install pick using "pip install pick". It is not included
1918
# as a dependency because it only used in examples
2019
from pick import pick
@@ -25,11 +24,10 @@ def main():
2524
if not contexts:
2625
print("Cannot find any context in kube-config file.")
2726
return
28-
contexts = [context["name"] for context in contexts]
29-
active_index = contexts.index(active_context["name"])
30-
option, _ = pick(
31-
contexts, title="Pick the context to load", default_index=active_index
32-
)
27+
contexts = [context['name'] for context in contexts]
28+
active_index = contexts.index(active_context['name'])
29+
option, _ = pick(contexts, title="Pick the context to load",
30+
default_index=active_index)
3331
# Configs can be set in Configuration class directly or using helper
3432
# utility
3533
config.load_kube_config(context=option)
@@ -41,10 +39,11 @@ def main():
4139
ret = v1.list_pod_for_all_namespaces(watch=False)
4240
for item in ret.items:
4341
print(
44-
"%s\t%s\t%s"
45-
% (item.status.pod_ip, item.metadata.namespace, item.metadata.name)
46-
)
42+
"%s\t%s\t%s" %
43+
(item.status.pod_ip,
44+
item.metadata.namespace,
45+
item.metadata.name))
4746

4847

49-
if __name__ == "__main__":
48+
if __name__ == '__main__':
5049
main()

examples/exec.py

100644100755
Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
c.assert_hostname = False
1212
Configuration.set_default(c)
1313
api = core_v1_api.CoreV1Api()
14-
name = "busybox-test"
14+
name = 'busybox-test'
1515

1616
resp = None
1717
try:
18-
resp = api.read_namespaced_pod(name=name, namespace="default")
18+
resp = api.read_namespaced_pod(name=name,
19+
namespace='default')
1920
except ApiException as e:
2021
if e.status != 404:
2122
print("Unknown error: %s" % e)
@@ -24,62 +25,56 @@
2425
if not resp:
2526
print("Pod %s does not exist. Creating it..." % name)
2627
pod_manifest = {
27-
"apiVersion": "v1",
28-
"kind": "Pod",
29-
"metadata": {"name": name},
30-
"spec": {
31-
"containers": [
32-
{
33-
"image": "busybox",
34-
"name": "sleep",
35-
"args": ["/bin/sh",
36-
"-c",
37-
"while true;do date;sleep 5; done"],
38-
}
39-
]
28+
'apiVersion': 'v1',
29+
'kind': 'Pod',
30+
'metadata': {
31+
'name': name
4032
},
33+
'spec': {
34+
'containers': [{
35+
'image': 'busybox',
36+
'name': 'sleep',
37+
"args": [
38+
"/bin/sh",
39+
"-c",
40+
"while true;do date;sleep 5; done"
41+
]
42+
}]
43+
}
4144
}
42-
resp = api.create_namespaced_pod(body=pod_manifest, namespace="default")
45+
resp = api.create_namespaced_pod(body=pod_manifest,
46+
namespace='default')
4347
while True:
44-
resp = api.read_namespaced_pod(name=name, namespace="default")
45-
if resp.status.phase != "Pending":
48+
resp = api.read_namespaced_pod(name=name,
49+
namespace='default')
50+
if resp.status.phase != 'Pending':
4651
break
4752
time.sleep(1)
4853
print("Done.")
4954

5055

5156
# calling exec and wait for response.
5257
exec_command = [
53-
"/bin/sh",
54-
"-c",
55-
"echo This message goes to stderr >&2; echo This message goes to stdout",
56-
]
57-
resp = stream(
58-
api.connect_get_namespaced_pod_exec,
59-
name,
60-
"default",
61-
command=exec_command,
62-
stderr=True,
63-
stdin=False,
64-
stdout=True,
65-
tty=False,
66-
)
58+
'/bin/sh',
59+
'-c',
60+
'echo This message goes to stderr >&2; echo This message goes to stdout']
61+
resp = stream(api.connect_get_namespaced_pod_exec, name, 'default',
62+
command=exec_command,
63+
stderr=True, stdin=False,
64+
stdout=True, tty=False)
6765
print("Response: " + resp)
6866

6967
# Calling exec interactively.
70-
exec_command = ["/bin/sh"]
71-
resp = stream(
72-
api.connect_get_namespaced_pod_exec,
73-
name,
74-
"default",
75-
command=exec_command,
76-
stderr=True,
77-
stdin=True,
78-
stdout=True,
79-
tty=False,
80-
_preload_content=False,
81-
)
82-
commands = ["echo test1", 'echo "This message goes to stderr" >&2']
68+
exec_command = ['/bin/sh']
69+
resp = stream(api.connect_get_namespaced_pod_exec, name, 'default',
70+
command=exec_command,
71+
stderr=True, stdin=True,
72+
stdout=True, tty=False,
73+
_preload_content=False)
74+
commands = [
75+
"echo test1",
76+
"echo \"This message goes to stderr\" >&2",
77+
]
8378
while resp.is_open():
8479
resp.update(timeout=1)
8580
if resp.peek_stdout():

examples/in_cluster_config.py

100644100755
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ def main():
5959
print("Listing pods with their IPs:")
6060
ret = v1.list_pod_for_all_namespaces(watch=False)
6161
for i in ret.items:
62-
print("%s\t%s\t%s" % (i.status.pod_ip,
63-
i.metadata.namespace,
64-
i.metadata.name))
62+
print("%s\t%s\t%s" %
63+
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
6564

6665

67-
if __name__ == "__main__":
66+
if __name__ == '__main__':
6867
main()

examples/manage_node_labels.py

100644100755
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ def main():
2929

3030
api_instance = client.CoreV1Api()
3131

32-
body = {"metadata": {"labels": {"foo": "bar", "baz": None}}}
32+
body = {
33+
"metadata": {
34+
"labels": {
35+
"foo": "bar",
36+
"baz": None}
37+
}
38+
}
3339

3440
api_response = api_instance.patch_node("minikube", body)
3541

3642
pprint(api_response)
3743

3844

39-
if __name__ == "__main__":
45+
if __name__ == '__main__':
4046
main()

examples/multiple_clusters.py

100644100755
Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
from kubernetes import client, config
16-
1716
# install pick using "pip install pick". It is not included
1817
# as a dependency because it only used in examples
1918
from pick import pick
@@ -25,34 +24,28 @@ def main():
2524
if not contexts:
2625
print("Cannot find any context in kube-config file.")
2726
return
28-
contexts = [context["name"] for context in contexts]
29-
active_index = contexts.index(active_context["name"])
30-
cluster1, first_index = pick(
31-
contexts, title="Pick the first context", default_index=active_index
32-
)
33-
cluster2, _ = pick(
34-
contexts, title="Pick the second context", default_index=first_index
35-
)
27+
contexts = [context['name'] for context in contexts]
28+
active_index = contexts.index(active_context['name'])
29+
cluster1, first_index = pick(contexts, title="Pick the first context",
30+
default_index=active_index)
31+
cluster2, _ = pick(contexts, title="Pick the second context",
32+
default_index=first_index)
3633

3734
client1 = client.CoreV1Api(
38-
api_client=config.new_client_from_config(context=cluster1)
39-
)
35+
api_client=config.new_client_from_config(context=cluster1))
4036
client2 = client.CoreV1Api(
41-
api_client=config.new_client_from_config(context=cluster2)
42-
)
37+
api_client=config.new_client_from_config(context=cluster2))
4338

4439
print("\nList of pods on %s:" % cluster1)
4540
for i in client1.list_pod_for_all_namespaces().items:
46-
print("%s\t%s\t%s" % (i.status.pod_ip,
47-
i.metadata.namespace,
48-
i.metadata.name))
41+
print("%s\t%s\t%s" %
42+
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
4943

5044
print("\n\nList of pods on %s:" % cluster2)
5145
for i in client2.list_pod_for_all_namespaces().items:
52-
print("%s\t%s\t%s" % (i.status.pod_ip,
53-
i.metadata.namespace,
54-
i.metadata.name))
46+
print("%s\t%s\t%s" %
47+
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
5548

5649

57-
if __name__ == "__main__":
50+
if __name__ == '__main__':
5851
main()

examples/nginx-deployment.yaml

100644100755
File mode changed.

examples/notebooks/README.md

100644100755
File mode changed.

examples/notebooks/create_configmap.ipynb

100644100755
File mode changed.

examples/notebooks/create_deployment.ipynb

100644100755
File mode changed.

examples/notebooks/create_pod.ipynb

100644100755
File mode changed.

examples/notebooks/create_secret.ipynb

100644100755
File mode changed.

examples/notebooks/create_service.ipynb

100644100755
File mode changed.

examples/notebooks/docker/Dockerfile

100644100755
File mode changed.

examples/notebooks/docker/jupyter.yml

100644100755
File mode changed.

examples/notebooks/intro_notebook.ipynb

100644100755
File mode changed.

examples/notebooks/test.ipynb

100644100755
File mode changed.

examples/notebooks/watch_notebook.ipynb

100644100755
File mode changed.

examples/post_start_hook.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@
4444
extension.create_namespaced_deployment(
4545
namespace="kube-client",
4646
body=deployment)
47+
48+
if __name__ == "__main__":
49+
main()

0 commit comments

Comments
 (0)