-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathspec.rs
More file actions
221 lines (191 loc) · 7.28 KB
/
Copy pathspec.rs
File metadata and controls
221 lines (191 loc) · 7.28 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
use serde::{Deserialize, Serialize};
use snafu::{OptionExt, ResultExt, Snafu};
use tracing::{Span, debug, info, instrument, warn};
use tracing_indicatif::span_ext::IndicatifSpanExt as _;
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use crate::{
common::manifest::ManifestSpec,
platform::{
cluster::{ResourceRequests, ResourceRequestsError},
demo::DemoInstallParameters,
manifests::{self, InstallManifestsExt},
release::ReleaseList,
stack::{self, StackInstallParameters, StackList},
},
utils::{
k8s::Client,
params::{
IntoParameters, IntoParametersError, Parameter, RawParameter, RawParameterParseError,
},
},
xfer,
};
pub type RawDemoParameterParseError = RawParameterParseError;
pub type RawDemoParameter = RawParameter;
pub type DemoParameter = Parameter;
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("no stack named {name:?}"))]
NoSuchStack { name: String },
#[snafu(display("demo resource requests error"), context(false))]
DemoResourceRequests { source: ResourceRequestsError },
#[snafu(display("cannot install demo in namespace {requested:?}, only {supported:?} supported", supported = supported.join(", ")))]
UnsupportedNamespace {
requested: String,
supported: Vec<String>,
},
#[snafu(display("failed to parse demo / stack parameters"))]
ParseParameters { source: IntoParametersError },
#[snafu(display("failed to install stack"))]
InstallStack { source: stack::Error },
#[snafu(display("failed to install stack manifests"))]
InstallManifests { source: manifests::Error },
}
impl InstallManifestsExt for DemoSpec {}
/// This struct describes a demo with the v2 spec
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct DemoSpec {
/// A short description of the demo
pub description: String,
/// An optional link to a documentation page
#[serde(skip_serializing_if = "Option::is_none")]
pub documentation: Option<String>,
/// Supported namespaces this demo can run in. An empty list indicates that
/// the demo can run in any namespace.
#[serde(default)]
pub supported_namespaces: Vec<String>,
/// The name of the underlying stack
#[serde(rename = "stackableStack")]
pub stack: String,
/// A variable number of labels (tags)
#[serde(default)]
pub labels: Vec<String>,
/// A variable number of Helm or YAML manifests
#[serde(default)]
pub manifests: Vec<ManifestSpec>,
/// The resource requests the demo imposes on a Kubernetes cluster
pub resource_requests: Option<ResourceRequests>,
/// A variable number of supported parameters
#[serde(default)]
pub parameters: Vec<Parameter>,
}
impl DemoSpec {
/// Checks if the prerequisites to run this demo are met. These checks
/// include:
///
/// - Does the demo support to be installed in the requested namespace?
/// - Does the cluster have enough resources available to run this demo?
#[instrument(skip_all)]
pub async fn check_prerequisites(&self, client: &Client, namespace: &str) -> Result<(), Error> {
debug!("Checking prerequisites before installing demo");
// Returns an error if the demo doesn't support to be installed in the
// requested namespace
if !self.supports_namespace(namespace) {
return Err(Error::UnsupportedNamespace {
requested: namespace.to_owned(),
supported: self.supported_namespaces.clone(),
});
}
// Checks if the available cluster resources are sufficient to deploy
// the demo.
if let Some(resource_requests) = &self.resource_requests {
if let Err(err) = resource_requests
.validate_cluster_size(client, "demo")
.await
{
match err {
ResourceRequestsError::ValidationErrors { errors } => {
for error in errors {
warn!("{error}");
}
}
err => return Err(err.into()),
}
}
}
Ok(())
}
#[instrument(skip_all, fields(
stack_name = %self.stack,
operator_namespace = %install_parameters.operator_namespace,
demo_namespace = %install_parameters.demo_namespace,
))]
pub async fn install(
&self,
stack_list: StackList,
release_list: ReleaseList,
install_parameters: DemoInstallParameters,
client: &Client,
transfer_client: &xfer::Client,
) -> Result<(), Error> {
// Get the stack spec based on the name defined in the demo spec
let stack = stack_list.get(&self.stack).context(NoSuchStackSnafu {
name: self.stack.clone(),
})?;
// Check demo prerequisites
self.check_prerequisites(client, &install_parameters.demo_namespace)
.await?;
let stack_install_parameters = StackInstallParameters {
stack_name: self.stack.clone(),
demo_name: Some(install_parameters.demo_name.clone()),
operator_namespace: install_parameters.operator_namespace.clone(),
stack_namespace: install_parameters.demo_namespace.clone(),
parameters: install_parameters.stack_parameters.clone(),
labels: install_parameters.stack_labels.clone(),
skip_release: install_parameters.skip_release,
chart_source: install_parameters.chart_source.clone(),
operator_values: install_parameters.operator_values.clone(),
};
stack
.install(
release_list,
stack_install_parameters,
client,
transfer_client,
)
.await
.context(InstallStackSnafu)?;
// Install demo manifests
self.prepare_manifests(install_parameters, client, transfer_client)
.await
}
#[instrument(skip_all, fields(
stack_name = %self.stack,
operator_namespace = %install_params.operator_namespace,
demo_namespace = %install_params.demo_namespace,
indicatif.pb_show = true
))]
async fn prepare_manifests(
&self,
install_params: DemoInstallParameters,
client: &Client,
transfer_client: &xfer::Client,
) -> Result<(), Error> {
info!("Installing demo manifests");
Span::current().pb_set_message("Installing manifests");
let params = install_params
.parameters
.to_owned()
.into_params(&self.parameters)
.context(ParseParametersSnafu)?;
Self::install_manifests(
&self.manifests,
¶ms,
&install_params.demo_namespace,
&install_params.stack_name,
Some(&install_params.demo_name),
install_params.labels,
client,
transfer_client,
)
.await
.context(InstallManifestsSnafu)
}
fn supports_namespace(&self, namespace: impl Into<String>) -> bool {
self.supported_namespaces.is_empty()
|| self.supported_namespaces.contains(&namespace.into())
}
}