Added k8s and .gitlab-ci config

This commit is contained in:
Daniel F 2023-07-11 23:28:47 +01:00
parent 4ce18938a5
commit 9e3fe730af
13 changed files with 528 additions and 0 deletions

81
go/.gitlab-ci.yml Normal file
View File

@ -0,0 +1,81 @@
---
stages:
- build
- package
- acceptance
- performance
build:
stage: build
image: golang:1.19.2-bullseye
script:
- go install github.com/swaggo/swag/cmd/swag@v1.16.1
- swag init
- go build -a -o target/bin/gilded-rose ./main.go
artifacts:
paths:
- target/bin/gilded-rose
test:
stage: build
image: golang:1.19.2-bullseye
script:
- go install github.com/swaggo/swag/cmd/swag@v1.16.1
- swag init
- go test ./... -v -coverprofile=coverage.out -covermode count
- go install github.com/boumenot/gocover-cobertura@v1.2.0
- $GOPATH/bin/gocover-cobertura < coverage.out > coverage.xml
coverage: /^coverage:\s(\d+(?:\.\d+)?%)/
artifacts:
name: $CI_JOB_NAME/coverage.txt
paths:
- coverage.xml
expire_in: 2 days
reports:
coverage_report:
coverage_format: cobertura
path: coverage.xml
dockerize:
stage: package
dependencies: []
image:
name: gcr.io/kaniko-project/executor:v1.9.0-debug
entrypoint: [""]
script:
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_JOB_TOKEN\"}}}" > /kaniko/.docker/config.json
- |
/kaniko/executor \
--cache=true \
--cache-ttl=720h \
--cache-repo "$CI_REGISTRY_IMAGE/cache" \
--snapshotMode full \
--context "$CI_PROJECT_DIR" \
--dockerfile "$CI_PROJECT_DIR/Dockerfile" \
--destination "$CI_REGISTRY_IMAGE:${CI_COMMIT_SHORT_SHA}" \
--destination "$CI_REGISTRY_IMAGE:${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA}" \
--destination "$CI_REGISTRY_IMAGE:${CI_COMMIT_REF_SLUG}"
helm-package:
stage: package
image:
name: alpine/helm:3.9.2
entrypoint: [""]
variables:
CHART: "gilded-rose"
ENVIRONMENT_DEPLOY_HOST: "$CI_COMMIT_REF_SLUG.$CI_PROJECT_NAME.k8sdev.example.int"
before_script:
- apk add git gettext
- helm plugin install --version=v0.10.3 https://github.com/chartmuseum/helm-push.git
- >
helm repo add ${CHART}
--username ${CI_REGISTRY_USER}
--password ${CI_JOB_TOKEN}
${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/helm/stable
script:
- cd k8s/charts
- cat ${CHART}/Chart.yaml | sed 's/\\\$/{_DOLLAR_}/g' | envsubst | sed 's/{_DOLLAR_}/$/g' > ${CHART}/Chart.prod.yaml && mv ${CHART}/Chart.prod.yaml ${CHART}/Chart.yaml
- cat ${CHART}/values.yaml | sed 's/\\\$/{_DOLLAR_}/g' | envsubst | sed 's/{_DOLLAR_}/$/g' > ${CHART}/values.prod.yaml && mv ${CHART}/values.prod.yaml ${CHART}/values.yaml
- helm cm-push ${CHART}/ ${CHART}

View File

@ -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/

View File

@ -0,0 +1,24 @@
apiVersion: v2
name: gilded-rose
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.$CI_PIPELINE_IID
# 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: "0.0.0"

View File

@ -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.backend.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "gilded-rose.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.backend.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "gilded-rose.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "gilded-rose.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
echo http://$SERVICE_IP:{{ .Values.backend.service.port }}
{{- else if contains "ClusterIP" .Values.backend.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "gilded-rose.name" . }},app.kubernetes.io/instance={{ .Release.Name }},service=backend" -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 }}

View File

@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "gilded-rose.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 "gilded-rose.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 "gilded-rose.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}
{{/*
Common labels
*/}}
{{- define "gilded-rose.labels" -}}
helm.sh/chart: {{ include "gilded-rose.chart" . }}
{{ include "gilded-rose.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
{{/*
Selector labels
*/}}
{{- define "gilded-rose.selectorLabels" -}}
app.kubernetes.io/name: {{ include "gilded-rose.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{/*
Create the name of the service account to use
*/}}
{{- define "gilded-rose.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "gilded-rose.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

View File

@ -0,0 +1,95 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "gilded-rose.fullname" . }}
labels:
{{- include "gilded-rose.labels" . | nindent 4 }}
service: backend
spec:
{{- if not .Values.backend.autoscaling.enabled }}
replicas: {{ .Values.backend.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "gilded-rose.selectorLabels" . | nindent 6 }}
service: backend
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
{{- with .Values.backend.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "gilded-rose.selectorLabels" . | nindent 8 }}
service: backend
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "gilded-rose.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.backend.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.backend.securityContext | nindent 12 }}
image: "{{ .Values.backend.image.repository }}:{{ .Values.backend.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.backend.image.pullPolicy }}
ports:
- name: http
containerPort: 8080
protocol: TCP
livenessProbe:
httpGet:
path: /status
port: http
readinessProbe:
httpGet:
path: /status
port: http
startupProbe:
httpGet:
path: /status
port: http
{{- with .Values.backend.volumeMounts }}
volumeMounts:
{{- toYaml . | nindent 12 }}
{{- end }}
resources:
{{- toYaml .Values.backend.resources | nindent 12 }}
env:
{{- toYaml .Values.backend.env | nindent 12 }}
- name: SERVER_PORT
value: "8080"
{{- with .Values.backend.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.backend.volumes }}
volumes:
{{- toYaml . | nindent 8 }}
{{- end}}
{{- with .Values.backend.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.backend.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
terminationGracePeriodSeconds: 10
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
#topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
{{- include "gilded-rose.selectorLabels" . | nindent 12 }}
service: backend

View File

@ -0,0 +1,33 @@
{{- if .Values.backend.autoscaling.enabled }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: {{ include "gilded-rose.fullname" . }}
labels:
{{- include "gilded-rose.labels" . | nindent 4 }}
service: backend
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ include "gilded-rose.fullname" . }}
minReplicas: {{ .Values.backend.autoscaling.minReplicas }}
maxReplicas: {{ .Values.backend.autoscaling.maxReplicas }}
metrics:
{{- if .Values.backend.autoscaling.targetCPUUtilizationPercentage }}
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: {{ .Values.backend.autoscaling.targetCPUUtilizationPercentage }}
{{- end }}
{{- if .Values.backend.autoscaling.targetMemoryUtilizationPercentage }}
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: {{ .Values.backend.autoscaling.targetMemoryUtilizationPercentage }}
{{- end }}
{{- end }}

View File

@ -0,0 +1,47 @@
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "gilded-rose.fullname" . -}}
{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }}
{{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }}
{{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}}
{{- end }}
{{- end }}
{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1
{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}
kind: Ingress
metadata:
name: {{ $fullName }}
labels:
{{- include "gilded-rose.labels" . | nindent 4 }}
{{- with .Values.ingress.annotations }}
annotations:
{{- tpl (toYaml .) $ | nindent 4 }}
{{- end }}
spec:
{{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }}
ingressClassName: {{ .Values.ingress.className }}
{{- end }}
tls:
- hosts:
{{- range .Values.ingress.config.deployHosts }}
- {{ tpl . $ | quote }}
{{- end }}
secretName: {{ $fullName }}-tls
rules:
{{- range .Values.ingress.config.deployHosts }}
- host: {{ tpl . $ | quote }}
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: {{ $fullName }}
port:
name: http
{{- end}}
{{- end }}

View File

@ -0,0 +1,13 @@
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: {{ include "gilded-rose.fullname" . }}-pdb
labels:
{{- include "gilded-rose.labels" . | nindent 4 }}
service: backend
spec:
minAvailable: 1
selector:
matchLabels:
{{- include "gilded-rose.selectorLabels" . | nindent 6 }}
service: backend

View File

@ -0,0 +1,17 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "gilded-rose.fullname" . }}
labels:
{{- include "gilded-rose.labels" . | nindent 4 }}
service: backend
spec:
type: {{ .Values.backend.service.type }}
ports:
- port: {{ .Values.backend.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
{{- include "gilded-rose.selectorLabels" . | nindent 4 }}
service: backend

View File

@ -0,0 +1,11 @@
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
name: np-deny-other-ns
spec:
podSelector:
matchLabels:
role: internal
ingress:
- from:
- podSelector: {}

View File

@ -0,0 +1,15 @@
apiVersion: v1
kind: Pod
metadata:
name: "{{ include "gilded-rose.fullname" . }}-test-connection-backend"
labels:
{{- include "gilded-rose.labels" . | nindent 4 }}
annotations:
"helm.sh/hook": test
spec:
containers:
- name: wget
image: busybox
command: ['wget']
args: ['{{ include "gilded-rose.fullname" . }}:{{ .Values.backend.service.port }}/status']
restartPolicy: Never

View File

@ -0,0 +1,85 @@
# Default values for gilded-rose.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
serviceAccount:
# Specifies whether a service account should be created
create: false
# Annotations to add to the service account
annotations: {}
# The name of the service account to use.
# If not set and create is true, a name is generated using the fullname template
name: ""
backend:
replicaCount: 1
image:
repository: ${CI_REGISTRY_IMAGE}
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: "${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA}"
podAnnotations: {}
podSecurityContext:
fsGroup: 1000
fsGroupChangePolicy: Always
securityContext: {}
service:
type: ClusterIP
port: 8080
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
# resources, such as Minikube. If you do want to specify resources, uncomment the following
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "4000m"
memory: "1024Mi"
volumes: []
volumeMounts: []
autoscaling:
enabled: false
minReplicas: 3
maxReplicas: 10
targetCPUUtilizationPercentage: 500
targetMemoryUtilizationPercentage: 400
nodeSelector: {}
tolerations: []
affinity: {}
env: []
# Example:
# - name: TZ
# value: "Europe/Madrid"
ingress:
enabled: true
className: "nginx"
config:
tlsIssuer: "letsencrypt-issuer"
deployHosts:
- "gilded-rose.k8sdev.example.int"
annotations:
cert-manager.io/cluster-issuer: "{{ .Values.ingress.config.tlsIssuer }}"
nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
nginx.ingress.kubernetes.io/ssl-redirect: "false"