-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathk8s-namespace.sh
executable file
·145 lines (130 loc) · 4.35 KB
/
k8s-namespace.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
# Copyright © 2018. TIBCO Software Inc.
#
# This file is subject to the license terms contained
# in the license file that is distributed with this file.
# create k8s namespace for a specified org,
# if the optional target env is az, create the storage account secret based on config file in $HOME/.azure/store-secret
# usage: k8s-namespace.sh <cmd> [-p <property file>] [-t <env type>]
# where property file is specified in ../config/org_name.env, e.g.
# k8s-namespace.sh create -p netop1 -t az
# use config parameters specified in ../config/netop1.env
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; echo "$(pwd)")"
function printK8sNamespace {
echo "
apiVersion: v1
kind: Namespace
metadata:
name: ${ORG}
labels:
use: hyperledger"
}
# create azure-secret yaml
function printAzureSecretYaml {
user=$(echo -n "${STORAGE_ACCT}" | base64 -w 0)
key=$(echo -n "${STORAGE_KEY}" | base64 -w 0)
echo "---
apiVersion: v1
kind: Secret
metadata:
name: azure-secret
namespace: ${ORG}
type: Opaque
data:
azurestorageaccountname: ${user}
azurestorageaccountkey: ${key}"
}
# set k8s default namespace
function setDefaultNamespace {
local curr=$(kubectl config current-context)
local c_namespace=$(kubectl config view -o=jsonpath="{.contexts[?(@.name=='${curr}')].context.namespace}")
if [ "${c_namespace}" != "${ORG}" ]; then
local c_user=$(kubectl config view -o=jsonpath="{.contexts[?(@.name=='${curr}')].context.user}")
local c_cluster=$(kubectl config view -o=jsonpath="{.contexts[?(@.name=='${curr}')].context.cluster}")
if [ ! -z "${c_cluster}" ]; then
echo "set default kube namespace ${ORG} for cluster ${c_cluster} and user ${c_user}"
kubectl config set-context ${ORG} --namespace=${ORG} --cluster=${c_cluster} --user=${c_user}
kubectl config use-context ${ORG}
else
echo "failed to set default context for namespace ${ORG}"
fi
else
echo "namespace ${ORG} is already set as default"
fi
}
function createNamespace {
${sumd} -p ${DATA_ROOT}/namespace/k8s
echo "check if namespace ${ORG} exists"
kubectl get namespace ${ORG}
if [ "$?" -ne 0 ]; then
echo "create k8s namespace ${ORG}"
printK8sNamespace | ${stee} ${DATA_ROOT}/namespace/k8s/namespace.yaml > /dev/null
kubectl create -f ${DATA_ROOT}/namespace/k8s/namespace.yaml
fi
if [ "${ENV_TYPE}" == "az" ]; then
# create secret for Azure File storage
echo "create Azure storage secret"
printAzureSecretYaml | ${stee} ${DATA_ROOT}/namespace/k8s/azure-secret.yaml > /dev/null
kubectl create -f ${DATA_ROOT}/namespace/k8s/azure-secret.yaml
fi
setDefaultNamespace
}
function deleteNamespace {
kubectl delete -f ${DATA_ROOT}/namespace/k8s/namespace.yaml
if [ "${ENV_TYPE}" == "az" ]; then
kubectl delete -f ${DATA_ROOT}/namespace/k8s/azure-secret.yaml
fi
}
# Print the usage message
function printHelp() {
echo "Usage: "
echo " k8s-namespace.sh <cmd> [-p <property file>] [-t <env type>]"
echo " <cmd> - one of 'create', or 'delete'"
echo " - 'create' - create k8s namespace for the organization defined in network spec; for Azure, also create storage secret"
echo " - 'delete' - delete k8s namespace, for Azure, also delete the storage secret"
echo " -p <property file> - the .env file in config folder that defines network properties, e.g., netop1 (default)"
echo " -t <env type> - deployment environment type: one of 'k8s' (default), 'aws', 'az', or 'gcp'"
echo " k8s-namespace.sh -h (print this message)"
}
ORG_ENV="netop1"
CMD=${1}
shift
while getopts "h?p:t:" opt; do
case "$opt" in
h | \?)
printHelp
exit 0
;;
p)
ORG_ENV=$OPTARG
;;
t)
ENV_TYPE=$OPTARG
;;
esac
done
source $(dirname "${SCRIPT_DIR}")/config/setup.sh ${ORG_ENV} ${ENV_TYPE}
if [ "${ENV_TYPE}" == "az" ]; then
# read secret key for Azure storage account
source ${HOME}/.azure/store-secret
if [ -z "${STORAGE_ACCT}" ] || [ -z "${STORAGE_KEY}" ]; then
echo "Error: 'STORAGE_ACCT' and 'STORAGE_KEY' must be set in ${HOME}/.azure/store-secret for Azure"
exit 1
fi
elif [ "${ENV_TYPE}" == "docker" ]; then
echo "No need to create namespace for docker"
exit 0
fi
case "${CMD}" in
create)
echo "create namespace ${ORG} for: ${ORG_ENV} ${ENV_TYPE}"
createNamespace
;;
delete)
echo "delete namespace ${ORG}: ${ORG_ENV} ${ENV_TYPE}"
deleteNamespace
;;
*)
printHelp
exit 1
esac