rustkernel-core 0.4.0

Core abstractions, traits, and registry for RustKernels GPU kernel library
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
//! Production Configuration Management
//!
//! Provides unified configuration for production deployments including:
//! - Security settings
//! - Observability configuration
//! - Resilience patterns
//! - Runtime parameters
//! - Memory management
//!
//! # Example
//!
//! ```rust,ignore
//! use rustkernel_core::config::ProductionConfig;
//!
//! // Load from environment
//! let config = ProductionConfig::from_env()?;
//!
//! // Or load from file
//! let config = ProductionConfig::from_file("config/production.toml")?;
//!
//! // Apply configuration
//! config.apply().await?;
//! ```

use crate::error::{KernelError, Result};
use crate::memory::MemoryConfig;
use crate::observability::ObservabilityConfig;
use crate::resilience::ResilienceConfig;
use crate::runtime::RuntimeConfig;
use crate::security::SecurityConfig;
use serde::{Deserialize, Serialize};
use std::path::Path;

/// Unified production configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProductionConfig {
    /// Security configuration
    pub security: SecurityConfig,
    /// Observability configuration
    pub observability: ObservabilityConfig,
    /// Resilience configuration
    pub resilience: ResilienceConfig,
    /// Runtime configuration
    pub runtime: RuntimeConfig,
    /// Memory configuration
    pub memory: MemoryConfig,
    /// Environment name
    pub environment: String,
    /// Service name
    pub service_name: String,
    /// Service version
    pub service_version: String,
}

impl Default for ProductionConfig {
    fn default() -> Self {
        Self {
            security: SecurityConfig::default(),
            observability: ObservabilityConfig::default(),
            resilience: ResilienceConfig::default(),
            runtime: RuntimeConfig::default(),
            memory: MemoryConfig::default(),
            environment: "development".to_string(),
            service_name: "rustkernels".to_string(),
            service_version: env!("CARGO_PKG_VERSION").to_string(),
        }
    }
}

impl ProductionConfig {
    /// Create development configuration
    pub fn development() -> Self {
        Self {
            security: SecurityConfig::development(),
            observability: ObservabilityConfig::development(),
            resilience: ResilienceConfig::development(),
            runtime: RuntimeConfig::development(),
            memory: MemoryConfig::development(),
            environment: "development".to_string(),
            ..Default::default()
        }
    }

    /// Create production configuration
    pub fn production() -> Self {
        Self {
            security: SecurityConfig::production(),
            observability: ObservabilityConfig::production(),
            resilience: ResilienceConfig::production(),
            runtime: RuntimeConfig::production(),
            memory: MemoryConfig::production(),
            environment: "production".to_string(),
            ..Default::default()
        }
    }

    /// Create high-performance configuration
    pub fn high_performance() -> Self {
        Self {
            security: SecurityConfig::default(),
            observability: ObservabilityConfig::default(),
            resilience: ResilienceConfig::production(), // Use production resilience for high-perf
            runtime: RuntimeConfig::high_performance(),
            memory: MemoryConfig::high_performance(),
            environment: "high-performance".to_string(),
            ..Default::default()
        }
    }

    /// Load configuration from environment variables
    pub fn from_env() -> Result<Self> {
        let mut config = match std::env::var("RUSTKERNEL_ENV")
            .as_deref()
            .unwrap_or("development")
        {
            "production" | "prod" => Self::production(),
            "high-performance" | "hp" => Self::high_performance(),
            _ => Self::development(),
        };

        // Override with specific environment variables
        if let Ok(name) = std::env::var("RUSTKERNEL_SERVICE_NAME") {
            config.service_name = name;
        }

        if let Ok(version) = std::env::var("RUSTKERNEL_SERVICE_VERSION") {
            config.service_version = version;
        }

        // Security overrides
        if std::env::var("RUSTKERNEL_AUTH_ENABLED").is_ok() {
            config.security.rbac_enabled = true;
        }

        if std::env::var("RUSTKERNEL_MULTI_TENANT").is_ok() {
            config.security.multi_tenancy_enabled = true;
        }

        // Runtime overrides
        if let Ok(val) = std::env::var("RUSTKERNEL_GPU_ENABLED") {
            config.runtime.gpu_enabled = val.parse().unwrap_or(true);
        }

        if let Ok(val) = std::env::var("RUSTKERNEL_MAX_INSTANCES") {
            if let Ok(n) = val.parse() {
                config.runtime.max_kernel_instances = n;
            }
        }

        // Memory overrides
        if let Ok(val) = std::env::var("RUSTKERNEL_MAX_GPU_MEMORY_GB") {
            if let Ok(gb) = val.parse::<u64>() {
                config.memory.max_gpu_memory = gb * 1024 * 1024 * 1024;
            }
        }

        Ok(config)
    }

    /// Load configuration from a TOML file
    pub fn from_file(path: impl AsRef<Path>) -> Result<Self> {
        let content = std::fs::read_to_string(path.as_ref())
            .map_err(|e| KernelError::ConfigError(format!("Failed to read config: {}", e)))?;

        toml::from_str(&content)
            .map_err(|e| KernelError::ConfigError(format!("Failed to parse config: {}", e)))
    }

    /// Save configuration to a TOML file
    pub fn to_file(&self, path: impl AsRef<Path>) -> Result<()> {
        let content = toml::to_string_pretty(self)
            .map_err(|e| KernelError::ConfigError(format!("Failed to serialize config: {}", e)))?;

        std::fs::write(path.as_ref(), content)
            .map_err(|e| KernelError::ConfigError(format!("Failed to write config: {}", e)))?;

        Ok(())
    }

    /// Validate the configuration
    pub fn validate(&self) -> Result<()> {
        self.runtime
            .validate()
            .map_err(|e| KernelError::ConfigError(e.to_string()))?;

        // Additional validation
        if self.environment == "production" {
            // Production should have security enabled
            if !self.security.rbac_enabled && self.security.auth.is_none() {
                tracing::warn!("Production environment without authentication or RBAC enabled");
            }
        }

        Ok(())
    }

    /// Set environment
    pub fn with_environment(mut self, env: impl Into<String>) -> Self {
        self.environment = env.into();
        self
    }

    /// Set service name
    pub fn with_service_name(mut self, name: impl Into<String>) -> Self {
        self.service_name = name.into();
        self
    }

    /// Set service version
    pub fn with_service_version(mut self, version: impl Into<String>) -> Self {
        self.service_version = version.into();
        self
    }

    /// Set security configuration
    pub fn with_security(mut self, config: SecurityConfig) -> Self {
        self.security = config;
        self
    }

    /// Set observability configuration
    pub fn with_observability(mut self, config: ObservabilityConfig) -> Self {
        self.observability = config;
        self
    }

    /// Set resilience configuration
    pub fn with_resilience(mut self, config: ResilienceConfig) -> Self {
        self.resilience = config;
        self
    }

    /// Set runtime configuration
    pub fn with_runtime(mut self, config: RuntimeConfig) -> Self {
        self.runtime = config;
        self
    }

    /// Set memory configuration
    pub fn with_memory(mut self, config: MemoryConfig) -> Self {
        self.memory = config;
        self
    }
}

/// Configuration builder
#[derive(Default)]
pub struct ProductionConfigBuilder {
    config: ProductionConfig,
}

impl ProductionConfigBuilder {
    /// Create new builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Start from production preset
    pub fn production() -> Self {
        Self {
            config: ProductionConfig::production(),
        }
    }

    /// Start from development preset
    pub fn development() -> Self {
        Self {
            config: ProductionConfig::development(),
        }
    }

    /// Start from high-performance preset
    pub fn high_performance() -> Self {
        Self {
            config: ProductionConfig::high_performance(),
        }
    }

    /// Set environment
    pub fn environment(mut self, env: impl Into<String>) -> Self {
        self.config.environment = env.into();
        self
    }

    /// Set service name
    pub fn service_name(mut self, name: impl Into<String>) -> Self {
        self.config.service_name = name.into();
        self
    }

    /// Configure security
    pub fn security(mut self, f: impl FnOnce(SecurityConfig) -> SecurityConfig) -> Self {
        self.config.security = f(self.config.security);
        self
    }

    /// Configure observability
    pub fn observability(
        mut self,
        f: impl FnOnce(ObservabilityConfig) -> ObservabilityConfig,
    ) -> Self {
        self.config.observability = f(self.config.observability);
        self
    }

    /// Configure resilience
    pub fn resilience(mut self, f: impl FnOnce(ResilienceConfig) -> ResilienceConfig) -> Self {
        self.config.resilience = f(self.config.resilience);
        self
    }

    /// Configure runtime
    pub fn runtime(mut self, f: impl FnOnce(RuntimeConfig) -> RuntimeConfig) -> Self {
        self.config.runtime = f(self.config.runtime);
        self
    }

    /// Configure memory
    pub fn memory(mut self, f: impl FnOnce(MemoryConfig) -> MemoryConfig) -> Self {
        self.config.memory = f(self.config.memory);
        self
    }

    /// Build and validate the configuration
    pub fn build(self) -> Result<ProductionConfig> {
        self.config.validate()?;
        Ok(self.config)
    }

    /// Build without validation
    pub fn build_unchecked(self) -> ProductionConfig {
        self.config
    }
}

/// Health endpoint configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthEndpointConfig {
    /// Enable health endpoints
    pub enabled: bool,
    /// Liveness endpoint path
    pub liveness_path: String,
    /// Readiness endpoint path
    pub readiness_path: String,
    /// Startup endpoint path
    pub startup_path: String,
    /// Include detailed health info
    pub detailed: bool,
}

impl Default for HealthEndpointConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            liveness_path: "/health/live".to_string(),
            readiness_path: "/health/ready".to_string(),
            startup_path: "/health/startup".to_string(),
            detailed: false,
        }
    }
}

/// Metrics endpoint configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricsEndpointConfig {
    /// Enable metrics endpoint
    pub enabled: bool,
    /// Metrics endpoint path
    pub path: String,
    /// Include detailed histogram buckets
    pub detailed_histograms: bool,
}

impl Default for MetricsEndpointConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            path: "/metrics".to_string(),
            detailed_histograms: false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = ProductionConfig::default();
        assert_eq!(config.environment, "development");
        assert_eq!(config.service_name, "rustkernels");
    }

    #[test]
    fn test_production_config() {
        let config = ProductionConfig::production();
        assert_eq!(config.environment, "production");
        assert!(config.security.rbac_enabled);
        assert!(config.security.audit_logging);
    }

    #[test]
    fn test_development_config() {
        let config = ProductionConfig::development();
        assert_eq!(config.environment, "development");
    }

    #[test]
    fn test_builder() {
        let config = ProductionConfigBuilder::production()
            .service_name("test-service")
            .environment("staging")
            .build_unchecked();

        assert_eq!(config.service_name, "test-service");
        assert_eq!(config.environment, "staging");
    }

    #[test]
    fn test_config_validation() {
        let config = ProductionConfig::development();
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_with_methods() {
        let config = ProductionConfig::default()
            .with_environment("staging")
            .with_service_name("my-service");

        assert_eq!(config.environment, "staging");
        assert_eq!(config.service_name, "my-service");
    }
}