#!/bin/bash

# Script to create a personal namespace for a user and configures RBAC and quota.
# This namespace is meant for personal experimentation. The user has admin permissions.
# No resource reservation is allowed.

set -euo pipefail

if [[ $# -ne 1 ]]; then
    echo "Wrong number of arguments" >&2
    echo >&2
    echo "Usage: $0 <USERNAME>" >&2
    exit 1
fi

USER_TO_ADD=$1
SCRIPT_DIR=$(readlink --canonicalize $(dirname $0))

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Namespace
metadata:
  name: ${USER_TO_ADD}
EOF
kubectl --namespace ${USER_TO_ADD} apply -f ${SCRIPT_DIR}/quota.yaml
kubectl --namespace ${USER_TO_ADD} apply -f ${SCRIPT_DIR}/default-requests.yaml
cat <<EOF | kubectl --namespace ${USER_TO_ADD} apply -f -
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: admin-${USER_TO_ADD}
subjects:
- kind: User
  name: ${USER_TO_ADD}
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: admin
  apiGroup: rbac.authorization.k8s.io
EOF
