oxirs-stream 0.2.4

Real-time streaming support with Kafka/NATS/MQTT/OPC-UA I/O, RDF Patch, and SPARQL Update delta
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! Kubernetes Integration Types

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub struct KubernetesConfig {
    /// Enable Kubernetes integration
    pub enabled: bool,
    /// Namespace for OxiRS resources
    pub namespace: String,
    /// Custom Resource Definitions
    pub crds: Vec<CustomResourceDefinition>,
    /// Operator configuration
    pub operator: OperatorConfig,
    /// Health check configuration
    pub health_checks: HealthCheckConfig,
    /// Network policies
    pub network_policies: NetworkPolicyConfig,
    /// Resource quotas
    pub resource_quotas: ResourceQuotaConfig,
}

impl Default for KubernetesConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            namespace: "oxirs".to_string(),
            crds: vec![
                CustomResourceDefinition::stream_processor(),
                CustomResourceDefinition::stream_cluster(),
                CustomResourceDefinition::stream_policy(),
            ],
            operator: OperatorConfig::default(),
            health_checks: HealthCheckConfig::default(),
            network_policies: NetworkPolicyConfig::default(),
            resource_quotas: ResourceQuotaConfig::default(),
        }
    }
}

/// Custom Resource Definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomResourceDefinition {
    pub api_version: String,
    pub kind: String,
    pub metadata: ObjectMetadata,
    pub spec: CRDSpec,
}

impl CustomResourceDefinition {
    /// Create StreamProcessor CRD
    pub fn stream_processor() -> Self {
        Self {
            api_version: "apiextensions.k8s.io/v1".to_string(),
            kind: "CustomResourceDefinition".to_string(),
            metadata: ObjectMetadata {
                name: "streamprocessors.oxirs.io".to_string(),
                namespace: None,
                labels: BTreeMap::from([
                    ("app".to_string(), "oxirs".to_string()),
                    ("component".to_string(), "stream-processor".to_string()),
                ]),
                annotations: BTreeMap::new(),
            },
            spec: CRDSpec {
                group: "oxirs.io".to_string(),
                versions: vec![CRDVersion {
                    name: "v1".to_string(),
                    served: true,
                    storage: true,
                    schema: ResourceSchema::stream_processor_schema(),
                }],
                scope: "Namespaced".to_string(),
                names: CRDNames {
                    plural: "streamprocessors".to_string(),
                    singular: "streamprocessor".to_string(),
                    kind: "StreamProcessor".to_string(),
                    short_names: vec!["sp".to_string()],
                },
            },
        }
    }

    /// Create StreamCluster CRD
    pub fn stream_cluster() -> Self {
        Self {
            api_version: "apiextensions.k8s.io/v1".to_string(),
            kind: "CustomResourceDefinition".to_string(),
            metadata: ObjectMetadata {
                name: "streamclusters.oxirs.io".to_string(),
                namespace: None,
                labels: BTreeMap::from([
                    ("app".to_string(), "oxirs".to_string()),
                    ("component".to_string(), "stream-cluster".to_string()),
                ]),
                annotations: BTreeMap::new(),
            },
            spec: CRDSpec {
                group: "oxirs.io".to_string(),
                versions: vec![CRDVersion {
                    name: "v1".to_string(),
                    served: true,
                    storage: true,
                    schema: ResourceSchema::stream_cluster_schema(),
                }],
                scope: "Namespaced".to_string(),
                names: CRDNames {
                    plural: "streamclusters".to_string(),
                    singular: "streamcluster".to_string(),
                    kind: "StreamCluster".to_string(),
                    short_names: vec!["sc".to_string()],
                },
            },
        }
    }

    /// Create StreamPolicy CRD
    pub fn stream_policy() -> Self {
        Self {
            api_version: "apiextensions.k8s.io/v1".to_string(),
            kind: "CustomResourceDefinition".to_string(),
            metadata: ObjectMetadata {
                name: "streampolicies.oxirs.io".to_string(),
                namespace: None,
                labels: BTreeMap::from([
                    ("app".to_string(), "oxirs".to_string()),
                    ("component".to_string(), "stream-policy".to_string()),
                ]),
                annotations: BTreeMap::new(),
            },
            spec: CRDSpec {
                group: "oxirs.io".to_string(),
                versions: vec![CRDVersion {
                    name: "v1".to_string(),
                    served: true,
                    storage: true,
                    schema: ResourceSchema::stream_policy_schema(),
                }],
                scope: "Namespaced".to_string(),
                names: CRDNames {
                    plural: "streampolicies".to_string(),
                    singular: "streampolicy".to_string(),
                    kind: "StreamPolicy".to_string(),
                    short_names: vec!["spol".to_string()],
                },
            },
        }
    }
}

/// Object metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObjectMetadata {
    pub name: String,
    pub namespace: Option<String>,
    pub labels: BTreeMap<String, String>,
    pub annotations: BTreeMap<String, String>,
}

/// CRD specification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CRDSpec {
    pub group: String,
    pub versions: Vec<CRDVersion>,
    pub scope: String,
    pub names: CRDNames,
}

/// CRD version
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CRDVersion {
    pub name: String,
    pub served: bool,
    pub storage: bool,
    pub schema: ResourceSchema,
}

/// CRD names
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CRDNames {
    pub plural: String,
    pub singular: String,
    pub kind: String,
    pub short_names: Vec<String>,
}

/// Resource schema
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceSchema {
    pub open_api_v3_schema: OpenAPIV3Schema,
}

impl ResourceSchema {
    /// StreamProcessor schema
    pub fn stream_processor_schema() -> Self {
        Self {
            open_api_v3_schema: OpenAPIV3Schema {
                schema_type: "object".to_string(),
                properties: BTreeMap::from([
                    ("spec".to_string(), SchemaProperty::stream_processor_spec()),
                    ("status".to_string(), SchemaProperty::stream_processor_status()),
                ]),
                required: vec!["spec".to_string()],
            },
        }
    }

    /// StreamCluster schema
    pub fn stream_cluster_schema() -> Self {
        Self {
            open_api_v3_schema: OpenAPIV3Schema {
                schema_type: "object".to_string(),
                properties: BTreeMap::from([
                    ("spec".to_string(), SchemaProperty::stream_cluster_spec()),
                    ("status".to_string(), SchemaProperty::stream_cluster_status()),
                ]),
                required: vec!["spec".to_string()],
            },
        }
    }

    /// StreamPolicy schema
    pub fn stream_policy_schema() -> Self {
        Self {
            open_api_v3_schema: OpenAPIV3Schema {
                schema_type: "object".to_string(),
                properties: BTreeMap::from([
                    ("spec".to_string(), SchemaProperty::stream_policy_spec()),
                    ("status".to_string(), SchemaProperty::stream_policy_status()),
                ]),
                required: vec!["spec".to_string()],
            },
        }
    }
}

/// OpenAPI v3 schema
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenAPIV3Schema {
    #[serde(rename = "type")]
    pub schema_type: String,
    pub properties: BTreeMap<String, SchemaProperty>,
    pub required: Vec<String>,
}

/// Schema property
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaProperty {
    #[serde(rename = "type")]
    pub property_type: String,
    pub description: Option<String>,
    pub properties: Option<BTreeMap<String, SchemaProperty>>,
    pub items: Option<Box<SchemaProperty>>,
    pub required: Option<Vec<String>>,
}

impl SchemaProperty {
    /// StreamProcessor spec schema
    pub fn stream_processor_spec() -> Self {
        Self {
            property_type: "object".to_string(),
            description: Some("StreamProcessor specification".to_string()),
            properties: Some(BTreeMap::from([
                ("replicas".to_string(), SchemaProperty {
                    property_type: "integer".to_string(),
                    description: Some("Number of replicas".to_string()),
                    properties: None,
                    items: None,
                    required: None,
                }),
                ("image".to_string(), SchemaProperty {
                    property_type: "string".to_string(),
                    description: Some("Container image".to_string()),
                    properties: None,
                    items: None,
                    required: None,
                }),
                ("config".to_string(), SchemaProperty {
                    property_type: "object".to_string(),
                    description: Some("Configuration".to_string()),
                    properties: None,
                    items: None,
                    required: None,
                }),
            ])),
            items: None,
            required: Some(vec!["replicas".to_string(), "image".to_string()]),
        }
    }

    /// StreamProcessor status schema
    pub fn stream_processor_status() -> Self {
        Self {
            property_type: "object".to_string(),
            description: Some("StreamProcessor status".to_string()),
            properties: Some(BTreeMap::from([
                ("phase".to_string(), SchemaProperty {
                    property_type: "string".to_string(),
                    description: Some("Current phase".to_string()),
                    properties: None,
                    items: None,
                    required: None,
                }),
                ("ready_replicas".to_string(), SchemaProperty {
                    property_type: "integer".to_string(),
                    description: Some("Number of ready replicas".to_string()),
                    properties: None,
                    items: None,
                    required: None,
                }),
            ])),
            items: None,
            required: None,
        }
    }

    /// StreamCluster spec schema
    pub fn stream_cluster_spec() -> Self {
        Self {
            property_type: "object".to_string(),
            description: Some("StreamCluster specification".to_string()),
            properties: Some(BTreeMap::from([
                ("size".to_string(), SchemaProperty {
                    property_type: "integer".to_string(),
                    description: Some("Cluster size".to_string()),
                    properties: None,
                    items: None,
                    required: None,
                }),
                ("storage".to_string(), SchemaProperty {
                    property_type: "object".to_string(),
                    description: Some("Storage configuration".to_string()),
                    properties: None,
                    items: None,
                    required: None,
                }),
            ])),
            items: None,
            required: Some(vec!["size".to_string()]),
        }
    }

    /// StreamCluster status schema
    pub fn stream_cluster_status() -> Self {
        Self {
            property_type: "object".to_string(),
            description: Some("StreamCluster status".to_string()),
            properties: Some(BTreeMap::from([
                ("phase".to_string(), SchemaProperty {
                    property_type: "string".to_string(),
                    description: Some("Current phase".to_string()),
                    properties: None,
                    items: None,
                    required: None,
                }),
                ("nodes".to_string(), SchemaProperty {
                    property_type: "array".to_string(),
                    description: Some("Cluster nodes".to_string()),
                    properties: None,
                    items: Some(Box::new(SchemaProperty {
                        property_type: "object".to_string(),
                        description: None,
                        properties: None,
                        items: None,
                        required: None,
                    })),
                    required: None,
                }),
            ])),
            items: None,
            required: None,
        }
    }

    /// StreamPolicy spec schema
    pub fn stream_policy_spec() -> Self {
        Self {
            property_type: "object".to_string(),
            description: Some("StreamPolicy specification".to_string()),
            properties: Some(BTreeMap::from([
                ("rules".to_string(), SchemaProperty {
                    property_type: "array".to_string(),
                    description: Some("Policy rules".to_string()),
                    properties: None,
                    items: Some(Box::new(SchemaProperty {
                        property_type: "object".to_string(),
                        description: None,
                        properties: None,
                        items: None,
                        required: None,
                    })),
                    required: None,
                }),
            ])),
            items: None,
            required: Some(vec!["rules".to_string()]),
        }
    }

    /// StreamPolicy status schema
    pub fn stream_policy_status() -> Self {
        Self {
            property_type: "object".to_string(),
            description: Some("StreamPolicy status".to_string()),
            properties: Some(BTreeMap::from([
                ("applied".to_string(), SchemaProperty {
                    property_type: "boolean".to_string(),
                    description: Some("Policy applied status".to_string()),
                    properties: None,
                    items: None,
                    required: None,
                }),
            ])),
            items: None,
            required: None,
        }
    }
}

/// Operator configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperatorConfig {
    /// Enable operator
    pub enabled: bool,
    /// Operator image
    pub image: String,
    /// Watch all namespaces
    pub cluster_scoped: bool,
    /// Reconciliation interval
    pub reconcile_interval: Duration,
    /// Leader election configuration
    pub leader_election: LeaderElectionConfig,
}

impl Default for OperatorConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            image: "oxirs/operator:latest".to_string(),
            cluster_scoped: false,
            reconcile_interval: Duration::from_secs(30),
            leader_election: LeaderElectionConfig::default(),
        }
    }
}

/// Leader election configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LeaderElectionConfig {
    pub enabled: bool,
    pub lock_name: String,
    pub lease_duration: Duration,
    pub renew_deadline: Duration,
    pub retry_period: Duration,
}

impl Default for LeaderElectionConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            lock_name: "oxirs-operator-lock".to_string(),
            lease_duration: Duration::from_secs(15),
            renew_deadline: Duration::from_secs(10),
            retry_period: Duration::from_secs(2),
        }
    }
}

/// Health check configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheckConfig {
    /// Enable health checks
    pub enabled: bool,
    /// Liveness probe configuration
    pub liveness_probe: ProbeConfig,
    /// Readiness probe configuration
    pub readiness_probe: ProbeConfig,
    /// Startup probe configuration
    pub startup_probe: ProbeConfig,
}

impl Default for HealthCheckConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            liveness_probe: ProbeConfig {
                path: "/health/live".to_string(),
                port: 8080,
                initial_delay_seconds: 30,
                period_seconds: 10,
                timeout_seconds: 5,
                failure_threshold: 3,
                success_threshold: 1,
            },
            readiness_probe: ProbeConfig {
                path: "/health/ready".to_string(),
                port: 8080,
                initial_delay_seconds: 10,
                period_seconds: 5,
                timeout_seconds: 3,
                failure_threshold: 3,
                success_threshold: 1,
            },
            startup_probe: ProbeConfig {
                path: "/health/startup".to_string(),
                port: 8080,
                initial_delay_seconds: 0,
                period_seconds: 5,
                timeout_seconds: 3,
                failure_threshold: 30,
                success_threshold: 1,
            },
        }
    }
}

/// Probe configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProbeConfig {
    pub path: String,
    pub port: u16,
    pub initial_delay_seconds: u32,
    pub period_seconds: u32,
    pub timeout_seconds: u32,
    pub failure_threshold: u32,
    pub success_threshold: u32,
}

/// Network policy configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkPolicyConfig {
    /// Enable network policies
    pub enabled: bool,
    /// Default deny all traffic
    pub default_deny: bool,
    /// Allowed ingress rules
    pub ingress_rules: Vec<NetworkPolicyRule>,
    /// Allowed egress rules
    pub egress_rules: Vec<NetworkPolicyRule>,
}

impl Default for NetworkPolicyConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            default_deny: true,
            ingress_rules: vec![
                NetworkPolicyRule {
                    description: "Allow ingress from service mesh".to_string(),
                    from_selector: Some(LabelSelector {
                        match_labels: BTreeMap::from([
                            ("app".to_string(), "istio-proxy".to_string()),
                        ]),
                    }),
                    ports: vec![NetworkPolicyPort {
                        port: 8080,
                        protocol: "TCP".to_string(),
                    }],
                },
            ],
            egress_rules: vec![
                NetworkPolicyRule {
                    description: "Allow egress to Kubernetes API".to_string(),
                    from_selector: None,
                    ports: vec![NetworkPolicyPort {
                        port: 443,
                        protocol: "TCP".to_string(),
                    }],
                },
            ],
        }
    }
}

/// Network policy rule
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkPolicyRule {
    pub description: String,
    pub from_selector: Option<LabelSelector>,
    pub ports: Vec<NetworkPolicyPort>,
}

/// Label selector
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LabelSelector {
    pub match_labels: BTreeMap<String, String>,
}

/// Network policy port
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkPolicyPort {
    pub port: u16,
    pub protocol: String,
}

/// Resource quota configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceQuotaConfig {
    /// Enable resource quotas
    pub enabled: bool,
    /// CPU limit
    pub cpu_limit: String,
    /// Memory limit
    pub memory_limit: String,
    /// Storage limit
    pub storage_limit: String,
    /// Pod limit
    pub pod_limit: u32,
    /// Service limit
    pub service_limit: u32,
}

impl Default for ResourceQuotaConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            cpu_limit: "10".to_string(),
            memory_limit: "20Gi".to_string(),
            storage_limit: "100Gi".to_string(),
            pod_limit: 50,
            service_limit: 10,
        }
    }
}