k8s_test_framework/
vector.rs

1//! Manage Vector.
2
3use std::process::{Command, Stdio};
4
5use crate::{helm_values_file::HelmValuesFile, resource_file::ResourceFile, up_down, Result};
6
7/// Parameters required to build `kubectl` & `helm` commands to manage charts deployments in the
8/// Kubernetes cluster.
9#[derive(Debug)]
10pub struct CommandBuilder {
11    interface_command: String,
12    namespace: String,
13    helm_chart: String,
14    release_name: String,
15    custom_helm_values_files: Vec<HelmValuesFile>,
16    custom_resource_file: Option<ResourceFile>,
17    custom_env: Option<Vec<(String, String)>>,
18}
19
20impl up_down::CommandBuilder for CommandBuilder {
21    fn build(&self, command_to_build: up_down::CommandToBuild) -> Command {
22        let mut command = Command::new(&self.interface_command);
23        command
24            .arg(match command_to_build {
25                up_down::CommandToBuild::Up => "up",
26                up_down::CommandToBuild::Down => "down",
27            })
28            .arg(&self.namespace)
29            .arg(&self.helm_chart)
30            .arg(&self.release_name)
31            .stdin(Stdio::null());
32
33        command.env(
34            "CUSTOM_HELM_VALUES_FILES",
35            self.custom_helm_values_files
36                .iter()
37                .map(|custom_helm_values_file| custom_helm_values_file.path().to_string_lossy())
38                .collect::<Vec<_>>()
39                .join(" "),
40        );
41
42        if let Some(ref custom_resource_file) = self.custom_resource_file {
43            command.env("CUSTOM_RESOURCE_CONFIGS_FILE", custom_resource_file.path());
44        }
45        if let Some(env) = &self.custom_env {
46            for envvar in env {
47                command.env(envvar.0.clone(), envvar.1.clone());
48            }
49        }
50        command
51    }
52}
53
54/// Vector configuration to deploy.
55#[derive(Debug, Default)]
56pub struct Config<'a> {
57    /// Custom Helm values to set, in the YAML format.
58    /// Set to empty to opt-out of passing any custom values.
59    pub custom_helm_values: Vec<&'a str>,
60
61    /// Custom Kubernetes resource(s) to deploy together with Vector.
62    /// Set to empty to opt-out of deploying custom resources.
63    pub custom_resource: &'a str,
64}
65
66/// Takes care of deploying Vector into the Kubernetes cluster.
67///
68/// Manages the config file secret accordingly, accept additional env var
69pub fn manager(
70    interface_command: &str,
71    namespace: &str,
72    helm_chart: &str,
73    release_name: &str,
74    config: Config<'_>,
75    custom_env: Option<Vec<(String, String)>>,
76) -> Result<up_down::Manager<CommandBuilder>> {
77    let Config {
78        custom_helm_values,
79        custom_resource,
80    } = config;
81    let custom_helm_values_files = custom_helm_values
82        .into_iter()
83        .map(HelmValuesFile::new)
84        .collect::<std::result::Result<Vec<_>, _>>()?;
85    let custom_resource_file = if custom_resource.is_empty() {
86        None
87    } else {
88        Some(ResourceFile::new(custom_resource)?)
89    };
90    Ok(up_down::Manager::new(CommandBuilder {
91        interface_command: interface_command.to_owned(),
92        namespace: namespace.to_owned(),
93        helm_chart: helm_chart.to_owned(),
94        release_name: release_name.to_owned(),
95        custom_helm_values_files,
96        custom_resource_file,
97        custom_env,
98    }))
99}