feat(infrastructure): automatic deploy

This commit is contained in:
2025-10-05 18:06:53 +02:00
parent 3a6ee3dace
commit 40131cf7ca
16 changed files with 565 additions and 54 deletions

View File

@@ -0,0 +1,6 @@
apiVersion: v2
name: myapp-chart
version: 0.1.0
description: Helm chart for my app with MariaDB Database CR
appVersion: "1.0.0"
type: application

View File

@@ -0,0 +1,54 @@
Thank you for installing myapp-chart.
This chart packages all Kubernetes manifests from the original deployment directory and parameterizes environment, database name (with optional PR suffix), image, and domain for external access.
Namespaces per developer (important):
- Install each developer's environment into their own namespace using Helm's -n/--namespace flag.
- No hardcoded namespace is used in templates; resources are created in .Release.Namespace.
- Example namespaces: dev-alice, dev-bob, pr-123, etc.
Key values:
- deployment -> used as Database CR name and DB username (MARIADB_DB and MARIADB_USER)
- image.repository/tag or image.digest -> container image
- domain -> public FQDN used by TunnelBinding (required to expose app)
- app/worker names, replicas, ports
Examples:
- Dev install (Alice):
helm upgrade --install myapp ./7project/charts/myapp-chart \
-n dev-alice --create-namespace \
-f values-dev.yaml \
--set domain=alice.demo.example.com \
--set-string rabbitmq.password="$RABBITMQ_PASSWORD" \
--set-string database.password="$DB_PASSWORD"
- Dev install (Bob):
helm upgrade --install myapp ./7project/charts/myapp-chart \
-n dev-bob --create-namespace \
-f values-dev.yaml \
--set domain=bob.demo.example.com
- Prod install (different cleanupPolicy):
helm upgrade --install myapp ./7project/charts/myapp-chart \
-n prod --create-namespace \
-f values-prod.yaml \
--set domain=app.example.com
- PR (preview) install with DB name containing PR number (also its own namespace):
PR=123
helm upgrade --install myapp-pr-$PR ./7project/charts/myapp-chart \
-n pr-$PR --create-namespace \
-f values-dev.yaml \
--set prNumber=$PR \
--set deployment=preview-$PR \
--set domain=pr-$PR.example.com
- Use a custom deployment identifier to suffix DB name, DB username and Secret name:
helm upgrade --install myapp ./7project/charts/myapp-chart \
-n dev-alice --create-namespace \
-f values-dev.yaml \
--set deployment=alice \
--set domain=alice.demo.example.com
Render locally (dry run):
helm template ./7project/charts/myapp-chart -f values-dev.yaml --set prNumber=456 --set deployment=test --set domain=demo.example.com --namespace dev-test | sed -n '/kind: Database/,$p' | head -n 30

View File

@@ -0,0 +1,49 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.app.name }}
spec:
replicas: {{ .Values.app.replicas }}
revisionHistoryLimit: 3
selector:
matchLabels:
app: {{ .Values.app.name }}
template:
metadata:
labels:
app: {{ .Values.app.name }}
spec:
containers:
- name: {{ .Values.app.name }}
image: "{{- if .Values.image.digest -}}{{ .Values.image.repository }}@{{ .Values.image.digest }}{{- else -}}{{ .Values.image.repository }}:{{ default "latest" .Values.image.tag }}{{- end -}}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- containerPort: {{ .Values.app.port }}
env:
- name: MARIADB_HOST
value: {{ printf "%s.%s.svc.cluster.local" .Values.mariadb.mariaDbRef.name .Values.mariadb.mariaDbRef.namespace | quote }}
- name: MARIADB_PORT
value: '3306'
- name: MARIADB_DB
value: {{ required "Set .Values.deployment" .Values.deployment | quote }}
- name: MARIADB_USER
value: {{ required "Set .Values.deployment" .Values.deployment | quote }}
- name: MARIADB_PASSWORD
valueFrom:
secretKeyRef:
name: {{ required "Set .Values.database.secretName" .Values.database.secretName }}
key: password
livenessProbe:
httpGet:
path: /
port: {{ .Values.app.port }}
initialDelaySeconds: 10
periodSeconds: 10
failureThreshold: 3
readinessProbe:
httpGet:
path: /
port: {{ .Values.app.port }}
initialDelaySeconds: 10
periodSeconds: 10
failureThreshold: 3

View File

@@ -0,0 +1,18 @@
apiVersion: k8s.mariadb.com/v1alpha1
kind: Grant
metadata:
name: grant
spec:
mariaDbRef:
name: {{ .Values.mariadb.mariaDbRef.name }}
namespace: {{ .Values.mariadb.mariaDbRef.namespace }}
privileges:
- "ALL PRIVILEGES"
database: {{ required "Set .Values.deployment" .Values.deployment | quote }}
table: "*"
username: {{ required "Set .Values.deployment" .Values.deployment | quote }}
grantOption: true
host: "%"
cleanupPolicy: {{ .Values.mariadb.cleanupPolicy }}
requeueInterval: {{ .Values.mariadb.requeueInterval | quote }}
retryInterval: {{ .Values.mariadb.retryInterval | quote }}

View File

@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: {{ required "Set .Values.database.secretName" .Values.database.secretName }}
type: kubernetes.io/basic-auth
stringData:
password: {{ required "Set .Values.database.password" .Values.database.password | quote }}

View File

@@ -0,0 +1,16 @@
apiVersion: k8s.mariadb.com/v1alpha1
kind: User
metadata:
name: {{ required "Set .Values.deployment" .Values.deployment }}
spec:
mariaDbRef:
name: {{ .Values.mariadb.mariaDbRef.name }}
namespace: {{ .Values.mariadb.mariaDbRef.namespace }}
passwordSecretKeyRef:
name: {{ required "Set .Values.database.secretName" .Values.database.secretName }}
key: password
maxUserConnections: 20
host: "%"
cleanupPolicy: {{ .Values.mariadb.cleanupPolicy }}
requeueInterval: {{ .Values.mariadb.requeueInterval | quote }}
retryInterval: {{ .Values.mariadb.retryInterval | quote }}

View File

@@ -0,0 +1,14 @@
apiVersion: k8s.mariadb.com/v1alpha1
kind: Database
metadata:
name: {{ required "Set .Values.deployment" .Values.deployment }}
spec:
mariaDbRef:
name: {{ .Values.mariadb.mariaDbRef.name | required "Values mariadb.mariaDbRef.name is required" }}
namespace: {{ .Values.mariadb.mariaDbRef.namespace | default .Release.Namespace }}
characterSet: utf8mb4
collate: utf8_general_ci
cleanupPolicy: {{ .Values.mariadb.cleanupPolicy }}
requeueInterval: {{ .Values.mariadb.requeueInterval | quote }}
retryInterval: {{ .Values.mariadb.retryInterval | quote }}

View File

@@ -0,0 +1,10 @@
apiVersion: v1
kind: Service
metadata:
name: {{ .Values.app.name }}
spec:
ports:
- port: {{ .Values.service.port }}
targetPort: {{ .Values.app.port }}
selector:
app: {{ .Values.app.name }}

View File

@@ -0,0 +1,14 @@
apiVersion: networking.cfargotunnel.com/v1alpha1
kind: TunnelBinding
metadata:
name: guestbook-tunnel-binding
namespace: {{ .Release.Namespace }}
subjects:
- name: app-server
spec:
target: {{ printf "http://%s.%s.svc.cluster.local" .Values.app.name .Release.Namespace | quote }}
fqdn: {{ required "Set .Values.domain via --set domain=example.com" .Values.domain | quote }}
noTlsVerify: true
tunnelRef:
kind: ClusterTunnel
name: cluster-tunnel

View File

@@ -0,0 +1,37 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.worker.name }}
spec:
replicas: {{ .Values.worker.replicas }}
revisionHistoryLimit: 3
selector:
matchLabels:
app: {{ .Values.worker.name }}
template:
metadata:
labels:
app: {{ .Values.worker.name }}
spec:
containers:
- name: {{ .Values.worker.name }}
image: "{{- if .Values.image.digest -}}{{ .Values.image.repository }}@{{ .Values.image.digest }}{{- else -}}{{ .Values.image.repository }}:{{ default "latest" .Values.image.tag }}{{- end -}}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command:
- celery
- -A
- app.celery_app
- worker
- -Q
- $(MAIL_QUEUE)
- --loglevel
- INFO
env:
- name: RABBITMQ_USERNAME
value: {{ .Values.rabbitmq.username | quote }}
- name: RABBITMQ_PASSWORD
value: {{ required "Set .Values.rabbitmq.password" .Values.rabbitmq.password | quote }}
- name: RABBITMQ_HOST
value: {{ .Values.rabbitmq.host | quote }}
- name: RABBITMQ_PORT
value: {{ .Values.rabbitmq.port | quote }}

View File

@@ -0,0 +1,5 @@
env: dev
mariadb:
cleanupPolicy: Delete

View File

@@ -0,0 +1,7 @@
env: prod
app:
replicas: 3
worker:
replicas: 3

View File

@@ -0,0 +1,52 @@
# Base values shared across environments
env: dev
# Optional PR number used to suffix DB name, set via --set prNumber=123 in CI
prNumber: ""
# Optional deployment identifier used to suffix resource names (db, user, secret)
# Example: --set deployment=alice or --set deployment=feature123
deployment: ""
# Public domain to expose the app under (used by TunnelBinding fqdn)
# Set at install time: --set domain=example.com
domain: ""
image:
repository: lukastrkan/cc-app-demo
# You can use a tag or digest. If digest is provided, it takes precedence.
digest: ""
pullPolicy: IfNotPresent
app:
name: ""
replicas: 1
port: 8000
worker:
name: app-demo-worker
replicas: 1
service:
port: 80
rabbitmq:
host: rabbitmq.rabbitmq.svc.cluster.local
port: "5672"
username: demo-app
password: ""
mariadb:
name: app-demo-database
cleanupPolicy: Skip
requeueInterval: 10h
retryInterval: 30s
mariaDbRef:
name: mariadb-repl
namespace: mariadb-operator
# Database access resources
database:
userName: app-demo-user
secretName: app-demo-database-secret
password: ""

View File

@@ -1,81 +1,58 @@
terraform {
required_providers {
kubectl = {
source = "gavinbunney/kubectl"
version = "1.19.0"
}
helm = {
source = "hashicorp/helm"
version = "3.0.2"
version = "3.0.2" # Doporučuji použít novější verzi providera
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "2.38.0"
}
kustomization = {
source = "kbst/kustomization"
version = "0.9.6"
}
time = {
source = "hashicorp/time"
version = "0.13.1"
version = "2.38.0" # Doporučuji použít novější verzi providera
}
# Ostatní provideři mohou zůstat
}
}
# Define the Helm release for RabbitMQ.
# This resource will install the RabbitMQ chart from the Bitnami repository.
resource "helm_release" "rabbitmq" {
# The name of the release in Kubernetes.
name = "rabbitmq"
# The repository where the chart is located.
repository = "https://charts.bitnami.com/bitnami"
resource "helm_release" "rabbitmq_operator" {
name = "rabbitmq-cluster-operator"
repository = "oci://registry-1.docker.io/bitnamicharts"
chart = "rabbitmq-cluster-operator"
# The name of the chart to deploy.
chart = "rabbitmq"
version = "4.4.34"
# The version of the chart to deploy. It's best practice to pin the version.
version = "14.4.1"
# The Kubernetes namespace to deploy into.
# If the namespace doesn't exist, you can create it with a kubernetes_namespace resource.
namespace = "rabbitmq"
namespace = "rabbitmq-system"
create_namespace = true
# Override default chart values.
# This is where you customize your RabbitMQ deployment.
# Zde můžete přepsat výchozí hodnoty chartu, pokud by bylo potřeba
# Například sledovat jen určité namespace, nastavit tolerations atd.
# Pro základní instalaci není potřeba nic měnit.
# values = [
# templatefile("${path.module}/values/operator-values.yaml", {})
# ]
set = [
{
name = "auth.username"
value = "admin"
name = "rabbitmqImage.repository"
value = "bitnamilegacy/rabbitmq"
},
{
name = "auth.password"
value = var.rabbitmq-password
name = "clusterOperator.image.repository"
value = "bitnamilegacy/rabbitmq-cluster-operator"
},
{
name = "persistence.enabled"
name = "msgTopologyOperator.image.repository"
value = "bitnamilegacy/rmq-messaging-topology-operator"
},
{
name = "credentialUpdaterImage.repository"
value = "bitnamilegacy/rmq-default-credential-updater"
},
{
name = "clusterOperator.metrics.service.enabled"
value = "true"
},
{
name = "replicaCount"
value = "1"
},
{
name = "podAntiAffinityPreset"
value = "soft"
},
{
name = "image.repository"
value = "bitnamilegacy/rabbitmq"
},
name = "clusterOperator.metrics.service.enabled"
value = "true"
}
]
}
resource "kubectl_manifest" "rabbitmq_ui" {
yaml_body = templatefile("${path.module}/rabbit-ui.yaml", {
base_domain = var.base_domain
})
depends_on = [helm_release.rabbitmq]
}