Skip to content

Commit

Permalink
New control scripts (#108)
Browse files Browse the repository at this point in the history
* add app and deployment overrides info scripts

* overrides/keys scripts

* add new scripts to the chart config

* archive using scaling

* archive check script + README

* fix timings values + up contolscripts version

* Get rid of kubectl calls

* remove tags

* remove status mode

* rename tag_check to config_check

* remove default values logic, optional name, additional checks at helm init

* bump chart and app version

* add keys sorting

* ingress.host to ingress.hostname hardcode

* add default override for wordpress

* parametrize ingress host key

* add gzip static + etag

* config_check: logs to stderr + proper user notifications

* fix bug with empty value key not being shown

* rename app-env-override and deployment-override cli options

* rename env configuration for octopod
  • Loading branch information
a13x5 committed Sep 29, 2021
1 parent a2c8ab0 commit c8ac911
Show file tree
Hide file tree
Showing 21 changed files with 714 additions and 242 deletions.
4 changes: 2 additions & 2 deletions charts/octopod/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions charts/octopod/templates/nginx-configmap.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{- $octopodAppAuthPassword := include "octopodUiAuthSecret" . -}}
{{- $etag := sha256sum (now | date "Mon Jan 2 15:04:05 MSK 2021") -}}
apiVersion: v1
kind: ConfigMap
metadata:
Expand All @@ -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: |
{
Expand Down
7 changes: 6 additions & 1 deletion charts/octopod/templates/octopod-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ 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 }}
CONFIG_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_CONFIG_COMMAND: {{ printf "%s/deployment_overrides" (include "controlScriptsPath" .) | quote }}
DEPLOYMENT_KEYS_COMMAND: {{ printf "%s/deployment_keys" (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 }}
{{ $name }}: {{ $value | quote }}
{{- end }}
11 changes: 6 additions & 5 deletions charts/octopod/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,28 @@ octopod:
projectName: Octopod
deploymentNamespace: octopod-deployment
baseDomain: ""
statusUpdateTimeout: 600
archiveRetention: 1209600
statusUpdateTimeout: "600"
archiveRetention: "1209600"
migrations:
enabled: true
env:
HELM_BIN: "/utils/helm"
KUBECTL_BIN: "/utils/kubectl"
DEFAULTS: |
{
"chart_name": "wordpress",
"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:
image:
repository: typeable/octopod-helm-control-scripts
pullPolicy: IfNotPresent
tag: 0.1.0
tag: 0.2.0
sqitch:
image:
repository: typeable/sqitch
Expand Down
2 changes: 1 addition & 1 deletion helm-control-scripts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 23 additions & 3 deletions helm-control-scripts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "helm-control-scripts"
version = "0.1.0"
version = "0.2.0"
authors = ["Aleksei Sizov <a.sizov@typeable.io>"]
edition = "2018"

Expand Down Expand Up @@ -54,5 +54,25 @@ 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"
path = "src/app_overrides.rs"

[[bin]]
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"
11 changes: 6 additions & 5 deletions helm-control-scripts/Dockerfile
Original file line number Diff line number Diff line change
@@ -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/
Expand All @@ -22,4 +18,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/
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/
ADD target/x86_64-unknown-linux-musl/release/deployment_keys /utils/
ADD target/x86_64-unknown-linux-musl/release/unarchive /utils/
16 changes: 11 additions & 5 deletions helm-control-scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- 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.
- 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

Expand All @@ -22,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
Expand All @@ -34,4 +40,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.
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.
31 changes: 31 additions & 0 deletions helm-control-scripts/src/app_keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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 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,
mode: HelmMode::ShowValues,
release_name: String::from(""),
namespace: String::from(""),
deployment_parameters: deployment_parameters,
overrides: vec![],
};
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);
}
}
}
11 changes: 11 additions & 0 deletions helm-control-scripts/src/app_overrides.rs
Original file line number Diff line number Diff line change
@@ -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_kv(default_values.app_overrides()));
}
45 changes: 33 additions & 12 deletions helm-control-scripts/src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,45 @@ 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 deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs);
let helm_cmd = HelmCmd {
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 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::Uninstall,
release_name: cli_opts.name,
release_domain: String::from(""),
mode: HelmMode::Template,
release_name: release_name,
namespace: cli_opts.namespace,
deployment_parameters: deployment_parameters,
overrides: vec![],
default_values: vec![],
image_tag: String::from("")
overrides: overrides,
};
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);
Expand Down
56 changes: 39 additions & 17 deletions helm-control-scripts/src/archive_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,50 @@ 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();
let deployment_parameters = HelmDeploymentParameters::new(&cli_opts, &default_values, &envs);
let helm_cmd = HelmCmd {
let cli_opts = CliOpts::from_args();
info!("Cli options received {:?}", &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 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::Status,
release_name: cli_opts.name,
release_domain: String::from(""),
mode: HelmMode::Template,
release_name: release_name,
namespace: cli_opts.namespace,
deployment_parameters: deployment_parameters,
overrides: vec![],
default_values: vec![],
image_tag: String::from("")
overrides: overrides,
};
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);
}
}
}
Loading

0 comments on commit c8ac911

Please sign in to comment.