-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathreplace-function-config.ts
More file actions
203 lines (188 loc) · 7.75 KB
/
replace-function-config.ts
File metadata and controls
203 lines (188 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import _ from 'lodash';
import logger from '../../../logger';
import {
computeLocalAuto,
getRemoteResourceConfig,
isCustomContainerRuntime,
isCustomRuntime,
} from './utils';
// 注意 remote 为空时不能 set remote
export default function (_local: any, _remote: any) {
const local = _.cloneDeep(_local);
const remote = _.cloneDeep(_remote);
logger.debug(`Pre init function local config: ${JSON.stringify(local)}`);
// 线上配置如果存在,则需要将 auto 资源替换为线上资源配置
if (remote) {
const { remoteNasConfig, remoteVpcConfig, remoteLogConfig, remoteRole } =
getRemoteResourceConfig(remote);
const { nasAuto, vpcAuto, slsAuto, roleAuto } = computeLocalAuto(local);
logger.debug(
`Init local compute local auto, nasAuto: ${nasAuto}; vpcAuto: ${vpcAuto}; slsAuto: ${slsAuto}; roleAuto: ${roleAuto}`,
);
if (nasAuto && !_.isEmpty(remoteNasConfig?.mountPoints)) {
_.set(local, 'nasConfig', remoteNasConfig);
}
if (vpcAuto) {
if (
_.isString(local.vpcConfig) &&
_.toUpper(local.vpcConfig) === 'AUTO' &&
remoteVpcConfig?.vpcId
) {
// vpcConfig: auto
_.set(local, 'vpcConfig', remoteVpcConfig);
} else if (
_.isObject(local.vpcConfig) &&
local.vpcConfig?.vpcId &&
remoteVpcConfig?.vpcId &&
local.vpcConfig.vpcId === remoteVpcConfig.vpcId
) {
if (
_.isString(local.vpcConfig.vSwitchIds) &&
_.toUpper(local.vpcConfig.vSwitchIds) === 'AUTO'
) {
/*
vpcConfig:
vpcId: myVpcId
vSwitchIds: auto
securityGroupId: auto | mysg
*/
_.set(local, 'vpcConfig.vSwitchIds', remoteVpcConfig.vSwitchIds);
}
if (
_.isString(local.vpcConfig.securityGroupId) &&
_.toUpper(local.vpcConfig.securityGroupId) === 'AUTO'
) {
/*
vpcConfig:
vpcId: myVpcId
vSwitchIds: auto | [myvSwitchId]
securityGroupId: auto
*/
_.set(local, 'vpcConfig.securityGroupId', remoteVpcConfig.securityGroupId);
}
}
logger.debug(`replace function config: local vpcConfig = ${JSON.stringify(local.vpcConfig)}`);
}
if (slsAuto && !_.isEmpty(remoteLogConfig?.project)) {
_.set(local, 'logConfig', remoteLogConfig);
}
if (!local.role) {
if (roleAuto) {
_.set(local, 'role', _.isString(remoteRole) && remoteRole !== '' ? remoteRole : 'auto');
} else {
_.set(local, 'role', '');
}
}
if (remote.customContainerConfig) {
_.unset(remote.customContainerConfig, 'resolvedImageUri');
}
_.unset(remote, 'lastUpdateStatus');
_.unset(remote, 'state');
}
// 适配钩子函数配置
if (!(_.isEmpty(local?.instanceLifecycleConfig) && _.isEmpty(remote?.instanceLifecycleConfig))) {
const { initializer, preStop } = local.instanceLifecycleConfig || {};
if (initializer?.handler && initializer?.command && !_.isEmpty(initializer?.command)) {
throw new Error(
'fc3 pre check: command and handler can not be set at the same time in lifecycle Lifecycle.Initializer',
);
}
if (preStop?.handler && preStop?.command && !_.isEmpty(preStop?.command)) {
throw new Error(
'fc3 pre check: command and handler can not be set at the same time in lifecycle Lifecycle.PreStop',
);
}
const initializerTimeout = _.get(remote, 'instanceLifecycleConfig.initializer.timeout', 3);
if (
remote?.instanceLifecycleConfig?.initializer?.handler ||
remote?.instanceLifecycleConfig?.initializer?.command ||
remote?.instanceLifecycleConfig?.initializer?.timeout
) {
if (initializer?.handler || (initializer?.command && !_.isEmpty(initializer.command))) {
if (
remote?.instanceLifecycleConfig?.initializer?.handler &&
initializer?.command &&
!_.isEmpty(initializer.command)
) {
_.set(local, 'instanceLifecycleConfig.initializer.handler', '');
}
if (remote?.instanceLifecycleConfig?.initializer?.command && initializer?.handler) {
_.set(local, 'instanceLifecycleConfig.initializer.command', []);
}
if (!initializer.timeout) {
_.set(local, 'instanceLifecycleConfig.initializer.timeout', initializerTimeout);
}
} else {
_.set(local, 'instanceLifecycleConfig.initializer.handler', '');
_.set(local, 'instanceLifecycleConfig.initializer.command', []);
_.set(local, 'instanceLifecycleConfig.initializer.timeout', 3);
}
} else if ((initializer?.command || initializer?.handler) && !initializer.timeout) {
_.set(local, 'instanceLifecycleConfig.initializer.timeout', initializerTimeout);
}
const preStopTimeout = _.get(remote, 'instanceLifecycleConfig.preStop.timeout', 3);
if (
remote?.instanceLifecycleConfig?.preStop?.handler ||
remote?.instanceLifecycleConfig?.preStop?.command ||
remote?.instanceLifecycleConfig?.preStop?.timeout
) {
if (preStop?.handler || (preStop?.command && !_.isEmpty(preStop.command))) {
if (
remote?.instanceLifecycleConfig?.preStop?.handler &&
preStop?.command &&
!_.isEmpty(preStop.command)
) {
_.set(local, 'instanceLifecycleConfig.preStop.handler', '');
}
if (remote?.instanceLifecycleConfig?.preStop?.command && preStop?.handler) {
_.set(local, 'instanceLifecycleConfig.preStop.command', []);
}
if (!preStop.timeout) {
_.set(local, 'instanceLifecycleConfig.preStop.timeout', preStopTimeout);
}
} else {
_.set(local, 'instanceLifecycleConfig.preStop.handler', '');
_.set(local, 'instanceLifecycleConfig.preStop.command', []);
_.set(local, 'instanceLifecycleConfig.preStop.timeout', 3);
}
} else if ((preStop?.command || preStop?.handler) && !preStop.timeout) {
_.set(local, 'instanceLifecycleConfig.preStop.timeout', preStopTimeout);
}
}
// 如果 local 和 remote 都是 handler 和 command 为 '', 则从 props 中删除
if (local?.instanceLifecycleConfig && remote?.instanceLifecycleConfig) {
const { initializer: initializerL, preStop: preStopL } = local.instanceLifecycleConfig || {};
const { initializer: initializerR, preStop: preStopR } = remote.instanceLifecycleConfig || {};
if (
initializerL?.handler === initializerR?.handler &&
initializerL?.handler === '' &&
_.isEqual(initializerL?.command, initializerR?.command) &&
_.isEmpty(initializerL?.command)
) {
_.unset(local, 'instanceLifecycleConfig.initializer');
_.unset(remote, 'instanceLifecycleConfig.initializer');
}
if (
preStopL?.handler === preStopR?.handler &&
preStopL?.handler === '' &&
_.isEqual(preStopL?.command, preStopR?.command) &&
_.isEmpty(preStopL?.command)
) {
_.unset(local, 'instanceLifecycleConfig.preStop');
_.unset(remote, 'instanceLifecycleConfig.preStop');
}
}
if (isCustomContainerRuntime(local.runtime)) {
// 传入 code:InvalidArgument: code: 400, Either ossBucketName/ossObjectName or zipFile must be set
_.unset(local, 'code');
// plan 时会多出提示一个状态信息
_.unset(remote, 'customContainerConfig.accelerationInfo');
}
if (isCustomContainerRuntime(local.runtime) || isCustomRuntime(local.runtime)) {
// 不传入 handler:InvalidArgument: code: 400, Handler is required but not provided
_.set(local, 'handler', local.handler || remote?.handler || 'handler');
_.set(local, 'handler', local.handler || remote?.handler || 'handler');
}
logger.debug(`Post init local config: ${JSON.stringify(local)}`);
return { local, remote };
}