-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeAgent_init.py
executable file
·94 lines (81 loc) · 3.7 KB
/
nodeAgent_init.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
#!/usr/bin/env python
# node-agent runs in host-network, and influxdb runs in pod-network.
# node-agent cannot communicate to influxdb using Service.
# This script will find Pod IP of each Pod and fill in /etc/node_agent/tasks.json.
import errno
import os
import sys
from jinja2 import Template
from kubernetes import client, config
class Accessor(object):
def env(self, env_name):
return os.environ[env_name]
def deployment_id(self):
"""Call kubernetes api container to retrieve the deployment id"""
try:
config.load_incluster_config()
nodes = client.CoreV1Api().list_node(watch=False)
if len(nodes.items) > 0:
return nodes.items[0].metadata.labels.get("hyperpilot/deployment", "")
except config.ConfigException:
print("Failed to load configuration. This container cannot run outside k8s.")
sys.exit(errno.EPERM)
def k8s_service(self, service_name, namespace='default'):
"""Call kubernetes api service to get service cluster ip"""
try:
config.load_incluster_config()
pod_service = client.CoreV1Api().read_namespaced_service(service_name, namespace)
cluster_ip = pod_service.spec.cluster_ip
port = pod_service.spec.ports[0].port
url = "http://%s:%s" % (cluster_ip, port)
print("Replacing k8s service %s to url %s" % (service_name, url))
return url
except config.ConfigException:
print("Failed to load configuration. This container cannot run outside k8s.")
sys.exit(errno.EPERM)
def pod_ip_label_selector(self, label_selector, namespace='default'):
"""Call kubernetes api service to get pod ip"""
try:
print("pod_ip_label_selector init label_selector: %s, namespace: %s" % (
label_selector, namespace))
config.load_incluster_config()
result = client.CoreV1Api().list_namespaced_pod(
namespace, label_selector=label_selector)
pod_name = result.items[0].metadata.name
pod = client.CoreV1Api().read_namespaced_pod(pod_name, namespace)
pod_ip = pod.status.pod_ip
print("pod_name: %s, label_selector: %s, namespace: %s, pod_ip: %s" % (
pod_name, label_selector, namespace, pod_ip))
if pod_ip is None:
print("Pod ip is not available, exit with error code 1")
sys.exit(errno.EPERM)
return pod_ip
except config.ConfigException:
print("Failed to load configuration. This container cannot run outside k8s.")
sys.exit(errno.EPERM)
def pod_ip_env_name(self, pod_env_name, namespace='default'):
"""Call kubernetes api service to get pod ip"""
try:
config.load_incluster_config()
label_selector = "app=%s" % os.environ[pod_env_name]
return self.pod_ip_label_selector(label_selector)
except config.ConfigException:
print("Failed to load configuration. This container cannot run outside k8s.")
sys.exit(errno.EPERM)
def main():
config_path = "/etc/node_agent/tasks.json"
with open(config_path, "r") as f:
# Double curly braces appears in json too often,
# so use <%= VAR => expression here instead
template = Template(f.read(),
variable_start_string="<%=",
variable_end_string="=>")
with open(config_path, "w") as f:
template_values = {
'a': Accessor(),
}
f.write(template.render(template_values))
# Success
sys.exit(0)
if __name__ == '__main__':
main()