From cd63c74660fee4f23a61f8c3dc3f3214dd3b52b9 Mon Sep 17 00:00:00 2001 From: ducoterra Date: Mon, 25 Jan 2021 19:56:08 -0500 Subject: [PATCH] separate adduser and createproject --- createuserspace.sh => adduser.sh | 2 +- createprojectspace.sh | 45 ++++++++++++++++++++++++++++ namespace/templates/rolebinding.yaml | 2 +- namespace/values.yaml | 1 + 4 files changed, 48 insertions(+), 2 deletions(-) rename createuserspace.sh => adduser.sh (96%) create mode 100755 createprojectspace.sh diff --git a/createuserspace.sh b/adduser.sh similarity index 96% rename from createuserspace.sh rename to adduser.sh index df0a5c5..1c0f853 100755 --- a/createuserspace.sh +++ b/adduser.sh @@ -38,7 +38,7 @@ if [ $? -ne 0 ]; then fi echo "Templating namespace with helm and copying to server" -helm template $USER ./namespace | ssh $SERVER "cat - > $SERVER_USER_DIR/namespace.yaml" +helm template $USER --set user=$USER ./namespace | ssh $SERVER "cat - > $SERVER_USER_DIR/namespace.yaml" if [ $? -ne 0 ]; then echo "Failed to template namespace. Is helm installed?" diff --git a/createprojectspace.sh b/createprojectspace.sh new file mode 100755 index 0000000..c658cbe --- /dev/null +++ b/createprojectspace.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +export PROJECT=$1 +export USER=$2 +export SERVER=$3 + +export SERVER_PROJECT_DIR="~/.kube/projects/$PROJECT" + +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 "adding server to config with new context $SERVER-$USER" +kubectl config set-context $SERVER-$USER-$PROJECT --cluster=$SERVER --namespace=$PROJECT --user=$USER-$SERVER +kubectl config set current-context $SERVER-$USER-$PROJECT +echo "done" \ No newline at end of file diff --git a/namespace/templates/rolebinding.yaml b/namespace/templates/rolebinding.yaml index dd3b627..171e990 100644 --- a/namespace/templates/rolebinding.yaml +++ b/namespace/templates/rolebinding.yaml @@ -5,7 +5,7 @@ metadata: namespace: {{ .Release.Name }} subjects: - kind: User - name: {{ .Release.Name }} + name: {{ .Values.user }} apiGroup: "" roleRef: kind: Role diff --git a/namespace/values.yaml b/namespace/values.yaml index e69de29..7c38eaa 100644 --- a/namespace/values.yaml +++ b/namespace/values.yaml @@ -0,0 +1 @@ +user: admin \ No newline at end of file