From 92a4b75dfd2f35b17e914fa90c7591fd616c3945 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Thu, 12 Oct 2023 09:33:01 -0300 Subject: [PATCH 1/4] Reduces Project Cost by Optimizing Resources --- deployment/backend/backend.yaml | 4 ++-- deployment/frontend/frontend.yaml | 4 ++-- deployment/postgres/postgres.yaml | 2 +- deployment/redis/redis.yaml | 2 +- deployment/storage/storage.yaml | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/deployment/backend/backend.yaml b/deployment/backend/backend.yaml index 1200bab..c129cd0 100644 --- a/deployment/backend/backend.yaml +++ b/deployment/backend/backend.yaml @@ -22,8 +22,8 @@ spec: memory: "256Mi" cpu: "250m" limits: - memory: "512Mi" - cpu: "750m" + memory: "768Mi" + cpu: "500m" ports: - containerPort: 8070 env: diff --git a/deployment/frontend/frontend.yaml b/deployment/frontend/frontend.yaml index a9e62af..f9638fd 100644 --- a/deployment/frontend/frontend.yaml +++ b/deployment/frontend/frontend.yaml @@ -24,8 +24,8 @@ spec: memory: "256Mi" cpu: "250m" limits: - memory: "1536Mi" - cpu: "750m" + memory: "512Mi" + cpu: "500m" ports: - containerPort: 5000 env: diff --git a/deployment/postgres/postgres.yaml b/deployment/postgres/postgres.yaml index e6f8399..827e349 100644 --- a/deployment/postgres/postgres.yaml +++ b/deployment/postgres/postgres.yaml @@ -22,7 +22,7 @@ spec: memory: "512Mi" cpu: "500m" limits: - memory: "512Mi" + memory: "768Mi" cpu: "500m" ports: - containerPort: 5432 diff --git a/deployment/redis/redis.yaml b/deployment/redis/redis.yaml index bf3ba34..3c1eaec 100644 --- a/deployment/redis/redis.yaml +++ b/deployment/redis/redis.yaml @@ -23,7 +23,7 @@ spec: cpu: "250m" limits: memory: "1024Mi" - cpu: "500m" + cpu: "250m" ports: - containerPort: 6379 env: diff --git a/deployment/storage/storage.yaml b/deployment/storage/storage.yaml index bfceda1..2ffe59d 100644 --- a/deployment/storage/storage.yaml +++ b/deployment/storage/storage.yaml @@ -22,8 +22,8 @@ spec: memory: "256Mi" cpu: "250m" limits: - memory: "512Mi" - cpu: "500m" + memory: "256Mi" + cpu: "250m" ports: - containerPort: 8000 env: From b95db2a673a0f72da12c1f97f318990214dcef08 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Sun, 15 Oct 2023 02:35:36 -0300 Subject: [PATCH 2/4] Refreshes Images in Case of New Image --- deploy.sh | 3 +++ refresh.sh | 19 +++++++++++++++++++ setup.py | 1 + 3 files changed, 23 insertions(+) create mode 100755 refresh.sh diff --git a/deploy.sh b/deploy.sh index 9c8eb9a..152b02f 100755 --- a/deploy.sh +++ b/deploy.sh @@ -111,6 +111,9 @@ function main() { fi + # Refreshes all pods in case of a new image + bash ./refresh.sh + exit 0 } diff --git a/refresh.sh b/refresh.sh new file mode 100755 index 0000000..3d2a6ed --- /dev/null +++ b/refresh.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +NAMESPACES=( + portfolio +) + +DEPLOYMENTS=( + "frontend-deployment" + "backend-deployment" + "storage-deployment" + "storage-processor-deployment" +) + +for i in "${NAMESPACES[@]}"; do + for x in "${DEPLOYMENTS[@]}"; do + PODS=$(kubectl -n $i get pods --no-headers | awk '{print $1}' | grep $x | tr '\n' ' ') + kubectl -n $i delete pods $PODS + done +done diff --git a/setup.py b/setup.py index d9466b7..f8edc4b 100644 --- a/setup.py +++ b/setup.py @@ -13,6 +13,7 @@ def write_template(template: str, output: str): open(output, 'w') as output: output.write(envsubst(template.read())) + def configure_templates(environment: str): if not environment in ("prod", "staging", "local"): raise ValueError("Invalid Environment Selected") From 57d1cf5453cb49eb0d97b58a76148bf50fb365c6 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Mon, 23 Oct 2023 20:06:12 -0300 Subject: [PATCH 3/4] Implements Workflow Dispatcher for Deployment Refresh --- .github/workflows/refresh-deployment.yml | 48 ++++++++++++++++++++++++ deploy.sh | 7 ++-- refresh.sh | 16 +++++--- 3 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/refresh-deployment.yml diff --git a/.github/workflows/refresh-deployment.yml b/.github/workflows/refresh-deployment.yml new file mode 100644 index 0000000..02f9511 --- /dev/null +++ b/.github/workflows/refresh-deployment.yml @@ -0,0 +1,48 @@ +name: workflow_02 + +on: + workflow_run: + workflows: ["build"] + branches: [main] + types: + - completed + repository_dispatch: + types: [refresh-deployments] + +jobs: + refresh-deployments: + name: Refresh deployments + environment: prod + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'success' }} + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Executing Remote Commands - Refresh All Deployments + env: + deployments: ${{ github.event.client_payload.deployments }} + if: ${{ env.deployments == '' }} + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.SSH_HOST }} + username: ${{ secrets.SSH_USER }} + port: ${{ secrets.SSH_PORT }} + key: ${{ secrets.SSH_KEY }} + script: | + [[ -d infra-hideyoshi.com ]] && \ + ./infra-hideyoshi.com/refresh.sh + + - name: Executing Remote Commands - Refresh Specific Deployments + env: + deployments: ${{ github.event.client_payload.deployments }} + if: ${{ env.deployments != '' }} + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.SSH_HOST }} + username: ${{ secrets.SSH_USER }} + port: ${{ secrets.SSH_PORT }} + key: ${{ secrets.SSH_KEY }} + script: | + [[ -d infra-hideyoshi.com ]] && \ + ./infra-hideyoshi.com/refresh.sh ${{ env.deployments }} \ No newline at end of file diff --git a/deploy.sh b/deploy.sh index 152b02f..6c0c271 100755 --- a/deploy.sh +++ b/deploy.sh @@ -109,10 +109,11 @@ function main() { kubectl apply -f \ ./deployment/cert-manager/cert-manager-certificate.yaml - fi + if [[ $1 == "--staging" || $1 == "-s" ]]; then + bash ./refresh.sh + fi - # Refreshes all pods in case of a new image - bash ./refresh.sh + fi exit 0 diff --git a/refresh.sh b/refresh.sh index 3d2a6ed..9f3e17d 100755 --- a/refresh.sh +++ b/refresh.sh @@ -4,12 +4,16 @@ NAMESPACES=( portfolio ) -DEPLOYMENTS=( - "frontend-deployment" - "backend-deployment" - "storage-deployment" - "storage-processor-deployment" -) +if [ $# -eq 0 ]; then + DEPLOYMENTS=( + "frontend-deployment" + "backend-deployment" + "storage-deployment" + "storage-processor-deployment" + ) +else + DEPLOYMENTS=("$@") +fi for i in "${NAMESPACES[@]}"; do for x in "${DEPLOYMENTS[@]}"; do From 7b42f972489e7315995f936c5e0ba540915eb2e4 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Mon, 23 Oct 2023 20:09:30 -0300 Subject: [PATCH 4/4] Fixes Desired Behavior --- .github/workflows/deploy-staging.yml | 3 ++- .github/workflows/refresh-deployment.yml | 3 +-- deploy.sh | 4 ---- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/.github/workflows/deploy-staging.yml b/.github/workflows/deploy-staging.yml index d1c8400..2b612fe 100644 --- a/.github/workflows/deploy-staging.yml +++ b/.github/workflows/deploy-staging.yml @@ -47,4 +47,5 @@ jobs: script: | sudo apt update && sudo apt install -y jq python3-pip cd infra-hideyoshi.com - ./deploy.sh --staging \ No newline at end of file + ./deploy.sh --staging + ./refresh.sh \ No newline at end of file diff --git a/.github/workflows/refresh-deployment.yml b/.github/workflows/refresh-deployment.yml index 02f9511..3c1f566 100644 --- a/.github/workflows/refresh-deployment.yml +++ b/.github/workflows/refresh-deployment.yml @@ -14,7 +14,6 @@ jobs: name: Refresh deployments environment: prod runs-on: ubuntu-latest - if: ${{ github.event.workflow_run.conclusion == 'success' }} steps: - name: Checkout uses: actions/checkout@v3 @@ -22,7 +21,7 @@ jobs: - name: Executing Remote Commands - Refresh All Deployments env: deployments: ${{ github.event.client_payload.deployments }} - if: ${{ env.deployments == '' }} + if: ${{ env.deployments == '' || github.event.workflow_run.conclusion == 'success' }} uses: appleboy/ssh-action@master with: host: ${{ secrets.SSH_HOST }} diff --git a/deploy.sh b/deploy.sh index 6c0c271..9c8eb9a 100755 --- a/deploy.sh +++ b/deploy.sh @@ -109,10 +109,6 @@ function main() { kubectl apply -f \ ./deployment/cert-manager/cert-manager-certificate.yaml - if [[ $1 == "--staging" || $1 == "-s" ]]; then - bash ./refresh.sh - fi - fi exit 0