-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmanifests.rs
More file actions
171 lines (146 loc) · 6.37 KB
/
Copy pathmanifests.rs
File metadata and controls
171 lines (146 loc) · 6.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
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
use std::collections::HashMap;
use snafu::{ResultExt, Snafu};
use stackable_operator::kvp::Labels;
use tracing::{Span, debug, info, instrument};
use tracing_indicatif::span_ext::IndicatifSpanExt as _;
use crate::{
PROGRESS_BAR_STYLE,
common::manifest::ManifestSpec,
helm,
utils::{
k8s::{self, Client},
path::{IntoPathOrUrl, PathOrUrlParseError},
},
xfer::{
self,
processor::{Processor, Template, Yaml},
},
};
#[derive(Debug, Snafu)]
pub enum Error {
/// This error indicates that parsing a string into a path or URL failed.
#[snafu(display("failed to parse {path_or_url:?} as path/url"))]
ParsePathOrUrl {
source: PathOrUrlParseError,
path_or_url: String,
},
/// This error indicates that receiving remote content failed.
#[snafu(display("failed to receive remote content"))]
FileTransfer { source: xfer::Error },
/// This error indicates that the Helm wrapper failed to add the Helm
/// repository.
#[snafu(display("failed to add Helm repository {repo_name}"))]
AddHelmRepository {
source: helm::Error,
repo_name: String,
},
/// This error indicates that the Hlm wrapper failed to install the Helm
/// release.
#[snafu(display("failed to install Helm release {release_name}"))]
InstallHelmRelease {
release_name: String,
source: helm::Error,
},
/// This error indicates that Helm chart options could not be serialized
/// into YAML.
#[snafu(display("failed to serialize Helm chart options"))]
SerializeOptions { source: serde_yaml::Error },
/// This error indicates that the creation of a kube client failed.
#[snafu(display("failed to create Kubernetes client"))]
CreateKubeClient { source: k8s::Error },
/// This error indicates that the kube client failed to deloy manifests.
#[snafu(display("failed to deploy manifests using the kube client"))]
DeployManifest { source: k8s::Error },
}
pub trait InstallManifestsExt {
// TODO (Techassi): This step shouldn't care about templating the manifests nor fetching them from remote
#[instrument(skip_all, fields(%namespace, indicatif.pb_show = true))]
#[allow(clippy::too_many_arguments, async_fn_in_trait)]
async fn install_manifests(
manifests: &[ManifestSpec],
parameters: &HashMap<String, String>,
namespace: &str,
stack_name: &str,
demo_name: Option<&str>,
labels: Labels,
client: &Client,
transfer_client: &xfer::Client,
) -> Result<(), Error> {
debug!("Installing manifests");
Span::current().pb_set_style(&PROGRESS_BAR_STYLE);
Span::current().pb_set_length(manifests.len() as u64);
let mut parameters = parameters.clone();
// We need some additional templating capabilities, e.g. the namespace, so that stacks/demos
// can use that to render e.g. the fqdn service names [which contain the namespace].
parameters.insert("NAMESPACE".to_owned(), namespace.to_owned());
parameters.insert("STACK".to_owned(), stack_name.into());
if let Some(demo_name) = demo_name {
parameters.insert("DEMO".to_owned(), demo_name.into());
}
for manifest in manifests {
let parameters = parameters.clone();
let labels = labels.clone();
match manifest {
ManifestSpec::HelmChart(helm_file) => {
debug!(helm_file, "Installing manifest from Helm chart");
// Read Helm chart YAML and apply templating
let helm_file = helm_file.into_path_or_url().context(ParsePathOrUrlSnafu {
path_or_url: helm_file.clone(),
})?;
let helm_chart: helm::Chart = transfer_client
.get(
&helm_file,
&Template::new(¶meters).then(Yaml::default()),
)
.await
.context(FileTransferSnafu)?;
info!(helm_chart.name, helm_chart.version, "Installing Helm chart",);
// Assumption: that all manifest helm charts refer to repos not registries
helm::add_repo(&helm_chart.repo.name, &helm_chart.repo.url).context(
AddHelmRepositorySnafu {
repo_name: helm_chart.repo.name.clone(),
},
)?;
// Serialize chart options to string
let values_yaml = serde_yaml::to_string(&helm_chart.options)
.context(SerializeOptionsSnafu)?;
// Install the Helm chart using the Helm wrapper
helm::upgrade_or_install_release_from_repo_or_registry(
&helm_chart.release_name,
helm::ChartVersion {
chart_source: &helm_chart.repo.name,
chart_name: &helm_chart.name,
chart_version: Some(&helm_chart.version),
},
Some(&values_yaml),
namespace,
true,
)
.context(InstallHelmReleaseSnafu {
release_name: helm_chart.release_name,
})?;
}
ManifestSpec::PlainYaml(manifest_file) => {
debug!(manifest_file, "Installing YAML manifest");
// Read YAML manifest and apply templating
let path_or_url =
manifest_file
.into_path_or_url()
.context(ParsePathOrUrlSnafu {
path_or_url: manifest_file.clone(),
})?;
let manifests = transfer_client
.get(&path_or_url, &Template::new(¶meters))
.await
.context(FileTransferSnafu)?;
client
.deploy_manifests(&manifests, namespace, labels.clone())
.await
.context(DeployManifestSnafu)?;
}
}
Span::current().pb_inc(1);
}
Ok(())
}
}