-
Notifications
You must be signed in to change notification settings - Fork 0
/
replica.py
166 lines (145 loc) · 7.14 KB
/
replica.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import os
from kubernetes import client
import settings.settings as settings
import logging
logger = logging.getLogger(os.path.basename(__file__))
class Replica:
"""
This class defines the properties and the behavior of one replica in the cluster.
"""
def __init__(self, uid, replica_name, replica_type, job_name, template, **kwargs):
# environment_variables=None, volumes=None, ports=None
self.uid = uid
self.replica_name = replica_name
self.replica_type = replica_type
self.job_name = job_name
self.template = template
self.status = "None"
self.scheduler_ip = ""
# TODO: create a function to check these parameters
self.container_params = kwargs
# self.environment_variables = kwargs['environment_variables']
# self.volumes = kwargs['volumes']
# self.ports = kwargs['ports']
self.api_instance = client.CoreV1Api()
def reconcile(self):
"""Check the current status of the replica and match it against the desired state.
"""
logger.info(f"Reconcile replica {self.replica_name} with type {self.replica_type}")
# check if pod with current replica name already exists using selector with replica name
# selector = client.V1LabelSelector(match_labels={"pod_name": self.replica_name})
selector = f"pod_name={self.replica_name}"
# returns a V1PodList object
pod_list = self.api_instance.list_namespaced_pod(settings.NAMESPACE, label_selector=selector)
if len(pod_list.items) == 0:
self.create_replica()
elif len(pod_list.items) > 1:
# this should not happen. The operator should make sure that a single
# pod with that label name is created.
assert False
else:
# TODO: should check the status of the replica here
pod = pod_list.items[0]
assert pod.metadata.labels['pod_name'] == self.replica_name
logger.info("Replica pod already exist. Checking the current status...")
pass
def create_replica(self):
self.create_pod()
# in case the pod needs to expose some ports
if 'ports' in self.container_params:
self.create_service(ports=self.container_params['ports'])
def create_pod(self):
pod = client.V1Pod()
pod.metadata = client.V1ObjectMeta(
name=self.replica_name,
labels={"pod_name": self.replica_name,
"job_name": self.job_name})
container_spec = self.template["spec"]["containers"][0]
# add container properties to container spec
if 'env' in self.container_params:
# add env variables to container_spec
container_spec['env'] = [
client.V1EnvVar(name=k, value=str(v))
for k, v in self.container_params['env'].items()
]
if 'volumes' in self.container_params:
# add volumes spec to container
# TODO: look at how to mount a volume (volumeMounts vs volumeDevices?)
container_spec['volumeMounts'] = []
# V1Container accepts keys defined in V1Container.attribute_map.
# The current spec is defined with camel case keys, so we need to map back
# to underscore keys.
inv_map = {
k: container_spec[v]
for k, v in client.V1Container.attribute_map.items()
if v in container_spec
}
pod_spec = client.V1PodSpec(containers=[client.V1Container(**inv_map)], hostname=self.replica_name)
pod.spec = pod_spec
self.api_instance.create_namespaced_pod(namespace=settings.NAMESPACE, body=pod)
logger.info(f"Created Pod for replica {self.replica_name} with container spec {inv_map}")
# TODO: Need to solve the Hostname resolve. Now only solution is to use direct IP address.
# if scheduler, we need to get scheduler IP address and inject it in the other pods.
if self.replica_type == "SCHEDULER":
selector = f"pod_name={self.replica_name}"
pod_list = client.models.v1_pod_list.V1PodList(items=[])
while len(pod_list.items) == 0:
pod_list = self.api_instance.list_namespaced_pod(settings.NAMESPACE, label_selector=selector)
self.scheduler_ip = pod_list.items[0].status.pod_ip
def create_service(self, ports):
"""Creates an tandem network service for this replica.
Args:
ports: The ports to be exposed to the other replicas
"""
service = client.V1Service()
service.api_version = "v1"
service.kind = "Service"
service.metadata = client.V1ObjectMeta(name=f"{self.replica_name}-service",
labels={"service_name": f"{self.replica_name}-service",
"job_name": self.job_name}
)
# Set Service object to target port on any Pod with that label.
# The label selection allows Kubernetes to determine which Pod
# should receive traffic when the service is used.
spec = client.V1ServiceSpec()
spec.selector = {"pod_name": self.replica_name}
spec.ports = [
client.V1ServicePort(name="dlport{}".format(i), port=port)
for i, port in enumerate(ports)
]
service.spec = spec
self.api_instance.create_namespaced_service(namespace=settings.NAMESPACE, body=service)
def clean_up(self):
logger.info(f"Deleting pod {self.replica_name}...")
selector = f"pod_name={self.replica_name}"
pod_list = self.api_instance.list_namespaced_pod(settings.NAMESPACE, label_selector=selector)
if len(pod_list.items) == 0:
logging.warning(f"No pod with name {self.replica_name} found during clean up")
return
if len(pod_list.items) > 1:
logging.warning(f"Multiple pods found with labels name {self.replica_name} found during clean up")
assert False
self.api_instance.delete_namespaced_pod(
self.replica_name,
settings.NAMESPACE,
body=client.V1DeleteOptions())
logger.info(f"Deleting service {self.replica_name}-service...")
selector = f"service_name={self.replica_name}-service"
service_list = self.api_instance.list_namespaced_service(settings.NAMESPACE, label_selector=selector)
if len(service_list.items) == 0:
logging.warning(f"No service with name {self.replica_name}-service found during clean up")
return
if len(service_list.items) > 1:
logging.warning(
f"Multiple services found with labels name {self.replica_name}-service found during clean up")
assert False
self.api_instance.delete_namespaced_service(
f"{self.replica_name}-service",
settings.NAMESPACE,
body=client.V1DeleteOptions())
def get_uid(self):
"""Getter function for the `uid` of this replica
Returns:
Int. The unique `uid` of this replica
"""
return self.uid