Skip to content

Commit

Permalink
Merge pull request #80 from ropable/master
Browse files Browse the repository at this point in the history
Update Kustomize definitions, update project dependency versions
  • Loading branch information
ropable authored Apr 18, 2024
2 parents 2f59103 + 6cd8cd1 commit addf97d
Show file tree
Hide file tree
Showing 18 changed files with 587 additions and 541 deletions.
35 changes: 32 additions & 3 deletions .github/workflows/image-build-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,67 @@ jobs:
packages: write
security-events: write
steps:
#----------------------------------------------
# Checkout repo
#----------------------------------------------
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
#----------------------------------------------
# Set up Docker BuildX environment
#----------------------------------------------
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
#----------------------------------------------
# Log Docker into the GitHub Container Repository
#----------------------------------------------
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
#----------------------------------------------
# Extract Docker image metadata from GitHub events
#----------------------------------------------
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
flavor: |
latest=true
#----------------------------------------------
# Build and push Docker image (not on PR)
#----------------------------------------------
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
scan:
name: Image vulnerability scan
runs-on: ubuntu-latest
needs: [build]
permissions:
contents: read
packages: read
security-events: write
steps:
#----------------------------------------------
# Run vulnerability scan on built image
#----------------------------------------------
- name: Run Trivy vuln scanner on Docker image
uses: aquasecurity/trivy-action@master
with:
image-ref: '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}'
ignore-unfixed: true
scan-type: 'image'
image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
vuln-type: 'os,library'
severity: 'CRITICAL,HIGH'
severity: 'HIGH,CRITICAL'
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
Expand Down
24 changes: 14 additions & 10 deletions csw/middleware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from django.db import connections
from django.http import HttpResponse, HttpResponseServerError
import logging


LOGGER = logging.getLogger("django")


class HealthCheckMiddleware(object):
Expand All @@ -8,9 +13,9 @@ def __init__(self, get_response):

def __call__(self, request):
if request.method == "GET":
if request.path == "/readiness":
if request.path == "/readyz":
return self.readiness(request)
elif request.path == "/liveness":
elif request.path == "/livez":
return self.liveness(request)
return self.get_response(request)

Expand All @@ -25,14 +30,13 @@ def readiness(self, request):
being present.
"""
try:
from django.db import connections
for name in connections:
cursor = connections[name].cursor()
cursor.execute("SELECT 1;")
row = cursor.fetchone()
if row is None:
return HttpResponseServerError("db: invalid response")
cursor = connections["default"].cursor()
cursor.execute("SELECT 1;")
row = cursor.fetchone()
if row is None:
return HttpResponseServerError("Database: invalid response")
except Exception as e:
return HttpResponseServerError("db: cannot connect to database.")
LOGGER.exception(e)
return HttpResponseServerError("Database: unable to connect")

return HttpResponse("OK")
2 changes: 1 addition & 1 deletion csw/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
CSRF_COOKIE_HTTPONLY = env('CSRF_COOKIE_HTTPONLY', False)
SESSION_COOKIE_SECURE = env('SESSION_COOKIE_SECURE', False)
if not DEBUG:
ALLOWED_HOSTS = env('ALLOWED_DOMAINS', '').split(',')
ALLOWED_HOSTS = env('ALLOWED_HOSTS', 'localhost').split(',')
else:
ALLOWED_HOSTS = ['*']
INTERNAL_IPS = ['127.0.0.1', '::1']
Expand Down
43 changes: 31 additions & 12 deletions kustomize/base/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: csw-deployment
labels:
app: csw-deployment
spec:
replicas: 2
strategy:
type: RollingUpdate
selector:
matchLabels:
app: csw-deployment
template:
metadata:
labels:
app: csw-deployment
spec:
containers:
- name: csw
image: ghcr.io/dbca-wa/csw
imagePullPolicy: Always
env:
- name: ALLOWED_DOMAINS
- name: ALLOWED_HOSTS
value: ".dbca.wa.gov.au"
- name: CSRF_COOKIE_SECURE
value: "True"
Expand All @@ -24,28 +31,40 @@ spec:
resources:
requests:
memory: "128Mi"
cpu: "25m"
cpu: "10m"
limits:
memory: "4096Mi"
memory: "2048Mi"
cpu: "1000m"
livenessProbe:
startupProbe:
httpGet:
path: /liveness
path: /livez
port: 8080
scheme: HTTP
initialDelaySeconds: 3
periodSeconds: 3
periodSeconds: 15
timeoutSeconds: 10
successThreshold: 1
failureThreshold: 3
timeoutSeconds: 2
livenessProbe:
httpGet:
path: /livez
port: 8080
scheme: HTTP
initialDelaySeconds: 0
periodSeconds: 15
successThreshold: 1
failureThreshold: 3
timeoutSeconds: 10
readinessProbe:
httpGet:
path: /readiness
path: /readyz
port: 8080
scheme: HTTP
initialDelaySeconds: 3
periodSeconds: 3
initialDelaySeconds: 0
periodSeconds: 15
successThreshold: 1
failureThreshold: 3
timeoutSeconds: 2
timeoutSeconds: 10
securityContext:
runAsNonRoot: true
privileged: false
Expand Down
17 changes: 17 additions & 0 deletions kustomize/base/deployment_hpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: csw-deployment-hpa
spec:
minReplicas: 1
maxReplicas: 3
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
metrics:
- resource:
name: cpu
target:
type: Utilization
averageUtilization: 250
type: Resource
3 changes: 3 additions & 0 deletions kustomize/base/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- deployment_hpa.yaml
- service.yaml
7 changes: 7 additions & 0 deletions kustomize/overlays/prod/deployment_hpa_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: csw-deployment-hpa
spec:
scaleTargetRef:
name: csw-deployment-prod
8 changes: 0 additions & 8 deletions kustomize/overlays/prod/deployment_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: csw-deployment
labels:
app: csw-prod
spec:
selector:
matchLabels:
app: csw-prod
template:
metadata:
labels:
app: csw-prod
spec:
containers:
- name: csw
Expand Down
15 changes: 8 additions & 7 deletions kustomize/overlays/prod/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
nameSuffix: -prod
resources:
- ../../base
- ingress.yaml
- pdb.yaml
secretGenerator:
- name: csw-env
type: Opaque
envs:
- .env
resources:
- ../../base
- ingress.yaml
- pdb.yaml
labels:
- includeSelectors: true
pairs:
variant: prod
images:
- name: ghcr.io/dbca-wa/csw
newTag: 1.3.9
patches:
- path: deployment_patch.yaml
- path: deployment_hpa_patch.yaml
- path: service_patch.yaml
images:
- name: ghcr.io/dbca-wa/csw
newTag: 1.3.10
2 changes: 1 addition & 1 deletion kustomize/overlays/prod/pdb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ spec:
minAvailable: 1
selector:
matchLabels:
app: csw-prod
app: csw-deployment
variant: prod
2 changes: 1 addition & 1 deletion kustomize/overlays/prod/service_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ metadata:
spec:
type: ClusterIP
selector:
app: csw-prod
app: csw-deployment
variant: prod
7 changes: 7 additions & 0 deletions kustomize/overlays/uat/deployment_hpa_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: csw-deployment-hpa
spec:
scaleTargetRef:
name: csw-deployment-uat
8 changes: 0 additions & 8 deletions kustomize/overlays/uat/deployment_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: csw-deployment
labels:
app: csw-uat
spec:
selector:
matchLabels:
app: csw-uat
template:
metadata:
labels:
app: csw-uat
spec:
containers:
- name: csw
Expand Down
9 changes: 5 additions & 4 deletions kustomize/overlays/uat/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
nameSuffix: -uat
resources:
- ../../base
- ingress.yaml
- pdb.yaml
secretGenerator:
- name: csw-env
type: Opaque
envs:
- .env
resources:
- ../../base
- ingress.yaml
- pdb.yaml
labels:
- includeSelectors: true
pairs:
variant: uat
patches:
- path: deployment_patch.yaml
- path: deployment_hpa_patch.yaml
- path: service_patch.yaml
2 changes: 1 addition & 1 deletion kustomize/overlays/uat/pdb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ spec:
minAvailable: 1
selector:
matchLabels:
app: csw-uat
app: csw-deployment
variant: uat
2 changes: 1 addition & 1 deletion kustomize/overlays/uat/service_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ metadata:
spec:
type: ClusterIP
selector:
app: csw-uat
app: csw-deployment
variant: uat
Loading

0 comments on commit addf97d

Please sign in to comment.