#!/usr/bin/env bash
WALLAROO_GCP_PROJECT=wallaroo
WALLAROO_CLUSTER=wallaroo # 
WALLAROO_GCP_REGION=us-central1
WALLAROO_PERSISTENT_ZONE=us-central1-f
WALLAROO_GCP_NETWORK_NAME=wallaroo-network
GENERAL_VM_SIZE=n2d-custom-8-16384
PERSISTENT_VM_SIZE=n2d-custom-8-16384
PIPELINES_VM_SIZE=c3-highcpu-8

# Some machine types aren't in every zone. You may need to specify zones using --node-locations
# gcloud compute machine-types list --filter="name=machine-type" can help find the zones where your machine type is available
gcloud compute networks \
  create $WALLAROO_GCP_NETWORK_NAME \
  --bgp-routing-mode regional \
  --subnet-mode custom

# if creating a private cluster you need a nat router and config to access the internet
ROUTER_NAME=wallaroo-nat-router

gcloud compute routers create $ROUTER_NAME \
    --network $WALLAROO_GCP_NETWORK_NAME \
    --region $WALLAROO_GCP_REGION

gcloud compute routers nats create wallaroo-nat-config \
    --router-region $WALLAROO_GCP_REGION \
    --router $ROUTER_NAME \
    --nat-all-subnet-ip-ranges \
    --auto-allocate-nat-external-ips

# the last 3 lines make this a private cluster so the nodes don't get public ip addresses
gcloud container clusters \
  create $WALLAROO_CLUSTER \
  --region $WALLAROO_GCP_REGION \
  --node-labels=wallaroo.ai/node-purpose=general \
  --machine-type $GENERAL_VM_SIZE \
  --enable-autoscaling \
  --num-nodes=1 \
  --disk-size=100 \
  --total-min-nodes=1 \
  --total-max-nodes=3 \
  --network $WALLAROO_GCP_NETWORK_NAME \
  --create-subnetwork "" \
  --enable-ip-alias \
  --cluster-version=1.29 \
  --no-enable-master-authorized-networks \
  --enable-private-nodes \
  --master-ipv4-cidr 172.16.0.32/28

# create the persistent node pool
gcloud container node-pools create persistent \
  --cluster=$WALLAROO_CLUSTER \
  --region $WALLAROO_GCP_REGION \
  --machine-type=$PERSISTENT_VM_SIZE \
  --enable-autoscaling \
  --num-nodes=1 \
  --disk-size=100 \
  --total-min-nodes=1 \
  --total-max-nodes=3 \
  --node-locations $WALLAROO_PERSISTENT_ZONE \
  --node-labels=wallaroo.ai/node-purpose=persistent \
  --node-taints wallaroo.ai/persistent=true:NoSchedule

# create the pipelines-x86 node pool
gcloud container node-pools create pipelines-x86 \
  --cluster=$WALLAROO_CLUSTER \
  --region $WALLAROO_GCP_REGION \
  --machine-type=$PIPELINES_VM_SIZE \
  --enable-autoscaling \
  --num-nodes=1 \
  --disk-size=100 \
  --total-min-nodes=2 \
  --total-max-nodes=3 \
  --node-locations us-central1-a,us-central1-b,us-central1-c \
  --node-labels=wallaroo.ai/node-purpose=pipelines \
  --node-taints wallaroo.ai/pipelines=true:NoSchedule

# optional GPU node pool
# A100
# gpu nodes need the machine type to support a gpu and to specify the accelerator field
# GKE adds the nvidia.com/gpu taint automatically
# gcloud container node-pools create pipelines-a100 \
#   --cluster=$WALLAROO_CLUSTER \
#   --region $WALLAROO_GCP_REGION \
#   --machine-type=a2-highgpu-1g \
#   --accelerator "type=nvidia-tesla-a100,count=1,gpu-driver-version=default" \
#   --enable-autoscaling \
#   --num-nodes=0 \
#   --disk-size=100 \
#   --total-min-nodes=0 \
#   --total-max-nodes=1 \
#   --node-labels=wallaroo.ai/node-purpose=pipelines \
#   --node-labels=wallaroo.ai/accelerator=a100 \
#   --node-taints wallaroo.ai/pipelines=true:NoSchedule

# Optional ARM node pools
# For packaging models for ARM deployment
gcloud container node-pools create general-arm \
  --cluster=$WALLAROO_CLUSTER \
  --region $WALLAROO_GCP_REGION \
  --machine-type=t2a-standard-8 \
  --enable-autoscaling \
  --num-nodes=0 \
  --disk-size=100 \
  --total-min-nodes=0 \
  --total-max-nodes=3 \
  --node-locations us-central1-a,us-central1-b,us-central1-f

# for deploying models on ARM nodes
gcloud container node-pools create pipelines-arm \
  --cluster=$WALLAROO_CLUSTER \
  --region $WALLAROO_GCP_REGION \
  --machine-type=t2a-standard-8 \
  --enable-autoscaling \
  --num-nodes=0 \
  --disk-size=100 \
  --total-min-nodes=0 \
  --total-max-nodes=3 \
  --node-locations us-central1-a,us-central1-b,us-central1-f \
  --node-labels=wallaroo.ai/node-purpose=pipelines \
  --node-taints wallaroo.ai/pipelines=true:NoSchedule