Skip to content

Commit

Permalink
feat: added Helm chart
Browse files Browse the repository at this point in the history
  • Loading branch information
le0m committed Aug 28, 2024
1 parent d90b5d1 commit 0435f97
Show file tree
Hide file tree
Showing 16 changed files with 557 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ updates:
patterns: ['golangci/*']
release:
patterns: ['softprops/*']
azure:
patterns: ['azure/*']
helm:
patterns: ['helm/*']
10 changes: 10 additions & 0 deletions .github/values.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
autoscaling:
enabled: true

ingress:
enabled: true
hosts:
- host: example.com
paths:
- path: /
pathType: ImplementationSpecific
46 changes: 46 additions & 0 deletions .github/workflows/helm-chart.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Helm chart

on:
push:
branches: [ main ]
paths:
- 'chart/**'
pull_request:
branches: [ main ]
paths:
- 'chart/**'

jobs:
lint:
name: Run linter
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Helm
uses: azure/setup-helm@v4

- name: Lint
run: helm lint chart

validation:
name: Validate manifests
runs-on: ubuntu-latest
strategy:
matrix:
version: ['1.29.8', '1.30.4', '1.31.0']

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Helm
uses: azure/setup-helm@v4

- name: Install plugin
run: helm plugin install https://github.com/jtyr/kubeconform-helm

- name: Validate
run: helm kubeconform --summary -f .github/values.yml chart
34 changes: 29 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,13 @@ jobs:
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/chialab/print2pdf
ghcr.io/chialab/print2pdf-go/${{ matrix.module }}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
flavor: |
suffix=-${{ matrix.module }}
latest=false
- name: Login to GitHub Packages
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ghcr.io
Expand All @@ -122,3 +118,31 @@ jobs:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

release-chart:
name: Package and release Helm chart
if: ${{ startsWith(github.ref_name, 'v') }}
runs-on: ubuntu-latest
timeout-minutes: 5

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Login to GitHub Packages
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Helm
uses: azure/setup-helm@v4

- name: Package chart
run: helm package chart

- name: Publish chart
run: |
version="$(grep 'version:' ./chart/Chart.yaml | tail -n1 | awk '{ print $2 }')"
helm push "print2pdf-go-${version}.tgz" oci://ghcr.io/chialab/helm-charts
6 changes: 2 additions & 4 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ on:
push:
branches: [ main ]
paths:
- '**.tf'
- '**.tfpl'
- 'terraform/**'
pull_request:
branches: [ main ]
paths:
- '**.tf'
- '**.tfpl'
- 'terraform/**'

jobs:
format:
Expand Down
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ for the available tags.

The `plain` image can be used like this:
```shell
docker run --rm -it -p '3000:3000' -e 'BUCKET=mybucket' ghcr.io/chialab/print2pdf:0.1-plain
docker run --rm -it -p '3000:3000' -e 'BUCKET=mybucket' ghcr.io/chialab/print2pdf-go/plain:0.1
```

The `lambda` image can be used locally like this:
```shell
docker run --rm -it -p '8080:8080' -e 'BUCKET=mybucket' --entrypoint '/usr/local/bin/aws-lambda-rie' ghcr.io/chialab/print2pdf:0.1-lambda "/app/print2pdf"
docker run --rm -it -p '8080:8080' -e 'BUCKET=mybucket' --entrypoint '/usr/local/bin/aws-lambda-rie' ghcr.io/chialab/print2pdf-go/lambda:0.1 "/app/print2pdf"
```

The image is based on [lambda/provided](https://gallery.ecr.aws/lambda/provided), so it comes with [Lambda RIE](https://github.com/aws/aws-lambda-runtime-interface-emulator/)
Expand All @@ -87,11 +87,24 @@ for more informations on how to deploy Lambda functions using container images.
Since it is expected to be run behind an API Gateway, to be used locally the request body for the `/print` endpoint must be converted
to a JSON string and used as value of a `body` parameter. Also, the actual endpoint to call is `http://localhost:8080/2015-03-31/functions/function/invocations`.

A Terraform module is also provided in the `terraform` directory for convenience, it can be used to setup the AWS infrastructure
### Terraform module

A Terraform module is provided in the `terraform` directory for convenience, it can be used to setup the AWS infrastructure
needed to deploy the image as a Lambda function:
```terraform
module "lambda" {
source = "git::ssh://[email protected]:chialab/print2pdf-go.git//terraform"
...
}
```

**NOTE:** you will need to build and push the Docker image to the created ECR repository before creating the Lambda function.

### Helm chart

An Helm chart is provided in the `chart` directory for deploying the `plain` application in Kubernetes, and is distributed using GitHub's OCI container registry.

The chart repo URL is `oci://ghcr.io/chialab/helm-charts/print2pdf-go`. Usage example:
```shell
helm install example-release oci://ghcr.io/chialab/helm-charts/print2pdf-go --namespace example-ns --values example.yml --version ~0.1.0
```
23 changes: 23 additions & 0 deletions chart/.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/
10 changes: 10 additions & 0 deletions chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v2
name: print2pdf-go
description: A Helm chart for deploying Print2PDF
type: application
sources:
- https://github.com/chialab/print2pdf-go
maintainers:
- name: Chialab
version: 0.1.0
appVersion: "0.1.9"
22 changes: 22 additions & 0 deletions chart/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range $host := .Values.ingress.hosts }}
{{- range .paths }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }}
{{- end }}
{{- end }}
{{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "print2pdf.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch its status by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "print2pdf.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "print2pdf.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "print2pdf.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT
{{- end }}
62 changes: 62 additions & 0 deletions chart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "print2pdf.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 "print2pdf.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 "print2pdf.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

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

{{/*
Selector labels
*/}}
{{- define "print2pdf.selectorLabels" -}}
app.kubernetes.io/name: {{ include "print2pdf.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "print2pdf.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "print2pdf.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
91 changes: 91 additions & 0 deletions chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "print2pdf.fullname" . }}
labels:
{{- include "print2pdf.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "print2pdf.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "print2pdf.labels" . | nindent 8 }}
{{- with .Values.podLabels }}
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "print2pdf.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.registry }}/{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: {{ .Values.service.containerPort }}
protocol: TCP
{{- with .Values.podEnvironment }}
env:
{{- range $key, $env := . }}
- name: {{ $key | quote }}
{{- if $env.value }}
value: {{ $env.value | quote }}
{{- else }}
valueFrom:
{{- if $env.configMap }}
configMapKeyRef:
name: {{ $env.configMap.name | quote }}
key: {{ $env.configMap.key | quote }}
optional: {{ $env.configMap.optional | default false }}
{{- end }}
{{- if $env.secret }}
secretKeyRef:
name: {{ $env.secret.name | quote }}
key: {{ $env.secret.key | quote }}
optional: {{ $env.secret.optional | default false }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
livenessProbe:
{{- toYaml .Values.livenessProbe | nindent 12 }}
readinessProbe:
{{- toYaml .Values.readinessProbe | nindent 12 }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.volumeMounts }}
volumeMounts:
{{- toYaml . | nindent 12 }}
{{- end }}
{{- with .Values.volumes }}
volumes:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
Loading

0 comments on commit 0435f97

Please sign in to comment.