Skip to content

Commit 3165e80

Browse files
authored
Merge pull request kubernetes-client#1843 from showjason/annotate-deployment
example annotate deployment
2 parents 0986337 + cc03f4e commit 3165e80

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

examples/annotate_deployment.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
This example covers the following:
3+
- Create deployment
4+
- Annotate deployment
5+
"""
6+
7+
8+
from kubernetes import client, config
9+
import time
10+
11+
12+
def create_deployment_object():
13+
container = client.V1Container(
14+
name="nginx-sample",
15+
image="nginx",
16+
image_pull_policy="IfNotPresent",
17+
ports=[client.V1ContainerPort(container_port=80)],
18+
)
19+
# Template
20+
template = client.V1PodTemplateSpec(
21+
metadata=client.V1ObjectMeta(labels={"app": "nginx"}),
22+
spec=client.V1PodSpec(containers=[container]))
23+
# Spec
24+
spec = client.V1DeploymentSpec(
25+
replicas=1,
26+
selector=client.V1LabelSelector(
27+
match_labels={"app": "nginx"}
28+
),
29+
template=template)
30+
# Deployment
31+
deployment = client.V1Deployment(
32+
api_version="apps/v1",
33+
kind="Deployment",
34+
metadata=client.V1ObjectMeta(name="deploy-nginx"),
35+
spec=spec)
36+
37+
return deployment
38+
39+
40+
def create_deployment(apps_v1_api, deployment_object):
41+
# Create the Deployment in default namespace
42+
# You can replace the namespace with you have created
43+
apps_v1_api.create_namespaced_deployment(
44+
namespace="default", body=deployment_object
45+
)
46+
47+
48+
def annotate_deployment(apps_v1_api, deployment_name, annotations):
49+
# Annotate the Deployment in default namespace
50+
# You can replace the namespace with you have created
51+
apps_v1_api.patch_namespaced_deployment(
52+
name=deployment_name, namespace='default', body=annotations)
53+
54+
55+
def main():
56+
# Loading the local kubeconfig
57+
config.load_kube_config()
58+
apps_v1_api = client.AppsV1Api()
59+
deployment_obj = create_deployment_object()
60+
61+
create_deployment(apps_v1_api, deployment_obj)
62+
time.sleep(1)
63+
before_annotating = apps_v1_api.read_namespaced_deployment(
64+
'deploy-nginx', 'default')
65+
print('Before annotating, annotations: %s' %
66+
before_annotating.metadata.annotations)
67+
68+
annotations = [
69+
{
70+
'op': 'add', # You can try different operations like 'replace', 'add' and 'remove'
71+
'path': '/metadata/annotations',
72+
'value': {
73+
'deployment.kubernetes.io/str': 'nginx',
74+
'deployment.kubernetes.io/int': '5'
75+
}
76+
}
77+
]
78+
79+
annotate_deployment(apps_v1_api, 'deploy-nginx', annotations)
80+
time.sleep(1)
81+
after_annotating = apps_v1_api.read_namespaced_deployment(
82+
name='deploy-nginx', namespace='default')
83+
print('After annotating, annotations: %s' %
84+
after_annotating.metadata.annotations)
85+
86+
87+
if __name__ == "__main__":
88+
main()

0 commit comments

Comments
 (0)