sherpack-kube 0.4.0

Kubernetes integration for Sherpack - storage drivers, release management, and cluster operations
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
//! CRD deletion protection and impact analysis
//!
//! This module provides safety checks before CRD deletion:
//! - Count existing CustomResources that would be deleted
//! - Analyze impact by namespace
//! - Generate confirmation requirements
//!
//! # Safety First
//!
//! Deleting a CRD cascades to ALL CustomResources of that type.
//! This module ensures users understand the impact before proceeding.

use kube::{
    Client,
    api::{Api, DynamicObject, ListParams},
    core::GroupVersionKind,
    discovery::{ApiCapabilities, ApiResource, Discovery, Scope},
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::policy::{CrdPolicy, DetectedCrd};
use crate::error::{KubeError, Result};

/// Impact analysis for CRD deletion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrdDeletionImpact {
    /// CRD name
    pub crd_name: String,
    /// CRD policy
    pub policy: CrdPolicy,
    /// Total CustomResources that would be deleted
    pub total_resources: usize,
    /// Resources by namespace (empty string = cluster-scoped)
    pub by_namespace: HashMap<String, usize>,
    /// Whether deletion is allowed by policy
    pub deletion_allowed: bool,
    /// Reason if deletion is blocked
    pub blocked_reason: Option<String>,
}

impl CrdDeletionImpact {
    /// Create an empty impact (CRD not found or no resources)
    pub fn empty(crd_name: impl Into<String>, policy: CrdPolicy) -> Self {
        Self {
            crd_name: crd_name.into(),
            policy,
            total_resources: 0,
            by_namespace: HashMap::new(),
            deletion_allowed: policy.allows_delete(),
            blocked_reason: if policy.allows_delete() {
                None
            } else {
                Some(format!("Policy '{}' does not allow deletion", policy))
            },
        }
    }

    /// Create impact from resource count
    pub fn with_resources(
        crd_name: impl Into<String>,
        policy: CrdPolicy,
        by_namespace: HashMap<String, usize>,
    ) -> Self {
        let total: usize = by_namespace.values().sum();
        Self {
            crd_name: crd_name.into(),
            policy,
            total_resources: total,
            by_namespace,
            deletion_allowed: policy.allows_delete(),
            blocked_reason: if policy.allows_delete() {
                None
            } else {
                Some(format!("Policy '{}' does not allow deletion", policy))
            },
        }
    }

    /// Check if this deletion would cause data loss
    pub fn has_data_loss(&self) -> bool {
        self.total_resources > 0
    }

    /// Check if deletion is safe (allowed and no data loss)
    pub fn is_safe(&self) -> bool {
        self.deletion_allowed && !self.has_data_loss()
    }

    /// Get sorted namespaces by resource count (descending)
    pub fn sorted_namespaces(&self) -> Vec<(&String, &usize)> {
        let mut sorted: Vec<_> = self.by_namespace.iter().collect();
        sorted.sort_by(|a, b| b.1.cmp(a.1));
        sorted
    }
}

/// Aggregate impact for multiple CRDs
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DeletionImpactSummary {
    /// Individual CRD impacts
    pub crds: Vec<CrdDeletionImpact>,
    /// Total resources across all CRDs
    pub total_resources: usize,
    /// Total CRDs that would be deleted
    pub total_crds: usize,
    /// CRDs blocked by policy
    pub blocked_crds: Vec<String>,
}

impl DeletionImpactSummary {
    /// Create a new summary
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a CRD impact
    pub fn add(&mut self, impact: CrdDeletionImpact) {
        if !impact.deletion_allowed {
            self.blocked_crds.push(impact.crd_name.clone());
        } else {
            self.total_resources += impact.total_resources;
            self.total_crds += 1;
        }
        self.crds.push(impact);
    }

    /// Check if any deletion is blocked
    pub fn has_blocked(&self) -> bool {
        !self.blocked_crds.is_empty()
    }

    /// Check if deletion would cause data loss
    pub fn has_data_loss(&self) -> bool {
        self.total_resources > 0
    }

    /// Check if all deletions are safe
    pub fn is_safe(&self) -> bool {
        !self.has_blocked() && !self.has_data_loss()
    }

    /// Get all affected namespaces
    pub fn affected_namespaces(&self) -> Vec<String> {
        let mut namespaces: Vec<_> = self
            .crds
            .iter()
            .flat_map(|c| c.by_namespace.keys())
            .cloned()
            .collect();
        namespaces.sort();
        namespaces.dedup();
        namespaces
    }
}

/// Protection checker for CRD operations
pub struct CrdProtection {
    client: Client,
}

impl CrdProtection {
    /// Create a new protection checker
    pub fn new(client: Client) -> Self {
        Self { client }
    }

    /// Analyze the impact of deleting a CRD
    pub async fn analyze_deletion_impact(
        &self,
        crd_name: &str,
        policy: CrdPolicy,
    ) -> Result<CrdDeletionImpact> {
        // Policy check first
        if !policy.allows_delete() {
            return Ok(CrdDeletionImpact::empty(crd_name, policy));
        }

        // Parse CRD name to get group and resource
        let Some((plural, group)) = parse_crd_name(crd_name) else {
            return Ok(CrdDeletionImpact::empty(crd_name, policy));
        };

        // Run discovery to find the API resource
        let discovery = Discovery::new(self.client.clone())
            .run()
            .await
            .map_err(KubeError::Api)?;

        // Try to find the resource in discovery
        let by_namespace = self
            .count_resources_by_namespace(&discovery, &group, &plural)
            .await?;

        Ok(CrdDeletionImpact::with_resources(
            crd_name,
            policy,
            by_namespace,
        ))
    }

    /// Analyze deletion impact for multiple CRDs
    pub async fn analyze_multi_deletion_impact(
        &self,
        crds: &[DetectedCrd],
    ) -> Result<DeletionImpactSummary> {
        let mut summary = DeletionImpactSummary::new();

        for crd in crds {
            let impact = self.analyze_deletion_impact(&crd.name, crd.policy).await?;
            summary.add(impact);
        }

        Ok(summary)
    }

    /// Count resources by namespace for a given API group/resource
    async fn count_resources_by_namespace(
        &self,
        discovery: &Discovery,
        group: &str,
        plural: &str,
    ) -> Result<HashMap<String, usize>> {
        // Try to find API versions (v1, v1beta1, v1alpha1)
        for version in &["v1", "v1beta1", "v1alpha1", "v2", "v2beta1"] {
            let gvk = GroupVersionKind {
                group: group.to_string(),
                version: version.to_string(),
                kind: plural.to_string(), // Note: this should be kind, not plural
            };

            if let Some((ar, caps)) = discovery.resolve_gvk(&gvk) {
                return self.count_with_api_resource(&ar, &caps).await;
            }
        }

        // CRD might not have any resources yet
        Ok(HashMap::new())
    }

    /// Count resources using a discovered API resource
    async fn count_with_api_resource(
        &self,
        ar: &ApiResource,
        caps: &ApiCapabilities,
    ) -> Result<HashMap<String, usize>> {
        let mut by_namespace = HashMap::new();

        if caps.scope == Scope::Namespaced {
            // List across all namespaces
            let api: Api<DynamicObject> = Api::all_with(self.client.clone(), ar);
            match api.list(&ListParams::default()).await {
                Ok(list) => {
                    for item in list.items {
                        let ns = item
                            .metadata
                            .namespace
                            .unwrap_or_else(|| "default".to_string());
                        *by_namespace.entry(ns).or_insert(0) += 1;
                    }
                }
                Err(kube::Error::Api(resp)) if resp.code == 404 => {
                    // Resource type doesn't exist, return empty
                }
                Err(e) => return Err(KubeError::Api(e)),
            }
        } else {
            // Cluster-scoped
            let api: Api<DynamicObject> = Api::all_with(self.client.clone(), ar);
            match api.list(&ListParams::default()).await {
                Ok(list) => {
                    if !list.items.is_empty() {
                        by_namespace.insert("".to_string(), list.items.len());
                    }
                }
                Err(kube::Error::Api(resp)) if resp.code == 404 => {}
                Err(e) => return Err(KubeError::Api(e)),
            }
        }

        Ok(by_namespace)
    }
}

/// Parse CRD name into (plural, group)
///
/// Example: "certificates.cert-manager.io" -> ("certificates", "cert-manager.io")
fn parse_crd_name(name: &str) -> Option<(String, String)> {
    let parts: Vec<&str> = name.splitn(2, '.').collect();
    if parts.len() == 2 {
        Some((parts[0].to_string(), parts[1].to_string()))
    } else {
        None
    }
}

/// Confirmation requirements for CRD deletion
#[derive(Debug, Clone)]
pub struct DeletionConfirmation {
    /// Whether confirmation is required
    pub required: bool,
    /// Confirmation flags needed
    pub required_flags: Vec<String>,
    /// Human-readable explanation
    pub explanation: String,
}

impl DeletionConfirmation {
    /// No confirmation needed
    pub fn not_required() -> Self {
        Self {
            required: false,
            required_flags: vec![],
            explanation: "No confirmation required".to_string(),
        }
    }

    /// Confirmation required with explanation
    pub fn required(explanation: impl Into<String>, flags: Vec<String>) -> Self {
        Self {
            required: true,
            required_flags: flags,
            explanation: explanation.into(),
        }
    }

    /// Generate confirmation requirements from impact summary
    pub fn from_impact(summary: &DeletionImpactSummary) -> Self {
        if summary.has_blocked() {
            return Self::required(
                format!(
                    "{} CRD(s) blocked by policy: {}",
                    summary.blocked_crds.len(),
                    summary.blocked_crds.join(", ")
                ),
                vec![], // No flags can override policy
            );
        }

        if summary.has_data_loss() {
            let mut flags = vec!["--delete-crds".to_string()];
            flags.push("--confirm-crd-deletion".to_string());

            return Self::required(
                format!(
                    "Deleting {} CRD(s) will permanently delete {} CustomResource(s)",
                    summary.total_crds, summary.total_resources
                ),
                flags,
            );
        }

        // No data loss, but still need --delete-crds
        if summary.total_crds > 0 {
            return Self::required(
                format!(
                    "Deleting {} CRD(s) (no existing CustomResources)",
                    summary.total_crds
                ),
                vec!["--delete-crds".to_string()],
            );
        }

        Self::not_required()
    }
}

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

    #[test]
    fn test_parse_crd_name() {
        let (plural, group) = parse_crd_name("certificates.cert-manager.io").unwrap();
        assert_eq!(plural, "certificates");
        assert_eq!(group, "cert-manager.io");

        let (plural, group) = parse_crd_name("tests.example.com").unwrap();
        assert_eq!(plural, "tests");
        assert_eq!(group, "example.com");

        assert!(parse_crd_name("invalid").is_none());
    }

    #[test]
    fn test_crd_deletion_impact_empty() {
        let impact = CrdDeletionImpact::empty("tests.example.com", CrdPolicy::Managed);

        assert_eq!(impact.total_resources, 0);
        assert!(impact.deletion_allowed);
        assert!(!impact.has_data_loss());
        assert!(impact.is_safe());
    }

    #[test]
    fn test_crd_deletion_impact_with_resources() {
        let mut by_ns = HashMap::new();
        by_ns.insert("production".to_string(), 10);
        by_ns.insert("staging".to_string(), 5);

        let impact =
            CrdDeletionImpact::with_resources("tests.example.com", CrdPolicy::Managed, by_ns);

        assert_eq!(impact.total_resources, 15);
        assert!(impact.deletion_allowed);
        assert!(impact.has_data_loss());
        assert!(!impact.is_safe());

        let sorted = impact.sorted_namespaces();
        assert_eq!(*sorted[0].0, "production");
        assert_eq!(*sorted[0].1, 10);
    }

    #[test]
    fn test_crd_deletion_impact_shared_policy() {
        let impact = CrdDeletionImpact::empty("tests.example.com", CrdPolicy::Shared);

        assert!(!impact.deletion_allowed);
        assert!(impact.blocked_reason.is_some());
    }

    #[test]
    fn test_deletion_impact_summary() {
        let mut summary = DeletionImpactSummary::new();

        // Add a deletable CRD with resources
        let mut by_ns = HashMap::new();
        by_ns.insert("default".to_string(), 5);
        summary.add(CrdDeletionImpact::with_resources(
            "first.example.com",
            CrdPolicy::Managed,
            by_ns,
        ));

        // Add a blocked CRD
        summary.add(CrdDeletionImpact::empty(
            "second.example.com",
            CrdPolicy::Shared,
        ));

        assert_eq!(summary.total_crds, 1);
        assert_eq!(summary.total_resources, 5);
        assert!(summary.has_blocked());
        assert!(summary.has_data_loss());
        assert_eq!(summary.blocked_crds, vec!["second.example.com"]);
    }

    #[test]
    fn test_deletion_confirmation_not_required() {
        let confirmation = DeletionConfirmation::not_required();
        assert!(!confirmation.required);
        assert!(confirmation.required_flags.is_empty());
    }

    #[test]
    fn test_deletion_confirmation_from_impact_with_data_loss() {
        let mut summary = DeletionImpactSummary::new();
        let mut by_ns = HashMap::new();
        by_ns.insert("production".to_string(), 10);
        summary.add(CrdDeletionImpact::with_resources(
            "tests.example.com",
            CrdPolicy::Managed,
            by_ns,
        ));

        let confirmation = DeletionConfirmation::from_impact(&summary);

        assert!(confirmation.required);
        assert!(
            confirmation
                .required_flags
                .contains(&"--delete-crds".to_string())
        );
        assert!(
            confirmation
                .required_flags
                .contains(&"--confirm-crd-deletion".to_string())
        );
    }

    #[test]
    fn test_deletion_confirmation_from_impact_no_data_loss() {
        let mut summary = DeletionImpactSummary::new();
        summary.add(CrdDeletionImpact::empty(
            "tests.example.com",
            CrdPolicy::Managed,
        ));

        let confirmation = DeletionConfirmation::from_impact(&summary);

        assert!(confirmation.required);
        assert!(
            confirmation
                .required_flags
                .contains(&"--delete-crds".to_string())
        );
        assert!(
            !confirmation
                .required_flags
                .contains(&"--confirm-crd-deletion".to_string())
        );
    }

    #[test]
    fn test_deletion_confirmation_from_blocked() {
        let mut summary = DeletionImpactSummary::new();
        summary.add(CrdDeletionImpact::empty(
            "tests.example.com",
            CrdPolicy::Shared,
        ));

        let confirmation = DeletionConfirmation::from_impact(&summary);

        assert!(confirmation.required);
        assert!(confirmation.required_flags.is_empty()); // No flags can override
        assert!(confirmation.explanation.contains("blocked"));
    }

    #[test]
    fn test_affected_namespaces() {
        let mut summary = DeletionImpactSummary::new();

        let mut by_ns1 = HashMap::new();
        by_ns1.insert("production".to_string(), 5);
        by_ns1.insert("staging".to_string(), 3);
        summary.add(CrdDeletionImpact::with_resources(
            "first.example.com",
            CrdPolicy::Managed,
            by_ns1,
        ));

        let mut by_ns2 = HashMap::new();
        by_ns2.insert("staging".to_string(), 2); // Duplicate
        by_ns2.insert("development".to_string(), 1);
        summary.add(CrdDeletionImpact::with_resources(
            "second.example.com",
            CrdPolicy::Managed,
            by_ns2,
        ));

        let namespaces = summary.affected_namespaces();

        assert_eq!(namespaces.len(), 3);
        assert!(namespaces.contains(&"production".to_string()));
        assert!(namespaces.contains(&"staging".to_string()));
        assert!(namespaces.contains(&"development".to_string()));
    }
}