#!/bin/bash

CLUSTER=$1
OPERATOR=$1-o
NAMESPACE=$1

echo $CLUSTER
echo $NAMESPACE

echo "Approving outstanding CSRS"

for i in $(kubectl get csr  --sort-by=.metadata.creationTimestamp | grep -E '^csr-' | grep Pending | awk '{print $1}') ; do kubectl certificate approve $i ; done

echo "Creating namespace ${NAMESPACE}"
kubectl create ns ${NAMESPACE}

echo "Adding 'rook-release' helm chart"
helm repo add rook-release "https://charts.rook.io/release"

echo "Setting ${NAMESPACE} to enforce=privileged"
kubectl label ns ${NAMESPACE} pod-security.kubernetes.io/enforce=privileged
kubectl label ns default pod-security.kubernetes.io/enforce=privileged


echo "Updating prod/operator.yaml"
CMD="helm template --create-namespace --namespace ${NAMESPACE} ${CLUSTER} rook-release/rook-ceph --values operator-values.yaml"
echo ${CMD}
$CMD > prod/operator.yaml

echo "Updating prod/cluster.yaml"
CMD="helm template --create-namespace --namespace ${NAMESPACE} ${CLUSTER}-operator --set operatorNamespace=${NAMESPACE} rook-release/rook-ceph-cluster --values ceph-values.yaml"
echo ${CMD}
$CMD > prod/cluster.yaml

