#!/bin/bash # Use # ./createprojectspace export SERVER=$1 export USER=$2 export PROJECT=$3 export SERVER_PROJECT_DIR="~/.kube/projects/$PROJECT" export SERVER_NAME=$(echo "$SERVER" | sed 's/\./-/g') export SERVER_USER="$USER-$SERVER_NAME" echo "Checking if project namespace exists" exists=$(ssh $SERVER "kubectl get namespace --output=jsonpath=\"{.items[?(@.metadata.name=='$PROJECT')].metadata.name}\"") if [ -z $exists ]; then echo "Namespace not found, creating" else echo "Namespace exists, not overwriting" exit 1 fi echo "Creating namespace dir on server" ssh $SERVER "mkdir -p $SERVER_PROJECT_DIR" if [ $? -ne 0 ]; then echo "Failed to create server project dir" exit 1 fi echo "Templating namespace with helm and copying to server" helm template $PROJECT ./namespace --set user=$USER | ssh $SERVER "cat - > $SERVER_PROJECT_DIR/namespace.yaml" if [ $? -ne 0 ]; then echo "Failed to template namespace. Is helm installed?" exit 1 fi echo "Creating namespace from template" ssh $SERVER "kubectl apply -f $SERVER_PROJECT_DIR/namespace.yaml" if [ $? -ne 0 ]; then echo "Failed to create namespace" exit 1 fi echo "Setting config" kubectl config set contexts.$(kubectl config current-context).namespace $PROJECT kubectl config set contexts.$(kubectl config current-context).user $SERVER_USER echo "done"