Skip to content

Commit

Permalink
refactor minio addon
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Rookie committed Oct 18, 2024
1 parent 7ba24ff commit 898ad0f
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 245 deletions.
1 change: 0 additions & 1 deletion addons-cluster/minio/templates/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ metadata:
labels: {{ include "kblib.clusterLabels" . | nindent 4 }}
spec:
terminationPolicy: {{ .Values.extra.terminationPolicy }}
{{ include "kblib.affinity" . | indent 2 }}
componentSpecs:
- componentDef: minio
name: minio
Expand Down
1 change: 0 additions & 1 deletion addons-cluster/minio/templates/rbac.yaml

This file was deleted.

58 changes: 58 additions & 0 deletions addons/minio/scripts/initialize-patch-configmap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

get_current_cm_key_value() {
local name=$1
local namespace=$2
local key=$3

kubectl get configmaps "$name" -n "$namespace" -o jsonpath="{.data.$key}" | tr -d '[]'
}

update_cm_key_value() {
local name=$1
local namespace=$2
local key=$3
local new_value=$4

kubectl patch configmap "$name" -n "$namespace" --type strategic -p "{\"data\":{\"$key\":\"$new_value\"}}"
}

get_cm_key_new_value() {
local cur=$1
local replicas=$2

if [[ -z "$cur" ]]; then
echo "[$replicas]"
else
IFS=',' read -ra array <<< "$cur"
last=${array[-1]}
if [[ "$last" == "$replicas" ]]; then
echo "[$cur]"
else
echo "[$cur,$replicas]"
fi
fi
}

update_configmap() {
local name="$MINIO_COMPONENT_NAME-minio-configuration"
local namespace="$CLUSTER_NAMESPACE"
local key="MINIO_REPLICAS_HISTORY"
local replicas="$MINIO_COMP_REPLICAS"

cur=$(get_current_cm_key_value "$name" "$namespace" "$key")
new=$(get_cm_key_new_value "$cur" "$replicas")

update_cm_key_value "$name" "$namespace" "$key" "$new"
echo "ConfigMap $name updated successfully with $key=$new"
}

# This is magic for shellspec ut framework.
# Sometime, functions are defined in a single shell script.
# You will want to test it. but you do not want to run the script.
# When included from shellspec, __SOURCED__ variable defined and script
# end here. The script path is assigned to the __SOURCED__ variable.
${__SOURCED__:+false} : || return 0

# main
update_configmap
71 changes: 71 additions & 0 deletions addons/minio/scripts/startup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

replicas_history_file="/minio-config/MINIO_REPLICAS_HISTORY"
bucket_dir="/data"

init_buckets() {
local buckets=$1
IFS=',' read -ra BUCKET_ARRAY <<< "$buckets"
for bucket in "${BUCKET_ARRAY[@]}"; do
directory="$bucket_dir/$bucket"
if mkdir -p "$directory"; then
echo "Successfully init bucket: $directory"
else
echo "Failed to init bucket: $directory"
fi
done
}

read_replicas_history() {
local file=$1
content=$(cat "$file")
content=$(echo "$content" | tr -d '[]')
IFS=',' read -r -a replicas <<< "$content"
echo "${replicas[@]}"
}

generate_server_pool() {
local replicas=("$@")
local server=""
for ((i=0; i < ${#replicas[@]}; i++)); do
if [ $i -eq 0 ]; then
cur=${replicas[i]}
server+=" $HTTP_PROTOCOL://$MINIO_COMPONENT_NAME-{0...$((cur-1))}.$MINIO_COMPONENT_NAME-headless.$CLUSTER_NAMESPACE.svc.$CLUSTER_DOMAIN/data"
else
prev=${replicas[i-1]}
cur=${replicas[i]}
server+=" $HTTP_PROTOCOL://$MINIO_COMPONENT_NAME-{$((prev))...$((cur-1))}.$MINIO_COMPONENT_NAME-headless.$CLUSTER_NAMESPACE.svc.$CLUSTER_DOMAIN/data"
fi
done
echo "$server"
}

startup() {
if [ ! -f "$replicas_history_file" ]; then
echo "minio config don't existed"
exit 1
fi

buckets="$MINIO_BUCKETS"
if [ -n "$buckets" ]; then
init_buckets "$buckets"
fi

mapfile -t replicas < <(read_replicas_history "$replicas_history_file")
server=$(generate_server_pool "${replicas[@]}")
echo "the minio server pool is $server"

cmd="/usr/bin/docker-entrypoint.sh minio server $server -S $CERTS_PATH --address :$MINIO_API_PORT --console-address :$MINIO_CONSOLE_PORT"
echo "Starting minio server with command: $cmd"
eval "$cmd"
}

# This is magic for shellspec ut framework.
# Sometime, functions are defined in a single shell script.
# You will want to test it. but you do not want to run the script.
# When included from shellspec, __SOURCED__ variable defined and script
# end here. The script path is assigned to the __SOURCED__ variable.
${__SOURCED__:+false} : || return 0

# main
startup
4 changes: 2 additions & 2 deletions addons/minio/templates/_helper_create_bucket.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/sh
set -e ; # Have script exit in the event of a failed command.

{{- if .Values.configPathmc }}
MC_CONFIG_DIR="{{ .Values.configPathmc }}"
{{- if .Values.mcConfigPath }}
MC_CONFIG_DIR="{{ .Values.mcConfigPath }}"
MC="/usr/bin/mc --insecure --config-dir ${MC_CONFIG_DIR}"
{{- else }}
MC="/usr/bin/mc --insecure"
Expand Down
162 changes: 23 additions & 139 deletions addons/minio/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -52,162 +52,46 @@ app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Return the appropriate apiVersion for poddisruptionbudget.
Common minio annotations
*/}}
{{- define "minio.pdb.apiVersion" -}}
{{- if semverCompare "<1.21-0" .Capabilities.KubeVersion.Version -}}
{{- print "policy/v1beta1" -}}
{{- else -}}
{{- print "policy/v1" -}}
{{- end -}}
{{- end -}}

{{/*
Return the appropriate apiVersion for networkpolicy.
*/}}
{{- define "minio.networkPolicy.apiVersion" -}}
{{- if semverCompare ">=1.4-0, <1.7-0" .Capabilities.KubeVersion.Version -}}
{{- print "extensions/v1beta1" -}}
{{- else if semverCompare "^1.7-0" .Capabilities.KubeVersion.Version -}}
{{- print "networking.k8s.io/v1beta1" -}}
{{- end -}}
{{- end -}}

{{/*
Return the appropriate apiVersion for deployment.
*/}}
{{- define "minio.deployment.apiVersion" -}}
{{- if semverCompare "<1.9-0" .Capabilities.KubeVersion.Version -}}
{{- print "apps/v1beta2" -}}
{{- else -}}
{{- print "apps/v1" -}}
{{- end -}}
{{- end -}}

{{/*
Return the appropriate apiVersion for statefulset.
*/}}
{{- define "minio.statefulset.apiVersion" -}}
{{- if semverCompare "<1.16-0" .Capabilities.KubeVersion.Version -}}
{{- print "apps/v1beta2" -}}
{{- else -}}
{{- print "apps/v1" -}}
{{- end -}}
{{- end -}}

{{/*
Return the appropriate apiVersion for ingress.
*/}}
{{- define "minio.ingress.apiVersion" -}}
{{- if semverCompare "<1.14-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "extensions/v1beta1" -}}
{{- else -}}
{{- print "networking.k8s.io/v1beta1" -}}
{{- end -}}
{{- end -}}

{{/*
Determine secret name.
*/}}
{{- define "minio.secretName" -}}
{{- if .Values.existingSecret -}}
{{- .Values.existingSecret }}
{{- else -}}
{{- include "minio.fullname" . -}}
{{- end -}}
{{- end -}}

{{/*
Determine service account name for deployment or statefulset.
*/}}
{{- define "minio.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
{{- default (include "minio.fullname" .) .Values.serviceAccount.name | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- default "default" .Values.serviceAccount.name -}}
{{- end -}}
{{- end -}}
{{- define "minio.annotations" -}}
helm.sh/resource-policy: keep
{{- end }}

{{/*
Determine name for scc role and rolebinding
Generate minio scripts configmap
*/}}
{{- define "minio.sccRoleName" -}}
{{- printf "%s-%s" "scc" (include "minio.fullname" .) | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- define "minio.extend.scripts" -}}
{{- range $path, $_ := $.Files.Glob "scripts/**" }}
{{ $path | base }}: |-
{{- $.Files.Get $path | nindent 2 }}
{{- end }}
{{- end }}

{{/*
Properly format optional additional arguments to Minio binary
Define minio component definition name
*/}}
{{- define "minio.extraArgs" -}}
{{- range .Values.extraArgs -}}
{{ " " }}{{ . }}
{{- end -}}
{{- define "minio.cmpdName" -}}
minio-{{ .Chart.Version }}
{{- end -}}

{{/*
Return the proper Docker Image Registry Secret Names
Define minio component definition regular expression name prefix
*/}}
{{- define "minio.imagePullSecrets" -}}
{{/*
Helm 2.11 supports the assignment of a value to a variable defined in a different scope,
but Helm 2.9 and 2.10 does not support it, so we need to implement this if-else logic.
Also, we can not use a single if because lazy evaluation is not an option
*/}}
{{- if .Values.global }}
{{- if .Values.global.imagePullSecrets }}
imagePullSecrets:
{{- range .Values.global.imagePullSecrets }}
- name: {{ . }}
{{- end }}
{{- else if .Values.imagePullSecrets }}
imagePullSecrets:
{{ toYaml .Values.imagePullSecrets }}
{{- end -}}
{{- else if .Values.imagePullSecrets }}
imagePullSecrets:
{{ toYaml .Values.imagePullSecrets }}
{{- end -}}
{{- define "minio.cmpdRegexpPattern" -}}
^minio-
{{- end -}}

{{/*
Formats volumeMount for Minio tls keys and trusted certs
Define minio scripts template name
*/}}
{{- define "minio.tlsKeysVolumeMount" -}}
{{- if .Values.tls.enabled }}
- name: cert-secret-volume
mountPath: {{ .Values.certsPath }}
{{- end }}
{{- if or .Values.tls.enabled (ne .Values.trustedCertsSecret "") }}
{{- $casPath := printf "%s/CAs" .Values.certsPath | clean }}
- name: trusted-cert-secret-volume
mountPath: {{ $casPath }}
{{- end }}
{{- define "minio.scriptsTplName" -}}
minio-scripts
{{- end -}}

{{/*
Formats volume for Minio tls keys and trusted certs
Define minio config template name
*/}}
{{- define "minio.tlsKeysVolume" -}}
{{- if .Values.tls.enabled }}
- name: cert-secret-volume
secret:
secretName: {{ .Values.tls.certSecret }}
items:
- key: {{ .Values.tls.publicCrt }}
path: public.crt
- key: {{ .Values.tls.privateKey }}
path: private.key
{{- end }}
{{- if or .Values.tls.enabled (ne .Values.trustedCertsSecret "") }}
{{- $certSecret := eq .Values.trustedCertsSecret "" | ternary .Values.tls.certSecret .Values.trustedCertsSecret }}
{{- $publicCrt := eq .Values.trustedCertsSecret "" | ternary .Values.tls.publicCrt "" }}
- name: trusted-cert-secret-volume
secret:
secretName: {{ $certSecret }}
{{- if ne $publicCrt "" }}
items:
- key: {{ $publicCrt }}
path: public.crt
{{- end }}
{{- end }}
{{- define "minio.configTplName" -}}
minio-configuration
{{- end -}}
Loading

0 comments on commit 898ad0f

Please sign in to comment.