|
| 1 | +{{- if .Values.defaultBuckets }} |
| 2 | +apiVersion: v1 |
| 3 | +kind: ConfigMap |
| 4 | +metadata: |
| 5 | + name: {{ include "minio.fullname" . }}-post-job |
| 6 | + namespace: {{ .Release.Namespace }} |
| 7 | + labels: |
| 8 | + {{- include "minio.labels" . | nindent 4 }} |
| 9 | + {{- if .Values.commonAnnotations }} |
| 10 | + annotations: |
| 11 | + {{- include "minio.annotations" . | nindent 4 }} |
| 12 | + {{- end }} |
| 13 | +data: |
| 14 | + provision-buckets.sh: | |
| 15 | + #!/bin/sh |
| 16 | + set -e |
| 17 | +
|
| 18 | + MINIO_URL="http://{{ include "minio.fullname" . }}.{{ .Release.Namespace }}.svc.cluster.local:{{ .Values.service.port }}" |
| 19 | + MINIO_STARTUP_TIMEOUT="${MINIO_STARTUP_TIMEOUT:-300}" |
| 20 | +
|
| 21 | + echo "=== MinIO Bucket Provisioning ===" |
| 22 | + echo "Target: $MINIO_URL" |
| 23 | +
|
| 24 | + ######################## |
| 25 | + # Check if MinIO is live |
| 26 | + ######################## |
| 27 | + is_minio_live() { |
| 28 | + local status_code |
| 29 | + status_code=$(curl --write-out '%{http_code}' --silent --output /dev/null "${MINIO_URL}/minio/health/live") |
| 30 | + if [ "$status_code" = "200" ]; then |
| 31 | + return 0 |
| 32 | + else |
| 33 | + return 1 |
| 34 | + fi |
| 35 | + } |
| 36 | +
|
| 37 | + ######################## |
| 38 | + # Wait for MinIO start |
| 39 | + ######################## |
| 40 | + wait_for_minio() { |
| 41 | + local waited_time |
| 42 | + waited_time=0 |
| 43 | + echo "Waiting for MinIO to be ready..." |
| 44 | + while ! is_minio_live && [ "$waited_time" -lt "$MINIO_STARTUP_TIMEOUT" ]; do |
| 45 | + echo "MinIO not ready yet, waiting... (${waited_time}s/${MINIO_STARTUP_TIMEOUT}s)" |
| 46 | + sleep 5 |
| 47 | + waited_time=$((waited_time + 5)) |
| 48 | + done |
| 49 | + |
| 50 | + if [ "$waited_time" -ge "$MINIO_STARTUP_TIMEOUT" ]; then |
| 51 | + echo "Timeout: MinIO not ready after ${MINIO_STARTUP_TIMEOUT}s" |
| 52 | + return 1 |
| 53 | + fi |
| 54 | + echo "MinIO is ready!" |
| 55 | + } |
| 56 | +
|
| 57 | + ######################## |
| 58 | + # MinIO client configuration |
| 59 | + ######################## |
| 60 | + configure_minio_client() { |
| 61 | + echo "Configuring MinIO client..." |
| 62 | + mc alias set local "$MINIO_URL" "${MINIO_ROOT_USER}" "${MINIO_ROOT_PASSWORD}" |
| 63 | + |
| 64 | + echo "Testing MinIO client connection..." |
| 65 | + mc ready local |
| 66 | + } |
| 67 | +
|
| 68 | + ######################## |
| 69 | + # Create default buckets |
| 70 | + ######################## |
| 71 | + minio_create_default_buckets() { |
| 72 | + if [ -n "{{ .Values.defaultBuckets }}" ]; then |
| 73 | + buckets=$(echo "{{ .Values.defaultBuckets }}" | tr ',;' ' ') |
| 74 | + echo "Creating default buckets..." |
| 75 | + |
| 76 | + for b in $buckets; do |
| 77 | + bucket_name=$(echo "$b" | cut -d':' -f1) |
| 78 | + bucket_policy=$(echo "$b" | cut -s -d':' -f2) |
| 79 | + |
| 80 | + bucket_name=$(echo "$bucket_name" | xargs) |
| 81 | + |
| 82 | + if [ -z "$bucket_name" ]; then |
| 83 | + continue |
| 84 | + fi |
| 85 | + |
| 86 | + echo "Processing bucket: $bucket_name" |
| 87 | + |
| 88 | + if mc ls "local/$bucket_name" >/dev/null 2>&1; then |
| 89 | + echo "Bucket local/$bucket_name already exists, skipping creation." |
| 90 | + else |
| 91 | + echo "Creating bucket: local/$bucket_name" |
| 92 | + {{- if .Values.config.region }} |
| 93 | + mc mb --region "{{ .Values.config.region }}" "local/$bucket_name" |
| 94 | + {{- else }} |
| 95 | + mc mb "local/$bucket_name" |
| 96 | + {{- end }} |
| 97 | + echo "Bucket local/$bucket_name created successfully." |
| 98 | + fi |
| 99 | + |
| 100 | + if [ -n "$bucket_policy" ]; then |
| 101 | + echo "Setting policy $bucket_policy for local bucket $bucket_name" |
| 102 | + mc anonymous set "$bucket_policy" "local/$bucket_name" |
| 103 | + fi |
| 104 | + done |
| 105 | + fi |
| 106 | + } |
| 107 | +
|
| 108 | + ######################## |
| 109 | + # Main execution |
| 110 | + ######################## |
| 111 | + |
| 112 | + wait_for_minio |
| 113 | + |
| 114 | + configure_minio_client |
| 115 | + |
| 116 | + minio_create_default_buckets |
| 117 | + |
| 118 | + echo "Bucket provisioning completed successfully." |
| 119 | +{{- end }} |
0 commit comments