Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor minio addon and upgrade to v1.0 API #1106

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

5 changes: 4 additions & 1 deletion addons/minio/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
apiVersion: v1
description: High Performance, Kubernetes Native Object Storage
name: minio

version: 1.0.0-alpha.0

appVersion: master

keywords:
- storage
- object-storage
Expand All @@ -18,6 +21,6 @@ maintainers:
url: https://github.com/apecloud/kubeblocks/

annotations:
addon.kubeblocks.io/kubeblocks-version: ">=0.8.0"
addon.kubeblocks.io/kubeblocks-version: ">=1.0.0"
addon.kubeblocks.io/model: "object-storage"
addon.kubeblocks.io/provider: "community"
86 changes: 86 additions & 0 deletions addons/minio/scripts-ut-spec/initialize_patch_configmap_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# shellcheck shell=bash
# shellcheck disable=SC2034

# validate_shell_type_and_version defined in shellspec/spec_helper.sh used to validate the expected shell type and version this script needs to run.
if ! validate_shell_type_and_version "bash" 4 &>/dev/null; then
echo "initialize_patch_configmap_spec.sh skip cases because dependency bash version 4 or higher is not installed."
exit 0
fi

Describe "Minio init container bash script Tests"
# load the scripts to be tested and dependencies
Include ../scripts/initialize-patch-configmap.sh

init() {
ut_mode="true"
}
BeforeAll "init"

Describe "get_current_cm_key_value()"
It "returns the value of the specified key from the ConfigMap"
kubectl() {
echo "[1,2,3]"
}

When call get_current_cm_key_value "my-configmap" "my-namespace" "replicas"
The output should eq "1,2,3"
End
End

Describe "update_cm_key_value()"
It "updates the value of the specified key in the ConfigMap"
kubectl() {
return 0
}

When call update_cm_key_value "my-configmap" "my-namespace" "replicas" "[1,2,3,4]"
The status should be success
End
End

Describe "get_cm_key_new_value()"
It "returns the replicas value when cur is empty"
When call get_cm_key_new_value "" "4"
The output should eq "[4]"
End

It "returns the cur value when last equals replicas"
When call get_cm_key_new_value "1,2,3" "3"
The output should eq "[1,2,3]"
End

It "appends the replicas value to cur when last does not equal replicas"
When call get_cm_key_new_value "1,2,3" "4"
The output should eq "[1,2,3,4]"
End
End

Describe "update_configmap()"
setup() {
export MINIO_COMPONENT_NAME="minio"
export CLUSTER_NAMESPACE="default"
export MINIO_COMP_REPLICAS="4"
}
Before "setup"

un_setup() {
unset MINIO_COMPONENT_NAME
unset CLUSTER_NAMESPACE
unset MINIO_COMP_REPLICAS
}
After "un_setup"

It "updates the ConfigMap with the new replicas value"
get_current_cm_key_value() {
echo "1,2,3"
}

update_cm_key_value() {
return 0
}

When run update_configmap
The output should eq "ConfigMap minio-minio-configuration updated successfully with MINIO_REPLICAS_HISTORY=[1,2,3,4]"
End
End
End
146 changes: 146 additions & 0 deletions addons/minio/scripts-ut-spec/startup_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# shellcheck shell=bash
# shellcheck disable=SC2034

# validate_shell_type_and_version defined in shellspec/spec_helper.sh used to validate the expected shell type and version this script needs to run.
if ! validate_shell_type_and_version "bash" 4 &>/dev/null; then
echo "initialize_patch_configmap_spec.sh skip cases because dependency bash version 4 or higher is not installed."
exit 0
fi

Describe "Minio startup bash script tests"
# load the scripts to be tested and dependencies
Include ../scripts/startup.sh

init() {
replicas_history_file="./replicas_history"
ut_mode="true"
}
BeforeAll "init"

cleanup() {
rm -f $replicas_history_file;
}
AfterAll 'cleanup'

Describe "init_buckets()"
setup() {
bucket_dir="./data"
mkdir -p "$bucket_dir"
}
Before "setup"

un_setup() {
rm -rf "$bucket_dir"
}
After "un_setup"

It "creates directories for the specified buckets"
When call init_buckets "bucket1,bucket2,bucket3"
The output should include "Successfully init bucket: $bucket_dir/bucket1"
The output should include "Successfully init bucket: $bucket_dir/bucket2"
The output should include "Successfully init bucket: $bucket_dir/bucket3"
The directory "$bucket_dir/bucket1" should be exist
The directory "$bucket_dir/bucket2" should be exist
The directory "$bucket_dir/bucket3" should be exist
End
End

Describe "read_replicas_history()"
setup() {
replicas_history_file="./replicas_history"
echo "[2,4,6]" > "$replicas_history_file"
}
Before "setup"

un_setup() {
rm -f "$replicas_history_file"
}
After "un_setup"

It "reads the replicas history from the specified file"
When call read_replicas_history "$replicas_history_file"
The output should eq "2,4,6"
End
End

Describe "generate_server_pool()"
setup() {
export HTTP_PROTOCOL="http"
export MINIO_COMPONENT_NAME="minio-minio"
export CLUSTER_NAMESPACE="default"
export CLUSTER_DOMAIN="cluster.local"
}
Before "setup"

un_setup() {
unset HTTP_PROTOCOL
unset MINIO_COMPONENT_NAME
unset CLUSTER_NAMESPACE
unset CLUSTER_DOMAIN
}
After "un_setup"

It "generates the server pool based on the replicas"
When call generate_server_pool "2,4,6"
The output should eq " http://minio-minio-{0...1}.minio-minio-headless.default.svc.cluster.local/data http://minio-minio-{2...3}.minio-minio-headless.default.svc.cluster.local/data http://minio-minio-{4...5}.minio-minio-headless.default.svc.cluster.local/data"
End
End

Describe "build_startup_cmd()"
setup() {
export HTTP_PROTOCOL="http"
export MINIO_COMPONENT_NAME="minio"
export MINIO_BUCKETS="bucket1,bucket2"
export CERTS_PATH="/certs"
export MINIO_API_PORT="9000"
export MINIO_CONSOLE_PORT="9001"
export CLUSTER_DOMAIN="cluster.local"
replicas_history_file="./replicas_history"
echo "[1,3,5]" > "$replicas_history_file"
}
Before "setup"

un_setup() {
unset HTTP_PROTOCOL
unset MINIO_COMPONENT_NAME
unset MINIO_BUCKETS
unset CERTS_PATH
unset MINIO_API_PORT
unset MINIO_CONSOLE_PORT
unset CLUSTER_DOMAIN
rm -f "$replicas_history_file"
}
After "un_setup"

It "builds the startup command with the generated server pool"
init_buckets() {
return 0
}

When call build_startup_cmd
The stderr should include "the minio replicas history is 1,3,5"
The output should eq "/usr/bin/docker-entrypoint.sh minio server http://minio-{0...0}.minio-headless..svc.cluster.local/data http://minio-{1...2}.minio-headless..svc.cluster.local/data http://minio-{3...4}.minio-headless..svc.cluster.local/data -S /certs --address :9000 --console-address :9001"
The status should be success
End

It "returns status 1 when replicas history file does not exist"
replicas_history_file="/nonexistent"

When run build_startup_cmd
The stderr should include "minio config don't existed"
The status should be failure
End
End

Describe "startup()"
It "exits with status 1 when failed to build startup command"
build_startup_cmd() {
return 1
}

When run startup
The stderr should include "Failed to build startup command"
The status should be failure
End
End
End
57 changes: 57 additions & 0 deletions addons/minio/scripts/initialize-patch-configmap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/sh

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

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

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

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

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

if [ -z "$cur" ]; then
printf "[%s]" "$replicas"
else
last=$(echo "$cur" | awk -F, '{print $NF}')
if [ "$last" = "$replicas" ]; then
printf "[%s]" "$cur"
else
printf "[%s,%s]" "$cur" "$replicas"
fi
fi
}

update_configmap() {
name="$MINIO_COMPONENT_NAME-minio-configuration"
namespace="$CLUSTER_NAMESPACE"
key="MINIO_REPLICAS_HISTORY"
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
Loading
Loading