Skip to content

Commit

Permalink
fix: add cleanup to helm chart and remove script
Browse files Browse the repository at this point in the history
  • Loading branch information
chliddle committed Dec 13, 2024
1 parent dc43426 commit da2a89e
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 1 deletion.
80 changes: 80 additions & 0 deletions charts/namada-indexer/templates/dependencies-job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{{- if or (and .Values.postgresOperator.install.enabled .Values.postgresOperator.enabled) (and .Values.redis.install.enabled .Values.redis.enabled) }}
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "namada-indexer.fullname" . }}-wait-dependencies
annotations:
"helm.sh/hook": post-install
"helm.sh/hook-weight": "-5"
"helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation
labels:
{{- include "namada-indexer.labels" . | nindent 4 }}
spec:
backoffLimit: 100
template:
metadata:
name: {{ include "namada-indexer.fullname" . }}-wait-dependencies
spec:
serviceAccountName: {{ include "namada-indexer.serviceAccountName" . }}
restartPolicy: OnFailure
containers:
- name: wait
image: bitnami/kubectl:latest
command:
- /bin/sh
- -c
- |
# Scale deployment to 0
kubectl scale deployment -n {{ .Release.Namespace }} {{ include "namada-indexer.fullname" . }} --replicas=0
{{- if and .Values.postgresOperator.install.enabled .Values.postgresOperator.enabled }}
# Wait for PostgreSQL Operator to be ready
until kubectl get deployment -n {{ .Release.Namespace }} {{ include "namada-indexer.fullname" . }}-postgres-operator; do
echo "Waiting for PostgreSQL Operator deployment..."
sleep 5
done
# Wait for PostgreSQL StatefulSet to be created
until kubectl get statefulset -n {{ .Release.Namespace }} {{ include "namada-indexer.fullname" . }}-db; do
echo "Waiting for PostgreSQL StatefulSet..."
sleep 5
done
# Wait for PostgreSQL secret
until kubectl get secret -n {{ .Release.Namespace }} namada.{{ include "namada-indexer.fullname" . }}-db.credentials.postgresql.acid.zalan.do; do
echo "Waiting for PostgreSQL secret..."
sleep 5
done
# Wait for PostgreSQL pods to be running
until [ "$(kubectl get pods -n {{ .Release.Namespace }} -l application=spilo --field-selector status.phase=Running -o name | wc -l)" -eq {{ .Values.postgresOperator.numberOfInstances }} ]; do
echo "Waiting for PostgreSQL pods ($(kubectl get pods -n {{ .Release.Namespace }} -l application=spilo --field-selector status.phase=Running -o name | wc -l)/{{ .Values.postgresOperator.numberOfInstances }})..."
sleep 5
done
{{- end }}
{{- if and .Values.redis.install.enabled .Values.redis.enabled }}
# Wait for Redis StatefulSet
until kubectl get statefulset -n {{ .Release.Namespace }} {{ .Release.Name }}-redis-node; do
echo "Waiting for Redis StatefulSet..."
sleep 5
done
# Get expected Redis replicas
REDIS_NODES=$(kubectl get statefulset -n {{ .Release.Namespace }} {{ .Release.Name }}-redis-node -o jsonpath='{.spec.replicas}')
# Wait for Redis nodes to be fully ready (all 3 containers running)
until [ "$(kubectl get pods -n {{ .Release.Namespace }} -l app.kubernetes.io/instance={{ .Release.Name }},app.kubernetes.io/name=redis,app.kubernetes.io/component=node --field-selector status.phase=Running | grep '3/3' | wc -l)" -eq "$REDIS_NODES" ]; do
echo "Waiting for Redis nodes ($(kubectl get pods -n {{ .Release.Namespace }} -l app.kubernetes.io/instance={{ .Release.Name }},app.kubernetes.io/name=redis,app.kubernetes.io/component=node --field-selector status.phase=Running | grep '3/3' | wc -l)/$REDIS_NODES)..."
sleep 5
done
{{- end }}
echo "All dependencies are ready!"
# Scale deployment back to desired replicas
kubectl scale deployment -n {{ .Release.Namespace }} {{ include "namada-indexer.fullname" . }} --replicas={{ .Values.replicaCount }}
echo "✅ Dependencies check completed, deployment scaled up to {{ .Values.replicaCount }} replicas"
exit 0
{{- end }}
4 changes: 3 additions & 1 deletion charts/namada-indexer/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ metadata:
{{- with .Values.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
annotations:
"helm.sh/hook-weight": "-10"
spec:
replicas: {{ .Values.replicaCount }}
strategy:
Expand Down Expand Up @@ -178,4 +180,4 @@ spec:
{{- end }}
{{- end }}
{{- end }}
restartPolicy: OnFailure
restartPolicy: Always
42 changes: 42 additions & 0 deletions charts/namada-indexer/templates/postgresql-cleanup-hook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{{- if and .Values.postgresOperator.install.enabled .Values.postgresOperator.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "namada-indexer.fullname" . }}-db-cleanup
annotations:
"helm.sh/hook": pre-delete
"helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation
spec:
template:
spec:
serviceAccountName: {{ include "namada-indexer.serviceAccountName" . }}
containers:
- name: kubectl
image: bitnami/kubectl:latest
command:
- /bin/sh
- -c
- |
# Delete dependencies job first
kubectl delete job -n {{ .Release.Namespace }} {{ include "namada-indexer.fullname" . }}-wait-dependencies --force --grace-period=0 || true
# Delete any leftover pods from the dependencies job
kubectl delete pods -n {{ .Release.Namespace }} -l job-name={{ include "namada-indexer.fullname" . }}-wait-dependencies --force --grace-period=0 || true
# Force delete the statefulset
kubectl delete statefulset -n {{ .Release.Namespace }} {{ include "namada-indexer.fullname" . }}-db --force --grace-period=0 || true
# Remove finalizers from postgresql CRD
kubectl patch postgresql -n {{ .Release.Namespace }} {{ include "namada-indexer.fullname" . }}-db -p '{"metadata":{"finalizers":null}}' --type=merge || true
# Delete PostgreSQL services
kubectl delete svc -n {{ .Release.Namespace }} \
{{ include "namada-indexer.fullname" . }}-db \
{{ include "namada-indexer.fullname" . }}-db-config \
{{ include "namada-indexer.fullname" . }}-db-repl \
{{ include "namada-indexer.fullname" . }}-db-pooler \
{{ include "namada-indexer.fullname" . }}-db-repl-pooler \
{{ include "namada-indexer.fullname" . }}-db-pooler-repl || true
restartPolicy: Never
backoffLimit: 1
{{- end }}
4 changes: 4 additions & 0 deletions charts/namada-indexer/templates/postgresql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ metadata:
name: {{ include "namada-indexer.fullname" . }}-db
labels:
{{- include "namada-indexer.labels" . | nindent 4 }}
finalizers:
- kubernetes
spec:
teamId: {{ .Values.postgresOperator.teamId }}
deleteOptions:
forcedDeletion: true
volume:
size: {{ .Values.postgresOperator.volume.size }}
{{- if .Values.postgresOperator.volume.storageClass }}
Expand Down
48 changes: 48 additions & 0 deletions charts/namada-indexer/templates/rbac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ include "namada-indexer.fullname" . }}
labels:
{{- include "namada-indexer.labels" . | nindent 4 }}
rules:
# For deployment scaling and management
- apiGroups: ["apps"]
resources: ["deployments", "deployments/scale", "statefulsets"]
verbs: ["get", "list", "watch", "update", "patch", "delete"]

# For PostgreSQL operator and database management
- apiGroups: ["acid.zalan.do"]
resources: ["postgresqls", "postgresqls/status"]
verbs: ["get", "list", "watch", "patch", "delete"]

# For pod and service management
- apiGroups: [""]
resources: ["pods", "services", "secrets", "configmaps", "persistentvolumeclaims"]
verbs: ["get", "list", "watch", "delete", "patch"]

# For job management
- apiGroups: ["batch"]
resources: ["jobs"]
verbs: ["get", "list", "watch", "delete", "create"]

# For connection pooler management
- apiGroups: ["apps"]
resources: ["deployments"]
resourceNames: ["db-connection-pooler"]
verbs: ["get", "list", "watch", "delete"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ include "namada-indexer.fullname" . }}
labels:
{{- include "namada-indexer.labels" . | nindent 4 }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ include "namada-indexer.fullname" . }}
subjects:
- kind: ServiceAccount
name: {{ include "namada-indexer.serviceAccountName" . }}
namespace: {{ .Release.Namespace }}
3 changes: 3 additions & 0 deletions charts/namada-indexer/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ postgresOperator:
parameters:
max_connections: "100"
shared_buffers: "128MB"
resourcePolicy: "delete"

externalPostgres:
enabled: false
Expand Down Expand Up @@ -51,11 +52,13 @@ redis:
enabled: true
size: "8Gi"
storageClass: ""
resourcePolicy: "delete"
master:
persistence:
enabled: true
size: "8Gi"
storageClass: ""
resourcePolicy: "delete"
metrics:
enabled: false
serviceMonitor:
Expand Down

0 comments on commit da2a89e

Please sign in to comment.