Skip to content

Commit 8beeedc

Browse files
committed
feat: Add caching
- Fixed code lints - Refactored the code into packages - Add caching - Add a helm chart to deploy to k8s - Fixes #7 Signed-off-by: Luis Davim <[email protected]>
1 parent 7914025 commit 8beeedc

27 files changed

+1540
-330
lines changed

.dockerignore

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# The .dockerignore file excludes files from the container build process.
2+
#
3+
# https://docs.docker.com/engine/reference/builder/#dockerignore-file
4+
5+
# Exclude locally vendored dependencies.
6+
vendor/
7+
8+
# Binaries for programs and plugins
9+
charts/
10+
bin/
11+
out/
12+
dist/
13+
docs/
14+
usage/
15+
imgs/
16+
17+
# local editor configs
18+
.project
19+
.idea/
20+
.idea
21+
*/*.iml
22+
.vscode/
23+
24+
# Irelevant files
25+
**/*.md
26+
*.test
27+
*.out
28+
docs.go
29+
30+
k8s_schemas/*
31+
openapi2jsonschema.py
32+
openapi2jsonschema.py.original
33+
requirements.txt
34+
aws-s3-reverse-proxy
35+
36+
# ignore .git and .cache folders
37+
.git
38+
.github
39+
.cache
40+
41+
# Exclude "build-time" ignore files.
42+
.dockerignore
43+
.gcloudignore
44+
.yamllint
45+
.goreleaser.yml
46+
47+
*.cover
48+
49+
# Exclude git history and configuration.
50+
.gitignore

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
aws-s3-reverse-proxy
2+
!aws-s3-reverse-proxy/
23
aws-s3-reverse-proxy.tar
34
config
45
test.txt
6+
cache.d/

Dockerfile

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
FROM golang:alpine as build
1+
FROM golang:1.20-alpine as build
22

33
RUN apk add -U --no-cache ca-certificates git bash
44

5+
ENV GOPATH=""
6+
57
WORKDIR /app
6-
COPY . .
78

8-
RUN go build -o aws-s3-reverse-proxy && \
9-
mv ./aws-s3-reverse-proxy /go/bin
9+
COPY go.mod go.sum ./
10+
RUN go mod download
11+
12+
COPY pkg ./pkg
13+
COPY main.go ./
14+
15+
RUN CGO_ENABLED=0 GOOS=linux go build -o /go/bin/aws-s3-reverse-proxy -trimpath -ldflags="-s -w -extldflags '-static'"
1016

11-
FROM alpine:3.13
17+
FROM alpine:3.18
1218

1319
WORKDIR /proxy
1420

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
.bzr/
9+
.bzrignore
10+
.hg/
11+
.hgignore
12+
.svn/
13+
# Common backup files
14+
*.swp
15+
*.bak
16+
*.tmp
17+
*.orig
18+
*~
19+
# Various IDEs
20+
.project
21+
.idea/
22+
*.tmproj
23+
.vscode/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: v2
2+
name: aws-s3-reverse-proxy
3+
description: A Helm chart for Kubernetes
4+
5+
# A chart can be either an 'application' or a 'library' chart.
6+
#
7+
# Application charts are a collection of templates that can be packaged into versioned archives
8+
# to be deployed.
9+
#
10+
# Library charts provide useful utilities or functions for the chart developer. They're included as
11+
# a dependency of application charts to inject those utilities and functions into the rendering
12+
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
13+
type: application
14+
15+
# This is the chart version. This version number should be incremented each time you make changes
16+
# to the chart and its templates, including the app version.
17+
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18+
version: 0.1.0
19+
20+
# This is the version number of the application being deployed. This version number should be
21+
# incremented each time you make changes to the application. Versions are not expected to
22+
# follow Semantic Versioning. They should reflect the version the application is using.
23+
# It is recommended to use it with quotes.
24+
appVersion: "1.1.0"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
1. Get the application URL by running these commands:
2+
{{- if .Values.ingress.enabled }}
3+
{{- range $host := .Values.ingress.hosts }}
4+
{{- range .paths }}
5+
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }}
6+
{{- end }}
7+
{{- end }}
8+
{{- else if contains "NodePort" .Values.service.type }}
9+
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "aws-s3-reverse-proxy.fullname" . }})
10+
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
11+
echo http://$NODE_IP:$NODE_PORT
12+
{{- else if contains "LoadBalancer" .Values.service.type }}
13+
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
14+
You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "aws-s3-reverse-proxy.fullname" . }}'
15+
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "aws-s3-reverse-proxy.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
16+
echo http://$SERVICE_IP:{{ .Values.service.port }}
17+
{{- else if contains "ClusterIP" .Values.service.type }}
18+
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "aws-s3-reverse-proxy.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
19+
export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
20+
echo "Visit http://127.0.0.1:8080 to use your application"
21+
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT
22+
{{- end }}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{{/*
2+
Expand the name of the chart.
3+
*/}}
4+
{{- define "aws-s3-reverse-proxy.name" -}}
5+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
6+
{{- end }}
7+
8+
{{/*
9+
Create a default fully qualified app name.
10+
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
11+
If release name contains chart name it will be used as a full name.
12+
*/}}
13+
{{- define "aws-s3-reverse-proxy.fullname" -}}
14+
{{- if .Values.fullnameOverride }}
15+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
16+
{{- else }}
17+
{{- $name := default .Chart.Name .Values.nameOverride }}
18+
{{- if contains $name .Release.Name }}
19+
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
20+
{{- else }}
21+
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
22+
{{- end }}
23+
{{- end }}
24+
{{- end }}
25+
26+
{{/*
27+
Create chart name and version as used by the chart label.
28+
*/}}
29+
{{- define "aws-s3-reverse-proxy.chart" -}}
30+
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
31+
{{- end }}
32+
33+
{{/*
34+
Common labels
35+
*/}}
36+
{{- define "aws-s3-reverse-proxy.labels" -}}
37+
helm.sh/chart: {{ include "aws-s3-reverse-proxy.chart" . }}
38+
{{ include "aws-s3-reverse-proxy.selectorLabels" . }}
39+
{{- if .Chart.AppVersion }}
40+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
41+
{{- end }}
42+
app.kubernetes.io/managed-by: {{ .Release.Service }}
43+
{{- end }}
44+
45+
{{/*
46+
Selector labels
47+
*/}}
48+
{{- define "aws-s3-reverse-proxy.selectorLabels" -}}
49+
app.kubernetes.io/name: {{ include "aws-s3-reverse-proxy.name" . }}
50+
app.kubernetes.io/instance: {{ .Release.Name }}
51+
{{- end }}
52+
53+
{{/*
54+
Create the name of the service account to use
55+
*/}}
56+
{{- define "aws-s3-reverse-proxy.serviceAccountName" -}}
57+
{{- if .Values.serviceAccount.create }}
58+
{{- default (include "aws-s3-reverse-proxy.fullname" .) .Values.serviceAccount.name }}
59+
{{- else }}
60+
{{- default "default" .Values.serviceAccount.name }}
61+
{{- end }}
62+
{{- end }}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{{- if .Values.certManager.enabled }}
2+
{{- if not .Values.certManager.issuer }}
3+
apiVersion: cert-manager.io/v1
4+
kind: Issuer
5+
metadata:
6+
name: {{ include "aws-s3-reverse-proxy.fullname" . }}-selfsigned
7+
spec:
8+
selfSigned: {}
9+
{{- end }}
10+
---
11+
apiVersion: cert-manager.io/v1
12+
kind: Certificate
13+
metadata:
14+
name: {{ include "aws-s3-reverse-proxy.fullname" . }}
15+
labels:
16+
{{- include "aws-s3-reverse-proxy.labels" . | nindent 4 }}
17+
app: aws-s3-reverse-proxy
18+
spec:
19+
secretName: {{ include "aws-s3-reverse-proxy.fullname" . }}-certificate
20+
dnsNames: {{ tpl (toYaml .Values.certManager.dnsNames) $ | nindent 4 }}
21+
issuerRef:
22+
name: {{ .Values.certManager.issuer | default ( printf "%s-selfsigned" (include "aws-s3-reverse-proxy.fullname" .)) }}
23+
kind: {{ .Values.certManager.issuerKind | default "Issuer" }}
24+
{{- end }}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{{- if .Values.autoscaling.enabled }}
2+
apiVersion: autoscaling/v2beta1
3+
kind: HorizontalPodAutoscaler
4+
metadata:
5+
name: {{ include "aws-s3-reverse-proxy.fullname" . }}
6+
labels:
7+
{{- include "aws-s3-reverse-proxy.labels" . | nindent 4 }}
8+
spec:
9+
scaleTargetRef:
10+
apiVersion: apps/v1
11+
kind: Deployment
12+
name: {{ include "aws-s3-reverse-proxy.fullname" . }}
13+
minReplicas: {{ .Values.autoscaling.minReplicas }}
14+
maxReplicas: {{ .Values.autoscaling.maxReplicas }}
15+
metrics:
16+
{{- if .Values.autoscaling.targetCPUUtilizationPercentage }}
17+
- type: Resource
18+
resource:
19+
name: cpu
20+
targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }}
21+
{{- end }}
22+
{{- if .Values.autoscaling.targetMemoryUtilizationPercentage }}
23+
- type: Resource
24+
resource:
25+
name: memory
26+
targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }}
27+
{{- end }}
28+
{{- end }}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{{- if .Values.ingress.enabled -}}
2+
{{- $fullName := include "aws-s3-reverse-proxy.fullname" . -}}
3+
{{- $svcPort := .Values.service.port -}}
4+
{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }}
5+
{{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }}
6+
{{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}}
7+
{{- end }}
8+
{{- end }}
9+
{{- if .Values.certManager.enabled }}
10+
{{- if not (hasKey .Values.ingress.annotations "cert-manager.io/issuer") }}
11+
{{- $_ := set .Values.ingress.annotations "cert-manager.io/issuer" .Values.certManager.issuer }}
12+
{{- end }}
13+
{{- end }}
14+
{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}}
15+
apiVersion: networking.k8s.io/v1
16+
{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
17+
apiVersion: networking.k8s.io/v1beta1
18+
{{- else -}}
19+
apiVersion: extensions/v1beta1
20+
{{- end }}
21+
kind: Ingress
22+
metadata:
23+
name: {{ $fullName }}
24+
labels:
25+
{{- include "aws-s3-reverse-proxy.labels" . | nindent 4 }}
26+
{{- with .Values.ingress.annotations }}
27+
annotations:
28+
{{- toYaml . | nindent 4 }}
29+
{{- end }}
30+
spec:
31+
{{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }}
32+
ingressClassName: {{ .Values.ingress.className }}
33+
{{- end }}
34+
{{- if .Values.ingress.tls }}
35+
tls:
36+
{{- range .Values.ingress.tls }}
37+
- hosts:
38+
{{- range .hosts }}
39+
- {{ . | quote }}
40+
{{- end }}
41+
secretName: {{ .secretName }}
42+
{{- end }}
43+
{{- end }}
44+
rules:
45+
{{- range .Values.ingress.hosts }}
46+
- host: {{ .host | quote }}
47+
http:
48+
paths:
49+
{{- range .paths }}
50+
- path: {{ .path }}
51+
{{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }}
52+
pathType: {{ .pathType }}
53+
{{- end }}
54+
backend:
55+
{{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }}
56+
service:
57+
name: {{ $fullName }}
58+
port:
59+
number: {{ $svcPort }}
60+
{{- else }}
61+
serviceName: {{ $fullName }}
62+
servicePort: {{ $svcPort }}
63+
{{- end }}
64+
{{- end }}
65+
{{- end }}
66+
{{- end }}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: {{ include "aws-s3-reverse-proxy.fullname" . }}
5+
labels:
6+
{{- include "aws-s3-reverse-proxy.labels" . | nindent 4 }}
7+
spec:
8+
type: {{ .Values.service.type }}
9+
ports:
10+
- port: {{ .Values.service.port }}
11+
targetPort: http
12+
protocol: TCP
13+
name: http
14+
- port: {{ .Values.metrics.port }}
15+
targetPort: metrics
16+
protocol: TCP
17+
name: metrics
18+
- port: {{ .Values.healthz.port }}
19+
targetPort: healthz
20+
protocol: TCP
21+
name: healthz
22+
selector:
23+
{{- include "aws-s3-reverse-proxy.selectorLabels" . | nindent 4 }}
24+
---
25+
apiVersion: v1
26+
kind: Service
27+
metadata:
28+
name: {{ include "aws-s3-reverse-proxy.fullname" . }}-headless
29+
labels:
30+
{{- include "aws-s3-reverse-proxy.labels" . | nindent 4 }}
31+
spec:
32+
type: {{ .Values.service.type }}
33+
ports:
34+
- port: {{ .Values.service.port }}
35+
targetPort: http
36+
protocol: TCP
37+
name: http
38+
- port: {{ .Values.metrics.port }}
39+
targetPort: metrics
40+
protocol: TCP
41+
name: metrics
42+
- port: {{ .Values.healthz.port }}
43+
targetPort: healthz
44+
protocol: TCP
45+
name: healthz
46+
clusterIP: None
47+
selector:
48+
{{- include "aws-s3-reverse-proxy.selectorLabels" . | nindent 4 }}

0 commit comments

Comments
 (0)