使用 i18n.t() 全局方式(避免 hook 定位问题): - 288 目标文件中 135 个成功转换 - 2228 处中文替换为 i18n.t() 调用 - 1006 个新翻译 key 添加到 zh-CN.json + en-US.json - 词典总量: 3554 keys (22 namespaces) - tsc 零错误 + Vite 构建成功 - 6 个含代码示例/数据字符串的文件安全回退
102 lines
3.7 KiB
TypeScript
102 lines
3.7 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||
// Deployment 多文档 YAML(Deployment + 关联 Service)加载与保存 // Logan.Li
|
||
import { kubernetesApi } from '@/api';
|
||
import i18n from '@/i18n';
|
||
import {
|
||
manifestToEditableYaml,
|
||
parseEditableManifest,
|
||
parseEditableManifests,
|
||
prepareManifestForSave,
|
||
} from './k8s-manifest-utils';
|
||
|
||
const toArray = (v: unknown) => (Array.isArray(v) ? v : ((v as Record<string, any>)?.list || (v as Record<string, any>)?.items || (v as Record<string, any>)?.data || []));
|
||
|
||
/** Service selector 是否匹配 Deployment 标签 // Logan.Li */
|
||
export function selectorMatches(
|
||
depSelector: Record<string, string>,
|
||
svcSelector: Record<string, string>,
|
||
): boolean {
|
||
if (Object.keys(svcSelector).length === 0) return false;
|
||
return Object.entries(svcSelector).every(([k, v]) => depSelector[k] === v);
|
||
}
|
||
|
||
/** 查找与 Deployment 关联的 Service 名称 // Logan.Li */
|
||
export function findMatchedServiceNames(
|
||
deploymentName: string,
|
||
depSelector: Record<string, string>,
|
||
services: any[],
|
||
): string[] {
|
||
const names = new Set<string>();
|
||
for (const svc of services) {
|
||
const svcName = svc.name || svc.metadata?.name || '';
|
||
if (!svcName) continue;
|
||
const svcSelector = svc.spec?.selector || svc.selector || {};
|
||
if (svcName === deploymentName || selectorMatches(depSelector, svcSelector)) {
|
||
names.add(svcName);
|
||
}
|
||
}
|
||
return [...names].sort();
|
||
}
|
||
|
||
/** 加载 Deployment + 关联 Service 的多文档 YAML // Logan.Li */
|
||
export async function loadDeploymentBundleYaml(
|
||
clusterCode: string,
|
||
namespace: string,
|
||
deploymentName: string,
|
||
): Promise<string> {
|
||
const depYaml = await kubernetesApi.getResourceYaml(clusterCode, namespace, 'deployments', deploymentName);
|
||
const { manifest: depManifest } = await parseEditableManifest(depYaml);
|
||
const depSelector = depManifest?.spec?.selector?.matchLabels
|
||
|| depManifest?.spec?.template?.metadata?.labels
|
||
|| {};
|
||
|
||
const svcListRes = await kubernetesApi.getNamespaceServices(clusterCode, namespace).catch(() => []);
|
||
const matchedSvcNames = findMatchedServiceNames(deploymentName, depSelector, toArray(svcListRes));
|
||
|
||
const docs: string[] = [];
|
||
if (depManifest) {
|
||
docs.push(await manifestToEditableYaml(depManifest));
|
||
} else {
|
||
const { yaml: stripped } = await parseEditableManifest(depYaml);
|
||
if (stripped.trim()) docs.push(stripped);
|
||
}
|
||
|
||
for (const svcName of matchedSvcNames) {
|
||
try {
|
||
const svcYaml = await kubernetesApi.getResourceYaml(clusterCode, namespace, 'services', svcName);
|
||
const { yaml: stripped } = await parseEditableManifest(svcYaml);
|
||
if (stripped.trim()) docs.push(stripped);
|
||
} catch {
|
||
// 单个 Service 拉取失败时跳过 // Logan.Li
|
||
}
|
||
}
|
||
|
||
return docs.join('\n---\n');
|
||
}
|
||
|
||
/** 保存多文档 YAML(Deployment + Service 等)// Logan.Li */
|
||
export async function saveDeploymentBundleYaml(
|
||
clusterCode: string,
|
||
namespace: string,
|
||
yamlText: string,
|
||
): Promise<void> {
|
||
const { manifests } = await parseEditableManifests(yamlText);
|
||
if (manifests.length === 0) throw new Error(i18n.t('kubernetes.deploymentbundl.s47161e05'));
|
||
|
||
for (const manifest of manifests) {
|
||
const kind = String(manifest.kind || '').toLowerCase();
|
||
const name = String(manifest.metadata?.name || '').trim();
|
||
if (!kind || !name) throw new Error(i18n.t('kubernetes.deploymentbundl.s659c1bb5'));
|
||
|
||
const docYaml = await prepareManifestForSave(manifest);
|
||
if (kind === 'deployment') {
|
||
await kubernetesApi.updateResourceYaml(clusterCode, namespace, 'deployments', name, docYaml);
|
||
continue;
|
||
}
|
||
if (kind === 'service') {
|
||
await kubernetesApi.applyYAML(clusterCode, docYaml);
|
||
continue;
|
||
}
|
||
throw new Error(`不支持的资源类型: ${manifest.kind}(仅支持 Deployment / Service)`);
|
||
}
|
||
} |