-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstacklet.rs
More file actions
311 lines (263 loc) · 9.47 KB
/
stacklet.rs
File metadata and controls
311 lines (263 loc) · 9.47 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
use clap::{Args, Subcommand};
use comfy_table::{
presets::{NOTHING, UTF8_FULL},
ContentArrangement, Table,
};
use snafu::{ResultExt, Snafu};
use tracing::{info, instrument};
use stackable_cockpit::{
constants::DEFAULT_NAMESPACE,
platform::stacklet::{self, get_credentials_for_product, list_stacklets},
utils::k8s::{self, Client, DisplayCondition},
};
use crate::{
args::CommonNamespaceArgs,
cli::{Cli, OutputType},
};
#[derive(Debug, Args)]
pub struct StackletArgs {
#[command(subcommand)]
subcommand: StackletCommands,
}
#[derive(Debug, Subcommand)]
pub enum StackletCommands {
/// Display credentials for a stacklet
#[command(aliases(["creds", "cr"]))]
Credentials(StackletCredentialsArgs),
/// List deployed stacklets
#[command(alias("ls"))]
List(StackletListArgs),
}
#[derive(Debug, Args)]
pub struct StackletCredentialsArgs {
/// The name of the product, for example 'superset'.
product_name: String,
/// The name of the stacklet, for example 'superset'.
stacklet_name: String,
/// Namespace in the cluster used to deploy the products.
#[arg(
long,
short = 'n',
global = true,
default_value = DEFAULT_NAMESPACE,
aliases(["product-ns", "product-namespace"]),
long_help = "Namespace in the cluster used to deploy the products. Use this to select
a different namespace for credential lookup.")]
pub namespace: String,
}
#[derive(Debug, Args)]
pub struct StackletListArgs {
#[arg(short, long = "output", value_enum, default_value_t)]
output_type: OutputType,
#[command(flatten)]
namespaces: CommonNamespaceArgs,
}
#[derive(Debug, Snafu)]
pub enum CmdError {
#[snafu(display("failed to list stacklets"))]
StackletList { source: stacklet::Error },
#[snafu(display("failed to retrieve credentials for stacklet"))]
StackletCredentials { source: stacklet::Error },
#[snafu(display("failed to serialize YAML output"))]
SerializeYamlOutput { source: serde_yaml::Error },
#[snafu(display("failed to serialize JSON output"))]
SerializeJsonOutput { source: serde_json::Error },
#[snafu(display("failed to create Kubernetes client"))]
KubeClientCreate { source: k8s::Error },
}
impl StackletArgs {
pub async fn run(&self, cli: &Cli) -> Result<String, CmdError> {
match &self.subcommand {
StackletCommands::List(args) => list_cmd(args, cli).await,
StackletCommands::Credentials(args) => credentials_cmd(args).await,
}
}
}
#[instrument(skip_all)]
async fn list_cmd(args: &StackletListArgs, cli: &Cli) -> Result<String, CmdError> {
info!("Listing installed stacklets");
let client = Client::new().await.context(KubeClientCreateSnafu)?;
// If the user wants to list stacklets from all namespaces, we use `None`.
// `None` indicates that don't want to list stacklets scoped to only ONE
// namespace.
let stacklets = list_stacklets(&client, Some(&args.namespaces.namespace))
.await
.context(StackletListSnafu)?;
if stacklets.is_empty() {
let mut result = cli.result();
result
.with_command_hint(
"stackablectl stack install <STACK_NAME>",
"install a complete stack",
)
.with_command_hint(
"stackablectl demo install <DEMO_NAME>",
"install an end-to-end demo",
)
.with_output("No stacklets found");
return Ok(result.render());
}
match args.output_type {
OutputType::Plain | OutputType::Table => {
let (arrangement, preset) = match args.output_type {
OutputType::Plain => (ContentArrangement::Disabled, NOTHING),
_ => (ContentArrangement::Dynamic, UTF8_FULL),
};
// The main table displays all installed (and discovered) stacklets
// and their condition.
let mut table = Table::new();
table
.set_header(vec![
"PRODUCT",
"NAME",
"NAMESPACE",
"ENDPOINTS",
"CONDITIONS",
])
.set_content_arrangement(arrangement)
.load_preset(preset);
let mut error_list = Vec::new();
let mut error_index = 1;
let max_endpoint_name_length = match args.output_type {
OutputType::Plain => 0,
_ => stacklets
.iter()
.flat_map(|s| &s.endpoints)
.map(|(endpoint_name, _)| endpoint_name.len())
.max()
.unwrap_or_default(),
};
for stacklet in stacklets {
let ConditionOutput { summary, errors } =
render_conditions(stacklet.conditions, &mut error_index);
let endpoints = stacklet
.endpoints
.iter()
.map(|(name, url)| {
format!("{name:width$} {url}", width = max_endpoint_name_length + 1)
})
.collect::<Vec<_>>()
.join("\n");
table.add_row(vec![
stacklet.product,
stacklet.name,
stacklet.namespace.unwrap_or_default(),
endpoints,
summary,
]);
if let Some(err) = render_errors(errors) {
error_list.push(err)
}
}
let mut result = cli.result();
result
.with_command_hint(
"stackablectl stacklet credentials [OPTIONS] <PRODUCT_NAME> <STACKLET_NAME>",
"display credentials for deployed stacklets",
)
.with_output(format!(
"{table}{errors}",
errors = if !error_list.is_empty() {
format!("\n\n{}", error_list.join("\n"))
} else {
"".into()
}
));
Ok(result.render())
}
OutputType::Json => serde_json::to_string(&stacklets).context(SerializeJsonOutputSnafu),
OutputType::Yaml => serde_yaml::to_string(&stacklets).context(SerializeYamlOutputSnafu),
}
}
#[instrument(skip_all)]
async fn credentials_cmd(args: &StackletCredentialsArgs) -> Result<String, CmdError> {
info!("Displaying stacklet credentials");
let client = Client::new().await.context(KubeClientCreateSnafu)?;
match get_credentials_for_product(
&client,
&args.namespace,
&args.stacklet_name,
&args.product_name,
)
.await
.context(StackletCredentialsSnafu)?
{
Some(credentials) => {
let mut table = Table::new();
table
.set_content_arrangement(ContentArrangement::Dynamic)
.load_preset(NOTHING)
.add_row(vec!["USERNAME", &credentials.username])
.add_row(vec!["PASSWORD", &credentials.password]);
let output = format!(
"Credentials for {} ({}) in namespace '{}':",
args.product_name, args.stacklet_name, args.namespace
);
Ok(format!("{}\n\n{}", output, table))
}
None => Ok("No credentials".into()),
}
}
pub struct ConditionOutput {
summary: String,
errors: Vec<String>,
}
/// Renders conditions for a single stacklet / product. It returns a
/// concatenated string of conditions (which are colored green and red) to
/// display next to each listed stacklet in the table. Additionally, it also
/// returns a list of errors to be displayed underneath the stacklet table.
fn render_conditions(
product_conditions: Vec<DisplayCondition>,
error_index: &mut usize,
) -> ConditionOutput {
let mut conditions = Vec::new();
let mut errors = Vec::new();
for cond in product_conditions {
conditions.push(color_condition(&cond.condition, cond.is_good, *error_index));
if let Some(error) = render_condition_error(cond.message, cond.is_good, *error_index) {
errors.push(error);
*error_index += 1;
};
}
ConditionOutput {
summary: conditions.join(", "),
errors,
}
}
/// Renders one condition and determines if it is an error (not good). If this
/// is the case, it get colored red and is returned.
fn render_condition_error(
message: Option<String>,
is_good: Option<bool>,
error_index: usize,
) -> Option<String> {
if !is_good.unwrap_or(true) {
let message = message.unwrap_or("-".into());
return Some(format!("[{}]: {}", error_index, message));
}
None
}
// TODO (Techassi): Add back color support
/// Adds an error index to the output.
fn color_condition(condition: &str, is_good: Option<bool>, error_index: usize) -> String {
match is_good {
Some(is_good) => {
if is_good {
condition.to_owned()
} else {
format!("{}: See [{}]", condition, error_index)
}
}
None => condition.to_owned(),
}
}
/// Renders multiple errors (of one stacklet)
fn render_errors(errors: Vec<String>) -> Option<String> {
if errors.is_empty() {
None
} else if errors.len() == 1 {
Some(errors[0].clone())
} else {
Some(format!("{}\n---\n", errors.join("\n")))
}
}