Skip to content

Commit

Permalink
Some UX nicities
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmadigan committed Apr 10, 2024
1 parent cd8f0e8 commit f1c2f1a
Showing 1 changed file with 171 additions and 116 deletions.
287 changes: 171 additions & 116 deletions hack/quickstart-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,36 @@

set -e pipefail

check_dependencies() {
# Check for Docker or Podman
if ! command -v docker &>/dev/null && ! command -v podman &>/dev/null; then
echo "Error: neither docker nor podman could be found. Please install docker or podman."
containerRuntime() {
local container_runtime=""
if command -v podman &>/dev/null; then
container_runtime="podman"
elif command -v docker &>/dev/null; then
container_runtime="docker"
else
echo "Neither Docker nor Podman is installed. Exiting..."
exit 1
fi
echo "$container_runtime"
}

# Check for other dependencies
for cmd in kind kubectl; do
if ! command -v $cmd &>/dev/null; then
echo "Error: $cmd could not be found. Please install $cmd."
exit 1
fi
done
dockerBinCmd() {
local network=""
if [ ! -z "${KIND_CLUSTER_DOCKER_NETWORK}" ]; then
network=" --network ${KIND_CLUSTER_DOCKER_NETWORK}"
fi

echo "$CONTAINER_RUNTIME_BIN run --rm -u $UID -v ${TMP_DIR}:${TMP_DIR}${network} -e KUBECONFIG=${TMP_DIR}/kubeconfig --entrypoint=$1 $TOOLS_IMAGE"
}

check_dependencies
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
BOLD='\033[1m'
INFO="${BOLD}${YELLOW}INFO:${NC}"
SUCCESS="${GREEN}${NC}"
FAILURE="${RED}${NC}"

if [ -z $KUADRANT_ORG ]; then
KUADRANT_ORG=${KUADRANT_ORG:="kuadrant"}
Expand All @@ -53,6 +66,59 @@ fi
export TOOLS_IMAGE=quay.io/kuadrant/mgc-tools:latest
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
export TMP_DIR=$SCRIPT_DIR/tmp/mgc
export CONTAINER_RUNTIME_BIN=$(containerRuntime)
export KIND_BIN=kind
export HELM_BIN=helm
export KUSTOMIZE_BIN=$(dockerBinCmd "kustomize")
LOCAL_SETUP_DIR="$(dirname "${BASH_SOURCE[0]}")"

YQ_BIN=$(dockerBinCmd "yq")

KUADRANT_REPO="github.com/${KUADRANT_ORG}/kuadrant-operator.git"
KUADRANT_REPO_RAW="https://raw.githubusercontent.com/${KUADRANT_ORG}/kuadrant-operator/${KUADRANT_REF}"
KUADRANT_DEPLOY_KUSTOMIZATION="${KUADRANT_REPO}/config/deploy?ref=${KUADRANT_REF}"
KUADRANT_GATEWAY_API_KUSTOMIZATION="${KUADRANT_REPO}/config/dependencies/gateway-api?ref=${KUADRANT_REF}"
KUADRANT_ISTIO_KUSTOMIZATION="${KUADRANT_REPO}/config/dependencies/istio/sail?ref=${KUADRANT_REF}"
KUADRANT_CERT_MANAGER_KUSTOMIZATION="${KUADRANT_REPO}/config/dependencies/cert-manager?ref=${KUADRANT_REF}"
KUADRANT_METALLB_KUSTOMIZATION="${KUADRANT_REPO}/config/metallb?ref=${KUADRANT_REF}"
MGC_REPO="github.com/${KUADRANT_ORG}/multicluster-gateway-controller.git"
MGC_ISTIO_KUSTOMIZATION="${MGC_REPO}/config/istio?ref=${MGC_REF}"

# Make temporary directory
mkdir -p ${TMP_DIR}

KUADRANT_CLUSTER_NAME=kuadrant-local
KUADRANT_NAMESPACE=kuadrant-system

info() {
echo -e "${INFO} $1"
}

success() {
echo -e "${SUCCESS} $1"
}

error() {
echo -e "${FAILURE} $1"
}

check_dependencies() {
# Check for Docker or Podman
if ! command -v docker &>/dev/null && ! command -v podman &>/dev/null; then
error "Neither docker nor podman could be found. Please install Docker or Podman."
exit 1
fi

# Check for other dependencies
for cmd in kind kubectl; do
if ! command -v $cmd &>/dev/null; then
error "Error: $cmd could not be found. Please install $cmd."
exit 1
fi
done

success "All dependencies are installed."
}

# Generate MetalLB IpAddressPool for a given network
generate_ip_address_pool() {
Expand Down Expand Up @@ -104,48 +170,25 @@ metadata:
EOF
}

containerRuntime() {
local container_runtime=""
if command -v podman &>/dev/null; then
container_runtime="podman"
elif command -v docker &>/dev/null; then
container_runtime="docker"
else
echo "Neither Docker nor Podman is installed. Exiting..."
exit 1
fi
echo "$container_runtime"
}

export CONTAINER_RUNTIME_BIN=$(containerRuntime)

dockerBinCmd() {
local network=""
if [ ! -z "${KIND_CLUSTER_DOCKER_NETWORK}" ]; then
network=" --network ${KIND_CLUSTER_DOCKER_NETWORK}"
fi

echo "$CONTAINER_RUNTIME_BIN run --rm -u $UID -v ${TMP_DIR}:${TMP_DIR}${network} -e KUBECONFIG=${TMP_DIR}/kubeconfig --entrypoint=$1 $TOOLS_IMAGE"
}

export KIND_BIN=kind
export HELM_BIN=helm
export KUSTOMIZE_BIN=$(dockerBinCmd "kustomize")

requiredENV() {
echo "Enter which DNS provider you will be using (gcp/aws)"
read PROVIDER </dev/tty
if [[ "$PROVIDER" =~ ^(gcp|aws)$ ]]; then
echo "Provider chosen: $PROVIDER."
export DNS_PROVIDER=$PROVIDER
else
echo "Invalid input given. Please enter either 'gcp' or 'aws' (case sensitive)."
exit 1
fi
info "Configuring DNS provider environment variables... 🛰️"

info "You have chosen to set up a DNS provider, which is required for using Kuadrant's DNSPolicy API."
info "Supported DNS providers are AWS Route 53 and Google Cloud DNS."
while true; do
read -r -p "Please enter 'aws' for AWS Route 53, or 'gcp' for Google Cloud DNS: " DNS_PROVIDER
if [[ "$DNS_PROVIDER" =~ ^(aws|gcp)$ ]]; then
info "You have selected the $DNS_PROVIDER DNS provider."
break
else
error "Invalid input. Supported providers are 'aws' and 'gcp' only."
fi
done
export DNS_PROVIDER

if [[ "$PROVIDER" == "aws" ]]; then
if [[ -z "${KUADRANT_AWS_ACCESS_KEY_ID}" ]]; then
echo "Enter an AWS access key ID for an account where you have access to Route53:"
echo "Enter an AWS access key ID for an account where you have access to AWS Route 53:"
read KUADRANT_AWS_ACCESS_KEY_ID </dev/tty
echo "export KUADRANT_AWS_ACCESS_KEY_ID for future executions of the script to skip this step"
fi
Expand All @@ -157,7 +200,7 @@ requiredENV() {
fi

if [[ -z "${KUADRANT_AWS_REGION}" ]]; then
echo "Enter an AWS region (e.g. eu-west-1) for an Account where you have access to Route53:"
echo "Enter an AWS region (e.g. eu-west-1) for an Account where you have access to AWS Route 53:"
read KUADRANT_AWS_REGION </dev/tty
echo "export KUADRANT_AWS_REGION for future executions of the script to skip this step"
fi
Expand Down Expand Up @@ -204,41 +247,11 @@ requiredENV() {
fi
}

configureController() {
postDeployMGCHub ${1} ${2}
}

postDeployMGCHub() {
clusterName=${1}
namespace=${2}
kubectl config use-context kind-${clusterName}
echo "Running post MGC deployment setup on ${clusterName}"

case $DNS_PROVIDER in
aws)
echo "Setting up an AWS Route 53 DNS provider"
setupAWSProvider ${namespace}
;;
gcp)
echo "Setting up a Google Cloud DNS provider"
setupGCPProvider ${namespace}
;;
*)
echo "Unknown DNS provider"
exit
;;
esac
}
# shellcheck shell=bash

# Shared functions between local-setup-mgc and quickstart-setup script

configureMetalLB() {
clusterName=${1}
metalLBSubnet=${2}

kubectl config use-context kind-${clusterName}
echo "Creating MetalLB AddressPool"
cat <<EOF | kubectl apply -f -
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
Expand All @@ -257,8 +270,6 @@ metadata:
EOF
}

# quickstart-setup specific functions

setupAWSProvider() {
local namespace="$1"
if [ -z "$1" ]; then
Expand Down Expand Up @@ -345,86 +356,130 @@ spec:
EOF
}

LOCAL_SETUP_DIR="$(dirname "${BASH_SOURCE[0]}")"
info "📘 Welcome to the Kuadrant Quick Start setup process"

YQ_BIN=$(dockerBinCmd "yq")
info "This script will guide you through setting up a local Kubernetes cluster with the following components:"
info " - Docker or Podman (Container Runtime)"
info " - kind (Kubernetes IN Docker)"
info " - Kuadrant and its dependencies, including:"
info " * Gateway API"
info " * Istio"
info " * Cert-Manager"
info " * MetalLB"
info " - Optional DNS provider setup for Kuadrant's DNSPolicy API"

KUADRANT_REPO="github.com/${KUADRANT_ORG}/kuadrant-operator.git"
KUADRANT_REPO_RAW="https://raw.githubusercontent.com/${KUADRANT_ORG}/kuadrant-operator/${KUADRANT_REF}"
KUADRANT_DEPLOY_KUSTOMIZATION="${KUADRANT_REPO}/config/deploy?ref=${KUADRANT_REF}"
KUADRANT_GATEWAY_API_KUSTOMIZATION="${KUADRANT_REPO}/config/dependencies/gateway-api?ref=${KUADRANT_REF}"
KUADRANT_ISTIO_KUSTOMIZATION="${KUADRANT_REPO}/config/dependencies/istio/sail?ref=${KUADRANT_REF}"
KUADRANT_CERT_MANAGER_KUSTOMIZATION="${KUADRANT_REPO}/config/dependencies/cert-manager?ref=${KUADRANT_REF}"
KUADRANT_METALLB_KUSTOMIZATION="${KUADRANT_REPO}/config/metallb?ref=${KUADRANT_REF}"
MGC_REPO="github.com/${KUADRANT_ORG}/multicluster-gateway-controller.git"
MGC_ISTIO_KUSTOMIZATION="${MGC_REPO}/config/istio?ref=${MGC_REF}"
info "Please ensure you have an internet connection and local admin access to perform installations."

# Make temporary directory
mkdir -p ${TMP_DIR}
while true; do
read -r -p "Are you ready to begin? (y/n) " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo "Setup canceled by user."; exit;;
* ) echo "Please answer yes (y) or no (n).";; # prompt again
esac
done

KUADRANT_CLUSTER_NAME=kuadrant-local
KUADRANT_NAMESPACE=kuadrant-system
info "Starting the Kuadrant setup process... 🚀"

echo "Do you want to set up a DNS provider? (y/N)"
info "Checking prerequisites and dependencies... 🛠️"
check_dependencies


echo "Do you want to set up a DNS provider for use with Kuadrant's DNSPolicy API? (y/n)"
read SETUP_PROVIDER </dev/tty
if [[ "$SETUP_PROVIDER" =~ ^[yY]$ ]]; then
requiredENV
fi

# Kind delete cluster
info "Deleting existing Kubernetes cluster if present... 🗑️"
${KIND_BIN} delete cluster --name ${KUADRANT_CLUSTER_NAME}
success "Existing cluster (if present) deleted successfully."

# Kind create cluster
info "Creating a new Kubernetes cluster... 🌟"
${KIND_BIN} create cluster --name ${KUADRANT_CLUSTER_NAME} --config=- <<<"$(curl -s ${KUADRANT_REPO_RAW}/utils/kind-cluster.yaml)"
kubectl config use-context kind-${KUADRANT_CLUSTER_NAME}
success "Kubernetes cluster created successfully."

# Create namespace
info "Creating the necessary Kubernetes namespaces... 📦"
kubectl create namespace ${KUADRANT_NAMESPACE}
success "Kubernetes namespaces created successfully."

# Install gateway api
echo "Installing Gateway API in ${KUADRANT_CLUSTER_NAME}"
info "Installing Gateway API... 🌉"
kubectl apply -k ${KUADRANT_GATEWAY_API_KUSTOMIZATION}
success "Gateway API installed successfully."

# Install istio
echo "Installing Istio in ${KUADRANT_CLUSTER_NAME}"
info "Installing Istio as a Gateway API provider... ✈️"
if [ "$ISTIO_INSTALL_SAIL" = true ]; then
echo "Installing via Sail"
info "Installing Istio via Sail"
kubectl apply -k ${KUADRANT_ISTIO_KUSTOMIZATION}
kubectl -n istio-system wait --for=condition=Available deployment istio-operator --timeout=300s
kubectl apply -f ${KUADRANT_REPO_RAW}/config/dependencies/istio/sail/istio.yaml
else
# Create CRD first to prevent race condition with creating CR
echo "Installing without Sail"
kubectl kustomize ${MGC_ISTIO_KUSTOMIZATION} | tee ${TMP_DIR}/doctmp
info "Generating Istio configuration... 🛠️"
kubectl kustomize ${MGC_ISTIO_KUSTOMIZATION} > ${TMP_DIR}/doctmp
success "Istio configuration generated."
${YQ_BIN} 'select(.kind == "CustomResourceDefinition")' ${TMP_DIR}/doctmp | kubectl apply -f -
kubectl -n istio-system wait --for=condition=established crd/istiooperators.install.istio.io --timeout=60s
cat ${TMP_DIR}/doctmp | kubectl apply -f -
kubectl -n istio-operator wait --for=condition=Available deployment istio-operator --timeout=300s
fi
success "Istio installed successfully."

# Install cert-manager
echo "Installing cert-manager in ${KUADRANT_CLUSTER_NAME}"
info "Installing cert-manager... 🛡️"
kubectl apply -k ${KUADRANT_CERT_MANAGER_KUSTOMIZATION}
echo "Waiting for cert-manager deployments to be ready"
info "Waiting for cert-manager deployments to be ready"
kubectl -n cert-manager wait --for=condition=Available deployments --all --timeout=300s
success "cert-manager installed successfully."

# Install metallb
echo "Installing metallb in ${KUADRANT_CLUSTER_NAME}"
kubectl apply -k ${KUADRANT_METALLB_KUSTOMIZATION}
echo "Waiting for metallb-system deployments to be ready"
info "Installing MetalLB... 🏗️"
{
kubectl apply -k ${KUADRANT_METALLB_KUSTOMIZATION} 2>&1
} | grep -v "Warning: .* deprecated" || true
kubectl -n metallb-system wait --for=condition=Available deployments controller --timeout=300s
kubectl -n metallb-system wait --for=condition=ready pod --selector=app=metallb --timeout=60s
info "Generating IP address pool for MetalLB..."
generate_ip_address_pool "kind" | kubectl apply -n metallb-system -f -
success "MetalLB installed and IP address pool generated successfully."

# Install kuadrant
echo "Installing Kuadrant in ${KUADRANT_CLUSTER_NAME}"
kubectl apply -k ${KUADRANT_DEPLOY_KUSTOMIZATION} --server-side --validate=false
info "Installing Kuadrant in ${KUADRANT_CLUSTER_NAME}..."
{
kubectl apply -k ${KUADRANT_DEPLOY_KUSTOMIZATION} --server-side --validate=false 2>&1
} | grep -v "Warning: .* deprecated" || true
info "Kuadrant installation applied, configuring ManagedZone if DNS provider is set..."

# Configure managedzone
# Deploy kuadrant
info "Deploying Kuadrant sample configuration..."
kubectl -n ${KUADRANT_NAMESPACE} apply -f ${KUADRANT_REPO_RAW}/config/samples/kuadrant_v1beta1_kuadrant.yaml
success "Kuadrant sample configuration deployed."

info "✨🌟 Setup Complete! Your Kuadrant Quick Start environment has been successfully created. 🌟✨"

info "Here's what has been configured:"
info " - Kubernetes cluster with name '${KUADRANT_CLUSTER_NAME}'"
info " - a Kuadrant namespace 'kuadrant-system'"
info " - Gateway API"
info " - Istio installed $( [ "$ISTIO_INSTALL_SAIL" = true ] && echo "via Sail" || echo "without Sail") as a Gateway API provider"
info " - cert-manager"
info " - MetalLB with configured IP address pool"
info " - Kuadrant components and a sample configuration"
if [ ! -z "$DNS_PROVIDER" ]; then
configureController ${KUADRANT_CLUSTER_NAME} ${KUADRANT_NAMESPACE}
info " - DNS provider set to '${DNS_PROVIDER}'"
fi

# Deploy kuadrant
kubectl -n ${KUADRANT_NAMESPACE} apply -f ${KUADRANT_REPO_RAW}/config/samples/kuadrant_v1beta1_kuadrant.yaml
echo "You are now set up to follow the quick start guide at https://docs.kuadrant.io/kuadrant-operator/doc/user-guides/secure-protect-connect/"
info "Next steps:"
info " - Explore your new Kuadrant environment using 'kubectl get all -n kuadrant-system'."
info " - Head over to the Kuadrant quick start guide for further instructions on how to use Kuadrant with this environment:"
info " 🔗 https://docs.kuadrant.io/kuadrant-operator/doc/user-guides/secure-protect-connect/"

echo ""
info "Thank you for using Kuadrant! If you have any questions or feedback, please reach out to our community."
info "🔗 https://github.com/Kuadrant/"

Check failure on line 485 in hack/quickstart-setup.sh

View workflow job for this annotation

GitHub Actions / Lint

[EOF Newline] reported by reviewdog 🐶 Missing newline Raw Output: hack/quickstart-setup.sh:485: Missing newline

0 comments on commit f1c2f1a

Please sign in to comment.