-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployment.ts
More file actions
404 lines (355 loc) · 14.5 KB
/
deployment.ts
File metadata and controls
404 lines (355 loc) · 14.5 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import { BOOLEAN_TYPES } from '@data/booleanTypes';
import { CR_VISIBILITY_OPTIONS } from '@data/crVisibilityOptions';
import { PIPELINE_INPUT_TYPES } from '@data/pipelineInputTypes';
import { PLUGIN_SIGNATURE_TYPES } from '@data/pluginSignatureTypes';
import { POLICY_TYPES } from '@data/policyTypes';
import {
dynamicEnvEntrySchema,
enabledBooleanTypeValue,
getCustomParametersArraySchema,
getFileVolumesArraySchema,
getKeyValueEntriesArraySchema,
getNameWithoutSpacesSchema,
getOptionalStringSchema,
getStringSchema,
nodeSchema,
workerCommandSchema,
} from '@schemas/common';
import { BasePluginType, PluginType } from '@typedefs/steps/deploymentStepTypes';
import { z } from 'zod';
// Common validation patterns
const validations = {
// String patterns
jobAlias: getNameWithoutSpacesSchema(3, 36),
containerImage: z
.string({ required_error: 'Value is required' })
.min(3, 'Value must be at least 3 characters')
.max(256, 'Value cannot exceed 256 characters'),
containerRegistry: z
.string({ required_error: 'Value is required' })
.min(3, 'Value must be at least 3 characters')
.max(128, 'Value cannot exceed 128 characters')
.regex(/^[^/]+\.[^/]+$/, 'Must be a valid domain format'),
uri: z
.string({ required_error: 'Value is required' })
.min(2, 'Value must be at least 2 characters')
.max(256, 'Value cannot exceed 256 characters')
.regex(/^https?:\/\/.+/, 'Must be a valid URI'),
optionalUri: z
.union([
z.literal(''),
z
.string()
.min(2, 'Value must be at least 2 characters')
.max(256, 'Value cannot exceed 256 characters')
.regex(/^https?:\/\/.+/, 'Must be a valid URI'),
])
.optional(),
port: z.union([
z.literal(''),
z
.number()
.int('Value must be a whole number')
.min(1, 'Value must be at least 1')
.max(65535, 'Value cannot exceed 65535'),
]),
ports: z
.array(
z.object({
hostPort: z
.number()
.int('Value must be a whole number')
.min(1, 'Value must be at least 1')
.max(65535, 'Value cannot exceed 65535'),
containerPort: z
.number()
.int('Value must be a whole number')
.min(1, 'Value must be at least 1')
.max(65535, 'Value cannot exceed 65535'),
}),
)
.refine(
(entries) => {
const hostPorts = entries.map((entry) => entry.hostPort);
return hostPorts.length === new Set(hostPorts).size;
},
{
message: 'Duplicate host ports are not allowed',
},
)
.default([]),
envVars: getKeyValueEntriesArraySchema(50),
dynamicEnvVars: z
.array(dynamicEnvEntrySchema)
.max(50, 'Maximum 50 dynamic environment variables')
.refine(
(entries) => {
const keys = entries.map((entry) => entry.key?.trim()).filter((key) => key && key !== ''); // Only non-empty keys
const uniqueKeys = new Set(keys);
return uniqueKeys.size === keys.length;
},
{
message: 'Duplicate keys are not allowed',
},
),
customParams: getCustomParametersArraySchema(),
pipelineParams: getKeyValueEntriesArraySchema(50),
volumes: getKeyValueEntriesArraySchema(50),
fileVolumes: getFileVolumesArraySchema(50),
// Enum patterns
restartPolicy: z.enum(POLICY_TYPES, { required_error: 'Value is required' }),
imagePullPolicy: z.enum(POLICY_TYPES, { required_error: 'Value is required' }),
pluginSignature: z.enum(PLUGIN_SIGNATURE_TYPES, { required_error: 'Value is required' }),
chainstoreResponse: z.enum(BOOLEAN_TYPES, { required_error: 'Value is required' }),
};
// Helper functions for tunneling refinements
const createTunnelingRequiredRefinement = (fieldName: 'tunnelingToken') => {
return (data: { [key: string]: any }) => {
if (data.enableTunneling !== enabledBooleanTypeValue) {
return true; // Allow undefined when tunneling is not enabled
}
return data[fieldName] !== undefined;
};
};
const createPortRequiredRefinement = () => {
return (data: { [key: string]: any }) => {
if (data.enableTunneling !== BOOLEAN_TYPES[0]) {
return true; // Allow any value when tunneling is not enabled
}
return data.port !== '';
};
};
const tunnelingRefinements = {
tunnelingToken: {
refine: createTunnelingRequiredRefinement('tunnelingToken'),
options: {
message: 'Required when tunneling is enabled',
path: ['tunnelingToken'],
},
},
port: {
refine: createPortRequiredRefinement(),
options: {
message: 'Required when tunneling is enabled',
path: ['port'],
},
},
};
// Helper functions to apply refinements
export const applyTunnelingRefinements = (schema) => {
return schema
.refine(tunnelingRefinements.tunnelingToken.refine, tunnelingRefinements.tunnelingToken.options)
.refine(tunnelingRefinements.port.refine, tunnelingRefinements.port.options);
};
export const applyDeploymentTypeRefinements = (schema) => {
return schema.superRefine((data, ctx) => {
if (!data?.deploymentType) {
return;
}
// Validate that crUsername and crPassword are provided when crVisibility is 'Private'
if (data.deploymentType.pluginType === PluginType.Container && data.deploymentType.crVisibility === 'Private') {
if (!data.deploymentType.crUsername) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Username is required',
path: ['deploymentType', 'crUsername'],
});
}
if (!data.deploymentType.crPassword) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Password/Authentication Token is required',
path: ['deploymentType', 'crPassword'],
});
}
}
// Validate that username and accessToken are provided when worker repositoryVisibility is 'private'
if (data.deploymentType.pluginType === PluginType.Worker && data.deploymentType.repositoryVisibility === 'private') {
if (!data.deploymentType.username) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Username is required for private repositories',
path: ['deploymentType', 'username'],
});
}
if (!data.deploymentType.accessToken) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Access token is required for private repositories',
path: ['deploymentType', 'accessToken'],
});
}
}
});
};
export const applyCustomPluginSignatureRefinements = (schema) => {
return schema.refine(
(data) => {
if (data.pluginSignature === PLUGIN_SIGNATURE_TYPES[PLUGIN_SIGNATURE_TYPES.length - 1]) {
return typeof data.customPluginSignature === 'string' && data.customPluginSignature.trim() !== '';
}
return true;
},
{
message: 'Required when plugin signature is CUSTOM',
path: ['customPluginSignature'],
},
);
};
const mainDeploymentSchema = z.object({
jobAlias: validations.jobAlias,
// Target Nodes
autoAssign: z.boolean(),
targetNodes: z.array(nodeSchema).refine(
(nodes) => {
const addresses = nodes.map((node) => node.address?.trim()).filter((address) => address && address !== ''); // Only non-empty addresses
const uniqueAddresses = new Set(addresses);
return uniqueAddresses.size === addresses.length;
},
{
message: 'Duplicate addresses are not allowed',
},
),
spareNodes: z.array(nodeSchema).refine(
(nodes) => {
const addresses = nodes.map((node) => node.address?.trim()).filter((address) => address && address !== ''); // Only non-empty addresses
const uniqueAddresses = new Set(addresses);
return uniqueAddresses.size === addresses.length;
},
{
message: 'Duplicate addresses are not allowed',
},
),
allowReplicationInTheWild: z.boolean(),
});
const tunnelingSchema = z.object({
enableTunneling: z.enum(BOOLEAN_TYPES, { required_error: 'Value is required' }),
port: validations.port,
tunnelingToken: getOptionalStringSchema(512),
tunnelingLabel: z
.union([
z.literal(''),
z
.string()
.min(3, 'Value must be at least 3 characters')
.max(64, 'Value cannot exceed 64 characters')
.regex(
/^[a-zA-Z0-9!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]*$/,
'Only letters, numbers and special characters allowed',
),
])
.optional(),
});
const baseDeploymentSchema = mainDeploymentSchema.merge(tunnelingSchema);
const containerDeploymentTypeSchema = z.object({
pluginType: z.literal(PluginType.Container),
containerImage: validations.containerImage,
containerRegistry: validations.containerRegistry,
crVisibility: z.enum(CR_VISIBILITY_OPTIONS, { required_error: 'Value is required' }),
crUsername: z.union([getStringSchema(3, 128), z.literal('')]).optional(),
crPassword: z.union([getStringSchema(3, 256), z.literal('')]).optional(),
});
const workerDeploymentTypeSchema = z.object({
pluginType: z.literal(PluginType.Worker),
image: getStringSchema(3, 256),
repositoryUrl: z
.string({ required_error: 'Value is required' })
.min(3, 'Value must be at least 3 characters')
.max(512, 'Value cannot exceed 512 characters')
.regex(/^https?:\/\/github\.com\/[^/\s]+\/[^/\s]+(?:\.git)?(?:\/.*)?$/i, 'Must be a valid GitHub repository URL'),
repositoryVisibility: z.enum(['public', 'private'], { required_error: 'Value is required' }),
username: z
.string()
.max(256, `Value cannot exceed 256 characters`)
.regex(/^[a-zA-Z0-9!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]*$/, 'Only letters, numbers and special characters allowed')
.optional(),
accessToken: z
.string()
.max(512, `Value cannot exceed 512 characters`)
.regex(/^[a-zA-Z0-9!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]*$/, 'Only letters, numbers and special characters allowed')
.optional(),
workerCommands: z.array(workerCommandSchema).refine(
(workerCommand) => {
const commands = workerCommand.map((item) => item.command?.trim()).filter((command) => command && command !== ''); // Only non-empty commands
const uniqueCommands = new Set(commands);
return uniqueCommands.size === commands.length;
},
{
message: 'Duplicate commands are not allowed',
},
),
});
export const deploymentTypeSchema = z.discriminatedUnion('pluginType', [
containerDeploymentTypeSchema,
workerDeploymentTypeSchema,
]);
const genericAppDeploymentSchemaWihtoutRefinements = baseDeploymentSchema.extend({
deploymentType: deploymentTypeSchema,
ports: validations.ports,
envVars: validations.envVars,
dynamicEnvVars: validations.dynamicEnvVars,
volumes: validations.volumes,
fileVolumes: validations.fileVolumes,
restartPolicy: validations.restartPolicy,
imagePullPolicy: validations.imagePullPolicy,
customParams: validations.customParams,
});
export const genericAppDeploymentSchema = applyDeploymentTypeRefinements(
applyTunnelingRefinements(genericAppDeploymentSchemaWihtoutRefinements),
);
// Plugins
const genericPluginSchema = z.object({
basePluginType: z.literal(BasePluginType.Generic),
// Tunneling
port: validations.port,
enableTunneling: z.enum(BOOLEAN_TYPES, { required_error: 'Value is required' }),
tunnelingToken: getOptionalStringSchema(512),
// Ports
ports: validations.ports,
// Deployment type
deploymentType: deploymentTypeSchema,
// Variables
envVars: validations.envVars,
dynamicEnvVars: validations.dynamicEnvVars,
volumes: validations.volumes,
fileVolumes: validations.fileVolumes,
// Policies
restartPolicy: z.enum(POLICY_TYPES, { required_error: 'Value is required' }),
imagePullPolicy: z.enum(POLICY_TYPES, { required_error: 'Value is required' }),
// Custom Parameters
customParams: validations.customParams,
});
const nativePluginSchema = z.object({
basePluginType: z.literal(BasePluginType.Native),
// Signature
pluginSignature: validations.pluginSignature,
customPluginSignature: getOptionalStringSchema(128),
// Tunneling
port: validations.port,
enableTunneling: z.enum(BOOLEAN_TYPES, { required_error: 'Value is required' }),
tunnelingToken: getOptionalStringSchema(512),
// Custom Parameters
customParams: validations.customParams,
});
const pluginSchemaWithoutRefinements = z.discriminatedUnion('basePluginType', [genericPluginSchema, nativePluginSchema]);
const pluginSchema = applyCustomPluginSignatureRefinements(
applyDeploymentTypeRefinements(applyTunnelingRefinements(pluginSchemaWithoutRefinements)),
);
export const nativeAppPluginsSchema = z
.array(pluginSchema)
.min(1, 'At least one plugin is required')
.max(5, 'Only 5 plugins allowed');
const nativeAppDeploymentSchemaWihtoutRefinements = mainDeploymentSchema.extend({
pipelineParams: validations.pipelineParams,
pipelineInputType: z.enum(PIPELINE_INPUT_TYPES, { required_error: 'Value is required' }),
pipelineInputUri: validations.optionalUri,
chainstoreResponse: validations.chainstoreResponse,
});
export const nativeAppDeploymentSchema = applyCustomPluginSignatureRefinements(
applyTunnelingRefinements(nativeAppDeploymentSchemaWihtoutRefinements),
);
const serviceAppDeploymentSchemaWihtoutRefinements = baseDeploymentSchema.extend({
inputs: validations.envVars,
serviceReplica: nodeSchema.shape.address.optional(),
});
export const serviceAppDeploymentSchema = applyTunnelingRefinements(serviceAppDeploymentSchemaWihtoutRefinements);