From 7def6f7b731fa8454ec830c31000214d39542990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Garc=C3=ADa=20Prado?= Date: Mon, 13 Dec 2021 17:18:47 +0100 Subject: [PATCH] ISSUE #1 * Add `microservice-python-deploy-kubernetes` template. --- .github/workflows/template-publish.yml | 4 +++ .../python/deploy/kubernetes/Makefile.jinja | 4 +++ .../python/deploy/kubernetes/copier.yml | 31 +++++++++++++++++++ .../deploy/kubernetes/deploy/Dockerfile | 17 ++++++++++ .../kubernetes/deploy/deployment.yaml.jinja | 24 ++++++++++++++ .../kubernetes/deploy/service.yaml.jinja | 12 +++++++ .../language/python/skeleton/copier.yml | 1 + 7 files changed, 93 insertions(+) create mode 100644 microservice/language/python/deploy/kubernetes/Makefile.jinja create mode 100644 microservice/language/python/deploy/kubernetes/copier.yml create mode 100644 microservice/language/python/deploy/kubernetes/deploy/Dockerfile create mode 100644 microservice/language/python/deploy/kubernetes/deploy/deployment.yaml.jinja create mode 100644 microservice/language/python/deploy/kubernetes/deploy/service.yaml.jinja diff --git a/.github/workflows/template-publish.yml b/.github/workflows/template-publish.yml index d5d431f..9683ab8 100644 --- a/.github/workflows/template-publish.yml +++ b/.github/workflows/template-publish.yml @@ -24,6 +24,9 @@ jobs: - name: Build Microservice Python Deploy Docker Template run: tar -czvf microservice-python-deploy-docker.tar.gz -C ${{ github.workspace }}/microservice/language/python/deploy/docker . + - name: Build Microservice Python Deploy Kubernetes Template + run: tar -czvf microservice-python-deploy-kubernetes.tar.gz -C ${{ github.workspace }}/microservice/language/python/deploy/kubernetes . + - name: Build Project Init Template run: tar -czvf project-init.tar.gz -C ${{ github.workspace }}/project/init . @@ -38,5 +41,6 @@ jobs: microservice-python-package-manager-poetry.tar.gz microservice-python-package-manager-pip.tar.gz microservice-python-deploy-docker.tar.gz + microservice-python-deploy-kubernetes.tar.gz project-init.tar.gz diff --git a/microservice/language/python/deploy/kubernetes/Makefile.jinja b/microservice/language/python/deploy/kubernetes/Makefile.jinja new file mode 100644 index 0000000..b552a50 --- /dev/null +++ b/microservice/language/python/deploy/kubernetes/Makefile.jinja @@ -0,0 +1,4 @@ +build: + docker build --no-cache -t {{ project_name }}_{{ name }} -f deploy/Dockerfile . + docker tag {{ project_name }}_{{ name }}:latest {{ kubernetes_registry }}/{{ project_name }}_{{ name }}:{{ version }} + docker push {{ kubernetes_registry }}/{{ project_name }}_{{ name }}:{{ version }} diff --git a/microservice/language/python/deploy/kubernetes/copier.yml b/microservice/language/python/deploy/kubernetes/copier.yml new file mode 100644 index 0000000..1cfcde1 --- /dev/null +++ b/microservice/language/python/deploy/kubernetes/copier.yml @@ -0,0 +1,31 @@ +name: + type: str + help: What is the name of the microservice? +version: + type: str + help: What is the version of the microservice? +project_name: + type: str + help: What is the name of the project? +rest_port: + type: int + default: 8080 + help: What is the rest's port? +kubernetes_config: + type: str + default: microservices-config + help: What is the kubernetes config? +kubernetes_registry: + type: str + help: What is the kubernetes registry? + +_envops: + block_start_string: "{%" + block_end_string: "%}" + comment_start_string: "{#" + comment_end_string: "#}" + variable_start_string: "{{" + variable_end_string: "}}" + keep_trailing_newline: false + +_templates_suffix: .jinja diff --git a/microservice/language/python/deploy/kubernetes/deploy/Dockerfile b/microservice/language/python/deploy/kubernetes/deploy/Dockerfile new file mode 100644 index 0000000..2e5f79e --- /dev/null +++ b/microservice/language/python/deploy/kubernetes/deploy/Dockerfile @@ -0,0 +1,17 @@ +FROM ghcr.io/clariteia/minos-microservice:0.1.5 as development + +COPY ./pyproject.toml ./poetry.lock ./ +RUN poetry install --no-root +COPY . . +CMD ["poetry", "run", "microservice", "start"] + +FROM development as build +RUN poetry export --without-hashes > req.txt && pip wheel -r req.txt --wheel-dir ./dist +RUN poetry build --format wheel + +FROM python:3.9-slim as production +COPY --from=build /microservice/dist/ ./dist +RUN pip install --no-deps ./dist/* +COPY config.yml ./config.yml +ENTRYPOINT ["microservice"] +CMD ["start"] diff --git a/microservice/language/python/deploy/kubernetes/deploy/deployment.yaml.jinja b/microservice/language/python/deploy/kubernetes/deploy/deployment.yaml.jinja new file mode 100644 index 0000000..2d827dd --- /dev/null +++ b/microservice/language/python/deploy/kubernetes/deploy/deployment.yaml.jinja @@ -0,0 +1,24 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ project_name }}-{{ name }} + namespace: {{ project_name }} +spec: + replicas: 1 + selector: + matchLabels: + app.kubernetes.io/name: {{ project_name }}-{{ name }} + template: + metadata: + labels: + app.kubernetes.io/name: {{ project_name }}-{{ name }} + spec: + containers: + - name: {{ project_name }}-{{ name }} + image: {{ kubernetes_registry }}/{{ project_name }}-{{ name }}:{{ version }} + imagePullPolicy: "IfNotPresent" + ports: + - containerPort: {{ rest_port }} + envFrom: + - configMapRef: + name: {{ kubernetes_config }} \ No newline at end of file diff --git a/microservice/language/python/deploy/kubernetes/deploy/service.yaml.jinja b/microservice/language/python/deploy/kubernetes/deploy/service.yaml.jinja new file mode 100644 index 0000000..cb6174b --- /dev/null +++ b/microservice/language/python/deploy/kubernetes/deploy/service.yaml.jinja @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ project_name }}-{{ name }} + namespace: {{ project_name }} +spec: + type: ClusterIP + ports: + - port: {{ rest_port }} + targetPort: {{ rest_port }} + selector: + app.kubernetes.io/name: {{ project_name }}-{{ name }} \ No newline at end of file diff --git a/microservice/language/python/skeleton/copier.yml b/microservice/language/python/skeleton/copier.yml index b2be14c..abb3cdc 100644 --- a/microservice/language/python/skeleton/copier.yml +++ b/microservice/language/python/skeleton/copier.yml @@ -75,6 +75,7 @@ deploy: help: What deploy system do you want to use? choices: docker: &docker_url "{{ template_root }}/microservice-python-deploy-docker.tar.gz" + kubernetes: "{{ template_root }}/microservice-python-deploy-kubernetes.tar.gz" default: *docker_url _envops: