Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add Deployment to PoC Environment #7

Merged
merged 6 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions .github/workflows/cicd-deploy-to-poc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: "♻️ Deploy to PoC Namespace"

on:
workflow_dispatch:
push:
branches: [main]
env:
KUBE_CLUSTER: ${{ secrets.KUBE_CLUSTER }}
KUBE_NAMESPACE: ${{ secrets.POC_KUBE_NAMESPACE }}
KUBE_CERT: ${{ secrets.POC_KUBE_CERT }}
KUBE_TOKEN: ${{ secrets.POC_KUBE_TOKEN }}

IMAGE_TAG: ${{ github.sha }}
ECR_REGISTRY: ${{ vars.DEVELOPMENT_ECR_REGISTRY }}
ECR_REPOSITORY: ${{ vars.DEVELOPMENT_ECR_REPOSITORY }}

POSTGRES_USER: ${{secrets.POC_POSTGRES_USER}}
POSTGRES_PASSWORD: ${{secrets.POC_POSTGRES_PASSWORD}}
POSTGRES_DB: ${{secrets.POC_POSTGRES_DB}}
POSTGRES_HOST: ${{secrets.POC_POSTGRES_HOST}}
POSTGRES_PORT: ${{secrets.POC_POSTGRES_PORT}}

jobs:
build-push:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4

- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.DEVELOPMENT_ECR_ROLE_TO_ASSUME }}
aws-region: ${{ vars.DEVELOPMENT_ECR_REGION }}

- uses: aws-actions/amazon-ecr-login@v2
id: login-ecr
- run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

deploy-to-poc:
needs: build-push
runs-on: ubuntu-latest
container: alpine/k8s:1.23.17
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Authenticate to the cluster
run: |
echo "${KUBE_CERT}" > ca.crt
kubectl config set-cluster ${KUBE_CLUSTER} --certificate-authority=./ca.crt --server=https://${KUBE_CLUSTER}
kubectl config set-credentials deploy-user --token=${KUBE_TOKEN}
kubectl config set-context ${KUBE_CLUSTER} --cluster=${KUBE_CLUSTER} --user=deploy-user --namespace=${KUBE_NAMESPACE}
kubectl config get-contexts
kubectl config use-context ${KUBE_CLUSTER}

- name: Deploy helm chart to PoC
run: |
helm upgrade kpi-dashboard \
helm/kpi-dashboard \
--install \
--force \
--wait \
--timeout 10m \
--namespace ${KUBE_NAMESPACE} \
--values=helm/kpi-dashboard/values-poc.yaml \
--set app.deployment.env.POSTGRES_USER=${POSTGRES_USER} \
--set app.deployment.env.POSTGRES_PASSWORD=${POSTGRES_PASSWORD} \
--set app.deployment.env.POSTGRES_DB=${POSTGRES_DB} \
--set app.deployment.env.POSTGRES_HOST=${POSTGRES_HOST} \
--set app.deployment.env.POSTGRES_PORT=${POSTGRES_PORT} \
--set app.deployment.image.repository=${ECR_REGISTRY}/${ECR_REPOSITORY} \
--set app.deployment.image.tag=${IMAGE_TAG}
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ FROM python:3.12.0-slim

LABEL maintainer="operations-engineering <[email protected]>"

RUN groupadd -r user && useradd -r -g user 1051

WORKDIR /home/operations-engineering-kpi-dashboard

COPY Pipfile Pipfile
COPY Pipfile.lock Pipfile.lock
COPY app app
COPY data data

COPY example-data data

RUN pip3 install --no-cache-dir pipenv
RUN pipenv install --system --deploy --ignore-pipfile
Expand All @@ -18,4 +19,6 @@ ENV PYTHONUNBUFFERED 1

EXPOSE 4567

USER 1051

ENTRYPOINT ["gunicorn", "--bind=0.0.0.0:4567", "app.run:app()"]
20 changes: 10 additions & 10 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ services:
networks:
- kpi-dashboard

grafana:
build:
dockerfile: ./grafana/Dockerfile
context: ./
container_name: grafana
restart: unless-stopped
ports:
- "3000:3000"
networks:
- kpi-dashboard
# grafana:
# build:
# dockerfile: ./grafana/Dockerfile
# context: ./
# container_name: grafana
# restart: unless-stopped
# ports:
# - "3000:3000"
# networks:
# - kpi-dashboard

networks:
kpi-dashboard:
23 changes: 23 additions & 0 deletions helm/kpi-dashboard/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
24 changes: 24 additions & 0 deletions helm/kpi-dashboard/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: kpi-dashboard
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"
51 changes: 51 additions & 0 deletions helm/kpi-dashboard/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "kpi-dashboard.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "kpi-dashboard.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "kpi-dashboard.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "kpi-dashboard.labels" -}}
helm.sh/chart: {{ include "kpi-dashboard.chart" . }}
{{ include "kpi-dashboard.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "kpi-dashboard.selectorLabels" -}}
app.kubernetes.io/name: {{ include "kpi-dashboard.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
35 changes: 35 additions & 0 deletions helm/kpi-dashboard/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "kpi-dashboard.fullname" . }}
labels: {{- include "kpi-dashboard.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.app.deployment.replicaCount }}
selector:
matchLabels: {{- include "kpi-dashboard.selectorLabels" . | nindent 6 }}
template:
metadata:
labels: {{- include "kpi-dashboard.selectorLabels" . | nindent 8 }}
spec:
# serviceAccountName created by the Cloud Platform environment
serviceAccountName: cd-serviceaccount
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.app.deployment.image.repository }}:{{ .Values.app.deployment.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: IfNotPresent
env:
- name: POSTGRES_USER
value: {{ .Values.app.deployment.env.POSTGRES_USER | quote }}
- name: POSTGRES_PASSWORD
value:
{{ .Values.app.deployment.env.POSTGRES_PASSWORD | quote }}
- name: POSTGRES_DB
value: {{ .Values.app.deployment.env.POSTGRES_DB | quote }}
- name: POSTGRES_HOST
value: {{ .Values.app.deployment.env.POSTGRES_HOST | quote }}
- name: POSTGRES_PORT
value: {{ .Values.app.deployment.env.POSTGRES_PORT | quote }}

ports:
- name: http
containerPort: 80
29 changes: 29 additions & 0 deletions helm/kpi-dashboard/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{- $fullName := include "kpi-dashboard.fullname" . -}}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ $fullName }}
labels:
{{- include "kpi-dashboard.labels" . | nindent 4 }}
annotations:
external-dns.alpha.kubernetes.io/set-identifier: {{ $fullName }}-{{ $fullName }}-green
external-dns.alpha.kubernetes.io/aws-weight: "100"
cloud-platform.justice.gov.uk/ignore-external-dns-weight: "true"
spec:
ingressClassName: "default"
tls:
- hosts:
- {{ .Values.app.ingress.host }}
rules:
- host: {{ .Values.app.ingress.host }}
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
service:
name: {{ $fullName }}
port:
number: 80

11 changes: 11 additions & 0 deletions helm/kpi-dashboard/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "kpi-dashboard.fullname" . }}
labels: {{- include "kpi-dashboard.labels" . | nindent 4 }}
spec:
ports:
- port: 80
targetPort: 4567
name: https
selector: {{- include "kpi-dashboard.selectorLabels" . | nindent 4 }}
8 changes: 8 additions & 0 deletions helm/kpi-dashboard/values-poc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
app:
ingress:
host: "kpi-dashboard-poc.cloud-platform.service.justice.gov.uk"

deployment:
replicaCount: 1
env:
LOGGING_LEVEL: INFO