-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkubernetes_api.py
174 lines (161 loc) · 5.09 KB
/
kubernetes_api.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
167
168
169
170
171
172
173
174
# --*-- coding:utf-8 --*--
from flask import Flask,g, request
import json,os,sys
from node_labels import label_node
from lists_ns import list_namespace
import logging
from lists_pod import list_pods
from deploy_create import create_deployment
from service_create import create_service
from update_pod_image import update_image
from update_pod_replicas import update_replicas
from deploy_delete import delete_deployment
from service_delete import delete_service
try:
import http.client as http_client
except ImportError:
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1
app = Flask(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.DEBUG)
@app.route('/label',methods=['POST'])
def node_label_k8s():
try:
data = json.loads(request.data)
g.key = data.get('key')
g.value = data.get('value')
g.node = data.get('node')
if g.key == '' or g.value == '' or g.node == '':
return('Make sure the POST value is correct!\n')
else:
label_node(g.node)
except Exception as e:
logging.error(e)
print(e)
return('OK\n')
@app.route('/ns-list',methods=['POST'])
def list_ns_k8s():
data = json.loads(request.data)
g.key = data.get('key')
g.value = data.get('value')
if g.key != 'get' or g.value != 'ns':
return('Make sure the POST value is correct!\n')
else:
return(list_namespace() + '\n')
@app.route('/pods-list',methods=['POST'])
def list_pods_k8s():
data = json.loads(request.data)
g.namespace = data.get('ns')
return(list_pods(g.namespace))
@app.route('/deploy-create',methods=['POST'])
def create_deployment_k8s():
try:
data = json.loads(request.data)
g.ns = data.get('ns')
g.name = data.get('name')
g.replicas = data.get('replicas')
g.image = data.get('image')
if g.ns == '':
g.ns = 'default'
if g.name == '' or g.replicas == '' or g.image == '':
return('Make sure the POST value is correct!\n')
else:
create_deployment()
except Exception as e:
logging.error(e)
print(e)
return('OK\n')
@app.route('/service-create',methods=['POST'])
def create_service_k8s():
try:
data = json.loads(request.data)
g.ns = data.get('ns')
g.name = data.get('name')
g.port = data.get('port')
g.nodeport = data.get('nodeport')
g.model = data.get('model')
if g.model == '':
g.model = 'ClusterIP'
if g.ns == '':
g.ns = 'default'
if g.name == '' or g.port == '' or g.nodeport == '':
return('Make sure the POST value is correct!\n')
else:
create_service(g.ns,g.name,g.port,g.nodeport,g.model)
except Exception as e:
logging.error(e)
print(e)
return('OK\n')
@app.route('/update-image',methods=['POST'])
def update_image_k8s():
try:
data = json.loads(request.data)
g.ns = data.get('ns')
g.name = data.get('name')
g.image = data.get('image')
if g.ns == '':
g.ns = 'default'
if g.name == '' or g.image == '':
return('Make sure the POST value is correct!\n')
else:
update_image(g.ns,g.name,g.image)
except Exception as e:
logging.error(e)
print(e)
return('OK\n')
@app.route('/update-replicas',methods=['POST'])
def update_replicas_k8s():
try:
data = json.loads(request.data)
g.ns = data.get('ns')
g.name = data.get('name')
g.replicas = data.get('replicas')
if g.ns == '':
g.ns = 'default'
if g.name == '' or g.replicas == '':
return('Make sure the POST value is correct!\n')
else:
update_replicas(g.ns,g.name,g.replicas)
except Exception as e:
logging.error(e)
print(e)
return('OK\n')
@app.route('/delete-deploy',methods=['POST'])
def delete_deploy_k8s():
try:
data = json.loads(request.data)
g.ns = data.get('ns')
g.name = data.get('name')
if g.ns == '':
g.ns = 'default'
if g.name == '':
return('Make sure the POST value is correct!\n')
else:
delete_deployment(g.ns,g.name)
except Exception as e:
logging.error(e)
print(e)
return ('OK\n')
@app.route('/delete-service',methods=['POST'])
def delete_service_k8s():
try:
data = json.loads(request.data)
g.ns = data.get('ns')
g.name = data.get('name')
if g.ns == '':
g.ns = 'default'
if g.name == '':
return('Make sure the POST value is correct!\n')
else:
delete_service(g.ns,g.name)
except Exception as e:
logging.error(e)
print(e)
return ('OK\n')
if __name__ == '__main__':
app.run(debug=True, port=5555, host='0.0.0.0')