From 179653b803833ccd5304fe469d62980d09c691a9 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Mon, 20 Sep 2021 19:22:40 +0300 Subject: [PATCH 01/21] add app and deployment overrides info scripts --- helm-control-scripts/Cargo.lock | 2 +- helm-control-scripts/Cargo.toml | 12 ++++++++-- .../src/default_app_overrides.rs | 13 +++++++++++ .../src/default_deployment_overrides.rs | 13 +++++++++++ helm-control-scripts/src/lib.rs | 23 +++++++++++++++++++ 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 helm-control-scripts/src/default_app_overrides.rs create mode 100644 helm-control-scripts/src/default_deployment_overrides.rs diff --git a/helm-control-scripts/Cargo.lock b/helm-control-scripts/Cargo.lock index 3ae85ea0..6c606154 100644 --- a/helm-control-scripts/Cargo.lock +++ b/helm-control-scripts/Cargo.lock @@ -465,7 +465,7 @@ dependencies = [ [[package]] name = "helm-control-scripts" -version = "0.1.0" +version = "0.2.0" dependencies = [ "dkregistry", "env_logger", diff --git a/helm-control-scripts/Cargo.toml b/helm-control-scripts/Cargo.toml index ad7ef0f0..8dc7ca62 100644 --- a/helm-control-scripts/Cargo.toml +++ b/helm-control-scripts/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "helm-control-scripts" -version = "0.1.0" +version = "0.2.0" authors = ["Aleksei Sizov "] edition = "2018" @@ -55,4 +55,12 @@ path = "src/init.rs" [[bin]] name = "tag_check" -path = "src/tag_check.rs" \ No newline at end of file +path = "src/tag_check.rs" + +[[bin]] +name = "default_app_overrides" +path = "src/default_app_overrides.rs" + +[[bin]] +name = "default_deployment_overrides" +path = "src/default_deployment_overrides.rs" \ No newline at end of file diff --git a/helm-control-scripts/src/default_app_overrides.rs b/helm-control-scripts/src/default_app_overrides.rs new file mode 100644 index 00000000..3f15b63c --- /dev/null +++ b/helm-control-scripts/src/default_app_overrides.rs @@ -0,0 +1,13 @@ +use helm_control_scripts::lib::*; + +fn main() { + let mut log_builder = Builder::from_default_env(); + log_builder.target(Target::Stderr).filter(None, LevelFilter::Info).init(); + info!("Utils version {}", env!("CARGO_PKG_VERSION")); + let envs = EnvVars::parse(); + info!("Env variables received {:?}", &envs); + let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); + let cli_opts = CliOpts::from_args(); + info!("Cli options received {:?}", &cli_opts); + print!("{}", print_kv(default_values.app_overrides())); +} diff --git a/helm-control-scripts/src/default_deployment_overrides.rs b/helm-control-scripts/src/default_deployment_overrides.rs new file mode 100644 index 00000000..7cfb8e21 --- /dev/null +++ b/helm-control-scripts/src/default_deployment_overrides.rs @@ -0,0 +1,13 @@ +use helm_control_scripts::lib::*; + +fn main() { + let mut log_builder = Builder::from_default_env(); + log_builder.target(Target::Stderr).filter(None, LevelFilter::Info).init(); + info!("Utils version {}", env!("CARGO_PKG_VERSION")); + let envs = EnvVars::parse(); + info!("Env variables received {:?}", &envs); + let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); + let cli_opts = CliOpts::from_args(); + info!("Cli options received {:?}", &cli_opts); + print!("{}", print_kv(Some(default_values.deployment_overrides()))); +} diff --git a/helm-control-scripts/src/lib.rs b/helm-control-scripts/src/lib.rs index 3899a8a1..d67f4f98 100644 --- a/helm-control-scripts/src/lib.rs +++ b/helm-control-scripts/src/lib.rs @@ -75,6 +75,29 @@ pub mod lib { pub chart_name: String, } + impl DefaultValues { + pub fn app_overrides(&self) -> Option> { + if self.default_overrides.is_empty() { + None + } else { + let mut overrides: Vec<(String,String)> = Vec::new(); + for app_override in &self.default_overrides { + let split_override = app_override.split('=').collect::>(); + overrides.push((split_override.first().unwrap().to_string(), split_override.last().unwrap().to_string())); + } + Some(overrides) + } + } + pub fn deployment_overrides(&self) -> Vec<(String,String)> { + vec![ + (String::from("chart_repo_url"), self.chart_repo_url.clone()), + (String::from("chart_repo_name"), self.chart_repo_name.clone()), + (String::from("chart_version"), self.chart_version.clone()), + (String::from("chart_name"), self.chart_name.clone()), + ] + } + } + #[derive(Debug, Clone)] pub struct HelmDeploymentParameters { pub chart_repo_url: String, From 19a645b97a4077f8305c6013aaa2f2065fa041d0 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Wed, 22 Sep 2021 14:06:03 +0300 Subject: [PATCH 02/21] overrides/keys scripts --- helm-control-scripts/Cargo.toml | 20 ++++-- helm-control-scripts/Dockerfile | 7 ++- helm-control-scripts/src/app_keys.rs | 36 +++++++++++ ...ault_app_overrides.rs => app_overrides.rs} | 2 - helm-control-scripts/src/deployment_keys.rs | 11 ++++ ...t_overrides.rs => deployment_overrides.rs} | 2 - helm-control-scripts/src/lib.rs | 63 +++++++++++++++++++ helm-control-scripts/src/unarchive.rs | 7 +++ 8 files changed, 139 insertions(+), 9 deletions(-) create mode 100644 helm-control-scripts/src/app_keys.rs rename helm-control-scripts/src/{default_app_overrides.rs => app_overrides.rs} (83%) create mode 100644 helm-control-scripts/src/deployment_keys.rs rename helm-control-scripts/src/{default_deployment_overrides.rs => deployment_overrides.rs} (84%) create mode 100644 helm-control-scripts/src/unarchive.rs diff --git a/helm-control-scripts/Cargo.toml b/helm-control-scripts/Cargo.toml index 8dc7ca62..a9497051 100644 --- a/helm-control-scripts/Cargo.toml +++ b/helm-control-scripts/Cargo.toml @@ -58,9 +58,21 @@ name = "tag_check" path = "src/tag_check.rs" [[bin]] -name = "default_app_overrides" -path = "src/default_app_overrides.rs" +name = "app_overrides" +path = "src/app_overrides.rs" [[bin]] -name = "default_deployment_overrides" -path = "src/default_deployment_overrides.rs" \ No newline at end of file +name = "deployment_overrides" +path = "src/deployment_overrides.rs" + +[[bin]] +name = "deployment_keys" +path = "src/deployment_keys.rs" + +[[bin]] +name = "app_keys" +path = "src/app_keys.rs" + +[[bin]] +name = "unarchive" +path = "src/unarchive.rs" \ No newline at end of file diff --git a/helm-control-scripts/Dockerfile b/helm-control-scripts/Dockerfile index 98b42136..aaf37f08 100644 --- a/helm-control-scripts/Dockerfile +++ b/helm-control-scripts/Dockerfile @@ -22,4 +22,9 @@ ADD target/x86_64-unknown-linux-musl/release/create /utils/ ADD target/x86_64-unknown-linux-musl/release/info /utils/ ADD target/x86_64-unknown-linux-musl/release/update /utils/ ADD target/x86_64-unknown-linux-musl/release/init /utils/ -ADD target/x86_64-unknown-linux-musl/release/tag_check /utils/ \ No newline at end of file +ADD target/x86_64-unknown-linux-musl/release/tag_check /utils/ +ADD target/x86_64-unknown-linux-musl/release/app_overrides /utils/ +ADD target/x86_64-unknown-linux-musl/release/app_keys /utils/ +ADD target/x86_64-unknown-linux-musl/release/deployment_overrides /utils/ +ADD target/x86_64-unknown-linux-musl/release/deployment_keys /utils/ +ADD target/x86_64-unknown-linux-musl/release/unarchive /utils/ diff --git a/helm-control-scripts/src/app_keys.rs b/helm-control-scripts/src/app_keys.rs new file mode 100644 index 00000000..62a3af80 --- /dev/null +++ b/helm-control-scripts/src/app_keys.rs @@ -0,0 +1,36 @@ +use helm_control_scripts::lib::*; + +fn main() { + let mut log_builder = Builder::from_default_env(); + log_builder.target(Target::Stderr).filter(None, LevelFilter::Info).init(); + info!("Utils version {}", env!("CARGO_PKG_VERSION")); + let envs = EnvVars::parse(); + info!("Env variables received {:?}", &envs); + let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); + // let cli_opts = CliOpts::from_args(); + // info!("Cli options received {:?}", &cli_opts); + // let domain_name = domain_name(&cli_opts); + let deployment_parameters = HelmDeploymentParameters::new_env_only(&default_values, &envs); + helm_init(&envs, &deployment_parameters); + let helm_values = HelmCmd { + name: envs.helm_bin, + mode: HelmMode::ShowValues, + release_name: String::from(""), + release_domain: String::from(""), + namespace: String::from(""), + deployment_parameters: deployment_parameters, + overrides: vec![], + default_values: default_values.default_overrides, + image_tag: String::from("") + }; + info!("Generated Helm args: {:?}", &helm_values.args()); + match helm_values.run_stdout() { + Ok(status) => { + print!("{}", print_keys(helm_values_as_keys(status))); + } + Err(status) => { + error!("Error during helm execution"); + panic!("{:?}", status); + } + } +} diff --git a/helm-control-scripts/src/default_app_overrides.rs b/helm-control-scripts/src/app_overrides.rs similarity index 83% rename from helm-control-scripts/src/default_app_overrides.rs rename to helm-control-scripts/src/app_overrides.rs index 3f15b63c..1fb041e0 100644 --- a/helm-control-scripts/src/default_app_overrides.rs +++ b/helm-control-scripts/src/app_overrides.rs @@ -7,7 +7,5 @@ fn main() { let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); - let cli_opts = CliOpts::from_args(); - info!("Cli options received {:?}", &cli_opts); print!("{}", print_kv(default_values.app_overrides())); } diff --git a/helm-control-scripts/src/deployment_keys.rs b/helm-control-scripts/src/deployment_keys.rs new file mode 100644 index 00000000..536f0023 --- /dev/null +++ b/helm-control-scripts/src/deployment_keys.rs @@ -0,0 +1,11 @@ +use helm_control_scripts::lib::*; + +fn main() { + let mut log_builder = Builder::from_default_env(); + log_builder.target(Target::Stderr).filter(None, LevelFilter::Info).init(); + info!("Utils version {}", env!("CARGO_PKG_VERSION")); + let envs = EnvVars::parse(); + info!("Env variables received {:?}", &envs); + let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); + print!("{}", print_keys(Some(default_values.deployment_keys()))); +} diff --git a/helm-control-scripts/src/default_deployment_overrides.rs b/helm-control-scripts/src/deployment_overrides.rs similarity index 84% rename from helm-control-scripts/src/default_deployment_overrides.rs rename to helm-control-scripts/src/deployment_overrides.rs index 7cfb8e21..831419b9 100644 --- a/helm-control-scripts/src/default_deployment_overrides.rs +++ b/helm-control-scripts/src/deployment_overrides.rs @@ -7,7 +7,5 @@ fn main() { let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); - let cli_opts = CliOpts::from_args(); - info!("Cli options received {:?}", &cli_opts); print!("{}", print_kv(Some(default_values.deployment_overrides()))); } diff --git a/helm-control-scripts/src/lib.rs b/helm-control-scripts/src/lib.rs index d67f4f98..188c543d 100644 --- a/helm-control-scripts/src/lib.rs +++ b/helm-control-scripts/src/lib.rs @@ -96,6 +96,14 @@ pub mod lib { (String::from("chart_name"), self.chart_name.clone()), ] } + pub fn deployment_keys(&self) -> Vec { + vec![ + String::from("chart_repo_url"), + String::from("chart_repo_name"), + String::from("chart_version"), + String::from("chart_name"), + ] + } } #[derive(Debug, Clone)] @@ -190,6 +198,13 @@ pub mod lib { args.extend(chart_version); args.extend(self.set_flag_values()); }, + HelmMode::ShowValues => { + args.extend(namespace); + args.push(String::from("show")); + args.push(String::from("values")); + args.push(chart_location); + args.extend(chart_version); + }, HelmMode::RepoAdd => { args.push(String::from("repo")); args.push(String::from("add")); @@ -260,6 +275,7 @@ pub mod lib { RepoAdd, RepoUpdate, Status, + ShowValues, } pub struct KubectlCmd { @@ -529,6 +545,18 @@ pub mod lib { } return out_string; } + pub fn print_keys(vals: Option>) -> String { + let mut out_string = String::from(""); + match vals { + Some(values) => { + for value in values { + out_string.push_str(&format!("{}\n", value)); + } + }, + None => return out_string + } + return out_string; + } //TODO: Generics!!! pub fn deployments_statefulsets_to_images(deployments: Vec, statefulsets: Vec) -> Option> { let mut images: Vec = Vec::new(); @@ -685,4 +713,39 @@ pub mod lib { } } } + pub fn helm_values_as_keys(yaml: String) -> Option> { + let mut keys: Vec = Vec::new(); + let docs = YamlLoader::load_from_str(&yaml).unwrap(); + let docs_hash = &docs[0].clone().into_hash().unwrap(); + for doc in docs_hash { + match doc.1.as_hash() { + Some(hash) => { + if hash.is_empty() { + keys.push(format!("{}", doc.0.as_str().unwrap())); + }else{ + for doc_2 in hash { + match doc_2.1.as_hash() { + Some(hash_2) => { + if hash_2.is_empty() { + keys.push(format!("{}.{}", doc.0.as_str().unwrap(), doc_2.0.as_str().unwrap())); + }else{ + for key in hash_2.keys() { + keys.push(format!("{}.{}.{}", doc.0.as_str().unwrap(), doc_2.0.as_str().unwrap(), key.as_str().unwrap())); + } + } + }, + None => keys.push(format!("{}.{}", doc.0.as_str().unwrap(), doc_2.0.as_str().unwrap())) + } + } + } + }, + None => continue + } + } + if keys.is_empty() { + None + }else{ + Some(keys) + } + } } diff --git a/helm-control-scripts/src/unarchive.rs b/helm-control-scripts/src/unarchive.rs new file mode 100644 index 00000000..e94e6435 --- /dev/null +++ b/helm-control-scripts/src/unarchive.rs @@ -0,0 +1,7 @@ +use helm_control_scripts::lib::*; + +fn main() { + let mut log_builder = Builder::from_default_env(); + log_builder.target(Target::Stdout).filter(None, LevelFilter::Info).init(); + warn!("This is mockup!"); +} From 33cdcd1eb8b7bc81fe510f850bc22b60bc552134 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Wed, 22 Sep 2021 14:06:26 +0300 Subject: [PATCH 03/21] add new scripts to the chart config --- charts/octopod/templates/octopod-configmap.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/charts/octopod/templates/octopod-configmap.yaml b/charts/octopod/templates/octopod-configmap.yaml index de7bcdf2..2bd16e96 100644 --- a/charts/octopod/templates/octopod-configmap.yaml +++ b/charts/octopod/templates/octopod-configmap.yaml @@ -18,6 +18,11 @@ data: ARCHIVE_CHECKING_COMMAND: {{ printf "%s/archive_check" (include "controlScriptsPath" .) | quote }} TAG_CHECKING_COMMAND: {{ printf "%s/tag_check" (include "controlScriptsPath" .) | quote }} INFO_COMMAND: {{ printf "%s/info" (include "controlScriptsPath" .) | quote }} + DEPLOYMENT_OVERRIDES_COMMAND: {{ printf "%s/deployment_overrides" (include "controlScriptsPath" .) | quote }} + DEPLOYMENT_KEYS_COMMAND: {{ printf "%s/deployment_keys" (include "controlScriptsPath" .) | quote }} + APPLICATION_OVERRIDES_COMMAND: {{ printf "%s/app_overrides" (include "controlScriptsPath" .) | quote }} + APPLICATION_KEYS_COMMAND: {{ printf "%s/app_keys" (include "controlScriptsPath" .) | quote }} + UNARCHIVE_COMMAND: {{ printf "%s/unarchive" (include "controlScriptsPath" .) | quote }} {{- range $name, $value := .Values.octopod.env }} {{ $name }}: {{ $value | quote }} {{- end }} From 5538087c54796e5a00155a40d270080695d5111e Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Wed, 22 Sep 2021 20:53:09 +0300 Subject: [PATCH 04/21] archive using scaling --- helm-control-scripts/src/archive.rs | 44 +++++++-- helm-control-scripts/src/check.rs | 2 +- helm-control-scripts/src/cleanup.rs | 19 ++++ helm-control-scripts/src/lib.rs | 130 +++++++++++++++++++++++--- helm-control-scripts/src/unarchive.rs | 53 ++++++++++- 5 files changed, 222 insertions(+), 26 deletions(-) diff --git a/helm-control-scripts/src/archive.rs b/helm-control-scripts/src/archive.rs index c5f92ce8..da50a305 100644 --- a/helm-control-scripts/src/archive.rs +++ b/helm-control-scripts/src/archive.rs @@ -9,21 +9,47 @@ fn main() { let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); + let overrides = match overrides(&cli_opts) { + Some(inner) => inner, + None => vec![], + }; + let domain_name = domain_name(&cli_opts); + info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); - let helm_cmd = HelmCmd { + let image_tag = match cli_opts.tag { + Some(tag) => tag, + None => { + error!("mandatory tag argument was not provided"); + panic!(); + } + }; + let namespace = String::from(&cli_opts.namespace); + let helm_template = HelmCmd { name: envs.helm_bin, - mode: HelmMode::Uninstall, + mode: HelmMode::Template, release_name: cli_opts.name, - release_domain: String::from(""), + release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, - overrides: vec![], - default_values: vec![], - image_tag: String::from("") + overrides: overrides, + default_values: default_values.default_overrides, + image_tag: image_tag }; - info!("Generated Helm args: {:?}", &helm_cmd.args()); - match helm_cmd.run() { - Ok(_status) => info!("Success!"), + info!("Generated Helm args: {:?}", &helm_template.args()); + match helm_template.run_stdout() { + Ok(status) => { + let (deployments, statefulsets, _ingresses, _old_ingresses) = match parse_to_k8s(status) { + Ok((deployments, statefulsets, ingresses, old_ingresses)) => (deployments, statefulsets, ingresses, old_ingresses), + Err(err) => panic!("{}", err) + }; + match scale(deployments, statefulsets, namespace, 0) { + Ok(_status) => info!("Success!"), + Err(status) => { + error!("Error checking statuses"); + panic!("{}", status); + } + } + } Err(status) => { error!("Error during helm execution"); panic!("{:?}", status); diff --git a/helm-control-scripts/src/check.rs b/helm-control-scripts/src/check.rs index 34b16a02..86025a1e 100644 --- a/helm-control-scripts/src/check.rs +++ b/helm-control-scripts/src/check.rs @@ -47,7 +47,7 @@ fn main() { Ok(_status) => info!("Success!"), Err(status) => { error!("Error checking statuses"); - panic!("{}", status); + panic!("{:?}", status); } } } diff --git a/helm-control-scripts/src/cleanup.rs b/helm-control-scripts/src/cleanup.rs index 262e00fb..2dd78814 100644 --- a/helm-control-scripts/src/cleanup.rs +++ b/helm-control-scripts/src/cleanup.rs @@ -10,6 +10,25 @@ fn main() { let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); + let helm_cmd = HelmCmd { + name: envs.helm_bin, + mode: HelmMode::Uninstall, + release_name: cli_opts.name.clone(), + release_domain: String::from(""), + namespace: cli_opts.namespace.clone(), + deployment_parameters: deployment_parameters.clone(), + overrides: vec![], + default_values: vec![], + image_tag: String::from("") + }; + info!("Generated Helm args: {:?}", &helm_cmd.args()); + match helm_cmd.run() { + Ok(_status) => info!("Success!"), + Err(status) => { + error!("Error during helm execution"); + panic!("{:?}", status); + } + } let kubectl_cmd = KubectlCmd { name: envs.kubectl_bin, release_name: cli_opts.name, diff --git a/helm-control-scripts/src/lib.rs b/helm-control-scripts/src/lib.rs index 188c543d..e02839ea 100644 --- a/helm-control-scripts/src/lib.rs +++ b/helm-control-scripts/src/lib.rs @@ -1,10 +1,13 @@ pub mod lib { pub use std::io::prelude::*; pub use std::process::{Command, ExitStatus, Stdio}; + pub use std::error::Error; + pub use std::fmt; pub use structopt::StructOpt; pub use env_logger::{Builder, Target}; pub use log::{LevelFilter, info, warn, error}; pub use serde::{Serialize, Deserialize}; + pub use serde_json::json; pub use std::error; pub use yaml_rust::{YamlLoader, YamlEmitter}; pub use k8s_openapi::api::{ @@ -13,12 +16,12 @@ pub mod lib { networking::v1beta1::Ingress as OldIngress }; pub use kube::{ - api::{Api, ListParams, ResourceExt}, + api::{Api, ListParams, ResourceExt, Patch, PatchParams}, Client, }; pub use dkregistry::{ v2::Client as RegClient, - errors::Error + errors::Error as RegError, }; pub use regex::Regex; @@ -395,8 +398,37 @@ pub mod lib { } return Ok((deployments, statefulsets, ingresses, old_ingresses)); } + + #[derive(Debug)] + pub struct NoReplicasError(String); + + impl fmt::Display for NoReplicasError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Deployment {} doesn't have any replicas", self.0) + } + } + impl Error for NoReplicasError {} + + #[derive(Debug)] + pub enum KubeError { + NoReplicasError(NoReplicasError), + KubeApiError(kube::Error), + } + + impl From for KubeError { + fn from(error: NoReplicasError) -> Self { + KubeError::NoReplicasError(error) + } + } + + impl From for KubeError { + fn from(error: kube::Error) -> Self { + KubeError::KubeApiError(error) + } + } + #[tokio::main] - async fn check_deployment(namespace: &str, name: &str) -> Result<(), kube::Error> { + async fn check_deployment(namespace: &str, name: &str) -> Result<(), KubeError> { let client = Client::try_default().await?; let api: Api = Api::namespaced(client, &namespace); let deployment = api.get(&name).await?; @@ -405,22 +437,22 @@ pub mod lib { match status.available_replicas { Some(replicas) => { if !replicas >= 1 { - panic!("Deployment {} doesn't have any replicas", &name); + return Err(KubeError::NoReplicasError(NoReplicasError(name.to_string()))); } }, None => { - panic!("Deployment {} doesn't have any replicas", &name); + return Err(KubeError::NoReplicasError(NoReplicasError(name.to_string()))); } } }, None => { - panic!("Unable to get status for deployment {}", &name); + return Err(KubeError::NoReplicasError(NoReplicasError(name.to_string()))); } } Ok(()) } #[tokio::main] - async fn check_statefulset(namespace: &str, name: &str) -> Result<(), kube::Error> { + async fn check_statefulset(namespace: &str, name: &str) -> Result<(), KubeError> { let client = Client::try_default().await?; let api: Api = Api::namespaced(client, &namespace); let statefulset = api.get(&name).await?; @@ -429,21 +461,21 @@ pub mod lib { match status.current_replicas { Some(replicas) => { if !replicas >= 1 { - panic!("StatefulSet {} doesn't have any replicas", &name); + return Err(KubeError::NoReplicasError(NoReplicasError(name.to_string()))); } }, None => { - panic!("StatefulSet {} doesn't have any replicas", &name); + return Err(KubeError::NoReplicasError(NoReplicasError(name.to_string()))); } } }, None => { - panic!("Unable to get status for StatefulSet {}", &name); + return Err(KubeError::NoReplicasError(NoReplicasError(name.to_string()))); } } Ok(()) } - pub fn check_all(deployments: Vec, statefulsets: Vec, namespace: String) -> Result<(), Box> { + pub fn check_all(deployments: Vec, statefulsets: Vec, namespace: String) -> Result<(), KubeError> { if deployments.is_empty() { info!("No deployments to check"); }else{ @@ -451,7 +483,7 @@ pub mod lib { match check_deployment(&namespace, &deploy.name()) { Ok(status) => status, Err(err) => { - return Err(Box::new(err)); + return Err(err); } } } @@ -464,7 +496,7 @@ pub mod lib { match check_statefulset(&namespace, &statefulset.name()) { Ok(status) => status, Err(err) => { - return Err(Box::new(err)); + return Err(err); } } } @@ -615,7 +647,7 @@ pub mod lib { } } #[tokio::main] - async fn check_image(registry: &str, repository: &str, tag: &str) -> Result<(), dkregistry::errors::Error> { + async fn check_image(registry: &str, repository: &str, tag: &str) -> Result<(), RegError> { let client = RegClient::configure() .insecure_registry(false) .registry(registry) @@ -649,7 +681,7 @@ pub mod lib { info!("Checking image: {} {} {}", registry, repository, tag); (registry, repository, tag) } - pub fn check_images(images: Vec) -> Result<(), dkregistry::errors::Error> { + pub fn check_images(images: Vec) -> Result<(), RegError> { for image in images { let (registry, repository, tag) = parse_image_name(&image); match check_image(®istry, &repository, &tag) { @@ -748,4 +780,72 @@ pub mod lib { Some(keys) } } + #[tokio::main] + async fn scale_deployment(namespace: &str, name: &str, replicas: i32) -> Result<(), kube::Error> { + let client = Client::try_default().await?; + let api: Api = Api::namespaced(client, &namespace); + let deployment = api.get(&name).await?; + let mut spec = match deployment.spec { + Some(spec) => spec, + None => panic!("Deployment without any spec!") + }; + spec.replicas = Some(replicas); + let patched_deployment = Deployment { + metadata: deployment.metadata, + spec: Some(spec), + status: None + }; + let patch = Patch::Merge(patched_deployment); + let patch_params = PatchParams::apply("octopod"); + api.patch(&name, &patch_params, &patch).await?; + Ok(()) + } + #[tokio::main] + async fn scale_statefulset(namespace: &str, name: &str, replicas: i32) -> Result<(), kube::Error> { + let client = Client::try_default().await?; + let api: Api = Api::namespaced(client, &namespace); + let statefulset = api.get(&name).await?; + let mut spec = match statefulset.spec { + Some(spec) => spec, + None => panic!("StatefulSet without any spec!") + }; + spec.replicas = Some(replicas); + let patched_statefulset = StatefulSet { + metadata: statefulset.metadata, + spec: Some(spec), + status: None + }; + let patch = Patch::Merge(patched_statefulset); + let patch_params = PatchParams::apply("octopod"); + api.patch(&name, &patch_params, &patch).await?; + Ok(()) + } + pub fn scale(deployments: Vec, statefulsets: Vec, namespace: String, replicas: i32) -> Result<(), Box> { + if deployments.is_empty() { + info!("No deployments to check"); + }else{ + for deploy in deployments { + match scale_deployment(&namespace, &deploy.name(), replicas) { + Ok(status) => status, + Err(err) => { + return Err(Box::new(err)); + } + } + } + } + + if statefulsets.is_empty() { + info!("No statefulsets to check"); + }else{ + for statefulset in statefulsets { + match scale_statefulset(&namespace, &statefulset.name(), replicas) { + Ok(status) => status, + Err(err) => { + return Err(Box::new(err)); + } + } + } + } + Ok(()) + } } diff --git a/helm-control-scripts/src/unarchive.rs b/helm-control-scripts/src/unarchive.rs index e94e6435..038ebc1b 100644 --- a/helm-control-scripts/src/unarchive.rs +++ b/helm-control-scripts/src/unarchive.rs @@ -3,5 +3,56 @@ use helm_control_scripts::lib::*; fn main() { let mut log_builder = Builder::from_default_env(); log_builder.target(Target::Stdout).filter(None, LevelFilter::Info).init(); - warn!("This is mockup!"); + info!("Utils version {}", env!("CARGO_PKG_VERSION")); + let envs = EnvVars::parse(); + info!("Env variables received {:?}", &envs); + let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); + let cli_opts = CliOpts::from_args(); + info!("Cli options received {:?}", &cli_opts); + let overrides = match overrides(&cli_opts) { + Some(inner) => inner, + None => vec![], + }; + let domain_name = domain_name(&cli_opts); + info!("Domain generated for deployment: {}", &domain_name); + let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); + let image_tag = match cli_opts.tag { + Some(tag) => tag, + None => { + error!("mandatory tag argument was not provided"); + panic!(); + } + }; + let namespace = String::from(&cli_opts.namespace); + let helm_template = HelmCmd { + name: envs.helm_bin, + mode: HelmMode::Template, + release_name: cli_opts.name, + release_domain: domain_name, + namespace: cli_opts.namespace, + deployment_parameters: deployment_parameters, + overrides: overrides, + default_values: default_values.default_overrides, + image_tag: image_tag + }; + info!("Generated Helm args: {:?}", &helm_template.args()); + match helm_template.run_stdout() { + Ok(status) => { + let (deployments, statefulsets, _ingresses, _old_ingresses) = match parse_to_k8s(status) { + Ok((deployments, statefulsets, ingresses, old_ingresses)) => (deployments, statefulsets, ingresses, old_ingresses), + Err(err) => panic!("{}", err) + }; + match scale(deployments, statefulsets, namespace, 1) { + Ok(_status) => info!("Success!"), + Err(status) => { + error!("Error checking statuses"); + panic!("{}", status); + } + } + } + Err(status) => { + error!("Error during helm execution"); + panic!("{:?}", status); + } + } } From 93f868687ceba2a0b72a2fcd4ebee573ad9bb4f6 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Thu, 23 Sep 2021 14:04:54 +0300 Subject: [PATCH 05/21] archive check script + README --- helm-control-scripts/README.md | 11 +++-- helm-control-scripts/src/archive_check.rs | 55 +++++++++++++++++------ 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/helm-control-scripts/README.md b/helm-control-scripts/README.md index 4ea89394..7544cfa6 100644 --- a/helm-control-scripts/README.md +++ b/helm-control-scripts/README.md @@ -7,13 +7,18 @@ You can get pre-built docker images from [hub.docker.com/r/typeable/octopod-gene - create – a script to install a helm release - update – a script to upgrade a helm release -- archive – a script to uninstall a helm release +- archive – a script to scale to zero all deployments and statefulsets +- archive_check – a script to check if all resources scaled to zero correctly +- unarchive - scale all deployments and statefulsets back (right now it will only scale to 1 and not to previous value) - info – a script to print comma-separated pairs which will be placed as links in the Octopod Web UI - cleanup – a script to delete dangling PVCs and letsencrypt certs left after `helm uninstall` command - check – a script to check if resources created by helm are healthy. Right now only deployments and statefuls sets are checked -- archive_check – a script to check if `helm uninstall` really deleted the release - init – a script to initialize something. This is the only script not run by Octopod, but as an init container. - tag_check – a script to check that images in helm release are present in registries before invoking create script +- app_overrides - a scirpt which returns a list of default app overrides wichh were passed in default_overrides parameter described below. +- deployment_overrides - a scirpt which returns a list of default deployment overrides from default paramaters option. +- deployment_keys - a scripts which returns a list of the possible deployment overrides keys user can use. +- app_keys - a scripts which returns a list of the possible app overrides keys. These are parsed from an output of the `helm show values` command. ### Parameters @@ -34,4 +39,4 @@ Also, several environment variables are used to parametrize the default behavior - chart_version – the version of a chart you want to install - chart_name – the name of a chart you want to install -These parameters, if set up in the `DEFAULTS` variable, will be passed to every deployment unless overridden in the "deployment overrides" section of an Octopod deployment configuration. \ No newline at end of file +These parameters, if set up in the `DEFAULTS` variable, will be passed to every deployment unless overridden in the "deployment overrides" section of an Octopod deployment configuration. diff --git a/helm-control-scripts/src/archive_check.rs b/helm-control-scripts/src/archive_check.rs index 8408a226..2b470e7d 100644 --- a/helm-control-scripts/src/archive_check.rs +++ b/helm-control-scripts/src/archive_check.rs @@ -8,24 +8,53 @@ fn main() { let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); + let cli_opts = CliOpts::from_args(); + info!("Cli options received {:?}", &cli_opts); + let overrides = match overrides(&cli_opts) { + Some(inner) => inner, + None => vec![], + }; + + let domain_name = domain_name(&cli_opts); + info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); - let helm_cmd = HelmCmd { + let image_tag = match cli_opts.tag { + Some(tag) => tag, + None => { + error!("mandatory tag argument was not provided"); + panic!(); + } + }; + let namespace = String::from(&cli_opts.namespace); + let helm_template = HelmCmd { name: envs.helm_bin, - mode: HelmMode::Status, + mode: HelmMode::Template, release_name: cli_opts.name, - release_domain: String::from(""), + release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, - overrides: vec![], - default_values: vec![], - image_tag: String::from("") + overrides: overrides, + default_values: default_values.default_overrides, + image_tag: image_tag }; - info!("Generated Helm args: {:?}", &helm_cmd.args()); - match helm_cmd.run() { - Ok(_status) => { - error!("Release is still present in cluster"); - panic!(); - }, - Err(_status) => info!("Success!"), + info!("Generated Helm args: {:?}", &helm_template.args()); + match helm_template.run_stdout() { + Ok(status) => { + let (deployments, statefulsets, _ingresses, _old_ingresses) = match parse_to_k8s(status) { + Ok((deployments, statefulsets, ingresses, old_ingresses)) => (deployments, statefulsets, ingresses, old_ingresses), + Err(err) => panic!("{}", err) + }; + match check_all(deployments, statefulsets, namespace) { + Ok(status) => { + error!("Deployment hasn't scaled down correctly"); + panic!("{:?}", status); + }, + Err(_status) => info!("Success!") + } + } + Err(status) => { + error!("Error during helm execution"); + panic!("{:?}", status); + } } } From bcb03aaed6706d8403011c1a78f2b6b37be60fd8 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Thu, 23 Sep 2021 14:06:06 +0300 Subject: [PATCH 06/21] fix timings values + up contolscripts version --- charts/octopod/values.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/charts/octopod/values.yaml b/charts/octopod/values.yaml index 142f103d..944232dd 100644 --- a/charts/octopod/values.yaml +++ b/charts/octopod/values.yaml @@ -79,8 +79,8 @@ octopod: projectName: Octopod deploymentNamespace: octopod-deployment baseDomain: "" - statusUpdateTimeout: 600 - archiveRetention: 1209600 + statusUpdateTimeout: "600" + archiveRetention: "1209600" migrations: enabled: true env: @@ -99,7 +99,7 @@ controlScripts: image: repository: typeable/octopod-helm-control-scripts pullPolicy: IfNotPresent - tag: 0.1.0 + tag: 0.2.0 sqitch: image: repository: typeable/sqitch From 706d5e7bae8dd803d4d2de013f211f716317af67 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Fri, 24 Sep 2021 18:07:49 +0300 Subject: [PATCH 07/21] Get rid of kubectl calls --- charts/octopod/values.yaml | 1 - helm-control-scripts/Cargo.toml | 2 +- helm-control-scripts/Dockerfile | 4 - helm-control-scripts/src/archive_check.rs | 1 - helm-control-scripts/src/cleanup.rs | 74 +++++++++++--- helm-control-scripts/src/lib.rs | 118 ++++++++++++---------- 6 files changed, 126 insertions(+), 74 deletions(-) diff --git a/charts/octopod/values.yaml b/charts/octopod/values.yaml index 944232dd..1e02b7e9 100644 --- a/charts/octopod/values.yaml +++ b/charts/octopod/values.yaml @@ -85,7 +85,6 @@ octopod: enabled: true env: HELM_BIN: "/utils/helm" - KUBECTL_BIN: "/utils/kubectl" DEFAULTS: | { "chart_name": "wordpress", diff --git a/helm-control-scripts/Cargo.toml b/helm-control-scripts/Cargo.toml index a9497051..ae9920b2 100644 --- a/helm-control-scripts/Cargo.toml +++ b/helm-control-scripts/Cargo.toml @@ -75,4 +75,4 @@ path = "src/app_keys.rs" [[bin]] name = "unarchive" -path = "src/unarchive.rs" \ No newline at end of file +path = "src/unarchive.rs" diff --git a/helm-control-scripts/Dockerfile b/helm-control-scripts/Dockerfile index aaf37f08..284042f8 100644 --- a/helm-control-scripts/Dockerfile +++ b/helm-control-scripts/Dockerfile @@ -1,17 +1,13 @@ FROM alpine:3 ARG HELM_BIN=/utils/helm -ARG KUBECTL_BIN=/utils/kubectl ENV HELM_BIN=$HELM_BIN -ENV KUBECTL_BIN=$KUBECTL_BIN ADD https://get.helm.sh/helm-v3.6.0-linux-amd64.tar.gz /tmp/helm.tar.gz -ADD https://dl.k8s.io/release/v1.21.0/bin/linux/amd64/kubectl $KUBECTL_BIN RUN tar -xf /tmp/helm.tar.gz -C /tmp &&\ mkdir -p /utils &&\ cp /tmp/linux-amd64/helm $HELM_BIN &&\ chmod +x $HELM_BIN &&\ - chmod +x $KUBECTL_BIN &&\ rm -r /tmp/* ADD target/x86_64-unknown-linux-musl/release/archive /utils/ diff --git a/helm-control-scripts/src/archive_check.rs b/helm-control-scripts/src/archive_check.rs index 2b470e7d..e73d2e99 100644 --- a/helm-control-scripts/src/archive_check.rs +++ b/helm-control-scripts/src/archive_check.rs @@ -4,7 +4,6 @@ fn main() { let mut log_builder = Builder::from_default_env(); log_builder.target(Target::Stdout).filter(None, LevelFilter::Info).init(); info!("Utils version {}", env!("CARGO_PKG_VERSION")); - let cli_opts = CliOpts::from_args(); let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); diff --git a/helm-control-scripts/src/cleanup.rs b/helm-control-scripts/src/cleanup.rs index 2dd78814..eb24be82 100644 --- a/helm-control-scripts/src/cleanup.rs +++ b/helm-control-scripts/src/cleanup.rs @@ -9,9 +9,24 @@ fn main() { let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); + let overrides = match overrides(&cli_opts) { + Some(inner) => inner, + None => vec![], + }; let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); + let namespace = String::from(&cli_opts.namespace); + let release_name = String::from(&cli_opts.name); + let domain_name = domain_name(&cli_opts); + let image_tag = match cli_opts.tag { + Some(tag) => tag, + None => { + error!("mandatory tag argument was not provided"); + panic!(); + } + }; + helm_init(&envs, &deployment_parameters); let helm_cmd = HelmCmd { - name: envs.helm_bin, + name: envs.helm_bin.clone(), mode: HelmMode::Uninstall, release_name: cli_opts.name.clone(), release_domain: String::from(""), @@ -21,20 +36,55 @@ fn main() { default_values: vec![], image_tag: String::from("") }; + let helm_template = HelmCmd { + name: envs.helm_bin, + mode: HelmMode::Template, + release_name: cli_opts.name, + release_domain: domain_name, + namespace: cli_opts.namespace, + deployment_parameters: deployment_parameters, + overrides: overrides, + default_values: default_values.default_overrides, + image_tag: image_tag + }; info!("Generated Helm args: {:?}", &helm_cmd.args()); - match helm_cmd.run() { - Ok(_status) => info!("Success!"), + match helm_template.run_stdout() { + Ok(status) => { + let (_deployments, _statefulsets, ingresses, old_ingresses) = match parse_to_k8s(status) { + Ok((deployments, statefulsets, ingresses, old_ingresses)) => (deployments, statefulsets, ingresses, old_ingresses), + Err(err) => panic!("{}", err) + }; + match helm_cmd.run() { + Ok(_status) => { + match delete_pvcs(&namespace, &format!("app.kubernetes.io/instance={}", release_name)) { + Ok(_status) => info!("All pvcs were deleted successfully"), + Err(err) => { + error!("Error deleting pvcs"); + panic!("{:?}", err); + } + } + match ingresses_to_secrets(ingresses, old_ingresses) { + Some(secrets) => { + for secret in secrets { + match delete_secret(&namespace, &secret) { + Ok(_status) => info!("Successfully deleted secret {}", &secret), + Err(error) => error!("Can't delete secret {}\n {}", &secret, error) + } + } + }, + None => info!("No secrets to delete") + } + info!("Success!"); + }, + Err(status) => { + error!("Error during helm uninstall"); + panic!("{:?}", status); + } + } + } Err(status) => { - error!("Error during helm execution"); + error!("Error during helm templating"); panic!("{:?}", status); } } - let kubectl_cmd = KubectlCmd { - name: envs.kubectl_bin, - release_name: cli_opts.name, - namespace: cli_opts.namespace, - deployment_parameters: deployment_parameters, - }; - kubectl_cmd.run(); - info!("Success!"); } diff --git a/helm-control-scripts/src/lib.rs b/helm-control-scripts/src/lib.rs index e02839ea..59a4f115 100644 --- a/helm-control-scripts/src/lib.rs +++ b/helm-control-scripts/src/lib.rs @@ -13,10 +13,11 @@ pub mod lib { pub use k8s_openapi::api::{ apps::v1::{Deployment, StatefulSet}, networking::v1::Ingress, - networking::v1beta1::Ingress as OldIngress + networking::v1beta1::Ingress as OldIngress, + core::v1::{PersistentVolumeClaim, Secret} }; pub use kube::{ - api::{Api, ListParams, ResourceExt, Patch, PatchParams}, + api::{Api, ListParams, ResourceExt, Patch, PatchParams, DeleteParams}, Client, }; pub use dkregistry::{ @@ -47,7 +48,6 @@ pub mod lib { #[derive(Deserialize, Debug)] pub struct EnvVars { pub helm_bin: String, - pub kubectl_bin: String, pub defaults: String, //json of DefaultValues #[serde(default)] pub helm_user: String, @@ -226,7 +226,6 @@ pub mod lib { args.push(String::from("status")); args.push(String::from(&self.release_name)); }, - _ => panic!("This helm mode is not expected"), } return args; @@ -281,57 +280,6 @@ pub mod lib { ShowValues, } - pub struct KubectlCmd { - pub name: String, - pub release_name: String, - pub namespace: String, - pub deployment_parameters: HelmDeploymentParameters, - } - - impl KubectlCmd { - pub fn args(&self, cmd_type: &str) -> Vec { - let mut args: Vec = Vec::new(); - let namespace = vec![String::from("-n"), String::from(&self.namespace),]; - let cert_secret_name = format!("{}-{}-tls", &self.release_name, &self.deployment_parameters.chart_name); - let label = vec![String::from("-l"), format!("app.kubernetes.io/instance={}", &self.release_name)]; - args.extend(namespace); - args.push(String::from("delete")); - match cmd_type { - "pvc" => { - args.push(String::from("pvc")); - args.extend(label); - return args; - }, - "cert" => { - args.push(String::from("secret")); - args.push(cert_secret_name); - return args; - }, - _ => unreachable!(), - } - } - pub fn run(&self) { - info!("Starting pvc cleanup"); - info!("kubectl args: {:?}", &self.args("pvc")); - let kubectl_pvc = Command::new(&self.name) - .args(&self.args("pvc")) - .output() - .expect("Failed to run"); - info!("kubectl stdout:\n {}", String::from_utf8(kubectl_pvc.stdout).unwrap()); - info!("kubectl stderr:\n {}", String::from_utf8(kubectl_pvc.stderr).unwrap()); - info!("Starting certificates cleanup"); - info!("kubectl args: {:?}", &self.args("pvc")); - let kubectl_cert = Command::new(&self.name) - .args(&self.args("cert")) - .output() - .expect("Failed to run"); - info!("kubectl stdout:\n {}", String::from_utf8(kubectl_cert.stdout).unwrap()); - info!("kubectl stderr:\n {}", String::from_utf8(kubectl_cert.stderr).unwrap()); - assert!(kubectl_pvc.status.success()); - assert!(kubectl_cert.status.success()); - } - } - fn check_value(value: String) -> Result { let split_value = value.split('=').collect::>(); if split_value.len() == 2 { @@ -848,4 +796,64 @@ pub mod lib { } Ok(()) } + pub fn ingresses_to_secrets(new_ingresses: Vec, old_ingresses: Vec) -> Option> { + let mut secrets: Vec = Vec::new(); + if new_ingresses.is_empty() { + for ingress in old_ingresses { + match &ingress.spec { + Some(spec) => { + if spec.tls.is_empty() { + continue + }else{ + for tls in &spec.tls { + match &tls.secret_name { + Some(secret) => secrets.push(String::from(secret)), + None => continue + } + } + } + }, + None => continue + } + } + }else{ + for ingress in new_ingresses { + match &ingress.spec { + Some(spec) => { + if spec.tls.is_empty() { + continue + }else{ + for tls in &spec.tls { + match &tls.secret_name { + Some(secret) => secrets.push(String::from(secret)), + None => continue + } + } + } + }, + None => continue + } + } + } + if secrets.is_empty() { + None + }else{ + Some(secrets) + } + } + #[tokio::main] + pub async fn delete_pvcs(namespace: &str, selector: &str) -> Result<(), kube::Error> { + let client = Client::try_default().await?; + let api: Api = Api::namespaced(client, &namespace); + let pvc_list = ListParams::default().labels(selector); + api.delete_collection(&DeleteParams::default(), &pvc_list).await?; + Ok(()) + } + #[tokio::main] + pub async fn delete_secret(namespace: &str, secret: &str) -> Result<(), kube::Error> { + let client = Client::try_default().await?; + let api: Api = Api::namespaced(client, &namespace); + api.delete(&secret, &DeleteParams::default()).await?; + Ok(()) + } } From a029023f5ada7d7ba3056c9dd2a0ba96ab8cbc59 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Fri, 24 Sep 2021 18:32:49 +0300 Subject: [PATCH 08/21] remove tags --- helm-control-scripts/src/app_keys.rs | 1 - helm-control-scripts/src/archive.rs | 8 -------- helm-control-scripts/src/archive_check.rs | 8 -------- helm-control-scripts/src/check.rs | 8 -------- helm-control-scripts/src/cleanup.rs | 9 --------- helm-control-scripts/src/create_update.rs | 8 -------- helm-control-scripts/src/info.rs | 8 -------- helm-control-scripts/src/lib.rs | 4 ---- helm-control-scripts/src/tag_check.rs | 8 -------- helm-control-scripts/src/unarchive.rs | 8 -------- 10 files changed, 70 deletions(-) diff --git a/helm-control-scripts/src/app_keys.rs b/helm-control-scripts/src/app_keys.rs index 62a3af80..a626fa53 100644 --- a/helm-control-scripts/src/app_keys.rs +++ b/helm-control-scripts/src/app_keys.rs @@ -21,7 +21,6 @@ fn main() { deployment_parameters: deployment_parameters, overrides: vec![], default_values: default_values.default_overrides, - image_tag: String::from("") }; info!("Generated Helm args: {:?}", &helm_values.args()); match helm_values.run_stdout() { diff --git a/helm-control-scripts/src/archive.rs b/helm-control-scripts/src/archive.rs index da50a305..c3c6fbb0 100644 --- a/helm-control-scripts/src/archive.rs +++ b/helm-control-scripts/src/archive.rs @@ -16,13 +16,6 @@ fn main() { let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); - let image_tag = match cli_opts.tag { - Some(tag) => tag, - None => { - error!("mandatory tag argument was not provided"); - panic!(); - } - }; let namespace = String::from(&cli_opts.namespace); let helm_template = HelmCmd { name: envs.helm_bin, @@ -33,7 +26,6 @@ fn main() { deployment_parameters: deployment_parameters, overrides: overrides, default_values: default_values.default_overrides, - image_tag: image_tag }; info!("Generated Helm args: {:?}", &helm_template.args()); match helm_template.run_stdout() { diff --git a/helm-control-scripts/src/archive_check.rs b/helm-control-scripts/src/archive_check.rs index e73d2e99..677c70db 100644 --- a/helm-control-scripts/src/archive_check.rs +++ b/helm-control-scripts/src/archive_check.rs @@ -17,13 +17,6 @@ fn main() { let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); - let image_tag = match cli_opts.tag { - Some(tag) => tag, - None => { - error!("mandatory tag argument was not provided"); - panic!(); - } - }; let namespace = String::from(&cli_opts.namespace); let helm_template = HelmCmd { name: envs.helm_bin, @@ -34,7 +27,6 @@ fn main() { deployment_parameters: deployment_parameters, overrides: overrides, default_values: default_values.default_overrides, - image_tag: image_tag }; info!("Generated Helm args: {:?}", &helm_template.args()); match helm_template.run_stdout() { diff --git a/helm-control-scripts/src/check.rs b/helm-control-scripts/src/check.rs index 86025a1e..c2626690 100644 --- a/helm-control-scripts/src/check.rs +++ b/helm-control-scripts/src/check.rs @@ -17,13 +17,6 @@ fn main() { let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); - let image_tag = match cli_opts.tag { - Some(tag) => tag, - None => { - error!("mandatory tag argument was not provided"); - panic!(); - } - }; let namespace = String::from(&cli_opts.namespace); let helm_template = HelmCmd { name: envs.helm_bin, @@ -34,7 +27,6 @@ fn main() { deployment_parameters: deployment_parameters, overrides: overrides, default_values: default_values.default_overrides, - image_tag: image_tag }; info!("Generated Helm args: {:?}", &helm_template.args()); match helm_template.run_stdout() { diff --git a/helm-control-scripts/src/cleanup.rs b/helm-control-scripts/src/cleanup.rs index eb24be82..36ad2bfc 100644 --- a/helm-control-scripts/src/cleanup.rs +++ b/helm-control-scripts/src/cleanup.rs @@ -17,13 +17,6 @@ fn main() { let namespace = String::from(&cli_opts.namespace); let release_name = String::from(&cli_opts.name); let domain_name = domain_name(&cli_opts); - let image_tag = match cli_opts.tag { - Some(tag) => tag, - None => { - error!("mandatory tag argument was not provided"); - panic!(); - } - }; helm_init(&envs, &deployment_parameters); let helm_cmd = HelmCmd { name: envs.helm_bin.clone(), @@ -34,7 +27,6 @@ fn main() { deployment_parameters: deployment_parameters.clone(), overrides: vec![], default_values: vec![], - image_tag: String::from("") }; let helm_template = HelmCmd { name: envs.helm_bin, @@ -45,7 +37,6 @@ fn main() { deployment_parameters: deployment_parameters, overrides: overrides, default_values: default_values.default_overrides, - image_tag: image_tag }; info!("Generated Helm args: {:?}", &helm_cmd.args()); match helm_template.run_stdout() { diff --git a/helm-control-scripts/src/create_update.rs b/helm-control-scripts/src/create_update.rs index 16e0eb03..328a18ce 100644 --- a/helm-control-scripts/src/create_update.rs +++ b/helm-control-scripts/src/create_update.rs @@ -16,13 +16,6 @@ fn main() { let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); - let image_tag = match cli_opts.tag { - Some(tag) => tag, - None => { - error!("mandatory tag argument was not provided"); - panic!(); - } - }; helm_init(&envs, &deployment_parameters); let helm_cmd = HelmCmd { name: envs.helm_bin, @@ -33,7 +26,6 @@ fn main() { deployment_parameters: deployment_parameters, overrides: overrides, default_values: default_values.default_overrides, - image_tag: image_tag, }; info!("Generated Helm args: {:?}", &helm_cmd.args()); match helm_cmd.run() { diff --git a/helm-control-scripts/src/info.rs b/helm-control-scripts/src/info.rs index 2df61ee9..2c72d56b 100644 --- a/helm-control-scripts/src/info.rs +++ b/helm-control-scripts/src/info.rs @@ -17,13 +17,6 @@ fn main() { let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); - let image_tag = match cli_opts.tag { - Some(tag) => tag, - None => { - error!("mandatory tag argument was not provided"); - panic!(); - } - }; let default_name = String::from(&cli_opts.name); helm_init(&envs, &deployment_parameters); let helm_template = HelmCmd { @@ -35,7 +28,6 @@ fn main() { deployment_parameters: deployment_parameters, overrides: overrides, default_values: default_values.default_overrides, - image_tag: image_tag }; match helm_template.run_stdout() { Ok(status) => { diff --git a/helm-control-scripts/src/lib.rs b/helm-control-scripts/src/lib.rs index 59a4f115..8974d246 100644 --- a/helm-control-scripts/src/lib.rs +++ b/helm-control-scripts/src/lib.rs @@ -165,7 +165,6 @@ pub mod lib { pub deployment_parameters: HelmDeploymentParameters, pub overrides: Vec, pub default_values: Vec, - pub image_tag: String, } impl HelmCmd { @@ -233,7 +232,6 @@ pub mod lib { fn set_flag_values(&self) -> Vec { let mut values = Vec::new(); let mut set_values = Vec::new(); - values.push(format!("image.tag={}", &self.image_tag)); values.push(format!("ingress.host={}", &self.release_domain)); values.extend(self.default_values.clone()); values.extend(self.overrides.clone()); @@ -649,7 +647,6 @@ pub mod lib { deployment_parameters: deployment_parameters.clone(), overrides: vec![], default_values: vec![], - image_tag: String::from(""), }; let helm_repo_update = HelmCmd { name: String::from(&envs.helm_bin), @@ -660,7 +657,6 @@ pub mod lib { deployment_parameters: deployment_parameters.clone(), overrides: vec![], default_values: vec![], - image_tag: String::from(""), }; match helm_repo_add.run() { Ok(_status) => info!("Repo add success!"), diff --git a/helm-control-scripts/src/tag_check.rs b/helm-control-scripts/src/tag_check.rs index 0d58e725..e6d04f87 100644 --- a/helm-control-scripts/src/tag_check.rs +++ b/helm-control-scripts/src/tag_check.rs @@ -17,13 +17,6 @@ fn main() { let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); - let image_tag = match cli_opts.tag { - Some(tag) => tag, - None => { - error!("mandatory tag argument was not provided"); - panic!(); - } - }; helm_init(&envs, &deployment_parameters); let helm_template = HelmCmd { name: envs.helm_bin, @@ -34,7 +27,6 @@ fn main() { deployment_parameters: deployment_parameters, overrides: overrides, default_values: default_values.default_overrides, - image_tag: image_tag }; match helm_template.run_stdout() { Ok(status) => { diff --git a/helm-control-scripts/src/unarchive.rs b/helm-control-scripts/src/unarchive.rs index 038ebc1b..d34a733d 100644 --- a/helm-control-scripts/src/unarchive.rs +++ b/helm-control-scripts/src/unarchive.rs @@ -16,13 +16,6 @@ fn main() { let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); - let image_tag = match cli_opts.tag { - Some(tag) => tag, - None => { - error!("mandatory tag argument was not provided"); - panic!(); - } - }; let namespace = String::from(&cli_opts.namespace); let helm_template = HelmCmd { name: envs.helm_bin, @@ -33,7 +26,6 @@ fn main() { deployment_parameters: deployment_parameters, overrides: overrides, default_values: default_values.default_overrides, - image_tag: image_tag }; info!("Generated Helm args: {:?}", &helm_template.args()); match helm_template.run_stdout() { From a8dfbb23cbed7c0dfa1f8c1b6a1d6fd8148807d2 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Fri, 24 Sep 2021 18:54:22 +0300 Subject: [PATCH 09/21] remove status mode --- helm-control-scripts/src/lib.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/helm-control-scripts/src/lib.rs b/helm-control-scripts/src/lib.rs index 8974d246..a78afd10 100644 --- a/helm-control-scripts/src/lib.rs +++ b/helm-control-scripts/src/lib.rs @@ -220,11 +220,6 @@ pub mod lib { args.push(String::from("repo")); args.push(String::from("update")); }, - HelmMode::Status => { - args.extend(namespace); - args.push(String::from("status")); - args.push(String::from(&self.release_name)); - }, } return args; @@ -274,7 +269,6 @@ pub mod lib { Template, RepoAdd, RepoUpdate, - Status, ShowValues, } From affbceac96bf53e09699425ce6546d8aad5c2b46 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Mon, 27 Sep 2021 10:58:52 +0300 Subject: [PATCH 10/21] rename tag_check to config_check --- charts/octopod/templates/octopod-configmap.yaml | 2 +- helm-control-scripts/Cargo.toml | 4 ++-- helm-control-scripts/Dockerfile | 2 +- helm-control-scripts/README.md | 2 +- helm-control-scripts/src/{tag_check.rs => config_check.rs} | 0 5 files changed, 5 insertions(+), 5 deletions(-) rename helm-control-scripts/src/{tag_check.rs => config_check.rs} (100%) diff --git a/charts/octopod/templates/octopod-configmap.yaml b/charts/octopod/templates/octopod-configmap.yaml index 2bd16e96..acdb6285 100644 --- a/charts/octopod/templates/octopod-configmap.yaml +++ b/charts/octopod/templates/octopod-configmap.yaml @@ -16,7 +16,7 @@ data: CHECKING_COMMAND: {{ printf "%s/check" (include "controlScriptsPath" .) | quote }} CLEANUP_COMMAND: {{ printf "%s/cleanup" (include "controlScriptsPath" .) | quote }} ARCHIVE_CHECKING_COMMAND: {{ printf "%s/archive_check" (include "controlScriptsPath" .) | quote }} - TAG_CHECKING_COMMAND: {{ printf "%s/tag_check" (include "controlScriptsPath" .) | quote }} + CONFIG_CHECKING_COMMAND: {{ printf "%s/config_check" (include "controlScriptsPath" .) | quote }} INFO_COMMAND: {{ printf "%s/info" (include "controlScriptsPath" .) | quote }} DEPLOYMENT_OVERRIDES_COMMAND: {{ printf "%s/deployment_overrides" (include "controlScriptsPath" .) | quote }} DEPLOYMENT_KEYS_COMMAND: {{ printf "%s/deployment_keys" (include "controlScriptsPath" .) | quote }} diff --git a/helm-control-scripts/Cargo.toml b/helm-control-scripts/Cargo.toml index ae9920b2..f17efedf 100644 --- a/helm-control-scripts/Cargo.toml +++ b/helm-control-scripts/Cargo.toml @@ -54,8 +54,8 @@ name = "init" path = "src/init.rs" [[bin]] -name = "tag_check" -path = "src/tag_check.rs" +name = "config_check" +path = "src/config_check.rs" [[bin]] name = "app_overrides" diff --git a/helm-control-scripts/Dockerfile b/helm-control-scripts/Dockerfile index 284042f8..b00f645b 100644 --- a/helm-control-scripts/Dockerfile +++ b/helm-control-scripts/Dockerfile @@ -18,7 +18,7 @@ ADD target/x86_64-unknown-linux-musl/release/create /utils/ ADD target/x86_64-unknown-linux-musl/release/info /utils/ ADD target/x86_64-unknown-linux-musl/release/update /utils/ ADD target/x86_64-unknown-linux-musl/release/init /utils/ -ADD target/x86_64-unknown-linux-musl/release/tag_check /utils/ +ADD target/x86_64-unknown-linux-musl/release/config_check /utils/ ADD target/x86_64-unknown-linux-musl/release/app_overrides /utils/ ADD target/x86_64-unknown-linux-musl/release/app_keys /utils/ ADD target/x86_64-unknown-linux-musl/release/deployment_overrides /utils/ diff --git a/helm-control-scripts/README.md b/helm-control-scripts/README.md index 7544cfa6..3705057d 100644 --- a/helm-control-scripts/README.md +++ b/helm-control-scripts/README.md @@ -14,7 +14,7 @@ You can get pre-built docker images from [hub.docker.com/r/typeable/octopod-gene - cleanup – a script to delete dangling PVCs and letsencrypt certs left after `helm uninstall` command - check – a script to check if resources created by helm are healthy. Right now only deployments and statefuls sets are checked - init – a script to initialize something. This is the only script not run by Octopod, but as an init container. -- tag_check – a script to check that images in helm release are present in registries before invoking create script +- config_check – a script to check that configuration (such as image tag name) passed to Octopod is correct. This script is invoked before deployment creation. - app_overrides - a scirpt which returns a list of default app overrides wichh were passed in default_overrides parameter described below. - deployment_overrides - a scirpt which returns a list of default deployment overrides from default paramaters option. - deployment_keys - a scripts which returns a list of the possible deployment overrides keys user can use. diff --git a/helm-control-scripts/src/tag_check.rs b/helm-control-scripts/src/config_check.rs similarity index 100% rename from helm-control-scripts/src/tag_check.rs rename to helm-control-scripts/src/config_check.rs From 43966e8b0b4d3c6abd7eb5c06cf0d9b4bc5bce02 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Mon, 27 Sep 2021 18:53:01 +0300 Subject: [PATCH 11/21] remove default values logic, optional name, additional checks at helm init --- helm-control-scripts/src/app_keys.rs | 9 +- helm-control-scripts/src/archive.rs | 14 ++- helm-control-scripts/src/archive_check.rs | 13 ++- helm-control-scripts/src/check.rs | 15 ++- helm-control-scripts/src/cleanup.rs | 18 ++-- helm-control-scripts/src/config_check.rs | 13 ++- helm-control-scripts/src/create_update.rs | 14 ++- helm-control-scripts/src/info.rs | 17 ++-- helm-control-scripts/src/lib.rs | 111 +++++++++++++++------- helm-control-scripts/src/unarchive.rs | 14 ++- 10 files changed, 162 insertions(+), 76 deletions(-) diff --git a/helm-control-scripts/src/app_keys.rs b/helm-control-scripts/src/app_keys.rs index a626fa53..8e24f3c8 100644 --- a/helm-control-scripts/src/app_keys.rs +++ b/helm-control-scripts/src/app_keys.rs @@ -6,11 +6,9 @@ fn main() { info!("Utils version {}", env!("CARGO_PKG_VERSION")); let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); - let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); - // let cli_opts = CliOpts::from_args(); - // info!("Cli options received {:?}", &cli_opts); - // let domain_name = domain_name(&cli_opts); - let deployment_parameters = HelmDeploymentParameters::new_env_only(&default_values, &envs); + let cli_opts = CliOpts::from_args(); + info!("Cli options received {:?}", &cli_opts); + let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); helm_init(&envs, &deployment_parameters); let helm_values = HelmCmd { name: envs.helm_bin, @@ -20,7 +18,6 @@ fn main() { namespace: String::from(""), deployment_parameters: deployment_parameters, overrides: vec![], - default_values: default_values.default_overrides, }; info!("Generated Helm args: {:?}", &helm_values.args()); match helm_values.run_stdout() { diff --git a/helm-control-scripts/src/archive.rs b/helm-control-scripts/src/archive.rs index c3c6fbb0..21c7ee14 100644 --- a/helm-control-scripts/src/archive.rs +++ b/helm-control-scripts/src/archive.rs @@ -6,26 +6,32 @@ fn main() { info!("Utils version {}", env!("CARGO_PKG_VERSION")); let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); - let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); let overrides = match overrides(&cli_opts) { Some(inner) => inner, None => vec![], }; + let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); - let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); + let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); let namespace = String::from(&cli_opts.namespace); + let release_name = match cli_opts.name { + Some(name) => name, + None => { + error!("mandatory name argument was not provided"); + panic!(); + } + }; let helm_template = HelmCmd { name: envs.helm_bin, mode: HelmMode::Template, - release_name: cli_opts.name, + release_name: release_name, release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, - default_values: default_values.default_overrides, }; info!("Generated Helm args: {:?}", &helm_template.args()); match helm_template.run_stdout() { diff --git a/helm-control-scripts/src/archive_check.rs b/helm-control-scripts/src/archive_check.rs index 677c70db..4e024008 100644 --- a/helm-control-scripts/src/archive_check.rs +++ b/helm-control-scripts/src/archive_check.rs @@ -6,7 +6,6 @@ fn main() { info!("Utils version {}", env!("CARGO_PKG_VERSION")); let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); - let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); let overrides = match overrides(&cli_opts) { @@ -16,17 +15,23 @@ fn main() { let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); - let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); + let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); let namespace = String::from(&cli_opts.namespace); + let release_name = match cli_opts.name { + Some(name) => name, + None => { + error!("mandatory name argument was not provided"); + panic!(); + } + }; let helm_template = HelmCmd { name: envs.helm_bin, mode: HelmMode::Template, - release_name: cli_opts.name, + release_name: release_name, release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, - default_values: default_values.default_overrides, }; info!("Generated Helm args: {:?}", &helm_template.args()); match helm_template.run_stdout() { diff --git a/helm-control-scripts/src/check.rs b/helm-control-scripts/src/check.rs index c2626690..2739fb57 100644 --- a/helm-control-scripts/src/check.rs +++ b/helm-control-scripts/src/check.rs @@ -6,27 +6,32 @@ fn main() { info!("Utils version {}", env!("CARGO_PKG_VERSION")); let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); - let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); let overrides = match overrides(&cli_opts) { Some(inner) => inner, None => vec![], }; - + let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); - let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); + let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); let namespace = String::from(&cli_opts.namespace); + let release_name = match cli_opts.name { + Some(name) => name, + None => { + error!("mandatory name argument was not provided"); + panic!(); + } + }; let helm_template = HelmCmd { name: envs.helm_bin, mode: HelmMode::Template, - release_name: cli_opts.name, + release_name: release_name, release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, - default_values: default_values.default_overrides, }; info!("Generated Helm args: {:?}", &helm_template.args()); match helm_template.run_stdout() { diff --git a/helm-control-scripts/src/cleanup.rs b/helm-control-scripts/src/cleanup.rs index 36ad2bfc..854477ac 100644 --- a/helm-control-scripts/src/cleanup.rs +++ b/helm-control-scripts/src/cleanup.rs @@ -6,37 +6,41 @@ fn main() { info!("Utils version {}", env!("CARGO_PKG_VERSION")); let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); - let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); let overrides = match overrides(&cli_opts) { Some(inner) => inner, None => vec![], }; - let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); + + let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); let namespace = String::from(&cli_opts.namespace); - let release_name = String::from(&cli_opts.name); let domain_name = domain_name(&cli_opts); + let release_name = match cli_opts.name { + Some(name) => name, + None => { + error!("mandatory name argument was not provided"); + panic!(); + } + }; helm_init(&envs, &deployment_parameters); let helm_cmd = HelmCmd { name: envs.helm_bin.clone(), mode: HelmMode::Uninstall, - release_name: cli_opts.name.clone(), + release_name: release_name.clone(), release_domain: String::from(""), namespace: cli_opts.namespace.clone(), deployment_parameters: deployment_parameters.clone(), overrides: vec![], - default_values: vec![], }; let helm_template = HelmCmd { name: envs.helm_bin, mode: HelmMode::Template, - release_name: cli_opts.name, + release_name: release_name.clone(), release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, - default_values: default_values.default_overrides, }; info!("Generated Helm args: {:?}", &helm_cmd.args()); match helm_template.run_stdout() { diff --git a/helm-control-scripts/src/config_check.rs b/helm-control-scripts/src/config_check.rs index e6d04f87..1440e5a4 100644 --- a/helm-control-scripts/src/config_check.rs +++ b/helm-control-scripts/src/config_check.rs @@ -6,7 +6,6 @@ fn main() { info!("Utils version {}", env!("CARGO_PKG_VERSION")); let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); - let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); let overrides = match overrides(&cli_opts) { @@ -16,17 +15,23 @@ fn main() { let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); - let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); + let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); helm_init(&envs, &deployment_parameters); + let release_name = match cli_opts.name { + Some(name) => name, + None => { + error!("mandatory name argument was not provided"); + panic!(); + } + }; let helm_template = HelmCmd { name: envs.helm_bin, mode: HelmMode::Template, - release_name: cli_opts.name, + release_name: release_name, release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, - default_values: default_values.default_overrides, }; match helm_template.run_stdout() { Ok(status) => { diff --git a/helm-control-scripts/src/create_update.rs b/helm-control-scripts/src/create_update.rs index 328a18ce..899b957d 100644 --- a/helm-control-scripts/src/create_update.rs +++ b/helm-control-scripts/src/create_update.rs @@ -6,26 +6,32 @@ fn main() { info!("Utils version {}", env!("CARGO_PKG_VERSION")); let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); - let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); let overrides = match overrides(&cli_opts) { Some(inner) => inner, None => vec![], }; + let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); - let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); + let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); helm_init(&envs, &deployment_parameters); + let release_name = match cli_opts.name { + Some(name) => name, + None => { + error!("mandatory name argument was not provided"); + panic!(); + } + }; let helm_cmd = HelmCmd { name: envs.helm_bin, mode: HelmMode::UpgradeInstall, - release_name: cli_opts.name, + release_name: release_name, release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, - default_values: default_values.default_overrides, }; info!("Generated Helm args: {:?}", &helm_cmd.args()); match helm_cmd.run() { diff --git a/helm-control-scripts/src/info.rs b/helm-control-scripts/src/info.rs index 2c72d56b..4523f513 100644 --- a/helm-control-scripts/src/info.rs +++ b/helm-control-scripts/src/info.rs @@ -6,28 +6,33 @@ fn main() { info!("Utils version {}", env!("CARGO_PKG_VERSION")); let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); - let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); let overrides = match overrides(&cli_opts) { Some(inner) => inner, None => vec![], }; - + let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); - let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); - let default_name = String::from(&cli_opts.name); + let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); helm_init(&envs, &deployment_parameters); + let release_name = match cli_opts.name { + Some(name) => name, + None => { + error!("mandatory name argument was not provided"); + panic!(); + } + }; + let default_name = String::from(&release_name); let helm_template = HelmCmd { name: envs.helm_bin, mode: HelmMode::Template, - release_name: cli_opts.name, + release_name: release_name, release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, - default_values: default_values.default_overrides, }; match helm_template.run_stdout() { Ok(status) => { diff --git a/helm-control-scripts/src/lib.rs b/helm-control-scripts/src/lib.rs index a78afd10..3e19ac68 100644 --- a/helm-control-scripts/src/lib.rs +++ b/helm-control-scripts/src/lib.rs @@ -36,7 +36,7 @@ pub mod lib { #[structopt(long)] pub namespace: String, #[structopt(long)] - pub name: String, + pub name: Option, #[structopt(long)] pub tag: Option, #[structopt(long)] @@ -69,6 +69,12 @@ pub mod lib { } } + #[derive(Debug, Serialize, Deserialize)] + struct HelmRepo { + name: String, + url: String + } + #[derive(Deserialize, Debug)] pub struct DefaultValues { pub default_overrides: Vec, @@ -105,11 +111,13 @@ pub mod lib { String::from("chart_repo_name"), String::from("chart_version"), String::from("chart_name"), + String::from("chart_repo_user"), + String::from("chart_repo_pass"), ] } } - #[derive(Debug, Clone)] + #[derive(Debug, Clone, Default)] pub struct HelmDeploymentParameters { pub chart_repo_url: String, pub chart_repo_name: String, @@ -120,30 +128,23 @@ pub mod lib { } impl HelmDeploymentParameters { - pub fn new(cli_opts: &CliOpts, default_values: &DefaultValues, envs: &EnvVars) -> Self { - let mut default_parameters = Self { - chart_repo_url: default_values.chart_repo_url.clone(), - chart_repo_name: default_values.chart_repo_name.clone(), - chart_repo_user: envs.helm_user.clone(), - chart_repo_pass: envs.helm_pass.clone(), - chart_version: default_values.chart_version.clone(), - chart_name: default_values.chart_name.clone(), - }; - if cli_opts.deployment_override.is_empty() { - return default_parameters; - } else { - for deployment_override in cli_opts.deployment_override.clone().into_iter() { - let split_override = deployment_override.split('=').collect::>(); - match split_override.first().unwrap().as_ref() { - "chart_repo_url" => default_parameters.chart_repo_url = split_override.last().unwrap().to_string(), - "chart_repo_name" => default_parameters.chart_repo_name = split_override.last().unwrap().to_string(), - "chart_version" => default_parameters.chart_version = split_override.last().unwrap().to_string(), - "chart_name" => default_parameters.chart_name = split_override.last().unwrap().to_string(), - _ => continue, - } + pub fn new(cli_opts: &CliOpts, envs: &EnvVars) -> Self { + let mut deployment_parameters = Self::default(); + deployment_parameters.chart_repo_user = envs.helm_user.clone(); + deployment_parameters.chart_repo_pass = envs.helm_pass.clone(); + for deployment_override in cli_opts.deployment_override.clone().into_iter() { + let split_override = deployment_override.split('=').collect::>(); + match split_override.first().unwrap().as_ref() { + "chart_repo_url" => deployment_parameters.chart_repo_url = split_override.last().unwrap().to_string(), + "chart_repo_name" => deployment_parameters.chart_repo_name = split_override.last().unwrap().to_string(), + "chart_version" => deployment_parameters.chart_version = split_override.last().unwrap().to_string(), + "chart_name" => deployment_parameters.chart_name = split_override.last().unwrap().to_string(), + "chart_repo_user" => deployment_parameters.chart_repo_user = split_override.last().unwrap().to_string(), + "chart_repo_pass" => deployment_parameters.chart_repo_pass = split_override.last().unwrap().to_string(), + _ => continue, } - return default_parameters; } + return deployment_parameters; } pub fn new_env_only(default_values: &DefaultValues, envs: &EnvVars) -> Self { Self { @@ -164,7 +165,6 @@ pub mod lib { pub namespace: String, pub deployment_parameters: HelmDeploymentParameters, pub overrides: Vec, - pub default_values: Vec, } impl HelmCmd { @@ -220,6 +220,12 @@ pub mod lib { args.push(String::from("repo")); args.push(String::from("update")); }, + HelmMode::RepoList => { + args.push(String::from("repo")); + args.push(String::from("list")); + args.push(String::from("-o")); + args.push(String::from("json")); + }, } return args; @@ -228,7 +234,6 @@ pub mod lib { let mut values = Vec::new(); let mut set_values = Vec::new(); values.push(format!("ingress.host={}", &self.release_domain)); - values.extend(self.default_values.clone()); values.extend(self.overrides.clone()); for value in values.into_iter() { set_values.push(String::from("--set")); @@ -269,6 +274,7 @@ pub mod lib { Template, RepoAdd, RepoUpdate, + RepoList, ShowValues, } @@ -294,7 +300,12 @@ pub mod lib { } } pub fn domain_name(cli_opts: &CliOpts) -> String { - format!("{}.{}", &cli_opts.name, &cli_opts.base_domain) + match &cli_opts.name { + Some(name) => { + return format!("{}.{}", name, &cli_opts.base_domain); + }, + None => panic!("No name argument provided") + } } pub fn parse_to_k8s(yaml: String) -> Result<(Vec, Vec, Vec, Vec), serde_yaml::Error> { let docs = YamlLoader::load_from_str(&yaml).unwrap(); @@ -640,7 +651,6 @@ pub mod lib { namespace: String::from(""), deployment_parameters: deployment_parameters.clone(), overrides: vec![], - default_values: vec![], }; let helm_repo_update = HelmCmd { name: String::from(&envs.helm_bin), @@ -650,7 +660,6 @@ pub mod lib { namespace: String::from(""), deployment_parameters: deployment_parameters.clone(), overrides: vec![], - default_values: vec![], }; match helm_repo_add.run() { Ok(_status) => info!("Repo add success!"), @@ -672,16 +681,54 @@ pub mod lib { Some(enabled) => { if *enabled { info!("Skipping helm initialization, it must be initialied on init"); + } else { + if helm_repo_exists(&envs, &deployment_parameters) { + info!("Skipping helm initialization, because requested repo was already added"); + } else { + info!("Starting helm initialization"); + helm_repo_add_update(&envs, &deployment_parameters); + } + } + }, + None => { + if helm_repo_exists(&envs, &deployment_parameters) { + info!("Skipping helm initialization, because requested repo was already added"); } else { info!("Starting helm initialization"); helm_repo_add_update(&envs, &deployment_parameters); } + } + } + } + + fn helm_repo_exists(envs: &EnvVars, deployment_parameters: &HelmDeploymentParameters) -> bool { + let chart_repo_url = String::from(&deployment_parameters.chart_repo_url); + let chart_repo_name = String::from(&deployment_parameters.chart_repo_name); + let helm_repo_list = HelmCmd { + name: String::from(&envs.helm_bin), + mode: HelmMode::RepoList, + release_name: String::from(""), + release_domain: String::from(""), + namespace: String::from(""), + deployment_parameters: deployment_parameters.clone(), + overrides: vec![], + }; + match helm_repo_list.run_stdout() { + Ok(list) => { + let helm_repos: Vec = serde_json::from_str(&list).unwrap(); + for repo in helm_repos { + if repo.name == chart_repo_name && repo.url == chart_repo_url { + return true; + } else { + continue + } + } }, - None => { - info!("Starting helm initialization"); - helm_repo_add_update(&envs, &deployment_parameters); + Err(_err) => { + return false; } } + false } pub fn helm_values_as_keys(yaml: String) -> Option> { let mut keys: Vec = Vec::new(); diff --git a/helm-control-scripts/src/unarchive.rs b/helm-control-scripts/src/unarchive.rs index d34a733d..56e4a6dc 100644 --- a/helm-control-scripts/src/unarchive.rs +++ b/helm-control-scripts/src/unarchive.rs @@ -6,26 +6,32 @@ fn main() { info!("Utils version {}", env!("CARGO_PKG_VERSION")); let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); - let default_values: DefaultValues = serde_json::from_str(&envs.defaults).unwrap(); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); let overrides = match overrides(&cli_opts) { Some(inner) => inner, None => vec![], }; + let domain_name = domain_name(&cli_opts); info!("Domain generated for deployment: {}", &domain_name); - let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs); + let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); let namespace = String::from(&cli_opts.namespace); + let release_name = match cli_opts.name { + Some(name) => name, + None => { + error!("mandatory name argument was not provided"); + panic!(); + } + }; let helm_template = HelmCmd { name: envs.helm_bin, mode: HelmMode::Template, - release_name: cli_opts.name, + release_name: release_name, release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, - default_values: default_values.default_overrides, }; info!("Generated Helm args: {:?}", &helm_template.args()); match helm_template.run_stdout() { From 48be60d8164e06abbad0fb01340572e7537fb905 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Mon, 27 Sep 2021 18:55:50 +0300 Subject: [PATCH 12/21] bump chart and app version --- charts/octopod/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/charts/octopod/Chart.yaml b/charts/octopod/Chart.yaml index 1c609cde..12087f76 100644 --- a/charts/octopod/Chart.yaml +++ b/charts/octopod/Chart.yaml @@ -2,8 +2,8 @@ apiVersion: v2 name: octopod description: An opensource self-hosted solution for managing multiple deployments in a Kubernetes cluster. type: application -version: 0.5.1 -appVersion: 1.3.1 +version: 0.6.0 +appVersion: 1.4 keywords: - kubernetes - octopod From dfe7a4390fd028795f5c91f4333cb765f3246524 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Tue, 28 Sep 2021 11:32:50 +0300 Subject: [PATCH 13/21] add keys sorting --- helm-control-scripts/src/lib.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/helm-control-scripts/src/lib.rs b/helm-control-scripts/src/lib.rs index 3e19ac68..2e63b011 100644 --- a/helm-control-scripts/src/lib.rs +++ b/helm-control-scripts/src/lib.rs @@ -106,14 +106,16 @@ pub mod lib { ] } pub fn deployment_keys(&self) -> Vec { - vec![ + let mut keys: Vec = vec![ String::from("chart_repo_url"), String::from("chart_repo_name"), String::from("chart_version"), String::from("chart_name"), String::from("chart_repo_user"), String::from("chart_repo_pass"), - ] + ]; + keys.sort(); + keys } } @@ -762,6 +764,7 @@ pub mod lib { if keys.is_empty() { None }else{ + keys.sort(); Some(keys) } } From 20a1888828e0e9540b189135c086f394d9b58d6f Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Tue, 28 Sep 2021 12:48:39 +0300 Subject: [PATCH 14/21] ingress.host to ingress.hostname hardcode --- helm-control-scripts/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helm-control-scripts/src/lib.rs b/helm-control-scripts/src/lib.rs index 2e63b011..df1dc252 100644 --- a/helm-control-scripts/src/lib.rs +++ b/helm-control-scripts/src/lib.rs @@ -235,7 +235,7 @@ pub mod lib { fn set_flag_values(&self) -> Vec { let mut values = Vec::new(); let mut set_values = Vec::new(); - values.push(format!("ingress.host={}", &self.release_domain)); + values.push(format!("ingress.hostname={}", &self.release_domain)); values.extend(self.overrides.clone()); for value in values.into_iter() { set_values.push(String::from("--set")); From bba366e740b269a740aaba4ccc6f3d0504360beb Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Tue, 28 Sep 2021 12:48:56 +0300 Subject: [PATCH 15/21] add default override for wordpress --- charts/octopod/values.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/charts/octopod/values.yaml b/charts/octopod/values.yaml index 1e02b7e9..b99e7270 100644 --- a/charts/octopod/values.yaml +++ b/charts/octopod/values.yaml @@ -91,7 +91,9 @@ octopod: "chart_repo_name": "bitnami", "chart_repo_url": "https://charts.bitnami.com/bitnami", "chart_version": "12.0.0", - "default_overrides": [] + "default_overrides": [ + "ingress.enabled=true" + ] } vaultEnv: {} controlScripts: From f491fcc484c0e4619c86b4bc8a3e363e5a209f48 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Tue, 28 Sep 2021 15:45:48 +0300 Subject: [PATCH 16/21] parametrize ingress host key --- helm-control-scripts/README.md | 3 ++- helm-control-scripts/src/app_keys.rs | 1 - helm-control-scripts/src/archive.rs | 5 +---- helm-control-scripts/src/archive_check.rs | 5 +---- helm-control-scripts/src/check.rs | 5 +---- helm-control-scripts/src/cleanup.rs | 5 +---- helm-control-scripts/src/config_check.rs | 5 +---- helm-control-scripts/src/create_update.rs | 5 +---- helm-control-scripts/src/info.rs | 5 +---- helm-control-scripts/src/lib.rs | 15 ++++++++------- helm-control-scripts/src/unarchive.rs | 5 +---- 11 files changed, 18 insertions(+), 41 deletions(-) diff --git a/helm-control-scripts/README.md b/helm-control-scripts/README.md index 3705057d..ecd63c57 100644 --- a/helm-control-scripts/README.md +++ b/helm-control-scripts/README.md @@ -27,10 +27,11 @@ All scipts accept common parametes which are passed by Octopod when it invokes t Also, several environment variables are used to parametrize the default behavior: - HELM_BIN – the path to the `helm` executable -- KUBECTL_BIN – the path to the `kubectl` executable - HELM_USER – the (optional) user for a private helm registry - HELM_PASS – the (optional) password for a private helm registry - DEFAUTLS – the json with the default parameters (described below) +- HELM_ON_INIT_ONLY - run helm add and update repository as a part of init script execution only. Otherwise `helm repo add` and `helm repo update` will be executed every time before any other helm command. +- INGRESS_HOST_KEY - key name which will be populated with domain name generated for the Octopod deployment. Defaults to `ingress.hostname`. #### Default parameters - default_overrides – an array with key-value pairs which will be passed as a `--set` flags for helm for each deployment diff --git a/helm-control-scripts/src/app_keys.rs b/helm-control-scripts/src/app_keys.rs index 8e24f3c8..f5a376e1 100644 --- a/helm-control-scripts/src/app_keys.rs +++ b/helm-control-scripts/src/app_keys.rs @@ -14,7 +14,6 @@ fn main() { name: envs.helm_bin, mode: HelmMode::ShowValues, release_name: String::from(""), - release_domain: String::from(""), namespace: String::from(""), deployment_parameters: deployment_parameters, overrides: vec![], diff --git a/helm-control-scripts/src/archive.rs b/helm-control-scripts/src/archive.rs index 21c7ee14..08a33a4f 100644 --- a/helm-control-scripts/src/archive.rs +++ b/helm-control-scripts/src/archive.rs @@ -8,13 +8,11 @@ fn main() { info!("Env variables received {:?}", &envs); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); - let overrides = match overrides(&cli_opts) { + let overrides = match overrides(&cli_opts, &envs) { Some(inner) => inner, None => vec![], }; - let domain_name = domain_name(&cli_opts); - info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); let namespace = String::from(&cli_opts.namespace); let release_name = match cli_opts.name { @@ -28,7 +26,6 @@ fn main() { name: envs.helm_bin, mode: HelmMode::Template, release_name: release_name, - release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, diff --git a/helm-control-scripts/src/archive_check.rs b/helm-control-scripts/src/archive_check.rs index 4e024008..eb5d05ec 100644 --- a/helm-control-scripts/src/archive_check.rs +++ b/helm-control-scripts/src/archive_check.rs @@ -8,13 +8,11 @@ fn main() { info!("Env variables received {:?}", &envs); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); - let overrides = match overrides(&cli_opts) { + let overrides = match overrides(&cli_opts, &envs) { Some(inner) => inner, None => vec![], }; - let domain_name = domain_name(&cli_opts); - info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); let namespace = String::from(&cli_opts.namespace); let release_name = match cli_opts.name { @@ -28,7 +26,6 @@ fn main() { name: envs.helm_bin, mode: HelmMode::Template, release_name: release_name, - release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, diff --git a/helm-control-scripts/src/check.rs b/helm-control-scripts/src/check.rs index 2739fb57..077339a4 100644 --- a/helm-control-scripts/src/check.rs +++ b/helm-control-scripts/src/check.rs @@ -8,13 +8,11 @@ fn main() { info!("Env variables received {:?}", &envs); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); - let overrides = match overrides(&cli_opts) { + let overrides = match overrides(&cli_opts, &envs) { Some(inner) => inner, None => vec![], }; - let domain_name = domain_name(&cli_opts); - info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); let namespace = String::from(&cli_opts.namespace); let release_name = match cli_opts.name { @@ -28,7 +26,6 @@ fn main() { name: envs.helm_bin, mode: HelmMode::Template, release_name: release_name, - release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, diff --git a/helm-control-scripts/src/cleanup.rs b/helm-control-scripts/src/cleanup.rs index 854477ac..07e2123f 100644 --- a/helm-control-scripts/src/cleanup.rs +++ b/helm-control-scripts/src/cleanup.rs @@ -8,14 +8,13 @@ fn main() { info!("Env variables received {:?}", &envs); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); - let overrides = match overrides(&cli_opts) { + let overrides = match overrides(&cli_opts, &envs) { Some(inner) => inner, None => vec![], }; let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); let namespace = String::from(&cli_opts.namespace); - let domain_name = domain_name(&cli_opts); let release_name = match cli_opts.name { Some(name) => name, None => { @@ -28,7 +27,6 @@ fn main() { name: envs.helm_bin.clone(), mode: HelmMode::Uninstall, release_name: release_name.clone(), - release_domain: String::from(""), namespace: cli_opts.namespace.clone(), deployment_parameters: deployment_parameters.clone(), overrides: vec![], @@ -37,7 +35,6 @@ fn main() { name: envs.helm_bin, mode: HelmMode::Template, release_name: release_name.clone(), - release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, diff --git a/helm-control-scripts/src/config_check.rs b/helm-control-scripts/src/config_check.rs index 1440e5a4..cdcc5f9c 100644 --- a/helm-control-scripts/src/config_check.rs +++ b/helm-control-scripts/src/config_check.rs @@ -8,13 +8,11 @@ fn main() { info!("Env variables received {:?}", &envs); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); - let overrides = match overrides(&cli_opts) { + let overrides = match overrides(&cli_opts, &envs) { Some(inner) => inner, None => vec![], }; - let domain_name = domain_name(&cli_opts); - info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); helm_init(&envs, &deployment_parameters); let release_name = match cli_opts.name { @@ -28,7 +26,6 @@ fn main() { name: envs.helm_bin, mode: HelmMode::Template, release_name: release_name, - release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, diff --git a/helm-control-scripts/src/create_update.rs b/helm-control-scripts/src/create_update.rs index 899b957d..b95ecf2e 100644 --- a/helm-control-scripts/src/create_update.rs +++ b/helm-control-scripts/src/create_update.rs @@ -8,13 +8,11 @@ fn main() { info!("Env variables received {:?}", &envs); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); - let overrides = match overrides(&cli_opts) { + let overrides = match overrides(&cli_opts, &envs) { Some(inner) => inner, None => vec![], }; - let domain_name = domain_name(&cli_opts); - info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); helm_init(&envs, &deployment_parameters); let release_name = match cli_opts.name { @@ -28,7 +26,6 @@ fn main() { name: envs.helm_bin, mode: HelmMode::UpgradeInstall, release_name: release_name, - release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, diff --git a/helm-control-scripts/src/info.rs b/helm-control-scripts/src/info.rs index 4523f513..32006d49 100644 --- a/helm-control-scripts/src/info.rs +++ b/helm-control-scripts/src/info.rs @@ -8,13 +8,11 @@ fn main() { info!("Env variables received {:?}", &envs); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); - let overrides = match overrides(&cli_opts) { + let overrides = match overrides(&cli_opts, &envs) { Some(inner) => inner, None => vec![], }; - let domain_name = domain_name(&cli_opts); - info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); helm_init(&envs, &deployment_parameters); let release_name = match cli_opts.name { @@ -29,7 +27,6 @@ fn main() { name: envs.helm_bin, mode: HelmMode::Template, release_name: release_name, - release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, diff --git a/helm-control-scripts/src/lib.rs b/helm-control-scripts/src/lib.rs index df1dc252..c33f0d27 100644 --- a/helm-control-scripts/src/lib.rs +++ b/helm-control-scripts/src/lib.rs @@ -54,6 +54,7 @@ pub mod lib { #[serde(default)] pub helm_pass: String, pub helm_on_init_only: Option, + pub ingress_host_key: Option, } impl EnvVars { @@ -163,7 +164,6 @@ pub mod lib { pub name: String, pub mode: HelmMode, pub release_name: String, - pub release_domain: String, pub namespace: String, pub deployment_parameters: HelmDeploymentParameters, pub overrides: Vec, @@ -235,7 +235,6 @@ pub mod lib { fn set_flag_values(&self) -> Vec { let mut values = Vec::new(); let mut set_values = Vec::new(); - values.push(format!("ingress.hostname={}", &self.release_domain)); values.extend(self.overrides.clone()); for value in values.into_iter() { set_values.push(String::from("--set")); @@ -288,9 +287,14 @@ pub mod lib { Err(format!("Override value {} is malformed", value)) } } - pub fn overrides(cli_opts: &CliOpts) -> Option> { + pub fn overrides(cli_opts: &CliOpts, envs: &EnvVars) -> Option> { let mut overrides_opts = Vec::new(); overrides_opts.extend(&cli_opts.app_env_override); + let ingress_override = match &envs.ingress_host_key { + Some(key) => format!("{}={}", &key, domain_name(&cli_opts)), + None => format!("ingress.hostname={}", domain_name(&cli_opts)), + }; + overrides_opts.push(&ingress_override); if overrides_opts.is_empty() { None } else { @@ -301,7 +305,7 @@ pub mod lib { ) } } - pub fn domain_name(cli_opts: &CliOpts) -> String { + fn domain_name(cli_opts: &CliOpts) -> String { match &cli_opts.name { Some(name) => { return format!("{}.{}", name, &cli_opts.base_domain); @@ -649,7 +653,6 @@ pub mod lib { name: String::from(&envs.helm_bin), mode: HelmMode::RepoAdd, release_name: String::from(""), - release_domain: String::from(""), namespace: String::from(""), deployment_parameters: deployment_parameters.clone(), overrides: vec![], @@ -658,7 +661,6 @@ pub mod lib { name: String::from(&envs.helm_bin), mode: HelmMode::RepoUpdate, release_name: String::from(""), - release_domain: String::from(""), namespace: String::from(""), deployment_parameters: deployment_parameters.clone(), overrides: vec![], @@ -710,7 +712,6 @@ pub mod lib { name: String::from(&envs.helm_bin), mode: HelmMode::RepoList, release_name: String::from(""), - release_domain: String::from(""), namespace: String::from(""), deployment_parameters: deployment_parameters.clone(), overrides: vec![], diff --git a/helm-control-scripts/src/unarchive.rs b/helm-control-scripts/src/unarchive.rs index 56e4a6dc..799f0dd6 100644 --- a/helm-control-scripts/src/unarchive.rs +++ b/helm-control-scripts/src/unarchive.rs @@ -8,13 +8,11 @@ fn main() { info!("Env variables received {:?}", &envs); let cli_opts = CliOpts::from_args(); info!("Cli options received {:?}", &cli_opts); - let overrides = match overrides(&cli_opts) { + let overrides = match overrides(&cli_opts, &envs) { Some(inner) => inner, None => vec![], }; - let domain_name = domain_name(&cli_opts); - info!("Domain generated for deployment: {}", &domain_name); let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &envs); let namespace = String::from(&cli_opts.namespace); let release_name = match cli_opts.name { @@ -28,7 +26,6 @@ fn main() { name: envs.helm_bin, mode: HelmMode::Template, release_name: release_name, - release_domain: domain_name, namespace: cli_opts.namespace, deployment_parameters: deployment_parameters, overrides: overrides, From 40b6c1ae5fa3bc9283c67441d362083177c74e76 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Tue, 28 Sep 2021 16:13:56 +0300 Subject: [PATCH 17/21] add gzip static + etag --- charts/octopod/templates/nginx-configmap.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/charts/octopod/templates/nginx-configmap.yaml b/charts/octopod/templates/nginx-configmap.yaml index 15f93e6c..5b27f917 100644 --- a/charts/octopod/templates/nginx-configmap.yaml +++ b/charts/octopod/templates/nginx-configmap.yaml @@ -1,4 +1,5 @@ {{- $octopodAppAuthPassword := include "octopodUiAuthSecret" . -}} +{{- $etag := sha256sum (now | date "Mon Jan 2 15:04:05 MSK 2021") -}} apiVersion: v1 kind: ConfigMap metadata: @@ -14,11 +15,10 @@ data: index index.html; error_page 404 =200 /index.html; charset utf-8; - gzip on; - gzip_disable msie6; - gzip_buffers 32 4k; - gzip_comp_level 3; - gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/rss+xml text/javascript image/svg+xml application/vnd.ms-f ontobject application/x-font-ttf font/opentype; + location ~* \.(html|css|js|xml)$ { + gzip_static on; + add_header 'ETag' '{{ $etag }}'; + } } config.json: | { From b0a57ad70b9e9df6cccbfcfa86596fa765760902 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Wed, 29 Sep 2021 11:30:27 +0300 Subject: [PATCH 18/21] config_check: logs to stderr + proper user notifications --- helm-control-scripts/src/config_check.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/helm-control-scripts/src/config_check.rs b/helm-control-scripts/src/config_check.rs index cdcc5f9c..d049d79c 100644 --- a/helm-control-scripts/src/config_check.rs +++ b/helm-control-scripts/src/config_check.rs @@ -2,7 +2,7 @@ use helm_control_scripts::lib::*; fn main() { let mut log_builder = Builder::from_default_env(); - log_builder.target(Target::Stdout).filter(None, LevelFilter::Info).init(); + log_builder.target(Target::Stderr).filter(None, LevelFilter::Info).init(); info!("Utils version {}", env!("CARGO_PKG_VERSION")); let envs = EnvVars::parse(); info!("Env variables received {:?}", &envs); @@ -40,10 +40,16 @@ fn main() { Some(images) => { match check_images(images) { Ok(_) => info!("Success!"), - Err(err) => panic!("{}", err), + Err(err) => { + println!("Error checking images. Are they present in the registry?"); + panic!("{}", err); + } } }, - None => panic!("No images found!"), + None => { + println!("No images found in pods' declarations"); + panic!("No images found!"); + } } } Err(status) => { From 12bad3752205cc164cd19f42719408449f1d0641 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Wed, 29 Sep 2021 15:31:47 +0300 Subject: [PATCH 19/21] fix bug with empty value key not being shown --- helm-control-scripts/src/app_keys.rs | 2 +- helm-control-scripts/src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/helm-control-scripts/src/app_keys.rs b/helm-control-scripts/src/app_keys.rs index f5a376e1..37b39177 100644 --- a/helm-control-scripts/src/app_keys.rs +++ b/helm-control-scripts/src/app_keys.rs @@ -21,7 +21,7 @@ fn main() { info!("Generated Helm args: {:?}", &helm_values.args()); match helm_values.run_stdout() { Ok(status) => { - print!("{}", print_keys(helm_values_as_keys(status))); + print!("{}", print_keys(helm_values_as_keys(status))); } Err(status) => { error!("Error during helm execution"); diff --git a/helm-control-scripts/src/lib.rs b/helm-control-scripts/src/lib.rs index c33f0d27..8f51382b 100644 --- a/helm-control-scripts/src/lib.rs +++ b/helm-control-scripts/src/lib.rs @@ -9,7 +9,7 @@ pub mod lib { pub use serde::{Serialize, Deserialize}; pub use serde_json::json; pub use std::error; - pub use yaml_rust::{YamlLoader, YamlEmitter}; + pub use yaml_rust::{YamlLoader, YamlEmitter, Yaml}; pub use k8s_openapi::api::{ apps::v1::{Deployment, StatefulSet}, networking::v1::Ingress, @@ -759,7 +759,7 @@ pub mod lib { } } }, - None => continue + None => keys.push(format!("{}", doc.0.as_str().unwrap())), } } if keys.is_empty() { From ba343d62303132b9b2c5260988b3d63c40561155 Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Wed, 29 Sep 2021 15:39:39 +0300 Subject: [PATCH 20/21] rename app-env-override and deployment-override cli options --- helm-control-scripts/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/helm-control-scripts/src/lib.rs b/helm-control-scripts/src/lib.rs index 8f51382b..28cbd320 100644 --- a/helm-control-scripts/src/lib.rs +++ b/helm-control-scripts/src/lib.rs @@ -40,9 +40,9 @@ pub mod lib { #[structopt(long)] pub tag: Option, #[structopt(long)] - pub app_env_override: Vec, + pub application_config: Vec, #[structopt(long)] - pub deployment_override: Vec, + pub deployment_config: Vec, } #[derive(Deserialize, Debug)] @@ -135,7 +135,7 @@ pub mod lib { let mut deployment_parameters = Self::default(); deployment_parameters.chart_repo_user = envs.helm_user.clone(); deployment_parameters.chart_repo_pass = envs.helm_pass.clone(); - for deployment_override in cli_opts.deployment_override.clone().into_iter() { + for deployment_override in cli_opts.deployment_config.clone().into_iter() { let split_override = deployment_override.split('=').collect::>(); match split_override.first().unwrap().as_ref() { "chart_repo_url" => deployment_parameters.chart_repo_url = split_override.last().unwrap().to_string(), @@ -289,7 +289,7 @@ pub mod lib { } pub fn overrides(cli_opts: &CliOpts, envs: &EnvVars) -> Option> { let mut overrides_opts = Vec::new(); - overrides_opts.extend(&cli_opts.app_env_override); + overrides_opts.extend(&cli_opts.application_config); let ingress_override = match &envs.ingress_host_key { Some(key) => format!("{}={}", &key, domain_name(&cli_opts)), None => format!("ingress.hostname={}", domain_name(&cli_opts)), From de28d4912ef6eefd5dadbbb5df14aaaab9c8157c Mon Sep 17 00:00:00 2001 From: Aleksei Sizov Date: Wed, 29 Sep 2021 15:40:31 +0300 Subject: [PATCH 21/21] rename env configuration for octopod --- charts/octopod/templates/octopod-configmap.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/charts/octopod/templates/octopod-configmap.yaml b/charts/octopod/templates/octopod-configmap.yaml index acdb6285..a64c9958 100644 --- a/charts/octopod/templates/octopod-configmap.yaml +++ b/charts/octopod/templates/octopod-configmap.yaml @@ -18,9 +18,9 @@ data: ARCHIVE_CHECKING_COMMAND: {{ printf "%s/archive_check" (include "controlScriptsPath" .) | quote }} CONFIG_CHECKING_COMMAND: {{ printf "%s/config_check" (include "controlScriptsPath" .) | quote }} INFO_COMMAND: {{ printf "%s/info" (include "controlScriptsPath" .) | quote }} - DEPLOYMENT_OVERRIDES_COMMAND: {{ printf "%s/deployment_overrides" (include "controlScriptsPath" .) | quote }} + DEPLOYMENT_CONFIG_COMMAND: {{ printf "%s/deployment_overrides" (include "controlScriptsPath" .) | quote }} DEPLOYMENT_KEYS_COMMAND: {{ printf "%s/deployment_keys" (include "controlScriptsPath" .) | quote }} - APPLICATION_OVERRIDES_COMMAND: {{ printf "%s/app_overrides" (include "controlScriptsPath" .) | quote }} + APPLICATION_CONFIG_COMMAND: {{ printf "%s/app_overrides" (include "controlScriptsPath" .) | quote }} APPLICATION_KEYS_COMMAND: {{ printf "%s/app_keys" (include "controlScriptsPath" .) | quote }} UNARCHIVE_COMMAND: {{ printf "%s/unarchive" (include "controlScriptsPath" .) | quote }} {{- range $name, $value := .Values.octopod.env }}