blueprint-remote-providers 0.2.0-alpha.2

Remote service providers for Tangle Blueprints
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
//! Shared Kubernetes deployment patterns across providers
//!
//! This module consolidates Kubernetes deployment logic that's
//! duplicated across all cloud provider adapters. Provides real
//! cluster authentication and provider-specific cluster setup.

#![cfg(feature = "kubernetes")]

use crate::core::error::{Error, Result};
use crate::core::resources::ResourceSpec;
use crate::deployment::kubernetes::KubernetesDeploymentClient;
use crate::infra::traits::BlueprintDeploymentResult;
use crate::infra::types::{InstanceStatus, ProvisionedInstance};
use blueprint_core::{info, warn};
use blueprint_std::collections::HashMap;
use std::process::Command;

/// Shared Kubernetes deployment implementation
pub struct SharedKubernetesDeployment;

impl SharedKubernetesDeployment {
    /// Deploy to managed Kubernetes service (EKS/GKE/AKS/DOKS/VKE) with cluster authentication
    pub async fn deploy_to_managed_k8s(
        cluster_id: &str,
        namespace: &str,
        blueprint_image: &str,
        resource_spec: &ResourceSpec,
        env_vars: HashMap<String, String>,
        provider_config: ManagedK8sConfig,
    ) -> Result<BlueprintDeploymentResult> {
        info!(
            "Deploying to {} cluster: {} with {} environment variables",
            provider_config.service_name,
            cluster_id,
            env_vars.len()
        );

        // Authenticate to the managed cluster
        Self::setup_cluster_authentication(cluster_id, &provider_config).await?;

        // Verify cluster connectivity
        Self::verify_cluster_health(cluster_id, &provider_config).await?;

        let k8s_client = KubernetesDeploymentClient::new(Some(namespace.to_string())).await?;

        let (deployment_id, exposed_ports) = k8s_client
            .deploy_blueprint("blueprint", blueprint_image, resource_spec, 1, env_vars)
            .await?;

        let mut port_mappings = HashMap::new();
        for port in exposed_ports {
            port_mappings.insert(port, port);
        }

        let mut metadata = HashMap::new();
        metadata.insert(
            "provider".to_string(),
            provider_config.provider_identifier.clone(),
        );
        metadata.insert("cluster_id".to_string(), cluster_id.to_string());
        metadata.insert("namespace".to_string(), namespace.to_string());

        // Add provider-specific metadata
        for (key, value) in provider_config.additional_metadata {
            metadata.insert(key, value);
        }

        let instance = ProvisionedInstance {
            id: format!("{}-{}", provider_config.instance_prefix, cluster_id),
            public_ip: None, // K8s service handles routing
            private_ip: None,
            status: InstanceStatus::Running,
            provider: provider_config.cloud_provider,
            region: provider_config.default_region,
            instance_type: format!("{}-cluster", provider_config.service_name),
        };

        Ok(BlueprintDeploymentResult {
            instance,
            blueprint_id: deployment_id,
            port_mappings,
            metadata,
        })
    }

    /// Setup authentication to managed Kubernetes cluster
    async fn setup_cluster_authentication(
        cluster_id: &str,
        config: &ManagedK8sConfig,
    ) -> Result<()> {
        info!(
            "Setting up {} cluster authentication for: {}",
            config.service_name, cluster_id
        );

        match config.cloud_provider {
            crate::core::remote::CloudProvider::AWS => {
                Self::setup_eks_auth(cluster_id, &config.default_region).await
            }
            crate::core::remote::CloudProvider::GCP => {
                Self::setup_gke_auth(
                    cluster_id,
                    &config.default_region,
                    &config.additional_metadata,
                )
                .await
            }
            crate::core::remote::CloudProvider::Azure => {
                Self::setup_aks_auth(cluster_id, &config.additional_metadata).await
            }
            crate::core::remote::CloudProvider::DigitalOcean => {
                Self::setup_doks_auth(cluster_id).await
            }
            crate::core::remote::CloudProvider::Vultr => Self::setup_vke_auth(cluster_id).await,
            _ => {
                warn!(
                    "No specific cluster authentication setup for provider: {:?}",
                    config.cloud_provider
                );
                Ok(())
            }
        }
    }

    /// Setup AWS EKS cluster authentication
    async fn setup_eks_auth(cluster_id: &str, region: &str) -> Result<()> {
        info!(
            "Configuring EKS cluster {} in region {}",
            cluster_id, region
        );

        let output = Command::new("aws")
            .args(&[
                "eks",
                "update-kubeconfig",
                "--region",
                region,
                "--name",
                cluster_id,
            ])
            .output()
            .map_err(|e| {
                Error::ConfigurationError(format!("Failed to run aws eks update-kubeconfig: {}", e))
            })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::ConfigurationError(format!(
                "AWS EKS kubeconfig update failed: {}",
                stderr
            )));
        }

        info!(
            "EKS cluster {} authentication configured successfully",
            cluster_id
        );
        Ok(())
    }

    /// Setup GCP GKE cluster authentication
    async fn setup_gke_auth(
        cluster_id: &str,
        region: &str,
        metadata: &HashMap<String, String>,
    ) -> Result<()> {
        let project_id = metadata.get("project_id").ok_or_else(|| {
            Error::ConfigurationError("GKE requires project_id in metadata".into())
        })?;

        info!(
            "Configuring GKE cluster {} in project {} region {}",
            cluster_id, project_id, region
        );

        let output = Command::new("gcloud")
            .args(&[
                "container",
                "clusters",
                "get-credentials",
                cluster_id,
                "--region",
                region,
                "--project",
                project_id,
            ])
            .output()
            .map_err(|e| {
                Error::ConfigurationError(format!("Failed to run gcloud get-credentials: {}", e))
            })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::ConfigurationError(format!(
                "GCP GKE kubeconfig update failed: {}",
                stderr
            )));
        }

        info!(
            "GKE cluster {} authentication configured successfully",
            cluster_id
        );
        Ok(())
    }

    /// Setup Azure AKS cluster authentication
    async fn setup_aks_auth(cluster_id: &str, metadata: &HashMap<String, String>) -> Result<()> {
        let resource_group = metadata.get("resource_group").ok_or_else(|| {
            Error::ConfigurationError("AKS requires resource_group in metadata".into())
        })?;

        info!(
            "Configuring AKS cluster {} in resource group {}",
            cluster_id, resource_group
        );

        let output = Command::new("az")
            .args(&[
                "aks",
                "get-credentials",
                "--resource-group",
                resource_group,
                "--name",
                cluster_id,
            ])
            .output()
            .map_err(|e| {
                Error::ConfigurationError(format!("Failed to run az aks get-credentials: {}", e))
            })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::ConfigurationError(format!(
                "Azure AKS kubeconfig update failed: {}",
                stderr
            )));
        }

        info!(
            "AKS cluster {} authentication configured successfully",
            cluster_id
        );
        Ok(())
    }

    /// Setup DigitalOcean DOKS cluster authentication
    async fn setup_doks_auth(cluster_id: &str) -> Result<()> {
        info!("Configuring DOKS cluster {}", cluster_id);

        let output = Command::new("doctl")
            .args(&["kubernetes", "cluster", "kubeconfig", "save", cluster_id])
            .output()
            .map_err(|e| {
                Error::ConfigurationError(format!("Failed to run doctl kubeconfig save: {}", e))
            })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::ConfigurationError(format!(
                "DigitalOcean DOKS kubeconfig update failed: {}",
                stderr
            )));
        }

        info!(
            "DOKS cluster {} authentication configured successfully",
            cluster_id
        );
        Ok(())
    }

    /// Setup Vultr VKE cluster authentication
    async fn setup_vke_auth(cluster_id: &str) -> Result<()> {
        info!("Configuring VKE cluster {}", cluster_id);

        // Note: vultr-cli doesn't have direct kubeconfig download, would need API call
        warn!(
            "VKE cluster authentication requires manual kubeconfig setup for cluster {}",
            cluster_id
        );

        // For now, assume kubeconfig is already configured
        // In production, would make Vultr API call to get kubeconfig
        Ok(())
    }

    /// Verify cluster health before deployment
    async fn verify_cluster_health(cluster_id: &str, config: &ManagedK8sConfig) -> Result<()> {
        info!(
            "Verifying {} cluster health: {}",
            config.service_name, cluster_id
        );

        let output = Command::new("kubectl")
            .args(&["cluster-info", "--request-timeout=10s"])
            .output()
            .map_err(|e| {
                Error::ConfigurationError(format!("Failed to run kubectl cluster-info: {}", e))
            })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::ConfigurationError(format!(
                "Cluster {} health check failed: {}",
                cluster_id, stderr
            )));
        }

        info!("Cluster {} is healthy and ready for deployment", cluster_id);
        Ok(())
    }

    /// Deploy to generic Kubernetes cluster
    pub async fn deploy_to_generic_k8s(
        namespace: &str,
        blueprint_image: &str,
        resource_spec: &ResourceSpec,
        env_vars: HashMap<String, String>,
    ) -> Result<BlueprintDeploymentResult> {
        info!(
            "Deploying to generic Kubernetes namespace: {} with {} environment variables",
            namespace,
            env_vars.len()
        );

        let k8s_client = KubernetesDeploymentClient::new(Some(namespace.to_string())).await?;

        let (deployment_id, exposed_ports) = k8s_client
            .deploy_blueprint("blueprint", blueprint_image, resource_spec, 1, env_vars)
            .await?;

        let mut port_mappings = HashMap::new();
        for port in exposed_ports {
            port_mappings.insert(port, port);
        }

        let mut metadata = HashMap::new();
        metadata.insert("provider".to_string(), "generic-k8s".to_string());
        metadata.insert("namespace".to_string(), namespace.to_string());

        let instance = ProvisionedInstance {
            id: format!("k8s-{}", namespace),
            public_ip: None,
            private_ip: None,
            status: InstanceStatus::Running,
            provider: crate::core::remote::CloudProvider::Generic,
            region: "generic".to_string(),
            instance_type: "kubernetes-cluster".to_string(),
        };

        Ok(BlueprintDeploymentResult {
            instance,
            blueprint_id: deployment_id,
            port_mappings,
            metadata,
        })
    }
}

/// Configuration for managed Kubernetes services
pub struct ManagedK8sConfig {
    pub service_name: &'static str,
    pub provider_identifier: String,
    pub instance_prefix: &'static str,
    pub cloud_provider: crate::core::remote::CloudProvider,
    pub default_region: String,
    pub additional_metadata: HashMap<String, String>,
}

impl ManagedK8sConfig {
    /// AWS EKS configuration
    pub fn eks(region: &str) -> Self {
        Self {
            service_name: "EKS",
            provider_identifier: "aws-eks".to_string(),
            instance_prefix: "eks",
            cloud_provider: crate::core::remote::CloudProvider::AWS,
            default_region: region.to_string(),
            additional_metadata: HashMap::new(),
        }
    }

    /// GCP GKE configuration
    pub fn gke(project_id: &str, region: &str) -> Self {
        let mut metadata = HashMap::new();
        metadata.insert("project_id".to_string(), project_id.to_string());

        Self {
            service_name: "GKE",
            provider_identifier: "gcp-gke".to_string(),
            instance_prefix: "gke",
            cloud_provider: crate::core::remote::CloudProvider::GCP,
            default_region: region.to_string(),
            additional_metadata: metadata,
        }
    }

    /// Azure AKS configuration
    pub fn aks(region: &str, resource_group: &str) -> Self {
        let mut metadata = HashMap::new();
        metadata.insert("resource_group".to_string(), resource_group.to_string());

        Self {
            service_name: "AKS",
            provider_identifier: "azure-aks".to_string(),
            instance_prefix: "aks",
            cloud_provider: crate::core::remote::CloudProvider::Azure,
            default_region: region.to_string(),
            additional_metadata: metadata,
        }
    }

    /// DigitalOcean DOKS configuration
    pub fn doks(region: &str) -> Self {
        Self {
            service_name: "DOKS",
            provider_identifier: "digitalocean-doks".to_string(),
            instance_prefix: "doks",
            cloud_provider: crate::core::remote::CloudProvider::DigitalOcean,
            default_region: region.to_string(),
            additional_metadata: HashMap::new(),
        }
    }

    /// Vultr VKE configuration
    pub fn vke(region: &str) -> Self {
        Self {
            service_name: "VKE",
            provider_identifier: "vultr-vke".to_string(),
            instance_prefix: "vke",
            cloud_provider: crate::core::remote::CloudProvider::Vultr,
            default_region: region.to_string(),
            additional_metadata: HashMap::new(),
        }
    }
}

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

    #[test]
    fn test_managed_k8s_config_eks() {
        let config = ManagedK8sConfig::eks("us-west-2");
        assert_eq!(config.service_name, "EKS");
        assert_eq!(config.provider_identifier, "aws-eks");
        assert_eq!(config.default_region, "us-west-2");
        assert_eq!(config.instance_prefix, "eks");
        assert!(matches!(
            config.cloud_provider,
            crate::core::remote::CloudProvider::AWS
        ));
    }

    #[test]
    fn test_managed_k8s_config_gke() {
        let config = ManagedK8sConfig::gke("my-project", "us-central1");
        assert_eq!(config.service_name, "GKE");
        assert_eq!(config.provider_identifier, "gcp-gke");
        assert_eq!(config.default_region, "us-central1");
        assert_eq!(
            config.additional_metadata.get("project_id").unwrap(),
            "my-project"
        );
        assert!(matches!(
            config.cloud_provider,
            crate::core::remote::CloudProvider::GCP
        ));
    }

    #[test]
    fn test_managed_k8s_config_aks() {
        let config = ManagedK8sConfig::aks("eastus", "my-resource-group");
        assert_eq!(config.service_name, "AKS");
        assert_eq!(config.provider_identifier, "azure-aks");
        assert_eq!(config.default_region, "eastus");
        assert_eq!(
            config.additional_metadata.get("resource_group").unwrap(),
            "my-resource-group"
        );
        assert!(matches!(
            config.cloud_provider,
            crate::core::remote::CloudProvider::Azure
        ));
    }

    #[test]
    fn test_managed_k8s_config_doks() {
        let config = ManagedK8sConfig::doks("nyc3");
        assert_eq!(config.service_name, "DOKS");
        assert_eq!(config.provider_identifier, "digitalocean-doks");
        assert_eq!(config.default_region, "nyc3");
        assert!(matches!(
            config.cloud_provider,
            crate::core::remote::CloudProvider::DigitalOcean
        ));
    }

    #[test]
    fn test_managed_k8s_config_vke() {
        let config = ManagedK8sConfig::vke("ewr");
        assert_eq!(config.service_name, "VKE");
        assert_eq!(config.provider_identifier, "vultr-vke");
        assert_eq!(config.default_region, "ewr");
        assert!(matches!(
            config.cloud_provider,
            crate::core::remote::CloudProvider::Vultr
        ));
    }

    #[tokio::test]
    async fn test_deploy_to_generic_k8s_signature() {
        // Test that the method signature is correct and env_vars are passed
        let mut env_vars = HashMap::new();
        env_vars.insert("TEST_VAR".to_string(), "test_value".to_string());

        // This will fail without a real cluster, but tests the signature
        let result = SharedKubernetesDeployment::deploy_to_generic_k8s(
            "test-namespace",
            "nginx:latest",
            &ResourceSpec::basic(),
            env_vars,
        )
        .await;

        // We expect an error since there's no actual cluster
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_deploy_to_managed_k8s_signature() {
        // Test that the method signature is correct and env_vars are passed
        let mut env_vars = HashMap::new();
        env_vars.insert("API_KEY".to_string(), "secret".to_string());
        env_vars.insert(
            "DATABASE_URL".to_string(),
            "postgres://localhost".to_string(),
        );

        let config = ManagedK8sConfig::eks("us-east-1");

        // This will fail without a real cluster, but tests the signature
        let result = SharedKubernetesDeployment::deploy_to_managed_k8s(
            "test-cluster",
            "production",
            "myapp:v1.0",
            &ResourceSpec::recommended(),
            env_vars,
            config,
        )
        .await;

        // We expect an error since there's no actual cluster
        assert!(result.is_err());
    }
}