Skip to content

Commit 8fc89a1

Browse files
committed
[CE-378] Updated code to handle k8s params from UI
1. Added k8s_cerd_type in src/common/utils.py 2. Added code to handle "kubernetes" host type in src/resources/host_api.py Change-Id: I7a0e96675957e42eb8aeb7714e5ed1f259de5db1 Signed-off-by: luke <jiahaochen1993@gmail.com>
1 parent 85b3974 commit 8fc89a1

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/common/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@
115115
NETWORK_STATUS_DELETING = 'deleting' # network is in deleting
116116
NETWORK_STATUS_STOPPED = 'stopped' # network is stopped
117117

118+
K8S_CRED_TYPE = {
119+
'account': '0',
120+
'cert': '1',
121+
'config': '2'
122+
}
123+
118124
# Vcenter and VirtualMachine Confs
119125
VIRTUAL_MACHINE = 'vm'
120126
VCENTER = 'vc'

src/resources/host_api.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
CODE_CREATED, \
1919
request_debug
2020

21+
from common.utils import K8S_CRED_TYPE
22+
2123
from modules import host_handler
2224
from modules.models import Cluster as ClusterModel
2325
from modules.models import Host as HostModel
@@ -141,6 +143,28 @@ def host_create():
141143
log_server=log_server,
142144
host_type=host_type,
143145
params=vsphere_param)
146+
147+
elif host_type == 'kubernetes':
148+
worker_api = r.form['k8s_master_address']
149+
k8s_param = create_k8s_host(name, capacity, log_type, r)
150+
if len(k8s_param) == 0:
151+
return make_fail_resp(error=error_msg, data=r.form)
152+
153+
logger.debug("name={}, capacity={},"
154+
"fillup={}, schedulable={}, log={}/{}, k8s_param={}".
155+
format(name, capacity, autofill, schedulable,
156+
log_type, log_server, k8s_param))
157+
158+
result = host_handler.create(name=name, worker_api=worker_api,
159+
capacity=int(capacity),
160+
autofill=autofill,
161+
schedulable=schedulable,
162+
log_level=log_level,
163+
log_type=log_type,
164+
log_server=log_server,
165+
host_type=host_type,
166+
params=k8s_param)
167+
144168
else:
145169
logger.debug("name={}, worker_api={}, capacity={}"
146170
"fillup={}, schedulable={}, log={}/{}".
@@ -276,3 +300,49 @@ def host_actions():
276300
error_msg = "unknown host action={}".format(action)
277301
logger.warning(error_msg)
278302
return make_fail_resp(error=error_msg, data=body)
303+
304+
305+
def create_k8s_host(name, capacity, log_type, request):
306+
k8s_param = {
307+
'address': request.form['k8s_master_address'],
308+
'credType': request.form['k8s_cred_type'],
309+
'username': request.form['k8s_username'],
310+
'password': request.form['k8s_password'],
311+
'cert': request.form['k8s_cert'],
312+
'key': request.form['k8s_key'],
313+
'config': request.form['k8s_config'],
314+
'nfsServer': request.form['k8s_nfs_server'],
315+
'extra_params': request.form['k8s_extra_params']
316+
}
317+
if "k8s_ssl" in request.form and request.form["k8s_ssl"] == "on":
318+
k8s_ssl = "true"
319+
else:
320+
k8s_ssl = "false"
321+
k8s_param['use_ssl'] = k8s_ssl
322+
323+
k8s_must_have_params = {
324+
'Name': name,
325+
'Capacity': capacity,
326+
'LoggingType': log_type,
327+
'K8SAddress': request.form['k8s_master_address'],
328+
'K8SCredType': request.form['k8s_cred_type'],
329+
'K8SNfsServer': request.form['k8s_nfs_server'],
330+
'K8SUseSsl': k8s_param['use_ssl']
331+
}
332+
333+
if k8s_must_have_params['K8SCredType'] == K8S_CRED_TYPE['account']:
334+
k8s_must_have_params['K8SUsername'] = request.form['k8s_username']
335+
k8s_must_have_params['K8SPassword'] = request.form['k8s_password']
336+
elif k8s_must_have_params['K8SCredType'] == K8S_CRED_TYPE['cert']:
337+
k8s_must_have_params['K8SCert'] = request.form['k8s_cert']
338+
k8s_must_have_params['K8SKey'] = request.form['k8s_key']
339+
elif k8s_must_have_params['K8SCredType'] == K8S_CRED_TYPE['config']:
340+
k8s_must_have_params['K8SConfig'] = request.form['k8s_config']
341+
342+
for key in k8s_must_have_params:
343+
if k8s_must_have_params[key] == '':
344+
error_msg = "host POST without {} data".format(key)
345+
logger.warning(error_msg)
346+
return []
347+
348+
return k8s_param

0 commit comments

Comments
 (0)