salescrm/b/cpcc/cpcpodyaml/update_cpcpodyaml.dspy
2025-10-27 15:50:44 +08:00

112 lines
4.3 KiB
Plaintext
Raw Permalink 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.

async def update_cpcpodyaml(params_kw={}):
ns = params_kw.copy()
# cpcid
# clusterid
if params_kw.get('adminpwd'):
ns['adminpwd'] = params_kw.get('adminpwd')
clusterid = params_kw.get("clusterid")
cpcid = params_kw.get("cpcid")
db = DBPools()
# dbname = await rfexe('get_module_dbname', 'cpcc')
dbname = 'kboss'
async with db.sqlorContext(dbname) as sor:
# 补充['cluster_type', 'host', 'port', 'user', 'password', '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=} {node.adminpwd=}")
# 请求方式待定,取决于获取参数值方式
orgid = await get_userorgid()
if not orgid:
orgid = params_kw.get("orgid",'000')
debug(f' >>> 当前用户组织ID: {orgid}')
if not orgid:
return {'status': False, 'msg': '无组织ID'}
headers = basic_auth_headers(cpc.api_user, cpc.api_pwd)
ns_name = clusterid.replace("_","-") + "-" + orgid # 集群信息+组织信息合成命名空间
ns_name = ns_name.lower()
ns_name = ns_name[:-1] + "kube" if ns_name.endswith('-') else ns_name
hc = HttpClient()
# type参数更新和新增都是为apply删除为delete
yamlconfig_id = params_kw.get('id')
if not yamlconfig_id:
return {'status': False, 'msg': '无资源实例配置ID'}
import requests
params = {
'cluster_type': cluster_type,
'host':node.ip,
'port':node.sshport,
'user':node.adminuser,
'password':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,
verify=False
)
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', 'password', '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('更新成功,资源准备中...');
time.sleep(10)
return {'status': True, 'msg': '资源参数更新成功,请10秒后查看实时资源实例面板', 'data': ns}
else:
return {'status': False, 'msg': '资源实例参数更新失败'}
return {'status': False, 'msg': '其他错误'}
ret = await update_cpcpodyaml(params_kw)
return ret