diff --git a/shared-functions/shared-code.sh b/shared-functions/shared-code.sh new file mode 100755 index 00000000000..ba8df5780de --- /dev/null +++ b/shared-functions/shared-code.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env bash +# Copyright (C) 2023, MinIO, Inc. +# +# This code is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License, version 3, +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License, version 3, +# along with this program. If not, see + +function wait_for_resource() { + waitdone=0 + totalwait=0 + echo "command to wait on:" + command_to_wait="kubectl -n $1 get pods -l $3=$2 --no-headers" + echo $command_to_wait + + while true; do + waitdone=$($command_to_wait | wc -l) + if [ "$waitdone" -ne 0 ]; then + echo "Found $waitdone pods" + break + fi + sleep 5 + totalwait=$((totalwait + 5)) + if [ "$totalwait" -gt 305 ]; then + echo "Unable to get resource after 5 minutes, exiting." + try false + fi + done +} + +function wait_for_resource_field_selector() { + # example 1 job: + # namespace="minio-tenant-1" + # codition="condition=Complete" + # selector="metadata.name=setup-bucket" + # wait_for_resource_field_selector $namespace job $condition $selector + # + # example 2 tenant: + # wait_for_resource_field_selector $namespace job $condition $selector + # condition=jsonpath='{.status.currentState}'=Initialized + # selector="metadata.name=storage-policy-binding" + # wait_for_resource_field_selector $namespace tenant $condition $selector 900s + + namespace=$1 + resourcetype=$2 + condition=$3 + fieldselector=$4 + if [ $# -ge 5 ]; then + timeout="$5" + else + timeout="600s" + fi + + echo "Waiting for $resourcetype \"$fieldselector\" for \"$condition\" ($timeout timeout)" + kubectl wait -n "$namespace" "$resourcetype" \ + --for=$condition \ + --field-selector $fieldselector \ + --timeout="$timeout" +} diff --git a/testing/common.sh b/testing/common.sh index c46a5d6aa04..bb12bf3998f 100644 --- a/testing/common.sh +++ b/testing/common.sh @@ -13,6 +13,8 @@ # You should have received a copy of the GNU Affero General Public License, version 3, # along with this program. If not, see +source "${GITHUB_WORKSPACE}/shared-functions/shared-code.sh" # This is common.sh for k8s tests across multiple repos. + ## this enables :dev tag for minio/operator container image. CI="true" export CI @@ -181,58 +183,6 @@ function destroy_kind() { fi } -function wait_for_resource() { - waitdone=0 - totalwait=0 - echo "command to wait on:" - command_to_wait="kubectl -n $1 get pods -l $3=$2 --no-headers" - echo $command_to_wait - - while true; do - waitdone=$($command_to_wait | wc -l) - if [ "$waitdone" -ne 0 ]; then - echo "Found $waitdone pods" - break - fi - sleep 5 - totalwait=$((totalwait + 5)) - if [ "$totalwait" -gt 305 ]; then - echo "Unable to get resource after 5 minutes, exiting." - try false - fi - done -} - -function wait_for_resource_field_selector() { - # example 1 job: - # namespace="minio-tenant-1" - # codition="condition=Complete" - # selector="metadata.name=setup-bucket" - # wait_for_resource_field_selector $namespace job $condition $selector - # - # example 2 tenant: - # wait_for_resource_field_selector $namespace job $condition $selector - # condition=jsonpath='{.status.currentState}'=Initialized - # selector="metadata.name=storage-policy-binding" - # wait_for_resource_field_selector $namespace tenant $condition $selector 900s - - namespace=$1 - resourcetype=$2 - condition=$3 - fieldselector=$4 - if [ $# -ge 5 ]; then - timeout="$5" - else - timeout="600s" - fi - - echo "Waiting for $resourcetype \"$fieldselector\" for \"$condition\" ($timeout timeout)" - kubectl wait -n "$namespace" "$resourcetype" \ - --for=$condition \ - --field-selector $fieldselector \ - --timeout="$timeout" -} - function check_tenant_status() { # Check MinIO is accessible # $1 namespace diff --git a/tests/common.sh b/tests/common.sh index 854aef743b6..b3344df47e8 100755 --- a/tests/common.sh +++ b/tests/common.sh @@ -13,6 +13,8 @@ # You should have received a copy of the GNU Affero General Public License, version 3, # along with this program. If not, see +source "${GITHUB_WORKSPACE}/shared-functions/shared-code.sh" # This is common.sh for k8s tests across multiple repos. + yell() { echo "$0: $*" >&2; } die() { @@ -103,28 +105,6 @@ function check_tenant_status() { echo "Done." } -function wait_for_resource() { - waitdone=0 - totalwait=0 - echo "command to wait on:" - command_to_wait="kubectl -n $1 get pods -l $3=$2 --no-headers" - echo $command_to_wait - - while true; do - waitdone=$($command_to_wait | wc -l) - if [ "$waitdone" -ne 0 ]; then - echo "Found $waitdone pods" - break - fi - sleep 5 - totalwait=$((totalwait + 5)) - if [ "$totalwait" -gt 305 ]; then - echo "Unable to get resource after 5 minutes, exiting." - try false - fi - done -} - # Install tenant function is being used by deploy-tenant and check-prometheus function install_tenant() {