-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.rs
More file actions
111 lines (96 loc) · 3.37 KB
/
utils.rs
File metadata and controls
111 lines (96 loc) · 3.37 KB
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
use std::env;
use serde_yaml::{Mapping, Value};
use snafu::{ResultExt, Snafu};
use stackable_cockpit::{
constants::{HELM_REPO_NAME_DEV, HELM_REPO_NAME_STABLE, HELM_REPO_NAME_TEST},
utils::path::PathOrUrl,
xfer::{self, processor::Yaml},
};
use crate::constants::{HELM_REPO_URL_DEV, HELM_REPO_URL_STABLE, HELM_REPO_URL_TEST};
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("failed to open or transfer values file '{path}'"))]
FileTransfer { source: xfer::Error, path: String },
#[snafu(display("operator values file '{path}' must be a YAML mapping at the top level"))]
InvalidValueType { path: String },
#[snafu(display(
"value for key '{key}' in operator values file '{path}' must be a YAML mapping"
))]
InvalidEntryType { key: String, path: String },
}
#[derive(Debug, Snafu)]
#[snafu(display("Invalid Helm repo name ({name}), cannot resolve to repo URL"))]
pub struct InvalidRepoNameError {
name: String,
}
/// This returns the Helm repository URL based on the repo name. If the provided
/// repo name is not recognized (invalid), an [`InvalidRepoNameError`] is
/// returned.
pub fn helm_repo_name_to_repo_url<'a, T>(repo_name: T) -> Result<&'a str, InvalidRepoNameError>
where
T: AsRef<str>,
{
let repo_name = repo_name.as_ref();
match repo_name {
HELM_REPO_NAME_STABLE => Ok(HELM_REPO_URL_STABLE),
HELM_REPO_NAME_TEST => Ok(HELM_REPO_URL_TEST),
HELM_REPO_NAME_DEV => Ok(HELM_REPO_URL_DEV),
_ => Err(InvalidRepoNameError {
name: repo_name.to_string(),
}),
}
}
/// Returns wether the application should use colored output based on the user
/// requested output and the `NO_COLOR` env variable. It currently does not
/// factor in terminal support.
pub fn use_colored_output(use_color: bool) -> bool {
use_color && env::var_os("NO_COLOR").is_none()
}
/// Loads operator helm values from a YAML file.
///
/// The file should contain a YAML mapping of operator names to their helm values.
/// Use YAML anchors and aliases to share values across operators:
/// ```yaml
/// airflow-operator:
/// tolerations: &default-tolerations
/// - key: "example"
/// operator: "Exists"
/// effect: "NoSchedule"
/// podAnnotations:
/// example.com/team: "data-engineering"
/// zookeeper-operator:
/// tolerations: *default-tolerations
/// podAnnotations:
/// example.com/team: "platform"
/// ```
pub async fn load_operator_values(
values_file: Option<&PathOrUrl>,
transfer_client: &xfer::Client,
) -> Result<Mapping, Error> {
let file = match values_file {
Some(file) => file,
None => return Ok(Mapping::new()),
};
let path = match file {
PathOrUrl::Path(p) => p.display().to_string(),
PathOrUrl::Url(u) => u.to_string(),
};
let value = transfer_client
.get(file, &Yaml::<Value>::default())
.await
.context(FileTransferSnafu { path: path.clone() })?;
let mapping = match value {
Value::Mapping(mapping) => mapping,
_ => return InvalidValueTypeSnafu { path: path.clone() }.fail(),
};
for (key, value) in &mapping {
if !value.is_mapping() {
return InvalidEntryTypeSnafu {
key: key.as_str().unwrap_or("<non-string key>").to_string(),
path: path.clone(),
}
.fail();
}
}
Ok(mapping)
}