amberjs 1.17.0

JavaScript/TypeScript runtime built with Rust and V8
Documentation
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// AmberWorkload Custom Resource Definition
/// Defines workload-level configuration for running JavaScript/TypeScript scripts
use kube::CustomResource;
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
use serde::{Deserialize, Serialize};
use std::path::Path;
/// AmberWorkload is the schema for the amberjsworkload API
#[derive(CustomResource, Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[kube(
    group = "cloudnative.amberjs.io",
    version = "v1",
    kind = "AmberWorkload",
    plural = "amberjsworkloads",
    shortname = "bjw",
    namespaced
)]
#[cfg_attr(test, derive(Default))]
pub struct AmberWorkloadSpec {
    /// Reference to the AmberCluster
    pub cluster_ref: String,
    /// Path to the JavaScript/TypeScript script
    pub script_path: String,
    /// Arguments to pass to the script
    pub script_args: Vec<String>,
    /// Environment variables
    pub environment: HashMap<String, String>,
    /// Number of replicas
    pub replicas: u32,
    /// Resource requirements
    pub resources: super::amberjs_cluster::ResourceRequirements,
    /// HPA configuration for auto-scaling
    pub hpa_config: HPAConfig,
    /// Configuration for script execution
    pub execution: Option<ExecutionConfig>,
    /// Configuration for inputs and outputs
    pub io_config: Option<IOConfig>,
    /// Configuration for persistence
    pub persistence: Option<PersistenceConfig>,
    /// Configuration for networking
    pub networking: Option<NetworkingConfig>,
}
/// HPA (Horizontal Pod Autoscaler) configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct HPAConfig {
    /// Enable HPA
    pub enabled: bool,
    /// Minimum number of replicas
    pub min_replicas: u32,
    /// Maximum number of replicas
    pub max_replicas: u32,
    /// Target CPU utilization percentage
    pub target_cpu_percent: f64,
    /// Target memory utilization percentage
    pub target_memory_percent: f64,
    /// Custom metrics for scaling
    pub custom_metrics: Option<Vec<CustomMetric>>,
    /// Scale up/down stabilization window
    pub stabilization_window_seconds: Option<u64>,
    /// Scale policy
    pub scale_policy: Option<ScalePolicy>,
}
/// Custom metric for auto-scaling
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct CustomMetric {
    /// Metric name
    pub name: String,
    /// Metric type (Pod, Object, Resource)
    pub metric_type: String,
    /// Target value
    pub target_value: String,
    /// Metric selector
    pub metric_selector: Option<HashMap<String, String>>,
}
/// Scale policy
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct ScalePolicy {
    /// Policy type (Percent, Pods)
    pub policy_type: String,
    /// Value (percentage or absolute number)
    pub value: u32,
    /// Period seconds
    pub period_seconds: u64,
}
/// Configuration for script execution
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct ExecutionConfig {
    /// Execution mode (standalone, distributed, batch)
    pub mode: ExecutionMode,
    /// Script timeout
    pub timeout_seconds: Option<u64>,
    /// Retry configuration
    pub retry: Option<RetryConfig>,
    /// Concurrency limit
    pub concurrency_limit: Option<u32>,
    /// Memory limit per execution
    pub memory_limit_mb: Option<u32>,
}
/// Execution mode
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, Default)]
#[cfg_attr(test, derive(Default))]
pub enum ExecutionMode {
    /// Standalone execution (single run)
    #[default]
    Standalone = 0,
    /// Distributed execution (across cluster)
    Distributed,
    /// Batch processing
    Batch,
    /// Streaming execution
    Streaming,
}
/// Retry configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct RetryConfig {
    /// Maximum number of retries
    pub max_attempts: u32,
    /// Initial backoff duration
    pub initial_backoff_seconds: u64,
    /// Backoff multiplier
    pub backoff_multiplier: f64,
    /// Maximum backoff duration
    pub max_backoff_seconds: u64,
}
/// Configuration for inputs and outputs
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct IOConfig {
    /// Input sources
    pub inputs: Option<Vec<InputSource>>,
    /// Output destinations
    pub outputs: Option<Vec<OutputDestination>>,
    /// Buffer configuration
    pub buffer: Option<BufferConfig>,
}
/// Input source configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct InputSource {
    /// Source type (file, http, kafka, s3, etc.)
    pub source_type: String,
    /// Source configuration
    pub config: HashMap<String, String>,
    /// Format (json, csv, parquet, etc.)
    pub format: Option<String>,
    /// Schema (for structured data)
    pub schema: Option<String>,
}
/// Output destination configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct OutputDestination {
    /// Destination type (file, http, kafka, s3, etc.)
    pub dest_type: String,
    /// Destination configuration
    pub config: HashMap<String, String>,
    /// Format (json, csv, parquet, etc.)
    pub format: Option<String>,
    /// Compression (gzip, bzip2, etc.)
    pub compression: Option<String>,
}
/// Buffer configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct BufferConfig {
    /// Buffer size in bytes
    pub size_bytes: Option<u64>,
    /// Flush interval in seconds
    pub flush_interval_seconds: Option<u64>,
    /// Maximum items in buffer
    pub max_items: Option<u32>,
}
/// Persistence configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct PersistenceConfig {
    /// Enable persistence
    pub enabled: bool,
    /// Storage class
    pub storage_class: Option<String>,
    /// Access mode (ReadWriteOnce, ReadOnlyMany, ReadWriteMany)
    pub access_mode: Option<String>,
    /// Size of the volume
    pub size: Option<String>,
    /// Volume mount path
    pub mount_path: Option<String>,
}
/// Networking configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct NetworkingConfig {
    /// Service configuration
    pub service: Option<ServiceConfig>,
    /// Ingress configuration
    pub ingress: Option<IngressConfig>,
    /// Network policies
    pub network_policies: Option<Vec<NetworkPolicyConfig>>,
}
/// Service configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct ServiceConfig {
    /// Enable service
    pub enabled: bool,
    /// Service type (ClusterIP, NodePort, LoadBalancer)
    pub service_type: String,
    /// Port configuration
    pub ports: Option<Vec<ServicePort>>,
    /// Session affinity
    pub session_affinity: Option<String>,
}
/// Service port configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct ServicePort {
    /// Port name
    pub name: Option<String>,
    /// Port number
    pub port: u32,
    /// Target port
    pub target_port: Option<u32>,
    /// Protocol (TCP, UDP)
    pub protocol: Option<String>,
}
/// Ingress configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct IngressConfig {
    /// Enable ingress
    pub enabled: bool,
    /// Ingress class
    pub ingress_class: Option<String>,
    /// Annotations
    pub annotations: Option<HashMap<String, String>>,
    /// Hosts
    pub hosts: Option<Vec<IngressHost>>,
    /// TLS configuration
    pub tls: Option<Vec<IngressTLS>>,
}
/// Ingress host configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct IngressHost {
    /// Hostname
    pub host: String,
    /// Paths
    pub paths: Vec<IngressPath>,
}
/// Ingress path configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct IngressPath {
    /// Path
    pub path: String,
    /// Path type (Exact, Prefix, ImplementationSpecific)
    pub path_type: String,
    /// Backend service
    pub backend: IngressBackend,
}
/// Ingress backend
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct IngressBackend {
    /// Service name
    pub service_name: String,
    /// Service port
    pub service_port: u32,
}
/// Ingress TLS configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct IngressTLS {
    /// Hosts
    pub hosts: Option<Vec<String>>,
    /// Secret name
    pub secret_name: Option<String>,
}
/// Network policy configuration
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct NetworkPolicyConfig {
    /// Policy name
    pub name: String,
    /// Policy types (Ingress, Egress)
    pub policy_types: Vec<String>,
    /// Ingress rules
    pub ingress: Option<Vec<NetworkPolicyRule>>,
    /// Egress rules
    pub egress: Option<Vec<NetworkPolicyRule>>,
}
/// Network policy rule
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct NetworkPolicyRule {
    /// From (sources)
    pub from: Option<Vec<NetworkPolicyPeer>>,
    /// To (destinations)
    pub to: Option<Vec<NetworkPolicyPeer>>,
    /// Ports
    pub ports: Option<Vec<NetworkPolicyPort>>,
}
/// Network policy peer
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct NetworkPolicyPeer {
    /// Namespace selector
    pub namespace_selector: Option<super::amberjs_cluster::LabelSelector>,
    /// Pod selector
    pub pod_selector: Option<super::amberjs_cluster::LabelSelector>,
    /// IP block
    pub ip_block: Option<NetworkPolicyIPBlock>,
}
/// Network policy IP block
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct NetworkPolicyIPBlock {
    /// CIDR
    pub cidr: String,
    /// Except
    pub except: Option<Vec<String>>,
}
/// Network policy port
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(test, derive(Default))]
pub struct NetworkPolicyPort {
    /// Port
    pub port: Option<u32>,
    /// Protocol
    pub protocol: Option<String>,
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_amberjs_workload_crd_creation() {
        let workload: _ = AmberWorkload::new(
            "my-workload",
            AmberWorkloadSpec {
                cluster_ref: "my-cluster".to_string(),
                script_path: "/app/main.js".to_string(),
                script_args: vec!["--mode=production".to_string()],
                environment: HashMap::from([
                    ("NODE_ENV".to_string(), "production".to_string()),
                    ("AMBER_MODE".to_string(), "distributed".to_string()),
                ]),
                replicas: 5,
                resources: super::super::amberjs_cluster::ResourceRequirements {
                    cpu: "1".to_string(),
                    memory: "2Gi".to_string(),
                    disk: "10Gi".to_string(),
                    gpu: None,
                },
                hpa_config: HPAConfig {
                    enabled: true,
                    min_replicas: 2,
                    max_replicas: 20,
                    target_cpu_percent: 70.0,
                    target_memory_percent: 80.0,
                    custom_metrics: None,
                    stabilization_window_seconds: None,
                    scale_policy: None,
                },
                execution: None,
                io_config: None,
                persistence: None,
                networking: None,
            },
        );
        assert_eq!(workload.spec.cluster_ref, "my-cluster");
        assert_eq!(workload.spec.script_path, "/app/main.js");
        assert_eq!(workload.spec.replicas, 5);
        assert!(workload.spec.hpa_config.enabled);
        assert_eq!(workload.spec.hpa_config.min_replicas, 2);
        assert_eq!(workload.spec.hpa_config.max_replicas, 20);
    }
    #[test]
    fn test_hpa_config() {
        let hpa: _ = HPAConfig {
            enabled: true,
            min_replicas: 3,
            max_replicas: 50,
            target_cpu_percent: 60.0,
            target_memory_percent: 75.0,
            custom_metrics: Some(vec![CustomMetric {
                name: "requests_per_second".to_string(),
                metric_type: "Pod".to_string(),
                target_value: "100".to_string(),
                metric_selector: None,
            }]),
            stabilization_window_seconds: Some(300),
            scale_policy: Some(ScalePolicy {
                policy_type: "Percent".to_string(),
                value: 10,
                period_seconds: 60,
            }),
        };
        assert!(hpa.enabled);
        assert_eq!(hpa.min_replicas, 3);
        assert_eq!(hpa.max_replicas, 50);
        assert_eq!(hpa.target_cpu_percent, 60.0);
        assert!(hpa.custom_metrics.is_some());
        assert_eq!(hpa.custom_metrics.as_ref().unwrap().len(), 1);
    }
    #[test]
    fn test_execution_config() {
        let execution: _ = ExecutionConfig {
            mode: ExecutionMode::Distributed,
            timeout_seconds: Some(300),
            retry: Some(RetryConfig {
                max_attempts: 3,
                initial_backoff_seconds: 5,
                backoff_multiplier: 2.0,
                max_backoff_seconds: 60,
            }),
            concurrency_limit: Some(100),
            memory_limit_mb: Some(512),
        };
        matches!(execution.mode, ExecutionMode::Distributed);
        assert_eq!(execution.timeout_seconds, Some(300));
        assert_eq!(execution.retry.as_ref().unwrap().max_attempts, 3);
        assert_eq!(execution.concurrency_limit, Some(100));
    }
    #[test]
    fn test_io_config() {
        let io_config: _ = IOConfig {
            inputs: Some(vec![InputSource {
                source_type: "kafka".to_string(),
                config: HashMap::from([
                    ("brokers".to_string(), "kafka:9092".to_string()),
                    ("topic".to_string(), "input-events".to_string()),
                ]),
                format: Some("json".to_string()),
                schema: None,
            }]),
            outputs: Some(vec![OutputDestination {
                dest_type: "s3".to_string(),
                config: HashMap::from([
                    ("bucket".to_string(), "output-bucket".to_string()),
                    ("region".to_string(), "us-west-2".to_string()),
                ]),
                format: Some("parquet".to_string()),
                compression: Some("gzip".to_string()),
            }]),
            buffer: Some(BufferConfig {
                size_bytes: Some(1024 * 1024), // 1MB
                flush_interval_seconds: Some(10),
                max_items: Some(1000),
            }),
        };
        assert_eq!(io_config.inputs.as_ref().unwrap().len(), 1);
        assert_eq!(io_config.outputs.as_ref().unwrap().len(), 1);
        assert_eq!(io_config.inputs.as_ref().unwrap()[0].source_type, "kafka");
        assert_eq!(io_config.outputs.as_ref().unwrap()[0].dest_type, "s3");
        assert_eq!(
            io_config.buffer.as_ref().unwrap().size_bytes,
            Some(1024 * 1024)
        );
    }
    #[test]
    fn test_networking_config() {
        let networking: _ = NetworkingConfig {
            service: Some(ServiceConfig {
                enabled: true,
                service_type: "ClusterIP".to_string(),
                ports: Some(vec![ServicePort {
                    name: Some("http".to_string()),
                    port: 8080,
                    target_port: Some(8080),
                    protocol: Some("TCP".to_string()),
                }]),
                session_affinity: Some("ClientIP".to_string()),
            }),
            ingress: Some(IngressConfig {
                enabled: true,
                ingress_class: Some("nginx".to_string()),
                annotations: Some(HashMap::from([(
                    "cert-manager.io/cluster-issuer".to_string(),
                    "letsencrypt".to_string(),
                )])),
                hosts: Some(vec![IngressHost {
                    host: "api.example.com".to_string(),
                    paths: vec![IngressPath {
                        path: "/".to_string(),
                        path_type: "Prefix".to_string(),
                        backend: IngressBackend {
                            service_name: "my-workload".to_string(),
                            service_port: 8080,
                        },
                    }],
                }]),
                tls: Some(vec![IngressTLS {
                    hosts: Some(vec!["api.example.com".to_string()]),
                    secret_name: Some("api-tls".to_string()),
                }]),
            }),
            network_policies: None,
        };
        assert!(networking.service.as_ref().unwrap().enabled);
        assert_eq!(
            networking.service.as_ref().unwrap().service_type,
            "ClusterIP"
        );
        assert!(networking.ingress.as_ref().unwrap().enabled);
        assert_eq!(
            networking
                .ingress
                .as_ref()
                .unwrap()
                .hosts
                .as_ref()
                .unwrap()
                .len(),
            1
        );
        assert_eq!(
            networking.ingress.as_ref().unwrap().hosts.as_ref().unwrap()[0].host,
            "api.example.com"
        );
    }
}
use std::collections::{BTreeMap, HashMap};