Skip to content

Commit 9ab689c

Browse files
fix: 接口调整,配置环境变量时如果检测到用户配置的变量名与内置冲突,提醒用户 (#2326)
1 parent 75c1d0e commit 9ab689c

5 files changed

Lines changed: 185 additions & 198 deletions

File tree

webfe/package_vue/src/language/lang/en.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2593,7 +2593,6 @@ export default {
25932593
'增强服务实例已分配,不能再修改配置信息': 'The enhanced service instance has been allocated and configuration information cannot be modified anymore.',
25942594
新建测试版本: 'Create Test Version',
25952595
提交并开始测试: 'Submit and Start Testing',
2596-
'环境变量不生效,KEY 与{s}增强服务的内置环境变量冲突': 'Environmental variables do not take effect, KEY conflicts with the built-in environmental variables of the {s} enhanced service',
25972596
重启次数: 'Restart Count',
25982597
测试报告: 'Test report',
25992598
终止测试: 'Terminate Test',
@@ -3049,4 +3048,8 @@ export default {
30493048
最早: 'Earliest',
30503049
构建详情: 'Build Details',
30513050
下载日志: 'Download Logs',
3051+
当前配置不生效: 'Current configuration is not effective',
3052+
当前配置将覆盖内置变量: 'Current configuration will override built-in variables',
3053+
和平台的内置环境变量冲突: 'Conflicts with the platforms built-in environment variables',
3054+
'和 {k} 增强服务的环境变量冲突': 'Conflicts with the environment variables of the {k} enhanced service',
30523055
};

webfe/package_vue/src/language/lang/zh.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2726,7 +2726,6 @@ export default {
27262726
'增强服务实例已分配,不能再修改配置信息': '增强服务实例已分配,不能再修改配置信息',
27272727
新建测试版本: '新建测试版本',
27282728
提交并开始测试: '提交并开始测试',
2729-
'环境变量不生效,KEY 与{s}增强服务的内置环境变量冲突': '环境变量不生效,KEY 与{s}增强服务的内置环境变量冲突',
27302729
重启次数: '重启次数',
27312730
测试报告: '测试报告',
27322731
终止测试: '终止测试',
@@ -3171,4 +3170,8 @@ export default {
31713170
最早: '最早',
31723171
构建详情: '构建详情',
31733172
下载日志: '下载日志',
3173+
当前配置不生效: '当前配置不生效',
3174+
当前配置将覆盖内置变量: '当前配置将覆盖内置变量',
3175+
和平台的内置环境变量冲突: '和平台的内置环境变量冲突',
3176+
'和 {k} 增强服务的环境变量冲突': '和 {k} 增强服务的环境变量冲突',
31743177
};

webfe/package_vue/src/store/modules/var.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ export default {
8585
return http.get(url);
8686
},
8787

88+
/**
89+
* 获取环境变量
90+
*/
91+
getEnvVariables({}, { appCode, moduleId, orderBy }) {
92+
const url = `${BACKEND_URL}/api/bkapps/applications/${appCode}/modules/${moduleId}/config_vars/?order_by=${orderBy}`;
93+
return http.get(url);
94+
},
95+
96+
/**
97+
* 获取有冲突的环境变量列表
98+
*/
99+
getConflictedEnvVariables({}, { appCode, moduleId }) {
100+
const url = `${BACKEND_URL}/api/bkapps/applications/${appCode}/modules/${moduleId}/config_vars/conflicted_keys/`;
101+
return http.get(url);
102+
},
103+
88104
/**
89105
* 保存环境变量数据
90106
*/
@@ -117,14 +133,6 @@ export default {
117133
return http.delete(url, {});
118134
},
119135

120-
/**
121-
* 获取增强服务内置环境变量
122-
*/
123-
getConfigVarKeys({}, { appCode, moduleId }) {
124-
const url = `${BACKEND_URL}/api/bkapps/applications/${appCode}/modules/${moduleId}/services/config_var_keys/`;
125-
return http.get(url, {});
126-
},
127-
128136
/**
129137
* 获取预设环境变量
130138
*/

webfe/package_vue/src/views/dev-center/app/engine/cloud-deployment/deploy-env.vue

Lines changed: 90 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,12 @@
201201
</div>
202202
<template v-else>
203203
<div class="var-key">{{ row.key }}</div>
204+
<!-- 环境变量冲突提示 -->
204205
<i
205-
v-if="row.isPresent"
206-
class="paasng-icon paasng-remind"
206+
v-if="!!row.conflict?.message"
207+
:class="['paasng-icon paasng-remind', { warning: row.conflict?.overrideConflicted }]"
207208
v-bk-tooltips="{
208-
content: $t('环境变量不生效,KEY 与{s}增强服务的内置环境变量冲突', { s: row.conflictingService }),
209+
content: row.conflict?.message,
209210
width: 200,
210211
}"
211212
></i>
@@ -587,7 +588,6 @@ export default {
587588
return {
588589
curItem: {},
589590
envVarList: [],
590-
runtimeImageList: [],
591591
rules: {
592592
key: [
593593
{
@@ -649,7 +649,6 @@ export default {
649649
},
650650
isLoading: true,
651651
isTableLoading: true,
652-
653652
envSidesliderConf: {
654653
visiable: false,
655654
},
@@ -688,7 +687,6 @@ export default {
688687
envEnums: ENV_ENUM,
689688
isBatchEdit: false,
690689
activeEnvValue: 'all',
691-
builtInEnvVars: {},
692690
showChild: false,
693691
varPresetlLength: 0,
694692
isDeployEnvVarChange: false,
@@ -718,67 +716,82 @@ export default {
718716
methods: {
719717
async init() {
720718
this.isLoading = true;
721-
await this.getConfigVarKeys();
722719
this.getEnvVarList();
723720
},
724-
// 是否已存在该环境变量
725-
isEnvVarAlreadyExists(varKey) {
726-
let flag = false;
727-
let services = '';
728-
// 检查是否已存在该环境变量
729-
for (const key in this.builtInEnvVars) {
730-
if (this.builtInEnvVars[key].includes(varKey)) {
731-
flag = true;
732-
services = key;
733-
break;
734-
}
735-
}
721+
722+
showErrorMessage(e) {
723+
this.$paasMessage({
724+
theme: 'error',
725+
message: `${this.$t('获取环境变量失败')}${e.message || e.detail}`,
726+
});
727+
},
728+
729+
// 获取环境变量冲突提示
730+
getConflictMessage(conflictedKeys, key) {
731+
const conflictItem = conflictedKeys.find((item) => item.key === key);
732+
733+
if (!conflictItem) return {};
734+
735+
const { conflicted_detail, override_conflicted, conflicted_source } = conflictItem;
736+
const basicText = override_conflicted ? this.$t('当前配置将覆盖内置变量') : this.$t('当前配置不生效');
737+
const conflictSourceMessage =
738+
conflicted_source === 'builtin_addons'
739+
? this.$t('和 {k} 增强服务的环境变量冲突', { k: conflicted_detail })
740+
: this.$t('和平台的内置环境变量冲突');
741+
736742
return {
737-
flag,
738-
services,
743+
message: `${basicText}${conflictSourceMessage}`,
744+
overrideConflicted: override_conflicted,
739745
};
740746
},
741-
getEnvVarList(isUpdate = true) {
747+
748+
// 获取冲突的环境变量
749+
async getConflictedEnvVariables() {
750+
try {
751+
const res = await this.$store.dispatch('envVar/getConflictedEnvVariables', {
752+
appCode: this.appCode,
753+
moduleId: this.curModuleId,
754+
});
755+
return res || [];
756+
} catch (e) {
757+
return [];
758+
}
759+
},
760+
761+
// 获取环境列表
762+
async getEnvVarList(isUpdate = true) {
742763
this.isTableLoading = true;
743-
this.$http
744-
.get(
745-
`${BACKEND_URL}/api/bkapps/applications/${this.appCode}/modules/${this.curModuleId}/config_vars/?order_by=${this.curSortKey}`
746-
)
747-
.then(
748-
(response) => {
749-
if (isUpdate) this.envVarList = [...response];
750-
// 添加自定义属性
751-
this.envVarList.forEach((v) => {
752-
this.$set(v, 'isEdit', false);
753-
const { flag, services } = this.isEnvVarAlreadyExists(v.key);
754-
this.$set(v, 'isPresent', flag);
755-
this.$set(v, 'conflictingService', services);
756-
if (!v.id) {
757-
const id = response.find((item) => item.key === v.key)?.id;
758-
this.$set(v, 'id', id);
759-
}
760-
});
761-
this.envLocalVarList = cloneDeep(this.envVarList);
762-
if (isUpdate) {
763-
this.handleFilterEnv(this.activeEnvValue);
764-
} else {
765-
this.$store.commit('cloudApi/updatePageEdit', false);
766-
}
767-
},
768-
(errRes) => {
769-
const errorMsg = errRes.message;
770-
this.$paasMessage({
771-
theme: 'error',
772-
message: `${this.$t('获取环境变量失败')}${errorMsg}`,
773-
});
774-
}
775-
)
776-
.finally(() => {
777-
this.showChild = true;
778-
this.isTableLoading = false;
779-
this.isLoading = false;
764+
try {
765+
const conflictedKeys = await this.getConflictedEnvVariables();
766+
const res = await this.$store.dispatch('envVar/getEnvVariables', {
767+
appCode: this.appCode,
768+
moduleId: this.curModuleId,
769+
orderBy: this.curSortKey,
780770
});
771+
if (isUpdate) {
772+
this.envVarList = [...res];
773+
}
774+
this.envVarList = res.map((item) => ({
775+
...item,
776+
isEdit: false, // 取消编辑态
777+
conflict: conflictedKeys.length > 0 ? this.getConflictMessage(conflictedKeys, item.key) : {},
778+
id: item.id || res.find((i) => i.key === item.key)?.id,
779+
}));
780+
this.envLocalVarList = cloneDeep(this.envVarList);
781+
if (isUpdate) {
782+
this.handleFilterEnv(this.activeEnvValue);
783+
} else {
784+
this.$store.commit('cloudApi/updatePageEdit', false);
785+
}
786+
} catch (e) {
787+
this.showErrorMessage(e);
788+
} finally {
789+
this.showChild = true;
790+
this.isTableLoading = false;
791+
this.isLoading = false;
792+
}
781793
},
794+
782795
// 处理input事件
783796
handleInputEvent(rowItem) {
784797
this.curItem = rowItem;
@@ -1068,30 +1081,25 @@ export default {
10681081
this.handleModuleSelected('', { name: this.curSelectModuleName });
10691082
},
10701083
1071-
handleModuleSelected(value, { name }) {
1084+
// 获取其他模块的环境变量
1085+
async handleModuleSelected(value, { name }) {
10721086
this.curSelectModuleName = name;
10731087
this.exportDialog.isLoading = true;
1074-
this.$http
1075-
.get(
1076-
`${BACKEND_URL}/api/bkapps/applications/${this.appCode}/modules/${this.curSelectModuleName}/config_vars/?order_by=-created`
1077-
)
1078-
.then(
1079-
(response) => {
1080-
this.exportDialog.count = (response || []).length;
1081-
},
1082-
(errRes) => {
1083-
const errorMsg = errRes.detail;
1084-
this.$paasMessage({
1085-
theme: 'error',
1086-
message: `${this.$t('获取环境变量失败')}${errorMsg}`,
1087-
});
1088-
}
1089-
)
1090-
.finally(() => {
1091-
this.exportDialog.isLoading = false;
1088+
try {
1089+
const response = await this.$store.dispatch('envVar/getEnvVariables', {
1090+
appCode: this.appCode,
1091+
moduleId: this.curModuleId,
1092+
orderBy: '-created',
10921093
});
1094+
this.exportDialog.count = (response || []).length;
1095+
} catch (e) {
1096+
this.showErrorMessage(e);
1097+
} finally {
1098+
this.exportDialog.isLoading = false;
1099+
}
10931100
},
10941101
1102+
// 导入其他模块的环境变量
10951103
async handleExportConfirm() {
10961104
this.exportDialog.loading = true;
10971105
try {
@@ -1189,11 +1197,7 @@ export default {
11891197
this.invokeBrowserDownload(response, `bk_paas3_${this.appCode}_${this.curModuleId}_env_vars.yaml`);
11901198
},
11911199
(errRes) => {
1192-
const errorMsg = errRes.detail;
1193-
this.$paasMessage({
1194-
theme: 'error',
1195-
message: `${this.$t('获取环境变量失败')}${errorMsg}`,
1196-
});
1200+
this.showErrorMessage(errRes);
11971201
}
11981202
)
11991203
.finally(() => {
@@ -1444,22 +1448,6 @@ export default {
14441448
this.getEnvVarList();
14451449
},
14461450
1447-
// 获取增强服务内置环境变量
1448-
async getConfigVarKeys() {
1449-
try {
1450-
const varKeys = await this.$store.dispatch('envVar/getConfigVarKeys', {
1451-
appCode: this.appCode,
1452-
moduleId: this.curModuleId,
1453-
});
1454-
this.builtInEnvVars = varKeys;
1455-
} catch (e) {
1456-
this.$paasMessage({
1457-
theme: 'error',
1458-
message: e.detail || e.message || this.$t('接口异常'),
1459-
});
1460-
}
1461-
},
1462-
14631451
// 环境变量变更
14641452
handleEnvVarChange(text, value = true) {
14651453
this.envVarChangeText = text;
@@ -1935,17 +1923,9 @@ a.is-disabled {
19351923
width: 100%;
19361924
.bk-form-content {
19371925
width: 100%;
1938-
.tooltips-icon {
1939-
right: 2px !important;
1940-
}
19411926
}
19421927
}
19431928
}
1944-
.env-input-cls {
1945-
/deep/ .bk-form-input {
1946-
width: 90%;
1947-
}
1948-
}
19491929
19501930
.export-btn-cls {
19511931
font-size: 12px;
@@ -1983,6 +1963,9 @@ a.is-disabled {
19831963
font-size: 14px;
19841964
color: #ea3636;
19851965
transform: translateY(0);
1966+
&.warning {
1967+
color: #ff9c01;
1968+
}
19861969
}
19871970
.var-key {
19881971
text-overflow: ellipsis;

0 commit comments

Comments
 (0)