fix: resolve critical deployment issues across Helm charts, Dockerfile, and K8s fallback

- Add helm/kubectl binaries and chart directory to backend Dockerfile
- Extend Helm templates for MongoDB/MariaDB database support (env vars, probes, ports)
- Add Redis and RabbitMQ Helm templates (deployment, service, secret, PVC)
- Add generic app-storage PVC and Fluent Bit sidecar with ES authentication
- Fix imagePullSecrets in K8s API fallback, prevent secret regeneration on redeploy
- Clean up Redis/RabbitMQ/FluentBit resources on app deletion without removing shared secrets
- Fix HelmService chartPath resolution for production Docker builds

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
keyhan
2026-05-13 19:16:07 +03:30
parent 38748b0827
commit 9937ee457d
11 changed files with 665 additions and 55 deletions
+7 -1
View File
@@ -12,7 +12,12 @@ RUN npm run build
# ---- Stage 2: Production ---- # ---- Stage 2: Production ----
FROM node:20-alpine AS production FROM node:20-alpine AS production
RUN apk add --no-cache dumb-init RUN apk add --no-cache dumb-init curl bash \
&& curl -fsSL https://get.helm.sh/helm-v3.15.4-linux-amd64.tar.gz | tar xz -C /tmp \
&& mv /tmp/linux-amd64/helm /usr/local/bin/helm \
&& rm -rf /tmp/linux-amd64 \
&& curl -fsSLO "https://dl.k8s.io/release/$(curl -fsSL https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" \
&& chmod +x kubectl && mv kubectl /usr/local/bin/kubectl
ENV NODE_ENV=production ENV NODE_ENV=production
WORKDIR /app WORKDIR /app
@@ -22,6 +27,7 @@ RUN npm ci --omit=dev && npm cache clean --force
COPY --from=builder /app/dist ./dist COPY --from=builder /app/dist ./dist
COPY --from=builder /app/templates ./templates COPY --from=builder /app/templates ./templates
COPY --from=builder /app/helm ./helm
RUN addgroup -S appgroup && adduser -S appuser -G appgroup RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser USER appuser
@@ -54,6 +54,10 @@ Database image — auto-computed from type + version if not explicitly set
{{- .Values.database.image }} {{- .Values.database.image }}
{{- else if eq .Values.database.type "postgresql" }} {{- else if eq .Values.database.type "postgresql" }}
{{- printf "postgres:%s-alpine" .Values.database.version }} {{- printf "postgres:%s-alpine" .Values.database.version }}
{{- else if eq .Values.database.type "mariadb" }}
{{- printf "mariadb:%s" .Values.database.version }}
{{- else if eq .Values.database.type "mongodb" }}
{{- printf "mongo:%s" .Values.database.version }}
{{- else }} {{- else }}
{{- printf "mysql:%s" .Values.database.version }} {{- printf "mysql:%s" .Values.database.version }}
{{- end }} {{- end }}
@@ -63,14 +67,20 @@ Database image — auto-computed from type + version if not explicitly set
Database port Database port
*/}} */}}
{{- define "cloudhost-app.dbPort" -}} {{- define "cloudhost-app.dbPort" -}}
{{- if eq .Values.database.type "postgresql" }}5432{{- else }}3306{{- end }} {{- if eq .Values.database.type "postgresql" }}5432
{{- else if eq .Values.database.type "mongodb" }}27017
{{- else }}3306
{{- end }}
{{- end }} {{- end }}
{{/* {{/*
Database data mount path (volume mount target) Database data mount path (volume mount target)
*/}} */}}
{{- define "cloudhost-app.dbDataPath" -}} {{- define "cloudhost-app.dbDataPath" -}}
{{- if eq .Values.database.type "postgresql" }}/var/lib/postgresql/data{{- else }}/var/lib/mysql{{- end }} {{- if eq .Values.database.type "postgresql" }}/var/lib/postgresql/data
{{- else if eq .Values.database.type "mongodb" }}/data/db
{{- else }}/var/lib/mysql
{{- end }}
{{- end }} {{- end }}
{{/* {{/*
@@ -79,3 +89,25 @@ PostgreSQL PGDATA path — must be a subdirectory of the mount to avoid "initdb:
{{- define "cloudhost-app.pgDataDir" -}} {{- define "cloudhost-app.pgDataDir" -}}
/var/lib/postgresql/data/pgdata /var/lib/postgresql/data/pgdata
{{- end }} {{- end }}
{{/*
App storage mount path based on runtime
*/}}
{{- define "cloudhost-app.storageMountPath" -}}
{{- if eq .Values.app.runtime "wordpress" }}/var/www/html/wp-content
{{- else if or (eq .Values.app.runtime "laravel") (eq .Values.app.runtime "php") }}/var/www/html/storage
{{- else if eq .Values.app.runtime "django" }}/app/media
{{- else }}/app/data
{{- end }}
{{- end }}
{{/*
Default log paths based on runtime
*/}}
{{- define "cloudhost-app.defaultLogPaths" -}}
{{- if eq .Values.app.runtime "wordpress" }}/var/www/html/wp-content/debug.log,/var/log/app/*.log
{{- else if eq .Values.app.runtime "laravel" }}/var/www/html/storage/logs/*.log,/var/log/app/*.log
{{- else if or (eq .Values.app.runtime "php") }}/var/www/html/storage/logs/*.log,/var/log/php/*.log,/var/log/app/*.log
{{- else }}/var/log/app/*.log
{{- end }}
{{- end }}
@@ -0,0 +1,17 @@
{{- $name := include "cloudhost-app.name" . -}}
{{- $ns := include "cloudhost-app.namespace" . -}}
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ $name }}-storage
namespace: {{ $ns }}
labels:
{{- include "cloudhost-app.labels" . | nindent 4 }}
annotations:
"helm.sh/resource-policy": keep
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: {{ .Values.app.storageSize | default "2Gi" | quote }}
@@ -44,6 +44,37 @@ spec:
secretKeyRef: secretKeyRef:
name: {{ $name }}-db-secret name: {{ $name }}-db-secret
key: password key: password
{{- else if eq .Values.database.type "mariadb" }}
- name: MARIADB_DATABASE
value: {{ include "cloudhost-app.dbName" . }}
- name: MARIADB_USER
valueFrom:
secretKeyRef:
name: {{ $name }}-db-secret
key: username
- name: MARIADB_PASSWORD
valueFrom:
secretKeyRef:
name: {{ $name }}-db-secret
key: password
- name: MARIADB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: {{ $name }}-db-secret
key: password
{{- else if eq .Values.database.type "mongodb" }}
- name: MONGO_INITDB_DATABASE
value: {{ include "cloudhost-app.dbName" . }}
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: {{ $name }}-db-secret
key: username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: {{ $name }}-db-secret
key: password
{{- else }} {{- else }}
- name: MYSQL_DATABASE - name: MYSQL_DATABASE
value: {{ include "cloudhost-app.dbName" . }} value: {{ include "cloudhost-app.dbName" . }}
@@ -77,6 +108,12 @@ spec:
{{- if eq .Values.database.type "postgresql" }} {{- if eq .Values.database.type "postgresql" }}
exec: exec:
command: ["pg_isready", "-U", {{ .Values.database.username | quote }}] command: ["pg_isready", "-U", {{ .Values.database.username | quote }}]
{{- else if eq .Values.database.type "mariadb" }}
exec:
command: ["healthcheck.sh", "--connect", "--innodb_initialized"]
{{- else if eq .Values.database.type "mongodb" }}
exec:
command: ["mongosh", "--eval", "db.adminCommand('ping')"]
{{- else }} {{- else }}
exec: exec:
command: ["mysqladmin", "ping", "-h", "127.0.0.1"] command: ["mysqladmin", "ping", "-h", "127.0.0.1"]
@@ -88,6 +125,12 @@ spec:
{{- if eq .Values.database.type "postgresql" }} {{- if eq .Values.database.type "postgresql" }}
exec: exec:
command: ["pg_isready", "-U", {{ .Values.database.username | quote }}] command: ["pg_isready", "-U", {{ .Values.database.username | quote }}]
{{- else if eq .Values.database.type "mariadb" }}
exec:
command: ["healthcheck.sh", "--connect", "--innodb_initialized"]
{{- else if eq .Values.database.type "mongodb" }}
exec:
command: ["mongosh", "--eval", "db.adminCommand('ping')"]
{{- else }} {{- else }}
exec: exec:
command: ["mysqladmin", "ping", "-h", "127.0.0.1"] command: ["mysqladmin", "ping", "-h", "127.0.0.1"]
@@ -74,6 +74,83 @@ spec:
- name: DATABASE_URL - name: DATABASE_URL
value: "mysql://$(DB_USER):$(DB_PASSWORD)@{{ include "cloudhost-app.dbDeploymentName" . }}:3306/{{ include "cloudhost-app.dbName" . }}" value: "mysql://$(DB_USER):$(DB_PASSWORD)@{{ include "cloudhost-app.dbDeploymentName" . }}:3306/{{ include "cloudhost-app.dbName" . }}"
{{- end }} {{- end }}
{{- if and .Values.database.enabled (eq .Values.database.type "mariadb") }}
- name: DB_HOST
value: {{ include "cloudhost-app.dbDeploymentName" . }}
- name: DB_PORT
value: "3306"
- name: DB_NAME
value: {{ include "cloudhost-app.dbName" . }}
- name: DB_USER
valueFrom:
secretKeyRef:
name: {{ $name }}-db-secret
key: username
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: {{ $name }}-db-secret
key: password
- name: DATABASE_URL
value: "mysql://$(DB_USER):$(DB_PASSWORD)@{{ include "cloudhost-app.dbDeploymentName" . }}:3306/{{ include "cloudhost-app.dbName" . }}"
{{- end }}
{{- if and .Values.database.enabled (eq .Values.database.type "mongodb") }}
- name: DB_HOST
value: {{ include "cloudhost-app.dbDeploymentName" . }}
- name: DB_PORT
value: "27017"
- name: DB_NAME
value: {{ include "cloudhost-app.dbName" . }}
- name: DB_USER
valueFrom:
secretKeyRef:
name: {{ $name }}-db-secret
key: username
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: {{ $name }}-db-secret
key: password
- name: MONGODB_URI
value: "mongodb://$(DB_USER):$(DB_PASSWORD)@{{ include "cloudhost-app.dbDeploymentName" . }}:27017/{{ include "cloudhost-app.dbName" . }}?authSource=admin"
- name: DATABASE_URL
value: "mongodb://$(DB_USER):$(DB_PASSWORD)@{{ include "cloudhost-app.dbDeploymentName" . }}:27017/{{ include "cloudhost-app.dbName" . }}?authSource=admin"
{{- end }}
{{- /* Redis connection env vars */}}
{{- if .Values.redis.enabled }}
- name: REDIS_HOST
value: {{ printf "%s-redis" $name }}
- name: REDIS_PORT
value: "6379"
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: {{ printf "%s-redis-secret" $name }}
key: password
- name: REDIS_URL
value: "redis://:$(REDIS_PASSWORD)@{{ printf "%s-redis" $name }}:6379"
{{- end }}
{{- /* RabbitMQ connection env vars */}}
{{- if .Values.rabbitmq.enabled }}
- name: RABBITMQ_HOST
value: {{ printf "%s-rabbitmq" $name }}
- name: RABBITMQ_PORT
value: "5672"
- name: RABBITMQ_MANAGEMENT_PORT
value: "15672"
- name: RABBITMQ_USER
valueFrom:
secretKeyRef:
name: {{ printf "%s-rabbitmq-secret" $name }}
key: username
- name: RABBITMQ_PASSWORD
valueFrom:
secretKeyRef:
name: {{ printf "%s-rabbitmq-secret" $name }}
key: password
- name: AMQP_URL
value: "amqp://$(RABBITMQ_USER):$(RABBITMQ_PASSWORD)@{{ printf "%s-rabbitmq" $name }}:5672"
{{- end }}
{{- /* WordPress-specific env vars (official image expects these) */}} {{- /* WordPress-specific env vars (official image expects these) */}}
{{- if and .Values.wordpress.enabled .Values.database.enabled }} {{- if and .Values.wordpress.enabled .Values.database.enabled }}
- name: WORDPRESS_DB_HOST - name: WORDPRESS_DB_HOST
@@ -111,14 +188,58 @@ spec:
initialDelaySeconds: 15 initialDelaySeconds: 15
periodSeconds: 10 periodSeconds: 10
failureThreshold: 5 failureThreshold: 5
{{- if .Values.wordpress.enabled }}
volumeMounts: volumeMounts:
- name: wp-content - name: app-storage
mountPath: /var/www/html/wp-content mountPath: {{ include "cloudhost-app.storageMountPath" . | trim }}
{{- if .Values.elasticsearch.enabled }}
- name: app-logs
mountPath: /var/log/app
{{- end }}
{{- if .Values.elasticsearch.enabled }}
- name: fluent-bit
image: fluent/fluent-bit:2.2
resources:
requests:
cpu: "10m"
memory: "32Mi"
limits:
cpu: "50m"
memory: "64Mi"
volumeMounts:
- name: app-logs
mountPath: /var/log/app
readOnly: true
- name: fluent-bit-config
mountPath: /fluent-bit/etc
env:
- name: APP_NAME
value: {{ $name }}
- name: APP_NAMESPACE
value: {{ $ns }}
- name: ES_HOST
value: "elasticsearch.logging.svc.cluster.local"
- name: ES_PORT
value: "9200"
- name: ES_PASSWORD
valueFrom:
secretKeyRef:
name: elasticsearch-credentials
key: ELASTIC_PASSWORD
optional: true
{{- end }} {{- end }}
{{- if .Values.wordpress.enabled }}
volumes: volumes:
- name: app-storage
persistentVolumeClaim:
claimName: {{ $name }}-storage
{{- if .Values.wordpress.enabled }}
- name: wp-content - name: wp-content
persistentVolumeClaim: persistentVolumeClaim:
claimName: {{ $name }}-wp-content claimName: {{ $name }}-wp-content
{{- end }} {{- end }}
{{- if .Values.elasticsearch.enabled }}
- name: app-logs
emptyDir: {}
- name: fluent-bit-config
configMap:
name: {{ $name }}-fluent-bit-config
{{- end }}
@@ -0,0 +1,71 @@
{{- if .Values.elasticsearch.enabled }}
{{- $name := include "cloudhost-app.name" . -}}
{{- $ns := include "cloudhost-app.namespace" . -}}
{{- $logPaths := .Values.elasticsearch.logPaths -}}
{{- $defaultPaths := include "cloudhost-app.defaultLogPaths" . | trim -}}
{{- $paths := ternary ($logPaths | join ",") $defaultPaths (and $logPaths (gt (len $logPaths) 0)) -}}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $name }}-fluent-bit-config
namespace: {{ $ns }}
labels:
{{- include "cloudhost-app.labels" . | nindent 4 }}
data:
fluent-bit.conf: |
[SERVICE]
Flush 5
Daemon Off
Log_Level info
Parsers_File /fluent-bit/etc/parsers.conf
[INPUT]
Name tail
Path {{ $paths }}
Tag app.{{ $name }}
Refresh_Interval 5
Mem_Buf_Limit 5MB
Skip_Long_Lines On
[FILTER]
Name record_modifier
Match *
Record app {{ $name }}
Record namespace {{ $ns }}
Record runtime {{ .Values.app.runtime }}
[FILTER]
Name parser
Match *
Key_Name log
Parser json
Reserve_Data On
Preserve_Key On
[OUTPUT]
Name es
Match *
Host ${ES_HOST}
Port ${ES_PORT}
HTTP_User elastic
HTTP_Passwd ${ES_PASSWORD}
Index logs-{{ $ns }}-{{ $name }}
Logstash_Format On
Logstash_Prefix logs-{{ $ns }}
Suppress_Type_Name On
tls Off
Retry_Limit 3
parsers.conf: |
[PARSER]
Name json
Format json
Time_Key time
Time_Format %Y-%m-%dT%H:%M:%S.%L
[PARSER]
Name docker
Format json
Time_Key time
Time_Format %Y-%m-%dT%H:%M:%S.%L
{{- end }}
@@ -0,0 +1,123 @@
{{- if .Values.rabbitmq.enabled }}
{{- $name := include "cloudhost-app.name" . -}}
{{- $ns := include "cloudhost-app.namespace" . -}}
{{- $rabbitName := printf "%s-rabbitmq" $name -}}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ $rabbitName }}-secret
namespace: {{ $ns }}
labels:
app: {{ $rabbitName }}
{{- include "cloudhost-app.labels" . | nindent 4 }}
annotations:
"helm.sh/resource-policy": keep
type: Opaque
data:
username: {{ "appuser" | b64enc | quote }}
password: {{ randAlphaNum 16 | b64enc | quote }}
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ $rabbitName }}-data
namespace: {{ $ns }}
labels:
app: {{ $rabbitName }}
{{- include "cloudhost-app.labels" . | nindent 4 }}
annotations:
"helm.sh/resource-policy": keep
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: {{ .Values.rabbitmq.storageSize | default "2Gi" | quote }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ $rabbitName }}
namespace: {{ $ns }}
labels:
app: {{ $rabbitName }}
{{- include "cloudhost-app.labels" . | nindent 4 }}
spec:
replicas: 1
selector:
matchLabels:
app: {{ $rabbitName }}
template:
metadata:
labels:
app: {{ $rabbitName }}
spec:
containers:
- name: rabbitmq
image: {{ printf "rabbitmq:%s-management-alpine" (.Values.rabbitmq.version | default "3.13") }}
ports:
- containerPort: 5672
name: amqp
- containerPort: 15672
name: management
env:
- name: RABBITMQ_DEFAULT_USER
valueFrom:
secretKeyRef:
name: {{ $rabbitName }}-secret
key: username
- name: RABBITMQ_DEFAULT_PASS
valueFrom:
secretKeyRef:
name: {{ $rabbitName }}-secret
key: password
volumeMounts:
- name: rabbitmq-data
mountPath: /var/lib/rabbitmq
resources:
requests:
cpu: {{ .Values.rabbitmq.resources.cpuRequest | default "100m" | quote }}
memory: {{ .Values.rabbitmq.resources.memoryRequest | default "256Mi" | quote }}
limits:
cpu: {{ .Values.rabbitmq.resources.cpuLimit | default "500m" | quote }}
memory: {{ .Values.rabbitmq.resources.memoryLimit | default "512Mi" | quote }}
readinessProbe:
exec:
command: ["rabbitmq-diagnostics", "-q", "ping"]
initialDelaySeconds: 20
periodSeconds: 10
timeoutSeconds: 5
livenessProbe:
exec:
command: ["rabbitmq-diagnostics", "-q", "status"]
initialDelaySeconds: 60
periodSeconds: 30
timeoutSeconds: 10
volumes:
- name: rabbitmq-data
persistentVolumeClaim:
claimName: {{ $rabbitName }}-data
---
apiVersion: v1
kind: Service
metadata:
name: {{ $rabbitName }}
namespace: {{ $ns }}
labels:
app: {{ $rabbitName }}
{{- include "cloudhost-app.labels" . | nindent 4 }}
spec:
type: ClusterIP
selector:
app: {{ $rabbitName }}
ports:
- port: 5672
targetPort: 5672
protocol: TCP
name: amqp
- port: 15672
targetPort: 15672
protocol: TCP
name: management
{{- end }}
@@ -0,0 +1,108 @@
{{- if .Values.redis.enabled }}
{{- $name := include "cloudhost-app.name" . -}}
{{- $ns := include "cloudhost-app.namespace" . -}}
{{- $redisName := printf "%s-redis" $name -}}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ $redisName }}-secret
namespace: {{ $ns }}
labels:
app: {{ $redisName }}
{{- include "cloudhost-app.labels" . | nindent 4 }}
annotations:
"helm.sh/resource-policy": keep
type: Opaque
data:
password: {{ randAlphaNum 16 | b64enc | quote }}
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ $redisName }}-data
namespace: {{ $ns }}
labels:
app: {{ $redisName }}
{{- include "cloudhost-app.labels" . | nindent 4 }}
annotations:
"helm.sh/resource-policy": keep
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: {{ .Values.redis.storageSize | default "1Gi" | quote }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ $redisName }}
namespace: {{ $ns }}
labels:
app: {{ $redisName }}
{{- include "cloudhost-app.labels" . | nindent 4 }}
spec:
replicas: 1
selector:
matchLabels:
app: {{ $redisName }}
template:
metadata:
labels:
app: {{ $redisName }}
spec:
containers:
- name: redis
image: {{ printf "redis:%s-alpine" (.Values.redis.version | default "7.2") }}
args: ["--requirepass", "$(REDIS_PASSWORD)"]
ports:
- containerPort: 6379
env:
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: {{ $redisName }}-secret
key: password
volumeMounts:
- name: redis-data
mountPath: /data
resources:
requests:
cpu: {{ .Values.redis.resources.cpuRequest | default "50m" | quote }}
memory: {{ .Values.redis.resources.memoryRequest | default "64Mi" | quote }}
limits:
cpu: {{ .Values.redis.resources.cpuLimit | default "200m" | quote }}
memory: {{ .Values.redis.resources.memoryLimit | default "256Mi" | quote }}
readinessProbe:
exec:
command: ["redis-cli", "ping"]
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
exec:
command: ["redis-cli", "ping"]
initialDelaySeconds: 15
periodSeconds: 20
volumes:
- name: redis-data
persistentVolumeClaim:
claimName: {{ $redisName }}-data
---
apiVersion: v1
kind: Service
metadata:
name: {{ $redisName }}
namespace: {{ $ns }}
labels:
app: {{ $redisName }}
{{- include "cloudhost-app.labels" . | nindent 4 }}
spec:
type: ClusterIP
selector:
app: {{ $redisName }}
ports:
- port: 6379
targetPort: 6379
protocol: TCP
{{- end }}
+29 -1
View File
@@ -7,10 +7,11 @@
app: app:
name: my-app name: my-app
namespace: user-default namespace: user-default
runtime: nodejs # nodejs | laravel | wordpress runtime: nodejs # nodejs | laravel | wordpress | go | php | python | django | dotnet
image: "" # e.g. registry.example.com/my-app:v1 image: "" # e.g. registry.example.com/my-app:v1
port: 3000 port: 3000
replicas: 1 replicas: 1
storageSize: "2Gi" # PVC size for app files (uploads, media, data)
# ── Resources ──────────────────────────────────────────── # ── Resources ────────────────────────────────────────────
resources: resources:
@@ -51,6 +52,33 @@ wordpress:
enabled: false enabled: false
wpContentStorageSize: "2Gi" wpContentStorageSize: "2Gi"
# ── Redis ──────────────────────────────────────────────
redis:
enabled: false
version: "7.2"
storageSize: "1Gi"
resources:
cpuRequest: "50m"
cpuLimit: "200m"
memoryRequest: "64Mi"
memoryLimit: "256Mi"
# ── RabbitMQ ──────────────────────────────────────────
rabbitmq:
enabled: false
version: "3.13"
storageSize: "2Gi"
resources:
cpuRequest: "100m"
cpuLimit: "500m"
memoryRequest: "256Mi"
memoryLimit: "512Mi"
# ── Elasticsearch (Fluent Bit sidecar for logging) ────
elasticsearch:
enabled: false
logPaths: []
# ── Change metadata ───────────────────────────────────── # ── Change metadata ─────────────────────────────────────
changeCause: "" changeCause: ""
+8 -2
View File
@@ -31,8 +31,14 @@ export class HelmService {
private readonly chartPath: string; private readonly chartPath: string;
constructor() { constructor() {
// Resolve the chart path relative to the backend project root // In production (dist/kubernetes/), __dirname resolves to dist/kubernetes
this.chartPath = path.resolve(__dirname, '..', '..', 'helm', 'cloudhost-app'); // so we go up two levels to project root, then into helm/
// In Docker, the helm/ dir is copied alongside dist/ at /app/helm/
const candidates = [
path.resolve(__dirname, '..', '..', 'helm', 'cloudhost-app'),
path.resolve(process.cwd(), 'helm', 'cloudhost-app'),
];
this.chartPath = candidates.find((p) => fs.existsSync(p)) || candidates[0];
} }
/** /**
+82 -27
View File
@@ -34,8 +34,11 @@ interface ManifestContext {
dbStorageSize: string; dbStorageSize: string;
appStorageSize: string; appStorageSize: string;
enableRedis: boolean; enableRedis: boolean;
redisVersion: string;
enableRabbitmq: boolean; enableRabbitmq: boolean;
rabbitmqVersion: string;
enableElasticsearch: boolean; enableElasticsearch: boolean;
elasticsearchVersion: string;
logPaths: string[]; logPaths: string[];
} }
@@ -225,8 +228,11 @@ export class KubernetesService implements OnModuleInit {
dbStorageSize: app.dbStorageSize || '1Gi', dbStorageSize: app.dbStorageSize || '1Gi',
appStorageSize: app.appStorageSize || '2Gi', appStorageSize: app.appStorageSize || '2Gi',
enableRedis: app.enableRedis || false, enableRedis: app.enableRedis || false,
redisVersion: app.redisVersion || '7.2',
enableRabbitmq: app.enableRabbitmq || false, enableRabbitmq: app.enableRabbitmq || false,
rabbitmqVersion: app.rabbitmqVersion || '3.13',
enableElasticsearch: app.enableElasticsearch || false, enableElasticsearch: app.enableElasticsearch || false,
elasticsearchVersion: app.elasticsearchVersion || '8.12',
logPaths: app.logPaths || [], logPaths: app.logPaths || [],
}; };
@@ -412,6 +418,7 @@ export class KubernetesService implements OnModuleInit {
template: { template: {
metadata: { labels: { app: ctx.appName, runtime: ctx.runtime } }, metadata: { labels: { app: ctx.appName, runtime: ctx.runtime } },
spec: { spec: {
imagePullSecrets: [{ name: 'registry-pull-secret' }],
containers: this.buildContainersSpec(ctx, envFrom, extraEnv), containers: this.buildContainersSpec(ctx, envFrom, extraEnv),
volumes: this.buildVolumesSpec(ctx), volumes: this.buildVolumesSpec(ctx),
}, },
@@ -511,12 +518,6 @@ export class KubernetesService implements OnModuleInit {
// Add Fluent Bit sidecar for log collection if Elasticsearch is enabled // Add Fluent Bit sidecar for log collection if Elasticsearch is enabled
if (ctx.enableElasticsearch) { if (ctx.enableElasticsearch) {
const logPaths = ctx.logPaths && ctx.logPaths.length > 0
? ctx.logPaths
: ['/var/log/app/*.log'];
const fluentbitConfig = this.buildFluentBitConfig(ctx.appName, ctx.namespace, logPaths);
containers.push({ containers.push({
name: 'fluent-bit', name: 'fluent-bit',
image: 'fluent/fluent-bit:2.2', image: 'fluent/fluent-bit:2.2',
@@ -531,9 +532,9 @@ export class KubernetesService implements OnModuleInit {
env: [ env: [
{ name: 'APP_NAME', value: ctx.appName }, { name: 'APP_NAME', value: ctx.appName },
{ name: 'APP_NAMESPACE', value: ctx.namespace }, { name: 'APP_NAMESPACE', value: ctx.namespace },
// Elasticsearch host - assumes cluster-level ES at elasticsearch.logging namespace
{ name: 'ES_HOST', value: 'elasticsearch.logging.svc.cluster.local' }, { name: 'ES_HOST', value: 'elasticsearch.logging.svc.cluster.local' },
{ name: 'ES_PORT', value: '9200' }, { name: 'ES_PORT', value: '9200' },
{ name: 'ES_PASSWORD', valueFrom: { secretKeyRef: { name: 'elasticsearch-credentials', key: 'ELASTIC_PASSWORD', optional: true } } },
], ],
}); });
} }
@@ -569,40 +570,84 @@ export class KubernetesService implements OnModuleInit {
return volumes; return volumes;
} }
/**
* Get default log paths based on runtime type
*/
private getDefaultLogPaths(runtime: string): string[] {
switch (runtime) {
case AppRuntime.WORDPRESS:
return ['/var/www/html/wp-content/debug.log', '/var/log/app/*.log'];
case AppRuntime.LARAVEL:
return ['/var/www/html/storage/logs/*.log', '/var/log/app/*.log'];
case AppRuntime.PHP:
return ['/var/www/html/storage/logs/*.log', '/var/log/php/*.log', '/var/log/app/*.log'];
case AppRuntime.DJANGO:
return ['/app/logs/*.log', '/var/log/app/*.log'];
case AppRuntime.PYTHON:
return ['/app/logs/*.log', '/var/log/app/*.log'];
case AppRuntime.NODEJS:
return ['/app/logs/*.log', '/var/log/app/*.log'];
case AppRuntime.GO:
return ['/app/logs/*.log', '/var/log/app/*.log'];
case AppRuntime.DOTNET:
return ['/app/logs/*.log', '/var/log/app/*.log'];
default:
return ['/var/log/app/*.log'];
}
}
/** /**
* Build Fluent Bit configuration for log collection * Build Fluent Bit configuration for log collection
*/ */
private buildFluentBitConfig(appName: string, namespace: string, logPaths: string[]): string { private buildFluentBitConfig(appName: string, namespace: string, runtime: string, customLogPaths?: string[]): string {
const logPaths = customLogPaths && customLogPaths.length > 0
? customLogPaths
: this.getDefaultLogPaths(runtime);
const pathsStr = logPaths.join(','); const pathsStr = logPaths.join(',');
return ` return `
[SERVICE] [SERVICE]
Flush 5 Flush 5
Daemon Off Daemon Off
Log_Level info Log_Level info
Parsers_File /fluent-bit/etc/parsers.conf
[INPUT] [INPUT]
Name tail Name tail
Path ${pathsStr} Path ${pathsStr}
Tag app.${appName} Tag app.${appName}
Parser json
Refresh_Interval 5 Refresh_Interval 5
Mem_Buf_Limit 5MB
Skip_Long_Lines On
[FILTER] [FILTER]
Name record_modifier Name record_modifier
Match * Match *
Record app ${appName} Record app ${appName}
Record namespace ${namespace} Record namespace ${namespace}
Record runtime ${runtime}
[FILTER]
Name parser
Match *
Key_Name log
Parser json
Reserve_Data On
Preserve_Key On
[OUTPUT] [OUTPUT]
Name es Name es
Match * Match *
Host \${ES_HOST} Host \${ES_HOST}
Port \${ES_PORT} Port \${ES_PORT}
HTTP_User elastic
HTTP_Passwd \${ES_PASSWORD}
Index logs-${namespace}-${appName} Index logs-${namespace}-${appName}
Type _doc
Logstash_Format On Logstash_Format On
Logstash_Prefix logs-${namespace} Logstash_Prefix logs-${namespace}
Suppress_Type_Name On Suppress_Type_Name On
tls Off
Retry_Limit 3
`; `;
} }
@@ -615,9 +660,9 @@ export class KubernetesService implements OnModuleInit {
): Promise<void> { ): Promise<void> {
if (!ctx.enableElasticsearch) return; if (!ctx.enableElasticsearch) return;
const logPaths = ctx.logPaths && ctx.logPaths.length > 0 const customLogPaths = ctx.logPaths && ctx.logPaths.length > 0
? ctx.logPaths ? ctx.logPaths
: ['/var/log/app/*.log']; : undefined;
const configMap = { const configMap = {
apiVersion: 'v1', apiVersion: 'v1',
@@ -628,7 +673,7 @@ export class KubernetesService implements OnModuleInit {
labels: { app: ctx.appName }, labels: { app: ctx.appName },
}, },
data: { data: {
'fluent-bit.conf': this.buildFluentBitConfig(ctx.appName, ctx.namespace, logPaths), 'fluent-bit.conf': this.buildFluentBitConfig(ctx.appName, ctx.namespace, ctx.runtime, customLogPaths),
'parsers.conf': ` 'parsers.conf': `
[PARSER] [PARSER]
Name json Name json
@@ -920,7 +965,11 @@ export class KubernetesService implements OnModuleInit {
// Create PVC for Redis persistence // Create PVC for Redis persistence
await this.createPVC(coreApi, ctx.namespace, `${redisName}-data`, '1Gi'); await this.createPVC(coreApi, ctx.namespace, `${redisName}-data`, '1Gi');
// Create Redis password secret // Only create Redis password secret if it doesn't already exist
try {
await coreApi.readNamespacedSecret(`${redisName}-secret`, ctx.namespace);
this.logger.log(`Redis secret ${redisName}-secret already exists, skipping`);
} catch {
const redisPassword = this.generatePassword(16); const redisPassword = this.generatePassword(16);
const redisSecret = { const redisSecret = {
apiVersion: 'v1', apiVersion: 'v1',
@@ -930,10 +979,6 @@ export class KubernetesService implements OnModuleInit {
password: Buffer.from(redisPassword).toString('base64'), password: Buffer.from(redisPassword).toString('base64'),
}, },
}; };
try {
await coreApi.replaceNamespacedSecret(`${redisName}-secret`, ctx.namespace, redisSecret);
} catch {
await coreApi.createNamespacedSecret(ctx.namespace, redisSecret); await coreApi.createNamespacedSecret(ctx.namespace, redisSecret);
} }
@@ -951,7 +996,7 @@ export class KubernetesService implements OnModuleInit {
containers: [ containers: [
{ {
name: 'redis', name: 'redis',
image: 'redis:7.2-alpine', image: `redis:${ctx.redisVersion}-alpine`,
args: ['--requirepass', '$(REDIS_PASSWORD)'], args: ['--requirepass', '$(REDIS_PASSWORD)'],
ports: [{ containerPort: 6379 }], ports: [{ containerPort: 6379 }],
env: [ env: [
@@ -1032,7 +1077,11 @@ export class KubernetesService implements OnModuleInit {
// Create PVC for RabbitMQ persistence // Create PVC for RabbitMQ persistence
await this.createPVC(coreApi, ctx.namespace, `${rabbitName}-data`, '2Gi'); await this.createPVC(coreApi, ctx.namespace, `${rabbitName}-data`, '2Gi');
// Create RabbitMQ credentials secret // Only create RabbitMQ credentials secret if it doesn't already exist
try {
await coreApi.readNamespacedSecret(`${rabbitName}-secret`, ctx.namespace);
this.logger.log(`RabbitMQ secret ${rabbitName}-secret already exists, skipping`);
} catch {
const rabbitUser = 'appuser'; const rabbitUser = 'appuser';
const rabbitPassword = this.generatePassword(16); const rabbitPassword = this.generatePassword(16);
const rabbitSecret = { const rabbitSecret = {
@@ -1044,10 +1093,6 @@ export class KubernetesService implements OnModuleInit {
password: Buffer.from(rabbitPassword).toString('base64'), password: Buffer.from(rabbitPassword).toString('base64'),
}, },
}; };
try {
await coreApi.replaceNamespacedSecret(`${rabbitName}-secret`, ctx.namespace, rabbitSecret);
} catch {
await coreApi.createNamespacedSecret(ctx.namespace, rabbitSecret); await coreApi.createNamespacedSecret(ctx.namespace, rabbitSecret);
} }
@@ -1065,7 +1110,7 @@ export class KubernetesService implements OnModuleInit {
containers: [ containers: [
{ {
name: 'rabbitmq', name: 'rabbitmq',
image: 'rabbitmq:3.13-management-alpine', image: `rabbitmq:${ctx.rabbitmqVersion}-management-alpine`,
ports: [ ports: [
{ containerPort: 5672, name: 'amqp' }, { containerPort: 5672, name: 'amqp' },
{ containerPort: 15672, name: 'management' }, { containerPort: 15672, name: 'management' },
@@ -1601,8 +1646,18 @@ export class KubernetesService implements OnModuleInit {
() => coreApi.deleteNamespacedPersistentVolumeClaim(`${app.name}-wp-content`, namespace), () => coreApi.deleteNamespacedPersistentVolumeClaim(`${app.name}-wp-content`, namespace),
// App env secret // App env secret
() => coreApi.deleteNamespacedSecret(`${app.name}-env`, namespace), () => coreApi.deleteNamespacedSecret(`${app.name}-env`, namespace),
// Registry pull secret (shared, but labeled per-app — safe to delete) // Fluent Bit config (if elasticsearch was enabled)
() => coreApi.deleteNamespacedSecret('registry-pull-secret', namespace), () => coreApi.deleteNamespacedConfigMap(`${app.name}-fluent-bit-config`, namespace),
// Redis resources
() => appsApi.deleteNamespacedDeployment(`${app.name}-redis`, namespace),
() => coreApi.deleteNamespacedService(`${app.name}-redis`, namespace),
() => coreApi.deleteNamespacedPersistentVolumeClaim(`${app.name}-redis-data`, namespace),
() => coreApi.deleteNamespacedSecret(`${app.name}-redis-secret`, namespace),
// RabbitMQ resources
() => appsApi.deleteNamespacedDeployment(`${app.name}-rabbitmq`, namespace),
() => coreApi.deleteNamespacedService(`${app.name}-rabbitmq`, namespace),
() => coreApi.deleteNamespacedPersistentVolumeClaim(`${app.name}-rabbitmq-data`, namespace),
() => coreApi.deleteNamespacedSecret(`${app.name}-rabbitmq-secret`, namespace),
// TLS secret created by cert-manager // TLS secret created by cert-manager
() => coreApi.deleteNamespacedSecret(`${app.name}-tls`, namespace), () => coreApi.deleteNamespacedSecret(`${app.name}-tls`, namespace),
]; ];