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
//! Action options for install, upgrade, uninstall, and rollback operations

use chrono::Duration;
use serde::{Deserialize, Serialize};

use crate::health::HealthCheckConfig;
use crate::storage::LargeReleaseStrategy;

/// Options for install operation
#[derive(Debug, Clone, Default)]
pub struct InstallOptions {
    /// Release name
    pub name: String,

    /// Target namespace
    pub namespace: String,

    /// Wait for resources to be ready
    pub wait: bool,

    /// Timeout for wait
    pub timeout: Option<Duration>,

    /// Run health checks after install
    pub health_check: Option<HealthCheckConfig>,

    /// Automatically rollback on failure (only with wait=true)
    pub atomic: bool,

    /// Create namespace if it doesn't exist
    pub create_namespace: bool,

    /// Strategy for large releases
    pub large_release_strategy: LargeReleaseStrategy,

    /// Skip schema validation
    pub skip_schema_validation: bool,

    /// Dry run mode (don't actually apply)
    pub dry_run: bool,

    /// Show diff before applying
    pub show_diff: bool,

    /// Custom labels to add to the release
    pub labels: std::collections::HashMap<String, String>,

    /// Description for this release
    pub description: Option<String>,
}

impl InstallOptions {
    /// Create default install options with name and namespace
    pub fn new(name: impl Into<String>, namespace: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            namespace: namespace.into(),
            ..Default::default()
        }
    }

    /// Enable waiting for resources
    pub fn with_wait(mut self, timeout: Duration) -> Self {
        self.wait = true;
        self.timeout = Some(timeout);
        self
    }

    /// Enable atomic mode (auto-rollback on failure)
    pub fn with_atomic(mut self, timeout: Duration) -> Self {
        self.wait = true;
        self.atomic = true;
        self.timeout = Some(timeout);
        self
    }

    /// Enable health checks
    pub fn with_health_check(mut self, config: HealthCheckConfig) -> Self {
        self.health_check = Some(config);
        self
    }

    /// Enable dry-run mode
    pub fn dry_run(mut self) -> Self {
        self.dry_run = true;
        self
    }

    /// Show diff before applying
    pub fn with_diff(mut self) -> Self {
        self.show_diff = true;
        self
    }
}

/// Options for upgrade operation
#[derive(Debug, Clone, Default)]
pub struct UpgradeOptions {
    /// Release name
    pub name: String,

    /// Target namespace
    pub namespace: String,

    /// Wait for resources to be ready
    pub wait: bool,

    /// Timeout for wait
    pub timeout: Option<Duration>,

    /// Run health checks after upgrade
    pub health_check: Option<HealthCheckConfig>,

    /// Automatically rollback on failure
    pub atomic: bool,

    /// Install if release doesn't exist
    pub install: bool,

    /// Force resource updates through delete/recreate
    pub force: bool,

    /// Strategy for immutable field conflicts
    pub immutable_strategy: ImmutableStrategy,

    /// Skip schema validation
    pub skip_schema_validation: bool,

    /// Reset values to defaults (don't merge with previous)
    pub reset_values: bool,

    /// Reuse values from previous release
    pub reuse_values: bool,

    /// Dry run mode
    pub dry_run: bool,

    /// Show diff before applying
    pub show_diff: bool,

    /// Skip hooks
    pub no_hooks: bool,

    /// Maximum history to keep
    pub max_history: Option<u32>,

    /// Custom labels to add
    pub labels: std::collections::HashMap<String, String>,

    /// Description for this revision
    pub description: Option<String>,
}

impl UpgradeOptions {
    /// Create default upgrade options
    pub fn new(name: impl Into<String>, namespace: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            namespace: namespace.into(),
            ..Default::default()
        }
    }

    /// Enable install-if-not-exists
    pub fn with_install(mut self) -> Self {
        self.install = true;
        self
    }

    /// Enable atomic mode
    pub fn with_atomic(mut self, timeout: Duration) -> Self {
        self.wait = true;
        self.atomic = true;
        self.timeout = Some(timeout);
        self
    }

    /// Enable force mode
    pub fn with_force(mut self) -> Self {
        self.force = true;
        self
    }

    /// Set immutable strategy
    pub fn with_immutable_strategy(mut self, strategy: ImmutableStrategy) -> Self {
        self.immutable_strategy = strategy;
        self
    }
}

/// Options for uninstall operation
#[derive(Debug, Clone, Default)]
pub struct UninstallOptions {
    /// Release name
    pub name: String,

    /// Target namespace
    pub namespace: String,

    /// Wait for resources to be deleted
    pub wait: bool,

    /// Timeout for wait
    pub timeout: Option<Duration>,

    /// Keep release history (don't delete storage)
    pub keep_history: bool,

    /// Skip pre/post-delete hooks
    pub no_hooks: bool,

    /// Dry run mode
    pub dry_run: bool,

    /// Cascade deletion (delete dependents)
    pub cascade: DeletionCascade,

    /// Description for the uninstall
    pub description: Option<String>,
}

impl UninstallOptions {
    /// Create default uninstall options
    pub fn new(name: impl Into<String>, namespace: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            namespace: namespace.into(),
            cascade: DeletionCascade::Background,
            ..Default::default()
        }
    }

    /// Keep history after uninstall
    pub fn keep_history(mut self) -> Self {
        self.keep_history = true;
        self
    }

    /// Wait for deletion
    pub fn with_wait(mut self, timeout: Duration) -> Self {
        self.wait = true;
        self.timeout = Some(timeout);
        self
    }
}

/// Options for rollback operation
#[derive(Debug, Clone, Default)]
pub struct RollbackOptions {
    /// Release name
    pub name: String,

    /// Target namespace
    pub namespace: String,

    /// Target revision (0 = previous)
    pub revision: u32,

    /// Wait for resources to be ready
    pub wait: bool,

    /// Timeout for wait
    pub timeout: Option<Duration>,

    /// Run health checks after rollback
    pub health_check: Option<HealthCheckConfig>,

    /// Force rollback through delete/recreate
    pub force: bool,

    /// Strategy for immutable field conflicts
    pub immutable_strategy: ImmutableStrategy,

    /// Strategy for PVCs
    pub pvc_strategy: PvcStrategy,

    /// Skip hooks
    pub no_hooks: bool,

    /// Dry run mode
    pub dry_run: bool,

    /// Show diff before applying
    pub show_diff: bool,

    /// Recreate pods (delete existing pods)
    pub recreate_pods: bool,

    /// Maximum history to keep
    pub max_history: Option<u32>,

    /// Description for this rollback
    pub description: Option<String>,
}

impl RollbackOptions {
    /// Create default rollback options
    pub fn new(name: impl Into<String>, namespace: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            namespace: namespace.into(),
            ..Default::default()
        }
    }

    /// Set target revision
    pub fn to_revision(mut self, revision: u32) -> Self {
        self.revision = revision;
        self
    }

    /// Enable force mode
    pub fn with_force(mut self) -> Self {
        self.force = true;
        self
    }

    /// Wait for rollback
    pub fn with_wait(mut self, timeout: Duration) -> Self {
        self.wait = true;
        self.timeout = Some(timeout);
        self
    }
}

/// Strategy for handling immutable field conflicts
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ImmutableStrategy {
    /// Fail on immutable field conflict (default)
    #[default]
    Fail,

    /// Delete and recreate the resource
    Recreate,

    /// Skip resources with immutable conflicts
    Skip,
}

impl std::fmt::Display for ImmutableStrategy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Fail => write!(f, "fail"),
            Self::Recreate => write!(f, "recreate"),
            Self::Skip => write!(f, "skip"),
        }
    }
}

impl std::str::FromStr for ImmutableStrategy {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "fail" => Ok(Self::Fail),
            "recreate" => Ok(Self::Recreate),
            "skip" => Ok(Self::Skip),
            _ => Err(format!("unknown immutable strategy: {}", s)),
        }
    }
}

/// Strategy for handling PVCs during rollback
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PvcStrategy {
    /// Don't touch PVCs (default)
    #[default]
    Preserve,

    /// Warn that PVC data won't be rolled back
    WarnAndPreserve,

    /// Try to restore from snapshot
    RestoreSnapshot {
        /// Volume snapshot class to use
        snapshot_class: String,
    },
}

impl std::fmt::Display for PvcStrategy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Preserve => write!(f, "preserve"),
            Self::WarnAndPreserve => write!(f, "warn"),
            Self::RestoreSnapshot { .. } => write!(f, "restore-snapshot"),
        }
    }
}

/// Cascade deletion strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum DeletionCascade {
    /// Delete in background (default)
    #[default]
    Background,

    /// Delete in foreground (wait for dependents)
    Foreground,

    /// Orphan dependents (don't delete them)
    Orphan,
}

impl std::fmt::Display for DeletionCascade {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Background => write!(f, "background"),
            Self::Foreground => write!(f, "foreground"),
            Self::Orphan => write!(f, "orphan"),
        }
    }
}

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

    #[test]
    fn test_install_options_builder() {
        let opts = InstallOptions::new("myapp", "default")
            .with_wait(Duration::minutes(5))
            .with_diff();

        assert_eq!(opts.name, "myapp");
        assert_eq!(opts.namespace, "default");
        assert!(opts.wait);
        assert!(opts.show_diff);
    }

    #[test]
    fn test_upgrade_options_atomic() {
        let opts = UpgradeOptions::new("myapp", "default")
            .with_atomic(Duration::minutes(10))
            .with_install();

        assert!(opts.wait);
        assert!(opts.atomic);
        assert!(opts.install);
    }

    #[test]
    fn test_rollback_options() {
        let opts = RollbackOptions::new("myapp", "default")
            .to_revision(3)
            .with_force();

        assert_eq!(opts.revision, 3);
        assert!(opts.force);
    }

    #[test]
    fn test_immutable_strategy_parse() {
        assert_eq!(
            "recreate".parse::<ImmutableStrategy>().unwrap(),
            ImmutableStrategy::Recreate
        );
        assert_eq!(
            "fail".parse::<ImmutableStrategy>().unwrap(),
            ImmutableStrategy::Fail
        );
    }
}