Initial Implementation of Better Deployment

This branch basically removes the necessity of a setup script and just deploys
This commit is contained in:
2024-10-14 02:36:17 -03:00
parent f6d74d3833
commit abe800dd5e
9 changed files with 170 additions and 445 deletions

189
deploy.sh
View File

@@ -1,154 +1,63 @@
#!/bin/bash
#!/bin/sh
function check_for_dependencies() {
if ! command -v kubectl &>/dev/null; then
echo "kubectl could not be found"
exit 1
fi
if ! command -v jq &>/dev/null; then
echo "jq could not be found"
exit 1
fi
if ! command -v helm &>/dev/null; then
echo "helm could not be found"
exit 1
# eval "$(awk 'BEGIN{
# for (i in ENVIRON) {
# if (i ~ /^(KUBE_)[a-zA-Z_][a-zA-Z0-9_]*$/) {
# printf "export " i "_B64=";
# system("echo \"$"i"\" | base64 -w0");
# print;
# }
# }
# }' /dev/null)"
function read_env_file() {
if [[ -f $1 ]]; then
set -a && source $1 && set +a;
fi
}
function configure_nginx_ingress() {
helm upgrade --install ingress-nginx ingress-nginx \
--repo https://kubernetes.github.io/ingress-nginx \
--namespace ingress-nginx --create-namespace
kubectl wait --namespace ingress-nginx \
--for=condition=ready pod \
--selector=app.kubernetes.io/component=controller \
--timeout=120s
function build_secret_envs() {
for i in $(env | grep -E '^KUBE_[a-zA-Z_][a-zA-Z0-9_]*=' | cut -d= -f1); do
eval "export ${i}_B64=$(echo ${!i} | base64 -w0)"
done
}
function configure_cert_manager() {
helm repo add jetstack https://charts.jetstack.io --force-update
helm repo update
helm install cert-manager jetstack/cert-manager \
--namespace cert-manager \
--create-namespace \
--version v1.14.2 \
--set installCRDs=true \
--timeout=600s
function deploy_kubernetes() {
KUBE_FILES=(
"./template/portfolio-namespace.template.yaml"
"./template/portfolio-secret.template.yml"
)
for file in ${KUBE_FILES[@]}; do
echo -e "\n\n----------------------------------------------------\n"
echo -e "Deploying: $file\n"
echo -e "----------------------------------------------------\n\n\n"
envsubst < $file
done
}
function configure_postgres() {
helm repo add cnpg https://cloudnative-pg.github.io/charts
helm upgrade --install cnpg \
--namespace portfolio \
--create-namespace \
cnpg/cloudnative-pg
kubectl wait --for=condition=available \
--timeout=600s \
deployment.apps/cnpg-cloudnative-pg \
-n portfolio
kubectl apply -f ./deployment/postgres/cn-cluster.yaml
kubectl wait --for=condition=Ready \
--timeout=600s \
cluster/postgres-cn-cluster \
-n portfolio
}
function application_deploy() {
kubectl create secret generic backend-secret -n portfolio \
--from-env-file <(jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" ./deployment/secrets/backendSecret.json)
kubectl create secret generic frontend-secret -n portfolio \
--from-env-file <(jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" ./deployment/secrets/frontendSecret.json)
kubectl create secret generic redis-secret -n portfolio \
--from-env-file <(jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" ./deployment/secrets/redisSecret.json)
kubectl create secret generic storage-secret -n portfolio \
--from-env-file <(jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" ./deployment/secrets/storageSecret.json)
kubectl apply -f ./deployment/redis
kubectl wait --for=condition=available \
--timeout=600s \
deployment.apps/redis-deployment \
-n portfolio
kubectl apply -f ./deployment/frontend
kubectl wait --for=condition=available \
--timeout=600s \
deployment.apps/frontend-deployment \
-n portfolio
kubectl apply -f ./deployment/storage
kubectl wait --for=condition=available \
--timeout=600s \
deployment.apps/storage-deployment \
-n portfolio
kubectl apply -f ./deployment/backend
kubectl wait --for=condition=available \
--timeout=600s \
deployment.apps/backend-deployment \
-n portfolio
kubectl apply -f \
./deployment/nginx-ingress
}
function main() {
build_secret_envs
check_for_dependencies
if [[ $1 == "--local" || $1 == "-l" ]]; then
function kubectl {
minikube kubectl -- $@
}
minikube start --driver kvm2 --cpus 2 --memory 4Gib
minikube addons enable ingress-dns
minikube addons enable ingress
else
configure_nginx_ingress
fi
configure_cert_manager
kubectl apply -f ./deployment/portfolio-namespace.yaml
configure_postgres
application_deploy
if [[ $1 == "--local" || $1 == "-l" ]]; then
kubectl apply -f \
./deployment/cert-manager/cert-manager-issuer-dev.yaml
kubectl apply -f \
./deployment/cert-manager/cert-manager-certificate.yaml
echo "http://$(/usr/bin/minikube ip)"
else
kubectl apply -f \
./deployment/cert-manager/cert-manager-issuer.yaml
kubectl apply -f \
./deployment/cert-manager/cert-manager-certificate.yaml
fi
exit 0
deploy_kubernetes
}
main $1
while getopts ":f:" opt; do
case ${opt} in
f )
echo "Reading env file: ${OPTARG}"
read_env_file ${OPTARG}
;;
\? )
echo "Usage: deploy.sh [-f <env_file>]"
;;
esac
done
main