cpcc/wwwroot/cpcpodyaml/update_cpcpodyaml.dspy
2025-07-16 14:32:09 +08:00

110 lines
3.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

ns = params_kw.copy()
# cpcid
# clusterid
if params_kw.get('adminpwd'):
ns['adminpwd'] = password_encode(params_kw.get('adminpwd'))
clusterid = params_kw.get("clusterid")
cpcid = params_kw.get("cpcid")
db = DBPools()
dbname = await rfexe('get_module_dbname', 'cpcc')
async with db.sqlorContext(dbname) as sor:
# 补充['cluster_type', 'host', 'port', 'user', 'psssword', 'kubeconfig']
cpclusters = await sor.R('cpccluster', {'id':clusterid})
if len(cpclusters) < 1:
e = Exception(f'cpclist {clusterid=} not exists')
exception(f'{e}')
raise e
cpcluster = cpclusters[0]
# 此处的nodeid即控制节点id
kubeconfig = cpcluster.get("kubeconfig")
nodeid = cpcluster.get("controllerid")
cpcs = await sor.R('cpclist', {'id':cpcid})
if len(cpcs) < 1:
e = Exception(f'cpclist {cpcid=} not exists')
exception(f'{e}')
raise e
cpc = cpcs[0]
nodes = await sor.R('cpcnode', {'id':nodeid})
# 这里有问题:没有匹配到节点信息!!!
if len(nodes) < 1:
e = Exception(f'cpcnode {nodeid=} 节点基础信息不匹配')
exception(f'{e}')
raise e
node = nodes[0]
url = cpc.pcapi_url + "/pcapi/api/v1/cluster/common/update_cpcpod"
debug(f"请求url: {url=}")
debug(f"目标IP认证信息: {node.ip=} {node.sshport=} {node.adminuser=} {password_decode(node.adminpwd)=}")
# 请求方式待定,取决于获取参数值方式
userorgid = await get_userorgid()
debug(f'当前组织ID:{userorgid}')
if not userorgid:
return {
"widgettype":"Error",
"options":{
"title":"Authorization Error",
"timeout":3,
"cwidth":16,
"cheight":9,
"message":"Please login"
}
}
headers = basic_auth_headers(cpc.api_user, password_decode(cpc.api_pwd))
ns_name = clusterid.replace("_","-") + "-" + userorgid # 集群信息+组织信息合成命名空间
ns_name = ns_name.lower()
hc = HttpClient()
# type参数更新和新增都是为apply删除为delete
yamlconfig_id = params_kw.get('id')
import requests
params = {
'cluster_type': cluster_type,
'host':node.ip,
'port':node.sshport,
'user':node.adminuser,
'psssword':password_decode(node.adminpwd),
'kubeconfig':kubeconfig,
'action':'apply',
'namespace_name': ns_name,
'serviceaccount_name': ns_name + "-serviceaccount",
'podcd_name': yamlconfig_id + "-" + params_kw.get("source_podengine").lower(),
'service_name': yamlconfig_id + "-service", #取代+ "-" + params_kw.get("source_name")
}
params.update(params_kw)
keys_to_remove = ['_webbricks_', 'width', 'height', '_is_mobile']
for key in keys_to_remove:
if key in params:
del params[key]
debug(f'请求pcapi参数: {params=}')
# 框架不支持超时时间
resp = requests.post(url,
headers = headers,
data=params,
timeout=500
)
resp = json.dumps(resp.json()) #这里模拟hc.request返回的结果写后续逻辑
debug(f'{type(resp)=}->{resp=}')
data = json.loads(resp).get('data')
keys_to_remove = ['cluster_type', 'host', 'port', 'user', 'psssword', 'kubeconfig']
for key in keys_to_remove:
if key in params:
del params[key]
params['id'] = yamlconfig_id
if json.loads(resp).get("status") == True:
r = await sor.U('yaml_config', ns)
debug('update success');
return UiMessage(title='更新YAML参数', message='资源参数更新成功,请10秒后查看实时资源实例面板')
else:
return UiError(title='更新YAML参数', message='资源实例更新失败')
return UiError(title='更新YAML参数', message='其他错误')