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