sherpack-convert 0.4.0

Helm chart to Sherpack pack converter
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
//! Chart.yaml to Pack.yaml converter
//!
//! Converts Helm Chart.yaml metadata to Sherpack Pack.yaml format.

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum ChartError {
    #[error("Failed to parse Chart.yaml: {0}")]
    Parse(#[from] serde_yaml::Error),

    #[error("Missing required field: {0}")]
    MissingField(String),

    #[error("Invalid version: {0}")]
    InvalidVersion(String),
}

/// Helm Chart.yaml structure
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HelmChart {
    /// API version (v1 or v2)
    pub api_version: String,

    /// Chart name
    pub name: String,

    /// Chart version (SemVer)
    pub version: String,

    /// Kubernetes version constraint
    #[serde(default)]
    pub kube_version: Option<String>,

    /// Chart description
    #[serde(default)]
    pub description: Option<String>,

    /// Chart type (application or library)
    #[serde(default, rename = "type")]
    pub chart_type: Option<String>,

    /// Keywords for searching
    #[serde(default)]
    pub keywords: Vec<String>,

    /// Project home page
    #[serde(default)]
    pub home: Option<String>,

    /// Source code URLs
    #[serde(default)]
    pub sources: Vec<String>,

    /// Chart dependencies
    #[serde(default)]
    pub dependencies: Vec<HelmDependency>,

    /// Maintainers
    #[serde(default)]
    pub maintainers: Vec<HelmMaintainer>,

    /// Icon URL
    #[serde(default)]
    pub icon: Option<String>,

    /// App version
    #[serde(default)]
    pub app_version: Option<String>,

    /// Whether chart is deprecated
    #[serde(default)]
    pub deprecated: bool,

    /// Annotations
    #[serde(default)]
    pub annotations: BTreeMap<String, String>,
}

/// Helm dependency
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct HelmDependency {
    /// Dependency name
    pub name: String,

    /// Version constraint
    pub version: String,

    /// Repository URL
    #[serde(default)]
    pub repository: Option<String>,

    /// Condition to enable
    #[serde(default)]
    pub condition: Option<String>,

    /// Tags for grouping
    #[serde(default)]
    pub tags: Vec<String>,

    /// Import values
    #[serde(default)]
    pub import_values: Vec<serde_yaml::Value>,

    /// Alias name
    #[serde(default)]
    pub alias: Option<String>,
}

/// Helm maintainer
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct HelmMaintainer {
    /// Maintainer name
    pub name: String,

    /// Email address
    #[serde(default)]
    pub email: Option<String>,

    /// URL
    #[serde(default)]
    pub url: Option<String>,
}

/// Sherpack Pack.yaml structure
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SherpackPack {
    /// API version
    pub api_version: String,

    /// Pack kind
    pub kind: String,

    /// Pack name
    pub name: String,

    /// Pack version
    pub version: String,

    /// Kubernetes version constraint
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kube_version: Option<String>,

    /// Description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Keywords
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub keywords: Vec<String>,

    /// Home page
    #[serde(skip_serializing_if = "Option::is_none")]
    pub home: Option<String>,

    /// Source URLs
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub sources: Vec<String>,

    /// Dependencies
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub dependencies: Vec<SherpackDependency>,

    /// Maintainers
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub maintainers: Vec<HelmMaintainer>,

    /// Icon URL
    #[serde(skip_serializing_if = "Option::is_none")]
    pub icon: Option<String>,

    /// App version
    #[serde(skip_serializing_if = "Option::is_none")]
    pub app_version: Option<String>,

    /// Annotations
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub annotations: BTreeMap<String, String>,
}

/// Sherpack dependency
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SherpackDependency {
    /// Dependency name
    pub name: String,

    /// Version constraint
    pub version: String,

    /// Repository URL
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repository: Option<String>,

    /// Condition to enable
    #[serde(skip_serializing_if = "Option::is_none")]
    pub condition: Option<String>,

    /// Tags for grouping
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,

    /// Alias name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alias: Option<String>,
}

impl HelmChart {
    /// Parse a Chart.yaml string
    pub fn parse(content: &str) -> Result<Self, ChartError> {
        let chart: HelmChart = serde_yaml::from_str(content)?;

        // Validate required fields
        if chart.name.is_empty() {
            return Err(ChartError::MissingField("name".to_string()));
        }
        if chart.version.is_empty() {
            return Err(ChartError::MissingField("version".to_string()));
        }

        Ok(chart)
    }

    /// Convert to Sherpack Pack
    pub fn to_sherpack(&self) -> SherpackPack {
        let kind = match self.chart_type.as_deref() {
            Some("library") => "library".to_string(),
            _ => "application".to_string(),
        };

        let dependencies: Vec<SherpackDependency> = self
            .dependencies
            .iter()
            .map(|d| SherpackDependency {
                name: d.name.clone(),
                version: d.version.clone(),
                repository: d.repository.clone(),
                condition: d.condition.clone(),
                tags: d.tags.clone(),
                alias: d.alias.clone(),
            })
            .collect();

        // Handle deprecated annotation
        let mut annotations = self.annotations.clone();
        if self.deprecated {
            annotations.insert("sherpack.io/deprecated".to_string(), "true".to_string());
        }

        SherpackPack {
            api_version: "sherpack/v1".to_string(),
            kind,
            name: self.name.clone(),
            version: self.version.clone(),
            kube_version: self.kube_version.clone(),
            description: self.description.clone(),
            keywords: self.keywords.clone(),
            home: self.home.clone(),
            sources: self.sources.clone(),
            dependencies,
            maintainers: self.maintainers.clone(),
            icon: self.icon.clone(),
            app_version: self.app_version.clone(),
            annotations,
        }
    }
}

impl SherpackPack {
    /// Serialize to YAML string
    pub fn to_yaml(&self) -> Result<String, serde_yaml::Error> {
        // Create a custom serialization with proper ordering
        // Sherpack expects: apiVersion, kind, metadata: { name, version, ... }, dependencies, engine
        let mut yaml = String::new();

        yaml.push_str(&format!("apiVersion: {}\n", self.api_version));
        yaml.push_str(&format!("kind: {}\n", self.kind));
        yaml.push_str("\nmetadata:\n");
        yaml.push_str(&format!("  name: {}\n", self.name));
        yaml.push_str(&format!("  version: {}\n", self.version));

        if let Some(ref app_version) = self.app_version {
            yaml.push_str(&format!("  appVersion: \"{}\"\n", app_version));
        }

        if let Some(ref description) = self.description {
            yaml.push_str(&format!("  description: {}\n", description));
        }

        if let Some(ref kube_version) = self.kube_version {
            // Quote kubeVersion if it contains special characters
            if kube_version.starts_with('>')
                || kube_version.starts_with('<')
                || kube_version.starts_with('=')
            {
                yaml.push_str(&format!("  kubeVersion: \"{}\"\n", kube_version));
            } else {
                yaml.push_str(&format!("  kubeVersion: {}\n", kube_version));
            }
        }

        if let Some(ref home) = self.home {
            yaml.push_str(&format!("  home: {}\n", home));
        }

        if let Some(ref icon) = self.icon {
            yaml.push_str(&format!("  icon: {}\n", icon));
        }

        if !self.sources.is_empty() {
            yaml.push_str("  sources:\n");
            for source in &self.sources {
                yaml.push_str(&format!("    - {}\n", source));
            }
        }

        if !self.keywords.is_empty() {
            yaml.push_str("  keywords:\n");
            for keyword in &self.keywords {
                yaml.push_str(&format!("    - {}\n", keyword));
            }
        }

        if !self.maintainers.is_empty() {
            yaml.push_str("  maintainers:\n");
            for maintainer in &self.maintainers {
                yaml.push_str(&format!("    - name: {}\n", maintainer.name));
                if let Some(ref email) = maintainer.email {
                    yaml.push_str(&format!("      email: {}\n", email));
                }
                if let Some(ref url) = maintainer.url {
                    yaml.push_str(&format!("      url: {}\n", url));
                }
            }
        }

        if !self.annotations.is_empty() {
            yaml.push_str("  annotations:\n");
            for (key, value) in &self.annotations {
                // Handle multiline values with YAML block scalar
                if value.contains('\n') {
                    yaml.push_str(&format!("    {}: |\n", key));
                    for line in value.lines() {
                        yaml.push_str(&format!("      {}\n", line));
                    }
                } else {
                    // Escape the value properly
                    let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
                    yaml.push_str(&format!("    {}: \"{}\"\n", key, escaped));
                }
            }
        }

        if !self.dependencies.is_empty() {
            yaml.push('\n');
            yaml.push_str("dependencies:\n");
            for dep in &self.dependencies {
                yaml.push_str(&format!("  - name: {}\n", dep.name));
                yaml.push_str(&format!("    version: \"{}\"\n", dep.version));
                if let Some(ref repo) = dep.repository {
                    yaml.push_str(&format!("    repository: {}\n", repo));
                }
                if let Some(ref condition) = dep.condition {
                    yaml.push_str(&format!("    condition: {}\n", condition));
                }
                if let Some(ref alias) = dep.alias {
                    yaml.push_str(&format!("    alias: {}\n", alias));
                }
                if !dep.tags.is_empty() {
                    yaml.push_str("    tags:\n");
                    for tag in &dep.tags {
                        yaml.push_str(&format!("      - {}\n", tag));
                    }
                }
            }
        }

        Ok(yaml)
    }
}

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

    #[test]
    fn test_parse_simple_chart() {
        let content = r#"
apiVersion: v2
name: my-app
version: 1.0.0
description: My application
"#;
        let chart = HelmChart::parse(content).unwrap();
        assert_eq!(chart.name, "my-app");
        assert_eq!(chart.version, "1.0.0");
        assert_eq!(chart.description, Some("My application".to_string()));
    }

    #[test]
    fn test_convert_to_sherpack() {
        let content = r#"
apiVersion: v2
name: my-app
version: 1.0.0
type: application
"#;
        let chart = HelmChart::parse(content).unwrap();
        let pack = chart.to_sherpack();

        assert_eq!(pack.api_version, "sherpack/v1");
        assert_eq!(pack.kind, "application");
        assert_eq!(pack.name, "my-app");
    }

    #[test]
    fn test_convert_library() {
        let content = r#"
apiVersion: v2
name: my-lib
version: 1.0.0
type: library
"#;
        let chart = HelmChart::parse(content).unwrap();
        let pack = chart.to_sherpack();

        assert_eq!(pack.kind, "library");
    }

    #[test]
    fn test_convert_with_dependencies() {
        let content = r#"
apiVersion: v2
name: my-app
version: 1.0.0
dependencies:
  - name: postgresql
    version: "12.x"
    repository: https://charts.bitnami.com/bitnami
    condition: postgresql.enabled
"#;
        let chart = HelmChart::parse(content).unwrap();
        let pack = chart.to_sherpack();

        assert_eq!(pack.dependencies.len(), 1);
        assert_eq!(pack.dependencies[0].name, "postgresql");
        assert_eq!(pack.dependencies[0].version, "12.x");
    }

    #[test]
    fn test_yaml_output() {
        let content = r#"
apiVersion: v2
name: my-app
version: 1.0.0
appVersion: "2.0.0"
description: My application
"#;
        let chart = HelmChart::parse(content).unwrap();
        let pack = chart.to_sherpack();
        let yaml = pack.to_yaml().unwrap();

        assert!(yaml.contains("apiVersion: sherpack/v1"));
        assert!(yaml.contains("kind: application"));
        assert!(yaml.contains("metadata:"));
        assert!(yaml.contains("  name: my-app"));
        assert!(yaml.contains("appVersion: \"2.0.0\""));
    }

    #[test]
    fn test_deprecated_annotation() {
        let content = r#"
apiVersion: v2
name: old-app
version: 1.0.0
deprecated: true
"#;
        let chart = HelmChart::parse(content).unwrap();
        let pack = chart.to_sherpack();

        assert!(pack.annotations.contains_key("sherpack.io/deprecated"));
    }
}