diff --git a/.gitignore b/.gitignore index 382902f..d48d751 100644 --- a/.gitignore +++ b/.gitignore @@ -1,17 +1,18 @@ -.env* - -.secret* - .idea/ - .vscode/ +.env* +.secret* + **/*.json !**/*.example.json +*.patch **/cert-manager-certificate.yaml - -**/deployment/nginx-ingress/nginx-ingress-api.yaml - -**/deployment/nginx-ingress/nginx-ingress-root.yaml -*.patch +**/nginx-ingress-api.yaml +**/nginx-ingress-root.yaml +**/backend.yaml +**/frontend.yaml +**/storage.yaml +**/storage-processor.yaml +**/cn-cluster.yaml \ No newline at end of file diff --git a/deploy.sh b/deploy.sh index ebae0d7..f48ead8 100755 --- a/deploy.sh +++ b/deploy.sh @@ -95,9 +95,7 @@ function application_deploy() { -n portfolio kubectl apply -f \ - ./deployment/nginx-ingress/nginx-ingress-root.yaml - kubectl apply -f \ - ./deployment/nginx-ingress/nginx-ingress-api.yaml + ./deployment/nginx-ingress } diff --git a/deployment/storage/storage-processor.yaml b/deployment/storage/storage-processor.yaml deleted file mode 100644 index cdff97e..0000000 --- a/deployment/storage/storage-processor.yaml +++ /dev/null @@ -1,93 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - namespace: portfolio - name: storage-processor-deployment -spec: - replicas: 1 - selector: - matchLabels: - app: storage-processor - template: - metadata: - labels: - app: storage-processor - spec: - nodeSelector: - node_type: worker - containers: - - name: storage-processor - image: yoshiunfriendly/storage-hideyoshi.com:latest - command: ["./run-queue.sh"] - args: ["-q"] - imagePullPolicy: "Always" - resources: - requests: - memory: "256Mi" - cpu: "250m" - limits: - memory: "512Mi" - cpu: "500m" - env: - - name: REDIS_BASE_URL - valueFrom: - configMapKeyRef: - name: redis-config - key: redis-url - - - name: REDIS_PORT - valueFrom: - configMapKeyRef: - name: redis-config - key: redis-port - - - name: REDIS_PASSWORD - valueFrom: - secretKeyRef: - name: redis-secret - key: redisPassword - - - name: REDIS_URL - value: "redis://:$(REDIS_PASSWORD)@$(REDIS_BASE_URL):$(REDIS_PORT)/rq" - - - name: STORAGE_TYPE - valueFrom: - secretKeyRef: - name: storage-secret - key: storageType - - - name: AWS_ACCESS_KEY_ID - valueFrom: - secretKeyRef: - name: storage-secret - key: awsAccessKeyId - - - name: AWS_SECRET_ACCESS_KEY - valueFrom: - secretKeyRef: - name: storage-secret - key: awsSecretAccessKey - - - name: AWS_REGION_NAME - valueFrom: - secretKeyRef: - name: storage-secret - key: awsRegion - - - name: AWS_BUCKET_NAME - valueFrom: - secretKeyRef: - name: storage-secret - key: awsBucket - - - name: VIRUS_CHECKER_TYPE - valueFrom: - secretKeyRef: - name: storage-secret - key: virusCheckerType - - - name: VIRUS_CHECKER_API_KEY - valueFrom: - secretKeyRef: - name: storage-secret - key: virusCheckerApiKey diff --git a/setup.py b/setup.py index 3d85c40..264d9bc 100644 --- a/setup.py +++ b/setup.py @@ -2,19 +2,25 @@ from base64 import b64decode, b64encode from dotenv import load_dotenv from envsubst import envsubst from pathlib import Path, PosixPath +from typing import Generator import argparse import warnings import json import os +def unpack_list_dict(dl: list[dict]) -> Generator[tuple[str, str], None, None]: + for d in dl: + yield tuple(d.values()) + + def write_template(template: str, output: str): with open(template, 'r') as template,\ open(output, 'w') as output: output.write(envsubst(template.read())) -def configure_templates(environment: str): +def configure_env_variables(environment: str): if not environment in ("prod", "staging", "local"): raise ValueError("Invalid Environment Selected") @@ -22,25 +28,40 @@ def configure_templates(environment: str): case "local": DOMAIN = "local.hideyoshi.com.br" API_DOMAIN = "api.local.hideyoshi.com.br" + MASTER_NODE_LABEL = "minikube.k8s.io/name: minikube" + WORKER_NODE_LABEL = "minikube.k8s.io/name: minikube" + case "staging": DOMAIN = "staging.hideyoshi.com.br" API_DOMAIN = "api.staging.hideyoshi.com.br" + MASTER_NODE_LABEL = "node_type: master" + WORKER_NODE_LABEL = "node_type: worker" + case _: DOMAIN = "hideyoshi.com.br" API_DOMAIN = "api.hideyoshi.com.br" + MASTER_NODE_LABEL = "node_type: master" + WORKER_NODE_LABEL = "node_type: worker" os.environ["DOMAIN"] = DOMAIN os.environ["API_DOMAIN"] = API_DOMAIN + os.environ["MASTER_NODE_LABEL"] = MASTER_NODE_LABEL + os.environ["WORKER_NODE_LABEL"] = WORKER_NODE_LABEL - write_template( - "template/cert-manager/cert-manager-certificate.template.yaml", - "deployment/cert-manager/cert-manager-certificate.yaml" - ) - write_template( - "template/nginx-ingress/nginx-ingress-root.yaml", - "deployment/nginx-ingress/nginx-ingress-root.yaml" - ) +def configure_templates(environment: str): + MAPPINS = [ + {"template": "template/cert-manager/cert-manager-certificate.template.yaml", "output": "deployment/cert-manager/cert-manager-certificate.yaml"}, + {"template": "template/nginx-ingress/nginx-ingress-root.template.yaml", "output": "deployment/nginx-ingress/nginx-ingress-root.yaml"}, + {"template": "template/postgres/cn-cluster.template.yaml", "output": "deployment/postgres/cn-cluster.yaml"}, + {"template": "template/frontend/frontend.template.yaml", "output": "deployment/frontend/frontend.yaml"}, + {"template": "template/backend/backend.template.yaml", "output": "deployment/backend/backend.yaml"}, + {"template": "template/storage/storage-processor.template.yaml", "output": "deployment/storage/storage-processor.yaml"}, + {"template": "template/storage/storage.template.yaml", "output": "deployment/storage/storage.yaml"}, + ] + + for template, output in unpack_list_dict(MAPPINS): + write_template(template, output) def validate_backend_secret(secret: str): @@ -167,6 +188,8 @@ def main(file, environment): write_secrets_to_file(env) + configure_env_variables(environment) + configure_templates(environment) diff --git a/deployment/backend/backend.yaml b/template/backend/backend.template.yaml similarity index 99% rename from deployment/backend/backend.yaml rename to template/backend/backend.template.yaml index 559b5c8..3ce530f 100644 --- a/deployment/backend/backend.yaml +++ b/template/backend/backend.template.yaml @@ -14,7 +14,7 @@ spec: app: backend spec: nodeSelector: - node_type: worker + ${WORKER_NODE_LABEL} containers: - name: backend image: yoshiunfriendly/backend-hideyoshi.com diff --git a/deployment/frontend/frontend.yaml b/template/frontend/frontend.template.yaml similarity index 96% rename from deployment/frontend/frontend.yaml rename to template/frontend/frontend.template.yaml index 6bf24dd..e989be6 100644 --- a/deployment/frontend/frontend.yaml +++ b/template/frontend/frontend.template.yaml @@ -15,8 +15,7 @@ spec: labels: app: frontend spec: - nodeSelector: - node_type: worker + nodeSelector: ${WORKER_NODE_LABEL} containers: - name: frontend image: yoshiunfriendly/frontend-hideyoshi.com:latest diff --git a/template/nginx-ingress/nginx-ingress-root.yaml b/template/nginx-ingress/nginx-ingress-root.template.yaml similarity index 100% rename from template/nginx-ingress/nginx-ingress-root.yaml rename to template/nginx-ingress/nginx-ingress-root.template.yaml diff --git a/deployment/postgres/cn-cluster.yaml b/template/postgres/cn-cluster.template.yaml similarity index 93% rename from deployment/postgres/cn-cluster.yaml rename to template/postgres/cn-cluster.template.yaml index 73918b7..97f65ea 100644 --- a/deployment/postgres/cn-cluster.yaml +++ b/template/postgres/cn-cluster.template.yaml @@ -21,7 +21,7 @@ spec: affinity: nodeSelector: - node_type: master + ${MASTER_NODE_LABEL} monitoring: enablePodMonitor: true diff --git a/template/storage/storage-processor.template.yaml b/template/storage/storage-processor.template.yaml new file mode 100644 index 0000000..fd68e77 --- /dev/null +++ b/template/storage/storage-processor.template.yaml @@ -0,0 +1,93 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + namespace: portfolio + name: storage-processor-deployment +spec: + replicas: 1 + selector: + matchLabels: + app: storage-processor + template: + metadata: + labels: + app: storage-processor + spec: + nodeSelector: + ${WORKER_NODE_LABEL} + containers: + - name: storage-processor + image: yoshiunfriendly/storage-hideyoshi.com:latest + command: ["./run-queue.sh"] + args: ["-q"] + imagePullPolicy: "Always" + resources: + requests: + memory: "256Mi" + cpu: "250m" + limits: + memory: "512Mi" + cpu: "500m" + env: + - name: REDIS_BASE_URL + valueFrom: + configMapKeyRef: + name: redis-config + key: redis-url + + - name: REDIS_PORT + valueFrom: + configMapKeyRef: + name: redis-config + key: redis-port + + - name: REDIS_PASSWORD + valueFrom: + secretKeyRef: + name: redis-secret + key: redisPassword + + - name: REDIS_URL + value: "redis://:$(REDIS_PASSWORD)@$(REDIS_BASE_URL):$(REDIS_PORT)/rq" + + - name: STORAGE_TYPE + valueFrom: + secretKeyRef: + name: storage-secret + key: storageType + + - name: AWS_ACCESS_KEY_ID + valueFrom: + secretKeyRef: + name: storage-secret + key: awsAccessKeyId + + - name: AWS_SECRET_ACCESS_KEY + valueFrom: + secretKeyRef: + name: storage-secret + key: awsSecretAccessKey + + - name: AWS_REGION_NAME + valueFrom: + secretKeyRef: + name: storage-secret + key: awsRegion + + - name: AWS_BUCKET_NAME + valueFrom: + secretKeyRef: + name: storage-secret + key: awsBucket + + - name: VIRUS_CHECKER_TYPE + valueFrom: + secretKeyRef: + name: storage-secret + key: virusCheckerType + + - name: VIRUS_CHECKER_API_KEY + valueFrom: + secretKeyRef: + name: storage-secret + key: virusCheckerApiKey diff --git a/deployment/storage/storage.yaml b/template/storage/storage.template.yaml similarity index 99% rename from deployment/storage/storage.yaml rename to template/storage/storage.template.yaml index f97558c..eb53dc8 100644 --- a/deployment/storage/storage.yaml +++ b/template/storage/storage.template.yaml @@ -14,7 +14,7 @@ spec: app: storage spec: nodeSelector: - node_type: worker + ${WORKER_NODE_LABEL} containers: - name: storage image: yoshiunfriendly/storage-hideyoshi.com:latest