Skip to content

Commit

Permalink
docker-network-ipaddresspool sourced, check for docker first in conta…
Browse files Browse the repository at this point in the history
…iner runtime check
  • Loading branch information
jasonmadigan committed Apr 12, 2024
1 parent 79860d0 commit fb54211
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 80 deletions.
128 changes: 53 additions & 75 deletions hack/quickstart-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ set -e pipefail

containerRuntime() {
local container_runtime=""
if command -v podman &>/dev/null; then
container_runtime="podman"
elif command -v docker &>/dev/null; then
if command -v docker &>/dev/null; then
container_runtime="docker"
elif command -v podman &>/dev/null; then
container_runtime="podman"
else
echo "Neither Docker nor Podman is installed. Exiting..."
exit 1
Expand Down Expand Up @@ -121,76 +121,38 @@ check_dependencies() {

# Generate MetalLB IpAddressPool for a given network
generate_ip_address_pool() {
set -euo pipefail

networkName=$1
YQ="${2:-yq}"

## Parse kind network subnet
## Take only IPv4 subnets, exclude IPv6
SUBNET=""

# Attempt to fetch network details using Podman
if command -v podman &> /dev/null; then
SUBNET=$(podman network inspect "$networkName" -f '{{range .Subnets}}{{if eq (len .Subnet.IP) 4}}{{.Subnet}}{{end}}{{end}}' 2>/dev/null)
if [[ -z "$SUBNET" ]]; then
echo "Failed to obtain subnet using podman for network '$networkName'. Trying docker instead..." >&2
local network_name="$1"
local script_path="${SCRIPT_DIR}/../utils/docker-network-ipaddresspool.sh"

# interactively or piped
if [ -t 0 ]; then
# interactively
if [ -f "$script_path" ]; then
bash "$script_path" "$network_name"
else
echo "Script file not found at $script_path" >&2
return 1 # Return error code to prevent further execution
fi
else
echo "Podman not found. Trying docker..." >&2
fi

# Fallback to Docker if no subnet was found using Podman
if [[ -z "$SUBNET" ]]; then
SUBNET=$(docker network inspect "$networkName" -f '{{ (index .IPAM.Config 0).Subnet }}' 2>/dev/null)
if [[ -z "$SUBNET" ]]; then
echo "Failed to obtain subnet using docker for network '$networkName'." >&2
fi
fi

# no subnet, error out
if [[ -z "$SUBNET" ]]; then
echo "Error: Failed to parse IPv4 network address for the network named '$networkName' using both podman and docker." >&2
exit 1
# piped
curl -s "${KUADRANT_REPO_RAW}/utils/docker-network-ipaddresspool.sh" | bash -s -- "$network_name"
fi

# Reset error handling to default behavior
set -e

# shellcheck disable=SC2206
subnetParts=(${SUBNET//./ })
cidr="${subnetParts[0]}.${subnetParts[1]}.0.252/30"

cat <<EOF | ADDRESS=$cidr ${YQ} '(select(.kind == "IPAddressPool") | .spec.addresses[0]) = env(ADDRESS)'
---
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: kuadrant-local
spec:
addresses: [] # set by make target
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: empty
EOF
}

requiredENV() {
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

# Read directly from the terminal, ensuring it can handle piped script execution
read -r -p "Please enter 'aws' for AWS Route 53, or 'gcp' for Google Cloud DNS: " DNS_PROVIDER </dev/tty

if [[ "$DNS_PROVIDER" =~ ^(aws|gcp)$ ]]; then
info "You have selected the $DNS_PROVIDER DNS provider."
else
error "Invalid input. Supported providers are 'aws' and 'gcp' only. Exiting."
exit 1 # Exit on any invalid input without retrying
fi
export DNS_PROVIDER

if [[ "$DNS_PROVIDER" == "aws" ]]; then
Expand Down Expand Up @@ -367,17 +329,21 @@ info " - Optional DNS provider setup for Kuadrant's DNSPolicy API"

info "Please ensure you have an internet connection and local admin access to perform installations."

while true; do
read -r -p "Are you ready to begin? (y/n) " yn
case $yn in
[Yy]*) break ;;
read -r -p "Are you ready to begin? (y/n) " yn </dev/tty

case $yn in
[Yy]*)
echo "Starting the setup process..."
;;
[Nn]*)
echo "Setup canceled by user."
exit
exit # Exit if the user responds with 'n'
;;
*)
echo "Invalid input. Exiting."
exit 1 # Exit on any other input
;;
*) echo "Please answer yes (y) or no (n)." ;; # prompt again
esac
done
esac

info "Starting the Kuadrant setup process... 🚀"

Expand All @@ -386,9 +352,21 @@ check_dependencies

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

case $SETUP_PROVIDER in
[Yy]*)
# Call the function to configure environment variables for DNS provider
requiredENV
;;
[Nn]*)
echo "DNS provider setup skipped."
;;
*)
error "Invalid input. Please respond with 'y' or 'n'. Exiting."
exit 1
;;
esac


# Kind delete cluster
info "Deleting existing Kubernetes cluster if present... 🗑️"
Expand Down
10 changes: 5 additions & 5 deletions utils/docker-network-ipaddresspool.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ SUBNET=""
# Try podman version of cmd first. docker alias may be used for podman, so network
# command will be different
set +e
if command -v podman &> /dev/null; then
SUBNET=`podman network inspect -f '{{range .Subnets}}{{if eq (len .Subnet.IP) 4}}{{.Subnet}}{{end}}{{end}}' $networkName`
if command -v podman &>/dev/null; then
SUBNET=$(podman network inspect -f '{{range .Subnets}}{{if eq (len .Subnet.IP) 4}}{{.Subnet}}{{end}}{{end}}' $networkName)
if [[ -z "$SUBNET" ]]; then
echo "Failed to obtain subnet using podman. Trying docker instead..." >&2
fi
Expand All @@ -29,12 +29,12 @@ set -e

# Fallback to docker version of cmd
if [[ -z "$SUBNET" ]]; then
SUBNET=`docker network inspect $networkName -f '{{ (index .IPAM.Config 0).Subnet }}'`
SUBNET=$(docker network inspect $networkName -f '{{ (index .IPAM.Config 0).Subnet }}')
fi
# Neither worked, error out
if [[ -z "$SUBNET" ]]; then
echo "Error: parsing IPv4 network address for '$networkName' docker network"
exit 1
echo "Error: parsing IPv4 network address for '$networkName' docker network"
exit 1
fi

# shellcheck disable=SC2206
Expand Down

0 comments on commit fb54211

Please sign in to comment.