#!/bin/bash ############################################################################## # validate-kiali-cr.sh # # This script can be used to validate a Kiali CR. # # To use this script, you must: # * Have "oc" or "kubectl" # * Be connected to a cluster # * Have cluster-admin rights # ############################################################################## set -u crd() { local crd_file="" # if not specified, use the default location; otherwise, it is either a file or a URL if [ -z "${KIALI_CRD_LOCATION:-}" ]; then local script_root="$(cd "$(dirname "$0")" ; pwd -P)" crd_file="${script_root}/../crd/kiali.io_kialis.yaml" elif [ -f "${KIALI_CRD_LOCATION}" ]; then crd_file="${KIALI_CRD_LOCATION}" fi ([ -n "${crd_file}" ] && cat "${crd_file}" || curl -sL "${KIALI_CRD_LOCATION}") | sed 's/ name: kialis.kiali.io/ name: testkialis.kiali.io/g' | sed 's/ kind: Kiali/ kind: TestKiali/g' | sed 's/ listKind: KialiList/ listKind: TestKialiList/g' | sed 's/ plural: kialis/ plural: testkialis/g' | sed 's/ singular: kiali/ singular: testkiali/g' } # process command line args to override environment _CMD="" while [[ $# -gt 0 ]]; do key="$1" case $key in -ce|--client-exe) CLIENT_EXE="$2" ; shift;shift ;; -crd|--crd-location) KIALI_CRD_LOCATION="$2" ; shift;shift ;; -kcf|--kiali-cr-file) KIALI_CR_FILE="$2" ; shift;shift ;; -kcn|--kiali-cr-name) KIALI_CR_NAME="$2" ; shift;shift ;; -n|--namespace) NAMESPACE="$2" ; shift;shift ;; -pc|--print-crd) PRINT_CRD="$2" ; shift;shift ;; -h|--help) cat </dev/null; then CLIENT_EXE="$(which oc)" echo "Using 'oc' located here: ${CLIENT_EXE}" else if which kubectl &>/dev/null; then CLIENT_EXE="$(which kubectl)" echo "Using 'kubectl' located here: ${CLIENT_EXE}" else echo "ERROR! You do not have 'oc' or 'kubectl' in your PATH. Please install it and retry." exit 1 fi fi else echo "Client executable: ${CLIENT_EXE}" fi if [ -z "${KIALI_CR_FILE:-}" -a -z "${KIALI_CR_NAME:-}" ]; then echo "ERROR! You must specify one of either --kiali-cr-file or --kiali-cr-name" exit 1 fi if [ -n "${KIALI_CR_FILE:-}" -a -n "${KIALI_CR_NAME:-}" ]; then echo "ERROR! You must specify only one of either --kiali-cr-file or --kiali-cr-name" exit 1 fi if [ -n "${KIALI_CR_FILE:-}" -a ! -f "${KIALI_CR_FILE:-}" ]; then echo "ERROR! Kiali CR file is not found: [${KIALI_CR_FILE:-}]" exit 1 fi if [ -n "${KIALI_CR_NAME:-}" ]; then if ! ${CLIENT_EXE} get -n "${NAMESPACE}" kiali "${KIALI_CR_NAME}" &> /dev/null; then echo "ERROR! Kiali CR [${KIALI_CR_NAME}] does not exist in namespace [${NAMESPACE}]" exit 1 fi fi # Make sure we have admin rights to some cluster if ! ${CLIENT_EXE} get namespaces &> /dev/null ; then echo "ERROR! You must be connected to/logged into a cluster" exit 1 fi if [ "$(${CLIENT_EXE} auth can-i create crd --all-namespaces)" != "yes" ]; then echo "ERROR! You must have cluster-admin permissions" exit 1 fi # install the test CRD with the schema if ! echo "$(crd)" | ${CLIENT_EXE} apply --validate=true --wait=true -f - &> /dev/null ; then echo "ERROR! Failed to install the test CRD" # run it again to show the errors echo "$(crd)" | ${CLIENT_EXE} apply --validate=true --wait=true -f - exit 1 fi # wait for the test CRD to be established and then give k8s a few more seconds. # if we don't do this, the validation test may report a false negative. if ! ${CLIENT_EXE} wait --for condition=established --timeout=60s crd/testkialis.kiali.io &> /dev/null ; then echo "WARNING! Test CRD is not established yet. The validation test may not produce accurate results." else for s in 3 2 1; do echo -n "." ; sleep 1 ; done echo fi # validate the CR by creating a test version of it echo "Validating the CR:" echo "----------" if [ -n "${KIALI_CR_FILE:-}" ]; then if ! cat "${KIALI_CR_FILE}" | sed 's/kind: Kiali/kind: TestKiali/g' | sed 's/- kiali.io\/finalizer//g' | kubectl apply -n ${NAMESPACE} -f - ; then echo "----------" echo "ERROR! Validation failed for Kiali CR [${KIALI_CR_FILE}]" exit 1 else echo "----------" echo "Kiali CR [${KIALI_CR_FILE}] is valid." fi else if ! ${CLIENT_EXE} get -n "${NAMESPACE}" kiali "${KIALI_CR_NAME}" -o yaml | sed 's/kind: Kiali/kind: TestKiali/g' | sed 's/- kiali.io\/finalizer//g' | kubectl apply -n "${NAMESPACE}" -f - ; then echo "----------" echo "ERROR! Validation failed for Kiali CR [${KIALI_CR_NAME}] in namespace [${NAMESPACE}]" exit 1 else echo "----------" echo "Kiali CR [${KIALI_CR_NAME}] in namespace [${NAMESPACE}] is valid." fi fi # delete the test CRD (which deletes the test CR along with it) if ! echo "$(crd)" | ${CLIENT_EXE} delete --wait=true -f - &> /dev/null ; then echo "ERROR! Failed to delete the test CRD. You should remove it manually." exit 1 fi