# Be sure to log in first!
az login

# Set the default location to avoid having to pass it on each command
az configure --defaults location=southcentralus

# Set the default subscription to avoid having to pass it on each command
az account set -s "Visual Studio Premium with MSDN"

# Create a new resource group (optional)
az group create -n OpenFaaS-RG

# Create the AKS cluster
az aks create -n openfaas-aks1 -g OpenFaaS-RG -k 1.12.8 -c 1 --generate-ssh-keys

# Get the kubectl config
az aks get-credentials --resource-group OpenFaaS-RG --name openfaas-aks1

# Set kubctl to the azure config
export KUBECONFIG=<output from above command>

# Verify kubectl
kubectl get nodes

# Helm setup
helm init
helm repo add openfaas https://openfaas.github.io/faas-netes/
helm repo update

# OpenFaaS
kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml

# generate a password
PASSWORD=$(head -c 12 /dev/urandom | shasum| cut -d' ' -f1)

# create the secrets on the cluster
kubectl -n openfaas create secret generic basic-auth --from-literal=basic-auth-user=admin --from-literal=basic-auth-password="$PASSWORD"

# install the openfaas deployment
helm upgrade openfaas --install openfaas/openfaas --namespace openfaas --set basic_auth=true --set functionNamespace=openfaas-fn --set operator.create=true --set serviceType=LoadBalancer

# verify the deployment
kubectl --namespace=openfaas get deployments -l "release=openfaas, app=openfaas"

# get the OpenFaaS gateway's external IP address
kubectl get service -l component=gateway --namespace openfaas

# set the OpenFaaS gateway address
export OPENFAAS_URL=http://<gateway-external ip address>:8080

# login to the openfaas cli
echo $PASSWORD | faas login -u admin -s

# when you're done, deleting the cluster is a single command
az aks delete -n openfaas-aks1 -g OpenFaaS-RG