-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmod.rs
More file actions
346 lines (298 loc) · 9.96 KB
/
mod.rs
File metadata and controls
346 lines (298 loc) · 9.96 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
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
use std::{fmt::Display, str::FromStr};
use listener_operator::LISTENER_CLASS_PRESET;
use semver::Version;
use serde::Serialize;
use serde_yaml::{Mapping, Value};
use snafu::{ResultExt, Snafu, ensure};
use tracing::{Span, info, instrument};
use tracing_indicatif::{indicatif_println, span_ext::IndicatifSpanExt};
use crate::{
constants::{
HELM_OCI_REGISTRY, HELM_REPO_NAME_DEV, HELM_REPO_NAME_STABLE, HELM_REPO_NAME_TEST,
},
helm,
utils::operator_chart_name,
};
pub mod listener_operator;
pub const VALID_OPERATORS: &[&str] = &[
"airflow",
"commons",
"druid",
"hbase",
"hdfs",
"hello-world",
"hive",
"kafka",
"listener",
"nifi",
"opa",
"opensearch",
"secret",
"spark-k8s",
"superset",
"trino",
"zookeeper",
];
#[derive(Debug, Snafu)]
pub enum SpecParseError {
#[snafu(display("invalid equal sign count in operator spec, expected one"))]
InvalidEqualSignCount,
#[snafu(display("failed to parse SemVer version"))]
ParseVersion { source: semver::Error },
#[snafu(display("the operator spec includes '=' but no version was specified"))]
MissingVersion,
#[snafu(display("empty operator spec input"))]
EmptyInput,
#[snafu(display(
"invalid operator name {name:?}. \
It could be the case that this version of stackablectl is too old to know about this particular operator, \
in which case you should update it."
))]
InvalidName { name: String },
}
/// OperatorSpec describes the format of an operator name with optional version
/// number. The string format is `<OPERATOR_NAME>(=<VERSION>)`. Valid values
/// are: `operator`, `operator=1.2.3` or `operator=1.2.3-rc1`.
#[derive(Clone, Debug)]
pub struct OperatorSpec {
pub version: Option<Version>,
pub name: String,
}
impl Display for OperatorSpec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{name}{version_selector}",
name = self.name,
version_selector = match &self.version {
Some(v) => format!("={v}"),
None => "".into(),
}
)
}
}
impl FromStr for OperatorSpec {
type Err = SpecParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let input = s.trim();
// Empty input is not allowed
ensure!(!input.is_empty(), EmptyInputSnafu);
// Split at each equal sign
let parts: Vec<&str> = input.split('=').collect();
let len = parts.len();
// If there are more than 2 equal signs, return error
// because of invalid spec format
ensure!(len <= 2, InvalidEqualSignCountSnafu);
// Check if the provided operator name is in the list of valid operators
ensure!(
VALID_OPERATORS.contains(&parts[0]),
InvalidNameSnafu { name: parts[0] }
);
// If there is only one part, the input didn't include
// the optional version identifier
if len == 1 {
return Ok(Self {
name: input.into(),
version: None,
});
}
// If there is an equal sign, but no version after
ensure!(!parts[1].is_empty(), MissingVersionSnafu);
// There are two parts, so an operator name and version
let version: Version = parts[1].parse().context(ParseVersionSnafu)?;
Ok(Self {
name: parts[0].into(),
version: Some(version),
})
}
}
impl TryFrom<String> for OperatorSpec {
type Error = SpecParseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::from_str(&value)
}
}
impl TryFrom<&str> for OperatorSpec {
type Error = SpecParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::try_from(value.to_string())
}
}
impl OperatorSpec {
pub fn new<T>(name: T, version: Option<Version>) -> Result<Self, SpecParseError>
where
T: AsRef<str>,
{
let name = name.as_ref();
if !VALID_OPERATORS.contains(&name) {
return Err(SpecParseError::InvalidName {
name: name.to_string(),
});
}
Ok(Self {
name: name.to_string(),
version,
})
}
/// Returns the name used by Helm
pub fn helm_name(&self) -> String {
operator_chart_name(&self.name)
}
/// Returns the repo used by Helm based on the specified version
pub fn helm_repo_name(&self) -> String {
match &self.version {
Some(version) => match version.pre.as_str() {
"nightly" => HELM_REPO_NAME_DEV,
"dev" => HELM_REPO_NAME_DEV,
v => {
if v.starts_with("pr") {
HELM_REPO_NAME_TEST
} else {
HELM_REPO_NAME_STABLE
}
}
},
None => HELM_REPO_NAME_DEV,
}
.into()
}
/// Installs the operator using Helm.
#[instrument(skip_all, fields(
%namespace,
name = %self.name,
// NOTE (@NickLarsenNZ): Option doesn't impl Display, so we need to call
// display for the inner type if it exists. Otherwise we gte the Debug
// impl for the whole Option.
version = self.version.as_ref().map(tracing::field::display),
indicatif.pb_show = true
))]
pub fn install(
&self,
namespace: &str,
chart_source: &ChartSourceType,
values: &Mapping,
) -> Result<(), helm::Error> {
info!(operator = %self, "Installing operator");
Span::current()
.pb_set_message(format!("Installing {name}-operator", name = self.name).as_str());
let version = self.version.as_ref().map(|v| v.to_string());
let helm_name = self.helm_name();
// we can't resolve this any earlier as, for the repository case,
// this will be dependent on the operator version.
let chart_source = match chart_source {
ChartSourceType::OCI => HELM_OCI_REGISTRY.to_string(),
ChartSourceType::Repo => self.helm_repo_name(),
};
let mut helm_values = values.clone();
if self.name == "listener" {
let preset = LISTENER_CLASS_PRESET
.get()
.expect("LISTENER_CLASS_PRESET must have been set")
.as_helm_value();
helm_values.insert(Value::String("preset".to_string()), preset);
}
let helm_values_yaml = if helm_values.is_empty() {
None
} else {
Some(
serde_yaml::to_string(&helm_values)
.expect("serializing a small YAML Mapping back to a YAML string can't fail"),
)
};
// Install using Helm
helm::install_release_from_repo_or_registry(
&helm_name,
helm::ChartVersion {
chart_version: version.as_deref(),
chart_name: &helm_name,
chart_source: &chart_source,
},
helm_values_yaml.as_deref(),
namespace,
true,
)?;
Ok(())
}
/// Uninstalls the operator using Helm.
#[instrument(skip_all, fields(%namespace))]
pub fn uninstall<T>(&self, namespace: T) -> Result<(), helm::Error>
where
T: AsRef<str> + std::fmt::Display + std::fmt::Debug,
{
match helm::uninstall_release(&self.helm_name(), namespace.as_ref(), true) {
Ok(status) => {
indicatif_println!("{status}");
Ok(())
}
Err(err) => Err(err),
}
}
}
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ChartSourceType {
/// OCI registry
OCI,
/// index.yaml-based repositories: resolution (dev, test, stable) is based on the version and thus may be operator-specific
Repo,
}
#[cfg(test)]
mod test {
use rstest::rstest;
use semver::Version;
use crate::{
constants::{HELM_REPO_NAME_DEV, HELM_REPO_NAME_STABLE, HELM_REPO_NAME_TEST},
platform::operator::{OperatorSpec, SpecParseError},
};
#[test]
fn simple_operator_spec() {
match OperatorSpec::try_from("airflow") {
Ok(spec) => {
assert_eq!(spec.name, String::from("airflow"));
assert_eq!(spec.version, None);
}
Err(err) => panic!("{err}"),
}
}
#[test]
fn version_operator_spec() {
match OperatorSpec::try_from("zookeeper=1.2.3") {
Ok(spec) => {
assert_eq!(spec.name, String::from("zookeeper"));
assert_eq!(spec.version, Some(Version::new(1, 2, 3)));
}
Err(err) => panic!("{err}"),
}
}
#[test]
fn empty_operator_spec() {
match OperatorSpec::try_from("") {
Ok(spec) => panic!("SHOULD FAIL: {spec}"),
Err(err) => assert!(matches!(err, SpecParseError::EmptyInput)),
}
}
#[test]
fn empty_version_operator_spec() {
match OperatorSpec::try_from("airflow=") {
Ok(spec) => panic!("SHOULD FAIL: {spec}"),
Err(err) => assert!(matches!(err, SpecParseError::MissingVersion)),
}
}
#[test]
fn invalid_version_operator_spec() {
match OperatorSpec::try_from("airflow=1.2.3=") {
Ok(spec) => panic!("SHOULD FAIL: {spec}"),
Err(err) => assert!(matches!(err, SpecParseError::InvalidEqualSignCount)),
}
}
#[rstest]
#[case("airflow=0.0.0-nightly", HELM_REPO_NAME_DEV)]
#[case("airflow=0.0.0-pr123", HELM_REPO_NAME_TEST)]
#[case("airflow=0.0.0-dev", HELM_REPO_NAME_DEV)]
#[case("airflow=1.2.3", HELM_REPO_NAME_STABLE)]
#[case("airflow", HELM_REPO_NAME_DEV)]
fn repo_name(#[case] input: &str, #[case] repo: &str) {
let spec = OperatorSpec::try_from(input).unwrap();
assert_eq!(spec.helm_repo_name(), repo);
}
}