From 98bd12241d3b8d7540535a1deb96dc7af650cfea Mon Sep 17 00:00:00 2001 From: "Benjamin A. Petersen" Date: Tue, 7 Nov 2023 11:21:58 -0500 Subject: [PATCH] extract helpers to lib file and use in various hack scripts --- hack/kind-down.sh | 2 +- hack/kind-up.sh | 8 +++--- hack/lib/carvel_packages/build.sh | 33 ++----------------------- hack/lib/carvel_packages/deploy.sh | 34 +++----------------------- hack/lib/helpers.sh | 34 ++++++++++++++++++++++++++ hack/prepare-for-integration-tests.sh | 35 +++------------------------ hack/prepare-impersonator-on-kind.sh | 16 ++++++------ hack/prepare-supervisor-on-kind.sh | 11 ++------- 8 files changed, 59 insertions(+), 114 deletions(-) create mode 100644 hack/lib/helpers.sh diff --git a/hack/kind-down.sh b/hack/kind-down.sh index 1f510e2c6..b52b9fe92 100755 --- a/hack/kind-down.sh +++ b/hack/kind-down.sh @@ -18,7 +18,7 @@ if [[ "${PINNIPED_USE_LOCAL_KIND_REGISTRY:-}" != "" ]]; then docker network disconnect "kind" "${reg_name}" >/dev/null fi - echo "Stopping container $reg_name ..." + log_note "Stopping container $reg_name ..." docker stop "${reg_name}" >/dev/null # Delete it. diff --git a/hack/kind-up.sh b/hack/kind-up.sh index d626e581b..43d46be89 100755 --- a/hack/kind-up.sh +++ b/hack/kind-up.sh @@ -8,12 +8,14 @@ set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "${ROOT}" +source hack/lib/helpers.sh + if [[ "${PINNIPED_USE_LOCAL_KIND_REGISTRY:-}" != "" ]]; then # Create registry container unless it already exists. reg_name='kind-registry.local' reg_port='5000' if [ "$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" != 'true' ]; then - echo "Running the registry:2 docker image..." + log_note "Running the registry:2 docker image..." docker run \ --detach \ --restart=always \ @@ -25,13 +27,13 @@ fi use_contour_registry="" if [[ "${PINNIPED_USE_CONTOUR:-}" != "" ]]; then - echo "Adding Contour port mapping to Kind config." + log_note "Adding Contour port mapping to Kind config." use_contour_registry="--file=${ROOT}/hack/lib/kind-config/contour-overlay.yaml" fi use_kind_registry="" if [[ "${PINNIPED_USE_LOCAL_KIND_REGISTRY:-}" != "" ]]; then - echo "Adding local registry to Kind config." + log_note "Adding local registry to Kind config." use_kind_registry="--file=${ROOT}/hack/lib/kind-config/kind-registry-overlay.yaml" fi diff --git a/hack/lib/carvel_packages/build.sh b/hack/lib/carvel_packages/build.sh index d068fb7e1..f2a88fde3 100755 --- a/hack/lib/carvel_packages/build.sh +++ b/hack/lib/carvel_packages/build.sh @@ -17,42 +17,13 @@ # set -euo pipefail -# -# Helper functions -# -function log_note() { - GREEN='\033[0;32m' - NC='\033[0m' - if [[ ${COLORTERM:-unknown} =~ ^(truecolor|24bit)$ ]]; then - echo -e "${GREEN}$*${NC}" - else - echo "$*" - fi -} - -function log_error() { - RED='\033[0;31m' - NC='\033[0m' - if [[ ${COLORTERM:-unknown} =~ ^(truecolor|24bit)$ ]]; then - echo -e "🙁${RED} Error: $* ${NC}" - else - echo ":( Error: $*" - fi -} - -function check_dependency() { - if ! command -v "$1" >/dev/null; then - log_error "Missing dependency..." - log_error "$2" - exit 1 - fi -} - # This script is best invoked from the root directory. # It is designed to be passed as --pre-install flag to hack/prepare-for-integration-tests.sh. hack_lib_path="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "${hack_lib_path}/../../" || exit 1 +source hack/lib/helpers.sh + # Check for dependencies check_dependency kbld "Please install kbld. e.g. 'brew tap vmware-tanzu/carvel && brew install kbld' for MacOS" check_dependency imgpkg "Please install imgpkg. e.g. 'brew tap vmware-tanzu/carvel && brew install imgpkg' for MacOS" diff --git a/hack/lib/carvel_packages/deploy.sh b/hack/lib/carvel_packages/deploy.sh index c5cde2773..ea76255f7 100755 --- a/hack/lib/carvel_packages/deploy.sh +++ b/hack/lib/carvel_packages/deploy.sh @@ -14,42 +14,14 @@ # set -euo pipefail -# -# Helper functions -# -function log_note() { - GREEN='\033[0;32m' - NC='\033[0m' - if [[ ${COLORTERM:-unknown} =~ ^(truecolor|24bit)$ ]]; then - echo -e "${GREEN}$*${NC}" - else - echo "$*" - fi -} - -function log_error() { - RED='\033[0;31m' - NC='\033[0m' - if [[ ${COLORTERM:-unknown} =~ ^(truecolor|24bit)$ ]]; then - echo -e "🙁${RED} Error: $* ${NC}" - else - echo ":( Error: $*" - fi -} - -function check_dependency() { - if ! command -v "$1" >/dev/null; then - log_error "Missing dependency..." - log_error "$2" - exit 1 - fi -} - # This script is best invoked from the root directory. # It is designed to be passed as --alternate-deploy flag to hack/prepare-for-integration-tests.sh. hack_lib_path="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$hack_lib_path/../../" || exit 1 +source hack/lib/helpers.sh + + # Expected arguments. app=${1:-"app-argument-not-provided"} tag=${2:-"tag-argument-not-provided"} diff --git a/hack/lib/helpers.sh b/hack/lib/helpers.sh new file mode 100644 index 000000000..4695a793e --- /dev/null +++ b/hack/lib/helpers.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +# Copyright 2023 the Pinniped contributors. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Helper functions +# +function log_note() { + GREEN='\033[0;32m' + NC='\033[0m' + if [[ ${COLORTERM:-unknown} =~ ^(truecolor|24bit)$ ]]; then + echo -e "${GREEN}$*${NC}" + else + echo "$*" + fi +} + +function log_error() { + RED='\033[0;31m' + NC='\033[0m' + if [[ ${COLORTERM:-unknown} =~ ^(truecolor|24bit)$ ]]; then + echo -e "🙁${RED} Error: $* ${NC}" + else + echo ":( Error: $*" + fi +} + +function check_dependency() { + if ! command -v "$1" >/dev/null; then + log_error "Missing dependency..." + log_error "$2" + exit 1 + fi +} diff --git a/hack/prepare-for-integration-tests.sh b/hack/prepare-for-integration-tests.sh index b1f353087..7159b9a15 100755 --- a/hack/prepare-for-integration-tests.sh +++ b/hack/prepare-for-integration-tests.sh @@ -17,36 +17,10 @@ # set -euo pipefail -# -# Helper functions -# -function log_note() { - GREEN='\033[0;32m' - NC='\033[0m' - if [[ ${COLORTERM:-unknown} =~ ^(truecolor|24bit)$ ]]; then - echo -e "${GREEN}$*${NC}" - else - echo "$*" - fi -} +pinniped_path="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$pinniped_path" || exit 1 -function log_error() { - RED='\033[0;31m' - NC='\033[0m' - if [[ ${COLORTERM:-unknown} =~ ^(truecolor|24bit)$ ]]; then - echo -e "🙁${RED} Error: $* ${NC}" - else - echo ":( Error: $*" - fi -} - -function check_dependency() { - if ! command -v "$1" >/dev/null; then - log_error "Missing dependency..." - log_error "$2" - exit 1 - fi -} +source hack/lib/helpers.sh # # Handle argument parsing and help message @@ -152,9 +126,6 @@ if [[ "$help" == "yes" ]]; then exit 1 fi -pinniped_path="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" -cd "$pinniped_path" || exit 1 - # # Check for dependencies # diff --git a/hack/prepare-impersonator-on-kind.sh b/hack/prepare-impersonator-on-kind.sh index 3734f16f1..0a17cfecc 100755 --- a/hack/prepare-impersonator-on-kind.sh +++ b/hack/prepare-impersonator-on-kind.sh @@ -26,6 +26,8 @@ LOCAL_HOST="127.0.0.1:${LOCAL_PORT}" ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$ROOT" +source hack/lib/helpers.sh + # Build the CLI for use later in the script. go build ./cmd/pinniped @@ -85,10 +87,10 @@ EOF # Wait for the CredentialIssuer's impersonator status to update to be successful. while [[ -z "$(kubectl get credentialissuer pinniped-concierge-config -o json | jq '.status.strategies[] | select((.type=="ImpersonationProxy") and (.status=="Success"))')" ]]; do - echo "Waiting for a successful ImpersonationProxy strategy on CredentialIssuer..." + log_note "Waiting for a successful ImpersonationProxy strategy on CredentialIssuer..." sleep 2 done -echo "Impersonator is available on https://${LOCAL_HOST}" +log_note "Impersonator is available on https://${LOCAL_HOST}" # Make the impersonation proxy's port from the inside the cluster available locally. kubectl port-forward -n $CONCIERGE_NAMESPACE deployment/$CONCIERGE_DEPLOYMENT ${LOCAL_PORT}:${IMPERSONATION_PROXY_PORT} & @@ -97,12 +99,12 @@ port_forward_pid=$! # Kill the kubectl port-forward command whenever the script is control-c cancelled or otherwise ends. function cleanup() { echo - echo "Cleaning up cluster resources..." + log_note "Cleaning up cluster resources..." kubectl delete secret -n $LOCAL_USER_AUTHENTICATOR_NAMESPACE pinny-the-seal kubectl delete configmap -n $CONCIERGE_NAMESPACE pinniped-concierge-impersonation-proxy-config kubectl delete clusterrolebinding pinny-the-seal-can-edit kubectl delete webhookauthenticator local-user-authenticator - echo "Stopping kubectl port-forward and exiting..." + log_note "Stopping kubectl port-forward and exiting..." # It may have already shut down, so ignore errors. kill -9 $port_forward_pid &> /dev/null || true } @@ -113,7 +115,7 @@ trap cleanup EXIT --static-token "pinny-the-seal:password123" \ --concierge-mode ImpersonationProxy >/tmp/kubeconfig -echo -echo 'Ready. In another tab, use "kubectl --kubeconfig /tmp/kubeconfig " to make requests through the impersonation proxy.' -echo "When done, cancel with ctrl-C to clean up." +log_note +log_note 'Ready. In another tab, use "kubectl --kubeconfig /tmp/kubeconfig " to make requests through the impersonation proxy.' +log_note "When done, cancel with ctrl-C to clean up." wait $port_forward_pid diff --git a/hack/prepare-supervisor-on-kind.sh b/hack/prepare-supervisor-on-kind.sh index ba6443886..5a0cb7202 100755 --- a/hack/prepare-supervisor-on-kind.sh +++ b/hack/prepare-supervisor-on-kind.sh @@ -37,15 +37,8 @@ set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$ROOT" -function log_error() { - RED='\033[0;31m' - NC='\033[0m' - if [[ ${COLORTERM:-unknown} =~ ^(truecolor|24bit)$ ]]; then - echo -e "🙁${RED} Error: $* ${NC}" - else - echo ":( Error: $*" - fi -} +source hack/lib/helpers.sh + use_oidc_upstream=no use_ldap_upstream=no