Skip to content

Commit 79ba441

Browse files
feat: add flag to control execution of secret generation job and allo… (#56)
* feat: add flag to control execution of secret generation job and allow for manual creation * run pre-commit --------- Co-authored-by: Ore Olarewaju <ore.olarewaju@aleph-alpha.com>
1 parent 689a868 commit 79ba441

21 files changed

+484
-164
lines changed

helm/README.md

Lines changed: 309 additions & 11 deletions
Large diffs are not rendered by default.

helm/qs-minio/templates/_helpers.tpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,3 @@ spec:
159159
runAsUser: 1001
160160
fsGroup: 1001
161161
{{- end }}
162-

helm/qs-minio/templates/configmap-secret-creation-script.yaml

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ metadata:
1313
data:
1414
create-secrets.sh: |
1515
#!/bin/bash
16-
16+
1717
# Set error handling
1818
[[ "${FAIL_ON_ERROR}" == "true" ]] && set -e
1919
@@ -67,17 +67,17 @@ data:
6767
local endpoint_url=$2
6868
local username=$3
6969
local user_key=$4
70-
70+
7171
local existing_endpoint=$(kubectl get secret "$secret_name" -o jsonpath='{.data.endpointUrl}' 2>/dev/null | base64 -d)
7272
local existing_username=$(kubectl get secret "$secret_name" -o jsonpath="{.data.$user_key}" 2>/dev/null | base64 -d)
7373
local existing_minio_label=$(kubectl get secret "$secret_name" -o jsonpath='{.metadata.labels.qs-minio/instance}' 2>/dev/null)
74-
74+
7575
local needs_update=false
76-
76+
7777
if [[ "$existing_endpoint" != "$endpoint_url" ]]; then print_warning "Endpoint URL changed: '$existing_endpoint' -> '$endpoint_url'" >&2; needs_update=true; fi
7878
if [[ "$existing_username" != "$username" ]]; then print_warning "Username changed or missing: '$existing_username' -> '$username'" >&2; needs_update=true; fi
7979
if [[ "$existing_minio_label" != "${INSTANCE_NAME}" ]]; then print_warning "Instance label changed" >&2; needs_update=true; fi
80-
80+
8181
echo "$needs_update"
8282
}
8383
@@ -91,14 +91,14 @@ data:
9191
local password_key=$6
9292
local action=$7
9393
local bucket=$8 # Optional bucket parameter
94-
94+
9595
local base_args="--from-literal=$user_key=$username --from-literal=$password_key=$password --from-literal=endpointUrl=$host"
96-
96+
9797
# Add bucket if specified
9898
if [[ -n "$bucket" ]]; then
9999
base_args="$base_args --from-literal=bucket=$bucket"
100100
fi
101-
101+
102102
if kubectl create secret generic "$secret_name" $base_args --dry-run=client -o yaml | \
103103
kubectl label --local -f - app.kubernetes.io/name="${APP_NAME}" qs-minio/instance="${INSTANCE_NAME}" -o yaml | \
104104
kubectl apply -f - > /dev/null 2>&1; then
@@ -118,49 +118,49 @@ data:
118118
return 1
119119
fi
120120
}
121-
121+
122122
# Parse bucket name from bucket specification (removes policy suffix)
123123
# Format can be: "bucket-name" or "bucket-name:policy"
124124
parse_bucket_name() {
125125
local bucket_spec=$1
126126
echo "${bucket_spec%%:*}"
127127
}
128-
128+
129129
# Create per-bucket secrets
130130
create_bucket_secrets() {
131131
local password=$1
132132
local minio_url=$2
133-
133+
134134
if [[ -z "${DEFAULT_BUCKETS}" ]]; then
135135
print_debug "No default buckets configured, skipping bucket-specific secrets"
136136
return 0
137137
fi
138-
138+
139139
print_status "Creating bucket-specific secrets..."
140-
140+
141141
# Parse buckets - they can be comma, semicolon, or space separated
142142
local buckets="${DEFAULT_BUCKETS}"
143143
buckets="${buckets//,/ }"
144144
buckets="${buckets//;/ }"
145-
145+
146146
for bucket_spec in $buckets; do
147147
if [[ -z "$bucket_spec" ]]; then
148148
continue
149149
fi
150-
150+
151151
# Extract bucket name (strip policy if present)
152152
local bucket_name=$(parse_bucket_name "$bucket_spec")
153153
local bucket_secret_name="${EXISTING_SECRET_NAME}-${bucket_name}"
154-
154+
155155
print_debug "Processing bucket: $bucket_name (from spec: $bucket_spec)"
156-
156+
157157
# Check if bucket secret exists
158158
if [[ "${DRY_RUN}" != "true" ]] && kubectl get secret "$bucket_secret_name" &> /dev/null; then
159159
print_status "Bucket secret '$bucket_secret_name' exists, checking for changes..."
160-
160+
161161
local existing_password=$(kubectl get secret "$bucket_secret_name" -o jsonpath="{.data.$PASSWORD_KEY}" 2>/dev/null | base64 -d)
162162
local needs_update=$(check_secret_changes "$bucket_secret_name" "$minio_url" "$MINIO_USER" "$USER_KEY")
163-
163+
164164
if [[ "$needs_update" == "true" ]]; then
165165
print_status "Updating bucket secret with preserved password"
166166
create_or_update_secret "$bucket_secret_name" "$MINIO_USER" "$existing_password" "$minio_url" "$USER_KEY" "$PASSWORD_KEY" "update" "$bucket_name"
@@ -187,30 +187,30 @@ data:
187187
print_dry_run "DRY-RUN MODE: No actual changes will be made"
188188
echo ""
189189
fi
190-
190+
191191
# Construct full MinIO URL with protocol and port
192192
local minio_url="${MINIO_PROTOCOL}://${MINIO_HOST}:${MINIO_PORT}"
193-
193+
194194
print_status "Processing MinIO secret for instance: ${INSTANCE_NAME}..."
195195
print_debug "MinIO fullname: ${MINIO_FULLNAME}"
196196
print_debug "MinIO URL: ${minio_url}"
197197
print_debug "Secret name: ${EXISTING_SECRET_NAME}"
198198
print_debug "User key: ${USER_KEY}"
199199
print_debug "Password key: ${PASSWORD_KEY}"
200200
print_debug "Default buckets: ${DEFAULT_BUCKETS:-<none>}"
201-
201+
202202
local password=""
203-
203+
204204
# Check if secret exists
205205
if [[ "${DRY_RUN}" != "true" ]] && kubectl get secret "$EXISTING_SECRET_NAME" &> /dev/null; then
206206
print_status "Secret '$EXISTING_SECRET_NAME' exists, checking for changes..."
207-
207+
208208
local existing_password=$(kubectl get secret "$EXISTING_SECRET_NAME" -o jsonpath="{.data.$PASSWORD_KEY}" 2>/dev/null | base64 -d)
209209
password="$existing_password"
210210
local needs_update=$(check_secret_changes "$EXISTING_SECRET_NAME" "$minio_url" "$MINIO_USER" "$USER_KEY")
211-
211+
212212
print_debug " Needs update: $needs_update"
213-
213+
214214
if [[ "$needs_update" == "true" ]]; then
215215
print_status "Updating secret with preserved password"
216216
create_or_update_secret "$EXISTING_SECRET_NAME" "$MINIO_USER" "$existing_password" "$minio_url" "$USER_KEY" "$PASSWORD_KEY" "update"
@@ -230,7 +230,7 @@ data:
230230
create_or_update_secret "$EXISTING_SECRET_NAME" "$MINIO_USER" "$password" "$minio_url" "$USER_KEY" "$PASSWORD_KEY" "create"
231231
fi
232232
fi
233-
233+
234234
# Create bucket-specific secrets if buckets are configured
235235
if [[ -n "${DEFAULT_BUCKETS}" ]]; then
236236
echo ""
@@ -253,15 +253,15 @@ data:
253253
print_status "MinIO Secret Management Job"
254254
print_status "============================"
255255
echo ""
256-
256+
257257
# Validate environment variables
258258
for var in INSTANCE_NAME MINIO_HOST MINIO_USER EXISTING_SECRET_NAME USER_KEY PASSWORD_KEY APP_NAME; do
259259
if [[ -z "${!var}" ]]; then
260260
print_error "$var environment variable is required"
261261
exit 1
262262
fi
263263
done
264-
264+
265265
# Show configuration
266266
print_status "Configuration:"
267267
print_status " - Instance: ${INSTANCE_NAME}"
@@ -272,7 +272,7 @@ data:
272272
print_debug " - MinIO User: ${MINIO_USER}"
273273
print_debug " - Secret Name: ${EXISTING_SECRET_NAME}"
274274
echo ""
275-
275+
276276
# Check required commands
277277
print_status "Checking required commands..."
278278
if ! command -v kubectl &> /dev/null; then
@@ -288,18 +288,18 @@ data:
288288
print_debug "openssl: $(command -v openssl)"
289289
fi
290290
echo ""
291-
291+
292292
# Create secret
293293
create_minio_secret
294294
echo ""
295-
295+
296296
# Final status
297297
if [[ "${DRY_RUN}" == "true" ]]; then
298298
print_success "Dry-run completed for MinIO instance '${INSTANCE_NAME}'!"
299299
else
300300
print_success "Secret processing completed for MinIO instance '${INSTANCE_NAME}'!"
301301
fi
302-
302+
303303
# Exit with error if any secrets failed
304304
if [[ $secrets_failed -gt 0 ]] && [[ "${FAIL_ON_ERROR}" == "true" ]]; then
305305
print_error "Job completed with ${secrets_failed} failure(s)"
@@ -309,4 +309,3 @@ data:
309309
310310
# Run main function
311311
main "$@"
312-

helm/qs-minio/templates/job-cleanup-secrets.yaml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if not .Values.secretCleanup.retainOnDelete }}
1+
{{- if and .Values.secretGenerationJob.enabled (not .Values.secretCleanup.retainOnDelete) }}
22
---
33
apiVersion: batch/v1
44
kind: Job
@@ -31,42 +31,42 @@ spec:
3131
- -c
3232
- |
3333
set -e
34-
34+
3535
echo "================================================"
3636
echo " MinIO Secrets Cleanup"
3737
echo "================================================"
3838
echo
3939
echo "⚠️ Secret retention is DISABLED (secretCleanup.retainOnDelete=false)"
4040
echo "⚠️ All generated secrets will be deleted"
4141
echo
42-
42+
4343
{{- range $key, $value := .Values }}
4444
{{- if and (hasPrefix "minio-" $key) (kindIs "map" $value) }}
4545
{{- if $value.enabled }}
4646
{{- $instanceName := trimPrefix "minio-" $key }}
4747
# Cleanup secrets for MinIO instance: {{ $instanceName }}
4848
echo "Cleaning up secrets for MinIO instance: {{ $instanceName }}"
49-
49+
5050
SECRETS=$(kubectl get secrets -n {{ $.Release.Namespace }} \
5151
-l qs-minio/instance={{ $instanceName }} \
5252
-o jsonpath='{.items[*].metadata.name}')
53-
53+
5454
if [ -n "$SECRETS" ]; then
5555
echo "Found secrets to delete:"
5656
for secret in $SECRETS; do
5757
echo " - $secret"
5858
done
5959
echo
60-
60+
6161
kubectl delete secrets -n {{ $.Release.Namespace }} \
6262
-l qs-minio/instance={{ $instanceName }}
63-
63+
6464
echo "✅ Deleted {{ $instanceName }} secrets"
6565
else
6666
echo "ℹ️ No secrets found for {{ $instanceName }}"
6767
fi
6868
echo
69-
69+
7070
{{- end }}
7171
{{- end }}
7272
{{- end }}
@@ -86,7 +86,10 @@ spec:
8686
fsGroup: 1001
8787
{{- else }}
8888
---
89+
{{- if not .Values.secretGenerationJob.enabled }}
90+
# Secret cleanup is skipped because secret generation job is disabled (secretGenerationJob.enabled=false)
91+
{{- else }}
8992
# Secret cleanup is disabled (secretCleanup.retainOnDelete=true)
9093
# Secrets will be retained after helm uninstall
9194
{{- end }}
92-
95+
{{- end }}

helm/qs-minio/templates/job-create-secrets.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{- if .Values.secretGenerationJob.enabled }}
12
{{- range $key, $value := .Values }}
23
{{- if and (hasPrefix "minio-" $key) (kindIs "map" $value) }}
34
{{- if $value.enabled }}
@@ -7,4 +8,4 @@
78
{{- end }}
89
{{- end }}
910
{{- end }}
10-
11+
{{- end }}

helm/qs-minio/templates/rbac.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,3 @@ subjects:
3737
- kind: ServiceAccount
3838
name: {{ include "qs-minio.serviceAccountName" . }}
3939
namespace: {{ .Release.Namespace }}
40-

helm/qs-minio/templates/serviceaccount.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ metadata:
1616
{{- end }}
1717
automountServiceAccountToken: true
1818
{{- end }}
19-

helm/qs-minio/templates/tests/test-connection.yaml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ spec:
6060
# Use endpoint URL from secret
6161
MINIO_URL="${MINIO_ENDPOINT_URL}"
6262
ALIAS="test-${INSTANCE_NAME}"
63-
63+
6464
# Extract hostname for DNS checks (using shell built-ins)
6565
MINIO_HOST="${MINIO_URL#*://}" # Remove protocol
6666
MINIO_HOST="${MINIO_HOST%%:*}" # Remove port and path
67-
67+
6868
echo " Target: ${MINIO_URL}"
6969
echo " User: ${MINIO_USER}"
7070
echo
@@ -75,13 +75,13 @@ spec:
7575
# Function to check DNS resolution
7676
check_dns() {
7777
echo "🔍 Checking DNS resolution for: ${MINIO_HOST}"
78-
78+
7979
# Try ping first (most likely to be available)
8080
if ping -c 1 -W 2 "${MINIO_HOST}" > /dev/null 2>&1; then
8181
echo "✅ DNS resolution successful (ping)"
8282
return 0
8383
fi
84-
84+
8585
# Try nslookup if available
8686
if command -v nslookup > /dev/null 2>&1; then
8787
if nslookup "${MINIO_HOST}" > /dev/null 2>&1; then
@@ -90,7 +90,7 @@ spec:
9090
return 0
9191
fi
9292
fi
93-
93+
9494
# Try getent if available
9595
if command -v getent > /dev/null 2>&1; then
9696
if getent hosts "${MINIO_HOST}" > /dev/null 2>&1; then
@@ -99,7 +99,7 @@ spec:
9999
return 0
100100
fi
101101
fi
102-
102+
103103
echo "⚠️ Unable to verify DNS resolution with available tools"
104104
echo " Proceeding anyway - MinIO client will fail if host is unreachable"
105105
return 0
@@ -111,15 +111,15 @@ spec:
111111
if command -v wget > /dev/null 2>&1; then
112112
wget --spider --quiet --timeout=5 "${MINIO_URL}/minio/health/live" 2>/dev/null && return 0
113113
fi
114-
114+
115115
# Try curl
116116
if command -v curl > /dev/null 2>&1; then
117117
curl -f -s --connect-timeout 5 --max-time 5 "${MINIO_URL}/minio/health/live" > /dev/null 2>&1 && return 0
118118
fi
119-
119+
120120
# Try using mc (MinIO client itself)
121121
mc alias set test-health "${MINIO_URL}" "${MINIO_USER}" "${MINIO_PASSWORD}" > /dev/null 2>&1 && return 0
122-
122+
123123
return 1
124124
}
125125
@@ -128,11 +128,11 @@ spec:
128128
echo "⏳ Waiting for MinIO to be ready..."
129129
MAX_RETRIES=60
130130
RETRY=0
131-
131+
132132
# First check DNS
133133
check_dns
134134
echo
135-
135+
136136
until check_minio_health || [ $RETRY -eq $MAX_RETRIES ]; do
137137
echo " Waiting... (attempt $((RETRY+1))/${MAX_RETRIES})"
138138
if [ $((RETRY % 6)) -eq 0 ] && [ $RETRY -gt 0 ]; then
@@ -364,4 +364,3 @@ spec:
364364
{{- end }}
365365
{{- end }}
366366
{{- end }}
367-

0 commit comments

Comments
 (0)